Ticket #2996: Test5.hs

File Test5.hs, 1.3 KB (added by amthrax, 5 years ago)

Test case

Line 
1import Network.BSD
2import Network.Socket
3
4connectToHaskell = do
5  proto <- getProtocolNumber "tcp"
6  sock <- socket AF_INET Stream proto
7  he <- getHostByName "www.haskell.org"
8  connect sock (SockAddrInet 80 (hostAddress he))
9  send sock "GET / HTTP/0.9\r\n\r\n"
10  return sock
11
12main = do
13  sock1 <- connectToHaskell
14  -- Close the socket we just created.  This does not change the
15  -- socket's status, but it frees up the underlying FD
16  sClose sock1
17  -- Now, when we create a new socket, *NIX will pick the lowest free
18  -- FD, which should be the same FD number that backed sock1
19  sock2 <- connectToHaskell
20  -- Read from sock2, just to make sure it worked
21  print =<< recv sock2 128
22
23  -- Now we make it misbehave
24
25  -- The following should fail because we closed sock1, but instead it
26  -- reads from sock2
27  putStrLn "Reading from closed socket; should fail but succeeds"
28  print =<< recv sock1 128
29  -- The following should fail or do nothing, but instead it closes
30  -- sock2
31  putStrLn "Closing closed socket; should fail or do nothing but succeeds"
32  sClose sock1
33  -- The following should succeed but doesn't because the previous
34  -- line accidentally closed sock2
35  putStrLn "Reading from supposedly open socket; should succeed but fails"
36  print =<< recv sock2 128