summaryrefslogtreecommitdiff
path: root/tests/Network/BitTorrent
diff options
context:
space:
mode:
authorjoe <joe@jerkface.net>2017-01-25 11:05:24 -0500
committerjoe <joe@jerkface.net>2017-01-25 11:05:24 -0500
commit450c65257185e856ee605857688a378f2473d27c (patch)
tree1a3cc391908634e875e7e98ba7a02f52c689a5f6 /tests/Network/BitTorrent
parent5d5488b5b1690e5ffb3e268bab9893aac3e32f89 (diff)
Arbitrary instances for DHT queries and responses.
Diffstat (limited to 'tests/Network/BitTorrent')
-rw-r--r--tests/Network/BitTorrent/DHT/MessageSpec.hs20
1 files changed, 15 insertions, 5 deletions
diff --git a/tests/Network/BitTorrent/DHT/MessageSpec.hs b/tests/Network/BitTorrent/DHT/MessageSpec.hs
index 3b1f5acd..6f3c7489 100644
--- a/tests/Network/BitTorrent/DHT/MessageSpec.hs
+++ b/tests/Network/BitTorrent/DHT/MessageSpec.hs
@@ -21,6 +21,16 @@ import Data.TorrentSpec ()
21import Network.BitTorrent.CoreSpec () 21import Network.BitTorrent.CoreSpec ()
22import Network.BitTorrent.DHT.TokenSpec () 22import Network.BitTorrent.DHT.TokenSpec ()
23 23
24-- Arbitrary queries and responses.
25instance Arbitrary Ping where arbitrary = pure Ping
26instance Arbitrary FindNode where arbitrary = FindNode <$> arbitrary
27instance Arbitrary ip => Arbitrary (NodeFound ip) where arbitrary = NodeFound <$> arbitrary
28instance Arbitrary GetPeers where arbitrary = GetPeers <$> arbitrary
29instance Arbitrary ip => Arbitrary (GotPeers ip) where arbitrary = GotPeers <$> arbitrary <*> arbitrary
30instance Arbitrary Announce where arbitrary = Announce <$> arbitrary <*> arbitrary <*> arbitrary <*> arbitrary <*> arbitrary
31instance Arbitrary Announced where arbitrary = pure Announced
32instance Arbitrary x => Arbitrary (Query x) where arbitrary = Query <$> arbitrary <*> arbitrary <*> arbitrary
33instance Arbitrary x => Arbitrary (Response x) where arbitrary = Response <$> arbitrary <*> arbitrary
24 34
25instance MonadLogger IO where 35instance MonadLogger IO where
26 monadLoggerLog _ _ _ _ = return () 36 monadLoggerLog _ _ _ _ = return ()
@@ -168,7 +178,7 @@ spec = do
168 \5:token8:aoeusnth\ 178 \5:token8:aoeusnth\
169 \e" `shouldBe` Right 179 \e" `shouldBe` Right
170 (Query "abcdefghij0123456789" False 180 (Query "abcdefghij0123456789" False
171 (Announce False "mnopqrstuvwxyz123456" 6881 "aoeusnth")) 181 (Announce False "mnopqrstuvwxyz123456" Nothing 6881 "aoeusnth"))
172 182
173 BE.decode "d2:id20:abcdefghij0123456789\ 183 BE.decode "d2:id20:abcdefghij0123456789\
174 \12:implied_porti1e\ 184 \12:implied_porti1e\
@@ -177,7 +187,7 @@ spec = do
177 \5:token8:aoeusnth\ 187 \5:token8:aoeusnth\
178 \e" `shouldBe` Right 188 \e" `shouldBe` Right
179 (Query "abcdefghij0123456789" False 189 (Query "abcdefghij0123456789" False
180 (Announce True "mnopqrstuvwxyz123456" 6881 "aoeusnth")) 190 (Announce True "mnopqrstuvwxyz123456" Nothing 6881 "aoeusnth"))
181 191
182 192
183 BE.decode "d2:id20:mnopqrstuvwxyz123456e" 193 BE.decode "d2:id20:mnopqrstuvwxyz123456e"
@@ -185,7 +195,7 @@ spec = do
185 (Response "mnopqrstuvwxyz123456" Announced) 195 (Response "mnopqrstuvwxyz123456" Announced)
186 196
187 it "properly bencoded (iso)" $ property $ \ nid flag topic port token -> do 197 it "properly bencoded (iso)" $ property $ \ nid flag topic port token -> do
188 prop_bencode (Query nid False (Announce flag topic port token)) 198 prop_bencode (Query nid False (Announce flag topic Nothing port token))
189 prop_bencode (Response nid (Announced)) 199 prop_bencode (Response nid (Announced))
190 200
191 201
@@ -194,7 +204,7 @@ spec = do
194 Response _remoteId Announced <- rpc $ do 204 Response _remoteId Announced <- rpc $ do
195 Response _ GotPeers {..} <- query remoteAddr (Query nid False (GetPeers def)) 205 Response _ GotPeers {..} <- query remoteAddr (Query nid False (GetPeers def))
196 let _ = peers :: Either [NodeInfo IPv4] [PeerAddr IPv4] 206 let _ = peers :: Either [NodeInfo IPv4] [PeerAddr IPv4]
197 query remoteAddr (Query nid False (Announce False def thisPort grantedToken)) 207 query remoteAddr (Query nid False (Announce False def Nothing thisPort grantedToken))
198 return () 208 return ()
199 209
200 it "does fail on invalid token" $ do 210 it "does fail on invalid token" $ do
@@ -206,6 +216,6 @@ spec = do
206 let q :: MonadKRPC h m => SockAddr -> Query Announce 216 let q :: MonadKRPC h m => SockAddr -> Query Announce
207 -> m (Response Announced) 217 -> m (Response Announced)
208 q = query 218 q = query
209 q remoteAddr (Query nid False (Announce False def thisPort invalidToken))) 219 q remoteAddr (Query nid False (Announce False def Nothing thisPort invalidToken)))
210 `shouldThrow` isQueryError 220 `shouldThrow` isQueryError
211 return () 221 return ()