test_node green on py3

This commit is contained in:
Victor Shyba 2018-07-17 21:57:02 -03:00 committed by Jack Robison
parent 5b35c4e8f0
commit e4ea1ccbfb
No known key found for this signature in database
GPG key ID: DF25C68FE0239BB2
4 changed files with 15 additions and 10 deletions

View file

@ -45,7 +45,7 @@ class DictDataStore(UserDict):
self._dict[key] = unexpired_peers
def hasPeersForBlob(self, key):
return True if key in self._dict and len(self.filter_bad_and_expired_peers(key)) else False
return True if key in self._dict and len(tuple(self.filter_bad_and_expired_peers(key))) else False
def addPeerToBlob(self, contact, key, compact_address, lastPublished, originallyPublished, originalPublisherID):
if key in self._dict:

View file

@ -1,7 +1,12 @@
import logging
from binascii import hexlify
from . import constants
from .distance import Distance
from .error import BucketFull
import sys
if sys.version_info > (3,):
long = int
log = logging.getLogger(__name__)
@ -135,8 +140,8 @@ class KBucket(object):
if not.
@rtype: bool
"""
if isinstance(key, str):
key = long(key.encode('hex'), 16)
if isinstance(key, bytes):
key = long(hexlify(key), 16)
return self.rangeMin <= key < self.rangeMax
def __len__(self):

View file

@ -528,7 +528,7 @@ class Node(MockKademliaHelper):
elif not self.verify_token(token, compact_ip):
raise ValueError("Invalid token")
if 0 <= port <= 65536:
compact_port = str(struct.pack('>H', port))
compact_port = struct.pack('>H', port)
else:
raise TypeError('Invalid port')
compact_address = compact_ip + compact_port + rpc_contact.id

View file

@ -20,7 +20,7 @@ class NodeIDTest(unittest.TestCase):
def testAutoCreatedID(self):
""" Tests if a new node has a valid node ID """
self.failUnlessEqual(type(self.node.node_id), str, 'Node does not have a valid ID')
self.failUnlessEqual(type(self.node.node_id), bytes, 'Node does not have a valid ID')
self.failUnlessEqual(len(self.node.node_id), 48, 'Node ID length is incorrect! '
'Expected 384 bits, got %d bits.' %
(len(self.node.node_id) * 8))
@ -48,13 +48,13 @@ class NodeDataTest(unittest.TestCase):
""" Test case for the Node class's data-related functions """
def setUp(self):
h = hashlib.sha384()
h.update('test')
h.update(b'test')
self.node = Node()
self.contact = self.node.contact_manager.make_contact(h.digest(), '127.0.0.1', 12345, self.node._protocol)
self.token = self.node.make_token(self.contact.compact_ip())
self.cases = []
for i in xrange(5):
h.update(str(i))
for i in range(5):
h.update(str(i).encode())
self.cases.append((h.digest(), 5000+2*i))
self.cases.append((h.digest(), 5001+2*i))
@ -66,7 +66,7 @@ class NodeDataTest(unittest.TestCase):
self.contact, key, self.token, port, self.contact.id, 0
)
for key, value in self.cases:
expected_result = self.contact.compact_ip() + str(struct.pack('>H', value)) + \
expected_result = self.contact.compact_ip() + struct.pack('>H', value) + \
self.contact.id
self.failUnless(self.node._dataStore.hasPeersForBlob(key),
'Stored key not found in node\'s DataStore: "%s"' % key)
@ -85,7 +85,7 @@ class NodeContactTest(unittest.TestCase):
""" Tests if a contact can be added and retrieved correctly """
# Create the contact
h = hashlib.sha384()
h.update('node1')
h.update(b'node1')
contactID = h.digest()
contact = self.node.contact_manager.make_contact(contactID, '127.0.0.1', 9182, self.node._protocol)
# Now add it...