Targets.hs 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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. import Data.ByteString (ByteString)
  10. import qualified Data.ByteString as BS
  11. tests :: IO [Test]
  12. tests = return [
  13. simpleTest "network" testNetwork,
  14. simpleTest "file" testFile,
  15. simpleTest "network TLS" testTls,
  16. simpleTest "byte string" testBS
  17. ]
  18. testNetwork :: IO Progress
  19. testNetwork = do
  20. recv <- U.bindPort 8888
  21. forkIO $ do
  22. s <- U.accept recv
  23. l <- U.uRead s 100
  24. U.uPut s l
  25. U.uClose s
  26. return ()
  27. r' <- timeout 1000000 $ do
  28. s <- U.connectToHost "127.0.0.1" 8888
  29. let l = "abcdef\n"
  30. U.uPut s l
  31. l' <- U.uRead s 100
  32. U.uClose s
  33. if l == l'
  34. then return . Finished $ Pass
  35. else return . Finished . Fail . C8.unpack $ l'
  36. U.closePort recv
  37. case r' of
  38. Just r -> return r
  39. Nothing -> return . Finished . Fail $ "Execution blocked"
  40. testFile :: IO Progress
  41. testFile = do
  42. let file = "test/testFile"
  43. s <- U.openFile file
  44. let l = "abcde\n"
  45. U.uPut s l
  46. U.uClose s
  47. s' <- U.openFile file
  48. l' <- U.uRead s' 100
  49. U.uClose s'
  50. if l == l'
  51. then return . Finished $ Pass
  52. else return . Finished . Fail . C8.unpack $ l'
  53. testTls :: IO Progress
  54. testTls = do
  55. recv <- U.bindPort 8888
  56. let set = U.TlsSettings "test/key.pem" "test/cert.pem" "test/dh.pem"
  57. forkIO $ do
  58. s' <- U.accept recv
  59. s <- U.startTls set s'
  60. l <- U.uRead s 100
  61. U.uPut s l
  62. U.uClose s
  63. return ()
  64. r' <- timeout 1000000 $ do
  65. s' <- U.connectToHost "127.0.0.1" 8888
  66. s <- U.startTls set s'
  67. let l = "abcdef\n"
  68. U.uPut s l
  69. l' <- U.uRead s 100
  70. U.uClose s
  71. if l == l'
  72. then return . Finished $ Pass
  73. else return . Finished . Fail . C8.unpack $ l'
  74. U.closePort recv
  75. case r' of
  76. Just r -> return r
  77. Nothing -> return . Finished . Fail $ "Execution blocked"
  78. testBS :: IO Progress
  79. testBS = do
  80. let dt = "Some data to test ByteString"
  81. (len, echo) <- U.withByteStringIO' dt (
  82. \io -> let
  83. count = countAndEcho io :: Int -> ByteString -> IO Int
  84. in U.mapOverInput io 2 count 0
  85. ) :: IO (Int, ByteString)
  86. if dt /= echo || BS.length dt /= len
  87. then return . Finished . Fail $ "Failure on ByteStringIO test"
  88. else return . Finished $ Pass
  89. where
  90. countAndEcho :: U.UniformIO io => io -> Int -> ByteString -> IO Int
  91. countAndEcho io initial dt = do
  92. U.uPut io dt
  93. return $ initial + BS.length dt