diff options
author | Sam Truzjan <pxqr.sta@gmail.com> | 2013-12-16 15:32:01 +0400 |
---|---|---|
committer | Sam Truzjan <pxqr.sta@gmail.com> | 2013-12-16 15:32:01 +0400 |
commit | f393a2ec1611d2e5587f6fc97317294377c72d5d (patch) | |
tree | 278edaafd24c59abf480e7be2bb5791c5b527813 /tests/Network | |
parent | 1bfd9cdb74a7ba70c54bf84949889253032f8869 (diff) |
Test peer list encoding
Diffstat (limited to 'tests/Network')
-rw-r--r-- | tests/Network/BitTorrent/Tracker/MessageSpec.hs | 56 |
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 #-} |
3 | module Network.BitTorrent.Tracker.MessageSpec | 4 | module Network.BitTorrent.Tracker.MessageSpec |
4 | ( spec | 5 | ( spec |
@@ -9,19 +10,19 @@ module Network.BitTorrent.Tracker.MessageSpec | |||
9 | import Control.Applicative | 10 | import Control.Applicative |
10 | import Control.Exception | 11 | import Control.Exception |
11 | import Data.BEncode as BE | 12 | import Data.BEncode as BE |
13 | import Data.ByteString.Lazy as BL | ||
12 | import Data.List as L | 14 | import Data.List as L |
13 | import Data.Maybe | 15 | import Data.Maybe |
14 | import Data.Word | ||
15 | import Network | ||
16 | import Test.Hspec | 16 | import Test.Hspec |
17 | import Test.QuickCheck | 17 | import Test.QuickCheck |
18 | 18 | ||
19 | import Data.Torrent.InfoHashSpec () | 19 | import Data.Torrent.InfoHashSpec () |
20 | import Data.Torrent.ProgressSpec () | 20 | import Data.Torrent.ProgressSpec () |
21 | import Network.BitTorrent.Core.PeerIdSpec () | 21 | import Network.BitTorrent.Core.PeerIdSpec () |
22 | import Network.BitTorrent.Core.PeerAddrSpec () | ||
22 | 23 | ||
23 | import Network.BitTorrent.Tracker.Message as Message | 24 | import Network.BitTorrent.Tracker.Message as Message |
24 | import Network.BitTorrent.Core.PeerAddr | 25 | import 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 | |||
33 | instance Arbitrary Event where | 34 | instance Arbitrary Event where |
34 | arbitrary = elements [minBound..maxBound] | 35 | arbitrary = elements [minBound..maxBound] |
35 | 36 | ||
36 | instance Arbitrary PortNumber where | ||
37 | arbitrary = fromIntegral <$> (arbitrary :: Gen Word16) | ||
38 | |||
39 | instance Arbitrary AnnounceQuery where | 37 | instance 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 | |||
56 | arbitrarySample :: Arbitrary a => IO a | 54 | arbitrarySample :: Arbitrary a => IO a |
57 | arbitrarySample = L.head <$> sample' arbitrary | 55 | arbitrarySample = L.head <$> sample' arbitrary |
58 | 56 | ||
57 | zeroPeerId :: PeerAddr a -> PeerAddr a | ||
58 | zeroPeerId addr = addr { peerId = Nothing } | ||
59 | |||
59 | spec :: Spec | 60 | spec :: Spec |
60 | spec = do | 61 | spec = 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 () |