def _sanitize_response(self): nodes_found = False peers_found = False # sender_id self.sender_id = self._get_id(RESPONSE, ID) self.sender_node = Node(self.sender_addr, self.sender_id) # all nodes self.all_nodes = [] # nodes c_nodes = self._get_str(RESPONSE, NODES, optional=True) if c_nodes: self.nodes = mt.uncompact_nodes(c_nodes) self.all_nodes = self.nodes # nodes2 try: c_nodes2 = self._msg_dict[RESPONSE][NODES2] except (KeyError): self.nodes2 = None else: self.nodes2 = mt.uncompact_nodes2(c_nodes2) for n in self.nodes2: if n not in self.all_nodes: self.all_nodes.append(n) # token self.token = self._get_str(RESPONSE, TOKEN, optional=True) # peers self.peers = None c_peers = self._get_value(RESPONSE, PEERS, optional=True) if c_peers: self.peers = mt.uncompact_peers(c_peers)
def _sanitize_response(self): # src_id self.src_id = self._get_id(RESPONSE, ID) self.src_node = Node(self.src_addr, self.src_id, self.version) # all nodes self.all_nodes = [] # nodes c_nodes = self._get_str(RESPONSE, NODES, optional=True) if c_nodes: self.nodes = mt.uncompact_nodes(c_nodes) self.all_nodes = self.nodes # nodes2 try: c_nodes2 = self._msg_dict[RESPONSE][NODES2] except (KeyError): self.nodes2 = None else: self.nodes2 = mt.uncompact_nodes2(c_nodes2) for n in self.nodes2: if n not in self.all_nodes: self.all_nodes.append(n) # token self.token = self._get_str(RESPONSE, TOKEN, optional=True) # peers self.peers = None c_peers = self._get_value(RESPONSE, PEERS, optional=True) if c_peers: self.peers = mt.uncompact_peers(c_peers)
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 sanitize_response(self, query=None): nodes_found = False peers_found = False self._sanitize_common() # sender_id self.sender_id = self._get_id(RESPONSE, ID) if query in [None, FIND_NODE, GET_PEERS]: self.all_nodes = [] # nodes c_nodes = self._get_str(RESPONSE, NODES, optional=True) if c_nodes: self.nodes = mt.uncompact_nodes(c_nodes) self.all_nodes = self.nodes nodes_found = True # nodes2 try: self.nodes2 = mt.uncompact_nodes2( self._msg_dict[RESPONSE][NODES2]) if nodes_found: logger.info('Both nodes and nodes2 found') for n in self.nodes2: if n not in self.all_nodes: self.all_nodes.append(n) nodes_found = True except (KeyError): pass if query in [None, GET_PEERS]: # token self.token = self._get_str(RESPONSE, TOKEN, query == None) # peers try: self.peers = mt.uncompact_peers( self._msg_dict[RESPONSE][VALUES]) peers_found = True if nodes_found: logger.debug( 'Nodes and peers found in get_peers response') except (KeyError): pass if query == FIND_NODE: if not nodes_found: logger.warning('No nodes in find_node response') raise MsgError, 'No nodes in find_node response' elif query == GET_PEERS: if not nodes_found and not peers_found: logger.warning( 'No nodes or peers found in get_peers response') raise MsgError, \ 'No nodes or peers found in get_peers response'
def sanitize_response(self, query=None): nodes_found = False peers_found = False self._sanitize_common() # sender_id self.sender_id = self._get_id(RESPONSE, ID) if query in [None, FIND_NODE, GET_PEERS]: self.all_nodes = [] # nodes c_nodes = self._get_str(RESPONSE, NODES, optional=True) if c_nodes: self.nodes = mt.uncompact_nodes(c_nodes) self.all_nodes = self.nodes nodes_found = True # nodes2 try: self.nodes2 = mt.uncompact_nodes2( self._msg_dict[RESPONSE][NODES2]) if nodes_found: logger.info('Both nodes and nodes2 found') for n in self.nodes2: if n not in self.all_nodes: self.all_nodes.append(n) nodes_found = True except (KeyError): pass if query in [None, GET_PEERS]: # token self.token = self._get_str(RESPONSE, TOKEN, query == None) # peers try: self.peers = mt.uncompact_peers( self._msg_dict[RESPONSE][VALUES]) peers_found = True if nodes_found: logger.debug('Nodes and peers found in get_peers response') except (KeyError): pass if query == FIND_NODE: if not nodes_found: logger.warning('No nodes in find_node response') raise MsgError, 'No nodes in find_node response' elif query == GET_PEERS: if not nodes_found and not peers_found: logger.warning('No nodes or peers found in get_peers response') raise MsgError, \ 'No nodes or peers found in get_peers response'
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 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 _sanitize_response(self): self.sender_id = self._get_id(RESPONSE, ID) self.sender_node = Node(self.sender_addr, self.sender_id) self.all_nodes = [] c_nodes = self._get_str(RESPONSE, NODES, optional=True) if c_nodes: self.nodes = mt.uncompact_nodes(c_nodes) self.all_nodes = self.nodes try: c_nodes2 = self._msg_dict[RESPONSE][NODES2] except KeyError: self.nodes2 = None else: self.nodes2 = mt.uncompact_nodes2(c_nodes2) for n in self.nodes2: if n not in self.all_nodes: self.all_nodes.append(n) self.token = self._get_str(RESPONSE, TOKEN, optional=True) self.peers = None c_peers = self._get_value(RESPONSE, PEERS, optional=True) if c_peers: self.peers = mt.uncompact_peers(c_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')