summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--bittorrent.cabal21
-rw-r--r--tests/InfoHash.hs29
-rw-r--r--tests/Main.hs40
3 files changed, 33 insertions, 57 deletions
diff --git a/bittorrent.cabal b/bittorrent.cabal
index 8ad50eca..c9eaaa47 100644
--- a/bittorrent.cabal
+++ b/bittorrent.cabal
@@ -29,10 +29,12 @@ source-repository head
29 type: git 29 type: git
30 location: git://github.com/pxqr/bittorrent.git 30 location: git://github.com/pxqr/bittorrent.git
31 31
32
32flag testing 33flag testing
33 description: Expose debug stuff in export declarations. 34 description: Expose debug stuff in export declarations.
34 default: False 35 default: False
35 36
37
36library 38library
37 exposed-modules: Data.Torrent 39 exposed-modules: Data.Torrent
38 , Data.Bitfield 40 , Data.Bitfield
@@ -138,24 +140,6 @@ executable example
138 140
139 141
140 142
141test-suite info-hash
142 type: exitcode-stdio-1.0
143 main-is: InfoHash.hs
144 hs-source-dirs: tests
145
146 build-depends: base
147 , bytestring
148 , containers
149 , bencoding
150 , bittorrent
151 , Diff
152
153 ghc-options: -Wall
154
155 if !flag(testing)
156 buildable: False
157
158
159test-suite properties 143test-suite properties
160 type: exitcode-stdio-1.0 144 type: exitcode-stdio-1.0
161 main-is: Main.hs 145 main-is: Main.hs
@@ -163,6 +147,7 @@ test-suite properties
163 build-depends: base == 4.* 147 build-depends: base == 4.*
164 , bytestring 148 , bytestring
165 , directory 149 , directory
150 , filepath
166 151
167 , cereal 152 , cereal
168 , network 153 , network
diff --git a/tests/InfoHash.hs b/tests/InfoHash.hs
deleted file mode 100644
index bbf77545..00000000
--- a/tests/InfoHash.hs
+++ /dev/null
@@ -1,29 +0,0 @@
1{-# LANGUAGE OverloadedStrings #-}
2module Main (main) where
3
4import qualified Data.ByteString.Char8 as BC
5import Data.Torrent
6
7import System.Environment
8import System.Exit
9
10checkInfo :: String
11checkInfo = "0221caf96aa3cb94f0f58d458e78b0fc344ad8bf"
12
13torrentFileName :: String
14torrentFileName = "res/dapper-dvd-amd64.iso.torrent"
15
16main :: IO ()
17main = do
18 args <- getArgs
19 let path = if length args == 0 then torrentFileName else head args
20
21 t <- fromFile path
22
23 BC.putStr "info hash: "
24 print (ppInfoHash (tInfoHash t))
25
26 let passed = checkInfo == show (ppInfoHash (tInfoHash t))
27
28 print passed
29 if passed then exitSuccess else exitFailure
diff --git a/tests/Main.hs b/tests/Main.hs
index bcadc97e..3c7bfcd3 100644
--- a/tests/Main.hs
+++ b/tests/Main.hs
@@ -16,7 +16,9 @@ import Data.Text as T
16 16
17import Network 17import Network
18import Network.URI 18import Network.URI
19
19import System.Directory 20import System.Directory
21import System.FilePath
20 22
21import Test.QuickCheck as QC 23import Test.QuickCheck as QC
22import Test.HUnit as HU 24import Test.HUnit as HU
@@ -105,6 +107,18 @@ instance Arbitrary Torrent where
105 <*> arbitrary <*> arbitrary <*> arbitrary 107 <*> arbitrary <*> arbitrary <*> arbitrary
106 <*> arbitrary <*> pure Nothing <*> arbitrary 108 <*> arbitrary <*> pure Nothing <*> arbitrary
107 109
110-- TODO add a few more torrents here
111torrentList :: [(FilePath, String)]
112torrentList =
113 [ ( "res" </> "dapper-dvd-amd64.iso.torrent"
114 , "0221caf96aa3cb94f0f58d458e78b0fc344ad8bf")
115 ]
116
117checkInfoHash :: (FilePath, String) -> Assertion
118checkInfoHash (path, expectedHash) = check =<< fromFile path
119 where
120 check t = expectedHash @=? show (ppInfoHash (tInfoHash t))
121
108{----------------------------------------------------------------------- 122{-----------------------------------------------------------------------
109 Handshake 123 Handshake
110-----------------------------------------------------------------------} 124-----------------------------------------------------------------------}
@@ -210,6 +224,10 @@ prop_messageEncoding msg
210 MemMap 224 MemMap
211-----------------------------------------------------------------------} 225-----------------------------------------------------------------------}
212 226
227tmpdir :: FilePath
228tmpdir = "tmp"
229
230boundaryTest :: Assertion
213boundaryTest = do 231boundaryTest = do
214 f <- mallocTo (Fixed.interval 0 1) Fixed.empty 232 f <- mallocTo (Fixed.interval 0 1) Fixed.empty
215 f <- mallocTo (Fixed.interval 1 2) f 233 f <- mallocTo (Fixed.interval 1 2) f
@@ -218,16 +236,18 @@ boundaryTest = do
218 bs <- readBytes (Fixed.interval 0 2) f 236 bs <- readBytes (Fixed.interval 0 2) f
219 "\x1\x2" @=? bs 237 "\x1\x2" @=? bs
220 238
239mmapSingle :: Assertion
221mmapSingle = do 240mmapSingle = do
222 f <- mmapTo "single.test" (10, 5) 5 Fixed.empty 241 f <- mmapTo (tmpdir </> "single.test") (10, 5) 5 Fixed.empty
223 writeBytes (Fixed.interval 5 5) "abcde" f 242 writeBytes (Fixed.interval 5 5) "abcde" f
224 bs <- readBytes (Fixed.interval 5 5) f 243 bs <- readBytes (Fixed.interval 5 5) f
225 "abcde" @=? bs 244 "abcde" @=? bs
226 245
246coalesceTest :: Assertion
227coalesceTest = do 247coalesceTest = do
228 f <- mmapTo "a.test" (0, 1) 10 Fixed.empty 248 f <- mmapTo (tmpdir </> "a.test") (0, 1) 10 Fixed.empty
229 f <- mmapTo "bc.test" (0, 2) 12 f 249 f <- mmapTo (tmpdir </> "bc.test") (0, 2) 12 f
230 f <- mmapTo "c.test" (0, 1) 13 f 250 f <- mmapTo (tmpdir </> "c.test") (0, 1) 13 f
231 writeBytes (Fixed.interval 10 4) "abcd" f 251 writeBytes (Fixed.interval 10 4) "abcd" f
232 bs <- readBytes (Fixed.interval 10 4) f 252 bs <- readBytes (Fixed.interval 10 4) f
233 "abcd" @=? bs 253 "abcd" @=? bs
@@ -243,6 +263,7 @@ allTests =
243 , testProperty "rarest in range" prop_rarestInRange 263 , testProperty "rarest in range" prop_rarestInRange
244 , testProperty "min less that max" prop_minMax 264 , testProperty "min less that max" prop_minMax
245 , testProperty "difference de morgan" prop_differenceDeMorgan 265 , testProperty "difference de morgan" prop_differenceDeMorgan
266
246 -- torrent module 267 -- torrent module
247 , testProperty "file info encoding" $ 268 , testProperty "file info encoding" $
248 prop_properBEncode (T :: T FileInfo) 269 prop_properBEncode (T :: T FileInfo)
@@ -250,9 +271,11 @@ allTests =
250 prop_properBEncode (T :: T ContentInfo) 271 prop_properBEncode (T :: T ContentInfo)
251 , testProperty "torrent encoding" $ 272 , testProperty "torrent encoding" $
252 prop_properBEncode (T :: T Torrent) 273 prop_properBEncode (T :: T Torrent)
253 274 ] ++
254 -- handshake module 275 fmap (testCase "info hash" . checkInfoHash) torrentList
255 , testProperty "handshake encoding" $ 276 ++
277 [ -- handshake module
278 testProperty "handshake encoding" $
256 prop_cerealEncoding (T :: T Handshake) 279 prop_cerealEncoding (T :: T Handshake)
257 , testProperty "message encoding" prop_messageEncoding 280 , testProperty "message encoding" prop_messageEncoding
258 281
@@ -264,8 +287,5 @@ allTests =
264 287
265main :: IO () 288main :: IO ()
266main = do 289main = do
267 let tmpdir = "tmp" -- for mem map test cases
268 createDirectoryIfMissing True tmpdir 290 createDirectoryIfMissing True tmpdir
269 setCurrentDirectory tmpdir
270
271 defaultMain allTests 291 defaultMain allTests