forked from amcjen/OpenBazaar
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Create and test a Contact class for the DHT.
- Loading branch information
Showing
4 changed files
with
91 additions
and
0 deletions.
There are no files selected for viewing
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import collections | ||
|
||
|
||
class Contact(collections.Hashable): | ||
""" | ||
A node in the DHT. | ||
Contains all information specified by the Kademlia protocol | ||
(aka: IP, PORT, GUID). | ||
""" | ||
def __init__(self, ip, port, guid): | ||
""" | ||
Make a new Contact. | ||
Args: | ||
ip: The IP of the Contact as a string or unicode. It may | ||
be an IPv4 or an IPv6. | ||
port: The port of the Contact as an integer | ||
guid: The unique ID of the Contact as a hex string of | ||
proper length (see constants module) | ||
""" | ||
self.ip = ip | ||
self.port = port | ||
self.guid = guid | ||
|
||
def __eq__(self, other): | ||
if isinstance(other, self.__class__): | ||
return self.guid == other.guid | ||
return NotImplemented | ||
|
||
def __hash__(self): | ||
return hash(self.guid) | ||
|
||
def __repr__(self): | ||
return 'Contact({0}, {1}, {2})'.format(self.ip, self.port, self.guid) |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
import unittest | ||
|
||
from dht import contact | ||
|
||
|
||
class TestContact(unittest.TestCase): | ||
""" | ||
Test the API of dht.contact. | ||
""" | ||
@classmethod | ||
def setUpClass(cls): | ||
cls.guid1 = '1' * 40 | ||
cls.guid2 = 'f' * 40 | ||
cls.ipv4 = '123.45.67.89' | ||
cls.ipv6 = '2001:db8:85a3::8a2e:370:7334' | ||
cls.port1 = 12345 | ||
cls.port2 = 12346 | ||
|
||
def _test_init_scenario(self, ip, port, guid): | ||
contact1 = contact.Contact(ip, port, guid) | ||
self.assertEqual(contact1.ip, ip) | ||
self.assertEqual(contact1.port, port) | ||
self.assertEqual(contact1.guid, guid) | ||
|
||
def test_init_classic(self): | ||
self._test_init_scenario(self.ipv4, self.port1, self.guid1) | ||
|
||
def test_init_unicode(self): | ||
self._test_init_scenario(self.ipv4, self.port1, unicode(self.guid1)) | ||
|
||
def test_init_ipv6(self): | ||
self._test_init_scenario(self.ipv6, self.port1, self.guid1) | ||
|
||
def test_eq_hash(self): | ||
c1 = contact.Contact(self.ipv4, self.port1, self.guid1) | ||
c2 = contact.Contact(self.ipv6, self.port2, self.guid1) | ||
self.assertIsNot(c1, c2) | ||
self.assertEqual(c1, c2) | ||
self.assertEqual(hash(c1), hash(c2)) | ||
|
||
def test_uneq(self): | ||
c1 = contact.Contact(self.ipv4, self.port1, self.guid1) | ||
c2 = contact.Contact(self.ipv4, self.port1, self.guid2) | ||
self.assertNotEqual(c1, c2) | ||
|
||
def test_repr(self): | ||
c1 = contact.Contact(self.ipv4, self.port1, self.guid1) | ||
cr = repr(c1) | ||
self.assertEqual( | ||
cr, | ||
'Contact({0}, {1}, {2})'.format(self.ipv4, self.port1, self.guid1) | ||
) | ||
|
||
|
||
if __name__ == "__main__": | ||
unittest.main() |