Example #1
0
 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 __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)
Example #4
0
 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)
Example #5
0
 def __init__(self, sender_id, token,
              nodes2=None, peers=None):
     if peers:
         self._bencoded_msg = BENCODED_MSG_TEMPLATES['og_get_peers_r_values']
         self._values = [sender_id.bin_id,
                         token,
                         mt.compact_peers(peers),
                         ''] #TID
         
     elif nodes2:
         self._bencoded_msg = BENCODED_MSG_TEMPLATES['og_get_peers_r_nodes']
         self._values = [sender_id.bin_id,
                         mt.compact_nodes2(nodes2),
                         token,
                         ''] #TID
     else:
         raise MsgError, 'must have nodes OR peers'
Example #6
0
    def __init__(self, sender_id, token, nodes2=None, peers=None):
        if peers:
            self._bencoded_msg = BENCODED_MSG_TEMPLATES[
                'og_get_peers_r_values']
            self._values = [
                sender_id.bin_id, token,
                mt.compact_peers(peers), ''
            ]  #TID

        elif nodes2:
            self._bencoded_msg = BENCODED_MSG_TEMPLATES['og_get_peers_r_nodes']
            self._values = [
                sender_id.bin_id,
                mt.compact_nodes2(nodes2), token, ''
            ]  #TID
        else:
            raise MsgError, 'must have nodes OR 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_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_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")
Example #11
0
    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')
Example #12
0
    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')