summaryrefslogtreecommitdiff
path: root/tests
diff options
context:
space:
mode:
authorSam Truzjan <pxqr.sta@gmail.com>2014-04-26 07:42:57 +0400
committerSam Truzjan <pxqr.sta@gmail.com>2014-04-26 07:42:57 +0400
commita7fda9d39ed82cb9d3ad0c28e76e88e59539a492 (patch)
tree925183a691bbb57ca5f7140614e1fdbc610b3b1e /tests
parent4587ffd5406162bb06a6549ffd2ff277e0a93916 (diff)
parent85bf8475bbbce79b1bedde641192fa945614283d (diff)
Merge branch 'tidy' into dev
Diffstat (limited to 'tests')
-rw-r--r--tests/Config.hs2
-rw-r--r--tests/Data/Torrent/InfoHashSpec.hs40
-rw-r--r--tests/Data/Torrent/LayoutSpec.hs30
-rw-r--r--tests/Data/Torrent/MagnetSpec.hs44
-rw-r--r--tests/Data/Torrent/MetainfoSpec.hs80
-rw-r--r--tests/Data/Torrent/PieceSpec.hs13
-rw-r--r--tests/Data/TorrentSpec.hs139
-rw-r--r--tests/Network/BitTorrent/Client/HandleSpec.hs2
-rw-r--r--tests/Network/BitTorrent/Core/FingerprintSpec.hs33
-rw-r--r--tests/Network/BitTorrent/Core/NodeInfoSpec.hs52
-rw-r--r--tests/Network/BitTorrent/Core/PeerAddrSpec.hs221
-rw-r--r--tests/Network/BitTorrent/Core/PeerIdSpec.hs25
-rw-r--r--tests/Network/BitTorrent/CoreSpec.hs308
-rw-r--r--tests/Network/BitTorrent/DHT/MessageSpec.hs4
-rw-r--r--tests/Network/BitTorrent/DHT/QuerySpec.hs2
-rw-r--r--tests/Network/BitTorrent/DHT/RoutingSpec.hs2
-rw-r--r--tests/Network/BitTorrent/DHT/SessionSpec.hs5
-rw-r--r--tests/Network/BitTorrent/DHT/TestData.hs2
-rw-r--r--tests/Network/BitTorrent/DHT/TokenSpec.hs2
-rw-r--r--tests/Network/BitTorrent/DHTSpec.hs2
-rw-r--r--tests/Network/BitTorrent/Exchange/BitfieldSpec.hs (renamed from tests/Data/Torrent/BitfieldSpec.hs)4
-rw-r--r--tests/Network/BitTorrent/Exchange/ConnectionSpec.hs3
-rw-r--r--tests/Network/BitTorrent/Exchange/DownloadSpec.hs (renamed from tests/Network/BitTorrent/Exchange/Session/MetadataSpec.hs)32
-rw-r--r--tests/Network/BitTorrent/Exchange/MessageSpec.hs6
-rw-r--r--tests/Network/BitTorrent/Exchange/SessionSpec.hs2
-rw-r--r--tests/Network/BitTorrent/Internal/ProgressSpec.hs (renamed from tests/Data/Torrent/ProgressSpec.hs)4
-rw-r--r--tests/Network/BitTorrent/Tracker/MessageSpec.hs10
-rw-r--r--tests/Network/BitTorrent/Tracker/RPC/HTTPSpec.hs2
-rw-r--r--tests/Network/BitTorrent/Tracker/RPC/UDPSpec.hs2
-rw-r--r--tests/Network/BitTorrent/Tracker/TestData.hs2
-rw-r--r--tests/System/Torrent/FileMapSpec.hs2
-rw-r--r--tests/System/Torrent/StorageSpec.hs5
32 files changed, 482 insertions, 600 deletions
diff --git a/tests/Config.hs b/tests/Config.hs
index 09e838cc..9ffb0d8c 100644
--- a/tests/Config.hs
+++ b/tests/Config.hs
@@ -33,7 +33,7 @@ import System.IO.Unsafe
33import Test.Hspec 33import Test.Hspec
34 34
35import Data.Torrent 35import Data.Torrent
36import Network.BitTorrent.Core (IP, PeerAddr (PeerAddr), genPeerId) 36import Network.BitTorrent.Address (IP, PeerAddr (PeerAddr), genPeerId)
37 37
38 38
39type ClientName = String 39type ClientName = String
diff --git a/tests/Data/Torrent/InfoHashSpec.hs b/tests/Data/Torrent/InfoHashSpec.hs
deleted file mode 100644
index 06570ec9..00000000
--- a/tests/Data/Torrent/InfoHashSpec.hs
+++ /dev/null
@@ -1,40 +0,0 @@
1{-# OPTIONS -fno-warn-orphans #-}
2module Data.Torrent.InfoHashSpec (spec) where
3
4import Control.Applicative
5import Data.ByteString as BS
6import Data.Convertible
7import System.FilePath
8import Test.Hspec
9import Test.QuickCheck
10import Test.QuickCheck.Instances ()
11
12import Data.Torrent
13import Data.Torrent.InfoHash as IH
14
15
16instance Arbitrary InfoHash where
17 arbitrary = do
18 bs <- BS.pack <$> vectorOf 20 arbitrary
19 pure $ either (const (error "arbitrary infohash")) id $ safeConvert bs
20
21type TestPair = (FilePath, String)
22
23-- TODO add a few more torrents here
24torrentList :: [TestPair]
25torrentList =
26 [ ( "res" </> "dapper-dvd-amd64.iso.torrent"
27 , "0221caf96aa3cb94f0f58d458e78b0fc344ad8bf")
28 ]
29
30infohashSpec :: (FilePath, String) -> Spec
31infohashSpec (filepath, expectedHash) = do
32 it ("should match " ++ filepath) $ do
33 torrent <- fromFile filepath
34 let actualHash = show $ idInfoHash $ tInfoDict torrent
35 actualHash `shouldBe` expectedHash
36
37spec :: Spec
38spec = do
39 describe "info hash" $ do
40 mapM_ infohashSpec torrentList
diff --git a/tests/Data/Torrent/LayoutSpec.hs b/tests/Data/Torrent/LayoutSpec.hs
deleted file mode 100644
index d3966b3f..00000000
--- a/tests/Data/Torrent/LayoutSpec.hs
+++ /dev/null
@@ -1,30 +0,0 @@
1{-# LANGUAGE GeneralizedNewtypeDeriving #-}
2{-# LANGUAGE StandaloneDeriving #-}
3module Data.Torrent.LayoutSpec (spec) where
4
5import Control.Applicative
6import Test.Hspec
7import Test.QuickCheck
8import System.Posix.Types
9
10import Data.Torrent.Layout
11
12
13instance Arbitrary COff where
14 arbitrary = fromIntegral <$> (arbitrary :: Gen Int)
15
16instance Arbitrary a => Arbitrary (FileInfo a) where
17 arbitrary = FileInfo <$> arbitrary <*> arbitrary <*> arbitrary
18
19instance Arbitrary LayoutInfo where
20 arbitrary = oneof
21 [ SingleFile <$> arbitrary
22 , MultiFile <$> arbitrary <*> arbitrary
23 ]
24
25spec :: Spec
26spec = do
27 describe "accumPosition" $ do
28 it "" $ property $ \ p1 p2 p3 s1 s2 s3 ->
29 accumPositions [(p1, s1), (p2, s2), (p3, s3)]
30 `shouldBe` [(p1, (0, s1)), (p2, (s1, s2)), (p3, (s1 + s2, s3))] \ No newline at end of file
diff --git a/tests/Data/Torrent/MagnetSpec.hs b/tests/Data/Torrent/MagnetSpec.hs
deleted file mode 100644
index 13554455..00000000
--- a/tests/Data/Torrent/MagnetSpec.hs
+++ /dev/null
@@ -1,44 +0,0 @@
1{-# OPTIONS -fno-warn-orphans #-}
2module Data.Torrent.MagnetSpec (spec) where
3
4import Control.Applicative
5import Data.Maybe
6import Data.Monoid
7import Test.Hspec
8import Test.QuickCheck
9import Test.QuickCheck.Instances ()
10import Network.URI
11
12import Data.Torrent.Magnet
13import Data.Torrent.InfoHashSpec ()
14
15
16instance Arbitrary URIAuth where
17 arbitrary = URIAuth <$> arbitrary <*> arbitrary <*> arbitrary
18
19instance Arbitrary URI where
20 arbitrary
21 = pure $ fromJust $ parseURI "http://ietf.org/1737.txt?a=1&b=h#123"
22
23instance Arbitrary Magnet where
24 arbitrary = Magnet <$> arbitrary <*> arbitrary
25 <*> arbitrary <*> arbitrary <*> arbitrary <*> arbitrary
26 <*> arbitrary <*> arbitrary <*> pure mempty
27
28magnetEncoding :: Magnet -> IO ()
29magnetEncoding m = parseMagnet (renderMagnet m) `shouldBe` Just m
30
31spec :: Spec
32spec = do
33 describe "Magnet" $ do
34 it "properly encoded" $ property $ magnetEncoding
35
36 it "parse base32" $ do
37 let magnet = "magnet:?xt=urn:btih:CT76LXJDDCH5LS2TUHKH6EUJ3NYKX4Y6"
38 let ih = "CT76LXJDDCH5LS2TUHKH6EUJ3NYKX4Y6"
39 parseMagnet magnet `shouldBe` Just (nullMagnet ih)
40
41 it "parse base16" $ do
42 let magnet = "magnet:?xt=urn:btih:0123456789abcdef0123456789abcdef01234567"
43 let ih = "0123456789abcdef0123456789abcdef01234567"
44 parseMagnet magnet `shouldBe` Just (nullMagnet ih)
diff --git a/tests/Data/Torrent/MetainfoSpec.hs b/tests/Data/Torrent/MetainfoSpec.hs
deleted file mode 100644
index 369c5e0f..00000000
--- a/tests/Data/Torrent/MetainfoSpec.hs
+++ /dev/null
@@ -1,80 +0,0 @@
1{-# LANGUAGE TypeSynonymInstances #-}
2{-# OPTIONS -fno-warn-orphans #-}
3module Data.Torrent.MetainfoSpec (spec) where
4
5import Control.Applicative
6import Data.ByteString as BS
7import Data.ByteString.Lazy as BL
8import Data.BEncode
9import Data.Maybe
10import Data.Time
11import Network.URI
12import Test.Hspec
13import Test.QuickCheck
14import Test.QuickCheck.Instances ()
15
16import Data.Torrent.Piece
17import Data.Torrent.Layout
18import Data.Torrent
19import Data.Torrent.LayoutSpec ()
20import Network.BitTorrent.Core.NodeInfoSpec ()
21
22{-----------------------------------------------------------------------
23-- Common
24-----------------------------------------------------------------------}
25
26data T a = T
27
28prop_properBEncode :: Show a => BEncode a => Eq a
29 => T a -> a -> IO ()
30prop_properBEncode _ expected = actual `shouldBe` Right expected
31 where
32 actual = decode $ BL.toStrict $ encode expected
33
34instance Arbitrary URI where
35 arbitrary = pure $ fromJust
36 $ parseURI "http://exsample.com:80/123365_asd"
37
38{-----------------------------------------------------------------------
39-- Instances
40-----------------------------------------------------------------------}
41
42instance Arbitrary HashList where
43 arbitrary = HashList <$> arbitrary
44
45instance Arbitrary PieceInfo where
46 arbitrary = PieceInfo <$> arbitrary <*> arbitrary
47
48instance Arbitrary InfoDict where
49 arbitrary = infoDictionary <$> arbitrary <*> arbitrary <*> arbitrary
50
51pico :: Gen (Maybe NominalDiffTime)
52pico = oneof
53 [ pure Nothing
54 , (Just . fromIntegral) <$> (arbitrary :: Gen Int)
55 ]
56
57instance Arbitrary Torrent where
58 arbitrary = Torrent <$> arbitrary
59 <*> arbitrary <*> arbitrary <*> arbitrary
60 <*> pico <*> arbitrary <*> arbitrary
61 <*> arbitrary
62 <*> arbitrary <*> pure Nothing <*> arbitrary
63
64{-----------------------------------------------------------------------
65-- Spec
66-----------------------------------------------------------------------}
67
68spec :: Spec
69spec = do
70 describe "FileInfo" $ do
71 it "properly bencoded" $ property $
72 prop_properBEncode (T :: T (FileInfo BS.ByteString))
73
74 describe "LayoutInfo" $ do
75 it "properly bencoded" $ property $
76 prop_properBEncode (T :: T LayoutInfo)
77
78 describe "Torrent" $ do
79 it "property bencoded" $ property $
80 prop_properBEncode (T :: T Torrent)
diff --git a/tests/Data/Torrent/PieceSpec.hs b/tests/Data/Torrent/PieceSpec.hs
deleted file mode 100644
index ef1f2938..00000000
--- a/tests/Data/Torrent/PieceSpec.hs
+++ /dev/null
@@ -1,13 +0,0 @@
1{-# OPTIONS_GHC -fno-warn-orphans #-}
2module Data.Torrent.PieceSpec (spec) where
3import Control.Applicative
4import Test.Hspec
5import Test.QuickCheck
6import Data.Torrent.Piece
7
8
9instance Arbitrary a => Arbitrary (Piece a) where
10 arbitrary = Piece <$> arbitrary <*> arbitrary
11
12spec :: Spec
13spec = return () \ No newline at end of file
diff --git a/tests/Data/TorrentSpec.hs b/tests/Data/TorrentSpec.hs
new file mode 100644
index 00000000..b4a280e4
--- /dev/null
+++ b/tests/Data/TorrentSpec.hs
@@ -0,0 +1,139 @@
1{-# LANGUAGE TypeSynonymInstances #-}
2{-# LANGUAGE GeneralizedNewtypeDeriving #-}
3{-# LANGUAGE StandaloneDeriving #-}
4{-# OPTIONS -fno-warn-orphans #-}
5module Data.TorrentSpec (spec) where
6import Control.Applicative
7import Data.BEncode
8import Data.ByteString as BS
9import Data.ByteString.Lazy as BL
10import Data.Convertible
11import Data.Maybe
12import Data.Monoid
13import Data.Time
14import Network.URI
15import System.FilePath
16import System.Posix.Types
17import Test.Hspec
18import Test.QuickCheck
19import Test.QuickCheck.Instances ()
20
21import Data.Torrent
22import Network.BitTorrent.CoreSpec ()
23
24
25pico :: Gen (Maybe NominalDiffTime)
26pico = oneof
27 [ pure Nothing
28 , (Just . fromIntegral) <$> (arbitrary :: Gen Int)
29 ]
30
31instance Arbitrary COff where
32 arbitrary = fromIntegral <$> (arbitrary :: Gen Int)
33
34instance Arbitrary URIAuth where
35 arbitrary = URIAuth <$> arbitrary <*> arbitrary <*> arbitrary
36
37instance Arbitrary URI where
38 arbitrary
39 = pure $ fromJust $ parseURI "http://ietf.org/1737.txt?a=1&b=h#123"
40
41instance Arbitrary InfoHash where
42 arbitrary = do
43 bs <- BS.pack <$> vectorOf 20 arbitrary
44 pure $ either (const (error "arbitrary infohash")) id $ safeConvert bs
45
46instance Arbitrary a => Arbitrary (FileInfo a) where
47 arbitrary = FileInfo <$> arbitrary <*> arbitrary <*> arbitrary
48
49instance Arbitrary LayoutInfo where
50 arbitrary = oneof
51 [ SingleFile <$> arbitrary
52 , MultiFile <$> arbitrary <*> arbitrary
53 ]
54
55instance Arbitrary a => Arbitrary (Piece a) where
56 arbitrary = Piece <$> arbitrary <*> arbitrary
57
58instance Arbitrary HashList where
59 arbitrary = HashList <$> arbitrary
60
61instance Arbitrary PieceInfo where
62 arbitrary = PieceInfo <$> arbitrary <*> arbitrary
63
64instance Arbitrary InfoDict where
65 arbitrary = infoDictionary <$> arbitrary <*> arbitrary <*> arbitrary
66
67instance Arbitrary Torrent where
68 arbitrary = Torrent <$> arbitrary
69 <*> arbitrary <*> arbitrary <*> arbitrary
70 <*> pico <*> arbitrary <*> arbitrary
71 <*> arbitrary
72 <*> arbitrary <*> pure Nothing <*> arbitrary
73
74instance Arbitrary Magnet where
75 arbitrary = Magnet <$> arbitrary <*> arbitrary
76 <*> arbitrary <*> arbitrary <*> arbitrary <*> arbitrary
77 <*> arbitrary <*> arbitrary <*> pure mempty
78
79type TestPair = (FilePath, String)
80
81-- TODO add a few more torrents here
82torrentList :: [TestPair]
83torrentList =
84 [ ( "res" </> "dapper-dvd-amd64.iso.torrent"
85 , "0221caf96aa3cb94f0f58d458e78b0fc344ad8bf")
86 ]
87
88infohashSpec :: (FilePath, String) -> Spec
89infohashSpec (filepath, expectedHash) = do
90 it ("should match " ++ filepath) $ do
91 torrent <- fromFile filepath
92 let actualHash = show $ idInfoHash $ tInfoDict torrent
93 actualHash `shouldBe` expectedHash
94
95magnetEncoding :: Magnet -> IO ()
96magnetEncoding m = parseMagnet (renderMagnet m) `shouldBe` Just m
97
98data T a = T
99
100prop_properBEncode :: Show a => BEncode a => Eq a
101 => T a -> a -> IO ()
102prop_properBEncode _ expected = actual `shouldBe` Right expected
103 where
104 actual = decode $ BL.toStrict $ encode expected
105
106spec :: Spec
107spec = do
108 describe "info hash" $ do
109 mapM_ infohashSpec torrentList
110
111 describe "accumPosition" $ do
112 it "" $ property $ \ p1 p2 p3 s1 s2 s3 ->
113 accumPositions [(p1, s1), (p2, s2), (p3, s3)]
114 `shouldBe` [(p1, (0, s1)), (p2, (s1, s2)), (p3, (s1 + s2, s3))]
115
116 describe "FileInfo" $ do
117 it "properly bencoded" $ property $
118 prop_properBEncode (T :: T (FileInfo BS.ByteString))
119
120 describe "LayoutInfo" $ do
121 it "properly bencoded" $ property $
122 prop_properBEncode (T :: T LayoutInfo)
123
124 describe "Torrent" $ do
125 it "property bencoded" $ property $
126 prop_properBEncode (T :: T Torrent)
127
128 describe "Magnet" $ do
129 it "properly encoded" $ property $ magnetEncoding
130
131 it "parse base32" $ do
132 let magnet = "magnet:?xt=urn:btih:CT76LXJDDCH5LS2TUHKH6EUJ3NYKX4Y6"
133 let ih = "CT76LXJDDCH5LS2TUHKH6EUJ3NYKX4Y6"
134 parseMagnet magnet `shouldBe` Just (nullMagnet ih)
135
136 it "parse base16" $ do
137 let magnet = "magnet:?xt=urn:btih:0123456789abcdef0123456789abcdef01234567"
138 let ih = "0123456789abcdef0123456789abcdef01234567"
139 parseMagnet magnet `shouldBe` Just (nullMagnet ih)
diff --git a/tests/Network/BitTorrent/Client/HandleSpec.hs b/tests/Network/BitTorrent/Client/HandleSpec.hs
index 8e16d9b0..d51bab02 100644
--- a/tests/Network/BitTorrent/Client/HandleSpec.hs
+++ b/tests/Network/BitTorrent/Client/HandleSpec.hs
@@ -2,7 +2,7 @@ module Network.BitTorrent.Client.HandleSpec (spec) where
2import Data.Default 2import Data.Default
3import Test.Hspec 3import Test.Hspec
4 4
5import Data.Torrent.Magnet 5import Data.Torrent
6import Network.BitTorrent.Client 6import Network.BitTorrent.Client
7import Network.BitTorrent.Client.Handle 7import Network.BitTorrent.Client.Handle
8 8
diff --git a/tests/Network/BitTorrent/Core/FingerprintSpec.hs b/tests/Network/BitTorrent/Core/FingerprintSpec.hs
deleted file mode 100644
index df62442a..00000000
--- a/tests/Network/BitTorrent/Core/FingerprintSpec.hs
+++ /dev/null
@@ -1,33 +0,0 @@
1-- | see <http://bittorrent.org/beps/bep_0020.html>
2module Network.BitTorrent.Core.FingerprintSpec (spec) where
3import Test.Hspec
4import Network.BitTorrent.Core.PeerId
5
6spec :: Spec
7spec = do
8 describe "client info" $ do
9 it "decode mainline encoded peer id" $ do
10 fingerprint "M4-3-6--xxxxxxxxxxxx" `shouldBe` "Mainline-4.3.6"
11 fingerprint "M4-20-8-xxxxxxxxxxxx" `shouldBe` "Mainline-4.20.8"
12
13 it "decode azureus encoded peer id" $ do
14 fingerprint "-AZ2060-xxxxxxxxxxxx" `shouldBe` "Azureus-2060"
15 fingerprint "-BS0000-xxxxxxxxxxxx" `shouldBe` "BTSlave-0"
16
17 it "decode Shad0w style peer id" $ do
18 fingerprint "S58B-----xxxxxxxxxxx" `shouldBe` "Shadow-5.8.11"
19 fingerprint "T58B-----xxxxxxxxxxx" `shouldBe` "BitTornado-5.8.11"
20
21 it "decode bitcomet style peer id" $ do
22 fingerprint "exbc01xxxxxxxxxxxxxx" `shouldBe` "BitComet-48.49"
23 fingerprint "FUTB01xxxxxxxxxxxxxx" `shouldBe` "BitComet-48.49"
24 fingerprint "exbc01LORDxxxxxxxxxx" `shouldBe` "BitLord-48.49"
25
26 it "decode opera style peer id" $ do
27 fingerprint "OP0123xxxxxxxxxxxxxx" `shouldBe` "Opera-123"
28
29 it "decode ML donkey style peer id" $ do
30 fingerprint "-ML2.7.2-xxxxxxxxxxx" `shouldBe` "MLdonkey-0"
31
32-- TODO XBT, Bits on Wheels, Queen Bee, BitTyrant, TorrenTopia,
33-- BitSpirit, Rufus, G3 Torrent, FlashGet \ No newline at end of file
diff --git a/tests/Network/BitTorrent/Core/NodeInfoSpec.hs b/tests/Network/BitTorrent/Core/NodeInfoSpec.hs
deleted file mode 100644
index fb777440..00000000
--- a/tests/Network/BitTorrent/Core/NodeInfoSpec.hs
+++ /dev/null
@@ -1,52 +0,0 @@
1{-# OPTIONS -fno-warn-orphans #-}
2module Network.BitTorrent.Core.NodeInfoSpec (spec) where
3import Control.Applicative
4import Data.Serialize as S
5import Data.String
6import Test.Hspec
7import Test.QuickCheck
8
9import Network.BitTorrent.Core
10import Network.BitTorrent.Core.PeerAddrSpec ()
11
12instance Arbitrary NodeId where
13 arbitrary = fromString <$> vector 20
14
15instance Arbitrary a => Arbitrary (NodeAddr a) where
16 arbitrary = NodeAddr <$> arbitrary <*> arbitrary
17
18instance Arbitrary a => Arbitrary (NodeInfo a) where
19 arbitrary = NodeInfo <$> arbitrary <*> arbitrary
20
21spec :: Spec
22spec = do
23 describe "NodeId" $ do
24 it "properly serialized" $ do
25 S.decode "mnopqrstuvwxyz123456"
26 `shouldBe` Right ("mnopqrstuvwxyz123456" :: NodeId)
27
28 S.encode ("mnopqrstuvwxyz123456" :: NodeId)
29 `shouldBe` "mnopqrstuvwxyz123456"
30
31 it "properly serialized (iso)" $ property $ \ nid ->
32 S.decode (S.encode nid) `shouldBe`
33 Right (nid :: NodeId)
34
35 describe "NodeAddr" $ do
36 it "properly serialized" $ do
37 S.decode "\127\0\0\1\1\2" `shouldBe`
38 Right ("127.0.0.1:258" :: NodeAddr IPv4)
39
40 it "properly serialized (iso)" $ property $ \ nid ->
41 S.decode (S.encode nid) `shouldBe`
42 Right (nid :: NodeAddr IPv4)
43
44 describe "NodeInfo" $ do
45 it "properly serialized" $ do
46 S.decode "mnopqrstuvwxyz123456\
47 \\127\0\0\1\1\2" `shouldBe` Right
48 (NodeInfo "mnopqrstuvwxyz123456" "127.0.0.1:258" :: NodeInfo IPv4)
49
50 it "properly serialized (iso)" $ property $ \ nid ->
51 S.decode (S.encode nid) `shouldBe`
52 Right (nid :: NodeInfo IPv4)
diff --git a/tests/Network/BitTorrent/Core/PeerAddrSpec.hs b/tests/Network/BitTorrent/Core/PeerAddrSpec.hs
deleted file mode 100644
index abb90183..00000000
--- a/tests/Network/BitTorrent/Core/PeerAddrSpec.hs
+++ /dev/null
@@ -1,221 +0,0 @@
1{-# LANGUAGE FlexibleInstances #-}
2{-# OPTIONS_GHC -fno-warn-orphans #-}
3module Network.BitTorrent.Core.PeerAddrSpec (spec) where
4import Control.Applicative
5import Data.BEncode as BE
6import Data.ByteString.Lazy as BL
7import Data.IP
8import Data.Serialize as S
9import Data.Word
10import Network
11import Test.Hspec
12import Test.QuickCheck
13
14import Network.BitTorrent.Core.PeerIdSpec hiding (spec)
15import Network.BitTorrent.Core.PeerAddr
16
17instance Arbitrary IPv4 where
18 arbitrary = do
19 a <- choose (0, 255)
20 b <- choose (0, 255)
21 c <- choose (0, 255)
22 d <- choose (0, 255)
23 return $ toIPv4 [a, b, c, d]
24
25instance Arbitrary IPv6 where
26 arbitrary = do
27 a <- choose (0, fromIntegral (maxBound :: Word16))
28 b <- choose (0, fromIntegral (maxBound :: Word16))
29 c <- choose (0, fromIntegral (maxBound :: Word16))
30 d <- choose (0, fromIntegral (maxBound :: Word16))
31 e <- choose (0, fromIntegral (maxBound :: Word16))
32 f <- choose (0, fromIntegral (maxBound :: Word16))
33 g <- choose (0, fromIntegral (maxBound :: Word16))
34 h <- choose (0, fromIntegral (maxBound :: Word16))
35 return $ toIPv6 [a, b, c, d, e, f, g, h]
36
37instance Arbitrary IP where
38 arbitrary = frequency
39 [ (1, IPv4 <$> arbitrary)
40 , (1, IPv6 <$> arbitrary)
41 ]
42
43instance Arbitrary PortNumber where
44 arbitrary = fromIntegral <$> (arbitrary :: Gen Word16)
45
46instance Arbitrary a => Arbitrary (PeerAddr a) where
47 arbitrary = PeerAddr <$> arbitrary <*> arbitrary <*> arbitrary
48
49spec :: Spec
50spec = do
51 describe "PortNumber" $ do
52 it "properly serialized" $ do
53 S.decode "\x1\x2" `shouldBe` Right (258 :: PortNumber)
54 S.encode (258 :: PortNumber) `shouldBe` "\x1\x2"
55
56 it "properly bencoded" $ do
57 BE.decode "i80e" `shouldBe` Right (80 :: PortNumber)
58
59 it "fail if port number is invalid" $ do
60 (BE.decode "i-10e" :: BE.Result PortNumber)
61 `shouldBe`
62 Left "fromBEncode: unable to decode PortNumber: -10"
63
64 (BE.decode "i70000e" :: BE.Result PortNumber)
65 `shouldBe`
66 Left "fromBEncode: unable to decode PortNumber: 70000"
67
68 describe "Peer IPv4" $ do
69 it "properly serialized" $ do
70 S.decode "\x1\x2\x3\x4" `shouldBe` Right (toIPv4 [1, 2, 3, 4])
71 S.encode (toIPv4 [1, 2, 3, 4]) `shouldBe` "\x1\x2\x3\x4"
72
73 it "properly serialized (iso)" $ property $ \ ip -> do
74 S.decode (S.encode ip) `shouldBe` Right (ip :: IPv4)
75
76 it "properly bencoded" $ do
77 BE.decode "11:168.192.0.1" `shouldBe` Right (toIPv4 [168, 192, 0, 1])
78 BE.encode (toIPv4 [168, 192, 0, 1]) `shouldBe` "11:168.192.0.1"
79
80 it "properly bencoded (iso)" $ property $ \ ip ->
81 BE.decode (BL.toStrict (BE.encode ip)) `shouldBe` Right (ip :: IPv4)
82
83 it "fail gracefully on invalid strings" $ do
84 BE.decode "3:1.1" `shouldBe`
85 (Left "fromBEncode: unable to decode IP: 1.1" :: BE.Result IPv4)
86
87 it "fail gracefully on invalid bencode" $ do
88 BE.decode "i10e" `shouldBe`
89 (Left "fromBEncode: unable to decode IP: addr should be a bstring"
90 :: BE.Result IPv4)
91
92 describe "Peer IPv6" $ do
93 it "properly serialized" $ do
94 S.decode "\x1\x2\x3\x4\x5\x6\x7\x8\x9\xa\xb\xc\xd\xe\xf\x10"
95 `shouldBe`
96 Right ("102:304:506:708:90a:b0c:d0e:f10" :: IPv6)
97
98 S.encode ("102:304:506:708:90a:b0c:d0e:f10" :: IPv6)
99 `shouldBe`
100 "\x1\x2\x3\x4\x5\x6\x7\x8\x9\xa\xb\xc\xd\xe\xf\x10"
101
102 it "properly serialized (iso)" $ property $ \ ip ->
103 S.decode (S.encode ip) `shouldBe` Right (ip :: IPv6)
104
105 it "properly bencoded" $ do
106 BE.decode "3:::1" `shouldBe` Right (toIPv6 [0, 0, 0, 0, 0, 0, 0, 1])
107 BE.encode (toIPv6 [0, 0, 0, 0, 0, 0, 0, 1]) `shouldBe`
108 "23:00:00:00:00:00:00:00:01"
109
110 BE.decode "23:00:00:00:00:00:00:00:01"
111 `shouldBe`
112 Right (toIPv6 [0, 0, 0, 0, 0, 0, 0, 1])
113
114 it "properly bencoded iso" $ property $ \ ip ->
115 BE.decode (BL.toStrict (BE.encode ip)) `shouldBe` Right (ip :: IPv4)
116
117 it "fail gracefully on invalid strings" $ do
118 BE.decode "4:g::1" `shouldBe`
119 (Left "fromBEncode: unable to decode IP: g::1" :: BE.Result IPv6)
120
121 it "fail gracefully on invalid bencode" $ do
122 BE.decode "i10e" `shouldBe`
123 (Left "fromBEncode: unable to decode IP: addr should be a bstring"
124 :: BE.Result IPv6)
125
126
127 describe "Peer IP" $ do
128 it "properly serialized IPv6" $ do
129 S.decode "\x1\x2\x3\x4\x5\x6\x7\x8\x9\xa\xb\xc\xd\xe\xf\x10"
130 `shouldBe`
131 Right ("102:304:506:708:90a:b0c:d0e:f10" :: IP)
132
133 S.encode ("102:304:506:708:90a:b0c:d0e:f10" :: IP)
134 `shouldBe`
135 "\x1\x2\x3\x4\x5\x6\x7\x8\x9\xa\xb\xc\xd\xe\xf\x10"
136
137 it "properly serialized (iso) IPv6" $ property $ \ ip ->
138 S.decode (S.encode ip) `shouldBe` Right (ip :: IP)
139
140 it "properly serialized IPv4" $ do
141 S.decode "\x1\x2\x3\x4" `shouldBe` Right (IPv4 $ toIPv4 [1, 2, 3, 4])
142 S.encode (toIPv4 [1, 2, 3, 4]) `shouldBe` "\x1\x2\x3\x4"
143
144 it "properly serialized (iso) IPv4" $ property $ \ ip -> do
145 S.decode (S.encode ip) `shouldBe` Right (ip :: IP)
146
147 it "properly bencoded" $ do
148 BE.decode "11:168.192.0.1" `shouldBe`
149 Right (IPv4 (toIPv4 [168, 192, 0, 1]))
150
151 BE.decode "3:::1" `shouldBe` Right
152 (IPv6 (toIPv6 [0, 0, 0, 0, 0, 0, 0, 1]))
153
154 BE.encode (IPv6 (toIPv6 [0, 0, 0, 0, 0, 0, 0, 1])) `shouldBe`
155 "23:00:00:00:00:00:00:00:01"
156
157 BE.decode "23:00:00:00:00:00:00:00:01"
158 `shouldBe`
159 Right (IPv6 (toIPv6 [0, 0, 0, 0, 0, 0, 0, 1]))
160
161 it "properly bencoded iso" $ property $ \ ip ->
162 BE.decode (BL.toStrict (BE.encode ip)) `shouldBe` Right (ip :: IP)
163
164 it "fail gracefully on invalid strings" $ do
165 BE.decode "4:g::1" `shouldBe`
166 (Left "fromBEncode: unable to decode IP: g::1" :: BE.Result IP)
167
168 it "fail gracefully on invalid bencode" $ do
169 BE.decode "i10e" `shouldBe`
170 (Left "fromBEncode: unable to decode IP: addr should be a bstring"
171 :: BE.Result IP)
172
173 describe "PeerAddr" $ do
174 it "IsString" $ do
175 ("127.0.0.1:80" :: PeerAddr IP)
176 `shouldBe` PeerAddr Nothing "127.0.0.1" 80
177
178 ("127.0.0.1:80" :: PeerAddr IPv4)
179 `shouldBe` PeerAddr Nothing "127.0.0.1" 80
180
181 ("[::1]:80" :: PeerAddr IP)
182 `shouldBe` PeerAddr Nothing "::1" 80
183
184 ("[::1]:80" :: PeerAddr IPv6)
185 `shouldBe` PeerAddr Nothing "::1" 80
186
187 it "properly bencoded (iso)" $ property $ \ addr ->
188 BE.decode (BL.toStrict (BE.encode addr))
189 `shouldBe` Right (addr :: PeerAddr IP)
190
191
192 it "properly bencoded (ipv4)" $ do
193 BE.decode "d2:ip11:168.192.0.1\
194 \7:peer id20:01234567890123456789\
195 \4:porti6881e\
196 \e"
197 `shouldBe`
198 Right (PeerAddr (Just "01234567890123456789")
199 (IPv4 (toIPv4 [168, 192, 0, 1]))
200 6881)
201
202 it "properly bencoded (ipv6)" $ do
203 BE.decode "d2:ip3:::1\
204 \7:peer id20:01234567890123456789\
205 \4:porti6881e\
206 \e"
207 `shouldBe`
208 Right (PeerAddr (Just "01234567890123456789")
209 (IPv6 (toIPv6 [0, 0, 0, 0, 0, 0, 0, 1]))
210 6881)
211
212 it "peer id is optional" $ do
213 BE.decode "d2:ip11:168.192.0.1\
214 \4:porti6881e\
215 \e"
216 `shouldBe`
217 Right (PeerAddr Nothing (IPv4 (toIPv4 [168, 192, 0, 1])) 6881)
218
219 it "has sock addr for both ipv4 and ipv6" $ do
220 show (peerSockAddr "128.0.0.1:80") `shouldBe` "128.0.0.1:80"
221 show (peerSockAddr "[::1]:8080" ) `shouldBe` "[::1]:8080"
diff --git a/tests/Network/BitTorrent/Core/PeerIdSpec.hs b/tests/Network/BitTorrent/Core/PeerIdSpec.hs
deleted file mode 100644
index 4b0c2398..00000000
--- a/tests/Network/BitTorrent/Core/PeerIdSpec.hs
+++ /dev/null
@@ -1,25 +0,0 @@
1{-# OPTIONS -fno-warn-orphans #-}
2module Network.BitTorrent.Core.PeerIdSpec (spec) where
3import Control.Applicative
4import Data.BEncode as BE
5import Data.Text.Encoding as T
6import Test.Hspec
7import Test.QuickCheck
8import Test.QuickCheck.Instances ()
9import Network.BitTorrent.Core.PeerId
10
11
12instance Arbitrary PeerId where
13 arbitrary = oneof
14 [ azureusStyle defaultClientId defaultVersionNumber
15 <$> (T.encodeUtf8 <$> arbitrary)
16 , shadowStyle 'X' defaultVersionNumber
17 <$> (T.encodeUtf8 <$> arbitrary)
18 ]
19
20spec :: Spec
21spec = do
22 describe "PeerId" $ do
23 it "properly bencoded" $ do
24 BE.decode "20:01234567890123456789"
25 `shouldBe` Right ("01234567890123456789" :: PeerId) \ No newline at end of file
diff --git a/tests/Network/BitTorrent/CoreSpec.hs b/tests/Network/BitTorrent/CoreSpec.hs
index 460c52be..5bf900b2 100644
--- a/tests/Network/BitTorrent/CoreSpec.hs
+++ b/tests/Network/BitTorrent/CoreSpec.hs
@@ -1,11 +1,305 @@
1-- | Re-export modules. 1{-# LANGUAGE FlexibleInstances #-}
2{-# OPTIONS_GHC -fno-warn-orphans #-}
2module Network.BitTorrent.CoreSpec (spec) where 3module Network.BitTorrent.CoreSpec (spec) where
3import Network.BitTorrent.Core.FingerprintSpec as CoreSpec () 4import Control.Applicative
4import Network.BitTorrent.Core.NodeInfoSpec as CoreSpec () 5import Data.BEncode as BE
5import Network.BitTorrent.Core.PeerIdSpec as CoreSpec () 6import Data.ByteString.Lazy as BL
6import Network.BitTorrent.Core.PeerAddrSpec as CoreSpec () 7import Data.IP
8import Data.Serialize as S
9import Data.String
10import Data.Text.Encoding as T
11import Data.Word
12import Network
13import Test.Hspec
14import Test.QuickCheck
15import Test.QuickCheck.Instances ()
7 16
8import Test.Hspec (Spec) 17import Network.BitTorrent.Address
18
19
20instance Arbitrary IPv4 where
21 arbitrary = do
22 a <- choose (0, 255)
23 b <- choose (0, 255)
24 c <- choose (0, 255)
25 d <- choose (0, 255)
26 return $ toIPv4 [a, b, c, d]
27
28instance Arbitrary IPv6 where
29 arbitrary = do
30 a <- choose (0, fromIntegral (maxBound :: Word16))
31 b <- choose (0, fromIntegral (maxBound :: Word16))
32 c <- choose (0, fromIntegral (maxBound :: Word16))
33 d <- choose (0, fromIntegral (maxBound :: Word16))
34 e <- choose (0, fromIntegral (maxBound :: Word16))
35 f <- choose (0, fromIntegral (maxBound :: Word16))
36 g <- choose (0, fromIntegral (maxBound :: Word16))
37 h <- choose (0, fromIntegral (maxBound :: Word16))
38 return $ toIPv6 [a, b, c, d, e, f, g, h]
39
40instance Arbitrary IP where
41 arbitrary = frequency
42 [ (1, IPv4 <$> arbitrary)
43 , (1, IPv6 <$> arbitrary)
44 ]
45
46instance Arbitrary PortNumber where
47 arbitrary = fromIntegral <$> (arbitrary :: Gen Word16)
48
49instance Arbitrary PeerId where
50 arbitrary = oneof
51 [ azureusStyle defaultClientId defaultVersionNumber
52 <$> (T.encodeUtf8 <$> arbitrary)
53 , shadowStyle 'X' defaultVersionNumber
54 <$> (T.encodeUtf8 <$> arbitrary)
55 ]
56
57instance Arbitrary a => Arbitrary (PeerAddr a) where
58 arbitrary = PeerAddr <$> arbitrary <*> arbitrary <*> arbitrary
59
60instance Arbitrary NodeId where
61 arbitrary = fromString <$> vector 20
62
63instance Arbitrary a => Arbitrary (NodeAddr a) where
64 arbitrary = NodeAddr <$> arbitrary <*> arbitrary
65
66instance Arbitrary a => Arbitrary (NodeInfo a) where
67 arbitrary = NodeInfo <$> arbitrary <*> arbitrary
9 68
10spec :: Spec 69spec :: Spec
11spec = return () \ No newline at end of file 70spec = do
71 describe "PeerId" $ do
72 it "properly bencoded" $ do
73 BE.decode "20:01234567890123456789"
74 `shouldBe` Right ("01234567890123456789" :: PeerId)
75
76 describe "PortNumber" $ do
77 it "properly serialized" $ do
78 S.decode "\x1\x2" `shouldBe` Right (258 :: PortNumber)
79 S.encode (258 :: PortNumber) `shouldBe` "\x1\x2"
80
81 it "properly bencoded" $ do
82 BE.decode "i80e" `shouldBe` Right (80 :: PortNumber)
83
84 it "fail if port number is invalid" $ do
85 (BE.decode "i-10e" :: BE.Result PortNumber)
86 `shouldBe`
87 Left "fromBEncode: unable to decode PortNumber: -10"
88
89 (BE.decode "i70000e" :: BE.Result PortNumber)
90 `shouldBe`
91 Left "fromBEncode: unable to decode PortNumber: 70000"
92
93 describe "Peer IPv4" $ do
94 it "properly serialized" $ do
95 S.decode "\x1\x2\x3\x4" `shouldBe` Right (toIPv4 [1, 2, 3, 4])
96 S.encode (toIPv4 [1, 2, 3, 4]) `shouldBe` "\x1\x2\x3\x4"
97
98 it "properly serialized (iso)" $ property $ \ ip -> do
99 S.decode (S.encode ip) `shouldBe` Right (ip :: IPv4)
100
101 it "properly bencoded" $ do
102 BE.decode "11:168.192.0.1" `shouldBe` Right (toIPv4 [168, 192, 0, 1])
103 BE.encode (toIPv4 [168, 192, 0, 1]) `shouldBe` "11:168.192.0.1"
104
105 it "properly bencoded (iso)" $ property $ \ ip ->
106 BE.decode (BL.toStrict (BE.encode ip)) `shouldBe` Right (ip :: IPv4)
107
108 it "fail gracefully on invalid strings" $ do
109 BE.decode "3:1.1" `shouldBe`
110 (Left "fromBEncode: unable to decode IP: 1.1" :: BE.Result IPv4)
111
112 it "fail gracefully on invalid bencode" $ do
113 BE.decode "i10e" `shouldBe`
114 (Left "fromBEncode: unable to decode IP: addr should be a bstring"
115 :: BE.Result IPv4)
116
117 describe "Peer IPv6" $ do
118 it "properly serialized" $ do
119 S.decode "\x1\x2\x3\x4\x5\x6\x7\x8\x9\xa\xb\xc\xd\xe\xf\x10"
120 `shouldBe`
121 Right ("102:304:506:708:90a:b0c:d0e:f10" :: IPv6)
122
123 S.encode ("102:304:506:708:90a:b0c:d0e:f10" :: IPv6)
124 `shouldBe`
125 "\x1\x2\x3\x4\x5\x6\x7\x8\x9\xa\xb\xc\xd\xe\xf\x10"
126
127 it "properly serialized (iso)" $ property $ \ ip ->
128 S.decode (S.encode ip) `shouldBe` Right (ip :: IPv6)
129
130 it "properly bencoded" $ do
131 BE.decode "3:::1" `shouldBe` Right (toIPv6 [0, 0, 0, 0, 0, 0, 0, 1])
132 BE.encode (toIPv6 [0, 0, 0, 0, 0, 0, 0, 1]) `shouldBe`
133 "23:00:00:00:00:00:00:00:01"
134
135 BE.decode "23:00:00:00:00:00:00:00:01"
136 `shouldBe`
137 Right (toIPv6 [0, 0, 0, 0, 0, 0, 0, 1])
138
139 it "properly bencoded iso" $ property $ \ ip ->
140 BE.decode (BL.toStrict (BE.encode ip)) `shouldBe` Right (ip :: IPv4)
141
142 it "fail gracefully on invalid strings" $ do
143 BE.decode "4:g::1" `shouldBe`
144 (Left "fromBEncode: unable to decode IP: g::1" :: BE.Result IPv6)
145
146 it "fail gracefully on invalid bencode" $ do
147 BE.decode "i10e" `shouldBe`
148 (Left "fromBEncode: unable to decode IP: addr should be a bstring"
149 :: BE.Result IPv6)
150
151
152 describe "Peer IP" $ do
153 it "properly serialized IPv6" $ do
154 S.decode "\x1\x2\x3\x4\x5\x6\x7\x8\x9\xa\xb\xc\xd\xe\xf\x10"
155 `shouldBe`
156 Right ("102:304:506:708:90a:b0c:d0e:f10" :: IP)
157
158 S.encode ("102:304:506:708:90a:b0c:d0e:f10" :: IP)
159 `shouldBe`
160 "\x1\x2\x3\x4\x5\x6\x7\x8\x9\xa\xb\xc\xd\xe\xf\x10"
161
162 it "properly serialized (iso) IPv6" $ property $ \ ip ->
163 S.decode (S.encode ip) `shouldBe` Right (ip :: IP)
164
165 it "properly serialized IPv4" $ do
166 S.decode "\x1\x2\x3\x4" `shouldBe` Right (IPv4 $ toIPv4 [1, 2, 3, 4])
167 S.encode (toIPv4 [1, 2, 3, 4]) `shouldBe` "\x1\x2\x3\x4"
168
169 it "properly serialized (iso) IPv4" $ property $ \ ip -> do
170 S.decode (S.encode ip) `shouldBe` Right (ip :: IP)
171
172 it "properly bencoded" $ do
173 BE.decode "11:168.192.0.1" `shouldBe`
174 Right (IPv4 (toIPv4 [168, 192, 0, 1]))
175
176 BE.decode "3:::1" `shouldBe` Right
177 (IPv6 (toIPv6 [0, 0, 0, 0, 0, 0, 0, 1]))
178
179 BE.encode (IPv6 (toIPv6 [0, 0, 0, 0, 0, 0, 0, 1])) `shouldBe`
180 "23:00:00:00:00:00:00:00:01"
181
182 BE.decode "23:00:00:00:00:00:00:00:01"
183 `shouldBe`
184 Right (IPv6 (toIPv6 [0, 0, 0, 0, 0, 0, 0, 1]))
185
186 it "properly bencoded iso" $ property $ \ ip ->
187 BE.decode (BL.toStrict (BE.encode ip)) `shouldBe` Right (ip :: IP)
188
189 it "fail gracefully on invalid strings" $ do
190 BE.decode "4:g::1" `shouldBe`
191 (Left "fromBEncode: unable to decode IP: g::1" :: BE.Result IP)
192
193 it "fail gracefully on invalid bencode" $ do
194 BE.decode "i10e" `shouldBe`
195 (Left "fromBEncode: unable to decode IP: addr should be a bstring"
196 :: BE.Result IP)
197
198 describe "PeerAddr" $ do
199 it "IsString" $ do
200 ("127.0.0.1:80" :: PeerAddr IP)
201 `shouldBe` PeerAddr Nothing "127.0.0.1" 80
202
203 ("127.0.0.1:80" :: PeerAddr IPv4)
204 `shouldBe` PeerAddr Nothing "127.0.0.1" 80
205
206 ("[::1]:80" :: PeerAddr IP)
207 `shouldBe` PeerAddr Nothing "::1" 80
208
209 ("[::1]:80" :: PeerAddr IPv6)
210 `shouldBe` PeerAddr Nothing "::1" 80
211
212 it "properly bencoded (iso)" $ property $ \ addr ->
213 BE.decode (BL.toStrict (BE.encode addr))
214 `shouldBe` Right (addr :: PeerAddr IP)
215
216
217 it "properly bencoded (ipv4)" $ do
218 BE.decode "d2:ip11:168.192.0.1\
219 \7:peer id20:01234567890123456789\
220 \4:porti6881e\
221 \e"
222 `shouldBe`
223 Right (PeerAddr (Just "01234567890123456789")
224 (IPv4 (toIPv4 [168, 192, 0, 1]))
225 6881)
226
227 it "properly bencoded (ipv6)" $ do
228 BE.decode "d2:ip3:::1\
229 \7:peer id20:01234567890123456789\
230 \4:porti6881e\
231 \e"
232 `shouldBe`
233 Right (PeerAddr (Just "01234567890123456789")
234 (IPv6 (toIPv6 [0, 0, 0, 0, 0, 0, 0, 1]))
235 6881)
236
237 it "peer id is optional" $ do
238 BE.decode "d2:ip11:168.192.0.1\
239 \4:porti6881e\
240 \e"
241 `shouldBe`
242 Right (PeerAddr Nothing (IPv4 (toIPv4 [168, 192, 0, 1])) 6881)
243
244 it "has sock addr for both ipv4 and ipv6" $ do
245 show (peerSockAddr "128.0.0.1:80") `shouldBe` "128.0.0.1:80"
246 show (peerSockAddr "[::1]:8080" ) `shouldBe` "[::1]:8080"
247
248 describe "NodeId" $ do
249 it "properly serialized" $ do
250 S.decode "mnopqrstuvwxyz123456"
251 `shouldBe` Right ("mnopqrstuvwxyz123456" :: NodeId)
252
253 S.encode ("mnopqrstuvwxyz123456" :: NodeId)
254 `shouldBe` "mnopqrstuvwxyz123456"
255
256 it "properly serialized (iso)" $ property $ \ nid ->
257 S.decode (S.encode nid) `shouldBe`
258 Right (nid :: NodeId)
259
260 describe "NodeAddr" $ do
261 it "properly serialized" $ do
262 S.decode "\127\0\0\1\1\2" `shouldBe`
263 Right ("127.0.0.1:258" :: NodeAddr IPv4)
264
265 it "properly serialized (iso)" $ property $ \ nid ->
266 S.decode (S.encode nid) `shouldBe`
267 Right (nid :: NodeAddr IPv4)
268
269 describe "NodeInfo" $ do
270 it "properly serialized" $ do
271 S.decode "mnopqrstuvwxyz123456\
272 \\127\0\0\1\1\2" `shouldBe` Right
273 (NodeInfo "mnopqrstuvwxyz123456" "127.0.0.1:258" :: NodeInfo IPv4)
274
275 it "properly serialized (iso)" $ property $ \ nid ->
276 S.decode (S.encode nid) `shouldBe`
277 Right (nid :: NodeInfo IPv4)
278
279 -- see <http://bittorrent.org/beps/bep_0020.html>
280 describe "Fingerprint" $ do
281 it "decode mainline encoded peer id" $ do
282 fingerprint "M4-3-6--xxxxxxxxxxxx" `shouldBe` "Mainline-4.3.6"
283 fingerprint "M4-20-8-xxxxxxxxxxxx" `shouldBe` "Mainline-4.20.8"
284
285 it "decode azureus encoded peer id" $ do
286 fingerprint "-AZ2060-xxxxxxxxxxxx" `shouldBe` "Azureus-2060"
287 fingerprint "-BS0000-xxxxxxxxxxxx" `shouldBe` "BTSlave-0"
288
289 it "decode Shad0w style peer id" $ do
290 fingerprint "S58B-----xxxxxxxxxxx" `shouldBe` "Shadow-5.8.11"
291 fingerprint "T58B-----xxxxxxxxxxx" `shouldBe` "BitTornado-5.8.11"
292
293 it "decode bitcomet style peer id" $ do
294 fingerprint "exbc01xxxxxxxxxxxxxx" `shouldBe` "BitComet-48.49"
295 fingerprint "FUTB01xxxxxxxxxxxxxx" `shouldBe` "BitComet-48.49"
296 fingerprint "exbc01LORDxxxxxxxxxx" `shouldBe` "BitLord-48.49"
297
298 it "decode opera style peer id" $ do
299 fingerprint "OP0123xxxxxxxxxxxxxx" `shouldBe` "Opera-123"
300
301 it "decode ML donkey style peer id" $ do
302 fingerprint "-ML2.7.2-xxxxxxxxxxx" `shouldBe` "MLdonkey-0"
303
304-- TODO XBT, Bits on Wheels, Queen Bee, BitTyrant, TorrenTopia,
305-- BitSpirit, Rufus, G3 Torrent, FlashGet \ No newline at end of file
diff --git a/tests/Network/BitTorrent/DHT/MessageSpec.hs b/tests/Network/BitTorrent/DHT/MessageSpec.hs
index 4ec875dd..ab6e1ea5 100644
--- a/tests/Network/BitTorrent/DHT/MessageSpec.hs
+++ b/tests/Network/BitTorrent/DHT/MessageSpec.hs
@@ -8,7 +8,7 @@ import Data.ByteString.Lazy as BL
8import Data.Default 8import Data.Default
9import Data.List as L 9import Data.List as L
10import Data.Maybe 10import Data.Maybe
11import Network.BitTorrent.Core 11import Network.BitTorrent.Address
12import Network.BitTorrent.DHT.Message 12import Network.BitTorrent.DHT.Message
13import qualified Network.KRPC as KRPC (def) 13import qualified Network.KRPC as KRPC (def)
14import Network.KRPC hiding (def) 14import Network.KRPC hiding (def)
@@ -17,9 +17,9 @@ import Test.Hspec
17import Test.QuickCheck 17import Test.QuickCheck
18import System.Timeout 18import System.Timeout
19 19
20import Data.TorrentSpec ()
20import Network.BitTorrent.CoreSpec () 21import Network.BitTorrent.CoreSpec ()
21import Network.BitTorrent.DHT.TokenSpec () 22import Network.BitTorrent.DHT.TokenSpec ()
22import Data.Torrent.InfoHashSpec ()
23 23
24 24
25instance MonadLogger IO where 25instance MonadLogger IO where
diff --git a/tests/Network/BitTorrent/DHT/QuerySpec.hs b/tests/Network/BitTorrent/DHT/QuerySpec.hs
index d25bd120..81c3b45b 100644
--- a/tests/Network/BitTorrent/DHT/QuerySpec.hs
+++ b/tests/Network/BitTorrent/DHT/QuerySpec.hs
@@ -9,7 +9,7 @@ import Data.Default
9import Data.List as L 9import Data.List as L
10import Test.Hspec 10import Test.Hspec
11 11
12import Network.BitTorrent.Core 12import Network.BitTorrent.Address
13import Network.BitTorrent.DHT 13import Network.BitTorrent.DHT
14import Network.BitTorrent.DHT.Session 14import Network.BitTorrent.DHT.Session
15import Network.BitTorrent.DHT.Query 15import Network.BitTorrent.DHT.Query
diff --git a/tests/Network/BitTorrent/DHT/RoutingSpec.hs b/tests/Network/BitTorrent/DHT/RoutingSpec.hs
index c4a33357..aeccff5f 100644
--- a/tests/Network/BitTorrent/DHT/RoutingSpec.hs
+++ b/tests/Network/BitTorrent/DHT/RoutingSpec.hs
@@ -8,7 +8,7 @@ import Data.Maybe
8import Test.Hspec 8import Test.Hspec
9import Test.QuickCheck 9import Test.QuickCheck
10 10
11import Network.BitTorrent.Core 11import Network.BitTorrent.Address
12import Network.BitTorrent.DHT.Routing as T 12import Network.BitTorrent.DHT.Routing as T
13 13
14import Network.BitTorrent.CoreSpec hiding (spec) 14import Network.BitTorrent.CoreSpec hiding (spec)
diff --git a/tests/Network/BitTorrent/DHT/SessionSpec.hs b/tests/Network/BitTorrent/DHT/SessionSpec.hs
index bb32cf0e..a5376c32 100644
--- a/tests/Network/BitTorrent/DHT/SessionSpec.hs
+++ b/tests/Network/BitTorrent/DHT/SessionSpec.hs
@@ -5,17 +5,18 @@ import Control.Concurrent
5import Control.Exception 5import Control.Exception
6import Control.Monad.Reader 6import Control.Monad.Reader
7import Control.Monad.Trans.Resource 7import Control.Monad.Trans.Resource
8import Data.Conduit.Lazy
8import Data.Default 9import Data.Default
9import Data.List as L 10import Data.List as L
10import Test.Hspec 11import Test.Hspec
11import Test.QuickCheck 12import Test.QuickCheck
12 13
13import Network.BitTorrent.Core 14import Network.BitTorrent.Address
14import Network.BitTorrent.DHT 15import Network.BitTorrent.DHT
15import Network.BitTorrent.DHT.Message 16import Network.BitTorrent.DHT.Message
16import Network.BitTorrent.DHT.Session 17import Network.BitTorrent.DHT.Session
17 18
18import Data.Torrent.InfoHashSpec () 19import Data.TorrentSpec ()
19import Network.BitTorrent.CoreSpec () 20import Network.BitTorrent.CoreSpec ()
20import Network.BitTorrent.DHT.TokenSpec () 21import Network.BitTorrent.DHT.TokenSpec ()
21 22
diff --git a/tests/Network/BitTorrent/DHT/TestData.hs b/tests/Network/BitTorrent/DHT/TestData.hs
index 2e000a77..e9473cbb 100644
--- a/tests/Network/BitTorrent/DHT/TestData.hs
+++ b/tests/Network/BitTorrent/DHT/TestData.hs
@@ -3,7 +3,7 @@ module Network.BitTorrent.DHT.TestData
3 , testTorrents 3 , testTorrents
4 ) where 4 ) where
5 5
6import Data.Torrent.InfoHash 6import Data.Torrent
7 7
8data TestEntry = TestEntry 8data TestEntry = TestEntry
9 { entryName :: String 9 { entryName :: String
diff --git a/tests/Network/BitTorrent/DHT/TokenSpec.hs b/tests/Network/BitTorrent/DHT/TokenSpec.hs
index 6353a24c..a45d2212 100644
--- a/tests/Network/BitTorrent/DHT/TokenSpec.hs
+++ b/tests/Network/BitTorrent/DHT/TokenSpec.hs
@@ -7,7 +7,7 @@ import Data.String
7import Test.Hspec 7import Test.Hspec
8import Test.QuickCheck 8import Test.QuickCheck
9 9
10import Network.BitTorrent.Core 10import Network.BitTorrent.Address
11import Network.BitTorrent.CoreSpec () 11import Network.BitTorrent.CoreSpec ()
12import Network.BitTorrent.DHT.Token as T 12import Network.BitTorrent.DHT.Token as T
13 13
diff --git a/tests/Network/BitTorrent/DHTSpec.hs b/tests/Network/BitTorrent/DHTSpec.hs
index 76b48257..30abc867 100644
--- a/tests/Network/BitTorrent/DHTSpec.hs
+++ b/tests/Network/BitTorrent/DHTSpec.hs
@@ -6,7 +6,7 @@ import Data.List as L
6import Test.Hspec 6import Test.Hspec
7import System.Timeout 7import System.Timeout
8 8
9import Data.Torrent.InfoHash 9import Data.Torrent
10import Network.BitTorrent.DHT 10import Network.BitTorrent.DHT
11 11
12 12
diff --git a/tests/Data/Torrent/BitfieldSpec.hs b/tests/Network/BitTorrent/Exchange/BitfieldSpec.hs
index 093f6f19..234965fa 100644
--- a/tests/Data/Torrent/BitfieldSpec.hs
+++ b/tests/Network/BitTorrent/Exchange/BitfieldSpec.hs
@@ -1,10 +1,10 @@
1{-# OPTIONS -fno-warn-orphans #-} 1{-# OPTIONS -fno-warn-orphans #-}
2module Data.Torrent.BitfieldSpec (spec) where 2module Network.BitTorrent.Exchange.BitfieldSpec (spec) where
3import Control.Applicative 3import Control.Applicative
4import Test.Hspec 4import Test.Hspec
5import Test.QuickCheck 5import Test.QuickCheck
6 6
7import Data.Torrent.Bitfield 7import Network.BitTorrent.Exchange.Bitfield
8 8
9instance Arbitrary Bitfield where 9instance Arbitrary Bitfield where
10 arbitrary = fromBitmap <$> arbitrary 10 arbitrary = fromBitmap <$> arbitrary
diff --git a/tests/Network/BitTorrent/Exchange/ConnectionSpec.hs b/tests/Network/BitTorrent/Exchange/ConnectionSpec.hs
index c21f55ef..d654cda1 100644
--- a/tests/Network/BitTorrent/Exchange/ConnectionSpec.hs
+++ b/tests/Network/BitTorrent/Exchange/ConnectionSpec.hs
@@ -8,8 +8,7 @@ import Test.Hspec
8import Test.QuickCheck 8import Test.QuickCheck
9 9
10import Data.Torrent 10import Data.Torrent
11import Data.Torrent.InfoHash 11import Network.BitTorrent.Address
12import Network.BitTorrent.Core
13import Network.BitTorrent.Exchange.Connection 12import Network.BitTorrent.Exchange.Connection
14import Network.BitTorrent.Exchange.Message 13import Network.BitTorrent.Exchange.Message
15 14
diff --git a/tests/Network/BitTorrent/Exchange/Session/MetadataSpec.hs b/tests/Network/BitTorrent/Exchange/DownloadSpec.hs
index 975ceb5b..d46f2034 100644
--- a/tests/Network/BitTorrent/Exchange/Session/MetadataSpec.hs
+++ b/tests/Network/BitTorrent/Exchange/DownloadSpec.hs
@@ -1,5 +1,5 @@
1{-# LANGUAGE RecordWildCards #-} 1{-# LANGUAGE RecordWildCards #-}
2module Network.BitTorrent.Exchange.Session.MetadataSpec (spec) where 2module Network.BitTorrent.Exchange.DownloadSpec (spec) where
3import Control.Concurrent 3import Control.Concurrent
4import Data.ByteString as BS 4import Data.ByteString as BS
5import Data.ByteString.Lazy as BL 5import Data.ByteString.Lazy as BL
@@ -7,11 +7,10 @@ import Test.Hspec
7import Test.QuickCheck 7import Test.QuickCheck
8 8
9import Data.BEncode as BE 9import Data.BEncode as BE
10import Data.Torrent 10import Data.Torrent as Torrent
11import Data.Torrent.Piece as P 11import Network.BitTorrent.Address
12import Network.BitTorrent.Core 12import Network.BitTorrent.Exchange.Download
13import Network.BitTorrent.Exchange.Message 13import Network.BitTorrent.Exchange.Message
14import Network.BitTorrent.Exchange.Session.Metadata
15 14
16import Config 15import Config
17import Network.BitTorrent.CoreSpec () 16import Network.BitTorrent.CoreSpec ()
@@ -25,27 +24,16 @@ chunkBy s bs
25 | BS.null bs = [] 24 | BS.null bs = []
26 | otherwise = BS.take s bs : chunkBy s (BS.drop s bs) 25 | otherwise = BS.take s bs : chunkBy s (BS.drop s bs)
27 26
28withUpdates :: Updates a -> IO a 27withUpdates :: Updates s a -> IO a
29withUpdates m = do 28withUpdates m = do
30 Torrent {..} <- getTestTorrent 29 Torrent {..} <- getTestTorrent
31 let infoDictLen = fromIntegral $ BL.length $ BE.encode tInfoDict 30 let infoDictLen = fromIntegral $ BL.length $ BE.encode tInfoDict
32 mvar <- newMVar (nullStatus infoDictLen) 31 --mvar <- newMVar (nullStatus infoDictLen)
33 runUpdates mvar placeholderAddr m 32 --runUpdates mvar placeholderAddr m
33 undefined
34 34
35simulateFetch :: InfoDict -> Updates (Maybe InfoDict) 35simulateFetch :: InfoDict -> Updates s (Maybe InfoDict)
36simulateFetch dict = go 36simulateFetch dict = undefined
37 where
38 blocks = chunkBy metadataPieceSize (BL.toStrict (BE.encode dict))
39 packPiece ix = P.Piece ix (blocks !! ix)
40 ih = idInfoHash dict
41
42 go = do
43 mix <- scheduleBlock
44 case mix of
45 Nothing -> return Nothing
46 Just ix -> do
47 mdict <- pushBlock (packPiece ix) ih
48 maybe go (return . Just) mdict
49 37
50spec :: Spec 38spec :: Spec
51spec = do 39spec = do
diff --git a/tests/Network/BitTorrent/Exchange/MessageSpec.hs b/tests/Network/BitTorrent/Exchange/MessageSpec.hs
index 63d814ff..d615b1ff 100644
--- a/tests/Network/BitTorrent/Exchange/MessageSpec.hs
+++ b/tests/Network/BitTorrent/Exchange/MessageSpec.hs
@@ -10,10 +10,10 @@ import Data.String
10import Test.Hspec 10import Test.Hspec
11import Test.QuickCheck 11import Test.QuickCheck
12 12
13import Data.Torrent.BitfieldSpec () 13import Data.TorrentSpec ()
14import Data.Torrent.InfoHashSpec () 14import Network.BitTorrent.Exchange.BitfieldSpec ()
15import Network.BitTorrent.CoreSpec () 15import Network.BitTorrent.CoreSpec ()
16import Network.BitTorrent.Core () 16import Network.BitTorrent.Address ()
17import Network.BitTorrent.Exchange.BlockSpec () 17import Network.BitTorrent.Exchange.BlockSpec ()
18import Network.BitTorrent.Exchange.Message 18import Network.BitTorrent.Exchange.Message
19 19
diff --git a/tests/Network/BitTorrent/Exchange/SessionSpec.hs b/tests/Network/BitTorrent/Exchange/SessionSpec.hs
index c2c76644..bf5b95a1 100644
--- a/tests/Network/BitTorrent/Exchange/SessionSpec.hs
+++ b/tests/Network/BitTorrent/Exchange/SessionSpec.hs
@@ -3,7 +3,7 @@ module Network.BitTorrent.Exchange.SessionSpec (spec) where
3import Test.Hspec 3import Test.Hspec
4 4
5import Data.Torrent 5import Data.Torrent
6import Network.BitTorrent.Core 6import Network.BitTorrent.Address
7import Network.BitTorrent.Exchange.Session 7import Network.BitTorrent.Exchange.Session
8 8
9import Config 9import Config
diff --git a/tests/Data/Torrent/ProgressSpec.hs b/tests/Network/BitTorrent/Internal/ProgressSpec.hs
index 32efbd7a..acbfd84c 100644
--- a/tests/Data/Torrent/ProgressSpec.hs
+++ b/tests/Network/BitTorrent/Internal/ProgressSpec.hs
@@ -1,9 +1,9 @@
1{-# OPTIONS -fno-warn-orphans #-} 1{-# OPTIONS -fno-warn-orphans #-}
2module Data.Torrent.ProgressSpec (spec) where 2module Network.BitTorrent.Internal.ProgressSpec (spec) where
3import Control.Applicative 3import Control.Applicative
4import Test.Hspec 4import Test.Hspec
5import Test.QuickCheck 5import Test.QuickCheck
6import Data.Torrent.Progress 6import Network.BitTorrent.Internal.Progress
7 7
8 8
9instance Arbitrary Progress where 9instance Arbitrary Progress where
diff --git a/tests/Network/BitTorrent/Tracker/MessageSpec.hs b/tests/Network/BitTorrent/Tracker/MessageSpec.hs
index c56afd2a..29854d58 100644
--- a/tests/Network/BitTorrent/Tracker/MessageSpec.hs
+++ b/tests/Network/BitTorrent/Tracker/MessageSpec.hs
@@ -16,13 +16,13 @@ import Data.Maybe
16import Test.Hspec 16import Test.Hspec
17import Test.QuickCheck 17import Test.QuickCheck
18 18
19import Data.Torrent.InfoHashSpec () 19import Data.TorrentSpec ()
20import Data.Torrent.ProgressSpec () 20import Network.BitTorrent.Internal.ProgressSpec ()
21import Network.BitTorrent.Core.PeerIdSpec () 21import Network.BitTorrent.Address ()
22import Network.BitTorrent.Core.PeerAddrSpec () 22import Network.BitTorrent.Address ()
23 23
24import Network.BitTorrent.Tracker.Message as Message 24import Network.BitTorrent.Tracker.Message as Message
25import Network.BitTorrent.Core 25import Network.BitTorrent.Address
26 26
27 27
28--prop_bencode :: Eq a => BEncode a => a -> Bool 28--prop_bencode :: Eq a => BEncode a => a -> Bool
diff --git a/tests/Network/BitTorrent/Tracker/RPC/HTTPSpec.hs b/tests/Network/BitTorrent/Tracker/RPC/HTTPSpec.hs
index 65f58911..e928f917 100644
--- a/tests/Network/BitTorrent/Tracker/RPC/HTTPSpec.hs
+++ b/tests/Network/BitTorrent/Tracker/RPC/HTTPSpec.hs
@@ -5,7 +5,7 @@ import Data.Default
5import Data.List as L 5import Data.List as L
6import Test.Hspec 6import Test.Hspec
7 7
8import Data.Torrent.Progress 8import Network.BitTorrent.Internal.Progress
9import Network.BitTorrent.Tracker.Message as Message 9import Network.BitTorrent.Tracker.Message as Message
10import Network.BitTorrent.Tracker.RPC.HTTP 10import Network.BitTorrent.Tracker.RPC.HTTP
11 11
diff --git a/tests/Network/BitTorrent/Tracker/RPC/UDPSpec.hs b/tests/Network/BitTorrent/Tracker/RPC/UDPSpec.hs
index 8a1ffc01..1ec3bdb7 100644
--- a/tests/Network/BitTorrent/Tracker/RPC/UDPSpec.hs
+++ b/tests/Network/BitTorrent/Tracker/RPC/UDPSpec.hs
@@ -9,7 +9,7 @@ import Data.List as L
9import Data.Maybe 9import Data.Maybe
10import Test.Hspec 10import Test.Hspec
11 11
12import Network.BitTorrent.Core 12import Network.BitTorrent.Address
13import Network.BitTorrent.Tracker.Message as Message 13import Network.BitTorrent.Tracker.Message as Message
14 14
15import Network.BitTorrent.Tracker.TestData 15import Network.BitTorrent.Tracker.TestData
diff --git a/tests/Network/BitTorrent/Tracker/TestData.hs b/tests/Network/BitTorrent/Tracker/TestData.hs
index e0edba25..b95e2df4 100644
--- a/tests/Network/BitTorrent/Tracker/TestData.hs
+++ b/tests/Network/BitTorrent/Tracker/TestData.hs
@@ -12,7 +12,7 @@ import Data.Maybe
12import Data.String 12import Data.String
13import Network.URI 13import Network.URI
14 14
15import Data.Torrent.InfoHash 15import Data.Torrent
16 16
17 17
18data TrackerEntry = TrackerEntry 18data TrackerEntry = TrackerEntry
diff --git a/tests/System/Torrent/FileMapSpec.hs b/tests/System/Torrent/FileMapSpec.hs
index 36632b3e..85180c0a 100644
--- a/tests/System/Torrent/FileMapSpec.hs
+++ b/tests/System/Torrent/FileMapSpec.hs
@@ -9,7 +9,7 @@ import System.FilePath
9import System.IO.Unsafe 9import System.IO.Unsafe
10import Test.Hspec 10import Test.Hspec
11 11
12import Data.Torrent.Layout 12import Data.Torrent
13import System.Torrent.FileMap as FM 13import System.Torrent.FileMap as FM
14 14
15 15
diff --git a/tests/System/Torrent/StorageSpec.hs b/tests/System/Torrent/StorageSpec.hs
index 30322545..b5e49078 100644
--- a/tests/System/Torrent/StorageSpec.hs
+++ b/tests/System/Torrent/StorageSpec.hs
@@ -7,9 +7,8 @@ import System.Directory
7import System.IO.Unsafe 7import System.IO.Unsafe
8import Test.Hspec 8import Test.Hspec
9 9
10import Data.Torrent.Bitfield as BF 10import Data.Torrent
11import Data.Torrent.Layout 11import Network.BitTorrent.Exchange.Bitfield as BF
12import Data.Torrent.Piece
13import System.Torrent.Storage 12import System.Torrent.Storage
14 13
15 14