summaryrefslogtreecommitdiff
path: root/tests/Network/BitTorrent/Exchange/MessageSpec.hs
diff options
context:
space:
mode:
authorSam Truzjan <pxqr.sta@gmail.com>2013-12-08 07:45:03 +0400
committerSam Truzjan <pxqr.sta@gmail.com>2013-12-08 07:45:03 +0400
commit61c25c415049af15e88a18644e1f2df794140320 (patch)
tree22d93ca0d8cc01c582ebb7ca3d5e7ddb1cf284a8 /tests/Network/BitTorrent/Exchange/MessageSpec.hs
parentab3f921e04cf817a66c155b05e593a20c884c085 (diff)
Add basic tests for message module
Diffstat (limited to 'tests/Network/BitTorrent/Exchange/MessageSpec.hs')
-rw-r--r--tests/Network/BitTorrent/Exchange/MessageSpec.hs43
1 files changed, 43 insertions, 0 deletions
diff --git a/tests/Network/BitTorrent/Exchange/MessageSpec.hs b/tests/Network/BitTorrent/Exchange/MessageSpec.hs
new file mode 100644
index 00000000..8d1041dd
--- /dev/null
+++ b/tests/Network/BitTorrent/Exchange/MessageSpec.hs
@@ -0,0 +1,43 @@
1module Network.BitTorrent.Exchange.MessageSpec (spec) where
2import Control.Applicative
3import Data.ByteString as BS
4import Data.Default
5import Data.List as L
6import Data.Set as S
7import Data.Serialize as S
8import Test.Hspec
9import Test.QuickCheck
10
11import Data.Torrent.InfoHashSpec ()
12import Network.BitTorrent.CoreSpec ()
13import Network.BitTorrent.Core
14import Network.BitTorrent.Exchange.Message
15
16instance Arbitrary Extension where
17 arbitrary = elements [minBound .. maxBound]
18
19instance Arbitrary Caps where
20 arbitrary = toCaps <$> arbitrary
21
22instance Arbitrary Handshake where
23 arbitrary = Handshake <$> arbitrary <*> arbitrary
24 <*> arbitrary <*> arbitrary
25
26spec :: Spec
27spec = do
28 describe "Caps" $ do
29 it "set-like container" $ property $ \ exts ->
30 L.all (`allowed` (toCaps exts :: Caps)) exts
31
32 it "preserve items" $ property $ \ extSet ->
33 S.fromList (fromCaps (toCaps (S.toList extSet) :: Caps))
34 `shouldBe` extSet
35
36 describe "Handshake" $ do
37 it "properly serialized" $ property $ \ hs ->
38 S.decode (S.encode hs ) `shouldBe` Right (hs :: Handshake)
39
40 it "fail if protocol string is too long" $ do
41 pid <- genPeerId
42 let hs = (defaultHandshake def pid) {hsProtocol = BS.replicate 256 0}
43 S.decode (S.encode hs) `shouldBe` Right hs \ No newline at end of file