Skip to content

Commit

Permalink
Merge pull request Tribler#21 from boudewijn-tribler/test-cleanup
Browse files Browse the repository at this point in the history
Fixes magnetlink unittest.
  • Loading branch information
Elric Milon committed May 7, 2013
2 parents 8ea7ca4 + 6ba52ec commit 0dbc086
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 126 deletions.
13 changes: 10 additions & 3 deletions Tribler/Core/Utilities/bencode.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,16 +83,23 @@ def decode_dict(x, f):
#decode_func['u'] = decode_unicode

def bdecode(x, sloppy = 0):
r, l = sloppy_bdecode(x)
if not sloppy and l != len(x):
raise ValueError, "bad bencoded data"
return r

def sloppy_bdecode(x):
"""
Same as bdecode, except that it returns the decoded data AND the number of bytes read from X.
"""
try:
r, l = decode_func[x[0]](x, 0)
# except (IndexError, KeyError):
except (IndexError, KeyError, ValueError):
if DEBUG:
print_exc()
raise ValueError, "bad bencoded data"
if not sloppy and l != len(x):
raise ValueError, "bad bencoded data"
return r
return r, l

def test_bdecode():
try:
Expand Down
76 changes: 43 additions & 33 deletions Tribler/Test/test_magnetlink.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
from btconn import BTConnection
from Tribler.Core.TorrentDef import TorrentDef
from Tribler.Core.DownloadConfig import DownloadStartupConfig
from Tribler.Core.Utilities.bencode import bencode, bdecode
from Tribler.Core.Utilities.bencode import bencode, bdecode, sloppy_bdecode
from Tribler.Core.MessageID import EXTEND
from Tribler.Core.simpledefs import dlstatus_strings, DLSTATUS_SEEDING
from Tribler.Core.DecentralizedTracking.MagnetLink.MagnetLink import MagnetHandler
Expand Down Expand Up @@ -54,24 +54,24 @@ def metadata_id_from_extend_handshake(self, data):
return val

def read_extend_handshake(self, conn):
responce = conn.recv()
self.assert_(len(responce) > 0)
# print >>sys.stderr,"test: Got reply", getMessageName(responce[0])
self.assert_(responce[0] == EXTEND)
return self.metadata_id_from_extend_handshake(responce[1:])
response = conn.recv()
self.assert_(len(response) > 0)
# print >>sys.stderr,"test: Got reply", getMessageName(response[0])
self.assert_(response[0] == EXTEND)
return self.metadata_id_from_extend_handshake(response[1:])

def read_extend_metadata_request(self, conn):
while True:
responce = conn.recv()
assert len(responce) > 0
# print >>sys.stderr,"test: Got data", getMessageName(responce[0])
if responce[0] == EXTEND:
response = conn.recv()
assert len(response) > 0
# print >>sys.stderr,"test: Got data", getMessageName(response[0])
if response[0] == EXTEND:
break

assert responce[0] == EXTEND
assert ord(responce[1]) == 42
assert response[0] == EXTEND
assert ord(response[1]) == 42

payload = bdecode(responce[2:])
payload = bdecode(response[2:])
assert "msg_type" in payload
assert payload["msg_type"] == 0
assert "piece" in payload
Expand All @@ -83,36 +83,46 @@ def read_extend_metadata_reply(self, conn, piece):
while True:
response = conn.recv()
assert len(response) > 0
# print >>sys.stderr,"test: Got data", getMessageName(responce[0])
# print >>sys.stderr,"test: Got data", getMessageName(response[0])
if response[0] == EXTEND:
break

assert response[0] == EXTEND
assert ord(response[1]) == 42

try:
payload = bdecode(response[2:])
assert payload["msg_type"] == 1
assert payload["piece"] == piece
payload, length = sloppy_bdecode(response[2:])
assert payload["msg_type"] == 1
assert payload["piece"] == piece
if "data" in payload:
assert payload["data"] == self.metadata_list[piece]
except:
print_exc()
print >> sys.stderr, response[2:]
else:
assert response[2 + length:] == self.metadata_list[piece]

def read_extend_metadata_reject(self, conn, piece):
while True:
responce = conn.recv()
assert len(responce) > 0
# print >>sys.stderr,"test: Got reject", getMessageName(responce[0])
if responce[0] == EXTEND:
response = conn.recv()
assert len(response) > 0
# print >>sys.stderr,"test: Got reject", getMessageName(response[0])
if response[0] == EXTEND:
break

assert responce[0] == EXTEND
assert ord(responce[1]) == 42
assert response[0] == EXTEND
assert ord(response[1]) == 42

payload, length = sloppy_bdecode(response[2:])
assert payload["msg_type"] in (1, 2), [payload, response[2:2 + length]]
assert payload["piece"] == piece, [payload, response[2:2 + length]]

payload = bdecode(responce[2:])
assert payload["msg_type"] == 2
assert payload["piece"] == piece
# some clients return msg_type 1, unfortunately this is not a reject but a proper response.
# instead libtorrent warns: max outstanding piece requests reached
if payload["msg_type"] == 1:
assert response[2 + length:] == self.metadata_list[piece]

# some clients return msg_type 2, we must make sure no "data" is given (i.e. the request was
# rejected)
if payload["msg_type"] == 2:
assert payload["piece"] == piece, [payload, response[2:2 + length]]
assert not "data" in payload, [payload, response[2:2 + length]]

def read_extend_metadata_close(self, conn):
"""
Expand All @@ -121,10 +131,10 @@ def read_extend_metadata_close(self, conn):
"""
conn.s.settimeout(10.0)
while True:
responce = conn.recv()
if len(responce) == 0:
response = conn.recv()
if len(response) == 0:
break
assert not (responce[0] == EXTEND and responce[1] == 42)
assert not (response[0] == EXTEND and response[1] == 42)

class TestMagnetMiniBitTorrent(TestAsServer, MagnetHelpers):
"""
Expand Down
90 changes: 0 additions & 90 deletions dispersy-test

This file was deleted.

0 comments on commit 0dbc086

Please sign in to comment.