Ejemplo n.º 1
0
    def test_message_flow(self):
        print("")
        print("Testing message flow")
        print("")

        # Create file we're suppose to be uploading.
        path = os.path.join(self.alice_storage, self.syn[u"data_id"])
        if not os.path.exists(path):
            with open(path, "w") as fp:
                fp.write("0")

        # Clear existing contracts.
        self.clean_slate_all()

        # Alice: build SYN.
        contract_id = self.alice.simple_data_request(
            data_id=self.syn[u"data_id"],
            node_unl=self.bob.net.unl.value,
            direction=u"send"
        )
        syn = self.alice.contracts[contract_id]
        self.assertIsInstance(syn, OrderedDict)

        print(self.alice.net.unl.value)
        print(self.bob.net.unl.value)
        print(syn)

        # Bob: process SYN, build SYN-ACK.
        syn_ack = process_syn(self.bob, syn)
        self.assertIsInstance(syn_ack, OrderedDict)

        # Alice: process SYN-ACK, build ACK.
        ack = process_syn_ack(self.alice, syn_ack)
        self.assertIsInstance(ack, OrderedDict)

        # Bob: process ack.
        fin = process_ack(self.bob, ack)
        self.assertTrue(fin == 1)

        print("")
        print("Done testing message flow")
        print("")
Ejemplo n.º 2
0
    def test_valid_syn_ack(self):
        print("")
        print("Testing process syn-ack")
        print("")

        self.clean_slate_all()

        syn = self.alice.sign_contract(copy.deepcopy(self.syn))
        syn_ack = OrderedDict([(u'status', u'SYN-ACK'), (u'syn', syn)])
        syn_ack = self.bob.sign_contract(syn_ack)

        # Clear any old contracts that might exist.
        self.alice.contracts = {}

        # Create file we're suppose to be uploading.
        path = os.path.join(self.alice_storage, syn_ack[u"syn"][u"data_id"])
        if not os.path.exists(path):
            with open(path, "w") as fp:
                fp.write("0")

        # Syn not in message.
        syn_ack_2 = copy.deepcopy(syn_ack)
        del syn_ack_2[u"syn"]
        self.assertTrue(process_syn_ack(self.alice, syn_ack_2) == -1)

        # Invalid fields.
        syn_ack_2 = copy.deepcopy(syn_ack)
        syn_ack_2[u"xxx"] = "0"
        self.assertTrue(process_syn_ack(self.alice, syn_ack_2) == -2)

        # Not a reply to something we sent.
        syn_ack_2 = copy.deepcopy(syn_ack)
        self.assertTrue(process_syn_ack(self.alice, syn_ack_2) == -3)

        # Save original SYN as a contract.
        contract_id = self.alice.contract_id(syn_ack_2[u"syn"])
        self.alice.contracts[contract_id] = syn_ack_2[u"syn"]

        # Is SYN valid.
        syn_ack_2 = copy.deepcopy(syn_ack)
        syn_ack_2[u"syn"][u"file_size"] = "10"
        contract_id = self.alice.contract_id(syn_ack_2[u"syn"])
        self.alice.contracts[contract_id] = syn_ack_2[u"syn"]
        self.assertTrue(process_syn_ack(self.alice, syn_ack_2) == -4)

        # Did we sign this?
        syn_ack_2 = copy.deepcopy(syn_ack)
        syn_ack_2[u"syn"][u"signature"] = "x"
        contract_id = self.alice.contract_id(syn_ack_2[u"syn"])
        self.alice.contracts[contract_id] = syn_ack_2[u"syn"]
        self.assertTrue(process_syn_ack(self.alice, syn_ack_2) == -5)

        # Check their sig is valid.
        syn_ack_2 = copy.deepcopy(syn_ack)
        syn_ack_2[u"signature"] = "x"
        contract_id = self.alice.contract_id(syn_ack_2[u"syn"])
        self.alice.contracts[contract_id] = syn_ack_2[u"syn"]
        self.assertTrue(process_syn_ack(self.alice, syn_ack_2) == -6)

        # Check handshake state is valid.
        syn_ack_2 = copy.deepcopy(syn_ack)
        self.alice.handshake = {}
        ret = process_syn_ack(self.alice, syn_ack_2)
        print("ERror 1")
        print(ret)
        self.assertTrue(ret == -7)
        self.alice.handshake[contract_id] = {
            u"state": u"ACK",
            u"timestamp": time.time()
        }
        contract_id = self.alice.contract_id(syn_ack_2[u"syn"])
        self.alice.contracts[contract_id] = syn_ack_2[u"syn"]
        self.assertTrue(process_syn_ack(self.alice, syn_ack_2) == -8)
        self.alice.handshake[contract_id] = {
            u"state": u"SYN",
            u"timestamp": time.time()
        }

        # This should pass.
        syn_ack_2 = copy.deepcopy(syn_ack)
        contract_id = self.alice.contract_id(syn_ack_2[u"syn"])
        self.alice.contracts[contract_id] = syn_ack_2[u"syn"]
        ret = process_syn_ack(self.alice, syn_ack_2)
        print(ret)
        self.assertIsInstance(ret, OrderedDict)

        print("")
        print("Ending process syn-ack")
        print("")