diff options
author | Sam Truzjan <pxqr.sta@gmail.com> | 2013-12-08 07:45:03 +0400 |
---|---|---|
committer | Sam Truzjan <pxqr.sta@gmail.com> | 2013-12-08 07:45:03 +0400 |
commit | 61c25c415049af15e88a18644e1f2df794140320 (patch) | |
tree | 22d93ca0d8cc01c582ebb7ca3d5e7ddb1cf284a8 /tests/Network/BitTorrent/Exchange/MessageSpec.hs | |
parent | ab3f921e04cf817a66c155b05e593a20c884c085 (diff) |
Add basic tests for message module
Diffstat (limited to 'tests/Network/BitTorrent/Exchange/MessageSpec.hs')
-rw-r--r-- | tests/Network/BitTorrent/Exchange/MessageSpec.hs | 43 |
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 @@ | |||
1 | module Network.BitTorrent.Exchange.MessageSpec (spec) where | ||
2 | import Control.Applicative | ||
3 | import Data.ByteString as BS | ||
4 | import Data.Default | ||
5 | import Data.List as L | ||
6 | import Data.Set as S | ||
7 | import Data.Serialize as S | ||
8 | import Test.Hspec | ||
9 | import Test.QuickCheck | ||
10 | |||
11 | import Data.Torrent.InfoHashSpec () | ||
12 | import Network.BitTorrent.CoreSpec () | ||
13 | import Network.BitTorrent.Core | ||
14 | import Network.BitTorrent.Exchange.Message | ||
15 | |||
16 | instance Arbitrary Extension where | ||
17 | arbitrary = elements [minBound .. maxBound] | ||
18 | |||
19 | instance Arbitrary Caps where | ||
20 | arbitrary = toCaps <$> arbitrary | ||
21 | |||
22 | instance Arbitrary Handshake where | ||
23 | arbitrary = Handshake <$> arbitrary <*> arbitrary | ||
24 | <*> arbitrary <*> arbitrary | ||
25 | |||
26 | spec :: Spec | ||
27 | spec = 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 | ||