diff options
author | Sam T <sta.cs.vsu@gmail.com> | 2013-04-03 01:13:59 +0400 |
---|---|---|
committer | Sam T <sta.cs.vsu@gmail.com> | 2013-04-03 01:13:59 +0400 |
commit | 0ccce42b773783341765ef751ea6d962f70d593c (patch) | |
tree | 6a1e6418edf964c123372e52f03896dda471e26f | |
parent | 8f5985915ece236a29fcb340e1e0ef731e3214e9 (diff) |
+ tests
-rw-r--r-- | tests/client.hs | 27 | ||||
-rw-r--r-- | tests/encoding.hs | 59 | ||||
-rw-r--r-- | tests/info-hash.hs | 49 |
3 files changed, 135 insertions, 0 deletions
diff --git a/tests/client.hs b/tests/client.hs new file mode 100644 index 00000000..5697d18a --- /dev/null +++ b/tests/client.hs | |||
@@ -0,0 +1,27 @@ | |||
1 | module Main (main) where | ||
2 | |||
3 | import Data.Torrent | ||
4 | import Network.Torrent.THP | ||
5 | import System.Environment | ||
6 | import Data.ByteString as B | ||
7 | import Data.ByteString.Lazy as L | ||
8 | import Data.BEncode | ||
9 | |||
10 | main :: IO () | ||
11 | main = do | ||
12 | [path] <- getArgs | ||
13 | contents <- B.readFile path | ||
14 | |||
15 | let Right contents' = decode contents >>= return . L.toStrict . encode | ||
16 | print (contents' == contents) | ||
17 | -- let (a, b) = showInfos contents | ||
18 | -- print b | ||
19 | -- print a | ||
20 | -- print (encode b == encoded a) | ||
21 | |||
22 | let Right b = decode contents | ||
23 | let Right t = fromBEncode b | ||
24 | |||
25 | let req = defaultRequest (tAnnounce t) (tInfoHash t) | ||
26 | resp <- sendRequest req | ||
27 | print resp | ||
diff --git a/tests/encoding.hs b/tests/encoding.hs new file mode 100644 index 00000000..9141a1ba --- /dev/null +++ b/tests/encoding.hs | |||
@@ -0,0 +1,59 @@ | |||
1 | {-# OPTIONS -fno-warn-orphans #-} | ||
2 | module Main (main) where | ||
3 | |||
4 | import Control.Applicative | ||
5 | import Data.Word | ||
6 | import Data.ByteString (ByteString) | ||
7 | import qualified Data.ByteString as B | ||
8 | import Data.Serialize | ||
9 | |||
10 | import Test.Framework (defaultMain) | ||
11 | import Test.Framework.Providers.QuickCheck2 (testProperty) | ||
12 | import Test.QuickCheck | ||
13 | |||
14 | import Network.Torrent.PWP | ||
15 | |||
16 | positive :: Gen Int | ||
17 | positive = fromIntegral <$> (arbitrary :: Gen Word32) | ||
18 | |||
19 | instance Arbitrary ByteString where | ||
20 | arbitrary = B.pack <$> arbitrary | ||
21 | |||
22 | instance Arbitrary BlockIx where | ||
23 | arbitrary = BlockIx <$> positive <*> positive <*> positive | ||
24 | |||
25 | instance Arbitrary Block where | ||
26 | arbitrary = Block <$> positive <*> positive <*> arbitrary | ||
27 | |||
28 | instance Arbitrary Message where | ||
29 | arbitrary = oneof | ||
30 | [ pure KeepAlive | ||
31 | , pure Choke | ||
32 | , pure Unchoke | ||
33 | , pure Interested | ||
34 | , pure NotInterested | ||
35 | , Have <$> positive | ||
36 | , Bitfield <$> arbitrary | ||
37 | , Request <$> arbitrary | ||
38 | , Piece <$> arbitrary | ||
39 | , Cancel <$> arbitrary | ||
40 | , Port <$> choose (0, fromIntegral (maxBound :: Word16)) | ||
41 | ] | ||
42 | |||
43 | |||
44 | encodeMessages :: [Message] -> ByteString | ||
45 | encodeMessages xs = runPut (mapM_ put xs) | ||
46 | |||
47 | decodeMessages :: ByteString -> Either String [Message] | ||
48 | decodeMessages = runGet (many get) | ||
49 | |||
50 | -- | TODO move tests | ||
51 | prop_encoding :: [Message] -> Bool | ||
52 | prop_encoding msgs = decodeMessages (encodeMessages msgs) == Right msgs | ||
53 | |||
54 | |||
55 | main :: IO () | ||
56 | main = do | ||
57 | defaultMain | ||
58 | [ testProperty "encode <-> decode" prop_encoding | ||
59 | ] \ No newline at end of file | ||
diff --git a/tests/info-hash.hs b/tests/info-hash.hs new file mode 100644 index 00000000..1e511e43 --- /dev/null +++ b/tests/info-hash.hs | |||
@@ -0,0 +1,49 @@ | |||
1 | {-# LANGUAGE OverloadedStrings #-} | ||
2 | module Main (main) where | ||
3 | |||
4 | import Control.Applicative | ||
5 | import Data.Foldable | ||
6 | import Data.Maybe | ||
7 | import qualified Data.Map as M | ||
8 | import Data.ByteString (ByteString) | ||
9 | import qualified Data.ByteString as B | ||
10 | import qualified Data.ByteString.Lazy as L | ||
11 | import qualified Data.ByteString.Builder as B | ||
12 | import qualified Data.ByteString.Builder.Prim as B | ||
13 | |||
14 | import System.Environment | ||
15 | |||
16 | import Data.Torrent | ||
17 | import Data.BEncode | ||
18 | |||
19 | |||
20 | checkInfo :: B.ByteString | ||
21 | checkInfo = "0221caf96aa3cb94f0f58d458e78b0fc344ad8bf" | ||
22 | |||
23 | -- | _should_ be 'id' if content is correct | ||
24 | reencode :: B.ByteString -> Result L.ByteString | ||
25 | reencode content = (encode . toBEncode . (`asTypeOf` (undefined :: Torrent))) | ||
26 | `fmap` (fromBEncode =<< decode content) | ||
27 | |||
28 | ppHex :: B.ByteString -> B.ByteString | ||
29 | ppHex = L.toStrict . B.toLazyByteString . foldMap (B.primFixed B.word8HexFixed) . B.unpack | ||
30 | |||
31 | chunk :: Int -> B.ByteString -> [B.ByteString] | ||
32 | chunk size b | B.length b == 0 = [b] | ||
33 | | otherwise = | ||
34 | let (x, xs) = B.splitAt size b | ||
35 | in x : chunk size xs | ||
36 | |||
37 | showInfos :: ByteString -> (TorrentInfo, BEncode) | ||
38 | showInfos bs = | ||
39 | let Right (be@(BDict dict)) = decode bs | ||
40 | Right t = tInfo <$> (fromBEncode be) | ||
41 | orig = BDict $ let BDict info = fromJust (M.lookup "info" dict) | ||
42 | in M.insert "pieces" (BString "") info | ||
43 | in (t { tPieces = "" }, orig) | ||
44 | |||
45 | main :: IO () | ||
46 | main = do | ||
47 | [path] <- getArgs | ||
48 | Right t <- fromFile path | ||
49 | B.putStrLn (ppHex (tInfoHash t)) | ||