1
1
# Copyright Amazon.com Inc. or its affiliates. All Rights Reserved.
2
2
# SPDX-License-Identifier: Apache-2.0
3
3
"""Test suite for the Raw RSA keyring example."""
4
+ import os
5
+
4
6
import pytest
5
7
6
8
from ...src .keyrings .raw_rsa_keyring_example import encrypt_and_decrypt_with_keyring , generate_rsa_keys
@@ -20,39 +22,95 @@ def test_encrypt_and_decrypt_with_keyring_with_user_defined_keys():
20
22
"""Test function for encrypt and decrypt using the Raw RSA Keyring example.
21
23
22
24
Here user provides the public and private keys. To test this, we create the
23
- keys using the generate_rsa_keys function
25
+ keys using the generate_rsa_keys function and write them to the file.
26
+ Then we call the encrypt_and_decrypt_with_keyring function and pass them
24
27
"""
28
+ # Generate the user keys for testing
25
29
user_public_key , user_private_key = generate_rsa_keys ()
26
- encrypt_and_decrypt_with_keyring (public_key = user_public_key , private_key = user_private_key )
30
+
31
+ # Convert the keys to strings
32
+ user_public_key = user_public_key .decode ('utf-8' )
33
+ user_private_key = user_private_key .decode ('utf-8' )
34
+
35
+ test_keys_directory = 'test_keys'
36
+ if not os .path .exists (test_keys_directory ):
37
+ os .makedirs (test_keys_directory )
38
+
39
+ # Define the file names for the keys
40
+ user_public_key_file_name = test_keys_directory + '/user_public_key_file_name.pem'
41
+ user_private_key_file_name = test_keys_directory + '/user_private_key_file_name.pem'
42
+
43
+ # Write the public key to the file
44
+ with open (user_public_key_file_name , "w" , encoding = "utf-8" ) as f :
45
+ f .write (user_public_key )
46
+
47
+ # Write the private key to the file
48
+ with open (user_private_key_file_name , "w" , encoding = "utf-8" ) as f :
49
+ f .write (user_private_key )
50
+
51
+ encrypt_and_decrypt_with_keyring (public_key_file_name = user_public_key_file_name ,
52
+ private_key_file_name = user_private_key_file_name )
27
53
28
54
29
55
def test_encrypt_and_decrypt_fails_if_user_provides_only_public_key ():
30
56
"""Test function for encrypt and decrypt using the Raw RSA Keyring example.
31
57
32
- Here user provides only the public key. The program should throw an Assertion error
58
+ Here user provides only the public key. The program should throw an Value error
33
59
as this example requires the user to either provide both private and public keys to
34
60
test both encryption and decryption, or not provide any keys and the example generates both
35
61
"""
62
+ # Generate the user keys for testing
36
63
user_public_key , user_private_key = generate_rsa_keys ()
64
+
65
+ # Convert the public key to string
66
+ user_public_key = user_public_key .decode ('utf-8' )
67
+
68
+ test_keys_directory = 'test_keys'
69
+ if not os .path .exists (test_keys_directory ):
70
+ os .makedirs (test_keys_directory )
71
+
72
+ # Define the file name for the public key
73
+ user_public_key_file_name = test_keys_directory + '/user_public_key_file_name.pem'
74
+
75
+ # Write the public key to the file
76
+ with open (user_public_key_file_name , "w" , encoding = "utf-8" ) as f :
77
+ f .write (user_public_key )
78
+
37
79
try :
38
- encrypt_and_decrypt_with_keyring (public_key = user_public_key )
80
+ encrypt_and_decrypt_with_keyring (public_key_file_name = user_public_key_file_name )
39
81
40
82
raise AssertionError ("encrypt_and_decrypt_with_keyring should raise an error" )
41
- except AssertionError :
83
+ except ValueError :
42
84
pass
43
85
44
86
45
87
def test_encrypt_and_decrypt_fails_if_user_provides_only_private_key ():
46
88
"""Test function for encrypt and decrypt using the Raw RSA Keyring example.
47
89
48
- Here user provides only the private key. The program should throw an Assertion error
90
+ Here user provides only the private key. The program should throw an Value error
49
91
as this example requires the user to either provide both private and public keys to
50
92
test both encryption and decryption, or not provide any keys and the example generates both
51
93
"""
94
+ # Generate the user keys for testing
52
95
user_public_key , user_private_key = generate_rsa_keys ()
96
+
97
+ # Convert the private key to string
98
+ user_private_key = user_private_key .decode ('utf-8' )
99
+
100
+ test_keys_directory = 'test_keys'
101
+ if not os .path .exists (test_keys_directory ):
102
+ os .makedirs (test_keys_directory )
103
+
104
+ # Define the file name for the private key
105
+ user_private_key_file_name = test_keys_directory + '/user_private_key_file_name.pem'
106
+
107
+ # Write the private key to the file
108
+ with open (user_private_key_file_name , "w" , encoding = "utf-8" ) as f :
109
+ f .write (user_private_key )
110
+
53
111
try :
54
- encrypt_and_decrypt_with_keyring (private_key = user_private_key )
112
+ encrypt_and_decrypt_with_keyring (private_key_file_name = user_private_key_file_name )
55
113
56
114
raise AssertionError ("encrypt_and_decrypt_with_keyring should raise an error" )
57
- except AssertionError :
115
+ except ValueError :
58
116
pass
0 commit comments