diff options
author | Sam T <pxqr.sta@gmail.com> | 2013-06-07 21:49:10 +0400 |
---|---|---|
committer | Sam T <pxqr.sta@gmail.com> | 2013-06-07 21:49:10 +0400 |
commit | 9356528a6a14e35448d5b8556bc951b8eefeef1e (patch) | |
tree | ea47c1ddf3148ac5eb26e874fcae3284c96c2f9a /tests/Encoding.hs | |
parent | 2940d8497947ebf18a70343b4a787a5e28c85754 (diff) |
~ Merge Encoding to Main.
Diffstat (limited to 'tests/Encoding.hs')
-rw-r--r-- | tests/Encoding.hs | 96 |
1 files changed, 0 insertions, 96 deletions
diff --git a/tests/Encoding.hs b/tests/Encoding.hs deleted file mode 100644 index 78f0dfc1..00000000 --- a/tests/Encoding.hs +++ /dev/null | |||
@@ -1,96 +0,0 @@ | |||
1 | {-# LANGUAGE StandaloneDeriving #-} | ||
2 | {-# LANGUAGE GeneralizedNewtypeDeriving #-} | ||
3 | {-# OPTIONS -fno-warn-orphans #-} | ||
4 | module Encoding where | ||
5 | |||
6 | import Control.Applicative | ||
7 | import Data.Word | ||
8 | import Data.ByteString (ByteString) | ||
9 | import qualified Data.ByteString as B | ||
10 | import Data.Serialize | ||
11 | import Test.Framework (Test) | ||
12 | import Test.Framework.Providers.QuickCheck2 (testProperty) | ||
13 | import Test.QuickCheck | ||
14 | |||
15 | |||
16 | import Network.URI | ||
17 | import Network | ||
18 | |||
19 | |||
20 | import Data.Bitfield | ||
21 | import Data.Torrent | ||
22 | import Network.BitTorrent | ||
23 | |||
24 | |||
25 | positive :: Gen Int | ||
26 | positive = fromIntegral <$> (arbitrary :: Gen Word32) | ||
27 | |||
28 | instance Arbitrary ByteString where | ||
29 | arbitrary = B.pack <$> arbitrary | ||
30 | |||
31 | instance Arbitrary BlockIx where | ||
32 | arbitrary = BlockIx <$> positive <*> positive <*> positive | ||
33 | |||
34 | instance Arbitrary Block where | ||
35 | arbitrary = Block <$> positive <*> positive <*> arbitrary | ||
36 | |||
37 | instance Arbitrary Bitfield where | ||
38 | arbitrary = mkBitfield <$> (succ . min 1000 <$> positive) | ||
39 | <*> arbitrary | ||
40 | |||
41 | instance Arbitrary PortNumber where | ||
42 | arbitrary = fromIntegral <$> (arbitrary :: Gen Word16) | ||
43 | |||
44 | instance Arbitrary Message where | ||
45 | arbitrary = oneof | ||
46 | [ pure KeepAlive | ||
47 | , pure Choke | ||
48 | , pure Unchoke | ||
49 | , pure Interested | ||
50 | , pure NotInterested | ||
51 | , Have <$> positive | ||
52 | , Bitfield <$> arbitrary | ||
53 | , Request <$> arbitrary | ||
54 | , Piece <$> arbitrary | ||
55 | , Cancel <$> arbitrary | ||
56 | , Port <$> arbitrary | ||
57 | ] | ||
58 | |||
59 | instance Arbitrary PeerID where | ||
60 | arbitrary = azureusStyle <$> pure defaultClientID | ||
61 | <*> arbitrary | ||
62 | <*> arbitrary | ||
63 | |||
64 | instance Arbitrary InfoHash where | ||
65 | arbitrary = (hash . B.pack) <$> arbitrary | ||
66 | |||
67 | instance Arbitrary Handshake where | ||
68 | arbitrary = defaultHandshake <$> arbitrary <*> arbitrary | ||
69 | |||
70 | |||
71 | data T a = T | ||
72 | |||
73 | prop_encoding :: (Serialize a, Eq a) => T a -> [a] -> Bool | ||
74 | prop_encoding _ msgs = decode (encode msgs) == Right msgs | ||
75 | |||
76 | -- | Note that in 6 esample we intensionally do not agree with specification, | ||
77 | -- because taking in account '/' in query parameter seems to be meaningless. | ||
78 | -- (And thats because other clients do not chunk uri by parts) | ||
79 | -- Moreover in practice there should be no difference. (I hope) | ||
80 | -- | ||
81 | test_scrape_url :: [Test] | ||
82 | test_scrape_url = zipWith mkTest [1 :: Int ..] (check `map` tests) | ||
83 | where | ||
84 | check (iu, ou) = (parseURI iu >>= (`scrapeURL` []) >>= return . show) == ou | ||
85 | tests = | ||
86 | [ ("http://example.com/announce" , Just "http://example.com/scrape") | ||
87 | , ("http://example.com/x/announce" , Just "http://example.com/x/scrape") | ||
88 | , ("http://example.com/announce.php" , Just "http://example.com/scrape.php") | ||
89 | , ("http://example.com/a" , Nothing) | ||
90 | , ("http://example.com/announce?x2%0644", Just "http://example.com/scrape?x2%0644") | ||
91 | , ("http://example.com/announce?x=2/4" , Just "http://example.com/scrape?x=2/4") | ||
92 | -- , ("http://example.com/announce?x=2/4" , Nothing) -- by specs | ||
93 | , ("http://example.com/x%064announce" , Nothing) | ||
94 | ] | ||
95 | |||
96 | mkTest i = testProperty ("scrape test #" ++ show i) | ||