Targets.hs 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. {-# LANGUAGE OverloadedStrings #-}
  2. module Targets (tests) where
  3. import Distribution.TestSuite
  4. import Base (simpleTest)
  5. import Control.Concurrent(forkIO)
  6. import qualified System.IO.Uniform as U
  7. import System.Timeout (timeout)
  8. import qualified Data.ByteString.Char8 as C8
  9. tests :: IO [Test]
  10. tests = return [
  11. simpleTest "network" testNetwork,
  12. simpleTest "file" testFile,
  13. simpleTest "network TLS" testTls
  14. ]
  15. testNetwork :: IO Progress
  16. testNetwork = do
  17. recv <- U.bindPort 8888
  18. forkIO $ do
  19. s <- U.accept recv
  20. l <- U.uRead s 100
  21. U.uPut s l
  22. U.uClose s
  23. return ()
  24. r' <- timeout 1000000 $ do
  25. s <- U.connectToHost "127.0.0.1" 8888
  26. let l = "abcdef\n"
  27. U.uPut s l
  28. l' <- U.uRead s 100
  29. U.uClose s
  30. if l == l'
  31. then return . Finished $ Pass
  32. else return . Finished . Fail . C8.unpack $ l'
  33. U.closePort recv
  34. case r' of
  35. Just r -> return r
  36. Nothing -> return . Finished . Fail $ "Execution blocked"
  37. testFile :: IO Progress
  38. testFile = do
  39. let file = "test/testFile"
  40. s <- U.openFile file
  41. let l = "abcde\n"
  42. U.uPut s l
  43. U.uClose s
  44. s' <- U.openFile file
  45. l' <- U.uRead s' 100
  46. U.uClose s'
  47. if l == l'
  48. then return . Finished $ Pass
  49. else return . Finished . Fail . C8.unpack $ l'
  50. testTls :: IO Progress
  51. testTls = do
  52. recv <- U.bindPort 8888
  53. let set = U.TlsSettings "test/key.pem" "test/cert.pem" "test/dh.pem"
  54. forkIO $ do
  55. s' <- U.accept recv
  56. s <- U.startTls set s'
  57. l <- U.uRead s 100
  58. U.uPut s l
  59. U.uClose s
  60. return ()
  61. r' <- timeout 1000000 $ do
  62. s' <- U.connectToHost "127.0.0.1" 8888
  63. s <- U.startTls set s'
  64. let l = "abcdef\n"
  65. U.uPut s l
  66. l' <- U.uRead s 100
  67. U.uClose s
  68. if l == l'
  69. then return . Finished $ Pass
  70. else return . Finished . Fail . C8.unpack $ l'
  71. U.closePort recv
  72. case r' of
  73. Just r -> return r
  74. Nothing -> return . Finished . Fail $ "Execution blocked"