diff options
Diffstat (limited to 'tests')
-rw-r--r-- | tests/InfoHash.hs | 29 | ||||
-rw-r--r-- | tests/Main.hs | 40 |
2 files changed, 30 insertions, 39 deletions
diff --git a/tests/InfoHash.hs b/tests/InfoHash.hs deleted file mode 100644 index bbf77545..00000000 --- a/tests/InfoHash.hs +++ /dev/null | |||
@@ -1,29 +0,0 @@ | |||
1 | {-# LANGUAGE OverloadedStrings #-} | ||
2 | module Main (main) where | ||
3 | |||
4 | import qualified Data.ByteString.Char8 as BC | ||
5 | import Data.Torrent | ||
6 | |||
7 | import System.Environment | ||
8 | import System.Exit | ||
9 | |||
10 | checkInfo :: String | ||
11 | checkInfo = "0221caf96aa3cb94f0f58d458e78b0fc344ad8bf" | ||
12 | |||
13 | torrentFileName :: String | ||
14 | torrentFileName = "res/dapper-dvd-amd64.iso.torrent" | ||
15 | |||
16 | main :: IO () | ||
17 | main = do | ||
18 | args <- getArgs | ||
19 | let path = if length args == 0 then torrentFileName else head args | ||
20 | |||
21 | t <- fromFile path | ||
22 | |||
23 | BC.putStr "info hash: " | ||
24 | print (ppInfoHash (tInfoHash t)) | ||
25 | |||
26 | let passed = checkInfo == show (ppInfoHash (tInfoHash t)) | ||
27 | |||
28 | print passed | ||
29 | if passed then exitSuccess else exitFailure | ||
diff --git a/tests/Main.hs b/tests/Main.hs index bcadc97e..3c7bfcd3 100644 --- a/tests/Main.hs +++ b/tests/Main.hs | |||
@@ -16,7 +16,9 @@ import Data.Text as T | |||
16 | 16 | ||
17 | import Network | 17 | import Network |
18 | import Network.URI | 18 | import Network.URI |
19 | |||
19 | import System.Directory | 20 | import System.Directory |
21 | import System.FilePath | ||
20 | 22 | ||
21 | import Test.QuickCheck as QC | 23 | import Test.QuickCheck as QC |
22 | import Test.HUnit as HU | 24 | import Test.HUnit as HU |
@@ -105,6 +107,18 @@ instance Arbitrary Torrent where | |||
105 | <*> arbitrary <*> arbitrary <*> arbitrary | 107 | <*> arbitrary <*> arbitrary <*> arbitrary |
106 | <*> arbitrary <*> pure Nothing <*> arbitrary | 108 | <*> arbitrary <*> pure Nothing <*> arbitrary |
107 | 109 | ||
110 | -- TODO add a few more torrents here | ||
111 | torrentList :: [(FilePath, String)] | ||
112 | torrentList = | ||
113 | [ ( "res" </> "dapper-dvd-amd64.iso.torrent" | ||
114 | , "0221caf96aa3cb94f0f58d458e78b0fc344ad8bf") | ||
115 | ] | ||
116 | |||
117 | checkInfoHash :: (FilePath, String) -> Assertion | ||
118 | checkInfoHash (path, expectedHash) = check =<< fromFile path | ||
119 | where | ||
120 | check t = expectedHash @=? show (ppInfoHash (tInfoHash t)) | ||
121 | |||
108 | {----------------------------------------------------------------------- | 122 | {----------------------------------------------------------------------- |
109 | Handshake | 123 | Handshake |
110 | -----------------------------------------------------------------------} | 124 | -----------------------------------------------------------------------} |
@@ -210,6 +224,10 @@ prop_messageEncoding msg | |||
210 | MemMap | 224 | MemMap |
211 | -----------------------------------------------------------------------} | 225 | -----------------------------------------------------------------------} |
212 | 226 | ||
227 | tmpdir :: FilePath | ||
228 | tmpdir = "tmp" | ||
229 | |||
230 | boundaryTest :: Assertion | ||
213 | boundaryTest = do | 231 | boundaryTest = do |
214 | f <- mallocTo (Fixed.interval 0 1) Fixed.empty | 232 | f <- mallocTo (Fixed.interval 0 1) Fixed.empty |
215 | f <- mallocTo (Fixed.interval 1 2) f | 233 | f <- mallocTo (Fixed.interval 1 2) f |
@@ -218,16 +236,18 @@ boundaryTest = do | |||
218 | bs <- readBytes (Fixed.interval 0 2) f | 236 | bs <- readBytes (Fixed.interval 0 2) f |
219 | "\x1\x2" @=? bs | 237 | "\x1\x2" @=? bs |
220 | 238 | ||
239 | mmapSingle :: Assertion | ||
221 | mmapSingle = do | 240 | mmapSingle = do |
222 | f <- mmapTo "single.test" (10, 5) 5 Fixed.empty | 241 | f <- mmapTo (tmpdir </> "single.test") (10, 5) 5 Fixed.empty |
223 | writeBytes (Fixed.interval 5 5) "abcde" f | 242 | writeBytes (Fixed.interval 5 5) "abcde" f |
224 | bs <- readBytes (Fixed.interval 5 5) f | 243 | bs <- readBytes (Fixed.interval 5 5) f |
225 | "abcde" @=? bs | 244 | "abcde" @=? bs |
226 | 245 | ||
246 | coalesceTest :: Assertion | ||
227 | coalesceTest = do | 247 | coalesceTest = do |
228 | f <- mmapTo "a.test" (0, 1) 10 Fixed.empty | 248 | f <- mmapTo (tmpdir </> "a.test") (0, 1) 10 Fixed.empty |
229 | f <- mmapTo "bc.test" (0, 2) 12 f | 249 | f <- mmapTo (tmpdir </> "bc.test") (0, 2) 12 f |
230 | f <- mmapTo "c.test" (0, 1) 13 f | 250 | f <- mmapTo (tmpdir </> "c.test") (0, 1) 13 f |
231 | writeBytes (Fixed.interval 10 4) "abcd" f | 251 | writeBytes (Fixed.interval 10 4) "abcd" f |
232 | bs <- readBytes (Fixed.interval 10 4) f | 252 | bs <- readBytes (Fixed.interval 10 4) f |
233 | "abcd" @=? bs | 253 | "abcd" @=? bs |
@@ -243,6 +263,7 @@ allTests = | |||
243 | , testProperty "rarest in range" prop_rarestInRange | 263 | , testProperty "rarest in range" prop_rarestInRange |
244 | , testProperty "min less that max" prop_minMax | 264 | , testProperty "min less that max" prop_minMax |
245 | , testProperty "difference de morgan" prop_differenceDeMorgan | 265 | , testProperty "difference de morgan" prop_differenceDeMorgan |
266 | |||
246 | -- torrent module | 267 | -- torrent module |
247 | , testProperty "file info encoding" $ | 268 | , testProperty "file info encoding" $ |
248 | prop_properBEncode (T :: T FileInfo) | 269 | prop_properBEncode (T :: T FileInfo) |
@@ -250,9 +271,11 @@ allTests = | |||
250 | prop_properBEncode (T :: T ContentInfo) | 271 | prop_properBEncode (T :: T ContentInfo) |
251 | , testProperty "torrent encoding" $ | 272 | , testProperty "torrent encoding" $ |
252 | prop_properBEncode (T :: T Torrent) | 273 | prop_properBEncode (T :: T Torrent) |
253 | 274 | ] ++ | |
254 | -- handshake module | 275 | fmap (testCase "info hash" . checkInfoHash) torrentList |
255 | , testProperty "handshake encoding" $ | 276 | ++ |
277 | [ -- handshake module | ||
278 | testProperty "handshake encoding" $ | ||
256 | prop_cerealEncoding (T :: T Handshake) | 279 | prop_cerealEncoding (T :: T Handshake) |
257 | , testProperty "message encoding" prop_messageEncoding | 280 | , testProperty "message encoding" prop_messageEncoding |
258 | 281 | ||
@@ -264,8 +287,5 @@ allTests = | |||
264 | 287 | ||
265 | main :: IO () | 288 | main :: IO () |
266 | main = do | 289 | main = do |
267 | let tmpdir = "tmp" -- for mem map test cases | ||
268 | createDirectoryIfMissing True tmpdir | 290 | createDirectoryIfMissing True tmpdir |
269 | setCurrentDirectory tmpdir | ||
270 | |||
271 | defaultMain allTests | 291 | defaultMain allTests |