summaryrefslogtreecommitdiff
path: root/tests/Data/Torrent/InfoHashSpec.hs
blob: 06570ec9628cda09f4b42208c32167a39de76b0e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
{-# OPTIONS -fno-warn-orphans #-}
module Data.Torrent.InfoHashSpec (spec) where

import Control.Applicative
import Data.ByteString as BS
import Data.Convertible
import System.FilePath
import Test.Hspec
import Test.QuickCheck
import Test.QuickCheck.Instances ()

import Data.Torrent
import Data.Torrent.InfoHash as IH


instance Arbitrary InfoHash where
  arbitrary = do
    bs <- BS.pack <$> vectorOf 20 arbitrary
    pure $ either (const (error "arbitrary infohash")) id $ safeConvert bs

type TestPair = (FilePath, String)

-- TODO add a few more torrents here
torrentList :: [TestPair]
torrentList =
  [ ( "res" </> "dapper-dvd-amd64.iso.torrent"
    , "0221caf96aa3cb94f0f58d458e78b0fc344ad8bf")
  ]

infohashSpec :: (FilePath, String) -> Spec
infohashSpec (filepath, expectedHash) = do
  it ("should match " ++ filepath) $ do
    torrent    <- fromFile filepath
    let actualHash = show $ idInfoHash $ tInfoDict torrent
    actualHash `shouldBe` expectedHash

spec :: Spec
spec = do
  describe "info hash" $ do
    mapM_ infohashSpec torrentList