diff options
author | Sam Truzjan <pxqr.sta@gmail.com> | 2013-10-31 11:25:59 +0400 |
---|---|---|
committer | Sam Truzjan <pxqr.sta@gmail.com> | 2013-10-31 11:25:59 +0400 |
commit | 01cef3fafc27d39d88c94cacdcd8e204c5f66b86 (patch) | |
tree | 01040aca19e49f4e7937383fef53b8c82bcec12b /tests/Data | |
parent | c1fec260f47084300ac30de2e43d52966316a2c7 (diff) |
Merge bittorrent package with torrent-content
Diffstat (limited to 'tests/Data')
-rw-r--r-- | tests/Data/Torrent/InfoHashSpec.hs | 36 | ||||
-rw-r--r-- | tests/Data/Torrent/MagnetSpec.hs | 44 | ||||
-rw-r--r-- | tests/Data/Torrent/MetainfoSpec.hs | 76 |
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 #-} | ||
2 | module Data.Torrent.InfoHashSpec (spec) where | ||
3 | |||
4 | import Control.Applicative | ||
5 | import System.FilePath | ||
6 | import Test.Hspec | ||
7 | import Test.QuickCheck | ||
8 | import Test.QuickCheck.Instances () | ||
9 | |||
10 | import Data.Torrent | ||
11 | import Data.Torrent.InfoHash as IH | ||
12 | |||
13 | |||
14 | instance Arbitrary InfoHash where | ||
15 | arbitrary = IH.hash <$> arbitrary | ||
16 | |||
17 | type TestPair = (FilePath, String) | ||
18 | |||
19 | -- TODO add a few more torrents here | ||
20 | torrentList :: [TestPair] | ||
21 | torrentList = | ||
22 | [ ( "res" </> "dapper-dvd-amd64.iso.torrent" | ||
23 | , "0221caf96aa3cb94f0f58d458e78b0fc344ad8bf") | ||
24 | ] | ||
25 | |||
26 | infohashSpec :: (FilePath, String) -> Spec | ||
27 | infohashSpec (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 | |||
33 | spec :: Spec | ||
34 | spec = 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 #-} | ||
2 | module Data.Torrent.MagnetSpec (spec) where | ||
3 | |||
4 | import Control.Applicative | ||
5 | import Data.Maybe | ||
6 | import Test.Hspec | ||
7 | import Test.QuickCheck | ||
8 | import Test.QuickCheck.Instances () | ||
9 | import Network.URI | ||
10 | |||
11 | import Data.Torrent.InfoHash | ||
12 | import Data.Torrent.Magnet | ||
13 | import Data.Torrent.InfoHashSpec () | ||
14 | |||
15 | |||
16 | instance Arbitrary URIAuth where | ||
17 | arbitrary = URIAuth <$> arbitrary <*> arbitrary <*> arbitrary | ||
18 | |||
19 | instance Arbitrary URI where | ||
20 | arbitrary | ||
21 | = pure $ fromJust $ parseURI "http://ietf.org/1737.txt?a=1&b=h#123" | ||
22 | |||
23 | instance Arbitrary Magnet where | ||
24 | arbitrary = Magnet <$> arbitrary <*> arbitrary | ||
25 | <*> arbitrary <*> arbitrary <*> arbitrary <*> arbitrary | ||
26 | <*> arbitrary <*> arbitrary <*> pure (error "arbitrary magnet") | ||
27 | |||
28 | magnetEncoding :: Magnet -> Bool | ||
29 | magnetEncoding m = parseMagnet (renderMagnet m) == Just m | ||
30 | |||
31 | spec :: Spec | ||
32 | spec = 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 #-} | ||
3 | module Data.Torrent.MetainfoSpec (spec) where | ||
4 | |||
5 | import Control.Applicative | ||
6 | import Data.ByteString as BS | ||
7 | import Data.ByteString.Lazy as BL | ||
8 | import Data.BEncode | ||
9 | import Data.Maybe | ||
10 | import Network.URI | ||
11 | import Test.Hspec | ||
12 | import Test.QuickCheck | ||
13 | import Test.QuickCheck.Instances () | ||
14 | |||
15 | import Data.Torrent.Layout | ||
16 | import Data.Torrent | ||
17 | |||
18 | |||
19 | {----------------------------------------------------------------------- | ||
20 | -- Common | ||
21 | -----------------------------------------------------------------------} | ||
22 | |||
23 | data T a = T | ||
24 | |||
25 | prop_properBEncode :: Show a => BEncode a => Eq a | ||
26 | => T a -> a -> Bool | ||
27 | prop_properBEncode _ expected = actual == Right expected | ||
28 | where | ||
29 | actual = decode $ BL.toStrict $ encode expected | ||
30 | |||
31 | instance Arbitrary URI where | ||
32 | arbitrary = pure $ fromJust | ||
33 | $ parseURI "http://exsample.com:80/123365_asd" | ||
34 | |||
35 | {----------------------------------------------------------------------- | ||
36 | -- Instances | ||
37 | -----------------------------------------------------------------------} | ||
38 | |||
39 | instance Arbitrary FileSize where | ||
40 | arbitrary = fromIntegral <$> (arbitrary :: Gen Int) | ||
41 | |||
42 | instance Arbitrary a => Arbitrary (FileInfo a) where | ||
43 | arbitrary = FileInfo <$> arbitrary <*> arbitrary <*> arbitrary | ||
44 | |||
45 | instance Arbitrary LayoutInfo where | ||
46 | arbitrary = oneof | ||
47 | [ SingleFile <$> arbitrary | ||
48 | , MultiFile <$> arbitrary <*> arbitrary | ||
49 | ] | ||
50 | |||
51 | instance Arbitrary InfoDict where | ||
52 | arbitrary = undefined | ||
53 | |||
54 | instance 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 | |||
64 | spec :: Spec | ||
65 | spec = 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) | ||