diff options
Diffstat (limited to 'tests/Network/BitTorrent')
-rw-r--r-- | tests/Network/BitTorrent/Tracker/RPC/HTTPSpec.hs | 51 | ||||
-rw-r--r-- | tests/Network/BitTorrent/Tracker/RPC/UDPSpec.hs | 66 | ||||
-rw-r--r-- | tests/Network/BitTorrent/Tracker/RPCSpec.hs | 47 | ||||
-rw-r--r-- | tests/Network/BitTorrent/Tracker/SessionSpec.hs | 6 | ||||
-rw-r--r-- | tests/Network/BitTorrent/Tracker/TestData.hs | 10 |
5 files changed, 109 insertions, 71 deletions
diff --git a/tests/Network/BitTorrent/Tracker/RPC/HTTPSpec.hs b/tests/Network/BitTorrent/Tracker/RPC/HTTPSpec.hs index 3e24c0f4..f9eb62d9 100644 --- a/tests/Network/BitTorrent/Tracker/RPC/HTTPSpec.hs +++ b/tests/Network/BitTorrent/Tracker/RPC/HTTPSpec.hs | |||
@@ -1,24 +1,18 @@ | |||
1 | {-# LANGUAGE RecordWildCards #-} | 1 | {-# LANGUAGE RecordWildCards #-} |
2 | module Network.BitTorrent.Tracker.RPC.HTTPSpec (spec, trackerURIs) where | 2 | module Network.BitTorrent.Tracker.RPC.HTTPSpec (spec) where |
3 | 3 | ||
4 | import Control.Applicative | 4 | import Control.Applicative |
5 | import Control.Monad | 5 | import Control.Monad |
6 | import Data.Default | 6 | import Data.Default |
7 | import Data.List as L | 7 | import Data.List as L |
8 | import Data.Maybe | ||
9 | import Network.URI | ||
10 | import Test.Hspec | 8 | import Test.Hspec |
11 | 9 | ||
12 | import Data.Torrent.Progress | 10 | import Data.Torrent.Progress |
13 | import Network.BitTorrent.Tracker.Message as Message | 11 | import Network.BitTorrent.Tracker.Message as Message |
14 | import Network.BitTorrent.Tracker.RPC.HTTP | 12 | import Network.BitTorrent.Tracker.RPC.HTTP |
15 | 13 | ||
16 | -- TODO add a good working tracker! | 14 | import Network.BitTorrent.Tracker.TestData |
17 | trackerURIs :: [URI] | 15 | |
18 | trackerURIs = fmap (fromJust . parseURI) | ||
19 | [ "http://tracker.openbittorrent.com:80/announce" | ||
20 | , "http://tracker.publicbt.com:80/announce" | ||
21 | ] | ||
22 | 16 | ||
23 | validateInfo :: AnnounceQuery -> AnnounceInfo -> Expectation | 17 | validateInfo :: AnnounceQuery -> AnnounceInfo -> Expectation |
24 | validateInfo _ Message.Failure {..} = error "validateInfo: failure" | 18 | validateInfo _ Message.Failure {..} = error "validateInfo: failure" |
@@ -28,20 +22,31 @@ validateInfo AnnounceQuery {..} AnnounceInfo {..} = do | |||
28 | Just n -> n `shouldBe` L.length (getPeerList respPeers) | 22 | Just n -> n `shouldBe` L.length (getPeerList respPeers) |
29 | 23 | ||
30 | spec :: Spec | 24 | spec :: Spec |
31 | spec = do | 25 | spec = parallel $ do |
32 | forM_ trackerURIs $ \ uri -> | 26 | forM_ (L.filter isHttpTracker trackers) $ \ TrackerEntry {..} -> |
33 | context (show uri) $ do | 27 | context trackerName $ do |
28 | |||
34 | describe "announce" $ do | 29 | describe "announce" $ do |
35 | it "have valid response" $ do | 30 | if tryAnnounce |
36 | withManager def $ \ mgr -> do | 31 | then do |
37 | -- q <- arbitrarySample | 32 | it "have valid response" $ do |
38 | let q = AnnounceQuery def "-HS0003-203534.37420" 6000 | 33 | withManager def $ \ mgr -> do |
39 | (Progress 0 0 0) Nothing Nothing (Just Started) | 34 | -- q <- arbitrarySample |
40 | info <- announce mgr uri q | 35 | let q = AnnounceQuery def "-HS0003-203534.37420" 6000 |
41 | validateInfo q info | 36 | (Progress 0 0 0) Nothing Nothing (Just Started) |
37 | info <- announce mgr trackerURI q | ||
38 | validateInfo q info | ||
39 | else do | ||
40 | it "should fail with RequestFailed" $ do | ||
41 | pending | ||
42 | 42 | ||
43 | describe "scrape" $ do | 43 | describe "scrape" $ do |
44 | it "have valid response" $ do | 44 | if tryScraping |
45 | withManager def $ \ mgr -> do | 45 | then do |
46 | xs <- scrape mgr uri [def] | 46 | it "have valid response" $ do |
47 | L.length xs `shouldSatisfy` (>= 1) | 47 | withManager def $ \ mgr -> do |
48 | xs <- scrape mgr trackerURI [def] | ||
49 | L.length xs `shouldSatisfy` (>= 1) | ||
50 | else do | ||
51 | it "should fail with ScrapelessTracker" $ do | ||
52 | pending | ||
diff --git a/tests/Network/BitTorrent/Tracker/RPC/UDPSpec.hs b/tests/Network/BitTorrent/Tracker/RPC/UDPSpec.hs index d0f0f26c..57680a5b 100644 --- a/tests/Network/BitTorrent/Tracker/RPC/UDPSpec.hs +++ b/tests/Network/BitTorrent/Tracker/RPC/UDPSpec.hs | |||
@@ -1,25 +1,19 @@ | |||
1 | {-# LANGUAGE RecordWildCards #-} | 1 | {-# LANGUAGE RecordWildCards #-} |
2 | module Network.BitTorrent.Tracker.RPC.UDPSpec (spec, trackerURIs) where | 2 | module Network.BitTorrent.Tracker.RPC.UDPSpec (spec, rpcOpts) where |
3 | import Control.Concurrent.Async | 3 | import Control.Concurrent.Async |
4 | import Control.Monad | 4 | import Control.Monad |
5 | import Data.Default | 5 | import Data.Default |
6 | import Data.List as L | 6 | import Data.List as L |
7 | import Data.Maybe | 7 | import Data.Maybe |
8 | import Network.URI | ||
9 | import Test.Hspec | 8 | import Test.Hspec |
10 | 9 | ||
11 | import Network.BitTorrent.Tracker.MessageSpec hiding (spec) | ||
12 | import Network.BitTorrent.Tracker.RPC.UDP | ||
13 | |||
14 | import Network.BitTorrent.Core | 10 | import Network.BitTorrent.Core |
15 | import Network.BitTorrent.Tracker.Message as Message | 11 | import Network.BitTorrent.Tracker.Message as Message |
16 | 12 | ||
13 | import Network.BitTorrent.Tracker.TestData | ||
14 | import Network.BitTorrent.Tracker.MessageSpec hiding (spec) | ||
15 | import Network.BitTorrent.Tracker.RPC.UDP | ||
17 | 16 | ||
18 | trackerURIs :: [URI] | ||
19 | trackerURIs = | ||
20 | [ fromJust $ parseURI "udp://tracker.openbittorrent.com:80/announce" | ||
21 | , fromJust $ parseURI "udp://tracker.publicbt.com:80/announce" | ||
22 | ] | ||
23 | 17 | ||
24 | validateInfo :: AnnounceQuery -> AnnounceInfo -> Expectation | 18 | validateInfo :: AnnounceQuery -> AnnounceInfo -> Expectation |
25 | validateInfo _ Message.Failure {..} = error "validateInfo: failure" | 19 | validateInfo _ Message.Failure {..} = error "validateInfo: failure" |
@@ -32,27 +26,45 @@ validateInfo AnnounceQuery {..} AnnounceInfo {..} = do | |||
32 | where | 26 | where |
33 | peerList = getPeerList respPeers | 27 | peerList = getPeerList respPeers |
34 | 28 | ||
29 | -- | Number of concurrent calls. | ||
30 | rpcCount :: Int | ||
31 | rpcCount = 100 | ||
32 | |||
33 | rpcOpts :: Options | ||
34 | rpcOpts = def | ||
35 | { optMinTimeout = 1 | ||
36 | , optMaxTimeout = 10 | ||
37 | } | ||
38 | |||
35 | spec :: Spec | 39 | spec :: Spec |
36 | spec = do | 40 | spec = parallel $ do |
37 | -- describe "RpcException" $ | 41 | forM_ (L.filter isUdpTracker trackers) $ \ TrackerEntry {..} -> |
42 | context trackerName $ do | ||
38 | 43 | ||
39 | parallel $ do | ||
40 | forM_ trackerURIs $ \ uri -> | ||
41 | context (show uri) $ do | ||
42 | describe "announce" $ do | 44 | describe "announce" $ do |
43 | it "have valid response" $ do | 45 | if tryAnnounce then do |
44 | withManager def $ \ mgr -> do | 46 | it "have valid response" $ do |
45 | q <- arbitrarySample | 47 | withManager rpcOpts $ \ mgr -> do |
46 | announce mgr uri q >>= validateInfo q | 48 | q <- arbitrarySample |
49 | announce mgr trackerURI q >>= validateInfo q | ||
50 | else do | ||
51 | it "should throw TrackerNotResponding" $ do | ||
52 | pending | ||
47 | 53 | ||
48 | describe "scrape" $ do | 54 | describe "scrape" $ do |
49 | it "have valid response" $ do | 55 | if tryScraping then do |
50 | withManager def $ \ mgr -> do | 56 | it "have valid response" $ do |
51 | xs <- scrape mgr uri [def] | 57 | withManager rpcOpts $ \ mgr -> do |
52 | L.length xs `shouldSatisfy` (>= 1) | 58 | xs <- scrape mgr trackerURI [def] |
59 | L.length xs `shouldSatisfy` (>= 1) | ||
60 | else do | ||
61 | it "should throw TrackerNotResponding" $ do | ||
62 | pending | ||
63 | |||
53 | 64 | ||
54 | describe "Manager" $ do | 65 | describe "Manager" $ do |
55 | it "should handle arbitrary intermixed concurrent queries" $ do | 66 | when tryScraping $ do |
56 | withManager def $ \ mgr -> do | 67 | it "should handle arbitrary intermixed concurrent queries" $ do |
57 | _ <- mapConcurrently (\ _ -> scrape mgr uri [def]) [1..100 :: Int] | 68 | withManager rpcOpts $ \ mgr -> do |
58 | return () \ No newline at end of file | 69 | _ <- mapConcurrently (\ _ -> scrape mgr trackerURI [def]) [1..rpcCount] |
70 | return () \ No newline at end of file | ||
diff --git a/tests/Network/BitTorrent/Tracker/RPCSpec.hs b/tests/Network/BitTorrent/Tracker/RPCSpec.hs index c3c7f9e2..3b89714c 100644 --- a/tests/Network/BitTorrent/Tracker/RPCSpec.hs +++ b/tests/Network/BitTorrent/Tracker/RPCSpec.hs | |||
@@ -1,37 +1,50 @@ | |||
1 | {-# LANGUAGE RecordWildCards #-} | ||
1 | module Network.BitTorrent.Tracker.RPCSpec (spec) where | 2 | module Network.BitTorrent.Tracker.RPCSpec (spec) where |
2 | import Control.Applicative | 3 | import Control.Applicative |
3 | import Control.Monad | 4 | import Control.Monad |
4 | import Data.Default | 5 | import Data.Default |
5 | import Data.List as L | 6 | import Data.List as L |
6 | import Network.URI | ||
7 | import Test.Hspec | 7 | import Test.Hspec |
8 | import Test.QuickCheck | 8 | import Test.QuickCheck |
9 | 9 | ||
10 | import Network.BitTorrent.Tracker.MessageSpec hiding (spec) | ||
11 | import Network.BitTorrent.Tracker.RPC.HTTPSpec as HTTP hiding (spec) | ||
12 | import Network.BitTorrent.Tracker.RPC.UDPSpec as UDP hiding (spec) | ||
13 | import Network.BitTorrent.Tracker.RPC as RPC | 10 | import Network.BitTorrent.Tracker.RPC as RPC |
14 | 11 | ||
15 | uris :: [URI] | 12 | import Network.BitTorrent.Tracker.TestData |
16 | uris = UDP.trackerURIs ++ HTTP.trackerURIs | 13 | import Network.BitTorrent.Tracker.MessageSpec hiding (spec) |
14 | import qualified Network.BitTorrent.Tracker.RPC.UDPSpec as UDP (rpcOpts) | ||
15 | |||
17 | 16 | ||
18 | instance Arbitrary SAnnounceQuery where | 17 | instance Arbitrary SAnnounceQuery where |
19 | arbitrary = SAnnounceQuery <$> arbitrary <*> arbitrary | 18 | arbitrary = SAnnounceQuery <$> arbitrary <*> arbitrary |
20 | <*> arbitrary <*> arbitrary | 19 | <*> arbitrary <*> arbitrary |
21 | 20 | ||
21 | rpcOpts :: Options | ||
22 | rpcOpts = def | ||
23 | { optUdpRPC = UDP.rpcOpts | ||
24 | } | ||
25 | |||
22 | spec :: Spec | 26 | spec :: Spec |
23 | spec = do | 27 | spec = do |
24 | forM_ uris $ \ uri -> | 28 | forM_ trackers $ \ TrackerEntry {..} -> |
25 | context (show uri) $ do | 29 | context trackerName $ do |
30 | |||
26 | describe "announce" $ do | 31 | describe "announce" $ do |
27 | it "have valid response" $ do | 32 | if tryAnnounce then do |
28 | withManager def def $ \ mgr -> do | 33 | it "have valid response" $ do |
29 | q <- arbitrarySample | 34 | withManager rpcOpts def $ \ mgr -> do |
30 | _ <- announce mgr uri q | 35 | q <- arbitrarySample |
31 | return () | 36 | _ <- announce mgr trackerURI q |
37 | return () | ||
38 | else do | ||
39 | it "should throw exception" $ do | ||
40 | pending | ||
32 | 41 | ||
33 | describe "scrape" $ do | 42 | describe "scrape" $ do |
34 | it "have valid response" $ do | 43 | if tryScraping then do |
35 | withManager def def $ \ mgr -> do | 44 | it "have valid response" $ do |
36 | xs <- scrape mgr uri [def] | 45 | withManager rpcOpts def $ \ mgr -> do |
37 | L.length xs `shouldSatisfy` (>= 1) | 46 | xs <- scrape mgr trackerURI [def] |
47 | L.length xs `shouldSatisfy` (>= 1) | ||
48 | else do | ||
49 | it "should throw exception" $ do | ||
50 | pending | ||
diff --git a/tests/Network/BitTorrent/Tracker/SessionSpec.hs b/tests/Network/BitTorrent/Tracker/SessionSpec.hs index 0c75fcaa..db86b60e 100644 --- a/tests/Network/BitTorrent/Tracker/SessionSpec.hs +++ b/tests/Network/BitTorrent/Tracker/SessionSpec.hs | |||
@@ -6,20 +6,18 @@ import Test.Hspec | |||
6 | 6 | ||
7 | import Data.Torrent | 7 | import Data.Torrent |
8 | import Network.BitTorrent.Tracker.List | 8 | import Network.BitTorrent.Tracker.List |
9 | import Network.BitTorrent.Tracker.RPC.UDPSpec (trackerURIs) | ||
10 | import Network.BitTorrent.Tracker.RPC | 9 | import Network.BitTorrent.Tracker.RPC |
11 | import Network.BitTorrent.Tracker.Session | 10 | import Network.BitTorrent.Tracker.Session |
12 | 11 | ||
12 | import Network.BitTorrent.Tracker.TestData | ||
13 | 13 | ||
14 | trackers :: TrackerList URI | ||
15 | trackers = trackerList def { tAnnounceList = Just [trackerURIs] } | ||
16 | 14 | ||
17 | spec :: Spec | 15 | spec :: Spec |
18 | spec = do | 16 | spec = do |
19 | describe "Session" $ do | 17 | describe "Session" $ do |
20 | it "" $ do | 18 | it "" $ do |
21 | withManager def def $ \ m -> do | 19 | withManager def def $ \ m -> do |
22 | s <- newSession def trackers | 20 | s <- newSession def undefined |
23 | notify m s Started | 21 | notify m s Started |
24 | peers <- askPeers m s | 22 | peers <- askPeers m s |
25 | peers `shouldSatisfy` (not . L.null) \ No newline at end of file | 23 | peers `shouldSatisfy` (not . L.null) \ No newline at end of file |
diff --git a/tests/Network/BitTorrent/Tracker/TestData.hs b/tests/Network/BitTorrent/Tracker/TestData.hs index 5d9718e5..85544b91 100644 --- a/tests/Network/BitTorrent/Tracker/TestData.hs +++ b/tests/Network/BitTorrent/Tracker/TestData.hs | |||
@@ -1,6 +1,9 @@ | |||
1 | {-# LANGUAGE RecordWildCards #-} | ||
1 | {-# OPTIONS_GHC -fno-warn-orphans #-} | 2 | {-# OPTIONS_GHC -fno-warn-orphans #-} |
2 | module Network.BitTorrent.Tracker.TestData | 3 | module Network.BitTorrent.Tracker.TestData |
3 | ( TrackerEntry (..) | 4 | ( TrackerEntry (..) |
5 | , isUdpTracker | ||
6 | , isHttpTracker | ||
4 | , trackers | 7 | , trackers |
5 | ) where | 8 | ) where |
6 | 9 | ||
@@ -29,6 +32,13 @@ data TrackerEntry = TrackerEntry | |||
29 | , hashList :: Maybe [InfoHash] | 32 | , hashList :: Maybe [InfoHash] |
30 | } | 33 | } |
31 | 34 | ||
35 | isUdpTracker :: TrackerEntry -> Bool | ||
36 | isUdpTracker TrackerEntry {..} = uriScheme trackerURI == "udp:" | ||
37 | |||
38 | isHttpTracker :: TrackerEntry -> Bool | ||
39 | isHttpTracker TrackerEntry {..} = uriScheme trackerURI == "http:" | ||
40 | || uriScheme trackerURI == "https:" | ||
41 | |||
32 | instance IsString URI where | 42 | instance IsString URI where |
33 | fromString str = fromMaybe err $ parseURI str | 43 | fromString str = fromMaybe err $ parseURI str |
34 | where | 44 | where |