summaryrefslogtreecommitdiff
path: root/tests/info-hash.hs
diff options
context:
space:
mode:
authorSam T <sta.cs.vsu@gmail.com>2013-04-07 06:02:55 +0400
committerSam T <sta.cs.vsu@gmail.com>2013-04-07 06:02:55 +0400
commit84d2138ae0df6025fea5f73a261ca71b6497636a (patch)
treee1dd3f58fe91a7c5dc007e31dedc7f1e886d7f34 /tests/info-hash.hs
parent881418d201e2a58e413161991a7e8d113f50a191 (diff)
~ make info hash test meaningful
Diffstat (limited to 'tests/info-hash.hs')
-rw-r--r--tests/info-hash.hs45
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 #-}
2module Main (main) where 2module Main (main) where
3 3
4import Control.Applicative
5import Data.Foldable 4import Data.Foldable
6import Data.Maybe
7import qualified Data.Map as M
8import Data.ByteString (ByteString) 5import Data.ByteString (ByteString)
9import qualified Data.ByteString as B 6import qualified Data.ByteString as B
7import qualified Data.ByteString.Char8 as BC
10import qualified Data.ByteString.Lazy as L 8import qualified Data.ByteString.Lazy as L
11import qualified Data.ByteString.Builder as B 9import qualified Data.ByteString.Builder as B
12import qualified Data.ByteString.Builder.Prim as B 10import qualified Data.ByteString.Builder.Prim as B
13
14import System.Environment
15
16import Data.Torrent 11import Data.Torrent
17import Data.BEncode 12import System.Environment
18 13import System.Exit
19 14
20checkInfo :: B.ByteString 15checkInfo :: ByteString
21checkInfo = "0221caf96aa3cb94f0f58d458e78b0fc344ad8bf" 16checkInfo = "0221caf96aa3cb94f0f58d458e78b0fc344ad8bf"
22 17
23-- | _should_ be 'id' if content is correct
24reencode :: B.ByteString -> Result L.ByteString
25reencode content = (encode . toBEncode . (`asTypeOf` (undefined :: Torrent)))
26 `fmap` (fromBEncode =<< decode content)
27
28ppHex :: B.ByteString -> B.ByteString 18ppHex :: B.ByteString -> B.ByteString
29ppHex = L.toStrict . B.toLazyByteString . foldMap (B.primFixed B.word8HexFixed) . B.unpack 19ppHex = L.toStrict . B.toLazyByteString . foldMap (B.primFixed B.word8HexFixed) . B.unpack
30 20
31chunk :: Int -> B.ByteString -> [B.ByteString] 21torrentFileName :: String
32chunk size b | B.length b == 0 = [b] 22torrentFileName = "tests/dapper-dvd-amd64.iso.torrent"
33 | otherwise =
34 let (x, xs) = B.splitAt size b
35 in x : chunk size xs
36
37showInfos :: ByteString -> (TorrentInfo, BEncode)
38showInfos 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
45main :: IO () 24main :: IO ()
46main = do 25main = 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