Coverage for tests\test_recheck.py: 100%

158 statements  

« prev     ^ index     » next       coverage.py v7.3.0, created at 2023-08-27 21:50 -0700

1#! /usr/bin/python3 

2# -*- coding: utf-8 -*- 

3 

4############################################################################## 

5# Copyright (C) 2021-current alexpdev 

6# 

7# Licensed under the Apache License, Version 2.0 (the "License"); 

8# you may not use this file except in compliance with the License. 

9# You may obtain a copy of the License at 

10# 

11# http://www.apache.org/licenses/LICENSE-2.0 

12# 

13# Unless required by applicable law or agreed to in writing, software 

14# distributed under the License is distributed on an "AS IS" BASIS, 

15# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 

16# See the License for the specific language governing permissions and 

17# limitations under the License. 

18############################################################################## 

19""" 

20Testing functions for the progress module. 

21""" 

22 

23import os 

24import sys 

25from pathlib import Path 

26 

27from tests import ( 

28 dir1, dir2, file1, file2, filemeta1, filemeta2, metafile1, metafile2, 

29 rmpath, sizedfiles, sizes) 

30from torrentfile.cli import main_script as main 

31from torrentfile.recheck import Checker 

32from torrentfile.utils import ArgumentError 

33 

34 

35def test_fixtures(): 

36 """ 

37 Test fixtures exist. 

38 """ 

39 assert dir1 and dir2 and file1 and file2 

40 assert filemeta1 and filemeta2 and metafile1 

41 assert metafile2 and sizes and sizedfiles 

42 

43 

44def test_checker_class(dir1, metafile1): 

45 """ 

46 Test Checker Class against meta files. 

47 """ 

48 checker = Checker(metafile1, dir1) 

49 assert checker.results() == 100 

50 

51 

52def test_checker_first_piece(dir2, sizedfiles): 

53 """ 

54 Test Checker Class when first piece is slightly alterred. 

55 """ 

56 

57 def change(path): 

58 """ 

59 Change some bytes in file. 

60 """ 

61 if path.is_file(): 

62 new = b"Something other than what was there before." 

63 with open(path, "rb") as bfile: 

64 data = bfile.read() 

65 new_len = len(new) 

66 content = b"".join([new, data[new_len:]]) 

67 with open(path, "wb") as bdoc: 

68 bdoc.write(content) 

69 elif path.is_dir(): 

70 for item in path.iterdir(): 

71 change(item) 

72 

73 change(Path(dir2)) 

74 checker = Checker(sizedfiles, dir2) 

75 assert checker.results() != 100 

76 

77 

78def test_checker_first_piece_alt(dir2, sizedfiles): 

79 """ 

80 Test Checker Class when first piece is slightly alterred. 

81 """ 

82 

83 def change(path): 

84 """ 

85 Change some bytes in file. 

86 """ 

87 if os.path.isfile(path): 

88 with open(path, "rb") as bfile: 

89 data = bfile.read() 

90 new = b"some_other_bytes_to_use" 

91 new_len = len(new) 

92 with open(path, "wb") as wfile: 

93 wfile.write(new + data[new_len:]) 

94 elif os.path.isdir(path): 

95 for item in os.listdir(path): 

96 change(os.path.join(path, item)) 

97 

98 change(dir2) 

99 checker = Checker(sizedfiles, dir2) 

100 assert checker.results() != 100 

101 

102 

103def test_partial_metafiles(dir2, sizedfiles): 

104 """ 

105 Test Checker with data that is expected to be incomplete. 

106 """ 

107 

108 def shortenfile(path): 

109 """ 

110 Shorten a few files for testing purposes. 

111 """ 

112 with open(path, "rb") as bfile: 

113 data = bfile.read() 

114 with open(path, "wb") as bfile: 

115 bfile.write(data[:-(2**10)]) 

116 

117 for item in os.listdir(dir2): 

118 full = os.path.join(dir2, item) 

119 if os.path.isfile(full): 

120 shortenfile(full) 

121 

122 testdir = os.path.dirname(dir2) 

123 checker = Checker(sizedfiles, testdir) 

124 assert checker.results() != 100 

125 

126 

127def test_checker_callback(dir1, metafile1): 

128 """ 

129 Test Checker class with directory that points to nothing. 

130 """ 

131 Checker.register_callback(lambda *x: print(x)) 

132 checker = Checker(metafile1, str(dir1)) 

133 assert checker.results() == 100 

134 

135 

136def test_checker_cli_args(dir1, metafile1): 

137 """ 

138 Test exclusive Checker Mode CLI. 

139 """ 

140 sys.argv = ["torrentfile", "check", str(metafile1), str(dir1)] 

141 output = main() 

142 assert output == 100 

143 

144 

145def test_checker_parent_dir(dir1, metafile1): 

146 """ 

147 Test providing the parent directory for torrent checking feature. 

148 """ 

149 checker = Checker(metafile1, os.path.dirname(dir1)) 

150 assert checker.results() == 100 

151 

152 

153def test_checker_with_file(file1, filemeta1): 

154 """ 

155 Test checker with single file torrent. 

156 """ 

157 checker = Checker(filemeta1, file1) 

158 assert checker.results() == 100 

159 

160 

161def test_checker_no_meta_file(): 

162 """ 

163 Test Checker when incorrect metafile is provided. 

164 """ 

165 try: 

166 Checker("peaches", "$") 

167 except FileNotFoundError: 

168 assert True 

169 

170 

171def test_checker_wrong_root_dir(metafile1): 

172 """ 

173 Test Checker when incorrect root directory is provided. 

174 """ 

175 try: 

176 Checker(metafile1, "fake") 

177 except FileNotFoundError: 

178 assert True 

179 

180 

181def test_checker_missing(sizedfiles, dir2): 

182 """ 

183 Test Checker class when files are missing from contents. 

184 """ 

185 count = 0 

186 for fd in Path(dir2).iterdir(): 

187 if fd.is_file() and count < 2: 

188 rmpath(fd) 

189 checker = Checker(sizedfiles, dir2) 

190 assert int(checker.results()) < 100 

191 

192 

193def test_checker_class_allfiles(sizedfiles, dir2): 

194 """ 

195 Test Checker class when all files are missing from contents. 

196 """ 

197 

198 def traverse(path): 

199 """ 

200 Traverse internal subdirectories. 

201 """ 

202 if path.is_file(): 

203 rmpath(path) 

204 elif path.is_dir(): 

205 for item in path.iterdir(): 

206 traverse(item) 

207 

208 traverse(dir2) 

209 checker = Checker(sizedfiles, dir2) 

210 assert int(checker.results()) < 100 

211 

212 

213def test_checker_class_allpaths(sizedfiles, dir2): 

214 """ 

215 Test Checker class when all files are missing from contents. 

216 """ 

217 for item in Path(str(dir2)).iterdir(): 

218 rmpath(item) 

219 checker = Checker(sizedfiles, dir2) 

220 assert int(checker.results()) < 100 

221 

222 

223def test_checker_class_half_file(filemeta2, file2): 

224 """ 

225 Test Checker class with half size single file. 

226 """ 

227 half = int(os.path.getsize(file2) / 2) 

228 barr = bytearray(half) 

229 with open(file2, "rb") as content: 

230 content.readinto(barr) 

231 with open(file2, "wb") as content: 

232 content.write(barr) 

233 checker = Checker(filemeta2, file2) 

234 assert int(checker.results()) != 10 

235 

236 

237def test_checker_missing_singles(dir2, sizedfiles): 

238 """ 

239 Test Checker class with half size single file. 

240 """ 

241 

242 def walk(root): 

243 """ 

244 Remove first file found. 

245 """ 

246 if root.is_file(): 

247 rmpath(root) 

248 return True 

249 if root.is_dir(): 

250 for item in root.iterdir(): 

251 walk(item) 

252 return False 

253 

254 walk(Path(dir2)) 

255 checker = Checker(sizedfiles, dir2) 

256 assert int(checker.results()) < 100 

257 

258 

259def test_checker_result_property(dir1, metafile1): 

260 """ 

261 Test Checker class with half size single file. 

262 """ 

263 checker = Checker(metafile1, dir1) 

264 result = checker.results() 

265 assert checker.results() == result 

266 

267 

268def test_checker_simplest(dir1, metafile1): 

269 """ 

270 Test the simplest example. 

271 """ 

272 checker = Checker(metafile1, dir1) 

273 assert checker.results() == 100 

274 

275 

276def test_checker_empty_files(dir2, sizedfiles): 

277 """ 

278 Test Checker when directory contains 0 length files. 

279 """ 

280 

281 def empty_files(root): 

282 """ 

283 Dump contents of files. 

284 """ 

285 if os.path.isfile(root): 

286 with open(root, "wb") as _: 

287 pass 

288 assert os.path.getsize(root) == 0 

289 elif os.path.isdir(root): 

290 for item in os.listdir(root): 

291 return empty_files(os.path.join(root, item)) 

292 return root 

293 

294 empty_files(dir2) 

295 checker = Checker(sizedfiles, dir2) 

296 assert checker.results() != 100 

297 

298 

299def test_recheck_wrong_dir(metafile1): 

300 """ 

301 Test recheck function with directory that doesn't contain the contents. 

302 """ 

303 grandparent = os.path.dirname(os.path.dirname(metafile1)) 

304 try: 

305 _ = Checker(metafile1, grandparent) 

306 except FileNotFoundError: 

307 assert True 

308 

309 

310def test_recheck_mismatch_args(metafile1): 

311 """ 

312 Test recheck function with mismatched directory. 

313 """ 

314 grandparent = os.path.dirname(os.path.dirname(metafile1)) 

315 try: 

316 _ = Checker(grandparent, metafile1) 

317 except ArgumentError: 

318 assert True