summaryrefslogtreecommitdiff
path: root/tests/Main.hs
diff options
context:
space:
mode:
authorSam T <pxqr.sta@gmail.com>2013-08-16 08:50:08 +0400
committerSam T <pxqr.sta@gmail.com>2013-08-16 08:50:08 +0400
commit6bb92a610c4874ea3fa37fb15cd55c48f219d6ed (patch)
treee9362f06242d11a55cade4d8705155c6d388a85e /tests/Main.hs
parent1c19636c20e918388ef7f16faa8c6fb617d917d8 (diff)
~ Remove torrent-content modules.
Diffstat (limited to 'tests/Main.hs')
-rw-r--r--tests/Main.hs142
1 files changed, 5 insertions, 137 deletions
diff --git a/tests/Main.hs b/tests/Main.hs
index 1a758cb2..a968c487 100644
--- a/tests/Main.hs
+++ b/tests/Main.hs
@@ -18,7 +18,6 @@ import Data.ByteString (ByteString)
18import qualified Data.ByteString as B 18import qualified Data.ByteString as B
19import qualified Data.ByteString.Lazy as Lazy 19import qualified Data.ByteString.Lazy as Lazy
20import Data.List as L 20import Data.List as L
21import Data.Ord
22import Data.Maybe 21import Data.Maybe
23import Data.Word 22import Data.Word
24import Data.Serialize as S 23import Data.Serialize as S
@@ -27,27 +26,20 @@ import Data.Text as T
27import Network 26import Network
28import Network.URI 27import Network.URI
29 28
30import System.Directory
31import System.FilePath
32
33import Test.QuickCheck as QC 29import Test.QuickCheck as QC
34import Test.HUnit as HU
35import Test.Framework as Framework (Test, defaultMain) 30import Test.Framework as Framework (Test, defaultMain)
36import Test.Framework.Providers.QuickCheck2 (testProperty) 31import Test.Framework.Providers.QuickCheck2 (testProperty)
37import Test.Framework.Providers.HUnit (testCase)
38 32
39import Data.Aeson as JSON 33import Data.Aeson as JSON
40import Data.BEncode as BE 34import Data.BEncode as BE
41import Data.Bitfield as BF 35import Data.Torrent.Block
42import Data.Torrent 36import Data.Torrent.Bitfield as BF
43import Network.BitTorrent as BT 37import Data.Torrent.Metainfo
44import Network.BitTorrent.Exchange.Protocol 38import Network.BitTorrent.Exchange.Protocol
45import Network.BitTorrent.Tracker 39import Network.BitTorrent.Tracker
46import Network.BitTorrent.Tracker.Protocol 40import Network.BitTorrent.Tracker.Protocol
47import Network.BitTorrent.Tracker.HTTP 41import Network.BitTorrent.Tracker.HTTP
48import Network.BitTorrent.Peer 42import Network.BitTorrent.Peer
49import System.IO.MMap.Fixed hiding (empty, interval)
50import qualified System.IO.MMap.Fixed as Fixed
51 43
52-- import Debug.Trace 44-- import Debug.Trace
53 45
@@ -73,71 +65,6 @@ instance Arbitrary Text where
73 arbitrary = T.pack <$> arbitrary 65 arbitrary = T.pack <$> arbitrary
74 66
75{----------------------------------------------------------------------- 67{-----------------------------------------------------------------------
76 Bitfield
77-----------------------------------------------------------------------}
78-- other properties are tested in IntervalSet
79
80prop_completenessRange :: Bitfield -> Bool
81prop_completenessRange bf = 0 <= c && c <= 1
82 where
83 c = completeness bf
84
85prop_minMax :: Bitfield -> Bool
86prop_minMax bf
87 | BF.null bf = True
88 | otherwise = BF.findMin bf <= BF.findMax bf
89
90prop_rarestInRange :: [Bitfield] -> Bool
91prop_rarestInRange xs = case rarest xs of
92 Just r -> 0 <= r
93 && r < totalCount (maximumBy (comparing totalCount) xs)
94 Nothing -> True
95
96{- this one should give pretty good coverage -}
97prop_differenceDeMorgan :: Bitfield -> Bitfield -> Bitfield -> Bool
98prop_differenceDeMorgan a b c =
99 (a `BF.difference` (b `BF.intersection` c))
100 == ((a `BF.difference` b) `BF.union` (a `BF.difference` c))
101 &&
102 (a `BF.difference` (b `BF.union` c))
103 == ((a `BF.difference` b) `BF.intersection` (a `BF.difference` c))
104
105
106{-----------------------------------------------------------------------
107 Torrent
108-----------------------------------------------------------------------}
109
110-- TODO tests for torrent: encoding <-> decoding
111instance Arbitrary FileInfo where
112 arbitrary = FileInfo <$> arbitrary <*> arbitrary <*> arbitrary
113
114instance Arbitrary ContentInfo where
115 arbitrary = oneof
116 [ SingleFile <$> arbitrary <*> arbitrary <*> arbitrary
117 <*> arbitrary <*> arbitrary <*> arbitrary
118 , MultiFile <$> arbitrary <*> arbitrary <*> arbitrary
119 <*> arbitrary <*> arbitrary
120 ]
121
122instance Arbitrary Torrent where
123 arbitrary = mktorrent <$> arbitrary
124 <*> arbitrary <*> arbitrary <*> arbitrary
125 <*> arbitrary <*> arbitrary <*> arbitrary
126 <*> arbitrary <*> pure Nothing <*> arbitrary
127
128-- TODO add a few more torrents here
129torrentList :: [(FilePath, String)]
130torrentList =
131 [ ( "res" </> "dapper-dvd-amd64.iso.torrent"
132 , "0221caf96aa3cb94f0f58d458e78b0fc344ad8bf")
133 ]
134
135checkInfoHash :: (FilePath, String) -> Assertion
136checkInfoHash (path, expectedHash) = check =<< fromFile path
137 where
138 check t = expectedHash @=? show (ppInfoHash (tInfoHash t))
139
140{-----------------------------------------------------------------------
141 Handshake 68 Handshake
142-----------------------------------------------------------------------} 69-----------------------------------------------------------------------}
143 70
@@ -211,10 +138,6 @@ instance Arbitrary BlockIx where
211instance Arbitrary Block where 138instance Arbitrary Block where
212 arbitrary = Block <$> positive <*> positive <*> arbitrary 139 arbitrary = Block <$> positive <*> positive <*> arbitrary
213 140
214instance Arbitrary Bitfield where
215 arbitrary = mkBitfield <$> (succ . min 1000 <$> positive)
216 <*> arbitrary
217
218instance Arbitrary PortNumber where 141instance Arbitrary PortNumber where
219 arbitrary = fromIntegral <$> (arbitrary :: Gen Word16) 142 arbitrary = fromIntegral <$> (arbitrary :: Gen Word16)
220 143
@@ -226,7 +149,7 @@ instance Arbitrary Message where
226 , pure Interested 149 , pure Interested
227 , pure NotInterested 150 , pure NotInterested
228 , Have <$> positive 151 , Have <$> positive
229 , Bitfield <$> arbitrary 152 , pure $ Bitfield $ BF.haveNone 0
230 , Request <$> arbitrary 153 , Request <$> arbitrary
231 , Piece <$> arbitrary 154 , Piece <$> arbitrary
232 , Cancel <$> arbitrary 155 , Cancel <$> arbitrary
@@ -243,68 +166,15 @@ prop_messageEncoding msg
243 = S.decode (S.encode msg) == Right msg 166 = S.decode (S.encode msg) == Right msg
244 167
245{----------------------------------------------------------------------- 168{-----------------------------------------------------------------------
246 MemMap
247-----------------------------------------------------------------------}
248
249tmpdir :: FilePath
250tmpdir = "tmp"
251
252boundaryTest :: Assertion
253boundaryTest = do
254 f <- mallocTo (Fixed.interval 0 1) Fixed.empty
255 f <- mallocTo (Fixed.interval 1 2) f
256 writeElem f 0 (1 :: Word8)
257 writeElem f 1 (2 :: Word8)
258 bs <- readBytes (Fixed.interval 0 2) f
259 "\x1\x2" @=? bs
260
261mmapSingle :: Assertion
262mmapSingle = do
263 f <- mmapTo (tmpdir </> "single.test") (10, 5) 5 Fixed.empty
264 writeBytes (Fixed.interval 5 5) "abcde" f
265 bs <- readBytes (Fixed.interval 5 5) f
266 "abcde" @=? bs
267
268coalesceTest :: Assertion
269coalesceTest = do
270 f <- mmapTo (tmpdir </> "a.test") (0, 1) 10 Fixed.empty
271 f <- mmapTo (tmpdir </> "bc.test") (0, 2) 12 f
272 f <- mmapTo (tmpdir </> "c.test") (0, 1) 13 f
273 writeBytes (Fixed.interval 10 4) "abcd" f
274 bs <- readBytes (Fixed.interval 10 4) f
275 "abcd" @=? bs
276
277{-----------------------------------------------------------------------
278 Main 169 Main
279-----------------------------------------------------------------------} 170-----------------------------------------------------------------------}
280 171
281allTests :: [Framework.Test] 172allTests :: [Framework.Test]
282allTests = 173allTests =
283 [ -- bitfield module
284 testProperty "completeness range" prop_completenessRange
285 , testProperty "rarest in range" prop_rarestInRange
286 , testProperty "min less that max" prop_minMax
287 , testProperty "difference de morgan" prop_differenceDeMorgan
288
289 -- torrent module
290 , testProperty "file info encoding" $
291 prop_properBEncode (T :: T FileInfo)
292 , testProperty "content info encoding" $
293 prop_properBEncode (T :: T ContentInfo)
294 , testProperty "torrent encoding" $
295 prop_properBEncode (T :: T Torrent)
296 ] ++
297 fmap (testCase "info hash" . checkInfoHash) torrentList
298 ++
299 [ -- handshake module 174 [ -- handshake module
300 testProperty "handshake encoding" $ 175 testProperty "handshake encoding" $
301 prop_cerealEncoding (T :: T Handshake) 176 prop_cerealEncoding (T :: T Handshake)
302 , testProperty "message encoding" prop_messageEncoding 177 , testProperty "message encoding" prop_messageEncoding
303
304 -- mem map
305 , testCase "boudary" boundaryTest
306 , testCase "single" mmapSingle
307 , testCase "coalesce" coalesceTest
308 ] ++ test_scrape_url 178 ] ++ test_scrape_url
309 ++ 179 ++
310 [ testProperty "scrape bencode" $ 180 [ testProperty "scrape bencode" $
@@ -312,6 +182,4 @@ allTests =
312 ] 182 ]
313 183
314main :: IO () 184main :: IO ()
315main = do 185main = defaultMain allTests
316 createDirectoryIfMissing True tmpdir
317 defaultMain allTests