Skip to content

Commit eb19a4a

Browse files
committed
Add tests for decoding as OrderedDict
1 parent 65b12d4 commit eb19a4a

File tree

2 files changed

+31
-7
lines changed

2 files changed

+31
-7
lines changed

tests/test_decode.py

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
11
# -*- coding: utf-8 -*-
22

33
from bencoder import bdecode
4+
import os
5+
6+
TORRENT_PATH = os.path.join(
7+
os.path.abspath(os.path.dirname(__file__)),
8+
"debian-8.3.0-amd64-netinst.iso.torrent"
9+
)
410

511

612
def test_decode_str():
@@ -22,6 +28,15 @@ def test_decode_dict():
2228
assert bdecode(b'd2:ka2:va2:kbi2ee') == od
2329

2430

31+
def test_ordered_dict():
32+
from bencoder import OrderedDict
33+
rv = bdecode(b'd2:ka2:va2:kbi2ee')
34+
assert isinstance(rv, OrderedDict)
35+
assert list(rv.keys()) == [b'ka', b'kb']
36+
assert list(bdecode(b'd2:kc2:va2:kei2ee').keys()) == [b'kc', b'ke']
37+
assert list(bdecode(b'd2:ke2:va2:kci2ee').keys()) == [b'ke', b'kc']
38+
39+
2540
def test_encode_complex():
2641
od = dict()
2742
od[b'KeyA'] = [b'listitemA', {b'k': b'v'}, 3]
@@ -33,12 +48,7 @@ def test_encode_complex():
3348

3449

3550
def test_decode_debian_torrent():
36-
import os
37-
torrent_path = os.path.join(
38-
os.path.abspath(os.path.dirname(__file__)),
39-
"debian-8.3.0-amd64-netinst.iso.torrent"
40-
)
41-
with open(torrent_path, "rb") as f:
51+
with open(TORRENT_PATH, "rb") as f:
4252
torrent = bdecode(f.read())
4353
assert torrent[b'announce'] == b'http://bttracker.debian.org:6969/announce'
4454
assert torrent[b'comment'] == b'"Debian CD from cdimage.debian.org"'

tests/test_encode.py

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
11
# -*- coding: utf-8 -*-
22

3-
from bencoder import bencode
3+
from bencoder import bencode, bdecode
4+
import os
5+
6+
TORRENT_PATH = os.path.join(
7+
os.path.abspath(os.path.dirname(__file__)),
8+
"debian-8.3.0-amd64-netinst.iso.torrent"
9+
)
410

511

612
def test_encode_str():
@@ -41,3 +47,11 @@ def test_encode_complex():
4147
od['KeyD'] = 'AString'
4248
expected_result = b'd4:KeyAl9:listitemAd1:k1:vei3ee4:KeyBd1:k1:ve4:KeyCi3e4:KeyD7:AStringe'
4349
assert bencode(od) == expected_result
50+
51+
52+
def test_infohash():
53+
import hashlib
54+
with open(TORRENT_PATH, "rb") as f:
55+
torrent = bdecode(f.read())
56+
infohash = hashlib.sha1(bencode(torrent[b'info'])).hexdigest()
57+
assert infohash == "4194e473d6c49630e1c6247d6716076809bb96ae"

0 commit comments

Comments
 (0)