summaryrefslogtreecommitdiff
path: root/tests/Network/BitTorrent/Tracker/RPC/UDPSpec.hs
blob: d0f0f26c4d51c9ee15e134afe7abe495d8cd7ff2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
{-# LANGUAGE RecordWildCards #-}
module Network.BitTorrent.Tracker.RPC.UDPSpec (spec, trackerURIs) where
import Control.Concurrent.Async
import Control.Monad
import Data.Default
import Data.List as L
import Data.Maybe
import Network.URI
import Test.Hspec

import Network.BitTorrent.Tracker.MessageSpec hiding (spec)
import Network.BitTorrent.Tracker.RPC.UDP

import Network.BitTorrent.Core
import Network.BitTorrent.Tracker.Message as Message


trackerURIs :: [URI]
trackerURIs =
  [ fromJust $ parseURI "udp://tracker.openbittorrent.com:80/announce"
  , fromJust $ parseURI "udp://tracker.publicbt.com:80/announce"
  ]

validateInfo :: AnnounceQuery -> AnnounceInfo -> Expectation
validateInfo _ Message.Failure {..} = error "validateInfo: failure"
validateInfo AnnounceQuery {..}  AnnounceInfo {..} = do
    respComplete    `shouldSatisfy` isJust
    respIncomplete  `shouldSatisfy` isJust
    respMinInterval `shouldSatisfy` isNothing
    respWarning     `shouldSatisfy` isNothing
    peerList `shouldSatisfy` L.all (isNothing . peerId)
  where
    peerList = getPeerList respPeers

spec :: Spec
spec = do
--  describe "RpcException" $

  parallel $ do
   forM_ trackerURIs $ \ uri ->
    context (show uri) $ do
      describe "announce" $ do
        it "have valid response" $ do
          withManager def $ \ mgr -> do
            q <- arbitrarySample
            announce mgr uri q >>= validateInfo q

      describe "scrape" $ do
        it "have valid response" $ do
          withManager def $ \ mgr -> do
            xs <- scrape mgr uri [def]
            L.length xs `shouldSatisfy` (>= 1)

      describe "Manager" $ do
        it "should handle arbitrary intermixed concurrent queries" $ do
          withManager def $ \ mgr -> do
            _ <- mapConcurrently (\ _ -> scrape mgr uri [def]) [1..100 :: Int]
            return ()