Skip to content

Commit fcc2d86

Browse files
karknukazu-yamamoto
authored andcommitted
Test cases for getNameInfo and show SockAddr
1 parent 08a7642 commit fcc2d86

File tree

1 file changed

+50
-0
lines changed

1 file changed

+50
-0
lines changed

tests/Network/SocketSpec.hs

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -219,3 +219,53 @@ spec = do
219219
s <- mkSocket fd
220220
sendAll s "HELLO WORLD"
221221
tcpTest client server
222+
223+
describe "getNameInfo" $ do
224+
it "works for IPv4 address" $ do
225+
let addr = SockAddrInet 80 (tupleToHostAddress (127, 0, 0, 1))
226+
(hn_m, sn_m) <- getNameInfo [NI_NUMERICHOST, NI_NUMERICSERV] True True addr
227+
228+
hn_m `shouldBe` (Just "127.0.0.1")
229+
sn_m `shouldBe` (Just "80")
230+
231+
it "works for IPv6 address" $ do
232+
let addr = SockAddrInet6 80 0
233+
(tupleToHostAddress6 (0x2001, 0x0db8, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7)) 0
234+
(hn_m, sn_m) <- getNameInfo [NI_NUMERICHOST, NI_NUMERICSERV] True True addr
235+
hn_m `shouldBe`(Just "2001:db8:2:3:4:5:6:7")
236+
sn_m `shouldBe` (Just "80")
237+
238+
it "works for IPv6 address" $ do
239+
let addr = SockAddrInet6 80 0
240+
(tupleToHostAddress6 (0x2001, 0x0db8, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7)) 0
241+
(hn_m, sn_m) <- getNameInfo [NI_NUMERICHOST, NI_NUMERICSERV] True True addr
242+
hn_m `shouldBe`(Just "2001:db8:2:3:4:5:6:7")
243+
sn_m `shouldBe` (Just "80")
244+
245+
it "works for global multicast IPv6 address" $ do
246+
let addr = SockAddrInet6 80 0
247+
(tupleToHostAddress6 (0xfe01, 0x0db8, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7)) 0
248+
(hn_m, sn_m) <- getNameInfo [NI_NUMERICHOST, NI_NUMERICSERV] True True addr
249+
hn_m `shouldBe`(Just "fe01:db8:2:3:4:5:6:7")
250+
sn_m `shouldBe` (Just "80")
251+
252+
describe "show SocketAddr" $ do
253+
it "works for IPv4 address" $
254+
let addr = SockAddrInet 80 (tupleToHostAddress (127, 0, 0, 1)) in
255+
show addr `shouldBe` "127.0.0.1:80"
256+
257+
it "works for IPv6 address" $
258+
let addr = SockAddrInet6 80 0
259+
(tupleToHostAddress6 (0x2001, 0x0db8, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7)) 0 in
260+
show addr `shouldBe` "[2001:db8:2:3:4:5:6:7]:80"
261+
262+
it "works for IPv6 address with zeros" $
263+
let addr = SockAddrInet6 80 0
264+
(tupleToHostAddress6 (0x2001, 0x0db8, 0x2, 0x3, 0x0, 0x0, 0x0, 0x7)) 0 in
265+
show addr `shouldBe` "[2001:db8:2:3::7]:80"
266+
267+
it "works for multicast IPv6 address with reserved scope" $ do
268+
let addr = SockAddrInet6 80 0
269+
(tupleToHostAddress6 (0xff01, 0x1234, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7)) 0
270+
show addr `shouldBe` "[ff01:1234:2:3:4:5:6:7]:80"
271+

0 commit comments

Comments
 (0)