summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--bittorrent.cabal2
-rw-r--r--tests/Network/BitTorrent/Exchange/MessageSpec.hs43
2 files changed, 45 insertions, 0 deletions
diff --git a/bittorrent.cabal b/bittorrent.cabal
index a457a9a0..3ad1d7e4 100644
--- a/bittorrent.cabal
+++ b/bittorrent.cabal
@@ -163,6 +163,7 @@ test-suite spec
163 Network.BitTorrent.Tracker.MessageSpec 163 Network.BitTorrent.Tracker.MessageSpec
164 Network.BitTorrent.Tracker.RPC.HTTPSpec 164 Network.BitTorrent.Tracker.RPC.HTTPSpec
165 Network.BitTorrent.Tracker.RPC.UDPSpec 165 Network.BitTorrent.Tracker.RPC.UDPSpec
166 Network.BitTorrent.Exchange.MessageSpec
166 System.Torrent.FileMapSpec 167 System.Torrent.FileMapSpec
167 build-depends: base == 4.* 168 build-depends: base == 4.*
168 , bytestring 169 , bytestring
@@ -172,6 +173,7 @@ test-suite spec
172 , convertible 173 , convertible
173 , data-default 174 , data-default
174 , monad-loops 175 , monad-loops
176 , containers
175 177
176 , aeson 178 , aeson
177 , cereal 179 , cereal
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