diff options
author | Sam Truzjan <pxqr.sta@gmail.com> | 2013-12-16 20:19:07 +0400 |
---|---|---|
committer | Sam Truzjan <pxqr.sta@gmail.com> | 2013-12-16 20:19:07 +0400 |
commit | 0cf1c142d0e18eef05e1190d0fdaa94d2fa4df59 (patch) | |
tree | b7103d0a55c665bd738eb23ccc3784f3e8d13c18 /tests/Network | |
parent | f393a2ec1611d2e5587f6fc97317294377c72d5d (diff) |
Add spec for AnnounceInfo encoding
Diffstat (limited to 'tests/Network')
-rw-r--r-- | tests/Network/BitTorrent/Tracker/MessageSpec.hs | 62 | ||||
-rw-r--r-- | tests/Network/BitTorrent/Tracker/RPC/HTTPSpec.hs | 1 |
2 files changed, 55 insertions, 8 deletions
diff --git a/tests/Network/BitTorrent/Tracker/MessageSpec.hs b/tests/Network/BitTorrent/Tracker/MessageSpec.hs index c3de7b30..5949de7a 100644 --- a/tests/Network/BitTorrent/Tracker/MessageSpec.hs +++ b/tests/Network/BitTorrent/Tracker/MessageSpec.hs | |||
@@ -1,6 +1,7 @@ | |||
1 | {-# LANGUAGE RecordWildCards #-} | 1 | {-# LANGUAGE RecordWildCards #-} |
2 | {-# LANGUAGE ViewPatterns #-} | 2 | {-# LANGUAGE ViewPatterns #-} |
3 | {-# OPTIONS -fno-warn-orphans #-} | 3 | {-# LANGUAGE FlexibleInstances #-} |
4 | {-# OPTIONS -fno-warn-orphans #-} | ||
4 | module Network.BitTorrent.Tracker.MessageSpec | 5 | module Network.BitTorrent.Tracker.MessageSpec |
5 | ( spec | 6 | ( spec |
6 | , validateInfo | 7 | , validateInfo |
@@ -39,6 +40,20 @@ instance Arbitrary AnnounceQuery where | |||
39 | <$> arbitrary <*> arbitrary <*> arbitrary <*> arbitrary | 40 | <$> arbitrary <*> arbitrary <*> arbitrary <*> arbitrary |
40 | <*> arbitrary <*> arbitrary <*> arbitrary | 41 | <*> arbitrary <*> arbitrary <*> arbitrary |
41 | 42 | ||
43 | instance Arbitrary (PeerList IP) where | ||
44 | arbitrary = frequency | ||
45 | [ (1, (PeerList . maybeToList) <$> arbitrary) | ||
46 | , (1, (CompactPeerList . maybeToList . fmap zeroPeerId) <$> arbitrary) | ||
47 | ] | ||
48 | |||
49 | shrink ( PeerList xs) = PeerList <$> shrink xs | ||
50 | shrink (CompactPeerList xs) = CompactPeerList <$> shrink xs | ||
51 | |||
52 | instance Arbitrary AnnounceInfo where | ||
53 | arbitrary = AnnounceInfo | ||
54 | <$> arbitrary <*> arbitrary <*> arbitrary <*> arbitrary | ||
55 | <*> arbitrary <*> arbitrary | ||
56 | |||
42 | validateInfo :: AnnounceQuery -> AnnounceInfo -> Expectation | 57 | validateInfo :: AnnounceQuery -> AnnounceInfo -> Expectation |
43 | validateInfo _ Message.Failure {..} = error "validateInfo: failure" | 58 | validateInfo _ Message.Failure {..} = error "validateInfo: failure" |
44 | validateInfo AnnounceQuery {..} AnnounceInfo {..} = do | 59 | validateInfo AnnounceQuery {..} AnnounceInfo {..} = do |
@@ -47,7 +62,8 @@ validateInfo AnnounceQuery {..} AnnounceInfo {..} = do | |||
47 | respMinInterval `shouldSatisfy` isNothing | 62 | respMinInterval `shouldSatisfy` isNothing |
48 | respWarning `shouldSatisfy` isNothing | 63 | respWarning `shouldSatisfy` isNothing |
49 | peerList `shouldSatisfy` L.all (isNothing . peerId) | 64 | peerList `shouldSatisfy` L.all (isNothing . peerId) |
50 | fromJust respComplete + fromJust respIncomplete `shouldBe` L.length peerList | 65 | fromJust respComplete + fromJust respIncomplete |
66 | `shouldBe` L.length peerList | ||
51 | where | 67 | where |
52 | peerList = getPeerList respPeers | 68 | peerList = getPeerList respPeers |
53 | 69 | ||
@@ -125,15 +141,47 @@ spec = do | |||
125 | errorCall "fromString: unable to decode AnnounceInfo: \ | 141 | errorCall "fromString: unable to decode AnnounceInfo: \ |
126 | \required field `peers' not found" | 142 | \required field `peers' not found" |
127 | 143 | ||
128 | it "parses peer list" $ do -- TODO | 144 | it "parses `peer' list" $ do -- TODO |
129 | "d8:intervali0e\ | 145 | "d8:intervali0e\ |
130 | \5:peersl\ | 146 | \5:peersl\ |
131 | \d2:ip7:1.2.3.4\ | 147 | \d2:ip7:1.2.3.4\ |
132 | \4:porti80e\ | 148 | \4:porti80e\ |
149 | \e\ | ||
150 | \d2:ip3:::1\ | ||
151 | \4:porti80e\ | ||
133 | \e\ | 152 | \e\ |
134 | \e\ | 153 | \e\ |
135 | \e" `shouldBe` | 154 | \e" `shouldBe` |
136 | AnnounceInfo Nothing Nothing 0 Nothing (PeerList ["1.2.3.4:80"]) Nothing | 155 | let xs = PeerList ["1.2.3.4:80", "[::1]:80"] in |
156 | AnnounceInfo Nothing Nothing 0 Nothing xs Nothing | ||
157 | |||
158 | it "parses `peers6' list" $ do | ||
159 | "d8:intervali0e\ | ||
160 | \5:peers0:\ | ||
161 | \6:peers60:\ | ||
162 | \e" `shouldBe` | ||
163 | AnnounceInfo Nothing Nothing 0 Nothing (CompactPeerList []) Nothing | ||
164 | |||
165 | it "fails on invalid combinations of the peer lists" $ do | ||
166 | BE.decode "d8:intervali0e\ | ||
167 | \5:peers0:\ | ||
168 | \6:peers6le\ | ||
169 | \e" | ||
170 | `shouldBe` (Left | ||
171 | "PeerList: the `peers6' field value should contain \ | ||
172 | \*compact* peer list" :: BE.Result AnnounceInfo) | ||
173 | |||
174 | BE.decode "d8:intervali0e\ | ||
175 | \5:peersle\ | ||
176 | \6:peers60:\ | ||
177 | \e" | ||
178 | `shouldBe` (Left | ||
179 | "PeerList: non-compact peer list provided, \ | ||
180 | \but the `peers6' field present" :: BE.Result AnnounceInfo) | ||
181 | |||
182 | it "properly bencoded (iso)" $ property $ \ info -> | ||
183 | BE.decode (BL.toStrict (BE.encode info)) | ||
184 | `shouldBe` Right (info :: AnnounceInfo) | ||
137 | 185 | ||
138 | describe "Scrape" $ do | 186 | describe "Scrape" $ do |
139 | return () | 187 | return () |
diff --git a/tests/Network/BitTorrent/Tracker/RPC/HTTPSpec.hs b/tests/Network/BitTorrent/Tracker/RPC/HTTPSpec.hs index 37029b75..eb549516 100644 --- a/tests/Network/BitTorrent/Tracker/RPC/HTTPSpec.hs +++ b/tests/Network/BitTorrent/Tracker/RPC/HTTPSpec.hs | |||
@@ -1,7 +1,6 @@ | |||
1 | module Network.BitTorrent.Tracker.RPC.HTTPSpec (spec, trackerURIs) where | 1 | module Network.BitTorrent.Tracker.RPC.HTTPSpec (spec, trackerURIs) where |
2 | 2 | ||
3 | import Control.Monad | 3 | import Control.Monad |
4 | import Control.Monad.Trans | ||
5 | import Control.Monad.Trans.Resource | 4 | import Control.Monad.Trans.Resource |
6 | import Data.Default | 5 | import Data.Default |
7 | import Data.List as L | 6 | import Data.List as L |