Targets.hs 2.6 KB

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