def data_request(self, action, data_id, file_size, node_unl): """ Action = put (upload), get (download.) """ _log.debug("In data request function") node_unl = node_unl.decode("utf-8") d = defer.Deferred() if node_unl == self.net.unl.value: e = "Can;t send data request to ourself" _log.debug(e) d.errback(Exception(e)) return d # Who is hosting this data? if action == u"download": # We store this data. host_unl = self.net.unl.value.decode("utf-8") cfg = self.store_config _log.debug(cfg) _log.debug(data_id) assert(storjnode.storage.manager.find(cfg, data_id) is not None) else: # They store the data. host_unl = node_unl if data_id in self.downloading: e = "Already trying to download this." _log.debug(e) d.errback(Exception(e)) return d # Create contract. contract = OrderedDict([ (u"status", u"SYN"), (u"data_id", data_id.decode("utf-8")), (u"file_size", file_size), (u"host_unl", host_unl), (u"dest_unl", node_unl), (u"src_unl", self.net.unl.value) ]) # Sign contract. contract = self.sign_contract(contract) # Check contract is valid. if is_valid_syn(self, contract) != 1: e = "our syn is invalid" _log.debug(e) d.errback(Exception(e)) return d # Route contract. contract_id = self.save_contract(contract) self.send_msg(contract, node_unl) _log.debug("Sending data request") # Update handshake. self.handshake[contract_id] = { u"state": u"SYN", u"timestamp": time.time() } # For async code. self.defers[contract_id] = d # Return defer for async code. return contract_id
def test_valid_syn(self): print("") print("Testing is_valid_syn") print("") self.clean_slate_all() # Non existing fields. syn = {} self.assertTrue(is_valid_syn(self.alice, syn) == -1) # Invalid number of fields. syn = copy.deepcopy(self.syn) syn["test"] = "test" self.assertTrue(is_valid_syn( self.alice, self.alice.sign_contract(syn)) == -2 ) del syn["test"] del syn["signature"] # The data ID is wrong. syn["data_id"] = "x" self.assertTrue(is_valid_syn( self.alice, self.alice.sign_contract(syn)) == -3 ) syn["data_id"] = hashlib.sha256(b"0").hexdigest() del syn["signature"] # Syn is too big. """ syn[u"file_size"] = int("9" * (5242880 + 10)) self.assertTrue(is_valid_syn( self.alice, self.alice.sign_contract(syn) ) == -4) syn[u"file_size"] = 1 """ # Invalid UNLs. syn["host_unl"] = "0" self.assertTrue(is_valid_syn( self.alice, self.alice.sign_contract(syn)) == -6 ) syn["host_unl"] = self.alice.net.unl.value del syn["signature"] # Invalid file size. syn["file_size"] = str("0") self.assertTrue(is_valid_syn( self.alice, self.alice.sign_contract(syn)) == -7 ) syn["file_size"] = 20 del syn["signature"] # We're the host and we don't have this file. self.assertTrue(is_valid_syn( self.alice, self.alice.sign_contract(syn)) == -8 ) del syn["signature"] # We're not the host. We're downloading this. # and we already have the file. syn[u"host_unl"] = self.bob.net.unl.value path = os.path.join(self.alice_storage, syn[u"data_id"]) if not os.path.exists(path): with open(path, "w") as fp: fp.write("0") self.assertTrue(is_valid_syn( self.alice, self.alice.sign_contract(syn)) == -9 ) del syn["signature"] # We're not the host and we're already downloading this os.remove(path) self.alice.downloading[syn[u"data_id"]] = path self.assertTrue(is_valid_syn( self.alice, self.alice.sign_contract(syn) ) == -10) del self.alice.downloading[syn[u"data_id"]] del syn["signature"] # This should pass. self.assertTrue(is_valid_syn( self.alice, self.alice.sign_contract(syn) ) == 1) print("") print("Ending is_valid_syn") print("")