-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_crypto.lua
More file actions
154 lines (132 loc) · 5.47 KB
/
test_crypto.lua
File metadata and controls
154 lines (132 loc) · 5.47 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
#!/usr/bin/env lua
-- test_crypto.lua - Test script for conversation encryption functionality
-- This script tests the crypto module and conversation manager encryption features
-- Add the codingbuddy directory to package path
package.path = './codingbuddy/?.lua;' .. package.path
local crypto = require('crypto')
local utils = require('utils')
local config = require('config')
local conversation_manager = require('conversation_manager')
print('=== CodingBuddy Crypto Test Suite ===')
print()
-- Test 1: Check if OpenSSL is available
print('Test 1: Checking OpenSSL availability...')
local openssl_ok, openssl_info = crypto.check_openssl()
if openssl_ok then
print('✓ OpenSSL is available: ' .. openssl_info)
else
print('✗ OpenSSL not available: ' .. tostring(openssl_info))
print('Please install OpenSSL to use encryption features.')
os.exit(1)
end
print()
-- Test 2: Check passphrase environment variable
print('Test 2: Checking passphrase environment variable...')
local passphrase = os.getenv('CODINGBUDDY_PASSPHRASE')
if passphrase and passphrase ~= '' then
print('✓ CODINGBUDDY_PASSPHRASE is set (length: ' .. #passphrase .. ' characters)')
else
print('✗ CODINGBUDDY_PASSPHRASE environment variable not set or empty')
print('Please set the passphrase: export CODINGBUDDY_PASSPHRASE="your-secure-passphrase"')
os.exit(1)
end
print()
-- Test 3: Test basic crypto functionality
print('Test 3: Testing basic crypto functionality...')
local crypto_test_ok, crypto_test_err = crypto.test_crypto()
if crypto_test_ok then
print('✓ Crypto functionality test passed')
else
print('✗ Crypto functionality test failed: ' .. tostring(crypto_test_err))
os.exit(1)
end
print()
-- Test 4: Test conversation encryption (disabled by default)
print('Test 4: Testing conversation manager without encryption...')
local conv = conversation_manager.create_new_conversation()
conversation_manager.add_message('user', 'Hello, this is a test message for unencrypted storage.')
conversation_manager.add_message('assistant', 'Hello! This response should also be stored without encryption.')
local save_ok, save_err = conversation_manager.save_current_conversation()
if save_ok then
print('✓ Conversation saved without encryption')
else
print('✗ Failed to save conversation: ' .. tostring(save_err))
os.exit(1)
end
-- Try to load it back
local loaded_conv, load_err = conversation_manager.load_conversation(conv.id)
if loaded_conv then
print('✓ Conversation loaded successfully')
print(' - Title: ' .. loaded_conv.title)
print(' - Messages: ' .. #loaded_conv.messages)
else
print('✗ Failed to load conversation: ' .. tostring(load_err))
os.exit(1)
end
print()
-- Test 5: Test conversation encryption (enabled)
print('Test 5: Testing conversation manager with encryption...')
-- Temporarily modify the config to enable encryption
local original_config = config.get()
original_config.conversation_encryption = true
-- Create a new conversation with encryption enabled
local enc_conv = conversation_manager.create_new_conversation()
conversation_manager.add_message('user', 'Hello, this is a test message for encrypted storage.')
conversation_manager.add_message('assistant', 'Hello! This response should be stored with encryption.')
local enc_save_ok, enc_save_err = conversation_manager.save_current_conversation()
if enc_save_ok then
print('✓ Conversation saved with encryption')
else
print('✗ Failed to save encrypted conversation: ' .. tostring(enc_save_err))
os.exit(1)
end
-- Try to load it back
local enc_loaded_conv, enc_load_err = conversation_manager.load_conversation(enc_conv.id)
if enc_loaded_conv then
print('✓ Encrypted conversation loaded successfully')
print(' - Title: ' .. enc_loaded_conv.title)
print(' - Messages: ' .. #enc_loaded_conv.messages)
-- Verify the content is the same
if enc_loaded_conv.messages[1].content == 'Hello, this is a test message for encrypted storage.' then
print('✓ Message content verified after encryption/decryption')
else
print('✗ Message content mismatch after encryption/decryption')
os.exit(1)
end
else
print('✗ Failed to load encrypted conversation: ' .. tostring(enc_load_err))
os.exit(1)
end
print()
-- Test 6: Test conversation listing with mixed encrypted/unencrypted files
print('Test 6: Testing conversation listing...')
local conversations = conversation_manager.list_conversations()
print('Found ' .. #conversations .. ' conversations:')
for i, conv_info in ipairs(conversations) do
local enc_status = conv_info.encrypted and 'encrypted' or 'plaintext'
print(' ' .. i .. '. ' .. conv_info.title .. ' (' .. enc_status .. ')')
end
print()
-- Test 7: Cleanup test conversations
print('Test 7: Cleaning up test conversations...')
local deleted1 = conversation_manager.delete_conversation(conv.id)
local deleted2 = conversation_manager.delete_conversation(enc_conv.id)
if deleted1 then
print('✓ Deleted plaintext test conversation')
else
print('✗ Failed to delete plaintext test conversation')
end
if deleted2 then
print('✓ Deleted encrypted test conversation')
else
print('✗ Failed to delete encrypted test conversation')
end
print()
-- Reset the config
original_config.conversation_encryption = false
print('=== All tests completed successfully! ===')
print()
print('Encryption at rest is now available for CodingBuddy conversations.')
print('To enable it, set conversation_encryption: true in your config.json file.')
print('Make sure to keep your CODINGBUDDY_PASSPHRASE environment variable secure.')
print()