def test_tools_error(self): c_nodes = mt.compact_nodes(tc.NODES) # Compact nodes is one byte short # assert_raises(m.MsgError, mt.uncompact_nodes, c_nodes[:-1]) # IP size is weird # assert_raises(m.MsgError, mt.bin_to_ip, '123') # Port is 0 ( eq_(mt.uncompact_nodes(c_nodes), tc.NODES) n = tc.NODES[0] tc.NODES[0] = node.Node((n.addr[0], 0), n.id) c_nodes = mt.compact_nodes(tc.NODES) eq_(mt.uncompact_nodes(c_nodes), tc.NODES[1:]) c_nodes2 = mt.compact_nodes2(tc.NODES) eq_(mt.uncompact_nodes2(c_nodes2), tc.NODES[1:]) tc.NODES[0] = n
def __init__(self, sender_id, token = None, nodes = None, peers = None): OutgoingResponseBase.__init__(self, sender_id) if token: self._dict[RESPONSE][TOKEN] = token if nodes: self._dict[RESPONSE][NODES] = mt.compact_nodes(nodes) if peers: self._dict[RESPONSE][VALUES] = mt.compact_peers(peers)
def get_peers_response(self, token, nodes, peers): assert nodes or peers if token: self._dict[RESPONSE][TOKEN] = token if nodes: self._dict[RESPONSE][NODES] = mt.compact_nodes(nodes) if peers: self._dict[RESPONSE][VALUES] = mt.compact_peers(peers)
def __init__(self, sender_id, token=None, nodes=None, peers=None): OutgoingResponseBase.__init__(self, sender_id) if token: self._dict[RESPONSE][TOKEN] = token if nodes: self._dict[RESPONSE][NODES] = mt.compact_nodes(nodes) if peers: self._dict[RESPONSE][VALUES] = mt.compact_peers(peers)
def test_tools_error(self): c_nodes = mt.compact_nodes(tc.NODES) # Compact nodes is one byte short eq_(mt.uncompact_nodes(c_nodes[:-1]), []) # Port is 0 ( eq_(mt.uncompact_nodes(c_nodes), tc.NODES) nodes = [n for n in tc.NODES] # One address has port ZERO nodes[0] = node.Node((nodes[0].addr[0], 0), nodes[0].id) peers = [n.addr for n in nodes] c_nodes = mt.compact_nodes(nodes) eq_(mt.uncompact_nodes(c_nodes), nodes[1:]) c_nodes2 = mt.compact_nodes2(nodes) eq_(mt.uncompact_nodes2(c_nodes2), nodes[1:]) c_peers = mt.compact_peers(peers) eq_(mt.uncompact_peers(c_peers), peers[1:])
def test_sanitize(self): self.ping_r.sanitize_response(m.PING) del self.fn2_r._msg_dict[m.RESPONSE][m.NODES2] # No NODES and no NODES2 assert_raises(m.MsgError, self.fn2_r.sanitize_response, m.FIND_NODE) self.fn2_r._msg_dict[m.RESPONSE][m.NODES] = \ mt.compact_nodes(tc.NODES) # Just NODES self.fn2_r.sanitize_response(m.FIND_NODE) self.fn2_r._msg_dict[m.RESPONSE][m.NODES2] = \ mt.compact_nodes2(tc.NODES) # Both NODES and NODES2 self.fn2_r.sanitize_response(m.FIND_NODE) # Both NODES and PEERS in response self.gp_r._msg_dict[m.RESPONSE][m.NODES] = \ mt.compact_nodes(tc.NODES) self.gp_r.sanitize_response(m.GET_PEERS) # No NODES and no PEERS del self.gp_r._msg_dict[m.RESPONSE][m.NODES] del self.gp_r._msg_dict[m.RESPONSE][m.VALUES] assert_raises(m.MsgError, self.gp_r.sanitize_response, m.GET_PEERS)
def _test_sanitize(self): self.ping_r.sanitize_response(m.PING) del self.fn2_r._msg_dict[m.RESPONSE][m.NODES2] # No NODES and no NODES2 assert_raises(m.MsgError, self.fn2_r.sanitize_response, m.FIND_NODE) self.fn2_r._msg_dict[m.RESPONSE][m.NODES] = \ mt.compact_nodes(tc.NODES) # Just NODES self.fn2_r.sanitize_response(m.FIND_NODE) self.fn2_r._msg_dict[m.RESPONSE][m.NODES2] = \ mt.compact_nodes2(tc.NODES) # Both NODES and NODES2 self.fn2_r.sanitize_response(m.FIND_NODE) # Both NODES and PEERS in response self.gp_r._msg_dict[m.RESPONSE][m.NODES] = \ mt.compact_nodes(tc.NODES) self.gp_r.sanitize_response(m.GET_PEERS) # No NODES and no PEERS del self.gp_r._msg_dict[m.RESPONSE][m.NODES] del self.gp_r._msg_dict[m.RESPONSE][m.VALUES] assert_raises(m.MsgError, self.gp_r.sanitize_response, m.GET_PEERS)
def test_tools_error(self): c_nodes = mt.compact_nodes(tc.NODES) # Compact nodes is one byte short eq_(mt.uncompact_nodes(c_nodes[:-1]), []) # Port is 0 ( eq_(mt.uncompact_nodes(c_nodes), tc.NODES) nodes = [n for n in tc.NODES] # One address has port ZERO nodes[0] = node.Node((nodes[0].addr[0], 0), nodes[0].id) peers = [n.addr for n in nodes] c_nodes = mt.compact_nodes(nodes) eq_(mt.uncompact_nodes(c_nodes), nodes[1:]) c_nodes2 = mt.compact_nodes2(nodes) eq_(mt.uncompact_nodes2(c_nodes2), nodes[1:]) c_peers = mt.compact_peers(peers) eq_(mt.uncompact_peers(c_peers), peers[1:]) addr = ('1.2.3.4', 1234) c_addr = mt.compact_addr(addr) assert_raises(mt.AddrError, mt.uncompact_addr, c_addr[:-1]) assert_raises(mt.AddrError, mt.uncompact_addr, c_addr[1:]) assert_raises(mt.AddrError, mt.uncompact_addr, c_addr+'X')
def test_tools_error(self): c_nodes = mt.compact_nodes(tc.NODES) # Compact nodes is one byte short eq_(mt.uncompact_nodes(c_nodes[:-1]), []) # Port is 0 ( eq_(mt.uncompact_nodes(c_nodes), tc.NODES) nodes = [n for n in tc.NODES] # One address has port ZERO nodes[0] = node.Node((nodes[0].addr[0], 0), nodes[0].id) peers = [n.addr for n in nodes] c_nodes = mt.compact_nodes(nodes) eq_(mt.uncompact_nodes(c_nodes), nodes[1:]) c_nodes2 = mt.compact_nodes2(nodes) eq_(mt.uncompact_nodes2(c_nodes2), nodes[1:]) c_peers = mt.compact_peers(peers) eq_(mt.uncompact_peers(c_peers), peers[1:]) addr = ('1.2.3.4', 1234) c_addr = mt.compact_addr(addr) assert_raises(mt.AddrError, mt.uncompact_addr, c_addr[:-1]) assert_raises(mt.AddrError, mt.uncompact_addr, c_addr[1:]) assert_raises(mt.AddrError, mt.uncompact_addr, c_addr + 'X')
def test_tools(self): bin_strs = ['23', '\1\5', 'a\3'] for bs in bin_strs: i = mt.bin_to_int(bs) bs2 = mt.int_to_bin(i) logger.debug('bs: %s, bin_to_int(bs): %d, bs2: %s' % (bs, i, bs2)) assert bs == bs2 ips = ['128.0.0.1', '222.222.222.222', '1.2.3.4'] ports = [12345, 99, 54321] for addr in zip(ips, ports): c_addr = mt.compact_addr(addr) addr2 = mt.uncompact_addr(c_addr) assert addr == addr2 c_peers = mt.compact_peers(tc.PEERS) peers = mt.uncompact_peers(c_peers) for p1, p2 in zip(tc.PEERS, peers): assert p1[0] == p2[0] assert p1[0] == p2[0] c_nodes = mt.compact_nodes(tc.NODES) nodes = mt.uncompact_nodes(c_nodes) for n1, n2 in zip(tc.NODES, nodes): assert n1 == n2 bin_ipv6s = ['\x00' * 10 + '\xff\xff' + '\1\2\3\4', '\x22' * 16, ] #assert mt.bin_to_ip(bin_ipv6s[0]) == '1.2.3.4' #assert_raises(mt.AddrError, mt.bin_to_ip, bin_ipv6s[1]) IP = '1.2.3.4' PORT = 7777 BIN_PORT = mt.int_to_bin(PORT) c_nodes2 = [tc.CLIENT_ID.bin_id + inet_aton(IP) + BIN_PORT] nodes2 = [node.Node((IP, PORT), tc.CLIENT_ID)] logger.debug(mt.uncompact_nodes2(c_nodes2)) eq_(mt.uncompact_nodes2(c_nodes2), nodes2) logger.warning( "**IGNORE WARNING LOG** This exception was raised by a test")
def test_tools(self): bin_strs = ['23', '\1\5', 'a\3'] for bs in bin_strs: i = mt.bin_to_int(bs) bs2 = mt.int_to_bin(i) logger.debug('bs: %s, bin_to_int(bs): %d, bs2: %s' % (bs, i, bs2)) assert bs == bs2 ips = ['127.0.0.1', '222.222.222.222', '1.2.3.4'] ports = [12345, 99, 54321] for addr in zip(ips, ports): c_addr = mt.compact_addr(addr) addr2 = mt.uncompact_addr(c_addr) assert addr == addr2 c_peers = mt.compact_peers(tc.PEERS) peers = mt.uncompact_peers(c_peers) for p1, p2 in zip(tc.PEERS, peers): assert p1[0] == p2[0] assert p1[0] == p2[0] c_nodes = mt.compact_nodes(tc.NODES) nodes = mt.uncompact_nodes(c_nodes) for n1, n2 in zip(tc.NODES, nodes): assert n1 == n2 bin_ipv6s = [ '\x00' * 10 + '\xff\xff' + '\1\2\3\4', '\x22' * 16, ] #assert mt.bin_to_ip(bin_ipv6s[0]) == '1.2.3.4' #assert_raises(mt.AddrError, mt.bin_to_ip, bin_ipv6s[1]) IP = '1.2.3.4' PORT = 7777 BIN_PORT = mt.int_to_bin(PORT) c_nodes2 = [tc.CLIENT_ID.bin_id + inet_aton(IP) + BIN_PORT] nodes2 = [node.Node((IP, PORT), tc.CLIENT_ID)] logger.debug(mt.uncompact_nodes2(c_nodes2)) eq_(mt.uncompact_nodes2(c_nodes2), nodes2) logger.warning( "**IGNORE WARNING LOG** This exception was raised by a test")
def __init__(self, sender_id, nodes): OutgoingResponseBase.__init__(self, sender_id) self._dict[RESPONSE][NODES] = mt.compact_nodes(nodes)
def find_node_response(self, nodes): self._dict[RESPONSE][NODES] = mt.compact_nodes(nodes)