Ejemplo n.º 1
0
 def test_iteration(self):
     heap = NodeHeap(mknode(intid=0), 5)
     nodes = [mknode(intid=x) for x in range(10)]
     for index, node in enumerate(nodes):
         heap.push(node)
     for index, node in enumerate(heap):
         self.assertEqual(index, node.long_id)
         self.assertTrue(index < 5)
Ejemplo n.º 2
0
 def test_iteration(self):
     heap = NodeHeap(mknode(intid=0), 5)
     nodes = [mknode(intid=x) for x in range(10)]
     for index, node in enumerate(nodes):
         heap.push(node)
     for index, node in enumerate(heap):
         self.assertEqual(index, node.long_id)
         self.assertTrue(index < 5)
Ejemplo n.º 3
0
 def test_removeNode(self):
     bucket = KBucket(0, 10, 2)
     self.assertTrue(bucket.addNode(mknode(intid=1)))
     self.assertTrue(bucket.addNode(mknode(intid=2)))
     bucket.removeNode(mknode(intid=1))
     self.assertEqual(len(bucket), 1)
     bucket.replacementNodes.push(mknode(intid=3))
     bucket.removeNode(mknode(intid=2))
     self.assertEqual(len(bucket), 1)
Ejemplo n.º 4
0
    def test_maxSize(self):
        n = NodeHeap(mknode(intid=0), 3)
        self.assertEqual(0, len(n))

        for d in range(10):
            n.push(mknode(intid=d))
        self.assertEqual(3, len(n))

        self.assertEqual(3, len(list(n)))
Ejemplo n.º 5
0
 def test_split(self):
     bucket = KBucket(0, 10, 5)
     bucket.addNode(mknode(intid=5))
     bucket.addNode(mknode(intid=6))
     one, two = bucket.split()
     self.assertEqual(len(one), 1)
     self.assertEqual(one.range, (0, 5))
     self.assertEqual(len(two), 1)
     self.assertEqual(two.range, (6, 10))
Ejemplo n.º 6
0
    def test_maxSize(self):
        n = NodeHeap(mknode(intid=0), 3)
        self.assertEqual(0, len(n))

        for d in range(10):
            n.push(mknode(intid=d))
        self.assertEqual(3, len(n))

        self.assertEqual(3, len(list(n)))
Ejemplo n.º 7
0
 def test_split(self):
     bucket = KBucket(0, 10, 5)
     bucket.addNode(mknode(intid=5))
     bucket.addNode(mknode(intid=6))
     one, two = bucket.split()
     self.assertEqual(len(one), 1)
     self.assertEqual(one.range, (0, 5))
     self.assertEqual(len(two), 1)
     self.assertEqual(two.range, (6, 10))
Ejemplo n.º 8
0
 def test_removeNode(self):
     bucket = KBucket(0, 10, 2)
     self.assertTrue(bucket.addNode(mknode(intid=1)))
     self.assertTrue(bucket.addNode(mknode(intid=2)))
     bucket.removeNode(mknode(intid=1))
     self.assertEqual(len(bucket), 1)
     bucket.replacementNodes.push(mknode(intid=3))
     bucket.removeNode(mknode(intid=2))
     self.assertEqual(len(bucket), 1)
Ejemplo n.º 9
0
    def test_remove(self):
        heap = NodeHeap(mknode(intid=0), 5)
        nodes = [mknode(intid=x) for x in range(10)]
        for node in nodes:
            heap.push(node)

        heap.remove([nodes[0].id, nodes[1].id])
        self.assertEqual(len(list(heap)), 5)
        for index, node in enumerate(heap):
            self.assertEqual(index + 2, node.long_id)
            self.assertTrue(index < 5)
Ejemplo n.º 10
0
    def test_remove(self):
        heap = NodeHeap(mknode(intid=0), 5)
        nodes = [mknode(intid=x) for x in range(10)]
        for node in nodes:
            heap.push(node)

        heap.remove([nodes[0].id, nodes[1].id])
        self.assertEqual(len(list(heap)), 5)
        for index, node in enumerate(heap):
            self.assertEqual(index + 2, node.long_id)
            self.assertTrue(index < 5)
    def test_transferKeyValues(self):
        self._connecting_to_connected()
        self.wire_protocol[self.addr1] = self.con

        self.protocol.addToRouter(mknode())

        self.protocol.storage[digest("keyword")] = (
            digest("key"),
            self.protocol.sourceNode.getProto().SerializeToString())
        self.protocol.transferKeyValues(
            Node(digest("id"), self.addr1[0], self.addr1[1]))

        self.clock.advance(1)
        connection.REACTOR.runUntilCurrent()
        sent_packet = packet.Packet.from_bytes(
            self.proto_mock.send_datagram.call_args_list[0][0][0])
        sent_message = sent_packet.payload
        x = message.Message()
        x.ParseFromString(sent_message)

        m = message.Message()
        m.sender.MergeFrom(self.protocol.sourceNode.getProto())
        m.command = message.Command.Value("STORE")
        m.arguments.append(digest("keyword"))
        m.arguments.append(digest("key"))
        m.arguments.append(
            self.protocol.sourceNode.getProto().SerializeToString())
        self.assertEqual(x.sender, m.sender)
        self.assertEqual(x.command, m.command)
        self.assertEqual(x.arguments[0], m.arguments[0])
        self.assertEqual(x.arguments[1], m.arguments[1])
        self.assertEqual(x.arguments[2], m.arguments[2])
Ejemplo n.º 12
0
    def test_transferKeyValues(self):
        self._connecting_to_connected()
        self.wire_protocol[self.addr1] = self.con

        self.protocol.addToRouter(mknode())

        self.protocol.storage[digest("keyword")] = (
            digest("key"), self.protocol.sourceNode.getProto().SerializeToString())
        self.protocol.transferKeyValues(Node(digest("id"), self.addr1[0], self.addr1[1]))

        self.clock.advance(1)
        connection.REACTOR.runUntilCurrent()
        sent_packet = packet.Packet.from_bytes(self.proto_mock.send_datagram.call_args_list[0][0][0])
        sent_message = sent_packet.payload
        x = message.Message()
        x.ParseFromString(sent_message)

        m = message.Message()
        m.sender.MergeFrom(self.protocol.sourceNode.getProto())
        m.command = message.Command.Value("STORE")
        m.arguments.append(digest("keyword"))
        m.arguments.append(digest("key"))
        m.arguments.append(self.protocol.sourceNode.getProto().SerializeToString())
        self.assertEqual(x.sender, m.sender)
        self.assertEqual(x.command, m.command)
        self.assertEqual(x.arguments[0], m.arguments[0])
        self.assertEqual(x.arguments[1], m.arguments[1])
        self.assertEqual(x.arguments[2], m.arguments[2])
Ejemplo n.º 13
0
 def test_MarketProtocol_rpc_get_image_invalid_image_hash(self):
     catcher = self.catcher
     mp = MarketProtocol(self.node, self.router, 0, 0)
     self.assertEqual(None, mp.rpc_get_image(mknode(), "invalid_hash"))
     catch_exception = catcher.pop()
     exception_message = catcher.pop()
     self.assertEquals(catch_exception["message"][0], "[WARNING] could not find image 696e76616c69645f68617368")
     self.assertEquals(exception_message["message"][0], "[WARNING] Image hash is not 20 characters invalid_hash")
Ejemplo n.º 14
0
 def test_MarketProtocol_rpc_get_image_invalid_image_hash(self):
     catcher = self.catcher
     mp = MarketProtocol(self.node, self.router, 0, 0)
     self.assertEqual(None, mp.rpc_get_image(mknode(), "invalid_hash"))
     catch_exception = catcher.pop()
     exception_message = catcher.pop()
     self.assertEquals(
         catch_exception["message"][0],
         "[WARNING] could not find image 696e76616c69645f68617368")
     self.assertEquals(
         exception_message["message"][0],
         "[WARNING] Image hash is not 20 characters invalid_hash")
Ejemplo n.º 15
0
    def test_transferKeyValues(self):
        self._connecting_to_connected()
        self.wire_protocol[self.addr1] = self.con

        self.protocol.router.addContact(mknode())

        self.protocol.storage[digest("keyword")] = (
            digest("key"),
            self.protocol.sourceNode.getProto().SerializeToString(), 10)
        self.protocol.storage[digest("keyword")] = (
            digest("key2"),
            self.protocol.sourceNode.getProto().SerializeToString(), 10)

        self.protocol.transferKeyValues(
            Node(digest("id"), self.addr1[0], self.addr1[1]))

        self.clock.advance(1)
        connection.REACTOR.runUntilCurrent()
        sent_packet = packet.Packet.from_bytes(
            self.proto_mock.send_datagram.call_args_list[0][0][0])
        sent_message = sent_packet.payload
        x = message.Message()
        x.ParseFromString(sent_message)

        i = objects.Inv()
        i.keyword = digest("keyword")
        i.valueKey = digest("key")

        i2 = objects.Inv()
        i2.keyword = digest("keyword")
        i2.valueKey = digest("key2")

        m = message.Message()
        m.sender.MergeFrom(self.protocol.sourceNode.getProto())
        m.command = message.Command.Value("INV")
        m.protoVer = self.version
        m.arguments.append(i.SerializeToString())
        m.arguments.append(i2.SerializeToString())
        self.assertEqual(x.sender, m.sender)
        self.assertEqual(x.command, m.command)
        self.assertTrue(x.arguments[0] in m.arguments)
        self.assertTrue(x.arguments[1] in m.arguments)
Ejemplo n.º 16
0
    def test_addNode(self):
        # when full, return false
        bucket = KBucket(0, 10, 2)
        self.assertTrue(bucket.addNode(mknode()))
        self.assertTrue(bucket.addNode(mknode()))
        self.assertFalse(bucket.addNode(mknode()))
        self.assertEqual(len(bucket), 2)

        # make sure when a node is double added it's put at the end
        bucket = KBucket(0, 10, 3)
        nodes = [mknode(), mknode(), mknode()]
        for node in nodes:
            bucket.addNode(node)
        for index, node in enumerate(bucket.getNodes()):
            self.assertEqual(node, nodes[index])
Ejemplo n.º 17
0
    def test_addNode(self):
        # when full, return false
        bucket = KBucket(0, 10, 2)
        self.assertTrue(bucket.addNode(mknode()))
        self.assertTrue(bucket.addNode(mknode()))
        self.assertFalse(bucket.addNode(mknode()))
        self.assertEqual(len(bucket), 2)

        # make sure when a node is double added it's put at the end
        bucket = KBucket(0, 10, 3)
        nodes = [mknode(), mknode(), mknode()]
        for node in nodes:
            bucket.addNode(node)
        for index, node in enumerate(bucket.getNodes()):
            self.assertEqual(node, nodes[index])
Ejemplo n.º 18
0
    def test_transferKeyValues(self):
        self._connecting_to_connected()
        self.wire_protocol[self.addr1] = self.con

        self.protocol.router.addContact(mknode())

        self.protocol.storage[digest("keyword")] = (
            digest("key"), self.protocol.sourceNode.getProto().SerializeToString(), 10)
        self.protocol.storage[digest("keyword")] = (
            digest("key2"), self.protocol.sourceNode.getProto().SerializeToString(), 10)

        self.protocol.transferKeyValues(Node(digest("id"), self.addr1[0], self.addr1[1]))

        self.clock.advance(1)
        connection.REACTOR.runUntilCurrent()
        sent_packet = packet.Packet.from_bytes(self.proto_mock.send_datagram.call_args_list[0][0][0])
        sent_message = sent_packet.payload
        x = message.Message()
        x.ParseFromString(sent_message)

        i = objects.Inv()
        i.keyword = digest("keyword")
        i.valueKey = digest("key")

        i2 = objects.Inv()
        i2.keyword = digest("keyword")
        i2.valueKey = digest("key2")

        m = message.Message()
        m.sender.MergeFrom(self.protocol.sourceNode.getProto())
        m.command = message.Command.Value("INV")
        m.protoVer = self.version
        m.arguments.append(i.SerializeToString())
        m.arguments.append(i2.SerializeToString())
        self.assertEqual(x.sender, m.sender)
        self.assertEqual(x.command, m.command)
        self.assertTrue(x.arguments[0] in m.arguments)
        self.assertTrue(x.arguments[1] in m.arguments)
Ejemplo n.º 19
0
 def test_badRPCDelete(self):
     n = mknode()
     val = self.protocol.rpc_delete(n, 'testkeyword', 'key', 'testsig')
     self.assertEqual(val, ["False"])
     val = self.protocol.rpc_delete(n, '', '', '')
Ejemplo n.º 20
0
 def test_addContact(self):
     self.router.addContact(mknode())
     self.assertTrue(len(self.router.buckets), 1)
     self.assertTrue(len(self.router.buckets[0].nodes), 1)
Ejemplo n.º 21
0
 def test_inRange(self):
     bucket = KBucket(0, 10, 10)
     self.assertTrue(bucket.hasInRange(mknode(intid=5)))
     self.assertFalse(bucket.hasInRange(mknode(intid=11)))
     self.assertTrue(bucket.hasInRange(mknode(intid=10)))
     self.assertTrue(bucket.hasInRange(mknode(intid=0)))
 def test_badRPCDelete(self):
     n = mknode()
     val = self.protocol.rpc_delete(n, 'testkeyword', 'key', 'testsig')
     self.assertEqual(val, ["False"])
     val = self.protocol.rpc_delete(n, '', '', '')
Ejemplo n.º 23
0
 def test_badRPCDelete(self):
     n = mknode()
     val = self.protocol.rpc_delete(n, "testkeyword", "key", "testsig")
     self.assertEqual(val, ["False"])
     val = self.protocol.rpc_delete(n, "", "", "")
Ejemplo n.º 24
0
 def setUp(self):
     self.id = mknode().id
     self.protocol = FakeProtocol(self.id)
     self.router = self.protocol.router
Ejemplo n.º 25
0
 def test_addContact(self):
     self.router.addContact(mknode())
     self.assertTrue(len(self.router.buckets), 1)
     self.assertTrue(len(self.router.buckets[0].nodes), 1)
Ejemplo n.º 26
0
 def test_inRange(self):
     bucket = KBucket(0, 10, 10)
     self.assertTrue(bucket.hasInRange(mknode(intid=5)))
     self.assertFalse(bucket.hasInRange(mknode(intid=11)))
     self.assertTrue(bucket.hasInRange(mknode(intid=10)))
     self.assertTrue(bucket.hasInRange(mknode(intid=0)))