-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_project.py
More file actions
242 lines (196 loc) · 8.86 KB
/
test_project.py
File metadata and controls
242 lines (196 loc) · 8.86 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
import pytest
import pathlib
from hybrid_crypto import genarate_keys, encryption, decryption
from hybrid_crypto import addon
TESTING_DIR = "__testfiles__/testing_module/"
TEST_FILES = {
"txt": "__testfiles__/inputs/massages.txt",
"audio": "__testfiles__/inputs/house-chords-12112.mp3",
"image": "__testfiles__/inputs/cat-551554_1280.jpg",
"vedio": "__testfiles__/inputs/145308_(1080p).mp4",
}
TEST_SENDER_KEYS_DIR = "__testfiles__/keys/KEYS_20240305_110825_858294"
TEST_RECIVER_KEYS_DIR = "__testfiles__/keys/KEYS_20240305_110840_641535"
def test_genarate_keys():
if not pathlib.Path(TESTING_DIR).exists():
pathlib.Path(TESTING_DIR).mkdir(exist_ok=True)
# Testing genarate keys
key_dir_name = genarate_keys.genarate_RSA_key(
pathlib.Path(TESTING_DIR).joinpath("genarate_keys")
)
keys_dir_path = pathlib.Path(TESTING_DIR).joinpath("genarate_keys", key_dir_name)
assert keys_dir_path.exists() == True
assert keys_dir_path.joinpath("private.pem").exists() == True
assert keys_dir_path.joinpath("public.pem").exists() == True
# Testing Exception Validation
with pytest.raises(TypeError):
genarate_keys.genarate_RSA_key()
with pytest.raises(TypeError):
genarate_keys.genarate_RSA_key(1234)
def test_encryption_file():
if not pathlib.Path(TESTING_DIR).exists():
pathlib.Path(TESTING_DIR).mkdir(exist_ok=True)
enc_test_path = pathlib.Path(TESTING_DIR).joinpath("encryption")
if not enc_test_path.exists():
enc_test_path.mkdir(exist_ok=True)
# Testing encryption file
enc_file_path = encryption.file_encryption(
pathlib.Path(TEST_FILES["txt"]),
enc_test_path.joinpath("encryption_file.enc"),
pathlib.Path(TEST_RECIVER_KEYS_DIR).joinpath("public.pem"),
pathlib.Path(TEST_SENDER_KEYS_DIR).joinpath("private.pem"),
)
assert pathlib.Path(enc_file_path).exists() == True
# Testing Exception Validation
with pytest.raises(FileNotFoundError):
encryption.file_encryption(
"filenotfound.txt",
enc_test_path.joinpath("encryption_file.enc"),
pathlib.Path(TEST_RECIVER_KEYS_DIR).joinpath("public.pem"),
pathlib.Path(TEST_SENDER_KEYS_DIR).joinpath("private.pem"),
)
with pytest.raises(FileNotFoundError):
encryption.file_encryption(
pathlib.Path(TEST_FILES["txt"]),
enc_test_path.joinpath("dir_not_exist/encryption_file.enc"),
pathlib.Path(TEST_RECIVER_KEYS_DIR).joinpath("public.pem"),
pathlib.Path(TEST_SENDER_KEYS_DIR).joinpath("private.pem"),
)
with pytest.raises(FileNotFoundError):
encryption.file_encryption(
pathlib.Path(TEST_FILES["txt"]),
enc_test_path.joinpath("encryption_file.enc"),
pathlib.Path(TEST_RECIVER_KEYS_DIR).joinpath("publickeyfilenotfound.pem"),
pathlib.Path(TEST_SENDER_KEYS_DIR).joinpath("private.pem"),
)
with pytest.raises(FileNotFoundError):
encryption.file_encryption(
pathlib.Path(TEST_FILES["txt"]),
enc_test_path.joinpath("encryption_file.enc"),
pathlib.Path(TEST_RECIVER_KEYS_DIR).joinpath("public.pem"),
pathlib.Path(TEST_SENDER_KEYS_DIR).joinpath("privatekeyfilenotfound.pem"),
)
def test_encryption_text():
if not pathlib.Path(TESTING_DIR).exists():
pathlib.Path(TESTING_DIR).mkdir(exist_ok=True)
enc_test_path = pathlib.Path(TESTING_DIR).joinpath("encryption")
# Testing encryption text
if not enc_test_path.exists():
enc_test_path.mkdir(exist_ok=True)
enc_text_file_path = encryption.text_encryption(
"Maow maow mmmaaaoowwwww",
pathlib.Path(enc_test_path).joinpath("encryption_text.enc"),
pathlib.Path(TEST_RECIVER_KEYS_DIR).joinpath("public.pem"),
pathlib.Path(TEST_SENDER_KEYS_DIR).joinpath("private.pem"),
)
assert pathlib.Path(enc_text_file_path).exists() == True
# Testing exception validation
with pytest.raises(FileNotFoundError):
encryption.text_encryption(
"Maow maow mmmaaaoowwwww",
pathlib.Path(enc_test_path).joinpath(
"dir_not_exist", "encryption_text.enc"
),
pathlib.Path(TEST_RECIVER_KEYS_DIR).joinpath("public.pem"),
pathlib.Path(TEST_SENDER_KEYS_DIR).joinpath("private.pem"),
)
with pytest.raises(FileNotFoundError):
encryption.text_encryption(
"Maow maow mmmaaaoowwwww",
pathlib.Path(enc_test_path).joinpath("encryption_text.enc"),
pathlib.Path(TEST_RECIVER_KEYS_DIR).joinpath("public_not_found.pem"),
pathlib.Path(TEST_SENDER_KEYS_DIR).joinpath("private.pem"),
)
with pytest.raises(FileNotFoundError):
encryption.text_encryption(
"Maow maow mmmaaaoowwwww",
pathlib.Path(enc_test_path).joinpath("encryption_text.enc"),
pathlib.Path(TEST_RECIVER_KEYS_DIR).joinpath("public.pem"),
pathlib.Path(TEST_SENDER_KEYS_DIR).joinpath("private_not_found.pem"),
)
def test_decryption_file():
if not pathlib.Path(TESTING_DIR).exists():
pathlib.Path(TESTING_DIR).mkdir(exist_ok=True)
dec_test_path = pathlib.Path(TESTING_DIR).joinpath("decryption")
if not dec_test_path.exists():
dec_test_path.mkdir(exist_ok=True)
# decrypt text
enc_text_file_path = encryption.text_encryption(
"Maow maow mmmaaaoowwwww",
pathlib.Path(dec_test_path).joinpath("encryption.enc"),
pathlib.Path(TEST_RECIVER_KEYS_DIR).joinpath("public.pem"),
pathlib.Path(TEST_SENDER_KEYS_DIR).joinpath("private.pem"),
)
dec_text_file_path = decryption.file_decryption(
pathlib.Path(enc_text_file_path),
dec_test_path,
pathlib.Path(TEST_RECIVER_KEYS_DIR).joinpath("private.pem"),
pathlib.Path(TEST_SENDER_KEYS_DIR).joinpath("public.pem"),
)
assert pathlib.Path(dec_text_file_path).exists() == True
with open(dec_text_file_path, "r") as dec_text_file:
text_data = dec_text_file.read()
assert addon.get_text_SHA256(
"Maow maow mmmaaaoowwwww".encode("utf-8")
) == addon.get_text_SHA256(text_data.encode("utf-8"))
# Decrept file
enc_file_path = encryption.file_encryption(
pathlib.Path(TEST_FILES["image"]),
pathlib.Path(dec_test_path).joinpath("encryption_file.enc"),
pathlib.Path(TEST_RECIVER_KEYS_DIR).joinpath("public.pem"),
pathlib.Path(TEST_SENDER_KEYS_DIR).joinpath("private.pem"),
)
dec_file_path = decryption.file_decryption(
pathlib.Path(enc_file_path),
dec_test_path,
pathlib.Path(TEST_RECIVER_KEYS_DIR).joinpath("private.pem"),
pathlib.Path(TEST_SENDER_KEYS_DIR).joinpath("public.pem"),
)
assert pathlib.Path(enc_file_path).exists() == True
assert addon.get_SHA256(
pathlib.Path(TEST_FILES["image"]), True
) == addon.get_SHA256(pathlib.Path(dec_file_path), True)
# Exception Validation
with pytest.raises(
decryption.KeyNotDecryptedError,
):
enc_text_file_wrong_key_path = encryption.text_encryption(
"Maow maow mmmaaaoowwwww",
pathlib.Path(dec_test_path).joinpath("encryption_wrong_key.enc"),
pathlib.Path(TEST_SENDER_KEYS_DIR).joinpath("public.pem"),
pathlib.Path(TEST_RECIVER_KEYS_DIR).joinpath("private.pem"),
)
decryption.file_decryption(
pathlib.Path(enc_text_file_wrong_key_path),
dec_test_path,
pathlib.Path(TEST_RECIVER_KEYS_DIR).joinpath("private.pem"),
pathlib.Path(TEST_SENDER_KEYS_DIR).joinpath("public.pem"),
)
with pytest.raises(FileNotFoundError):
decryption.file_decryption(
"not_valid_path.enc",
dec_test_path,
pathlib.Path(TEST_RECIVER_KEYS_DIR).joinpath("private.pem"),
pathlib.Path(TEST_SENDER_KEYS_DIR).joinpath("public.pem"),
)
with pytest.raises(FileNotFoundError):
decryption.file_decryption(
pathlib.Path(enc_text_file_path),
dec_test_path.joinpath("dir_not_exist"),
pathlib.Path(TEST_RECIVER_KEYS_DIR).joinpath("private.pem"),
pathlib.Path(TEST_SENDER_KEYS_DIR).joinpath("public.pem"),
)
with pytest.raises(FileNotFoundError):
decryption.file_decryption(
pathlib.Path(enc_text_file_path),
dec_test_path,
pathlib.Path(TEST_RECIVER_KEYS_DIR).joinpath("private_not_found.pem"),
pathlib.Path(TEST_SENDER_KEYS_DIR).joinpath("public.pem"),
)
with pytest.raises(FileNotFoundError):
decryption.file_decryption(
pathlib.Path(enc_text_file_path),
dec_test_path,
pathlib.Path(TEST_RECIVER_KEYS_DIR).joinpath("private.pem"),
pathlib.Path(TEST_SENDER_KEYS_DIR).joinpath("public_not_found.pem"),
)