resolve_address )
where
-import Control.Applicative ((<$>))
import Control.Monad (liftM)
import qualified Data.ByteString.Char8 as BS (
append,
import Data.IP (IPv4)
import Network.DNS (
Domain,
- DNSFormat(..),
+ DNSError,
Resolver,
- RDATA(..),
- TYPE(..),
defaultResolvConf,
lookupA,
lookupMX,
lookupNS,
- lookupRaw,
+ lookupNSAuth,
makeResolvSeed,
- rdata,
- rrtype,
withResolver )
-import Test.Framework (Test, testGroup)
-import Test.Framework.Providers.HUnit (testCase)
-import Test.Framework.Providers.QuickCheck2 (testProperty)
-import Test.HUnit (assertEqual)
+import Test.Tasty ( TestTree, testGroup )
+import Test.Tasty.HUnit ( (@?=), testCase )
+import Test.Tasty.QuickCheck ( testProperty )
import Text.Read (readMaybe)
-type LookupResult = (Domain, Maybe [Domain])
-
--- | Perform a query, but take the result from the authority section
--- of the response rather than the answer section. Code shamelessly
--- stolen from Network.DNS.lookup.
-lookup_authority :: Resolver -> Domain -> TYPE -> IO (Maybe [RDATA])
-lookup_authority rlv dom typ = (>>= toRDATA) <$> lookupRaw rlv dom typ
- where
- correct r = rrtype r == typ
- listToMaybe [] = Nothing
- listToMaybe xs = Just xs
- toRDATA = listToMaybe . map rdata . filter correct . authority
-
--- | Like lookupNS, except we take the result from the authority
--- section of the response (as opposed to the answer section).
-lookupNS_authority :: Resolver -> Domain -> IO (Maybe [Domain])
-lookupNS_authority rlv dom = toNS <$> DNS.lookup_authority rlv dom NS
- where
- toNS = fmap (map unTag)
- unTag (RD_NS dm) = dm
- unTag _ = error "lookupNS_authority"
-
+type LookupResult = (Domain, Either DNSError [Domain])
-- | Takes a String representing either a hostname or an IP
-- address. If a hostname was supplied, it is resolved to either an
--- IPv4 or Nothing. If an IP address is supplied, it is returned as an
--- IPv4.
+-- [IPv4] or an error. If an IP address is supplied, it is returned
+-- as a singleton [IPv4].
--
-- Examples:
--
-- >>> resolve_address "example.com"
--- Just 93.184.216.119
+-- Right [93.184.216.119]
-- >>> resolve_address "93.184.216.119"
--- Just 93.184.216.119
+-- Right [93.184.216.119]
--
-resolve_address :: String -> IO (Maybe IPv4)
+resolve_address :: String -> IO (Either DNSError [IPv4])
resolve_address s =
case read_result of
- Just _ -> return read_result
+ Just addr -> return $ Right [addr]
Nothing -> do
default_rs <- makeResolvSeed defaultResolvConf
- withResolver default_rs $ \resolver -> do
- result <- lookupA resolver (BS.pack s)
- return $ case result of
- Just (x:_) -> Just x
- _ -> Nothing
+ withResolver default_rs $ \resolver ->
+ lookupA resolver (BS.pack s)
where
read_result :: Maybe IPv4
read_result = readMaybe s
-- >>> rs <- makeResolvSeed defaultResolvConf
-- >>> let domain = BS.pack "example.com."
-- >>> withResolver rs $ \resolver -> lookupMX' resolver domain
--- ("example.com.",Nothing)
+-- ("example.com.",Right [])
--
lookupMX' :: Resolver -> Domain -> IO LookupResult
lookupMX' resolver domain =
liftM (pair_em . drop_priority) $ lookupMX resolver domain
where
- drop_priority :: Maybe [(Domain, Int)] -> Maybe [Domain]
+ drop_priority :: Either DNSError [(Domain, Int)]
+ -> Either DNSError [Domain]
drop_priority = fmap (map fst)
pair_em :: a -> (Domain, a)
-- them to get a reliable result.
--
-- >>> import Data.List (sort)
+-- >>> import Control.Applicative ((<$>))
+-- >>>
-- >>> let sort_snd (x,y) = (x, sort <$> y)
-- >>> rs <- makeResolvSeed defaultResolvConf
-- >>> let domain = BS.pack "example.com."
-- >>> withResolver rs $ \resolver -> sort_snd <$> lookupNS' resolver domain
--- ("example.com.",Just ["a.iana-servers.net.","b.iana-servers.net."])
+-- ("example.com.",Right ["a.iana-servers.net.","b.iana-servers.net."])
--
lookupNS' :: Resolver -> Domain -> IO LookupResult
lookupNS' resolver domain = do
answer_result <- lookupNS resolver domain
- auth_result <- lookupNS_authority resolver domain
+ auth_result <- lookupNSAuth resolver domain
liftM pair_em $ return $ combine answer_result auth_result
where
pair_em :: a -> (Domain, a)
pair_em = (,) domain
- combine :: (Maybe [Domain]) -> (Maybe [Domain]) -> (Maybe [Domain])
- combine Nothing Nothing = Nothing
- combine m1 Nothing = m1
- combine Nothing m2 = m2
- combine (Just ds1) (Just ds2) = Just (ds1 ++ ds2)
+ combine :: (Either DNSError [Domain])
+ -> (Either DNSError [Domain])
+ -> (Either DNSError [Domain])
+ combine e1 e2 = do
+ l1 <- e1
+ l2 <- e2
+ return (l1 ++ l2)
-- | Perform both normalize_case and normalize_root.
normalize :: Domain -> Domain
-- | Normalize the given name by appending a trailing dot (the DNS
-- root) if necessary.
+--
normalize_root :: Domain -> Domain
normalize_root d
| BS.null d = BS.pack "."
-- | Normalize the given name by lowercasing it.
+--
normalize_case :: Domain -> Domain
normalize_case = BS.map toLower
-test_normalize_case :: Test
+
+-- * Tests
+
+test_normalize_case :: TestTree
test_normalize_case =
- testCase desc $
- assertEqual desc expected actual
+ testCase desc $ actual @?= expected
where
desc = "normalize_case lowercases DNS names"
expected = BS.pack "example.com"
actual = normalize_case $ BS.pack "ExAmPlE.COM"
-prop_normalize_case_idempotent :: String -> Bool
-prop_normalize_case_idempotent s =
- (normalize_case . normalize_case) bs == normalize_case bs
+prop_normalize_case_idempotent :: TestTree
+prop_normalize_case_idempotent =
+ testProperty desc $ prop
where
- bs = BS.pack s
+ desc = "normalize_case is idempotent"
-test_normalize_root_adds_dot :: Test
+ prop :: String -> Bool
+ prop s = (normalize_case . normalize_case) bs == normalize_case bs
+ where
+ bs = BS.pack s
+
+test_normalize_root_adds_dot :: TestTree
test_normalize_root_adds_dot =
- testCase desc $
- assertEqual desc expected actual
+ testCase desc $ actual @?= expected
where
desc = "normalize_root adds a trailing dot"
expected = BS.pack "example.com."
actual = normalize_root $ BS.pack "example.com"
-prop_normalize_root_idempotent :: String -> Bool
-prop_normalize_root_idempotent s =
- (normalize_root . normalize_root) bs == normalize_root bs
+prop_normalize_root_idempotent :: TestTree
+prop_normalize_root_idempotent =
+ testProperty desc prop
where
- bs = BS.pack s
+ desc = "normalize_root is idempotent"
+
+ prop :: String -> Bool
+ prop s = (normalize_root . normalize_root) bs == normalize_root bs
+ where
+ bs = BS.pack s
-dns_tests :: Test
+dns_tests :: TestTree
dns_tests =
testGroup "DNS Tests" [
test_normalize_case,
test_normalize_root_adds_dot ]
-dns_properties :: Test
+dns_properties :: TestTree
dns_properties =
testGroup "DNS Properties" [
- testProperty
- "normalize_case is idempotent"
- prop_normalize_case_idempotent,
- testProperty
- "normalize_root is idempotent"
- prop_normalize_root_idempotent ]
+ prop_normalize_case_idempotent,
+ prop_normalize_root_idempotent ]