diff options
author | Sam T <sta.cs.vsu@gmail.com> | 2013-04-07 06:02:55 +0400 |
---|---|---|
committer | Sam T <sta.cs.vsu@gmail.com> | 2013-04-07 06:02:55 +0400 |
commit | 84d2138ae0df6025fea5f73a261ca71b6497636a (patch) | |
tree | e1dd3f58fe91a7c5dc007e31dedc7f1e886d7f34 /tests/info-hash.hs | |
parent | 881418d201e2a58e413161991a7e8d113f50a191 (diff) |
~ make info hash test meaningful
Diffstat (limited to 'tests/info-hash.hs')
-rw-r--r-- | tests/info-hash.hs | 45 |
1 files changed, 16 insertions, 29 deletions
diff --git a/tests/info-hash.hs b/tests/info-hash.hs index 1e511e43..07c01c9c 100644 --- a/tests/info-hash.hs +++ b/tests/info-hash.hs | |||
@@ -1,49 +1,36 @@ | |||
1 | {-# LANGUAGE OverloadedStrings #-} | 1 | {-# LANGUAGE OverloadedStrings #-} |
2 | module Main (main) where | 2 | module Main (main) where |
3 | 3 | ||
4 | import Control.Applicative | ||
5 | import Data.Foldable | 4 | import Data.Foldable |
6 | import Data.Maybe | ||
7 | import qualified Data.Map as M | ||
8 | import Data.ByteString (ByteString) | 5 | import Data.ByteString (ByteString) |
9 | import qualified Data.ByteString as B | 6 | import qualified Data.ByteString as B |
7 | import qualified Data.ByteString.Char8 as BC | ||
10 | import qualified Data.ByteString.Lazy as L | 8 | import qualified Data.ByteString.Lazy as L |
11 | import qualified Data.ByteString.Builder as B | 9 | import qualified Data.ByteString.Builder as B |
12 | import qualified Data.ByteString.Builder.Prim as B | 10 | import qualified Data.ByteString.Builder.Prim as B |
13 | |||
14 | import System.Environment | ||
15 | |||
16 | import Data.Torrent | 11 | import Data.Torrent |
17 | import Data.BEncode | 12 | import System.Environment |
18 | 13 | import System.Exit | |
19 | 14 | ||
20 | checkInfo :: B.ByteString | 15 | checkInfo :: ByteString |
21 | checkInfo = "0221caf96aa3cb94f0f58d458e78b0fc344ad8bf" | 16 | checkInfo = "0221caf96aa3cb94f0f58d458e78b0fc344ad8bf" |
22 | 17 | ||
23 | -- | _should_ be 'id' if content is correct | ||
24 | reencode :: B.ByteString -> Result L.ByteString | ||
25 | reencode content = (encode . toBEncode . (`asTypeOf` (undefined :: Torrent))) | ||
26 | `fmap` (fromBEncode =<< decode content) | ||
27 | |||
28 | ppHex :: B.ByteString -> B.ByteString | 18 | ppHex :: B.ByteString -> B.ByteString |
29 | ppHex = L.toStrict . B.toLazyByteString . foldMap (B.primFixed B.word8HexFixed) . B.unpack | 19 | ppHex = L.toStrict . B.toLazyByteString . foldMap (B.primFixed B.word8HexFixed) . B.unpack |
30 | 20 | ||
31 | chunk :: Int -> B.ByteString -> [B.ByteString] | 21 | torrentFileName :: String |
32 | chunk size b | B.length b == 0 = [b] | 22 | torrentFileName = "tests/dapper-dvd-amd64.iso.torrent" |
33 | | otherwise = | ||
34 | let (x, xs) = B.splitAt size b | ||
35 | in x : chunk size xs | ||
36 | |||
37 | showInfos :: ByteString -> (TorrentInfo, BEncode) | ||
38 | showInfos bs = | ||
39 | let Right (be@(BDict dict)) = decode bs | ||
40 | Right t = tInfo <$> (fromBEncode be) | ||
41 | orig = BDict $ let BDict info = fromJust (M.lookup "info" dict) | ||
42 | in M.insert "pieces" (BString "") info | ||
43 | in (t { tPieces = "" }, orig) | ||
44 | 23 | ||
45 | main :: IO () | 24 | main :: IO () |
46 | main = do | 25 | main = do |
47 | [path] <- getArgs | 26 | args <- getArgs |
27 | let path = if length args == 0 then torrentFileName else head args | ||
28 | |||
48 | Right t <- fromFile path | 29 | Right t <- fromFile path |
49 | B.putStrLn (ppHex (tInfoHash t)) | 30 | |
31 | BC.putStr "info hash: " | ||
32 | BC.putStrLn (ppHex (tInfoHash t)) | ||
33 | |||
34 | let passed = checkInfo == ppHex (tInfoHash t) | ||
35 | print passed | ||
36 | if passed then exitSuccess else exitFailure | ||