]> git.rkrishnan.org Git - functorrent.git/blobdiff - test/MetainfoSpec.hs
hspec tests for single/multi torrent, bencode etc
[functorrent.git] / test / MetainfoSpec.hs
diff --git a/test/MetainfoSpec.hs b/test/MetainfoSpec.hs
new file mode 100644 (file)
index 0000000..93bf9ab
--- /dev/null
@@ -0,0 +1,46 @@
+{-
+ - Copyright (C) 2015-2016 Ramakrishnan Muthukrishnan <ram@rkrishnan.org>
+ -
+ - This file is part of FuncTorrent.
+ -
+ - FuncTorrent is free software; you can redistribute it and/or modify
+ - it under the terms of the GNU General Public License as published by
+ - the Free Software Foundation; either version 3 of the License, or
+ - (at your option) any later version.
+ -
+ - FuncTorrent is distributed in the hope that it will be useful,
+ - but WITHOUT ANY WARRANTY; without even the implied warranty of
+ - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ - GNU General Public License for more details.
+ -
+ - You should have received a copy of the GNU General Public License
+ - along with FuncTorrent; if not,  see <http://www.gnu.org/licenses/>
+ -}
+
+{-# LANGUAGE OverloadedStrings #-}
+
+module MetainfoSpec where
+
+import Test.Hspec
+
+import FuncTorrent.Metainfo (Info(..), Metainfo(..), DynamicInfo(..), torrentToMetainfo)
+import qualified Data.ByteString.Lazy.Char8 as BC (readFile, toStrict)
+import qualified Data.ByteString as B
+
+spec :: Spec
+spec =
+  describe "Bencode" $ do
+    it "torrent-file-1" $ do
+      c <- BC.readFile "data/test1.torrent"
+      let (Right m) = torrentToMetainfo (BC.toStrict c)
+      length (announceList m) `shouldBe` 1
+      pieceLength (info m) `shouldBe` 524288
+      pieces (info m) `shouldSatisfy` (\p -> B.length p `mod` 20 == 0)
+      let (SingleFileInfo name' _ _) = dyninfo $ info m
+      name' `shouldNotBe` ""
+    it "torrent-file-4" $ do
+      c <- BC.readFile "data/test4.torrent"
+      let (Right m) = torrentToMetainfo (BC.toStrict c)
+      length (announceList m) `shouldBe` 3
+      let (MultiFileInfo _ files') = dyninfo $ info m
+      length files' `shouldNotBe` 1