]> git.rkrishnan.org Git - functorrent.git/blob - test/Test.hs
0f93e8ace0c2a81625fdd17d80f06ccc14980523
[functorrent.git] / test / Test.hs
1 {-# LANGUAGE OverloadedStrings #-}
2 module Main where
3
4 import Prelude hiding (readFile)
5 import Data.ByteString.Char8 (ByteString, readFile)
6 import Data.Map.Strict (fromList)
7
8 import Test.Tasty
9 import Test.Tasty.HUnit
10
11 import FuncTorrent.Bencode (decode, BVal(..))
12 import FuncTorrent.Metainfo (Info(..), Metainfo(..), mkMetaInfo)
13 import FuncTorrent.Peer (Peer(..))
14 import FuncTorrent.Tracker (TrackerResponse(..), peers, mkTrackerResponse)
15
16 -- Parsed .torrent file
17 file :: BVal
18 file = Bdict (fromList [
19                ("announce", Bstr "http://carrot.cs.swarthmore.edu:6969"),
20                ("created by", Bstr "Enhanced-CTorrent/dnh3.3.2"),
21                ("creation date", Bint 1352142460),
22                ("info", Bdict (fromList [
23                                 ("length", Bint 31690),
24                                 ("name", Bstr "moby_dick.txt"),
25                                 ("piece length", Bint 262144),
26                                 ("pieces", Bstr "\bW\196\168g\178\198=\156\204\221M\242\207\DC1\159\ETB~\241H")]))])
27
28 moby :: Metainfo
29 moby = Metainfo {
30          info = Info {
31            pieceLength = 262144,
32            pieces = "\bW\196\168g\178\198=\156\204\221M\242\207\DC1\159\ETB~\241H",
33            private = Nothing,
34            name = "moby_dick.txt",
35            lengthInBytes = 31690,
36            md5sum = Nothing
37          },
38          announceList = ["http://carrot.cs.swarthmore.edu:6969"],
39          creationDate = Nothing,
40          comment = Nothing,
41          createdBy = Just "Enhanced-CTorrent/dnh3.3.2",
42          encoding = Nothing
43        }
44
45 testFile :: TestTree
46 testFile = testCase "Should parse valid torrent files" $ do
47                str <- readFile "./data/moby_dick.txt.torrent"
48                case decode str of
49                  Right expected -> expected @?= file
50                  Left _ -> error "Failed parsing test file"
51
52
53 testMkMetaInfo :: TestTree
54 testMkMetaInfo = testCase "Should mkInfo valid torrent files" $ do
55                    str <- readFile "./data/moby_dick.txt.torrent"
56                    case decode str of
57                      Right expected -> mkMetaInfo expected @?= Just moby
58                      Left _ -> error "Failed parsing test file"
59
60
61 testResponse1 :: TestTree
62 testResponse1 = testCase "Should parse valid tracker response" $ do
63                   str <- readFile "./data/debian-7.8.0-amd64-CD-1.iso.cache"
64                   case decode str of
65                     Right bval -> expectation @?= mkTrackerResponse bval
66                     Left _ -> error "Failed parsing test file"
67                   where
68                     expectation :: Either a TrackerResponse
69                     expectation = Right TrackerResponse {
70                                     interval = Just 900,
71                                     peers = [Peer "85.25.201.101" 51413, Peer "37.59.28.236" 22222, Peer "76.21.149.43" 51866, Peer "31.183.33.205" 43467, Peer "213.210.120.86" 27480, Peer "213.239.216.205" 6914, Peer "91.192.163.152" 11834, Peer "62.210.240.65" 6999, Peer "84.250.103.161" 6949, Peer "88.195.241.192" 51413, Peer "88.165.61.223" 6881, Peer "86.157.234.243" 59583, Peer "213.41.137.242" 51413, Peer "91.10.84.195" 46941, Peer "64.56.249.183" 7023, Peer "202.62.16.71" 59929, Peer "31.43.126.122" 57816, Peer "68.169.133.72" 50222, Peer "223.135.97.177" 58813, Peer "5.166.93.118" 64459, Peer "200.148.109.141" 51413, Peer "109.226.236.160" 44444, Peer "78.58.139.154" 22818, Peer "188.244.47.186" 39643, Peer "203.86.204.111" 52411, Peer "80.110.40.98" 6918, Peer "68.187.142.217" 58352, Peer "71.115.139.180" 63065, Peer "70.169.35.173" 51413, Peer "185.3.135.186" 10889, Peer "88.198.224.202" 51413, Peer "183.157.65.217" 9179, Peer "87.251.189.150" 46680, Peer "87.114.202.174" 12393, Peer "93.58.5.16" 51411, Peer "89.102.9.69" 10044, Peer "94.159.19.222" 15783, Peer "95.28.49.176" 58794, Peer "217.114.58.135" 6881, Peer "79.141.162.38" 35806, Peer "136.169.50.72" 54927, Peer "187.67.188.151" 51413, Peer "79.111.218.50" 53636, Peer "62.75.137.129" 51413, Peer "14.204.20.156" 11600, Peer "79.141.162.34" 24531, Peer "82.144.192.7" 63208, Peer "212.34.231.10" 20684, Peer "95.225.246.221" 51413, Peer "124.41.237.102" 24874],
72                                     complete = Nothing,
73                                     incomplete = Nothing
74                                 }
75
76 testResponse2 :: TestTree
77 testResponse2 = testCase "Should parse invalid tracker response" $ do
78                   str <- readFile "./data/debian-7.8.0-amd64-CD-1.iso.error"
79                   case decode str of
80                     Right bval -> expectation @?= mkTrackerResponse bval
81                     Left _ -> error "Failed parsing test file"
82                   where
83                     expectation :: Either ByteString a
84                     expectation = Left "torrent not found"
85
86
87 unitTests :: TestTree
88 unitTests = testGroup "Unit tests" [testFile, testMkMetaInfo, testResponse1,
89                                             testResponse2]
90
91 tests :: TestTree
92 tests = testGroup "Tests" [unitTests]
93
94 main :: IO ()
95 main = defaultMain tests