diff options
author | Sam Truzjan <pxqr.sta@gmail.com> | 2013-12-09 06:37:28 +0400 |
---|---|---|
committer | Sam Truzjan <pxqr.sta@gmail.com> | 2013-12-09 06:37:28 +0400 |
commit | b00f17874babc0a63a501a4fb33f4f9c8b7d5c7d (patch) | |
tree | f945b2fa2ada038a927debc3ba0b8f8fff8733ab /tests/Network | |
parent | c147b181094937d780b93ca82fb8604deeb9a7cd (diff) |
Add stats method to PeerMessage class
Diffstat (limited to 'tests/Network')
-rw-r--r-- | tests/Network/BitTorrent/Exchange/BlockSpec.hs | 16 | ||||
-rw-r--r-- | tests/Network/BitTorrent/Exchange/MessageSpec.hs | 47 |
2 files changed, 63 insertions, 0 deletions
diff --git a/tests/Network/BitTorrent/Exchange/BlockSpec.hs b/tests/Network/BitTorrent/Exchange/BlockSpec.hs new file mode 100644 index 00000000..0712a21d --- /dev/null +++ b/tests/Network/BitTorrent/Exchange/BlockSpec.hs | |||
@@ -0,0 +1,16 @@ | |||
1 | module Network.BitTorrent.Exchange.BlockSpec (spec) where | ||
2 | import Control.Applicative | ||
3 | import Test.Hspec | ||
4 | import Test.QuickCheck | ||
5 | |||
6 | import Network.BitTorrent.Exchange.Block | ||
7 | |||
8 | |||
9 | instance Arbitrary a => Arbitrary (Block a) where | ||
10 | arbitrary = Block <$> arbitrary <*> arbitrary <*> arbitrary | ||
11 | |||
12 | instance Arbitrary BlockIx where | ||
13 | arbitrary = BlockIx <$> arbitrary <*> arbitrary <*> arbitrary | ||
14 | |||
15 | spec :: Spec | ||
16 | spec = return () \ No newline at end of file | ||
diff --git a/tests/Network/BitTorrent/Exchange/MessageSpec.hs b/tests/Network/BitTorrent/Exchange/MessageSpec.hs index 38a20112..5d332eaa 100644 --- a/tests/Network/BitTorrent/Exchange/MessageSpec.hs +++ b/tests/Network/BitTorrent/Exchange/MessageSpec.hs | |||
@@ -2,6 +2,7 @@ module Network.BitTorrent.Exchange.MessageSpec (spec) where | |||
2 | import Control.Applicative | 2 | import Control.Applicative |
3 | import Control.Exception | 3 | import Control.Exception |
4 | import Data.ByteString as BS | 4 | import Data.ByteString as BS |
5 | import Data.ByteString.Lazy as BL | ||
5 | import Data.Default | 6 | import Data.Default |
6 | import Data.List as L | 7 | import Data.List as L |
7 | import Data.Set as S | 8 | import Data.Set as S |
@@ -10,9 +11,11 @@ import Data.String | |||
10 | import Test.Hspec | 11 | import Test.Hspec |
11 | import Test.QuickCheck | 12 | import Test.QuickCheck |
12 | 13 | ||
14 | import Data.Torrent.BitfieldSpec () | ||
13 | import Data.Torrent.InfoHashSpec () | 15 | import Data.Torrent.InfoHashSpec () |
14 | import Network.BitTorrent.CoreSpec () | 16 | import Network.BitTorrent.CoreSpec () |
15 | import Network.BitTorrent.Core | 17 | import Network.BitTorrent.Core |
18 | import Network.BitTorrent.Exchange.BlockSpec () | ||
16 | import Network.BitTorrent.Exchange.Message | 19 | import Network.BitTorrent.Exchange.Message |
17 | 20 | ||
18 | instance Arbitrary Extension where | 21 | instance Arbitrary Extension where |
@@ -28,6 +31,45 @@ instance Arbitrary Handshake where | |||
28 | arbitrary = Handshake <$> arbitrary <*> arbitrary | 31 | arbitrary = Handshake <$> arbitrary <*> arbitrary |
29 | <*> arbitrary <*> arbitrary | 32 | <*> arbitrary <*> arbitrary |
30 | 33 | ||
34 | instance Arbitrary StatusUpdate where | ||
35 | arbitrary = frequency | ||
36 | [ (1, Choking <$> arbitrary) | ||
37 | , (1, Interested <$> arbitrary) | ||
38 | ] | ||
39 | |||
40 | instance Arbitrary Available where | ||
41 | arbitrary = frequency | ||
42 | [ (1, Have <$> arbitrary) | ||
43 | , (1, Bitfield <$> arbitrary) | ||
44 | ] | ||
45 | |||
46 | instance Arbitrary Transfer where | ||
47 | arbitrary = frequency | ||
48 | [ (1, Request <$> arbitrary) | ||
49 | , (1, Piece <$> arbitrary) | ||
50 | , (1, Cancel <$> arbitrary) | ||
51 | ] | ||
52 | |||
53 | instance Arbitrary FastMessage where | ||
54 | arbitrary = frequency | ||
55 | [ (1, pure HaveAll) | ||
56 | , (1, pure HaveNone) | ||
57 | , (1, SuggestPiece <$> arbitrary) | ||
58 | , (1, RejectRequest <$> arbitrary) | ||
59 | , (1, AllowedFast <$> arbitrary) | ||
60 | ] | ||
61 | |||
62 | instance Arbitrary Message where | ||
63 | arbitrary = frequency | ||
64 | [ (1, pure KeepAlive) | ||
65 | , (1, Status <$> arbitrary) | ||
66 | , (1, Available <$> arbitrary) | ||
67 | , (1, Transfer <$> arbitrary) | ||
68 | , (1, Fast <$> arbitrary) | ||
69 | ] | ||
70 | |||
71 | -- TODO test extension protocol | ||
72 | |||
31 | spec :: Spec | 73 | spec :: Spec |
32 | spec = do | 74 | spec = do |
33 | describe "Caps" $ do | 75 | describe "Caps" $ do |
@@ -38,6 +80,11 @@ spec = do | |||
38 | S.fromList (fromCaps (toCaps (S.toList extSet) :: Caps)) | 80 | S.fromList (fromCaps (toCaps (S.toList extSet) :: Caps)) |
39 | `shouldBe` extSet | 81 | `shouldBe` extSet |
40 | 82 | ||
83 | describe "ByteStats" $ do | ||
84 | it "preserve size" $ property $ \ msg -> | ||
85 | byteLength (stats msg) `shouldBe` | ||
86 | fromIntegral (BS.length (S.encode (msg :: Message))) | ||
87 | |||
41 | describe "ProtocolString" $ do | 88 | describe "ProtocolString" $ do |
42 | it "fail to construct invalid string" $ do | 89 | it "fail to construct invalid string" $ do |
43 | let str = L.replicate 500 'x' | 90 | let str = L.replicate 500 'x' |