diff options
-rw-r--r-- | bittorrent.cabal | 2 | ||||
-rw-r--r-- | tests/Network/BitTorrent/Exchange/MessageSpec.hs | 43 |
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 @@ | |||
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 | ||