Blame tests/Regression.hs

Packit 090c59
-- | Tests for things that didn't work in the past.
Packit 090c59
module Main where
Packit 090c59
Packit 090c59
import Network.Socket
Packit 090c59
Packit 090c59
import Control.Exception
Packit 090c59
Packit 090c59
import Test.Framework (Test, defaultMain)
Packit 090c59
import Test.Framework.Providers.HUnit (testCase)
Packit 090c59
import Test.HUnit (assertFailure)
Packit 090c59
Packit 090c59
------------------------------------------------------------------------
Packit 090c59
-- Tests
Packit 090c59
Packit 090c59
-- Used to segfault on OS X 10.8.2 due to AI_NUMERICSERV being set
Packit 090c59
-- without a service being set. This is a OS X bug.
Packit 090c59
testGetAddrInfo :: IO ()
Packit 090c59
testGetAddrInfo = do
Packit 090c59
    let hints = defaultHints { addrFlags = [AI_NUMERICSERV] }
Packit 090c59
    _ <- getAddrInfo (Just hints) (Just "localhost") Nothing
Packit 090c59
    return ()
Packit 090c59
Packit 090c59
mkBadSocketAndTry :: (Socket -> IO a) -> IO (Either IOException a)
Packit 090c59
mkBadSocketAndTry f = do
Packit 090c59
    sock <- socket AF_INET Stream defaultProtocol
Packit 090c59
    try $ f sock
Packit 090c59
Packit 090c59
-- Because of 64/32 bitness issues, -1 wasn't correctly checked for on Windows.
Packit 090c59
-- See also GHC ticket #12010
Packit 090c59
badRecvShouldThrow :: IO ()
Packit 090c59
badRecvShouldThrow = do
Packit 090c59
    res <- mkBadSocketAndTry $ flip recv 1024
Packit 090c59
    case res of
Packit 090c59
        Left _ex -> return ()
Packit 090c59
        Right _  -> assertFailure "recv didn't throw an exception"
Packit 090c59
Packit 090c59
badSendShouldThrow :: IO ()
Packit 090c59
badSendShouldThrow = do
Packit 090c59
    res <- mkBadSocketAndTry $ flip send "hello"
Packit 090c59
    case res of
Packit 090c59
        Left _ex -> return ()
Packit 090c59
        Right _  -> assertFailure "send didn't throw an exception"
Packit 090c59
Packit 090c59
------------------------------------------------------------------------
Packit 090c59
-- List of all tests
Packit 090c59
Packit 090c59
tests :: [Test]
Packit 090c59
tests =
Packit 090c59
    [ testCase "testGetAddrInfo" testGetAddrInfo
Packit 090c59
    , testCase "badRecvShouldThrow" badRecvShouldThrow
Packit 090c59
    , testCase "badSendShouldThrow" badSendShouldThrow
Packit 090c59
    ]
Packit 090c59
Packit 090c59
------------------------------------------------------------------------
Packit 090c59
-- Test harness
Packit 090c59
Packit 090c59
main :: IO ()
Packit 090c59
main = withSocketsDo $ defaultMain tests