summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSam Truzjan <pxqr.sta@gmail.com>2013-11-30 16:40:34 +0400
committerSam Truzjan <pxqr.sta@gmail.com>2013-11-30 16:40:34 +0400
commit58ea9495514fa90cdd0c53a5628372d370a6bd0c (patch)
treef1f78c1ec2263af198aefef60e28d4747d610b81
parentbaff7fbe8a491ce743b3fe2eef0e00ee37ee5c98 (diff)
Add tests for http tracker protocol
-rw-r--r--bittorrent.cabal3
-rw-r--r--src/Network/BitTorrent/Tracker/RPC/HTTP.hs11
-rw-r--r--src/Network/BitTorrent/Tracker/RPC/Message.hs3
-rw-r--r--tests/Network/BitTorrent/Tracker/RPC/MessageSpec.hs27
-rw-r--r--tests/Network/BitTorrent/Tracker/RPC/UDPSpec.hs23
5 files changed, 38 insertions, 29 deletions
diff --git a/bittorrent.cabal b/bittorrent.cabal
index 7177a899..51da7d4f 100644
--- a/bittorrent.cabal
+++ b/bittorrent.cabal
@@ -170,6 +170,9 @@ test-suite spec
170 , network 170 , network
171 , text 171 , text
172 172
173 , mtl
174 , resourcet
175
173 , hspec 176 , hspec
174 , QuickCheck 177 , QuickCheck
175 , quickcheck-instances 178 , quickcheck-instances
diff --git a/src/Network/BitTorrent/Tracker/RPC/HTTP.hs b/src/Network/BitTorrent/Tracker/RPC/HTTP.hs
index 81208590..2006ae70 100644
--- a/src/Network/BitTorrent/Tracker/RPC/HTTP.hs
+++ b/src/Network/BitTorrent/Tracker/RPC/HTTP.hs
@@ -51,8 +51,15 @@ data Connection = Connection
51putConnection :: Connection -> IO () 51putConnection :: Connection -> IO ()
52putConnection = undefined 52putConnection = undefined
53 53
54connect :: URI -> IO Connection 54-- TODO share manager between several threads
55connect = undefined 55connect :: URI -> ResourceT IO Connection
56connect uri = do
57 (_, m) <- allocate (newManager def) closeManager
58 return Connection
59 { announceURI = uri
60 , manager = m
61 , connProxy = Nothing
62 }
56 63
57setSimpleQuery :: SimpleQuery -> Request m -> Request m 64setSimpleQuery :: SimpleQuery -> Request m -> Request m
58setSimpleQuery q r = r 65setSimpleQuery q r = r
diff --git a/src/Network/BitTorrent/Tracker/RPC/Message.hs b/src/Network/BitTorrent/Tracker/RPC/Message.hs
index e91d223e..26868ead 100644
--- a/src/Network/BitTorrent/Tracker/RPC/Message.hs
+++ b/src/Network/BitTorrent/Tracker/RPC/Message.hs
@@ -475,9 +475,6 @@ parseFailureMessage e = BS.concat $ case e of
475parseFailureStatus :: ParamParseFailure -> Status 475parseFailureStatus :: ParamParseFailure -> Status
476parseFailureStatus = mkStatus <$> parseFailureCode <*> parseFailureMessage 476parseFailureStatus = mkStatus <$> parseFailureCode <*> parseFailureMessage
477 477
478--type AnnounceResponse = Either Status AnnounceInfo -- TODO
479--type TrackerResponse = () -- TODO
480
481{----------------------------------------------------------------------- 478{-----------------------------------------------------------------------
482 Scrape message 479 Scrape message
483-----------------------------------------------------------------------} 480-----------------------------------------------------------------------}
diff --git a/tests/Network/BitTorrent/Tracker/RPC/MessageSpec.hs b/tests/Network/BitTorrent/Tracker/RPC/MessageSpec.hs
index 8e95286a..f8cf052a 100644
--- a/tests/Network/BitTorrent/Tracker/RPC/MessageSpec.hs
+++ b/tests/Network/BitTorrent/Tracker/RPC/MessageSpec.hs
@@ -1,7 +1,14 @@
1{-# LANGUAGE RecordWildCards #-}
1{-# OPTIONS -fno-warn-orphans #-} 2{-# OPTIONS -fno-warn-orphans #-}
2module Network.BitTorrent.Tracker.RPC.MessageSpec (spec) where 3module Network.BitTorrent.Tracker.RPC.MessageSpec
4 ( spec
5 , validateInfo
6 , arbitrarySample
7 ) where
3 8
4import Control.Applicative 9import Control.Applicative
10import Data.List as L
11import Data.Maybe
5import Data.Word 12import Data.Word
6import Network 13import Network
7import Test.Hspec 14import Test.Hspec
@@ -11,7 +18,8 @@ import Data.Torrent.InfoHashSpec ()
11import Data.Torrent.ProgressSpec () 18import Data.Torrent.ProgressSpec ()
12import Network.BitTorrent.Core.PeerIdSpec () 19import Network.BitTorrent.Core.PeerIdSpec ()
13 20
14import Network.BitTorrent.Tracker.RPC.Message 21import Network.BitTorrent.Tracker.RPC.Message as Message
22import Network.BitTorrent.Core.PeerAddr
15 23
16 24
17--prop_bencode :: Eq a => BEncode a => a -> Bool 25--prop_bencode :: Eq a => BEncode a => a -> Bool
@@ -31,6 +39,21 @@ instance Arbitrary AnnounceQuery where
31 <$> arbitrary <*> arbitrary <*> arbitrary <*> arbitrary 39 <$> arbitrary <*> arbitrary <*> arbitrary <*> arbitrary
32 <*> arbitrary <*> arbitrary <*> arbitrary 40 <*> arbitrary <*> arbitrary <*> arbitrary
33 41
42validateInfo :: AnnounceQuery -> AnnounceInfo -> Expectation
43validateInfo _ Message.Failure {..} = error "validateInfo: failure"
44validateInfo AnnounceQuery {..} AnnounceInfo {..} = do
45 respComplete `shouldSatisfy` isJust
46 respIncomplete `shouldSatisfy` isJust
47 respMinInterval `shouldSatisfy` isNothing
48 respWarning `shouldSatisfy` isNothing
49 peerList `shouldSatisfy` L.all (isNothing . peerID)
50 fromJust respComplete + fromJust respIncomplete `shouldBe` L.length peerList
51 where
52 peerList = getPeerList respPeers
53
54arbitrarySample :: Arbitrary a => IO a
55arbitrarySample = L.head <$> sample' arbitrary
56
34spec :: Spec 57spec :: Spec
35spec = do 58spec = do
36 describe "Announce" $ do 59 describe "Announce" $ do
diff --git a/tests/Network/BitTorrent/Tracker/RPC/UDPSpec.hs b/tests/Network/BitTorrent/Tracker/RPC/UDPSpec.hs
index 73cf07f3..4954ee25 100644
--- a/tests/Network/BitTorrent/Tracker/RPC/UDPSpec.hs
+++ b/tests/Network/BitTorrent/Tracker/RPC/UDPSpec.hs
@@ -1,44 +1,23 @@
1{-# LANGUAGE RecordWildCards #-} 1{-# LANGUAGE RecordWildCards #-}
2module Network.BitTorrent.Tracker.RPC.UDPSpec (spec) where 2module Network.BitTorrent.Tracker.RPC.UDPSpec (spec) where
3 3
4import Control.Applicative
5import Control.Monad 4import Control.Monad
6import Data.Default 5import Data.Default
7import Data.List as L 6import Data.List as L
8import Data.Maybe 7import Data.Maybe
9import Network.URI 8import Network.URI
10import Test.Hspec 9import Test.Hspec
11import Test.QuickCheck
12 10
13import Network.BitTorrent.Core.PeerAddr 11import Network.BitTorrent.Tracker.RPC.MessageSpec hiding (spec)
14import Network.BitTorrent.Tracker.RPC.Message as Message
15import Network.BitTorrent.Tracker.RPC.UDP 12import Network.BitTorrent.Tracker.RPC.UDP
16import Network.BitTorrent.Tracker.RPC.MessageSpec ()
17 13
18 14
19arbitrarySample :: Arbitrary a => IO a
20arbitrarySample = L.head <$> sample' arbitrary
21
22trackerURIs :: [URI] 15trackerURIs :: [URI]
23trackerURIs = 16trackerURIs =
24 [ fromJust $ parseURI "udp://tracker.openbittorrent.com:80/announce" 17 [ fromJust $ parseURI "udp://tracker.openbittorrent.com:80/announce"
25 , fromJust $ parseURI "udp://tracker.publicbt.com:80/announce" 18 , fromJust $ parseURI "udp://tracker.publicbt.com:80/announce"
26 ] 19 ]
27 20
28-- relation with query: peer id, numwant
29validateInfo :: AnnounceQuery -> AnnounceInfo -> Expectation
30validateInfo _ Message.Failure {..} = error "validateInfo: failure"
31validateInfo AnnounceQuery {..} AnnounceInfo {..} = do
32 respComplete `shouldSatisfy` isJust
33 respIncomplete `shouldSatisfy` isJust
34 respMinInterval `shouldSatisfy` isNothing
35 respWarning `shouldSatisfy` isNothing
36 peerList `shouldSatisfy` L.all (isNothing . peerID)
37 fromJust respComplete + fromJust respIncomplete `shouldBe` L.length peerList
38 where
39 peerList = getPeerList respPeers
40
41
42spec :: Spec 21spec :: Spec
43spec = do 22spec = do
44 forM_ trackerURIs $ \ uri -> 23 forM_ trackerURIs $ \ uri ->