From c1e3c9762eb5fea16188a0fb21ad01dd3240ab88 Mon Sep 17 00:00:00 2001 From: Sam Truzjan Date: Sun, 15 Dec 2013 19:44:12 +0400 Subject: Fix bugs in PeerAddr encoding. Also: * PeerAddr.hs internals and export list have been simplified; * tests added. --- tests/Network/BitTorrent/Core/PeerIdSpec.hs | 7 ++++- tests/Network/BitTorrent/Tracker/MessageSpec.hs | 36 ++++++++++++++++++++++++- 2 files changed, 41 insertions(+), 2 deletions(-) (limited to 'tests/Network') 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 @@ {-# OPTIONS -fno-warn-orphans #-} module Network.BitTorrent.Core.PeerIdSpec (spec) where import Control.Applicative +import Data.BEncode as BE import Data.Text.Encoding as T import Test.Hspec import Test.QuickCheck @@ -17,4 +18,8 @@ instance Arbitrary PeerId where ] spec :: Spec -spec = return () \ No newline at end of file +spec = do + describe "PeerId" $ do + it "properly bencoded" $ do + BE.decode "20:01234567890123456789" + `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 ) where import Control.Applicative +import Control.Exception +import Data.BEncode as BE import Data.List as L import Data.Maybe import Data.Word @@ -56,10 +58,42 @@ arbitrarySample = L.head <$> sample' arbitrary spec :: Spec spec = do - describe "Announce" $ do + describe "AnnounceQuery" $ do it "properly url encoded" $ property $ \ q -> parseAnnounceQuery (renderAnnounceQuery q) `shouldBe` Right q + describe "AnnounceInfo" $ do + it "parses minimal sample" $ do + "d8:intervali0e5:peerslee" + `shouldBe` + AnnounceInfo Nothing Nothing 0 Nothing (PeerList []) Nothing + + it "parses optional fields" $ do + "d8:completei1e\ + \10:incompletei2e\ + \8:intervali3e\ + \12:min intervali4e\ + \5:peersle\ + \15:warning message3:str\ + \e" + `shouldBe` + AnnounceInfo (Just 1) (Just 2) 3 (Just 4) (PeerList []) (Just "str") + + it "parses failed response" $ do + "d14:failure reason10:any reasone" + `shouldBe` + Message.Failure "any reason" + + it "fail if no peer list present" $ do + evaluate ("d8:intervali0ee" :: AnnounceInfo) + `shouldThrow` + errorCall "fromString: unable to decode AnnounceInfo: \ + \required field `peers' not found" + + it "parses peer list" $ do -- TODO + "d8:intervali0e5:peerslee" `shouldBe` + AnnounceInfo Nothing Nothing 0 Nothing (PeerList []) Nothing + describe "Scrape" $ do return () -- cgit v1.2.3