diff options
author | Sam Truzjan <pxqr.sta@gmail.com> | 2013-12-15 19:44:12 +0400 |
---|---|---|
committer | Sam Truzjan <pxqr.sta@gmail.com> | 2013-12-15 19:44:12 +0400 |
commit | c1e3c9762eb5fea16188a0fb21ad01dd3240ab88 (patch) | |
tree | f65cffdb5156c1140dead382d4a29da845e70e53 /tests/Network | |
parent | aee6069785bd552100824e36995e55e72bdbb42e (diff) |
Fix bugs in PeerAddr encoding.
Also:
* PeerAddr.hs internals and export list have been simplified;
* tests added.
Diffstat (limited to 'tests/Network')
-rw-r--r-- | tests/Network/BitTorrent/Core/PeerIdSpec.hs | 7 | ||||
-rw-r--r-- | tests/Network/BitTorrent/Tracker/MessageSpec.hs | 36 |
2 files changed, 41 insertions, 2 deletions
diff --git a/tests/Network/BitTorrent/Core/PeerIdSpec.hs b/tests/Network/BitTorrent/Core/PeerIdSpec.hs index a4cc30b8..4b0c2398 100644 --- a/tests/Network/BitTorrent/Core/PeerIdSpec.hs +++ b/tests/Network/BitTorrent/Core/PeerIdSpec.hs | |||
@@ -1,6 +1,7 @@ | |||
1 | {-# OPTIONS -fno-warn-orphans #-} | 1 | {-# OPTIONS -fno-warn-orphans #-} |
2 | module Network.BitTorrent.Core.PeerIdSpec (spec) where | 2 | module Network.BitTorrent.Core.PeerIdSpec (spec) where |
3 | import Control.Applicative | 3 | import Control.Applicative |
4 | import Data.BEncode as BE | ||
4 | import Data.Text.Encoding as T | 5 | import Data.Text.Encoding as T |
5 | import Test.Hspec | 6 | import Test.Hspec |
6 | import Test.QuickCheck | 7 | import Test.QuickCheck |
@@ -17,4 +18,8 @@ instance Arbitrary PeerId where | |||
17 | ] | 18 | ] |
18 | 19 | ||
19 | spec :: Spec | 20 | spec :: Spec |
20 | spec = return () \ No newline at end of file | 21 | spec = do |
22 | describe "PeerId" $ do | ||
23 | it "properly bencoded" $ do | ||
24 | BE.decode "20:01234567890123456789" | ||
25 | `shouldBe` Right ("01234567890123456789" :: PeerId) \ No newline at end of file | ||
diff --git a/tests/Network/BitTorrent/Tracker/MessageSpec.hs b/tests/Network/BitTorrent/Tracker/MessageSpec.hs index ac770905..bf89e717 100644 --- a/tests/Network/BitTorrent/Tracker/MessageSpec.hs +++ b/tests/Network/BitTorrent/Tracker/MessageSpec.hs | |||
@@ -7,6 +7,8 @@ module Network.BitTorrent.Tracker.MessageSpec | |||
7 | ) where | 7 | ) where |
8 | 8 | ||
9 | import Control.Applicative | 9 | import Control.Applicative |
10 | import Control.Exception | ||
11 | import Data.BEncode as BE | ||
10 | import Data.List as L | 12 | import Data.List as L |
11 | import Data.Maybe | 13 | import Data.Maybe |
12 | import Data.Word | 14 | import Data.Word |
@@ -56,10 +58,42 @@ arbitrarySample = L.head <$> sample' arbitrary | |||
56 | 58 | ||
57 | spec :: Spec | 59 | spec :: Spec |
58 | spec = do | 60 | spec = do |
59 | describe "Announce" $ do | 61 | describe "AnnounceQuery" $ do |
60 | it "properly url encoded" $ property $ \ q -> | 62 | it "properly url encoded" $ property $ \ q -> |
61 | parseAnnounceQuery (renderAnnounceQuery q) | 63 | parseAnnounceQuery (renderAnnounceQuery q) |
62 | `shouldBe` Right q | 64 | `shouldBe` Right q |
63 | 65 | ||
66 | describe "AnnounceInfo" $ do | ||
67 | it "parses minimal sample" $ do | ||
68 | "d8:intervali0e5:peerslee" | ||
69 | `shouldBe` | ||
70 | AnnounceInfo Nothing Nothing 0 Nothing (PeerList []) Nothing | ||
71 | |||
72 | it "parses optional fields" $ do | ||
73 | "d8:completei1e\ | ||
74 | \10:incompletei2e\ | ||
75 | \8:intervali3e\ | ||
76 | \12:min intervali4e\ | ||
77 | \5:peersle\ | ||
78 | \15:warning message3:str\ | ||
79 | \e" | ||
80 | `shouldBe` | ||
81 | AnnounceInfo (Just 1) (Just 2) 3 (Just 4) (PeerList []) (Just "str") | ||
82 | |||
83 | it "parses failed response" $ do | ||
84 | "d14:failure reason10:any reasone" | ||
85 | `shouldBe` | ||
86 | Message.Failure "any reason" | ||
87 | |||
88 | it "fail if no peer list present" $ do | ||
89 | evaluate ("d8:intervali0ee" :: AnnounceInfo) | ||
90 | `shouldThrow` | ||
91 | errorCall "fromString: unable to decode AnnounceInfo: \ | ||
92 | \required field `peers' not found" | ||
93 | |||
94 | it "parses peer list" $ do -- TODO | ||
95 | "d8:intervali0e5:peerslee" `shouldBe` | ||
96 | AnnounceInfo Nothing Nothing 0 Nothing (PeerList []) Nothing | ||
97 | |||
64 | describe "Scrape" $ do | 98 | describe "Scrape" $ do |
65 | return () | 99 | return () |