summaryrefslogtreecommitdiff
path: root/tests/Network/BitTorrent/Exchange/WireSpec.hs
diff options
context:
space:
mode:
Diffstat (limited to 'tests/Network/BitTorrent/Exchange/WireSpec.hs')
-rw-r--r--tests/Network/BitTorrent/Exchange/WireSpec.hs59
1 files changed, 59 insertions, 0 deletions
diff --git a/tests/Network/BitTorrent/Exchange/WireSpec.hs b/tests/Network/BitTorrent/Exchange/WireSpec.hs
new file mode 100644
index 00000000..550c20f9
--- /dev/null
+++ b/tests/Network/BitTorrent/Exchange/WireSpec.hs
@@ -0,0 +1,59 @@
1{-# LANGUAGE RecordWildCards #-}
2{-# OPTIONS_GHC -fno-warn-orphans #-}
3module Network.BitTorrent.Exchange.WireSpec (spec) where
4import Control.Applicative
5import Control.Monad.Trans
6import Data.Default
7import Test.Hspec
8import Test.QuickCheck
9
10import Data.Torrent
11import Data.Torrent.InfoHash
12import Network.BitTorrent.Core
13import Network.BitTorrent.Exchange.Message
14import Network.BitTorrent.Exchange.Wire
15
16import Config
17import Network.BitTorrent.Exchange.MessageSpec ()
18
19nullSession :: InfoHash -> PeerId -> ConnectionSession ()
20nullSession ih pid = ConnectionSession ih pid Nothing Nothing ()
21
22instance Arbitrary Options where
23 arbitrary = return def
24
25instance Arbitrary ConnectionPrefs where
26 arbitrary = ConnectionPrefs <$> arbitrary <*> pure def
27 <*> arbitrary <*> arbitrary
28
29withWire :: ConnectionPrefs -> Wire () () -> IO ()
30withWire prefs wire =
31 withRemote $ \ ClientOpts {..} -> do
32 pid <- genPeerId
33 t <- getTestTorrent
34 let ih = idInfoHash (tInfoDict t)
35 let cfg = ConnectionConfig prefs (nullSession ih pid) (wire)
36 let addr = PeerAddr Nothing "127.0.0.1" peerPort
37 connectWire addr cfg
38
39spec :: Spec
40spec = do
41 describe "connectWire" $ do
42 it "can establish connection with all possible preferences" $
43 property $ \ prefs -> do
44 withWire prefs (return ())
45
46 it "must not connect with invalid topic" $ do
47 pending
48
49 describe "acceptWire" $ do
50 it "" $ do
51 pending
52
53 describe "messaging" $ do
54 it "first message is bitfield" $ do
55 withWire def $ do
56 msg <- recvMessage
57 let isBitfield (Available (Bitfield _)) = True
58 isBitfield _ = False
59 liftIO $ msg `shouldSatisfy` isBitfield