]> gitweb @ CieloNegro.org - haskell-dns.git/blob - DNSUnitTest.hs
Farewell to the Sanity.hs
[haskell-dns.git] / DNSUnitTest.hs
1 import           Data.Binary
2 import qualified Data.ByteString.Lazy as LBS
3 import           Data.Word
4 import           Network.DNS.Message
5 import           Network.Socket
6 import           System.IO.Unsafe
7 import           Test.HUnit
8
9
10 messages :: [([Word8], Message)]
11 messages = [ ( [ 0x22, 0x79, 0x01, 0x00, 0x00, 0x01, 0x00, 0x00
12                , 0x00, 0x00, 0x00, 0x00, 0x04, 0x6D, 0x61, 0x69
13                , 0x6C, 0x0A, 0x63, 0x69, 0x65, 0x6C, 0x6F, 0x6E
14                , 0x65, 0x67, 0x72, 0x6F, 0x03, 0x6F, 0x72, 0x67
15                , 0x00, 0x00, 0x05, 0x00, 0x01
16                ]
17              , Message {
18                  msgHeader = Header {
19                                hdMessageID             = 8825
20                              , hdMessageType           = Query
21                              , hdOpcode                = StandardQuery
22                              , hdIsAuthoritativeAnswer = False
23                              , hdIsTruncated           = False
24                              , hdIsRecursionDesired    = True
25                              , hdIsRecursionAvailable  = False
26                              , hdResponseCode          = NoError
27                              }
28                , msgQuestions   = [ wrapQuestion $
29                                     Question {
30                                       qName  = mkDomainName "mail.cielonegro.org."
31                                     , qType  = CNAME
32                                     , qClass = IN
33                                     }
34                                   ]
35                , msgAnswers     = []
36                , msgAuthorities = []
37                , msgAdditionals = []
38                }
39              )
40            , ( [ 0x22, 0x79, 0x85, 0x00, 0x00, 0x01, 0x00, 0x01
41                , 0x00, 0x01, 0x00, 0x01, 0x04, 0x6D, 0x61, 0x69
42                , 0x6C, 0x0A, 0x63, 0x69, 0x65, 0x6C, 0x6F, 0x6E
43                , 0x65, 0x67, 0x72, 0x6F, 0x03, 0x6F, 0x72, 0x67
44                , 0x00, 0x00, 0x05, 0x00, 0x01, 0xC0, 0x0C, 0x00
45                , 0x05, 0x00, 0x01, 0x00, 0x01, 0x51, 0x80, 0x00
46                , 0x06, 0x03, 0x6E, 0x65, 0x6D, 0xC0, 0x11, 0xC0
47                , 0x11, 0x00, 0x02, 0x00, 0x01, 0x00, 0x00, 0x0E
48                , 0x10, 0x00, 0x02, 0xC0, 0x31, 0xC0, 0x31, 0x00
49                , 0x01, 0x00, 0x01, 0x00, 0x00, 0x0E, 0x10, 0x00
50                , 0x04, 0xDB, 0x5E, 0x82, 0x8B
51                ]
52              , Message {
53                  msgHeader = Header {
54                                hdMessageID             = 8825
55                              , hdMessageType           = Response
56                              , hdOpcode                = StandardQuery
57                              , hdIsAuthoritativeAnswer = True
58                              , hdIsTruncated           = False
59                              , hdIsRecursionDesired    = True
60                              , hdIsRecursionAvailable  = False
61                              , hdResponseCode          = NoError
62                              }
63                , msgQuestions   = [ wrapQuestion $
64                                     Question {
65                                       qName  = mkDomainName "mail.cielonegro.org."
66                                     , qType  = CNAME
67                                     , qClass = IN
68                                     }
69                                   ]
70                , msgAnswers     = [ wrapRecord $
71                                     ResourceRecord {
72                                       rrName  = mkDomainName "mail.cielonegro.org."
73                                     , rrType  = CNAME
74                                     , rrClass = IN
75                                     , rrTTL   = 86400
76                                     , rrData  = mkDomainName "nem.cielonegro.org."
77                                     }
78                                   ]
79                , msgAuthorities = [ wrapRecord $
80                                     ResourceRecord {
81                                       rrName  = mkDomainName "cielonegro.org."
82                                     , rrType  = NS
83                                     , rrClass = IN
84                                     , rrTTL   = 3600
85                                     , rrData  = mkDomainName "nem.cielonegro.org."
86                                     }
87                                   ]
88                , msgAdditionals = [ wrapRecord $
89                                     ResourceRecord {
90                                       rrName  = mkDomainName "nem.cielonegro.org."
91                                     , rrType  = A
92                                     , rrClass = IN
93                                     , rrTTL   = 3600
94                                     , rrData  = unsafePerformIO (inet_addr "219.94.130.139")
95                                     }
96                                   ]
97                }
98              )
99            , ( [ 0xED, 0x68, 0x01, 0x00, 0x00, 0x01, 0x00, 0x00
100                , 0x00, 0x00, 0x00, 0x00, 0x0A, 0x63, 0x69, 0x65
101                , 0x6C, 0x6F, 0x6E, 0x65, 0x67, 0x72, 0x6F, 0x03
102                , 0x6F, 0x72, 0x67, 0x00, 0x00, 0x06, 0x00, 0x01
103                ]
104              , Message {
105                  msgHeader = Header {
106                                hdMessageID             = 60776
107                              , hdMessageType           = Query
108                              , hdOpcode                = StandardQuery
109                              , hdIsAuthoritativeAnswer = False
110                              , hdIsTruncated           = False
111                              , hdIsRecursionDesired    = True
112                              , hdIsRecursionAvailable  = False
113                              , hdResponseCode          = NoError
114                              }
115                , msgQuestions   = [ wrapQuestion $
116                                     Question {
117                                       qName  = mkDomainName "cielonegro.org."
118                                     , qType  = SOA
119                                     , qClass = IN
120                                     }
121                                   ]
122                , msgAnswers     = []
123                , msgAuthorities = []
124                , msgAdditionals = []
125                }
126              )
127            , ( [ 0xED, 0x68, 0x85, 0x00, 0x00, 0x01, 0x00, 0x01
128                , 0x00, 0x01, 0x00, 0x01, 0x0A, 0x63, 0x69, 0x65
129                , 0x6C, 0x6F, 0x6E, 0x65, 0x67, 0x72, 0x6F, 0x03
130                , 0x6F, 0x72, 0x67, 0x00, 0x00, 0x06, 0x00, 0x01
131                , 0xC0, 0x0C, 0x00, 0x06, 0x00, 0x01, 0x00, 0x00
132                , 0x0E, 0x10, 0x00, 0x21, 0x03, 0x6E, 0x65, 0x6D
133                , 0xC0, 0x0C, 0x04, 0x72, 0x6F, 0x6F, 0x74, 0xC0
134                , 0x2C, 0x77, 0xAF, 0xFC, 0x84, 0x00, 0x00, 0x0E
135                , 0x10, 0x00, 0x00, 0x03, 0x84, 0x00, 0x36, 0xEE
136                , 0x80, 0x00, 0x00, 0x0E, 0x10, 0xC0, 0x0C, 0x00
137                , 0x02, 0x00, 0x01, 0x00, 0x00, 0x0E, 0x10, 0x00
138                , 0x02, 0xC0, 0x2C, 0xC0, 0x2C, 0x00, 0x01, 0x00
139                , 0x01, 0x00, 0x00, 0x0E, 0x10, 0x00, 0x04, 0xDB
140                , 0x5E, 0x82, 0x8B
141                ]
142              , Message {
143                  msgHeader = Header {
144                                hdMessageID             = 60776
145                              , hdMessageType           = Response
146                              , hdOpcode                = StandardQuery
147                              , hdIsAuthoritativeAnswer = True
148                              , hdIsTruncated           = False
149                              , hdIsRecursionDesired    = True
150                              , hdIsRecursionAvailable  = False
151                              , hdResponseCode          = NoError
152                              }
153                , msgQuestions   = [ wrapQuestion $
154                                     Question {
155                                       qName  = mkDomainName "cielonegro.org."
156                                     , qType  = SOA
157                                     , qClass = IN
158                                     }
159                                   ]
160                , msgAnswers     = [ wrapRecord $
161                                     ResourceRecord {
162                                       rrName  = mkDomainName "cielonegro.org."
163                                     , rrType  = SOA
164                                     , rrClass = IN
165                                     , rrTTL   = 3600
166                                     , rrData  = SOAFields {
167                                                   soaMasterNameServer   = mkDomainName "nem.cielonegro.org."
168                                                 , soaResponsibleMailbox = mkDomainName "root.nem.cielonegro.org."
169                                                 , soaSerialNumber       = 2008022148
170                                                 , soaRefreshInterval    = 3600
171                                                 , soaRetryInterval      = 900
172                                                 , soaExpirationLimit    = 3600000
173                                                 , soaMinimumTTL         = 3600
174                                                 }
175                                     }
176                                   ]
177                , msgAuthorities = [ wrapRecord $
178                                     ResourceRecord {
179                                       rrName  = mkDomainName "cielonegro.org."
180                                     , rrType  = NS
181                                     , rrClass = IN
182                                     , rrTTL   = 3600
183                                     , rrData  = mkDomainName "nem.cielonegro.org."
184                                     }
185                                   ]
186                , msgAdditionals = [ wrapRecord $
187                                     ResourceRecord {
188                                       rrName  = mkDomainName "nem.cielonegro.org."
189                                     , rrType  = A
190                                     , rrClass = IN
191                                     , rrTTL   = 3600
192                                     , rrData  = unsafePerformIO (inet_addr "219.94.130.139")
193                                     }
194                                   ]
195                }
196              )
197            ]
198
199 packMsg :: Message -> [Word8]
200 packMsg = LBS.unpack . encode
201
202 unpackMsg :: [Word8] -> Message
203 unpackMsg = decode . LBS.pack
204
205 testData :: [Test]
206 testData = map mkPackTest messages
207            ++
208            map mkUnpackTest messages
209     where
210       mkPackTest :: ([Word8], Message) -> Test
211       mkPackTest (bin, msg) = packMsg msg ~?= bin
212
213       mkUnpackTest :: ([Word8], Message) -> Test
214       mkUnpackTest (bin, msg) = unpackMsg bin ~?= msg
215
216 main :: IO ()
217 main = runTestTT (test testData) >> return ()