summaryrefslogtreecommitdiff
path: root/tests/Network
diff options
context:
space:
mode:
authorSam Truzjan <pxqr.sta@gmail.com>2013-12-16 15:32:01 +0400
committerSam Truzjan <pxqr.sta@gmail.com>2013-12-16 15:32:01 +0400
commitf393a2ec1611d2e5587f6fc97317294377c72d5d (patch)
tree278edaafd24c59abf480e7be2bb5791c5b527813 /tests/Network
parent1bfd9cdb74a7ba70c54bf84949889253032f8869 (diff)
Test peer list encoding
Diffstat (limited to 'tests/Network')
-rw-r--r--tests/Network/BitTorrent/Tracker/MessageSpec.hs56
1 files changed, 48 insertions, 8 deletions
diff --git a/tests/Network/BitTorrent/Tracker/MessageSpec.hs b/tests/Network/BitTorrent/Tracker/MessageSpec.hs
index bf89e717..c3de7b30 100644
--- a/tests/Network/BitTorrent/Tracker/MessageSpec.hs
+++ b/tests/Network/BitTorrent/Tracker/MessageSpec.hs
@@ -1,4 +1,5 @@
1{-# LANGUAGE RecordWildCards #-} 1{-# LANGUAGE RecordWildCards #-}
2{-# LANGUAGE ViewPatterns #-}
2{-# OPTIONS -fno-warn-orphans #-} 3{-# OPTIONS -fno-warn-orphans #-}
3module Network.BitTorrent.Tracker.MessageSpec 4module Network.BitTorrent.Tracker.MessageSpec
4 ( spec 5 ( spec
@@ -9,19 +10,19 @@ module Network.BitTorrent.Tracker.MessageSpec
9import Control.Applicative 10import Control.Applicative
10import Control.Exception 11import Control.Exception
11import Data.BEncode as BE 12import Data.BEncode as BE
13import Data.ByteString.Lazy as BL
12import Data.List as L 14import Data.List as L
13import Data.Maybe 15import Data.Maybe
14import Data.Word
15import Network
16import Test.Hspec 16import Test.Hspec
17import Test.QuickCheck 17import Test.QuickCheck
18 18
19import Data.Torrent.InfoHashSpec () 19import Data.Torrent.InfoHashSpec ()
20import Data.Torrent.ProgressSpec () 20import Data.Torrent.ProgressSpec ()
21import Network.BitTorrent.Core.PeerIdSpec () 21import Network.BitTorrent.Core.PeerIdSpec ()
22import Network.BitTorrent.Core.PeerAddrSpec ()
22 23
23import Network.BitTorrent.Tracker.Message as Message 24import Network.BitTorrent.Tracker.Message as Message
24import Network.BitTorrent.Core.PeerAddr 25import Network.BitTorrent.Core
25 26
26 27
27--prop_bencode :: Eq a => BEncode a => a -> Bool 28--prop_bencode :: Eq a => BEncode a => a -> Bool
@@ -33,9 +34,6 @@ import Network.BitTorrent.Core.PeerAddr
33instance Arbitrary Event where 34instance Arbitrary Event where
34 arbitrary = elements [minBound..maxBound] 35 arbitrary = elements [minBound..maxBound]
35 36
36instance Arbitrary PortNumber where
37 arbitrary = fromIntegral <$> (arbitrary :: Gen Word16)
38
39instance Arbitrary AnnounceQuery where 37instance Arbitrary AnnounceQuery where
40 arbitrary = AnnounceQuery 38 arbitrary = AnnounceQuery
41 <$> arbitrary <*> arbitrary <*> arbitrary <*> arbitrary 39 <$> arbitrary <*> arbitrary <*> arbitrary <*> arbitrary
@@ -56,6 +54,9 @@ validateInfo AnnounceQuery {..} AnnounceInfo {..} = do
56arbitrarySample :: Arbitrary a => IO a 54arbitrarySample :: Arbitrary a => IO a
57arbitrarySample = L.head <$> sample' arbitrary 55arbitrarySample = L.head <$> sample' arbitrary
58 56
57zeroPeerId :: PeerAddr a -> PeerAddr a
58zeroPeerId addr = addr { peerId = Nothing }
59
59spec :: Spec 60spec :: Spec
60spec = do 61spec = do
61 describe "AnnounceQuery" $ do 62 describe "AnnounceQuery" $ do
@@ -63,6 +64,39 @@ spec = do
63 parseAnnounceQuery (renderAnnounceQuery q) 64 parseAnnounceQuery (renderAnnounceQuery q)
64 `shouldBe` Right q 65 `shouldBe` Right q
65 66
67 describe "PeerList" $ do
68 context "Non compact" $ do
69 it "properly encoded (both ipv4 and ipv6)" $ do
70 BE.decode "ld2:ip7:1.2.3.44:porti80eed2:ip3:::14:porti8080eee"
71 `shouldBe` Right
72 (PeerList ["1.2.3.4:80", "[::1]:8080"] :: PeerList IPv4)
73
74 it "properly encoded (iso)" $ property $ \ xs ->
75 BE.decode (BL.toStrict (BE.encode (PeerList xs :: PeerList IPv4)))
76 `shouldBe` Right (PeerList xs :: PeerList IPv4)
77
78 context "Compact" $ do
79 it "properly encodes (ipv4)" $ do
80 BE.decode "12:\x1\x2\x3\x4\x1\x2\x9\x8\x7\x6\x1\x2"
81 `shouldBe` Right
82 (CompactPeerList ["1.2.3.4:258", "9.8.7.6:258"] :: PeerList IPv4)
83
84 it "properly encodes (ipv6)" $ do
85 BE.decode "18:\x1\x2\x3\x4\x5\x6\x7\x8\x1\x2\x3\x4\x5\x6\x7\x8\x1\x2"
86 `shouldBe` Right
87 (CompactPeerList ["[102:304:506:708:102:304:506:708]:258"]
88 :: PeerList IPv6)
89
90 it "properly encoded (ipv4, iso)" $
91 property $ \ (fmap zeroPeerId -> xs) ->
92 BE.decode (BL.toStrict (BE.encode (CompactPeerList xs)))
93 `shouldBe` Right (CompactPeerList xs :: PeerList IPv4)
94
95 it "properly encoded (ipv6, iso)" $
96 property $ \ (fmap zeroPeerId -> xs) ->
97 BE.decode (BL.toStrict (BE.encode (CompactPeerList xs)))
98 `shouldBe` Right (CompactPeerList xs :: PeerList IPv6)
99
66 describe "AnnounceInfo" $ do 100 describe "AnnounceInfo" $ do
67 it "parses minimal sample" $ do 101 it "parses minimal sample" $ do
68 "d8:intervali0e5:peerslee" 102 "d8:intervali0e5:peerslee"
@@ -92,8 +126,14 @@ spec = do
92 \required field `peers' not found" 126 \required field `peers' not found"
93 127
94 it "parses peer list" $ do -- TODO 128 it "parses peer list" $ do -- TODO
95 "d8:intervali0e5:peerslee" `shouldBe` 129 "d8:intervali0e\
96 AnnounceInfo Nothing Nothing 0 Nothing (PeerList []) Nothing 130 \5:peersl\
131 \d2:ip7:1.2.3.4\
132 \4:porti80e\
133 \e\
134 \e\
135 \e" `shouldBe`
136 AnnounceInfo Nothing Nothing 0 Nothing (PeerList ["1.2.3.4:80"]) Nothing
97 137
98 describe "Scrape" $ do 138 describe "Scrape" $ do
99 return () 139 return ()