summaryrefslogtreecommitdiff
path: root/tests/Network
diff options
context:
space:
mode:
authorSam Truzjan <pxqr.sta@gmail.com>2013-12-09 06:37:28 +0400
committerSam Truzjan <pxqr.sta@gmail.com>2013-12-09 06:37:28 +0400
commitb00f17874babc0a63a501a4fb33f4f9c8b7d5c7d (patch)
treef945b2fa2ada038a927debc3ba0b8f8fff8733ab /tests/Network
parentc147b181094937d780b93ca82fb8604deeb9a7cd (diff)
Add stats method to PeerMessage class
Diffstat (limited to 'tests/Network')
-rw-r--r--tests/Network/BitTorrent/Exchange/BlockSpec.hs16
-rw-r--r--tests/Network/BitTorrent/Exchange/MessageSpec.hs47
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 @@
1module Network.BitTorrent.Exchange.BlockSpec (spec) where
2import Control.Applicative
3import Test.Hspec
4import Test.QuickCheck
5
6import Network.BitTorrent.Exchange.Block
7
8
9instance Arbitrary a => Arbitrary (Block a) where
10 arbitrary = Block <$> arbitrary <*> arbitrary <*> arbitrary
11
12instance Arbitrary BlockIx where
13 arbitrary = BlockIx <$> arbitrary <*> arbitrary <*> arbitrary
14
15spec :: Spec
16spec = 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
2import Control.Applicative 2import Control.Applicative
3import Control.Exception 3import Control.Exception
4import Data.ByteString as BS 4import Data.ByteString as BS
5import Data.ByteString.Lazy as BL
5import Data.Default 6import Data.Default
6import Data.List as L 7import Data.List as L
7import Data.Set as S 8import Data.Set as S
@@ -10,9 +11,11 @@ import Data.String
10import Test.Hspec 11import Test.Hspec
11import Test.QuickCheck 12import Test.QuickCheck
12 13
14import Data.Torrent.BitfieldSpec ()
13import Data.Torrent.InfoHashSpec () 15import Data.Torrent.InfoHashSpec ()
14import Network.BitTorrent.CoreSpec () 16import Network.BitTorrent.CoreSpec ()
15import Network.BitTorrent.Core 17import Network.BitTorrent.Core
18import Network.BitTorrent.Exchange.BlockSpec ()
16import Network.BitTorrent.Exchange.Message 19import Network.BitTorrent.Exchange.Message
17 20
18instance Arbitrary Extension where 21instance 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
34instance Arbitrary StatusUpdate where
35 arbitrary = frequency
36 [ (1, Choking <$> arbitrary)
37 , (1, Interested <$> arbitrary)
38 ]
39
40instance Arbitrary Available where
41 arbitrary = frequency
42 [ (1, Have <$> arbitrary)
43 , (1, Bitfield <$> arbitrary)
44 ]
45
46instance Arbitrary Transfer where
47 arbitrary = frequency
48 [ (1, Request <$> arbitrary)
49 , (1, Piece <$> arbitrary)
50 , (1, Cancel <$> arbitrary)
51 ]
52
53instance 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
62instance 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
31spec :: Spec 73spec :: Spec
32spec = do 74spec = 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'