diff options
author | James Crayne <jim.crayne@gmail.com> | 2019-09-28 13:43:29 -0400 |
---|---|---|
committer | Joe Crayne <joe@jerkface.net> | 2020-01-01 19:27:53 -0500 |
commit | 11987749fc6e6d3e53ea737d46d5ab13a16faeb8 (patch) | |
tree | 5716463275c2d3e902889db619908ded2a73971c /dht/bittorrent/tests/Network/BitTorrent/Exchange/MessageSpec.hs | |
parent | add2c76bced51fde5e9917e7449ef52be70faf87 (diff) |
Factor out some new libraries
word64-map:
Data.Word64Map
network-addr:
Network.Address
tox-crypto:
Crypto.Tox
lifted-concurrent:
Control.Concurrent.Lifted.Instrument
Control.Concurrent.Async.Lifted.Instrument
psq-wrap:
Data.Wrapper.PSQInt
Data.Wrapper.PSQ
minmax-psq:
Data.MinMaxPSQ
tasks:
Control.Concurrent.Tasks
kad:
Network.Kademlia
Network.Kademlia.Bootstrap
Network.Kademlia.Routing
Network.Kademlia.CommonAPI
Network.Kademlia.Persistence
Network.Kademlia.Search
Diffstat (limited to 'dht/bittorrent/tests/Network/BitTorrent/Exchange/MessageSpec.hs')
-rw-r--r-- | dht/bittorrent/tests/Network/BitTorrent/Exchange/MessageSpec.hs | 102 |
1 files changed, 102 insertions, 0 deletions
diff --git a/dht/bittorrent/tests/Network/BitTorrent/Exchange/MessageSpec.hs b/dht/bittorrent/tests/Network/BitTorrent/Exchange/MessageSpec.hs new file mode 100644 index 00000000..d615b1ff --- /dev/null +++ b/dht/bittorrent/tests/Network/BitTorrent/Exchange/MessageSpec.hs | |||
@@ -0,0 +1,102 @@ | |||
1 | {-# OPTIONS_GHC -fno-warn-orphans #-} | ||
2 | module Network.BitTorrent.Exchange.MessageSpec (spec) where | ||
3 | import Control.Applicative | ||
4 | import Control.Exception | ||
5 | import Data.ByteString as BS | ||
6 | import Data.List as L | ||
7 | import Data.Set as S | ||
8 | import Data.Serialize as S | ||
9 | import Data.String | ||
10 | import Test.Hspec | ||
11 | import Test.QuickCheck | ||
12 | |||
13 | import Data.TorrentSpec () | ||
14 | import Network.BitTorrent.Exchange.BitfieldSpec () | ||
15 | import Network.BitTorrent.CoreSpec () | ||
16 | import Network.BitTorrent.Address () | ||
17 | import Network.BitTorrent.Exchange.BlockSpec () | ||
18 | import Network.BitTorrent.Exchange.Message | ||
19 | |||
20 | instance Arbitrary Extension where | ||
21 | arbitrary = elements [minBound .. maxBound] | ||
22 | |||
23 | instance Arbitrary Caps where | ||
24 | arbitrary = toCaps <$> arbitrary | ||
25 | |||
26 | instance Arbitrary ExtendedExtension where | ||
27 | arbitrary = elements [minBound .. maxBound] | ||
28 | |||
29 | instance Arbitrary ExtendedCaps where | ||
30 | arbitrary = toCaps <$> arbitrary | ||
31 | |||
32 | instance Arbitrary ProtocolName where | ||
33 | arbitrary = fromString <$> (arbitrary `suchThat` ((200 <) . L.length)) | ||
34 | |||
35 | instance Arbitrary Handshake where | ||
36 | arbitrary = Handshake <$> arbitrary <*> arbitrary | ||
37 | <*> arbitrary <*> arbitrary | ||
38 | |||
39 | instance Arbitrary StatusUpdate where | ||
40 | arbitrary = frequency | ||
41 | [ (1, Choking <$> arbitrary) | ||
42 | , (1, Interested <$> arbitrary) | ||
43 | ] | ||
44 | |||
45 | instance Arbitrary Available where | ||
46 | arbitrary = frequency | ||
47 | [ (1, Have <$> arbitrary) | ||
48 | , (1, Bitfield <$> arbitrary) | ||
49 | ] | ||
50 | |||
51 | instance Arbitrary Transfer where | ||
52 | arbitrary = frequency | ||
53 | [ (1, Request <$> arbitrary) | ||
54 | , (1, Piece <$> arbitrary) | ||
55 | , (1, Cancel <$> arbitrary) | ||
56 | ] | ||
57 | |||
58 | instance Arbitrary FastMessage where | ||
59 | arbitrary = frequency | ||
60 | [ (1, pure HaveAll) | ||
61 | , (1, pure HaveNone) | ||
62 | , (1, SuggestPiece <$> arbitrary) | ||
63 | , (1, RejectRequest <$> arbitrary) | ||
64 | , (1, AllowedFast <$> arbitrary) | ||
65 | ] | ||
66 | |||
67 | instance Arbitrary Message where | ||
68 | arbitrary = frequency | ||
69 | [ (1, pure KeepAlive) | ||
70 | , (1, Status <$> arbitrary) | ||
71 | , (1, Available <$> arbitrary) | ||
72 | , (1, Transfer <$> arbitrary) | ||
73 | , (1, Fast <$> arbitrary) | ||
74 | ] | ||
75 | |||
76 | -- TODO test extension protocol | ||
77 | |||
78 | spec :: Spec | ||
79 | spec = do | ||
80 | describe "Caps" $ do | ||
81 | it "set-like container" $ property $ \ exts -> | ||
82 | L.all (`allowed` (toCaps exts :: Caps)) exts | ||
83 | |||
84 | it "preserve items" $ property $ \ extSet -> | ||
85 | S.fromList (fromCaps (toCaps (S.toList extSet) :: Caps)) | ||
86 | `shouldBe` extSet | ||
87 | |||
88 | describe "ByteStats" $ do | ||
89 | it "preserve size" $ property $ \ msg -> | ||
90 | byteLength (stats msg) `shouldBe` | ||
91 | fromIntegral (BS.length (S.encode (msg :: Message))) | ||
92 | |||
93 | describe "ProtocolName" $ do | ||
94 | it "fail to construct invalid string" $ do | ||
95 | let str = L.replicate 500 'x' | ||
96 | evaluate (fromString str :: ProtocolName) | ||
97 | `shouldThrow` | ||
98 | errorCall ("fromString: ProtocolName too long: " ++ str) | ||
99 | |||
100 | describe "Handshake" $ do | ||
101 | it "properly serialized" $ property $ \ hs -> | ||
102 | S.decode (S.encode hs ) `shouldBe` Right (hs :: Handshake) | ||