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 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630
|
{-# LANGUAGE BangPatterns #-}
{-# LANGUAGE CPP #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE RankNTypes #-}
{-# LANGUAGE ScopedTypeVariables #-}
module System.IO.Streams.Network.HAProxy.Tests (tests) where
------------------------------------------------------------------------------
import Control.Applicative ((<$>))
import Control.Concurrent
import qualified Control.Exception as E
import Control.Monad (forever)
import Control.Monad.IO.Class (MonadIO, liftIO)
import qualified Data.ByteString as S8
import Data.ByteString.Char8 (ByteString)
import qualified Data.ByteString.Char8 as S
import Data.Typeable
import qualified Network.Socket as N
import System.IO (hPutStrLn, stderr)
import System.IO.Streams (InputStream, OutputStream)
import qualified System.IO.Streams as Streams
import qualified System.IO.Streams.Network.HAProxy as HA
import System.IO.Streams.Network.Internal.Address (AddressNotSupportedException (..), getSockAddr, getSockAddrImpl)
import System.Timeout (timeout)
import Test.Framework
import Test.Framework.Providers.HUnit
import Test.HUnit hiding (Test)
------------------------------------------------------------------------------
------------------------------------------------------------------------------
tests :: [Test]
tests = [ testOldHaProxy
, testOldHaProxy6
, testOldHaProxyFailure
, testOldHaProxyLocal
, testOldHaProxyLocal6
, testOldHaProxyBadAddress
, testBlackBox
, testBlackBoxLocal
, testNewHaProxy
#ifndef WINDOWS
, testNewHaProxyUnix
#endif
, testNewHaProxy6
, testNewHaProxyTooBig
, testNewHaProxyTooSmall
, testNewHaProxyBadVersion
, testNewHaProxyLocal
, testGetSockAddr
, testTrivials
]
------------------------------------------------------------------------------
runInput :: ByteString
-> N.SockAddr
-> N.SockAddr
-> (HA.ProxyInfo -> InputStream ByteString -> OutputStream ByteString -> IO a)
-> IO a
runInput input sa sb action = do
is <- Streams.fromList [input]
(os, _) <- Streams.listOutputStream
let pinfo = HA.makeProxyInfo sa sb (addrFamily sa) N.Stream
HA.behindHAProxyWithLocalInfo pinfo (is, os) action
------------------------------------------------------------------------------
addrFamily :: N.SockAddr -> N.Family
addrFamily s = case s of
(N.SockAddrInet _ _) -> N.AF_INET
(N.SockAddrInet6 _ _ _ _) -> N.AF_INET6
#ifndef WINDOWS
(N.SockAddrUnix _ ) -> N.AF_UNIX
#endif
------------------------------------------------------------------------------
blackbox :: (Chan Bool
-> HA.ProxyInfo
-> InputStream ByteString
-> OutputStream ByteString
-> IO ())
-> ByteString
-> IO ()
blackbox action input = withTimeout 10 $ do
chan <- newChan
E.bracket (startServer chan) (killThread . fst) client
readChan chan >>= assertBool "success"
where
client (_, port) = do
(family, addr) <- getSockAddr port "127.0.0.1"
E.bracket (N.socket family N.Stream 0) N.close $ \sock -> do
N.connect sock addr
(_, os) <- Streams.socketToStreams sock
threadDelay 10000
Streams.write (Just input) os
Streams.write Nothing os
threadDelay 10000
withTimeout n m = timeout (n * 1000000) m >>= maybe (fail "timeout") return
startServer :: Chan Bool -> IO (ThreadId, Int)
startServer = E.bracketOnError getSock N.close . forkServer
getBindAddr = do
#if MIN_VERSION_network(2,7,0)
getSockAddr (fromIntegral N.defaultPort) "127.0.0.1"
#else
getSockAddr (fromIntegral N.aNY_PORT) "127.0.0.1"
#endif
getSock = do
(family, addr) <- getBindAddr
sock <- N.socket family N.Stream 0
N.setSocketOption sock N.ReuseAddr 1
N.setSocketOption sock N.NoDelay 1
#if MIN_VERSION_network(2,7,0)
N.bind sock addr
#else
N.bindSocket sock addr
#endif
N.listen sock 150
return $! sock
forkServer chan sock = do
port <- fromIntegral <$> N.socketPort sock
tid <- E.mask_ $ forkIOWithUnmask $ server chan sock
return (tid, port)
server :: Chan Bool -> N.Socket -> (forall z. IO z -> IO z) -> IO ()
server chan boundSocket restore = loop `E.finally` N.close boundSocket
where
loop = forever $
E.bracketOnError (restore $ N.accept boundSocket)
(N.close . fst)
(\(sock, sa) ->
forkIOWithUnmask
$ \r -> flip E.finally (N.close sock)
$ r $ HA.behindHAProxy sock sa (action chan))
------------------------------------------------------------------------------
testBlackBox :: Test
testBlackBox = testCase "test/blackbox" $
blackbox action
"PROXY TCP4 127.0.0.1 127.0.0.1 10000 80\r\nblah"
where
action chan proxyInfo !is !_ = do
sa <- localhost 10000
sb <- localhost 80
x <- E.try $ do assertEqual "src addr" sa $ HA.getSourceAddr proxyInfo
assertEqual "dest addr" sb $ HA.getDestAddr proxyInfo
Streams.toList is >>= assertEqual "rest" ["blah"]
case x of
Left (e :: E.SomeException) -> do hPutStrLn stderr $ show e
writeChan chan False
Right !_ -> writeChan chan True
------------------------------------------------------------------------------
testBlackBoxLocal :: Test
testBlackBoxLocal = testCase "test/blackbox_local" $
blackbox action "PROXY UNKNOWN\r\nblah"
where
action chan proxyInfo !is !_ = do
let q = HA.getSourceAddr proxyInfo `seq` HA.getDestAddr proxyInfo
`seq` ()
x <- q `seq` E.try $ go is proxyInfo
case x of
Left (e :: E.SomeException) -> do hPutStrLn stderr $ show e
writeChan chan False
Right !_ -> writeChan chan True
go is proxyInfo = do
Streams.toList is >>= assertEqual "rest" ["blah"]
assertEqual "family" N.AF_INET $ HA.getFamily proxyInfo
assertEqual "type" N.Stream $ HA.getSocketType proxyInfo
------------------------------------------------------------------------------
testOldHaProxy :: Test
testOldHaProxy = testCase "test/old_ha_proxy" $ do
sa <- localhost 1111
sb <- localhost 2222
runInput "PROXY TCP4 127.0.0.1 127.0.0.1 10000 80\r\nblah" sa sb action
where
action proxyInfo !is !_ = do
sa <- localhost 10000
sb <- localhost 80
assertEqual "src addr" sa $ HA.getSourceAddr proxyInfo
assertEqual "dest addr" sb $ HA.getDestAddr proxyInfo
assertEqual "family" N.AF_INET $ HA.getFamily proxyInfo
assertEqual "stype" N.Stream $ HA.getSocketType proxyInfo
Streams.toList is >>= assertEqual "rest" ["blah"]
------------------------------------------------------------------------------
testOldHaProxy6 :: Test
testOldHaProxy6 = testCase "test/old_ha_proxy6" $ do
sa <- localhost6 1111
sb <- localhost6 2222
runInput "PROXY TCP6 ::1 ::1 10000 80\r\nblah" sa sb action
where
action proxyInfo !is !_ = do
sa <- localhost6 10000
sb <- localhost6 80
assertEqual "src addr" sa $ HA.getSourceAddr proxyInfo
assertEqual "dest addr" sb $ HA.getDestAddr proxyInfo
assertEqual "family" N.AF_INET6 $ HA.getFamily proxyInfo
assertEqual "stype" N.Stream $ HA.getSocketType proxyInfo
Streams.toList is >>= assertEqual "rest" ["blah"]
------------------------------------------------------------------------------
testOldHaProxyLocal :: Test
testOldHaProxyLocal = testCase "test/old_ha_proxy_local" $ do
sa <- localhost 1111
sb <- localhost 2222
runInput "PROXY UNKNOWN\r\nblah" sa sb action
where
action proxyInfo !is !_ = do
sa <- localhost 1111
sb <- localhost 2222
assertEqual "src addr" sa $ HA.getSourceAddr proxyInfo
assertEqual "dest addr" sb $ HA.getDestAddr proxyInfo
assertEqual "family" N.AF_INET $ HA.getFamily proxyInfo
assertEqual "stype" N.Stream $ HA.getSocketType proxyInfo
Streams.toList is >>= assertEqual "rest" ["blah"]
------------------------------------------------------------------------------
testOldHaProxyLocal6 :: Test
testOldHaProxyLocal6 = testCase "test/old_ha_proxy_local6" $ do
sa <- localhost6 1111
sb <- localhost6 2222
runInput "PROXY UNKNOWN\r\nblah" sa sb action
where
action proxyInfo !is !_ = do
sa <- localhost6 1111
sb <- localhost6 2222
assertEqual "src addr" sa $ HA.getSourceAddr proxyInfo
assertEqual "dest addr" sb $ HA.getDestAddr proxyInfo
assertEqual "family" N.AF_INET6 $ HA.getFamily proxyInfo
assertEqual "stype" N.Stream $ HA.getSocketType proxyInfo
Streams.toList is >>= assertEqual "rest" ["blah"]
------------------------------------------------------------------------------
testOldHaProxyFailure :: Test
testOldHaProxyFailure = testCase "test/old_ha_proxy_failure" $ do
sa <- localhost 1111
sb <- localhost 2222
expectException "bad family"
$ runInput "PROXY ZZZ qqqq wwww 10000 80\r\nblah" sa sb action
expectException "short"
$ runInput "PROXY TCP4 \r\nblah" sa sb action
expectException "non-integral"
$ runInput "PROXY TCP4 127.0.0.1 127.0.0.1 xxx yyy\r\nblah" sa sb action
where
action _ _ _ = return ()
------------------------------------------------------------------------------
testOldHaProxyBadAddress :: Test
testOldHaProxyBadAddress = testCase "test/old_ha_proxy_bad_address" $ do
sa <- localhost 1111
sb <- localhost 2222
expectException "bad address" $
runInput "PROXY TCP4 @~!@#$%^ (*^%$ 10000 80\r\nblah" sa sb action
where
action proxyInfo !is !_ = do
sa <- localhost 10000
sb <- localhost 80
assertEqual "src addr" sa $ HA.getSourceAddr proxyInfo
assertEqual "dest addr" sb $ HA.getDestAddr proxyInfo
Streams.toList is >>= assertEqual "rest" ["blah"]
------------------------------------------------------------------------------
protocolHeader :: ByteString
protocolHeader = S8.pack [ 0x0D, 0x0A, 0x0D, 0x0A, 0x00, 0x0D
, 0x0A, 0x51, 0x55, 0x49, 0x54, 0x0A ]
{-# NOINLINE protocolHeader #-}
------------------------------------------------------------------------------
testNewHaProxy :: Test
testNewHaProxy = testCase "test/new_ha_proxy" $ do
sa <- localhost 1111
sb <- localhost 2222
let input = S.concat [ protocolHeader
, "\x21\x11" -- TCP over v4
, "\x00\x0c" -- 12 bytes of address (network ordered)
, "\x7f\x00\x00\x01" -- localhost
, "\x7f\x00\x00\x01"
, "\x27\x10" -- 10000 in network order
, "\x00\x50" -- 80 in network order
, "blah" -- the rest
]
runInput input sa sb action
where
action proxyInfo !is !_ = do
sa <- localhost 10000
sb <- localhost 80
assertEqual "src addr" sa $ HA.getSourceAddr proxyInfo
assertEqual "dest addr" sb $ HA.getDestAddr proxyInfo
assertEqual "family" N.AF_INET $ HA.getFamily proxyInfo
assertEqual "stype" N.Stream $ HA.getSocketType proxyInfo
Streams.toList is >>= assertEqual "rest" ["blah"]
------------------------------------------------------------------------------
#ifndef WINDOWS
unixPath :: ByteString -> ByteString
unixPath s = S.append s (S.replicate (108 - S.length s) '\x00')
------------------------------------------------------------------------------
unixSock :: ByteString -> N.SockAddr
unixSock = N.SockAddrUnix . S.unpack
------------------------------------------------------------------------------
testNewHaProxyUnix :: Test
testNewHaProxyUnix = testCase "test/new_ha_proxy_unix" $ do
sa <- localhost 1111
sb <- localhost 2222
let input = S.concat [ protocolHeader
, "\x21\x31" -- unix stream
, "\x00\xd8" -- 216 bytes
, unixPath "/foo"
, unixPath "/bar"
, "blah" -- the rest
]
runInput input sa sb action
let input2 = S.concat [ protocolHeader
, "\x21\x32" -- unix datagram
, "\x00\xd8" -- 216 bytes
, unixPath "/foo"
, unixPath "/bar"
, "blah" -- the rest
]
runInput input2 sa sb action2
where
action proxyInfo !is !_ = do
let sa = unixSock "/foo"
let sb = unixSock "/bar"
assertEqual "src addr" sa $ HA.getSourceAddr proxyInfo
assertEqual "dest addr" sb $ HA.getDestAddr proxyInfo
assertEqual "family" N.AF_UNIX $ HA.getFamily proxyInfo
assertEqual "stype" N.Stream $ HA.getSocketType proxyInfo
Streams.toList is >>= assertEqual "rest" ["blah"]
action2 proxyInfo !is !_ = do
let sa = unixSock "/foo"
let sb = unixSock "/bar"
assertEqual "src addr" sa $ HA.getSourceAddr proxyInfo
assertEqual "dest addr" sb $ HA.getDestAddr proxyInfo
assertEqual "family" N.AF_UNIX $ HA.getFamily proxyInfo
assertEqual "stype" N.Datagram $ HA.getSocketType proxyInfo
Streams.toList is >>= assertEqual "rest" ["blah"]
#endif
------------------------------------------------------------------------------
testNewHaProxy6 :: Test
testNewHaProxy6 = testCase "test/new_ha_proxy_6" $ do
sa <- localhost6 1111
sb <- localhost6 2222
let input = S.concat [ protocolHeader
, "\x21\x21" -- TCP over v6
, "\x00\x24" -- 36 bytes of address (network ordered)
, lhBinary
, lhBinary
, "\x27\x10" -- 10000 in network order
, "\x00\x50" -- 80 in network order
, "blah" -- the rest
]
runInput input sa sb action
where
lhBinary = S.concat [ S.replicate 15 '\x00', S.singleton '\x01' ]
action proxyInfo !is !_ = do
sa <- localhost6 10000
sb <- localhost6 80
assertEqual "src addr" sa $ HA.getSourceAddr proxyInfo
assertEqual "dest addr" sb $ HA.getDestAddr proxyInfo
assertEqual "family" N.AF_INET6 $ HA.getFamily proxyInfo
assertEqual "stype" N.Stream $ HA.getSocketType proxyInfo
Streams.toList is >>= assertEqual "rest" ["blah"]
------------------------------------------------------------------------------
testNewHaProxyBadVersion :: Test
testNewHaProxyBadVersion = testCase "test/new_ha_proxy_bad_version" $ do
sa <- localhost 1111
sb <- localhost 2222
let input = S.concat [ protocolHeader
, "\x31\x11" -- TCP over v4, bad version
, "\x00\x0c" -- 12 bytes of address (network ordered)
, "\x7f\x00\x00\x01" -- localhost
, "\x7f\x00\x00\x01"
, "\x27\x10" -- 10000 in network order
, "\x00\x50" -- 80 in network order
, "blah" -- the rest
]
expectException "bad version" $ runInput input sa sb action
let input2 = S.concat [ protocolHeader
, "\x2F\x11" -- TCP over v4, bad command
, "\x00\x0c" -- 12 bytes of address (network ordered)
, "\x7f\x00\x00\x01" -- localhost
, "\x7f\x00\x00\x01"
, "\x27\x10" -- 10000 in network order
, "\x00\x50" -- 80 in network order
, "blah" -- the rest
]
expectException "bad version" $ runInput input2 sa sb action
let input3 = S.concat [ protocolHeader
, "\x21\x41" -- bad family
, "\x00\x0c" -- 12 bytes of address (network ordered)
, "\x7f\x00\x00\x01" -- localhost
, "\x7f\x00\x00\x01"
, "\x27\x10" -- 10000 in network order
, "\x00\x50" -- 80 in network order
, "blah" -- the rest
]
expectException "bad family" $ runInput input3 sa sb action
let input4 = S.concat [ protocolHeader
, "\x21\x14" -- bad family
, "\x00\x0c" -- 12 bytes of address (network ordered)
, "\x7f\x00\x00\x01" -- localhost
, "\x7f\x00\x00\x01"
, "\x27\x10" -- 10000 in network order
, "\x00\x50" -- 80 in network order
, "blah" -- the rest
]
expectException "bad type" $ runInput input4 sa sb action
where
action _ !_ !_ = return ()
------------------------------------------------------------------------------
testNewHaProxyTooSmall :: Test
testNewHaProxyTooSmall = testCase "test/new_ha_proxy_too_small" $ do
sa <- localhost 1111
sb <- localhost 2222
let input = S.concat [ protocolHeader
, "\x21\x11" -- TCP over v4
, "\x00\x02" -- 2 bytes
, "\x00\x00"
, "blah" -- the rest
]
expectException "too small" $ runInput input sa sb action
let input2 = S.concat [ protocolHeader
, "\x21\x21" -- TCP over v6
, "\x00\x02" -- 2 bytes
, "\x00\x00"
, "blah" -- the rest
]
expectException "too small" $ runInput input2 sa sb action
#ifndef WINDOWS
let input3 = S.concat [ protocolHeader
, "\x21\x31" -- unix
, "\x00\x02" -- 2 bytes
, "\x00\x00"
, "blah" -- the rest
]
expectException "too small" $ runInput input3 sa sb action
#endif
where
action _ !_ !_ = return ()
------------------------------------------------------------------------------
testNewHaProxyTooBig :: Test
testNewHaProxyTooBig = testCase "test/new_ha_proxy_too_big" $ do
sa <- localhost 1111
sb <- localhost 2222
let input = S.concat [ protocolHeader
, "\x21\x11" -- TCP over v4
, "\x03\x0c" -- 780: 12 bytes of address
-- (network ordered) plus 768
-- bytes of slop
, "\x7f\x00\x00\x01" -- localhost
, "\x7f\x00\x00\x01"
, S.replicate 768 '0'
, "\x27\x10" -- 10000 in network order
, "\x00\x50" -- 80 in network order
, "blah" -- the rest
]
expectException "too big" $ runInput input sa sb action
let input2 = S.concat [ protocolHeader
, "\x21\x00" -- TCP over v4
, "\x03\x0c" -- 780: 12 bytes of address
-- (network ordered) plus 768
-- bytes of slop
, "\x7f\x00\x00\x01" -- localhost
, "\x7f\x00\x00\x01"
, S.replicate 768 '0'
, "\x27\x10" -- 10000 in network order
, "\x00\x50" -- 80 in network order
, "blah" -- the rest
]
expectException "too big" $ runInput input2 sa sb action
where
action _ !_ !_ = return ()
------------------------------------------------------------------------------
testNewHaProxyLocal :: Test
testNewHaProxyLocal = testCase "test/new_ha_proxy_local" $ do
sa <- localhost 1111
sb <- localhost 2222
let input = S.concat [ protocolHeader
, "\x20\x00" -- LOCAL UNSPEC
, "\x00\x00" -- 0 bytes of address (network ordered)
, "blah" -- the rest
]
runInput input sa sb action
#ifndef WINDOWS
let ua = unixSock "/foo"
let ub = unixSock "/bar"
let input2 = S.concat [ protocolHeader
, "\x20\x00" -- LOCAL UNSPEC
, "\x00\x00" -- 0 bytes of address (network ordered)
, "blah" -- the rest
]
runInput input2 ua ub action2
#endif
where
action proxyInfo !is !_ = do
sa <- localhost 1111
sb <- localhost 2222
assertEqual "src addr" sa $ HA.getSourceAddr proxyInfo
assertEqual "dest addr" sb $ HA.getDestAddr proxyInfo
assertEqual "family" N.AF_INET $ HA.getFamily proxyInfo
assertEqual "stype" N.Stream $ HA.getSocketType proxyInfo
Streams.toList is >>= assertEqual "rest" ["blah"]
#ifndef WINDOWS
action2 proxyInfo !is !_ = do
let sa = unixSock "/foo"
let sb = unixSock "/bar"
assertEqual "src addr" sa $ HA.getSourceAddr proxyInfo
assertEqual "dest addr" sb $ HA.getDestAddr proxyInfo
assertEqual "family" N.AF_UNIX $ HA.getFamily proxyInfo
assertEqual "stype" N.Stream $ HA.getSocketType proxyInfo
Streams.toList is >>= assertEqual "rest" ["blah"]
#endif
------------------------------------------------------------------------------
testGetSockAddr :: Test
testGetSockAddr = testCase "test/address/getSockAddr" $ do
(f1, a1) <- getSockAddr 10 "127.0.0.1"
x1 <- localhost 10
assertEqual "f1" f1 N.AF_INET
assertEqual "x1" x1 a1
(f2, a2) <- getSockAddr 10 "::1"
x2 <- localhost6 10
assertEqual "f2" f2 N.AF_INET6
assertEqual "x2" x2 a2
expectException "empty result" $
getSockAddrImpl (\_ _ _ -> return []) 10 "foo"
------------------------------------------------------------------------------
testTrivials :: Test
testTrivials = testCase "test/trivials" $ do
coverShowInstance $ HA.makeProxyInfo undefined undefined undefined undefined
coverTypeableInstance $ AddressNotSupportedException undefined
coverShowInstance $ AddressNotSupportedException "ok"
------------------------------------------------------------------------------
localhost :: Int -> IO (N.SockAddr)
localhost p = snd <$> getSockAddr p "127.0.0.1"
------------------------------------------------------------------------------
localhost6 :: Int -> IO (N.SockAddr)
localhost6 p = snd <$> getSockAddr p "::1"
------------------------------------------------------------------------------
expectException :: String -> IO a -> IO ()
expectException name act = do
e <- E.try act
case e of
Left (z::E.SomeException) -> (length $ show z) `seq` return ()
Right _ -> fail $ name ++ ": expected exception, didn't get one"
------------------------------------------------------------------------------
coverTypeableInstance :: (Monad m, Typeable a) => a -> m ()
coverTypeableInstance a = typeOf a `seq` return ()
------------------------------------------------------------------------------
eatException :: IO a -> IO ()
eatException a = (a >> return ()) `E.catch` handler
where
handler :: E.SomeException -> IO ()
handler _ = return ()
------------------------------------------------------------------------------
-- | Kill the false negative on derived show instances.
coverShowInstance :: (MonadIO m, Show a) => a -> m ()
coverShowInstance x = liftIO (a >> b >> c)
where
a = eatException $ E.evaluate $ length $ showsPrec 0 x ""
b = eatException $ E.evaluate $ length $ show x
c = eatException $ E.evaluate $ length $ showList [x] ""
|