-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathvalidate_core_functionality.py
More file actions
296 lines (247 loc) · 10.1 KB
/
Copy pathvalidate_core_functionality.py
File metadata and controls
296 lines (247 loc) · 10.1 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
import sys
import os
import tempfile
import shutil
from pathlib import Path
# Add current directory to path for imports
sys.path.insert(0, '.')
def test_sandbox_validator():
"""Test the sandbox validator functionality."""
print("Testing SandboxValidator...")
from addons.agentic_chatbot.security.sandbox import SandboxValidator, SecurityError
# Create a temporary workspace
with tempfile.TemporaryDirectory() as temp_dir:
workspace = Path(temp_dir) / "test_workspace"
workspace.mkdir()
validator = SandboxValidator(workspace)
# Test 1: Valid path within workspace
try:
valid_path = validator.validate_path("test_file.txt")
assert valid_path.is_absolute()
assert validator.is_safe_path("test_file.txt")
print("✓ Valid path validation passed")
except Exception as e:
print(f"✗ Valid path validation failed: {e}")
return False
# Test 2: Path traversal attempt
try:
validator.validate_path("../../../etc/passwd")
print("✗ Path traversal validation failed - should have been blocked")
return False
except SecurityError:
print("✓ Path traversal blocked correctly")
except Exception as e:
print(f"✗ Path traversal test error: {e}")
return False
# Test 3: Sanitize path
try:
sanitized = validator.sanitize_path("subdir/file.txt")
assert sanitized.is_absolute()
print("✓ Path sanitization passed")
except Exception as e:
print(f"✗ Path sanitization failed: {e}")
return False
# Test 4: List workspace contents
try:
# Create a test file
test_file = workspace / "test.txt"
test_file.write_text("test content")
contents = validator.list_workspace_contents()
assert "test.txt" in contents
print("✓ Workspace listing passed")
except Exception as e:
print(f"✗ Workspace listing failed: {e}")
return False
return True
def test_command_filter():
"""Test the command filter functionality."""
print("\nTesting CommandFilter...")
from addons.agentic_chatbot.security.command_filter import CommandFilter, SecurityError
config = {
'allowed_commands': ['ls', 'cat', 'grep', 'find'],
'denied_commands': ['rm', 'sudo', 'chmod'],
'command_timeout': 30
}
filter = CommandFilter(config)
# Test 1: Allowed command
try:
assert filter.validate_command("ls -la")
print("✓ Allowed command validation passed")
except Exception as e:
print(f"✗ Allowed command validation failed: {e}")
return False
# Test 2: Denied command
try:
assert not filter.validate_command("rm -rf /")
print("✓ Denied command blocked correctly")
except Exception as e:
print(f"✗ Denied command test failed: {e}")
return False
# Test 3: Dangerous pattern
try:
assert not filter.validate_command("sudo rm -rf /")
print("✓ Dangerous pattern blocked correctly")
except Exception as e:
print(f"✗ Dangerous pattern test failed: {e}")
return False
# Test 4: Command sanitization
try:
sanitized = filter.sanitize_command("ls -la")
assert sanitized == "ls -la"
print("✓ Command sanitization passed")
except Exception as e:
print(f"✗ Command sanitization failed: {e}")
return False
# Test 5: Get command info
try:
info = filter.get_command_info("ls -la")
assert info["is_valid"] == True
assert info["base_command"] == "ls"
print("✓ Command info retrieval passed")
except Exception as e:
print(f"✗ Command info retrieval failed: {e}")
return False
return True
def test_tool_registry():
"""Test the tool registry functionality."""
print("\nTesting ToolRegistry...")
from addons.agentic_chatbot.security.sandbox import SandboxValidator
from addons.agentic_chatbot.security.command_filter import CommandFilter
from addons.agentic_chatbot.tool_registry import ToolRegistry
# Create temporary workspace and components
with tempfile.TemporaryDirectory() as temp_dir:
workspace = Path(temp_dir) / "test_workspace"
workspace.mkdir()
validator = SandboxValidator(workspace)
filter_config = {'allowed_commands': ['ls'], 'denied_commands': ['rm']}
cmd_filter = CommandFilter(filter_config)
registry = ToolRegistry(validator, cmd_filter)
# Test 1: Get available tools
try:
tools = registry.get_available_tools()
expected_tools = ['list_directory', 'read_file', 'write_file', 'edit_file']
for tool in expected_tools:
assert tool in tools, f"Tool {tool} not found in registry"
print("✓ Built-in tools registered correctly")
except Exception as e:
print(f"✗ Built-in tools registration failed: {e}")
return False
# Test 2: Get tool schemas
try:
schemas = registry.get_tool_schemas()
assert len(schemas) >= 4
assert 'list_directory' in schemas
print("✓ Tool schemas retrieved correctly")
except Exception as e:
print(f"✗ Tool schemas retrieval failed: {e}")
return False
# Test 3: Execute a tool (list_directory)
try:
result = registry.execute_tool('list_directory', {'path': ''})
assert result['status'] == 'success'
print("✓ Tool execution passed")
except Exception as e:
print(f"✗ Tool execution failed: {e}")
return False
# Test 4: Execute invalid tool
try:
result = registry.execute_tool('nonexistent_tool', {})
assert result['status'] == 'error'
assert 'not found' in result['error']
print("✓ Invalid tool handling passed")
except Exception as e:
print(f"✗ Invalid tool handling failed: {e}")
return False
return True
def test_filesystem_tools():
"""Test the file system tools functionality."""
print("\nTesting FileSystem Tools...")
from addons.agentic_chatbot.security.sandbox import SandboxValidator
from addons.agentic_chatbot.tools.filesystem import ListDirectoryTool, ReadFileTool, WriteFileTool, EditFileTool
# Create temporary workspace
with tempfile.TemporaryDirectory() as temp_dir:
workspace = Path(temp_dir) / "test_workspace"
workspace.mkdir()
validator = SandboxValidator(workspace)
# Test WriteFileTool
try:
write_tool = WriteFileTool(validator)
result = write_tool.execute({
'path': 'test_file.txt',
'content': 'Hello, World!\nThis is a test file.'
})
assert result['status'] == 'success'
print("✓ WriteFileTool passed")
except Exception as e:
print(f"✗ WriteFileTool failed: {e}")
return False
# Test ReadFileTool
try:
read_tool = ReadFileTool(validator)
result = read_tool.execute({'path': 'test_file.txt'})
assert result['status'] == 'success'
assert 'Hello, World!' in result['result']['content']
print("✓ ReadFileTool passed")
except Exception as e:
print(f"✗ ReadFileTool failed: {e}")
return False
# Test ListDirectoryTool
try:
list_tool = ListDirectoryTool(validator)
result = list_tool.execute({'path': ''})
assert result['status'] == 'success'
assert any(item['name'] == 'test_file.txt' for item in result['result']['contents'])
print("✓ ListDirectoryTool passed")
except Exception as e:
print(f"✗ ListDirectoryTool failed: {e}")
return False
# Test EditFileTool
try:
edit_tool = EditFileTool(validator)
result = edit_tool.execute({
'path': 'test_file.txt',
'operation': 'replace',
'find': 'Hello, World!',
'replace': 'Hello, Universe!'
})
assert result['status'] == 'success'
assert result['result']['changes_made'] == 1
# Verify the edit worked
read_result = read_tool.execute({'path': 'test_file.txt'})
assert 'Hello, Universe!' in read_result['result']['content']
print("✓ EditFileTool passed")
except Exception as e:
print(f"✗ EditFileTool failed: {e}")
return False
return True
def main():
"""Run all validation tests."""
print("=== Agentic Chatbot Core Functionality Validation ===\n")
tests = [
("Sandbox Validator", test_sandbox_validator),
("Command Filter", test_command_filter),
("Tool Registry", test_tool_registry),
("FileSystem Tools", test_filesystem_tools)
]
passed = 0
total = len(tests)
for test_name, test_func in tests:
try:
if test_func():
passed += 1
print(f"✓ {test_name} - ALL TESTS PASSED\n")
else:
print(f"✗ {test_name} - SOME TESTS FAILED\n")
except Exception as e:
print(f"✗ {test_name} - CRITICAL ERROR: {e}\n")
print("=== VALIDATION SUMMARY ===")
print(f"Tests passed: {passed}/{total}")
if passed == total:
print("🎉 ALL CORE FUNCTIONALITY TESTS PASSED!")
return True
else:
print("❌ SOME TESTS FAILED - Review the output above")
return False
if __name__ == "__main__":
success = main()
sys.exit(0 if success else 1)