hash_fields(finish #4) and hash raw:

- hash fields method and parameter allow user customize hash field list
- hash raw parameter allow the output of hash field to be raw bytes
- BEncoder now support encode raw bytes
This commit is contained in:
7sDream committed 2018-06-23 12:57:04 +08:00
1 parent ee3128b32b
commit 177d1c7de9
6 files changed
+224 -57

No files matched your search

+2
View File
@@ -3,3 +3,5 @@ from .test_parse import *
from .test_encoding_error import *
from .test_encode import *
from .test_decode import *
from .test_hash_field import *
from .test_hash_raw import *
+21
View File
@@ -0,0 +1,21 @@
from __future__ import unicode_literals
import os.path
import unittest
from torrent_parser import (
TorrentFileParser, parse_torrent_file, decode
)
class TestHashField(unittest.TestCase):
TEST_FILES_DIR = os.path.join(os.path.dirname(__file__), 'test_files')
FILE = os.path.join(TEST_FILES_DIR, 'utf8.encoding.error.torrent')
def test_not_raise_exception_when_add_hash_fields(self):
parse_torrent_file(self.FILE, hash_fields={'info_hash': (20, False)})
with open(self.FILE, 'rb') as f:
TorrentFileParser(f).hash_field('info_hash').parse()
with open(self.FILE, 'rb') as f:
data = f.read()
decode(data, hash_fields={'info_hash': (20, False)})
+23
View File
@@ -0,0 +1,23 @@
from __future__ import unicode_literals
import os.path
import unittest
from torrent_parser import decode, encode
class TestHashRaw(unittest.TestCase):
TEST_FILES_DIR = os.path.join(os.path.dirname(__file__), 'test_files')
FILE = os.path.join(TEST_FILES_DIR, 'utf8.encoding.error.torrent')
def test_hash_raw_decode(self):
data = b'd4:hash4:\xAA\xBB\xCC\xDDe'
res = decode(data, hash_fields={'hash': (4, False)}, hash_raw=False)
self.assertEqual(res['hash'], 'aabbccdd')
res = decode(data, hash_fields={'hash': (4, False)}, hash_raw=True)
self.assertEqual(res['hash'], b'\xAA\xBB\xCC\xDD')
def test_raw_bytes_encode(self):
res = {'hash': b'\xAA\xBB\xCC\xDD'}
data = encode(res)
self.assertEqual(data, b'd4:hash4:\xAA\xBB\xCC\xDDe')