-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_encrypted_storage.py
More file actions
223 lines (184 loc) 路 7.99 KB
/
Copy pathtest_encrypted_storage.py
File metadata and controls
223 lines (184 loc) 路 7.99 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
#!/usr/bin/env python3
"""
Comprehensive Test Suite for Encrypted Local Storage Feature
Tests the encryption implementation in /workspace/src/history.sh
"""
import subprocess
import os
import sys
import json
import tempfile
import shutil
class TestEncryptedStorage:
def __init__(self):
self.test_dir = None
self.passed = 0
self.failed = 0
self.results = []
def setup(self):
self.test_dir = tempfile.mkdtemp(prefix='orchat_encrypt_test_')
print(f"[SETUP] Test directory: {self.test_dir}")
def teardown(self):
if self.test_dir and os.path.exists(self.test_dir):
shutil.rmtree(self.test_dir)
print(f"[TEARDOWN] Cleaned up test directory")
def log_result(self, test_name, passed, details=""):
status = "PASS" if passed else "FAIL"
self.results.append((test_name, passed, details))
if passed:
self.passed += 1
else:
self.failed += 1
print(f"{status}: {test_name}")
if details:
print(f" {details}")
def test_encryption_code_exists(self):
history_sh_path = "/workspace/src/history.sh"
if not os.path.exists(history_sh_path):
self.log_result("Encryption code exists", False, "history.sh not found")
return False
with open(history_sh_path, 'r') as f:
content = f.read()
checks = [
'_encrypt_data',
'_decrypt_data',
'cryptography.fernet',
'Fernet(',
'.encrypt(',
'.decrypt(',
'ENCRYPTION_KEY',
'ENCRYPTION_ENABLED',
]
missing = []
for pattern in checks:
if pattern not in content:
missing.append(pattern)
if missing:
self.log_result("Encryption code exists", False, f"Missing: {', '.join(missing)}")
return False
else:
self.log_result("Encryption code exists", True, "All encryption components found")
return True
def test_encrypt_decrypt_roundtrip(self):
test_content = '{"test": "hello world", "number": 42}'
test_key = "0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef"
script = f'''source /workspace/src/history.sh
export ORCHAT_ENCRYPTION_ENABLED=true
export ORCHAT_ENCRYPTION_KEY="{test_key}"
_refresh_encryption_settings
encrypted=$(_encrypt_data '{test_content}' "$ENCRYPTION_KEY")
echo "ENCRYPTED:$encrypted"
decrypted=$(_decrypt_data "$encrypted" "$ENCRYPTION_KEY")
echo "DECRYPTED:$decrypted"'''
result = subprocess.run(['bash', '-c', script], capture_output=True, text=True)
lines = result.stdout.split('\n')
encrypted_line = [l for l in lines if l.startswith('ENCRYPTED:')][0]
decrypted_line = [l for l in lines if l.startswith('DECRYPTED:')][0]
encrypted = encrypted_line.replace('ENCRYPTED:', '')
decrypted = decrypted_line.replace('DECRYPTED:', '')
if decrypted == test_content:
self.log_result("Encrypt/Decrypt roundtrip", True, f"Original: {test_content[:30]}...")
return True
else:
self.log_result("Encrypt/Decrypt roundtrip", False, f"Expected: {test_content}, Got: {decrypted}")
return False
def test_encrypted_file_storage(self):
test_key = "fedcba9876543210fedcba9876543210fedcba9876543210fedcba9876543210"
script = f'''export ORCHAT_HISTORY_DIR="{self.test_dir}"
export ORCHAT_ENCRYPTION_ENABLED=true
export ORCHAT_ENCRYPTION_KEY="{test_key}"
source /workspace/src/history.sh
hf=$(history_init "test_session")
echo "HISTORY_FILE:$hf"
history_add "$hf" "user" "Hello, this is a test message"
raw_content=$(cat "$hf")
echo "RAW_CONTENT:$raw_content"
messages=$(history_get_messages "$hf")
echo "MESSAGES:$messages"'''
result = subprocess.run(['bash', '-c', script], capture_output=True, text=True)
lines = result.stdout.split('\n')
raw_content = [l for l in lines if l.startswith('RAW_CONTENT:')][0].replace('RAW_CONTENT:', '')
messages = [l for l in lines if l.startswith('MESSAGES:')][0].replace('MESSAGES:', '')
is_encrypted = not raw_content.strip().startswith('[')
try:
msg_data = json.loads(messages)
has_correct_message = any('Hello, this is a test message' in str(m) for m in msg_data)
except:
has_correct_message = False
if is_encrypted and has_correct_message:
self.log_result("Encrypted file storage", True, "File stored encrypted, retrieved correctly")
return True
else:
details = []
if not is_encrypted:
details.append("File not encrypted")
if not has_correct_message:
details.append("Message retrieval failed")
self.log_result("Encrypted file storage", False, ', '.join(details))
return False
def test_key_generation(self):
script = f'''export HOME="{self.test_dir}"
export ORCHAT_ENCRYPTION_ENABLED=true
unset ORCHAT_ENCRYPTION_KEY
source /workspace/src/history.sh
_init_encryption_key
echo "KEY_GENERATED:$ENCRYPTION_KEY"
echo "KEY_LENGTH:${{#ENCRYPTION_KEY}}"
if [[ -f "$HOME/.orchat/.encryption_key" ]]; then
echo "KEY_FILE_EXISTS:true"
else
echo "KEY_FILE_EXISTS:false"
fi'''
result = subprocess.run(['bash', '-c', script], capture_output=True, text=True)
lines = result.stdout.split('\n')
key = [l for l in lines if l.startswith('KEY_GENERATED:')][0].replace('KEY_GENERATED:', '')
key_length = [l for l in lines if l.startswith('KEY_LENGTH:')][0].replace('KEY_LENGTH:', '')
key_file_exists = [l for l in lines if l.startswith('KEY_FILE_EXISTS:')][0].replace('KEY_FILE_EXISTS:', '')
if key and int(key_length) >= 32 and key_file_exists == 'true':
self.log_result("Key generation", True, f"Key length: {key_length}, file saved")
return True
else:
self.log_result("Key generation", False, f"Key: {key[:10]}..., Length: {key_length}, File: {key_file_exists}")
return False
def test_disabled_encryption(self):
script = f'''export ORCHAT_HISTORY_DIR="{self.test_dir}"
export ORCHAT_ENCRYPTION_ENABLED=false
unset ORCHAT_ENCRYPTION_KEY
source /workspace/src/history.sh
hf=$(history_init "disabled_session")
history_add "$hf" "user" "Unencrypted message"
cat "$hf"'''
result = subprocess.run(['bash', '-c', script], capture_output=True, text=True)
raw_content = result.stdout.strip()
try:
data = json.loads(raw_content)
is_plain_json = isinstance(data, list)
except:
is_plain_json = False
if is_plain_json:
self.log_result("Disabled encryption", True, "Data stored as plain JSON")
return True
else:
self.log_result("Disabled encryption", False, f"Data not stored as plain JSON. Content: {raw_content[:100]}")
return False
def run_all_tests(self):
print("=" * 60)
print("ENCRYPTED LOCAL STORAGE TEST SUITE")
print("=" * 60)
self.setup()
try:
self.test_encryption_code_exists()
self.test_encrypt_decrypt_roundtrip()
self.test_encrypted_file_storage()
self.test_key_generation()
self.test_disabled_encryption()
finally:
self.teardown()
print("\n" + "=" * 60)
print(f"RESULTS: {self.passed} passed, {self.failed} failed")
print("=" * 60)
return self.failed == 0
if __name__ == '__main__':
tester = TestEncryptedStorage()
success = tester.run_all_tests()
sys.exit(0 if success else 1)