summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSam T <sta.cs.vsu@gmail.com>2013-04-03 01:13:59 +0400
committerSam T <sta.cs.vsu@gmail.com>2013-04-03 01:13:59 +0400
commit0ccce42b773783341765ef751ea6d962f70d593c (patch)
tree6a1e6418edf964c123372e52f03896dda471e26f
parent8f5985915ece236a29fcb340e1e0ef731e3214e9 (diff)
+ tests
-rw-r--r--tests/client.hs27
-rw-r--r--tests/encoding.hs59
-rw-r--r--tests/info-hash.hs49
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 @@
1module Main (main) where
2
3import Data.Torrent
4import Network.Torrent.THP
5import System.Environment
6import Data.ByteString as B
7import Data.ByteString.Lazy as L
8import Data.BEncode
9
10main :: IO ()
11main = 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 #-}
2module Main (main) where
3
4import Control.Applicative
5import Data.Word
6import Data.ByteString (ByteString)
7import qualified Data.ByteString as B
8import Data.Serialize
9
10import Test.Framework (defaultMain)
11import Test.Framework.Providers.QuickCheck2 (testProperty)
12import Test.QuickCheck
13
14import Network.Torrent.PWP
15
16positive :: Gen Int
17positive = fromIntegral <$> (arbitrary :: Gen Word32)
18
19instance Arbitrary ByteString where
20 arbitrary = B.pack <$> arbitrary
21
22instance Arbitrary BlockIx where
23 arbitrary = BlockIx <$> positive <*> positive <*> positive
24
25instance Arbitrary Block where
26 arbitrary = Block <$> positive <*> positive <*> arbitrary
27
28instance 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
44encodeMessages :: [Message] -> ByteString
45encodeMessages xs = runPut (mapM_ put xs)
46
47decodeMessages :: ByteString -> Either String [Message]
48decodeMessages = runGet (many get)
49
50-- | TODO move tests
51prop_encoding :: [Message] -> Bool
52prop_encoding msgs = decodeMessages (encodeMessages msgs) == Right msgs
53
54
55main :: IO ()
56main = 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 #-}
2module Main (main) where
3
4import Control.Applicative
5import Data.Foldable
6import Data.Maybe
7import qualified Data.Map as M
8import Data.ByteString (ByteString)
9import qualified Data.ByteString as B
10import qualified Data.ByteString.Lazy as L
11import qualified Data.ByteString.Builder as B
12import qualified Data.ByteString.Builder.Prim as B
13
14import System.Environment
15
16import Data.Torrent
17import Data.BEncode
18
19
20checkInfo :: B.ByteString
21checkInfo = "0221caf96aa3cb94f0f58d458e78b0fc344ad8bf"
22
23-- | _should_ be 'id' if content is correct
24reencode :: B.ByteString -> Result L.ByteString
25reencode content = (encode . toBEncode . (`asTypeOf` (undefined :: Torrent)))
26 `fmap` (fromBEncode =<< decode content)
27
28ppHex :: B.ByteString -> B.ByteString
29ppHex = L.toStrict . B.toLazyByteString . foldMap (B.primFixed B.word8HexFixed) . B.unpack
30
31chunk :: Int -> B.ByteString -> [B.ByteString]
32chunk size b | B.length b == 0 = [b]
33 | otherwise =
34 let (x, xs) = B.splitAt size b
35 in x : chunk size xs
36
37showInfos :: ByteString -> (TorrentInfo, BEncode)
38showInfos 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
45main :: IO ()
46main = do
47 [path] <- getArgs
48 Right t <- fromFile path
49 B.putStrLn (ppHex (tInfoHash t))