]> git.rkrishnan.org Git - functorrent.git/blob - test/MetainfoSpec.hs
beginning of the filesystem module
[functorrent.git] / test / MetainfoSpec.hs
1 {-
2  - Copyright (C) 2015-2016 Ramakrishnan Muthukrishnan <ram@rkrishnan.org>
3  -
4  - This file is part of FuncTorrent.
5  -
6  - FuncTorrent is free software; you can redistribute it and/or modify
7  - it under the terms of the GNU General Public License as published by
8  - the Free Software Foundation; either version 3 of the License, or
9  - (at your option) any later version.
10  -
11  - FuncTorrent is distributed in the hope that it will be useful,
12  - but WITHOUT ANY WARRANTY; without even the implied warranty of
13  - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  - GNU General Public License for more details.
15  -
16  - You should have received a copy of the GNU General Public License
17  - along with FuncTorrent; if not,  see <http://www.gnu.org/licenses/>
18  -}
19
20 {-# LANGUAGE OverloadedStrings #-}
21
22 module MetainfoSpec where
23
24 import Test.Hspec
25
26 import FuncTorrent.Metainfo (Info(..), Metainfo(..), FileInfo(..), torrentToMetainfo)
27 import qualified Data.ByteString.Lazy.Char8 as BC (readFile, toStrict)
28 import qualified Data.ByteString as B
29
30 spec :: Spec
31 spec =
32   describe "Bencode" $ do
33     it "torrent-file-1" $ do
34       c <- BC.readFile "data/test1.torrent"
35       let (Right m) = torrentToMetainfo (BC.toStrict c)
36       length (announceList m) `shouldBe` 1
37       pieceLength (info m) `shouldBe` 524288
38       pieces (info m) `shouldSatisfy` (\p -> B.length p `mod` 20 == 0)
39       let fs = files $ info m
40           (FileInfo _ _ name') = head fs
41       name' `shouldNotBe` ""
42       length fs `shouldBe` 1
43     it "torrent-file-2" $ do
44       c <- BC.readFile "data/test2.torrent"
45       let (Right m) = torrentToMetainfo (BC.toStrict c)
46           fs = files $ info m
47           (FileInfo _ _ name') = head fs
48       name' `shouldNotBe` ""
49       length fs `shouldBe` 1
50     it "torrent-file-3" $ do
51       c <- BC.readFile "data/test3.torrent"
52       let (Right m) = torrentToMetainfo (BC.toStrict c)
53           fs = files $ info m
54           (FileInfo _ _ name') = head fs
55       name' `shouldNotBe` ""
56       length fs `shouldBe` 1
57     it "torrent-file-4" $ do
58       c <- BC.readFile "data/test4.torrent"
59       let (Right m) = torrentToMetainfo (BC.toStrict c)
60       length (announceList m) `shouldBe` 3
61       let fs = files $ info m
62           p = path $ head fs
63       length fs `shouldBe` 9 -- 9 files in the torrent
64       p `shouldStartWith` "NASA_NTRS_Archive_19740027163"
65