|
|
@@ -11,12 +11,14 @@ import sys |
|
|
|
import tempfile |
|
|
|
import unittest |
|
|
|
|
|
|
|
_encoding = 'utf-8' |
|
|
|
|
|
|
|
class Storage: |
|
|
|
def __init__(self, rootpath, files, piecelen, encoding='us-ascii'): |
|
|
|
|
|
|
|
def __init__(self, rootpath, files, piecelen): |
|
|
|
self._rootpath = pathlib.Path(rootpath) |
|
|
|
self._files = files |
|
|
|
self._piecelen = piecelen |
|
|
|
self._encoding = encoding |
|
|
|
|
|
|
|
self._buildindex() |
|
|
|
|
|
|
@@ -31,7 +33,7 @@ class Storage: |
|
|
|
|
|
|
|
for curfile in self._files: |
|
|
|
fname = pathlib.PurePath( |
|
|
|
*(x.decode(self._encoding) for x in |
|
|
|
*(x.decode(_encoding) for x in |
|
|
|
curfile['path'])) |
|
|
|
curfilepath = self._rootpath / fname |
|
|
|
|
|
|
@@ -90,15 +92,9 @@ def validate(torrent, basedir): |
|
|
|
|
|
|
|
basedir = pathlib.Path(basedir) |
|
|
|
|
|
|
|
try: |
|
|
|
encoding = torrent['encoding'].decode('us-ascii') |
|
|
|
except KeyError: |
|
|
|
encoding = 'us-ascii' |
|
|
|
torrentdir = basedir / info['name'].decode(_encoding) |
|
|
|
|
|
|
|
torrentdir = basedir / info['name'].decode(encoding) |
|
|
|
|
|
|
|
stor = Storage(torrentdir, info['files'], info['piece length'], |
|
|
|
encoding) |
|
|
|
stor = Storage(torrentdir, info['files'], info['piece length']) |
|
|
|
|
|
|
|
pieces = info['pieces'] |
|
|
|
piecescnt = len(pieces) // 20 |
|
|
@@ -124,6 +120,8 @@ def validate(torrent, basedir): |
|
|
|
|
|
|
|
class _TestCases(unittest.TestCase): |
|
|
|
dirname = 'somedir' |
|
|
|
|
|
|
|
# file contents for somedir.torrent |
|
|
|
origfiledata = { |
|
|
|
'filea.txt': b'foo\n', |
|
|
|
'fileb.txt': b'bar\n', |
|
|
@@ -133,6 +131,13 @@ class _TestCases(unittest.TestCase): |
|
|
|
'filef/filef.txt': b'\n', |
|
|
|
} |
|
|
|
|
|
|
|
# some munging to make some files bad |
|
|
|
badfiles = { |
|
|
|
'filea.txt': b'', |
|
|
|
'filec.txt': b'\x00\x00\x00\x00a\n', |
|
|
|
'filee.txt': b'no', |
|
|
|
} |
|
|
|
|
|
|
|
def setUp(self): |
|
|
|
d = pathlib.Path(tempfile.mkdtemp()).resolve() |
|
|
|
|
|
|
@@ -196,13 +201,7 @@ class _TestCases(unittest.TestCase): |
|
|
|
|
|
|
|
missingfiles = self.origfiledata.copy() |
|
|
|
|
|
|
|
badfiles = { |
|
|
|
'filea.txt': b'', |
|
|
|
'filec.txt': b'\x00\x00\x00\x00a\n', |
|
|
|
'filee.txt': b'no', |
|
|
|
} |
|
|
|
|
|
|
|
missingfiles.update(badfiles) |
|
|
|
missingfiles.update(self.badfiles) |
|
|
|
|
|
|
|
sd = self.basetempdir / self.dirname |
|
|
|
sd.mkdir() |
|
|
@@ -212,6 +211,6 @@ class _TestCases(unittest.TestCase): |
|
|
|
val, inval = validate(self.torrent, self.basetempdir) |
|
|
|
|
|
|
|
self.assertEqual(set(val), { sd / x for x in |
|
|
|
missingfiles.keys() if x not in badfiles }) |
|
|
|
missingfiles.keys() if x not in self.badfiles }) |
|
|
|
self.assertEqual(set(inval), { sd / x for x in |
|
|
|
badfiles.keys() }) |
|
|
|
self.badfiles.keys() }) |