-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.py
306 lines (271 loc) · 12.5 KB
/
test.py
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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
import unittest
from unittest.mock import patch
import scryptopy
import tempfile
from pathlib import Path
import json
import csv
import shutil
import filecmp
def write_lorem_ipsum(fname):
with open(fname, 'w') as file:
file.write('''
Lorem ipsum dolor sit amet,
consectetur adipiscing elit,
sed do eiusmod tempor incididunt
ut labore et dolore magna aliqua.
''')
def write_csv_spam(fname):
with open(fname, 'w', newline='') as csvfile:
spamwriter = csv.writer(csvfile, delimiter=' ',
quotechar='|', quoting=csv.QUOTE_MINIMAL)
spamwriter.writerow(['Egg', 'bacon'])
spamwriter.writerow(['Spam'] * 5 + ['Baked Beans'])
spamwriter.writerow(['Spam', 'Lovely Spam', 'Wonderful Spam'])
def write_csv_dict(fname):
with open(fname, 'w', newline='') as csvfile:
fieldnames = ['first_name', 'last_name']
writer = csv.DictWriter(csvfile, fieldnames=fieldnames)
writer.writeheader()
writer.writerow({'first_name': 'Baked', 'last_name': 'Beans'})
writer.writerow({'first_name': 'Lovely', 'last_name': 'Spam'})
writer.writerow({'first_name': 'Wonderful', 'last_name': 'Spam'})
class FileEncDecCheckTestCase(unittest.TestCase):
def __test_file(self, fname, create_func, keys):
unencrypted_path = self.temp_dir_path / 'unencrypted' / fname
encrypted_path = self.temp_dir_path / 'encrypted' / fname
decrypted_path = self.temp_dir_path / 'decrypted' / fname
keys_path = self.temp_dir_path / 'keys' / fname
create_func(unencrypted_path)
with open(keys_path, 'w') as f:
f.write(json.dumps(keys))
scryptopy.encrypt(
input=unencrypted_path,
output=encrypted_path,
keyfile=keys_path,
double_check=True,
sync=True,
encrypted_dirnames=True,
confirm=False)
scryptopy.check(
unencrypted=unencrypted_path,
encrypted=encrypted_path,
keyfile=keys_path,
encrypted_dirnames=True,
confirm=False)
scryptopy.decrypt(
input=encrypted_path,
output=decrypted_path,
keyfile=keys_path,
sync=True,
encrypted_dirnames=True,
confirm=False)
self.assertTrue(filecmp.cmp(unencrypted_path, decrypted_path, shallow=False))
unencrypted_path.unlink()
encrypted_path.unlink()
decrypted_path.unlink()
keys_path.unlink()
def setUp(self):
self.temp_dir = tempfile.TemporaryDirectory()
self.temp_dir_path = Path(self.temp_dir.name)
(self.temp_dir_path / 'unencrypted').mkdir()
(self.temp_dir_path / 'encrypted').mkdir()
(self.temp_dir_path / 'decrypted').mkdir()
(self.temp_dir_path / 'keys').mkdir()
def tearDown(self):
self.temp_dir.cleanup()
def test_lorem_ipsum(self):
keys = {
'keys': [
[{'algorithm': 'AES256', 'passphrase_template': 'prefix{salt0}suffix', 'num_salts': 5}],
[{'algorithm': 'AES256', 'passphrase_template': 'prefix{salt1}suffix', 'num_salts': 5}],
[{'algorithm': 'AES256', 'passphrase_template': 'prefix{salt3}suffix', 'num_salts': 5}]
],
'data_key_index': 0,
'filename_key_index': 1,
'dirname_key_index': 2
}
self.__test_file('lorem.txt', write_lorem_ipsum, keys)
def test_csv_spam(self):
keys = {
'keys': [
[
{'algorithm': 'TWOFISH', 'passphrase_template': '???{salt1}{salt3}!!!', 'num_salts': 5},
{'algorithm': 'AES256', 'passphrase_template': 'prefix{salt4}suffix', 'num_salts': 5}
],
[{'algorithm': 'AES256', 'passphrase_template': 'prefixsuffix', 'num_salts': 5}],
[{'algorithm': 'AES256', 'passphrase_template': 'prefix{salt3}suffix', 'num_salts': 5}]
],
'data_key_index': 0,
'filename_key_index': 2,
'dirname_key_index': 1
}
self.__test_file('spam.csv', write_csv_spam, keys)
def test_csv_dict(self):
keys = {
'keys': [
[{'algorithm': 'AES256', 'passphrase_template': '{salt4}', 'num_salts': 5}],
[
{'algorithm': 'TWOFISH', 'passphrase_template': '{salt0}{salt3}', 'num_salts': 4},
{'algorithm': 'AES256', 'passphrase_template': 'prefixsuffix', 'num_salts': 0}
],
[{'algorithm': 'AES256', 'passphrase_template': 'prefix{salt3}suffix', 'num_salts': 5}]
],
'data_key_index': 1,
'filename_key_index': 0,
'dirname_key_index': 1
}
self.__test_file('dict.csv', write_csv_dict, keys)
class DirectoryEncDecCheckTestCase(unittest.TestCase):
def __test_directory(self, dname, encrypted_dirnames):
unencrypted_path = self.temp_dir_path / 'unencrypted' / dname
encrypted_path = self.temp_dir_path / 'encrypted' / dname
decrypted_path = self.temp_dir_path / 'decrypted' / dname
keys_path = self.temp_dir_path / 'keys' / dname
with patch('click.echo_via_pager', return_value=None) as click_echo_mock, \
patch('click.prompt', side_effect=('r', 'y')) as click_prompt_mock:
scryptopy.encrypt(
input=unencrypted_path,
output=encrypted_path,
keyfile=keys_path,
double_check=True,
sync=True,
encrypted_dirnames=encrypted_dirnames,
confirm=True)
with patch('click.echo_via_pager', return_value=None) as click_echo_mock, \
patch('click.prompt', side_effect=('r', 'y')) as click_prompt_mock:
scryptopy.check(
unencrypted=unencrypted_path,
encrypted=encrypted_path,
keyfile=keys_path,
encrypted_dirnames=encrypted_dirnames,
confirm=True)
with patch('click.echo_via_pager', return_value=None) as click_echo_mock, \
patch('click.prompt', side_effect=('r', 'y')) as click_prompt_mock:
scryptopy.decrypt(
input=encrypted_path,
output=decrypted_path,
keyfile=keys_path,
sync=True,
encrypted_dirnames=encrypted_dirnames,
confirm=True)
dircmp = filecmp.dircmp(unencrypted_path, decrypted_path, ignore=None, hide=None)
self.assertEqual(dircmp.right_only, [])
self.assertEqual(dircmp.left_only, [])
self.assertEqual(dircmp.diff_files, [])
self.assertEqual(dircmp.funny_files, [])
shutil.rmtree(encrypted_path)
shutil.rmtree(decrypted_path)
keys_path.unlink()
def setUp(self):
self.temp_dir = tempfile.TemporaryDirectory()
self.temp_dir_path = Path(self.temp_dir.name)
(self.temp_dir_path / 'unencrypted').mkdir()
(self.temp_dir_path / 'encrypted').mkdir()
(self.temp_dir_path / 'decrypted').mkdir()
(self.temp_dir_path / 'keys').mkdir()
def tearDown(self):
self.temp_dir.cleanup()
def __generate_keys(self):
return {
'keys': [
[
{'algorithm': '3DES', 'passphrase_template': 'some{salt0}_e{salt4}{salt1}nc_{salt2}key{salt3}', 'num_salts': 5},
{'algorithm': 'AES256', 'passphrase_template': 'prefix{salt0}suffix', 'num_salts': 1},
{'algorithm': 'CAMELLIA256', 'passphrase_template': 'some_key', 'num_salts': 0},
{'algorithm': 'TWOFISH', 'passphrase_template': '???{salt1}!!!', 'num_salts': 2},
{'algorithm': 'BLOWFISH', 'passphrase_template': '{salt0}', 'num_salts': 1}
]
],
'data_key_index': 0,
'filename_key_index': 0,
'dirname_key_index': 0
}
def __test_subfolders(self, dname, encrypted_dirnames):
unencrypted_path = self.temp_dir_path / 'unencrypted' / dname
unencrypted_path.mkdir()
(unencrypted_path / 'texts').mkdir()
write_lorem_ipsum(unencrypted_path / 'texts' / 'lorem.txt')
(unencrypted_path / 'CSVs').mkdir()
write_csv_spam(unencrypted_path / 'CSVs' / 'spam.csv')
write_csv_dict(unencrypted_path / 'CSVs' / 'dict.csv')
keys_path = self.temp_dir_path / 'keys' / dname
with open(keys_path, 'w') as f:
f.write(json.dumps(self.__generate_keys()))
self.__test_directory(dname, encrypted_dirnames)
shutil.rmtree(unencrypted_path)
def __test_subfolders_partial(self, dname, encrypted_dirnames):
unencrypted_path = self.temp_dir_path / 'unencrypted' / dname
encrypted_path = self.temp_dir_path / 'encrypted' / dname
decrypted_path = self.temp_dir_path / 'decrypted' / dname
unencrypted_path.mkdir()
write_lorem_ipsum(unencrypted_path / 'lorem1.txt')
write_lorem_ipsum(unencrypted_path / 'lorem3.txt')
write_csv_spam(unencrypted_path / 'lorem4.txt')
(unencrypted_path / 'texts').mkdir()
write_lorem_ipsum(unencrypted_path / 'texts' / 'lorem1.txt')
write_lorem_ipsum(unencrypted_path / 'texts' / 'lorem3.txt')
write_csv_spam(unencrypted_path / 'texts' / 'lorem4.txt')
(unencrypted_path / 'CSVs').mkdir()
(unencrypted_path / 'CSVs' / '1').mkdir()
write_csv_spam(unencrypted_path / 'CSVs' / '1' / 'spam.csv')
write_csv_dict(unencrypted_path / 'CSVs' / '1' /'dict.csv')
(unencrypted_path / 'CSVs' / '3').mkdir()
write_csv_spam(unencrypted_path / 'CSVs' / '3' /'spam.csv')
write_csv_dict(unencrypted_path / 'CSVs' / '3' /'dict.csv')
(unencrypted_path / 'CSVs' / '4').mkdir()
write_csv_dict(unencrypted_path / 'CSVs' / '4' /'spam.csv')
write_csv_spam(unencrypted_path / 'CSVs' / '4' /'dict.csv')
keys_path = self.temp_dir_path / 'keys' / dname
with open(keys_path, 'w') as f:
f.write(json.dumps(self.__generate_keys()))
scryptopy.encrypt(
input=unencrypted_path,
output=encrypted_path,
keyfile=keys_path,
double_check=True,
sync=True,
encrypted_dirnames=encrypted_dirnames,
confirm=False)
scryptopy.decrypt(
input=encrypted_path,
output=decrypted_path,
keyfile=keys_path,
sync=True,
encrypted_dirnames=encrypted_dirnames,
confirm=False)
write_lorem_ipsum(unencrypted_path / 'lorem2.txt')
write_lorem_ipsum(unencrypted_path / 'texts' / 'lorem2.txt')
(unencrypted_path / 'CSVs' / '2').mkdir()
write_csv_spam(unencrypted_path / 'CSVs' / '2' /'spam.csv')
write_csv_dict(unencrypted_path / 'CSVs' / '2' /'dict.csv')
(unencrypted_path / 'lorem3.txt').unlink()
(unencrypted_path / 'texts' / 'lorem3.txt').unlink()
shutil.rmtree(unencrypted_path / 'CSVs' / '3')
(unencrypted_path / 'lorem4.txt').unlink()
(unencrypted_path / 'texts' / 'lorem4.txt').unlink()
shutil.rmtree(unencrypted_path / 'CSVs' / '4')
write_lorem_ipsum(unencrypted_path / 'lorem4.txt')
write_lorem_ipsum(unencrypted_path / 'texts' / 'lorem4.txt')
(unencrypted_path / 'CSVs' / '4').mkdir()
write_csv_spam(unencrypted_path / 'CSVs' / '4' /'spam.csv')
write_csv_dict(unencrypted_path / 'CSVs' / '4' /'dict.csv')
self.assertEqual(
scryptopy.check(
unencrypted=unencrypted_path,
encrypted=encrypted_path,
keyfile=keys_path,
encrypted_dirnames=encrypted_dirnames,
confirm=False), 1)
self.__test_directory(dname, encrypted_dirnames)
shutil.rmtree(unencrypted_path)
def test_subfolders_full_encrypted_dirnames(self):
self.__test_subfolders('full_encrypted_dirnames', encrypted_dirnames=True)
def test_subfolders_full_not_encrypted_dirnames(self):
self.__test_subfolders('full_not_encrypted_dirnames', encrypted_dirnames=False)
def test_subfolders_partial_encrypted_dirnames(self):
self.__test_subfolders_partial('partial_encrypted_dirnames', encrypted_dirnames=True)
def test_subfolders_partial_not_encrypted_dirnames(self):
self.__test_subfolders_partial('partial_not_encrypted_dirnames', encrypted_dirnames=False)
if __name__ == '__main__':
unittest.main('test')