def check_encryption(*args, **kwargs): """ Function that will be used as ``side_effect`` argument, when an instance of the ``Mock()`` class will be created for the ``requests.post()`` function. It asserts that file, which is passed to the ``requests.post()`` are properly encrypted. It will be called inside the upload function, where is temporary encrypted copy of original file is available. :param args: positional arguments of the ``requests.post()`` func. :param kwargs: optional args of the ``requests.post()`` func. :return: empty response object :rtype: requests.models.Response object """ tested_file_obj = kwargs['files']['file_data'] sampler_encrypted_file_name = os.path.join( self.testing_dir, 'copy_' + os.path.split(test_source_file.name)[-1] ) self.addCleanup(os.remove, sampler_encrypted_file_name) shutil.copy2(test_source_file.name, sampler_encrypted_file_name) convergence.encrypt_file_inline( sampler_encrypted_file_name, None ) self.assertTrue( filecmp.cmp(tested_file_obj.name, sampler_encrypted_file_name), "A generated copy of the file that was originally passed " "to upload(), should be the same as encrypted test sampler " "file!" ) return Response()
def test_passphrase_does_something(self): convergence.encrypt_file_inline(self.sample1, "first") convergence.encrypt_file_inline(self.sample2, "second") self.assertNotEqual( self.contents(self.sample1), self.contents(self.sample2))
def test_deterministic_inline_encryption(self): convergence.encrypt_file_inline(self.sample1, None) convergence.encrypt_file_inline(self.sample2, None) self.assertEqual( self.contents(self.sample1), self.contents(self.sample2))
def test_inline_decryption(self): plaintext = self.contents(self.sample1) key = convergence.encrypt_file_inline(self.sample1, None) convergence.decrypt_file_inline(self.sample1, key) self.assertEqual(plaintext, self.contents(self.sample1))
def test_inline_decryption_with_passphrase(self): plaintext = self.contents(self.sample1) key = convergence.encrypt_file_inline(self.sample1, "super secret") convergence.decrypt_file_inline(self.sample1, key) self.assertEqual(plaintext, self.contents(self.sample1))
def test_authentication(self): plaintext = self.contents(self.sample1) k,h = convergence.encrypt_file_inline(self.sample1, "test secret", True) convergence.decrypt_file_inline(self.sample1, k, h) self.assertEqual(plaintext, self.contents(self.sample1))
def test_streaming_authenticated_decryption(self): plaintext = self.contents(self.sample1) k,h = convergence.encrypt_file_inline(self.sample1, "test secret", True) decrypted = b"" for chunk in convergence.decrypt_generator(self.sample1, k, h): decrypted += chunk self.assertEqual(plaintext,decrypted)
def test_streaming_decryption(self): plaintext = self.contents(self.sample1) key = convergence.encrypt_file_inline(self.sample1, "super secret") decrypted = "".encode() for chunk in convergence.decrypt_generator(self.sample1, key): decrypted += chunk self.assertEqual(plaintext, decrypted)
def copy_encrypt_binary(): global bin_file, api save_path2 = 'C:/test_lr' b = api.select(Binary).where( 'observed_filename:{0}'.format(bin_file)).first() file_name = str(b.observed_filename).rsplit('\\', 1)[-1].replace("']", '') shutil.copyfileobj(b.file, open(os.path.join(save_path2, file_name), 'wb')) key = convergence.encrypt_file_inline("c:\\test_lr\\{0}".format(bin_file)) return key
def test_streaming_authenticated_decryption_failed(self): k,h = convergence.encrypt_file_inline(self.sample1, "test secret", True) # modify file with open(self.sample1, "ab") as f: f.write("data added!\n".encode()) decrypted = b"" with self.assertRaises(AuthenticationError) as ex: for chunk in convergence.decrypt_generator(self.sample1, k, h): decrypted += chunk self.assertEqual(str(ex.exception),"Hash Message Authentication Code invalid.")
def test_authentication_failed(self): plaintext = self.contents(self.sample1) k,h = convergence.encrypt_file_inline(self.sample1, "test secret", True) # modify file with open(self.sample1, "r+b") as f: f.write("file changed!\n".encode()) with self.assertRaises(AuthenticationError) as ex: convergence.decrypt_file_inline(self.sample1, k, h) self.assertEqual(str(ex.exception),"Hash Message Authentication Code invalid.")
def test_add_decryption_key_to_the_response_obj(self): """ Test on adding ``decryption_key`` to the derived Response object, when ``encrypt=True``. The ``upload()`` function then derive ``decryption_key`` value, whilst encrypt local copy of the file, and should modify ``Response`` object by adding this decryption key to the json's content. """ # Make the copy of the original source file and encrypt it for # further usage as the expected sent file sampler. sampler_encrypted_file_name = os.path.join( self.testing_dir, 'copy_' + os.path.split(self.test_source_file.name)[-1] ) self.addCleanup(os.remove, sampler_encrypted_file_name) shutil.copy2(self.test_source_file.name, sampler_encrypted_file_name) sampler_decryption_key = convergence.encrypt_file_inline( sampler_encrypted_file_name, None ) sampler_decryption_key = binascii.hexlify(sampler_decryption_key) if sys.version_info.major == 3: sampler_decryption_key = sampler_decryption_key.decode() with open(sampler_encrypted_file_name, 'rb') as file_: sampler_data_hash = sha256(file_.read()).hexdigest() # Fill the mocked response object. mock_response_data_dict = dict( data_hash=sampler_data_hash, file_role=self.upload_param['file_role'] ) response_content = json.dumps(mock_response_data_dict, indent=2, sort_keys=True) mock_response = self.mock_post.return_value mock_response.status_code = 201 mock_response._content = response_content.encode('ascii') # The actual test. tested_response = core.upload(file_=self.test_source_file, encrypt=True, **self.upload_param) self.assertIs(tested_response, mock_response, 'Returned object should be the same object which was ' 'returned from the ``requests.post()`` call!') tested_response_data_dict = tested_response.json() mock_response_data_dict.update( decryption_key=sampler_decryption_key) self.assertDictEqual( tested_response_data_dict, mock_response_data_dict, 'When encrypt=True, returned response object should contain same ' 'data, but with additional and expected decryption_key value.' )
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_deterministic_inline_encryption(self): convergence.encrypt_file_inline(self.sample1, None) convergence.encrypt_file_inline(self.sample2, None) self.assertEqual(self.contents(self.sample1), self.contents(self.sample2))
def test_passphrase_does_something(self): convergence.encrypt_file_inline(self.sample1, "first") convergence.encrypt_file_inline(self.sample2, "second") self.assertNotEqual(self.contents(self.sample1), self.contents(self.sample2))
def test_inline_encryption_with_passphrase(self): convergence.encrypt_file_inline(self.sample1, "potato")
def test_inline_encryption(self): convergence.encrypt_file_inline(self.sample1, None) self.assertNotEqual( self.contents(self.sample1), "Superstar!\n".encode())
def test_inline_encryption(self): convergence.encrypt_file_inline(self.sample1, None) self.assertNotEqual(self.contents(self.sample1), "Superstar!\n".encode())