def test_encrypt(self): pubkey_a, private_key_a = generate_rsa_key() pubkey_b, private_key_b = generate_rsa_key() data = get_random_bytes(1024 * 1024 * 20) # 20MB encrypted = encryption.encrypt(data, pubkey_a, pubkey_b) decrypted_a = decrypt(encrypted, private_key_a, 0) decrypted_b = decrypt(encrypted, private_key_b, 1) self.assertEqual(data, decrypted_a) self.assertEqual(data, decrypted_b)
def test_initialize_mode_production(self): httpretty.register_uri(httpretty.GET, URI_PRODUCTION, body=read_fixture("public_keys.json")) enc = Encrypt.new(mode=MODE_PRODUCTION) self.assertEqual( "ca5af2d14bee923a0a0d1687b7c77e7211a57f84:::683150ee69b4d906aa883d0ac12b0fdd79f95bcf", enc.fingerprint, ) data = get_random_bytes(1024 * 1024) # 1MB encrypted = enc.encrypt(data) private_keys = json.loads(read_fixture("private_keys.json")) decrypted_a = decrypt(encrypted, private_keys["private_key_a"], 0) self.assertEqual(data, decrypted_a) decrypted_b = decrypt(encrypted, private_keys["private_key_b"], 1) self.assertEqual(data, decrypted_b)
def test_initialize_custom_uri(self): uri = "https://api.example.com/keys" httpretty.register_uri(httpretty.GET, uri, body=read_fixture("public_keys.json")) enc = Encrypt.new(uri=uri) self.assertEqual( "ca5af2d14bee923a0a0d1687b7c77e7211a57f84:::683150ee69b4d906aa883d0ac12b0fdd79f95bcf", enc.fingerprint, ) data = get_random_bytes(1024 * 1024) # 1MB encrypted = enc.encrypt(data) private_keys = json.loads(read_fixture("private_keys.json")) decrypted_a = decrypt(encrypted, private_keys["private_key_a"], 0) self.assertEqual(data, decrypted_a) decrypted_b = decrypt(encrypted, private_keys["private_key_b"], 1) self.assertEqual(data, decrypted_b)
def test_mode_production(self): private_keys = json.loads(read_fixture("private_keys.json")) httpretty.register_uri( httpretty.GET, URI_PRODUCTION, body=read_fixture("public_keys.json") ) args = "--mode production".split(" ") input_data = "test data".encode("utf-8") exitcode, out_data, err_data = run_main(args=args, in_data=input_data) self.assertEqual("", err_data) self.assertEqual(0, exitcode) decrypted_a = decrypt(out_data, private_keys["private_key_a"], 0) self.assertEqual(input_data, decrypted_a) decrypted_b = decrypt(out_data, private_keys["private_key_b"], 1) self.assertEqual(input_data, decrypted_b)
def test_custom_uri(self): uri = "https://example.com/keys" private_keys = json.loads(read_fixture("private_keys.json")) httpretty.register_uri( httpretty.GET, uri, body=read_fixture("public_keys.json") ) args = f"--uri {uri}".split(" ") input_data = "test data".encode("utf-8") exitcode, out_data, err_data = run_main(args=args, in_data=input_data) self.assertEqual("", err_data) self.assertEqual(0, exitcode) decrypted_a = decrypt(out_data, private_keys["private_key_a"], 0) self.assertEqual(input_data, decrypted_a) decrypted_b = decrypt(out_data, private_keys["private_key_b"], 1) self.assertEqual(input_data, decrypted_b)
def test_input_file(self): private_keys = json.loads(read_fixture("private_keys.json")) httpretty.register_uri( httpretty.GET, URI_PRODUCTION, body=read_fixture("public_keys.json") ) in_path = f"{TESTS_ROOT}/fixtures/test_file.txt" with open(in_path, mode="rb") as f: in_file_data = f.read() args = f"--mode production --input {in_path}".split(" ") exitcode, out_data, err_data = run_main(args=args) self.assertEqual("", err_data) self.assertEqual(0, exitcode) decrypted_a = decrypt(out_data, private_keys["private_key_a"], 0) self.assertEqual(in_file_data, decrypted_a) decrypted_b = decrypt(out_data, private_keys["private_key_b"], 1) self.assertEqual(in_file_data, decrypted_b)
def test_output_file(self): private_keys = json.loads(read_fixture("private_keys.json")) httpretty.register_uri( httpretty.GET, URI_PRODUCTION, body=read_fixture("public_keys.json") ) input_data = "test data".encode("utf-8") out_path = f"{TESTS_ROOT}/fixtures/test.enc" args = f"--mode production --output {out_path}".split(" ") exitcode, out_data, err_data = run_main(args=args, in_data=input_data) with open(out_path, mode="rb") as f: out_file_data = f.read() os.remove(out_path) self.assertEqual("", err_data) self.assertEqual(0, exitcode) self.assertEqual(b"", out_data) decrypted_a = decrypt(out_file_data, private_keys["private_key_a"], 0) self.assertEqual(input_data, decrypted_a) decrypted_b = decrypt(out_file_data, private_keys["private_key_b"], 1) self.assertEqual(input_data, decrypted_b)