def setUp(self): """ Switch to test config. Create initial files set in the Upload Dir. Remember initial blacklist content. """ self.app = storj.app self.app.config['TESTING'] = True self.blocked_data = b'blocked_data' self.blocked_hash = sha256(self.blocked_data).hexdigest() with open(self.app.config['BLACKLIST_FILE'], 'r+') as fp: self.initial_blacklist = fp.read() fp.writelines((self.blocked_hash + '\n',)) self.files_id = files.insert().values( hash=sha256(b'_').hexdigest(), role='000', size=1, owner=test_owner_address ).execute().inserted_primary_key self.files_id.extend( files.insert().values( hash=sha256(b'__').hexdigest(), role='000', size=2, owner=test_owner_address ).execute().inserted_primary_key )
def upload(file, data_hash, role, sender, signature): """ Check if data_hash is valid SHA-256 hash matched with uploading file. Check file size. Save uploaded file to the Upload Dir and insert a record in the 'files' table. :param file: file-like object with binary data :param data_hash: SHA-256 hash for data :param role: file role :param sender: file sender's BitCoin address :param signature: data signature """ node = app.config['NODE'] checker = Checker(data_hash, sender, signature) checks_result = checker.check_all('double_uploading') if checks_result: return {'file_role': checker.file.role} checks_result = checker.check_all('signature', 'hash', 'blacklist') if checks_result: return checks_result file_data = file.read() file_size = len(file_data) try: os.makedirs(app.config['UPLOAD_FOLDER']) except OSError: pass if file_size > app.config['MAX_FILE_SIZE']: return ERR_TRANSFER['HUGE_FILE'] if file_size > node.capacity: return ERR_TRANSFER['FULL_DISK'] if node.limits['incoming'] is not None and ( file_size > node.limits['incoming'] - node.current['incoming'] ): return ERR_TRANSFER['LIMIT_REACHED'] if data_hash != sha256(file_data).hexdigest(): return ERR_TRANSFER['MISMATCHED_HASH'] with open(os.path.join(app.config['UPLOAD_FOLDER'], data_hash), 'wb') as file_to_save: file_to_save.write(file_data) files.insert().values( hash=data_hash, role=role, size=len(file_data), owner=sender ).execute()
def test_success_get_info_with_existing_files_and_hide_blocked(self): """ Successful getting files info with existing files and hide blocked ones. """ self.files_id.extend( files.insert().values( hash=self.blocked_hash, role='000', size=len(self.blocked_data), owner=test_owner_address ).execute().inserted_primary_key ) with self.app.test_client() as c: response = c.get(path=self.url) self.assertEqual(200, response.status_code, "'OK' status code is expected.") self.assertEqual('application/json', response.content_type, "Has to be a JSON-response.") data = json.loads(response.data.decode()) self.assertIsInstance(data, list, "Has to be an array.") self.assertSetEqual( set(_['hash'] for _ in files.select( files.c.hash != self.blocked_hash ).execute()), set(data), "Has to contain all files hashes without blacklisted ones." )
def setUp(self): """ Switch to test config. Create initial files set in the Upload Dir. Create initial records in the 'files' table. Remember challenge seed and response. Remember initial blacklist content. """ self.app = storj.app self.app.config['TESTING'] = True self.file_data = b'existing file data' self.data_hash = sha256(self.file_data).hexdigest() valid_signature = test_btctx_api.sign_unicode(test_owner_wif, self.data_hash) self.blocked_data = b'blocked_data' self.blocked_hash = sha256(self.blocked_data).hexdigest() with open(self.app.config['BLACKLIST_FILE'], 'r+') as fp: self.initial_blacklist = fp.read() fp.writelines((self.blocked_hash + '\n',)) self.file_saving_path = os.path.join( self.app.config['UPLOAD_FOLDER'], self.data_hash ) with open(self.file_saving_path, 'wb') as stored_file: stored_file.write(self.file_data) self.owner = test_owner_address self.files_id = files.insert().values( hash=self.data_hash, role='000', size=len(self.file_data), owner=self.owner ).execute().inserted_primary_key audit.delete().execute() self.challenge_seed = sha256(b'seed').hexdigest() self.challenge_response = sha256( self.file_data + self.challenge_seed.encode() ).hexdigest() self.send_data = { 'data_hash': self.data_hash, 'challenge_seed': self.challenge_seed } self.headers = { 'sender_address': self.owner, 'signature': valid_signature } self.other = test_other_address self.other_signature = test_btctx_api.sign_unicode(test_other_wfi, self.data_hash) self.patcher = patch('metacore.processor.BTCTX_API', test_btctx_api) self.patcher.start()
def setUp(self): """ Switch to test config. Create initial files set in the Upload Dir. Create initial records in the 'files' table. """ self.app = storj.app self.app.config['TESTING'] = True self.file_data = b'existing file data' temp_hash = sha256(self.file_data).hexdigest() file_path = os.path.join( self.app.config['UPLOAD_FOLDER'], temp_hash ) with open(file_path, 'wb') as fp: fp.write(self.file_data) self.key = binascii.hexlify( convergence.encrypt_file_inline(file_path, None)) with open(file_path, 'rb') as fp: self.encrypted_data = fp.read() self.data_hash = sha256(self.encrypted_data).hexdigest() valid_signature = test_btctx_api.sign_unicode(test_owner_wif, self.data_hash) self.file_saving_path = os.path.join(self.app.config['UPLOAD_FOLDER'], self.data_hash) os.rename(file_path, self.file_saving_path) self.files_id = files.insert().values( hash=self.data_hash, role='001', size=len(self.encrypted_data), owner=test_owner_address ).execute().inserted_primary_key self.headers = { 'sender_address': test_owner_address, 'signature': valid_signature } self.query_string = { 'decryption_key': self.key, 'file_alias': 'file.txt' } self.patcher = patch('metacore.processor.BTCTX_API', test_btctx_api) self.patcher.start()
def test_non_authenticated_access(self): """ Testing the non-authenticated public access to files with "001" and "101" values of the role. """ self.headers = {} cached_data_from_setup = self.data_hash public_shared_data_tuple = (b'001', b'101') for public_shared_data in public_shared_data_tuple: public_data_hash = sha256(public_shared_data).hexdigest() self.data_hash= public_data_hash shared_file_path = os.path.join( self.app.config['UPLOAD_FOLDER'], public_data_hash ) with open(shared_file_path, 'wb') as stored_file: stored_file.write(public_shared_data) self.files_id += files.insert().values( hash=public_data_hash, role=public_shared_data.decode(), size=len(public_shared_data), owner=test_owner_address ).execute().inserted_primary_key response = self.make_request() self.assertEqual(response.data, public_shared_data, "Stored file content is expected.") try: pass os.unlink(shared_file_path) except OSError: pass self.data_hash = cached_data_from_setup response = self.make_request() self.assertDictEqual({'error_code': ERR_TRANSFER['INVALID_SIGNATURE']}, json.loads(response.data.decode()), "Unexpected response data.")