1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
|
{-# LANGUAGE OverloadedStrings #-}
module IOSpec where
import Network.Socket hiding (send)
import Test.Hspec
import Network.DNS.IO as DNS
import Network.DNS.Imports
import Network.DNS.Types as DNS
spec :: Spec
spec = describe "send/receive" $ do
it "resolves well with UDP" $ do
sock <- connectedSocket Datagram
-- Google's resolvers support the AD and CD bits
let qry = encodeQuestion 1 (Question "www.mew.org" A) $
adFlag FlagSet <> ednsEnabled FlagClear
send sock qry
ans <- receive sock
identifier (header ans) `shouldBe` 1
it "resolves well with TCP" $ do
sock <- connectedSocket Stream
let qry = encodeQuestion 1 (Question "www.mew.org" A) $
adFlag FlagClear <> cdFlag FlagSet <> doFlag FlagSet
sendVC sock qry
ans <- receiveVC sock
identifier (header ans) `shouldBe` 1
connectedSocket :: SocketType -> IO Socket
connectedSocket typ = do
let hints = defaultHints { addrFamily = AF_INET, addrSocketType = typ, addrFlags = [AI_NUMERICHOST]}
addr:_ <- getAddrInfo (Just hints) (Just "8.8.8.8") (Just "domain")
sock <- socket (addrFamily addr) (addrSocketType addr) (addrProtocol addr)
connect sock $ addrAddress addr
return sock
|