-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_cli_integration.py
More file actions
304 lines (250 loc) · 9.38 KB
/
test_cli_integration.py
File metadata and controls
304 lines (250 loc) · 9.38 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
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
"""
Integration Test CLI functionality
"""
import tempfile
from pathlib import Path
import pytest
from tests.support_cli_args import run_cli_decrypt, run_cli_encrypt
from tests.support_common import (
compare_tdf3_file_size,
handle_subprocess_error,
validate_plaintext_file_created,
validate_tdf3_file,
)
from tests.support_otdfctl_args import (
run_otdfctl_decrypt_command,
run_otdfctl_encrypt_command,
)
@pytest.mark.integration
def test_cli_decrypt_otdfctl_tdf(
collect_server_logs, temp_credentials_file, project_root
):
"""
Test that the Python CLI can successfully decrypt TDF files created by otdfctl.
"""
# Create temporary directory for work
with tempfile.TemporaryDirectory() as temp_dir:
temp_path = Path(temp_dir)
# Create input file
input_file = temp_path / "input.txt"
input_content = "Hello, World! This is a test for decryption."
with input_file.open("w") as f:
f.write(input_content)
# Define TDF file created by otdfctl
otdfctl_tdf_output = temp_path / "hello-world-otdfctl.txt.tdf"
# Define decrypted output from our CLI
cli_decrypt_output = temp_path / "decrypted-by-cli.txt"
# Run otdfctl encrypt
otdfctl_result = run_otdfctl_encrypt_command(
creds_file=temp_credentials_file,
input_file=input_file,
output_file=otdfctl_tdf_output,
mime_type="text/plain",
cwd=temp_path,
)
# Fail fast on errors
handle_subprocess_error(
result=otdfctl_result,
collect_server_logs=collect_server_logs,
scenario_name="otdfctl encrypt",
)
validate_tdf3_file(otdfctl_tdf_output, "otdfctl")
# Run our Python CLI decrypt on the otdfctl-created TDF
cli_decrypt_result = run_cli_decrypt(
creds_file=temp_credentials_file,
input_file=otdfctl_tdf_output,
output_file=cli_decrypt_output,
cwd=project_root,
)
# Fail fast on errors
handle_subprocess_error(
result=cli_decrypt_result,
collect_server_logs=collect_server_logs,
scenario_name="Python CLI decrypt",
)
validate_plaintext_file_created(
path=cli_decrypt_output,
scenario="Python decrypt",
expected_content=input_content,
)
@pytest.mark.integration
def test_otdfctl_decrypt_comparison(
collect_server_logs, temp_credentials_file, project_root
):
"""
Test comparative decryption between otdfctl and Python CLI on the same TDF.
"""
# Create temporary directory for work
with tempfile.TemporaryDirectory() as temp_dir:
temp_path = Path(temp_dir)
# Create input file
input_file = temp_path / "input.txt"
input_content = "Hello, World! This is a test for otdfctl decrypt comparison."
with input_file.open("w") as f:
f.write(input_content)
# Define TDF file created by otdfctl
otdfctl_tdf_output = temp_path / "hello-world-otdfctl.txt.tdf"
# Define decrypted outputs from both tools
otdfctl_decrypt_output = temp_path / "decrypted-by-otdfctl.txt"
cli_decrypt_output = temp_path / "decrypted-by-cli.txt"
# Run otdfctl encrypt first to create a TDF file
otdfctl_encrypt_result = run_otdfctl_encrypt_command(
creds_file=temp_credentials_file,
input_file=input_file,
output_file=otdfctl_tdf_output,
mime_type="text/plain",
cwd=temp_path,
)
# Fail fast on errors
handle_subprocess_error(
result=otdfctl_encrypt_result,
collect_server_logs=collect_server_logs,
scenario_name="otdfctl encrypt",
)
validate_tdf3_file(otdfctl_tdf_output, "otdfctl")
# Now run otdfctl decrypt (this is the reference implementation)
otdfctl_decrypt_result = run_otdfctl_decrypt_command(
temp_credentials_file,
otdfctl_tdf_output,
otdfctl_decrypt_output,
cwd=temp_path,
)
# Fail fast on errors
handle_subprocess_error(
result=otdfctl_decrypt_result,
collect_server_logs=collect_server_logs,
scenario_name="otdfctl decrypt",
)
cli_decrypt_result = run_cli_decrypt(
creds_file=temp_credentials_file,
input_file=otdfctl_tdf_output,
output_file=cli_decrypt_output,
cwd=project_root,
)
# Fail fast on errors
handle_subprocess_error(
result=cli_decrypt_result,
collect_server_logs=collect_server_logs,
scenario_name="Python CLI decrypt",
)
validate_plaintext_file_created(
path=otdfctl_decrypt_output,
scenario="otdfctl",
expected_content=input_content,
)
validate_plaintext_file_created(
path=cli_decrypt_output,
scenario="Python CLI",
expected_content=input_content,
)
@pytest.mark.integration
def test_otdfctl_encrypt_decrypt_roundtrip(collect_server_logs, temp_credentials_file):
"""
Test complete encrypt-decrypt roundtrip using otdfctl to verify functionality.
"""
# Create temporary directory for work
with tempfile.TemporaryDirectory() as temp_dir:
temp_path = Path(temp_dir)
# Create input file
input_file = temp_path / "input.txt"
input_content = (
"Hello, World! This is a test for otdfctl roundtrip encryption/decryption."
)
with input_file.open("w") as f:
f.write(input_content)
# Define TDF file and decrypted output
otdfctl_tdf_output = temp_path / "otdfctl-roundtrip.txt.tdf"
otdfctl_decrypt_output = temp_path / "otdfctl-roundtrip-decrypted.txt"
# Run otdfctl encrypt
otdfctl_encrypt_result = run_otdfctl_encrypt_command(
creds_file=temp_credentials_file,
input_file=input_file,
output_file=otdfctl_tdf_output,
mime_type="text/plain",
cwd=temp_path,
)
# Fail fast on errors
handle_subprocess_error(
result=otdfctl_encrypt_result,
collect_server_logs=collect_server_logs,
scenario_name="otdfctl encrypt",
)
# Verify the TDF file was created
validate_tdf3_file(otdfctl_tdf_output, "otdfctl")
# Run otdfctl decrypt
otdfctl_decrypt_result = run_otdfctl_decrypt_command(
temp_credentials_file,
otdfctl_tdf_output,
otdfctl_decrypt_output,
cwd=temp_path,
)
# Fail fast on errors
handle_subprocess_error(
result=otdfctl_decrypt_result,
collect_server_logs=collect_server_logs,
scenario_name="otdfctl decrypt",
)
validate_plaintext_file_created(
path=otdfctl_decrypt_output,
scenario="otdfctl",
expected_content=input_content,
)
# Verify file sizes are reasonable
original_size = input_file.stat().st_size
tdf_size = otdfctl_tdf_output.stat().st_size
decrypted_size = otdfctl_decrypt_output.stat().st_size
assert tdf_size > original_size, "TDF file should be larger than original"
assert decrypted_size == original_size, (
"Decrypted file should match original size"
)
print(
f"✓ otdfctl roundtrip successful: {original_size} bytes -> {tdf_size} bytes -> {decrypted_size} bytes"
)
@pytest.mark.integration
def test_cli_encrypt_integration(
collect_server_logs, temp_credentials_file, project_root
):
"""Integration test comparing our CLI with otdfctl"""
# Create temporary directory for work
with tempfile.TemporaryDirectory() as temp_dir:
temp_path = Path(temp_dir)
# Create input file
input_file = temp_path / "input.txt"
input_content = "Hello, World"
with input_file.open("w") as f:
f.write(input_content)
# Define output files
otdfctl_output = temp_path / "hello-world-otdfctl.txt.tdf"
cli_output = temp_path / "hello-world-cli.txt.tdf"
# Run otdfctl encrypt
otdfctl_result = run_otdfctl_encrypt_command(
creds_file=temp_credentials_file,
input_file=input_file,
output_file=otdfctl_output,
mime_type="text/plain",
cwd=temp_path,
)
# Fail fast on errors
handle_subprocess_error(
result=otdfctl_result,
collect_server_logs=collect_server_logs,
scenario_name="otdfctl encrypt",
)
# Run our Python CLI encrypt
cli_result = run_cli_encrypt(
creds_file=temp_credentials_file,
input_file=input_file,
output_file=cli_output,
mime_type="text/plain",
attributes=None,
cwd=project_root,
)
# Fail fast on errors
handle_subprocess_error(
result=cli_result,
collect_server_logs=collect_server_logs,
scenario_name="Python CLI encrypt",
)
validate_tdf3_file(otdfctl_output, "otdfctl")
validate_tdf3_file(cli_output, "Python CLI")
compare_tdf3_file_size(otdfctl_output, cli_output)