1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383
|
import os
import pytest
import torf
def test_wrong_info_type(generated_singlefile_torrent):
t = generated_singlefile_torrent
for typ in (bytearray, list, tuple):
t.metainfo['info'] = typ()
with pytest.raises(torf.MetainfoError) as excinfo:
t.validate()
assert str(excinfo.value) == (f"Invalid metainfo: ['info'] "
f"must be dict, not {typ.__qualname__}: {t.metainfo['info']}")
def test_length_and_files_in_info(generated_multifile_torrent):
t = generated_multifile_torrent
t.metainfo['info']['length'] = 123
with pytest.raises(torf.MetainfoError) as excinfo:
t.validate()
assert str(excinfo.value) == "Invalid metainfo: ['info'] includes both 'length' and 'files'"
def test_wrong_name_type(generated_singlefile_torrent):
t = generated_singlefile_torrent
t.metainfo['info']['name'] = 123
with pytest.raises(torf.MetainfoError) as excinfo:
t.validate()
assert str(excinfo.value) == ("Invalid metainfo: ['info']['name'] "
"must be str or bytes, not int: 123")
def test_wrong_piece_length_type(generated_singlefile_torrent):
t = generated_singlefile_torrent
t.metainfo['info']['piece length'] = [700]
with pytest.raises(torf.MetainfoError) as excinfo:
t.validate()
assert str(excinfo.value) == ("Invalid metainfo: ['info']['piece length'] "
"must be int, not list: [700]")
@pytest.mark.parametrize(
argnames='piece_length, exp_exception',
argvalues=(
(-1, torf.MetainfoError("['info']['piece length'] is invalid: -1")),
(0, torf.MetainfoError("['info']['piece length'] is invalid: 0")),
(16385, torf.MetainfoError("['info']['piece length'] is invalid: 16385")),
),
)
def test_piece_length_not_divisible_by_16_kib(piece_length, exp_exception, generated_singlefile_torrent):
t = generated_singlefile_torrent
t.metainfo['info']['piece length'] = piece_length
with pytest.raises(type(exp_exception)) as excinfo:
t.validate()
assert str(excinfo.value) == str(exp_exception)
def test_wrong_pieces_type(generated_singlefile_torrent):
t = generated_singlefile_torrent
t.metainfo['info']['pieces'] = 'many'
with pytest.raises(torf.MetainfoError) as excinfo:
t.validate()
assert str(excinfo.value) == ("Invalid metainfo: ['info']['pieces'] "
"must be bytes, not str: 'many'")
def test_pieces_is_empty(generated_singlefile_torrent):
t = generated_singlefile_torrent
t.metainfo['info']['pieces'] = bytes()
with pytest.raises(torf.MetainfoError) as excinfo:
t.validate()
assert str(excinfo.value) == "Invalid metainfo: ['info']['pieces'] is empty"
def test_invalid_number_of_bytes_in_pieces(generated_singlefile_torrent):
t = generated_singlefile_torrent
t.path = None
t.metainfo['info']['piece length'] = 512 * 1024
for i in range(1, 10):
t.metainfo['info']['length'] = i * t.metainfo['info']['piece length']
t.metainfo['info']['pieces'] = bytes(os.urandom(i * 20))
t.validate()
for j in ((i * 20) + 1, (i * 20) - 1):
t.metainfo['info']['pieces'] = bytes(os.urandom(j))
with pytest.raises(torf.MetainfoError) as excinfo:
t.validate()
assert str(excinfo.value) == ("Invalid metainfo: length of ['info']['pieces'] "
"is not divisible by 20")
def test_wrong_creation_date_type(generated_singlefile_torrent):
t = generated_singlefile_torrent
t.metainfo['creation date'] = 'hello'
with pytest.raises(torf.MetainfoError) as excinfo:
t.validate()
assert str(excinfo.value) == "Invalid metainfo: ['creation date'] must be int or datetime, not str: 'hello'"
def test_singlefile__unexpected_number_of_bytes_in_pieces(generated_singlefile_torrent):
t = generated_singlefile_torrent
t.path = None # Don't complain about wrong file size
t.metainfo['info']['length'] = 1024 * 1024
t.metainfo['info']['piece length'] = int(1024 * 1024 / 8)
t.metainfo['info']['pieces'] = os.urandom(20 * 9)
with pytest.raises(torf.MetainfoError) as excinfo:
t.validate()
assert str(excinfo.value) == 'Invalid metainfo: Expected 8 pieces but there are 9'
t.metainfo['info']['pieces'] = os.urandom(20 * 7)
with pytest.raises(torf.MetainfoError) as excinfo:
t.validate()
assert str(excinfo.value) == 'Invalid metainfo: Expected 8 pieces but there are 7'
def test_multifile__unexpected_number_of_bytes_in_pieces(generated_multifile_torrent):
t = generated_multifile_torrent
t.path = None # Don't complain about wrong file size
total_size = 0
for i,file in enumerate(t.metainfo['info']['files'], start=1):
file['length'] = 1024 * 1024 * i + 123
total_size += file['length']
import math
t.metainfo['info']['piece length'] = int(1024 * 1024 / 8)
piece_count = math.ceil(total_size / t.metainfo['info']['piece length'])
t.metainfo['info']['pieces'] = os.urandom(20 * (piece_count + 1))
with pytest.raises(torf.MetainfoError) as excinfo:
t.validate()
assert str(excinfo.value) == 'Invalid metainfo: Expected 49 pieces but there are 50'
t.metainfo['info']['pieces'] = os.urandom(20 * (piece_count - 1))
with pytest.raises(torf.MetainfoError) as excinfo:
t.validate()
assert str(excinfo.value) == 'Invalid metainfo: Expected 49 pieces but there are 48'
def test_no_announce_is_ok(generated_singlefile_torrent):
t = generated_singlefile_torrent
if 'announce' in t.metainfo:
del t.metainfo['announce']
t.validate()
def test_wrong_announce_type(generated_singlefile_torrent):
t = generated_singlefile_torrent
for typ in (bytearray, list, tuple):
t.metainfo['announce'] = typ()
with pytest.raises(torf.MetainfoError) as excinfo:
t.validate()
assert str(excinfo.value) == (f"Invalid metainfo: ['announce'] "
f"must be str, not {typ.__qualname__}: {t.metainfo['announce']}")
def test_invalid_announce_url(generated_singlefile_torrent):
t = generated_singlefile_torrent
for url in ('123', 'http://123:xxx/announce'):
t.metainfo['announce'] = url
with pytest.raises(torf.MetainfoError) as excinfo:
t.validate()
assert str(excinfo.value) == f"Invalid metainfo: ['announce'] is invalid: {url!r}"
def test_no_announce_list_is_ok(generated_singlefile_torrent):
t = generated_singlefile_torrent
if 'announce-list' in t.metainfo:
del t.metainfo['announce-list']
t.validate()
def test_wrong_announce_list_type(generated_singlefile_torrent):
t = generated_singlefile_torrent
# announce-list must be a list
for value in (3, 'foo', None, lambda: None):
t.metainfo['announce-list'] = value
with pytest.raises(torf.MetainfoError) as excinfo:
t.validate()
assert str(excinfo.value) == (f"Invalid metainfo: ['announce-list'] "
f"must be Iterable, not {type(value).__qualname__}: "
f"{t.metainfo['announce-list']!r}")
# Each item in announce-list must be a list
for tier in (3, 'foo', None, lambda: None):
for lst in ([tier],
[tier, []],
[[], tier],
[[], tier, []]):
t.metainfo['announce-list'] = lst
with pytest.raises(torf.MetainfoError) as excinfo:
t.validate()
tier_index = lst.index(tier)
assert str(excinfo.value) == (f"Invalid metainfo: ['announce-list'][{tier_index}] "
f"must be Iterable, not {type(tier).__qualname__}: {tier!r}")
# Each item in each list in announce-list must be a string
for typ in (bytearray, set):
url = typ()
for tier in ([url],
['http://localhost:123/', url],
[url, 'http://localhost:123/'],
['http://localhost:123/', url, 'http://localhost:456/']):
url_index = tier.index(url)
for lst in ([tier],
[tier, []],
[[], tier],
[[], tier, []]):
tier_index = lst.index(tier)
t.metainfo['announce-list'] = lst
with pytest.raises(torf.MetainfoError) as excinfo:
t.validate()
assert str(excinfo.value) == (f"Invalid metainfo: ['announce-list'][{tier_index}][{url_index}] "
f"must be str, not {typ.__qualname__}: {url!r}")
def test_invalid_url_in_announce_list(generated_singlefile_torrent):
t = generated_singlefile_torrent
for url in ('123', 'http://123:xxx/announce'):
for tier in ([url],
['http://localhost:123/', url],
[url, 'http://localhost:123/'],
['http://localhost:123/', url, 'http://localhost:456/']):
url_index = tier.index(url)
for lst in ([tier],
[tier, []],
[[], tier],
[[], tier, []]):
tier_index = lst.index(tier)
t.metainfo['announce-list'] = lst
with pytest.raises(torf.MetainfoError) as excinfo:
t.validate()
assert str(excinfo.value) == (f"Invalid metainfo: ['announce-list'][{tier_index}][{url_index}] "
f"is invalid: {url!r}")
def test_no_announce_and_no_announce_list_when_torrent_is_private(generated_singlefile_torrent):
t = generated_singlefile_torrent
t.metainfo['info']['private'] = True
if 'announce' in t.metainfo:
del t.metainfo['announce']
if 'announce-list' in t.metainfo:
del t.metainfo['announce-list']
t.validate()
assert t.generate() is True
assert t.is_ready is True
def test_singlefile_wrong_length_type(generated_singlefile_torrent):
t = generated_singlefile_torrent
t.metainfo['info']['length'] = 'foo'
with pytest.raises(torf.MetainfoError) as excinfo:
t.validate()
assert str(excinfo.value) == ("Invalid metainfo: ['info']['length'] "
"must be int or float, not str: 'foo'")
def test_singlefile_wrong_md5sum_type(generated_singlefile_torrent):
t = generated_singlefile_torrent
t.metainfo['info']['md5sum'] = 0
with pytest.raises(torf.MetainfoError) as excinfo:
t.validate()
assert str(excinfo.value) == ("Invalid metainfo: ['info']['md5sum'] "
"must be str, not int: 0")
t.metainfo['info']['md5sum'] = 'Z8b329da9893e34099c7d8ad5cb9c940'
with pytest.raises(torf.MetainfoError) as excinfo:
t.validate()
assert str(excinfo.value) == ("Invalid metainfo: ['info']['md5sum'] is invalid: "
"'Z8b329da9893e34099c7d8ad5cb9c940'")
def test_multifile_wrong_files_type(generated_multifile_torrent):
t = generated_multifile_torrent
t._path = None
t.metainfo['info']['files'] = 'foo'
with pytest.raises(torf.MetainfoError) as excinfo:
t.validate()
assert str(excinfo.value) == ("Invalid metainfo: ['info']['files'] "
"must be Iterable, not str: 'foo'")
def test_multifile_wrong_path_type(generated_multifile_torrent):
t = generated_multifile_torrent
t._path = None
t.metainfo['info']['files'][0]['path'] = 'foo/bar/baz'
with pytest.raises(torf.MetainfoError) as excinfo:
t.validate()
assert str(excinfo.value) == ("Invalid metainfo: ['info']['files'][0]['path'] "
"must be Iterable, not str: 'foo/bar/baz'")
def test_multifile_wrong_path_item_type(generated_multifile_torrent):
t = generated_multifile_torrent
t._path = None
t.metainfo['info']['files'][1]['path'][0] = 17
with pytest.raises(torf.MetainfoError) as excinfo:
t.validate()
assert str(excinfo.value) == ("Invalid metainfo: ['info']['files'][1]['path'][0] "
"must be str or bytes, not int: 17")
def test_multifile_wrong_length_type(generated_multifile_torrent):
t = generated_multifile_torrent
t._path = None
t.metainfo['info']['files'][2]['length'] = ['this', 'is', 'not', 'a', 'length']
with pytest.raises(torf.MetainfoError) as excinfo:
t.validate()
assert str(excinfo.value) == ("Invalid metainfo: ['info']['files'][2]['length'] "
"must be int or float, not list: ['this', 'is', 'not', 'a', 'length']")
def test_multifile_wrong_md5sum_type(generated_multifile_torrent):
t = generated_multifile_torrent
t.metainfo['info']['files'][0]['md5sum'] = 0
with pytest.raises(torf.MetainfoError) as excinfo:
t.validate()
assert str(excinfo.value) == ("Invalid metainfo: ['info']['files'][0]['md5sum'] "
"must be str, not int: 0")
t.metainfo['info']['files'][0]['md5sum'] = 'Z8b329da9893e34099c7d8ad5cb9c940'
with pytest.raises(torf.MetainfoError) as excinfo:
t.validate()
assert str(excinfo.value) == ("Invalid metainfo: ['info']['files'][0]['md5sum'] is invalid: "
"'Z8b329da9893e34099c7d8ad5cb9c940'")
def assert_missing_metainfo(torrent, *keys):
md = torrent.metainfo
for key in keys[:-1]:
md = md[key]
del md[keys[-1]]
with pytest.raises(torf.MetainfoError) as excinfo:
torrent.validate()
assert excinfo.match(rf"Invalid metainfo: Missing {keys[-1]!r} in \['info'\]")
def test_singlefile_missing_info_path(generated_singlefile_torrent):
assert_missing_metainfo(generated_singlefile_torrent, 'info', 'name')
def test_singlefile_missing_info_piece_length(generated_singlefile_torrent):
assert_missing_metainfo(generated_singlefile_torrent, 'info', 'piece length')
def test_singlefile_missing_info_pieces(generated_singlefile_torrent):
assert_missing_metainfo(generated_singlefile_torrent, 'info', 'pieces')
def test_multifile_missing_info_path(generated_multifile_torrent):
assert_missing_metainfo(generated_multifile_torrent, 'info', 'name')
def test_multifile_missing_info_piece_length(generated_multifile_torrent):
assert_missing_metainfo(generated_multifile_torrent, 'info', 'piece length')
def test_multifile_missing_info_pieces(generated_multifile_torrent):
assert_missing_metainfo(generated_multifile_torrent, 'info', 'pieces')
def test_multifile_missing_info_files_0_length(generated_multifile_torrent):
assert_missing_metainfo(generated_multifile_torrent, 'info', 'files', 0, 'length')
def test_multifile_missing_info_files_1_length(generated_multifile_torrent):
assert_missing_metainfo(generated_multifile_torrent, 'info', 'files', 1, 'length')
def test_multifile_missing_info_files_1_path(generated_multifile_torrent):
assert_missing_metainfo(generated_multifile_torrent, 'info', 'files', 1, 'path')
def test_multifile_missing_info_files_2_path(generated_multifile_torrent):
assert_missing_metainfo(generated_multifile_torrent, 'info', 'files', 2, 'path')
def assert_mismatching_filesizes(torrent):
torrent.validate() # Should validate
for torrent_path, fs_path in zip(torrent.files, torrent.filepaths):
# Remember file content
with open(fs_path, 'rb') as f:
orig_fs_path_content = f.read()
# Change file size
with open(fs_path, 'ab') as f:
f.write(b'foo')
# Expect validation error
mi_size = torrent.partial_size(torrent_path)
fs_size = os.path.getsize(fs_path)
assert fs_size == mi_size + len('foo')
with pytest.raises(torf.MetainfoError) as excinfo:
torrent.validate()
assert str(excinfo.value) == (f'Invalid metainfo: Mismatching file sizes in metainfo ({mi_size}) '
f'and file system ({fs_size}): {fs_path}')
# Restore original file content
with open(fs_path, 'wb') as f:
f.write(orig_fs_path_content)
torrent.validate() # Should validate again
def test_singlefile_mismatching_filesize(generated_singlefile_torrent):
assert_mismatching_filesizes(generated_singlefile_torrent)
def test_multifile_mismatching_filesize(generated_multifile_torrent):
assert_mismatching_filesizes(generated_multifile_torrent)
|