diff options
Diffstat (limited to 'tests')
-rw-r--r-- | tests/encoding.hs | 25 | ||||
-rw-r--r-- | tests/info-hash.hs | 9 |
2 files changed, 25 insertions, 9 deletions
diff --git a/tests/encoding.hs b/tests/encoding.hs index b820dc26..e1a8b63e 100644 --- a/tests/encoding.hs +++ b/tests/encoding.hs | |||
@@ -11,7 +11,9 @@ import Test.Framework (defaultMain) | |||
11 | import Test.Framework.Providers.QuickCheck2 (testProperty) | 11 | import Test.Framework.Providers.QuickCheck2 (testProperty) |
12 | import Test.QuickCheck | 12 | import Test.QuickCheck |
13 | 13 | ||
14 | import Data.Torrent | ||
14 | import Network.Torrent | 15 | import Network.Torrent |
16 | import Network.URI | ||
15 | 17 | ||
16 | positive :: Gen Int | 18 | positive :: Gen Int |
17 | positive = fromIntegral <$> (arbitrary :: Gen Word32) | 19 | positive = fromIntegral <$> (arbitrary :: Gen Word32) |
@@ -45,16 +47,30 @@ instance Arbitrary PeerID where | |||
45 | <*> arbitrary | 47 | <*> arbitrary |
46 | <*> arbitrary | 48 | <*> arbitrary |
47 | 49 | ||
50 | instance Arbitrary InfoHash where | ||
51 | arbitrary = (hash . B.pack) <$> vectorOf 20 arbitrary | ||
52 | |||
48 | instance Arbitrary Handshake where | 53 | instance Arbitrary Handshake where |
49 | arbitrary = defaultHandshake | 54 | arbitrary = defaultHandshake <$> arbitrary <*> arbitrary |
50 | <$> (B.pack <$> (vectorOf 20 arbitrary)) | ||
51 | <*> arbitrary | ||
52 | 55 | ||
53 | data T a = T | 56 | data T a = T |
54 | 57 | ||
55 | prop_encoding :: (Serialize a, Eq a) => T a -> [a] -> Bool | 58 | prop_encoding :: (Serialize a, Eq a) => T a -> [a] -> Bool |
56 | prop_encoding _ msgs = decode (encode msgs) == Right msgs | 59 | prop_encoding _ msgs = decode (encode msgs) == Right msgs |
57 | 60 | ||
61 | test_scrape_url :: Bool | ||
62 | test_scrape_url = check `all` tests | ||
63 | where | ||
64 | check (iu, ou) = (parseURI iu >>= (`scrapeURL` []) >>= return . show) == ou | ||
65 | tests = | ||
66 | [ ("http://example.com/announce" , Just "http://example.com/scrape") | ||
67 | , ("http://example.com/x/announce" , Just "http://example.com/x/scrape") | ||
68 | , ("http://example.com/announce.php" , Just "http://example.com/scrape.php") | ||
69 | , ("http://example.com/a" , Nothing) | ||
70 | , ("http://example.com/announce?x2%0644", Just "http://example.com/scrape?x2%0644") | ||
71 | , ("http://example.com/announce?x=2/4" , Nothing) | ||
72 | , ("http://example.com/x%064announce" , Nothing) | ||
73 | ] | ||
58 | 74 | ||
59 | main :: IO () | 75 | main :: IO () |
60 | main = do | 76 | main = do |
@@ -67,4 +83,7 @@ main = do | |||
67 | 83 | ||
68 | , testProperty "Handshake encode <-> decode" $ | 84 | , testProperty "Handshake encode <-> decode" $ |
69 | prop_encoding (T :: T Handshake) | 85 | prop_encoding (T :: T Handshake) |
86 | |||
87 | , testProperty "Scrape URL" $ | ||
88 | test_scrape_url | ||
70 | ] \ No newline at end of file | 89 | ] \ No newline at end of file |
diff --git a/tests/info-hash.hs b/tests/info-hash.hs index 07c01c9c..1638a205 100644 --- a/tests/info-hash.hs +++ b/tests/info-hash.hs | |||
@@ -1,23 +1,20 @@ | |||
1 | {-# LANGUAGE OverloadedStrings #-} | 1 | {-# LANGUAGE OverloadedStrings #-} |
2 | module Main (main) where | 2 | module Main (main) where |
3 | 3 | ||
4 | import Data.Foldable | ||
5 | import Data.ByteString (ByteString) | 4 | import Data.ByteString (ByteString) |
6 | import qualified Data.ByteString as B | 5 | import qualified Data.ByteString as B |
7 | import qualified Data.ByteString.Char8 as BC | 6 | import qualified Data.ByteString.Char8 as BC |
8 | import qualified Data.ByteString.Lazy as L | 7 | import qualified Data.ByteString.Lazy as L |
9 | import qualified Data.ByteString.Builder as B | 8 | |
10 | import qualified Data.ByteString.Builder.Prim as B | ||
11 | import Data.Torrent | 9 | import Data.Torrent |
10 | import Data.Torrent.InfoHash | ||
11 | |||
12 | import System.Environment | 12 | import System.Environment |
13 | import System.Exit | 13 | import System.Exit |
14 | 14 | ||
15 | checkInfo :: ByteString | 15 | checkInfo :: ByteString |
16 | checkInfo = "0221caf96aa3cb94f0f58d458e78b0fc344ad8bf" | 16 | checkInfo = "0221caf96aa3cb94f0f58d458e78b0fc344ad8bf" |
17 | 17 | ||
18 | ppHex :: B.ByteString -> B.ByteString | ||
19 | ppHex = L.toStrict . B.toLazyByteString . foldMap (B.primFixed B.word8HexFixed) . B.unpack | ||
20 | |||
21 | torrentFileName :: String | 18 | torrentFileName :: String |
22 | torrentFileName = "tests/dapper-dvd-amd64.iso.torrent" | 19 | torrentFileName = "tests/dapper-dvd-amd64.iso.torrent" |
23 | 20 | ||