Targets.hs 3.1 KB

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