summaryrefslogtreecommitdiff
path: root/tests/Data
diff options
context:
space:
mode:
authorSam Truzjan <pxqr.sta@gmail.com>2013-10-31 11:25:59 +0400
committerSam Truzjan <pxqr.sta@gmail.com>2013-10-31 11:25:59 +0400
commit01cef3fafc27d39d88c94cacdcd8e204c5f66b86 (patch)
tree01040aca19e49f4e7937383fef53b8c82bcec12b /tests/Data
parentc1fec260f47084300ac30de2e43d52966316a2c7 (diff)
Merge bittorrent package with torrent-content
Diffstat (limited to 'tests/Data')
-rw-r--r--tests/Data/Torrent/InfoHashSpec.hs36
-rw-r--r--tests/Data/Torrent/MagnetSpec.hs44
-rw-r--r--tests/Data/Torrent/MetainfoSpec.hs76
3 files changed, 156 insertions, 0 deletions
diff --git a/tests/Data/Torrent/InfoHashSpec.hs b/tests/Data/Torrent/InfoHashSpec.hs
new file mode 100644
index 00000000..ba9ce9a4
--- /dev/null
+++ b/tests/Data/Torrent/InfoHashSpec.hs
@@ -0,0 +1,36 @@
1{-# OPTIONS -fno-warn-orphans #-}
2module Data.Torrent.InfoHashSpec (spec) where
3
4import Control.Applicative
5import System.FilePath
6import Test.Hspec
7import Test.QuickCheck
8import Test.QuickCheck.Instances ()
9
10import Data.Torrent
11import Data.Torrent.InfoHash as IH
12
13
14instance Arbitrary InfoHash where
15 arbitrary = IH.hash <$> arbitrary
16
17type TestPair = (FilePath, String)
18
19-- TODO add a few more torrents here
20torrentList :: [TestPair]
21torrentList =
22 [ ( "res" </> "dapper-dvd-amd64.iso.torrent"
23 , "0221caf96aa3cb94f0f58d458e78b0fc344ad8bf")
24 ]
25
26infohashSpec :: (FilePath, String) -> Spec
27infohashSpec (filepath, expectedHash) = do
28 it ("should match " ++ filepath) $ do
29 torrent <- fromFile filepath
30 let actualHash = show $ idInfoHash $ tInfoDict torrent
31 actualHash `shouldBe` expectedHash
32
33spec :: Spec
34spec = do
35 describe "info hash" $ do
36 mapM_ infohashSpec torrentList
diff --git a/tests/Data/Torrent/MagnetSpec.hs b/tests/Data/Torrent/MagnetSpec.hs
new file mode 100644
index 00000000..5adc6df7
--- /dev/null
+++ b/tests/Data/Torrent/MagnetSpec.hs
@@ -0,0 +1,44 @@
1{-# OPTIONS -fno-warn-orphans #-}
2module Data.Torrent.MagnetSpec (spec) where
3
4import Control.Applicative
5import Data.Maybe
6import Test.Hspec
7import Test.QuickCheck
8import Test.QuickCheck.Instances ()
9import Network.URI
10
11import Data.Torrent.InfoHash
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 (error "arbitrary magnet")
27
28magnetEncoding :: Magnet -> Bool
29magnetEncoding m = parseMagnet (renderMagnet m) == 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 = InfoHash "\DC4\255\229\221#\CAN\143\213\203S\161\212\DEL\DC2\137\219p\171\243\RS"
39 parseMagnet magnet `shouldBe` Just (nullMagnet ih)
40
41 it "parse base16" $ do
42 let magnet = "magnet:?xt=urn:btih:0123456789abcdef0123456789abcdef01234567"
43 let ih = InfoHash "\SOH#Eg\137\171\205\239\SOH#Eg\137\171\205\239\SOH#Eg"
44 parseMagnet magnet `shouldBe` Just (nullMagnet ih)
diff --git a/tests/Data/Torrent/MetainfoSpec.hs b/tests/Data/Torrent/MetainfoSpec.hs
new file mode 100644
index 00000000..297b28f1
--- /dev/null
+++ b/tests/Data/Torrent/MetainfoSpec.hs
@@ -0,0 +1,76 @@
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 Network.URI
11import Test.Hspec
12import Test.QuickCheck
13import Test.QuickCheck.Instances ()
14
15import Data.Torrent.Layout
16import Data.Torrent
17
18
19{-----------------------------------------------------------------------
20-- Common
21-----------------------------------------------------------------------}
22
23data T a = T
24
25prop_properBEncode :: Show a => BEncode a => Eq a
26 => T a -> a -> Bool
27prop_properBEncode _ expected = actual == Right expected
28 where
29 actual = decode $ BL.toStrict $ encode expected
30
31instance Arbitrary URI where
32 arbitrary = pure $ fromJust
33 $ parseURI "http://exsample.com:80/123365_asd"
34
35{-----------------------------------------------------------------------
36-- Instances
37-----------------------------------------------------------------------}
38
39instance Arbitrary FileSize where
40 arbitrary = fromIntegral <$> (arbitrary :: Gen Int)
41
42instance Arbitrary a => Arbitrary (FileInfo a) where
43 arbitrary = FileInfo <$> arbitrary <*> arbitrary <*> arbitrary
44
45instance Arbitrary LayoutInfo where
46 arbitrary = oneof
47 [ SingleFile <$> arbitrary
48 , MultiFile <$> arbitrary <*> arbitrary
49 ]
50
51instance Arbitrary InfoDict where
52 arbitrary = undefined
53
54instance Arbitrary Torrent where
55 arbitrary = Torrent <$> arbitrary
56 <*> arbitrary <*> arbitrary <*> arbitrary
57 <*> arbitrary <*> arbitrary <*> arbitrary
58 <*> arbitrary <*> pure Nothing <*> arbitrary
59
60{-----------------------------------------------------------------------
61-- Spec
62-----------------------------------------------------------------------}
63
64spec :: Spec
65spec = do
66 describe "FileInfo" $ do
67 it "properly bencoded" $ property $
68 prop_properBEncode (T :: T (FileInfo BS.ByteString))
69
70 describe "LayoutInfo" $ do
71 it "properly bencoded" $ property $
72 prop_properBEncode (T :: T LayoutInfo)
73
74 describe "Torrent" $ do
75 it "property bencoded" $ property $
76 prop_properBEncode (T :: T Torrent)