]> git.rkrishnan.org Git - functorrent.git/blob - test/Test.hs
b0be736950df6b4dfde6be30d43b1ab7e69a261b
[functorrent.git] / test / Test.hs
1 {-# LANGUAGE OverloadedStrings #-}
2 module Main where
3
4 import Prelude hiding (readFile)
5
6 import Data.ByteString (ByteString, readFile)
7 import Data.Map.Strict (fromList)
8
9 import Test.Tasty
10 import Test.Tasty.HUnit
11
12 import FuncTorrent.Bencode (decode, BVal(..))
13 import FuncTorrent.Metainfo (Info(..), Metainfo(..), mkMetaInfo)
14 import FuncTorrent.Peer (Peer(..))
15 import FuncTorrent.Tracker
16
17 -- Parsed .torrent file
18 file :: BVal
19 file = Bdict (fromList [
20                ("announce",Bstr "http://9.rarbg.com:2710/announce"),
21                ("comment",Bstr "hello world"),
22                ("created by",Bstr "Jaseem Abid"),
23                ("creation date",Bint 1428717851),
24                ("encoding",Bstr "UTF-8"),
25                ("info",Bdict (fromList [
26                                ("length",Bint 12),
27                                ("name",Bstr "hello.txt"),
28                                ("piece length",Bint 32768),
29                                ("pieces",Bstr "\"Ycc\179\222@\176o\152\US\184]\130\&1.\140\SO\213\DC1"),
30                                ("private",Bint 0)]))])
31
32 hello :: Metainfo
33 hello = Metainfo {
34           info = Info {
35             pieceLength = 32768,
36             pieces = "\"Ycc\179\222@\176o\152\US\184]\130\&1.\140\SO\213\DC1",
37             private = Nothing,
38             name = "hello.txt",
39             lengthInBytes = 12,
40             md5sum = Nothing
41           },
42           announceList = ["http://9.rarbg.com:2710/announce"],
43           creationDate = Just 1428717851,
44           comment = Just "hello world",
45           createdBy = Just "Jaseem Abid",
46           encoding = Just "UTF-8",
47           infoHash = "\205CX(;\163<?TWS\175\CAND\222\253\250\214\136\EOT"
48         }
49
50 testFile :: TestTree
51 testFile = testCase "Should parse valid torrent files" $ do
52                str <- readFile "./data/hello.txt.torrent"
53                case decode str of
54                  Right expected -> expected @?= file
55                  Left _ -> error "Failed parsing test file"
56
57
58 testMkMetaInfo :: TestTree
59 testMkMetaInfo = testCase "Should mkInfo valid torrent files" $ do
60                    str <- readFile "./data/hello.txt.torrent"
61                    case decode str of
62                      Right expected -> mkMetaInfo expected @?= Just hello
63                      Left _ -> error "Failed parsing test file"
64
65 testResponse1 :: TestTree
66 testResponse1 = testCase "Should parse valid tracker response" $ do
67                   str <- readFile "./data/debian-7.8.0-amd64-CD-1.iso.cache"
68                   case decode str of
69                     Right bval -> expectation @?= mkTrackerResponse bval
70                     Left _ -> error "Failed parsing test file"
71                   where
72                     expectation :: Either a TrackerResponse
73                     expectation = Right TrackerResponse {
74                                     interval = Just 900,
75                                     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],
76                                     complete = Nothing,
77                                     incomplete = Nothing
78                                 }
79
80 testResponse2 :: TestTree
81 testResponse2 = testCase "Should parse invalid tracker response" $ do
82                   str <- readFile "./data/debian-7.8.0-amd64-CD-1.iso.error"
83                   case decode str of
84                     Right bval -> expectation @?= mkTrackerResponse bval
85                     Left _ -> error "Failed parsing test file"
86                   where
87                     expectation :: Either ByteString a
88                     expectation = Left "torrent not found"
89
90
91 unitTests :: TestTree
92 unitTests = testGroup "Unit tests" [testFile, testMkMetaInfo, testResponse1,
93                                             testResponse2]
94
95 tests :: TestTree
96 tests = testGroup "Tests" [unitTests]
97
98 main :: IO ()
99 main = defaultMain tests