/
Socket.hs
475 lines (412 loc) · 17.5 KB
/
Socket.hs
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
{-# LANGUAGE BangPatterns #-}
{-# LANGUAGE NamedFieldPuns #-}
{-# LANGUAGE NumericUnderscores #-}
{-# LANGUAGE RankNTypes #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE TypeApplications #-}
module Test.Async.Socket (tests) where
import Control.Exception
import Control.Concurrent
import Data.Binary
import Data.Bool (bool)
import Data.ByteString (ByteString)
import qualified Data.ByteString as BS
import qualified Data.ByteString.Lazy as BL
import Data.Functor (void)
import Data.Function (on)
import Data.Foldable (foldl', traverse_)
import GHC.IO.Exception (IOException (..))
import System.IOManager
import qualified System.Win32.Async.Socket as Async
import qualified System.Win32.Async.Socket.ByteString as Async
import qualified System.Win32.Async.Socket.ByteString.Lazy as Async.Lazy
import Network.Socket (Socket, SockAddr (..))
import qualified Network.Socket as Socket
import Test.Generators hiding (tests)
import Test.Async.PingPong
import Test.Tasty
import Test.Tasty.HUnit
import Test.Tasty.QuickCheck
import Test.QuickCheck.Instances.ByteString ()
tests :: TestTree
tests =
testGroup "Async.Socket"
[ testCase "interruptible connect"
test_interruptible_connect
, testCase "interruptible accept"
test_interruptible_accept
, testCase "terminate accept via close"
test_close_accept
, testProperty "send and recv"
(ioProperty . prop_send_recv)
, testProperty "sendTo and recvFrom"
(ioProperty . prop_sendTo_recvFrom)
, testProperty "PingPong test"
prop_PingPong
, testProperty "PingPongPipelined test"
prop_PingPongPipelined
, testGroup "vectored io"
[ testProperty "PingPong test"
prop_PingPongLazy
, testProperty "PingPongPipelined test"
prop_PingPongPipelinedLazy
]
]
-- The stock 'connect' is not interruptible. This tests is not reliable on
-- Windows because of using loopback device.
--
test_interruptible_connect :: IO ()
test_interruptible_connect =
withIOManager $ \ioManager ->
bracket
((,) <$> Socket.socket Socket.AF_INET Socket.Stream Socket.defaultProtocol
<*> Socket.socket Socket.AF_INET Socket.Stream Socket.defaultProtocol)
(\(x, y) -> Socket.close x >> Socket.close y)
$ \ (fd_in, fd_out) -> do
lock <- newEmptyMVar
associateWithIOManager ioManager (Right fd_in)
associateWithIOManager ioManager (Right fd_out)
(v :: MVar (Maybe SomeException)) <- newEmptyMVar
let addr = SockAddrInet 0 (Socket.tupleToHostAddress (127, 0, 0, 1))
Socket.bind fd_in addr
addr' <- Socket.getSocketName fd_in
Socket.listen fd_in 1
tid <- mask_ $ forkIOWithUnmask $ \unmask ->
do
putMVar lock ()
unmask (Async.connect fd_out addr')
`catch` (putMVar v . Just)
`finally` tryPutMVar v Nothing
takeMVar lock
killThread tid
me <- takeMVar v
case me of
Nothing -> assertFailure "connect finished before ThreadKilled was delivered"
Just e -> do
-- check that the 'ThreadKilled' exception was caught.
assertEqual "wrong exception"
(Just ThreadKilled)
(fromException e :: Maybe AsyncException)
test_interruptible_accept :: IO ()
test_interruptible_accept =
bracket
(Socket.socket Socket.AF_INET Socket.Stream Socket.defaultProtocol)
Socket.close
$ \ fd -> do
(v :: MVar SomeException) <- newEmptyMVar
let addr = SockAddrInet 0 (Socket.tupleToHostAddress (127, 0, 0, 1))
Socket.bind fd addr
Socket.listen fd 1
tid <- forkIO $ void $
Async.accept fd
`catch` (\e -> putMVar v e >> throwIO e)
threadDelay 100
killThread tid
e <- takeMVar v
assertEqual "wrong exception"
(Just ThreadKilled)
(fromException e :: Maybe AsyncException)
-- | Verify that closing a socket will terminate the `Network.Socket.accept`
-- call.
--
test_close_accept :: IO ()
test_close_accept =
bracket
(Socket.socket Socket.AF_INET Socket.Stream Socket.defaultProtocol)
Socket.close
$ \fd -> do
(v :: MVar SomeException) <- newEmptyMVar
let addr = SockAddrInet 0 (Socket.tupleToHostAddress (127, 0, 0, 1))
Socket.bind fd addr
Socket.listen fd 1
_ <- forkIO $
void (Socket.accept fd) `catch` putMVar v
threadDelay 1000
-- THIS IS WIRED: it should block if `close` does not run!
Socket.close fd
e <- takeMVar v
case fromException e of
Nothing -> assertFailure $ "wrong exception: " ++ show e
Just err -> do
-- `WSAEINTR` which is explained as:
-- ```
-- Interrupted function call.
-- A blocking operation was interrupted by a call to WSACancelBlockingCall.
-- ```
-- It might be the case that windows is using something similar to
-- WSACancelBlockingCall to cancel the thread which is blocked on
-- `accept` when the socket was closed.
--
-- TODO: `ioe_errno` returns `Nothing`, the wsa errors are not in
-- `errtable` in base/cbits/Win32Utils.c used by `failWith`. We
-- should improve `wsaFailWith` and not use `failWith`.
"Interrupted function call (WSAEINTR)" @=? ioe_description err
recvLen :: Socket -> Int -> IO BL.ByteString
recvLen sock = go []
where
go bufs !l | l <= 0 = pure $ BL.fromChunks (reverse bufs)
| otherwise = do
buf <- Async.recv sock l
go (buf : bufs) (l - BS.length buf)
prop_send_recv :: LargeNonEmptyBS -> IO Bool
prop_send_recv (LargeNonEmptyBS bs _size) =
withIOManager $ \ioManager ->
bracket
((,) <$> Socket.socket Socket.AF_INET Socket.Stream Socket.defaultProtocol
<*> Socket.socket Socket.AF_INET Socket.Stream Socket.defaultProtocol)
(\(x, y) -> Socket.close x >> Socket.close y)
$ \ (fd_in, fd_out) -> do
v <- newEmptyMVar
syncVar <- newEmptyMVar
associateWithIOManager ioManager (Right fd_in)
mainThread <- myThreadId
_ <- forkIO $ handle (\e -> throwTo mainThread e >> ioError e) $ do
let addr = SockAddrInet 0 (Socket.tupleToHostAddress (127, 0, 0, 1))
Socket.bind fd_out addr
addr' <- Socket.getSocketName fd_out
Socket.listen fd_out 1024
`catch` \(e :: IOException) -> putStrLn ("listen errored: " ++ displayException e) >> throwIO e
putMVar syncVar addr'
(fd, _) <- Async.accept fd_out
`catch` \(e :: IOException) -> putStrLn ("accept errored: " ++ displayException e) >> throwIO e
associateWithIOManager ioManager (Right fd)
bs' <- BL.toStrict <$> recvLen fd (BS.length bs)
putMVar v bs'
_ <- forkIO $ handle (\e -> throwTo mainThread e >> ioError e) $ do
-- wait for the other end to start listening
addr' <- takeMVar syncVar
Socket.connect fd_in addr'
Async.sendAll fd_in bs
`catch` \(e :: IOException) -> putStrLn ("sendAll errored: " ++ displayException e) >> throwIO e
bs' <- takeMVar v
pure $ bs == bs'
-- | Like recv but using `recvFrom`. It assumes that each 'recvFrom' will
-- return the same address otherwise it will throw an exception.
--
recvFromLen :: Socket -> Int -> IO (BL.ByteString, Socket.SockAddr)
recvFromLen sock len0 = do
res <- go [] len0
case res of
as@((_, addr) : _)
| all (uncurry $ on (==) snd) (zip as (tail as))
-> pure (BL.fromChunks (fst `map` as), addr)
-- not all addresses where the same
| otherwise
-> throwIO $ userError "recvFromLen: recevied from various addresses"
[] -> throwIO $ userError "recvFromLen: requesting less than zero bytes is not supported"
where
go as !l | l <= 0 = pure $ reverse as
| otherwise = do
a@(buf, _) <- Async.recvFrom sock l
go (a : as) (l - BS.length buf)
-- TODO: this test fails when using 'LargeNonEmptyBS' generator.
--
prop_sendTo_recvFrom :: NonEmptyBS -> IO Bool
prop_sendTo_recvFrom (NonEmptyBS bs) =
withIOManager $ \ioManager ->
bracket
((,) <$> Socket.socket Socket.AF_INET Socket.Datagram Socket.defaultProtocol
<*> Socket.socket Socket.AF_INET Socket.Datagram Socket.defaultProtocol)
(\(x, y) -> Socket.close x >> Socket.close y)
$ \ (fd_in, fd_out) -> do
v <- newEmptyMVar
syncVar <- newEmptyMVar
associateWithIOManager ioManager (Right fd_in)
associateWithIOManager ioManager (Right fd_out)
mainThread <- myThreadId
_ <- forkIO $ handle (\e -> throwTo mainThread e >> ioError e) $ do
let addr = SockAddrInet 0 (Socket.tupleToHostAddress (127, 0, 0, 1))
Socket.bind fd_in addr
addr' <- Socket.getSocketName fd_in
putMVar syncVar addr'
(bs', _) <- recvFromLen fd_in (BS.length bs)
putMVar v (BL.toStrict bs')
_ <- forkIO $ handle (\e -> throwTo mainThread e >> ioError e) $ do
-- wait for the other end to start listening
addr' <- takeMVar syncVar
Async.sendAllTo fd_out bs addr'
bs' <- takeMVar v
pure $ bs == bs'
--
-- BinaryChannels using 'System.Win32.Socket.Bytestring' or
-- 'System.Win32.Socket.ByteString.Lazy' (vectored io).
--
-- | 'BinaryChannel' defined in terms of
-- 'System.Win32.Socket.Bytestring.sendAll' and
-- 'System.Win32.Socket.Bytestring.recv'
--
socketToBinaryChannel :: Binary a
=> Socket
-> BinaryChannel a
socketToBinaryChannel sock = BinaryChannel { readChannel, writeChannel, closeChannel }
where
readChannel b = do
s <- decode . BL.fromStrict <$> Async.recv sock 8
if b
then do
bs' <- recvLen sock s
pure $ Just $ decode bs'
else pure Nothing
writeChannel b a = do
let chunks :: [ByteString]
chunks = BL.toChunks (encode a)
size :: Int
size = bool (+1) id b $ foldl' (\x y -> x + BS.length y) 0 chunks
_ <- Async.sendAll sock (BL.toStrict $ encode size)
traverse_ (\chunk -> Async.sendAll sock chunk) chunks
closeChannel = Socket.close sock
-- | Like 'socketToBinaryChannel' but using
-- 'System.Win32.Async.Socket.ByteString.Lazy' (vectored io).
--
socketToLazyBinaryChannel :: Binary a
=> Socket
-> BinaryChannel a
socketToLazyBinaryChannel sock = BinaryChannel { readChannel, writeChannel, closeChannel }
where
recvLazyLen :: Int -> IO BL.ByteString
recvLazyLen = go []
where
go bufs !l | l <= 0 = return $ BL.concat (reverse bufs)
| otherwise = do
buf <- Async.Lazy.recv sock l
go (buf : bufs) (l - fromIntegral (BL.length buf))
readChannel b = do
-- putStrLn "readChannel: header"
s <- decode <$> Async.Lazy.recv sock 8
-- putStrLn $ "readChannel: header: " ++ show s
if b
then do
-- putStrLn $ "recvLazyLen: " ++ show s
bs' <- recvLazyLen s
-- putStrLn $ "recvLazyLen: done"
pure $ Just $ decode bs'
else pure Nothing
writeChannel b a =
do
let bs :: BL.ByteString
bs = encode a
size :: Int
size = bool (+1) id b (fromIntegral $ BL.length bs)
Async.Lazy.sendAll sock (encode size)
Async.Lazy.sendAll sock bs
`catch` (\(e :: IOException) -> putStrLn (show e) >> throwIO e)
closeChannel = Socket.close sock
--
-- Ping Pong Tests
--
test_PingPong :: (forall a. Binary a => Socket -> BinaryChannel a)
-> Int
-> Blocking
-> ByteString
-> IO Bool
test_PingPong createBinaryChannel n blocking bs =
withIOManager $ \ioManager ->
bracket
((,) <$> Socket.socket Socket.AF_INET Socket.Stream Socket.defaultProtocol
<*> Socket.socket Socket.AF_INET Socket.Stream Socket.defaultProtocol)
(\(x, y) -> Socket.close x >> Socket.close y)
$ \(sockIn, sockOut) -> do
lock <- newEmptyMVar
-- fork a PingPong server
let addr = SockAddrInet 0 (Socket.tupleToHostAddress (127, 0, 0, 1))
Socket.bind sockIn addr
addr' <- Socket.getSocketName sockIn
Socket.listen sockIn 1024
tid <- mask_ $ forkIOWithUnmask $ \unmask ->
do
(socket, _remoteAddr) <- Socket.accept sockIn
associateWithIOManager ioManager (Right socket)
let channel = createBinaryChannel socket
unmask (runPingPongServer channel (constPingPongServer @ByteString))
`catch` \(e :: IOException) -> do
putStrLn ("ping-pong server cought: " ++ show e)
throwIO e
`finally` putMVar lock ()
-- run a PingPong client, at this stage server socket is in
-- listening state accepting connections.
Socket.connect sockOut addr'
associateWithIOManager ioManager (Right sockOut)
let channelOut = createBinaryChannel sockOut
res <- runPingPongClient channelOut blocking tid (constPingPongClient n bs)
`catch` \(e :: IOException) -> do
putStrLn ("ping-pong client cought: " ++ show e)
throwIO e
-- this lock asserts that the server was terminated
-- TODO: check that it was killed by the right exception
takeMVar lock
pure $ case blocking of
NonBlocking -> res == replicate n bs
_ -> res == replicate (pred n) bs
prop_PingPong :: Positive Int
-> Blocking
-> LargeNonEmptyBS
-> Property
prop_PingPong (Positive n) blocking (LargeNonEmptyBS bs _bufSize) =
ioProperty $ test_PingPong socketToBinaryChannel n blocking bs
prop_PingPongLazy :: Positive Int
-> Blocking
-> LargeNonEmptyBS
-> Property
prop_PingPongLazy (Positive n) blocking (LargeNonEmptyBS bs _bufSize) =
ioProperty $ test_PingPong socketToLazyBinaryChannel n blocking bs
--
-- Pipelined Ping Pong Tests
--
test_PingPongPipelined :: (forall a. Binary a => Socket -> BinaryChannel a)
-> Blocking
-> [ByteString]
-> IO Bool
test_PingPongPipelined createBinaryChannel blocking bss =
withIOManager $ \ioManager ->
bracket
((,) <$> Socket.socket Socket.AF_INET Socket.Stream Socket.defaultProtocol
<*> Socket.socket Socket.AF_INET Socket.Stream Socket.defaultProtocol)
(\(x, y) -> Socket.close x >> Socket.close y)
$ \(sockIn, sockOut) -> do
lock <- newEmptyMVar
-- fork a PingPong server
let addr = SockAddrInet 0 (Socket.tupleToHostAddress (127, 0, 0, 1))
Socket.bind sockIn addr
addr' <- Socket.getSocketName sockIn
Socket.listen sockIn 1024
tid <- mask_ $ forkIOWithUnmask $ \unmask ->
do
(socket, _) <- Socket.accept sockIn
associateWithIOManager ioManager (Right socket)
let channel = createBinaryChannel socket
unmask (runPingPongServer channel (constPingPongServer @ByteString))
`catch` \(e :: IOException) -> do
putStrLn ("ping-pong server cought: " ++ show e)
throwIO e
`finally` putMVar lock ()
-- run a PingPong client, at this stage server socket is in
-- listening state accepting connections.
Socket.connect sockOut addr'
associateWithIOManager ioManager (Right sockOut)
let channelOut = createBinaryChannel sockOut
res <-
runPingPongClientPipelined channelOut blocking tid bss
`catch` \(e :: IOException) -> do
putStrLn ("ping-pong client cought: " ++ show e)
throwIO e
-- this lock asserts that the server was terminated
-- TODO: check that it was killed by the right exception
takeMVar lock
pure $ case blocking of
NonBlocking -> case res of
Just bss' -> bss == bss'
Nothing -> False
_ -> True -- if we evalute this case branch, it means that
-- killing blocked thread did not deadlock.
prop_PingPongPipelined :: Blocking
-> NonEmptyList LargeNonEmptyBS
-> Property
prop_PingPongPipelined blocking (NonEmpty bss) =
ioProperty $
test_PingPongPipelined socketToBinaryChannel blocking (map getLargeNonEmptyBS bss)
prop_PingPongPipelinedLazy :: Blocking
-> NonEmptyList LargeNonEmptyBS
-> Property
prop_PingPongPipelinedLazy blocking (NonEmpty bss) =
ioProperty $
test_PingPongPipelined socketToLazyBinaryChannel blocking (map getLargeNonEmptyBS bss)