-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_llm_scanner.py
More file actions
177 lines (141 loc) · 4.87 KB
/
test_llm_scanner.py
File metadata and controls
177 lines (141 loc) · 4.87 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
#!/usr/bin/env python3
"""
Simple test for LLM scanner module
"""
import os
import json
# Set up test environment
os.environ["GEMINI_API_KEY"] = "test_key"
# Import the module
from llm_scanner import LLMScanner
def test_scanner_initialization():
"""Test that the scanner initializes correctly."""
print("Testing LLM Scanner initialization...")
try:
# Test Gemini backend (will fail due to invalid API key, but should initialize)
scanner = LLMScanner(backend="gemini", model="gemini-2.0-flash-exp")
print("✅ Gemini scanner initialized")
print(f" Backend: {scanner.backend}")
print(f" Model: {scanner.model}")
print(f" Cache enabled: {scanner.cache_enabled}")
except Exception as e:
print(f"❌ Failed to initialize scanner: {e}")
return False
return True
def test_file_filtering():
"""Test that file filtering works correctly."""
print("\nTesting file filtering...")
scanner = LLMScanner(backend="gemini")
test_files = [
("test.py", True),
("test.js", True),
("test.java", True),
("test.txt", False),
("test.md", False),
("test.cpp", True),
("test.rb", True),
]
for filename, should_scan in test_files:
result = scanner._should_scan_file(filename)
status = "✅" if result == should_scan else "❌"
print(f" {status} {filename}: {result} (expected: {should_scan})")
return True
def test_language_detection():
"""Test language detection from filenames."""
print("\nTesting language detection...")
scanner = LLMScanner(backend="gemini")
test_cases = [
("test.py", "python"),
("test.js", "javascript"),
("test.java", "java"),
("test.cpp", "cpp"),
("test.c", "c"),
("test.rb", "ruby"),
]
for filename, expected_lang in test_cases:
result = scanner._get_language_from_file(filename)
status = "✅" if result == expected_lang else "❌"
print(f" {status} {filename}: {result} (expected: {expected_lang})")
return True
def test_cache_key_generation():
"""Test cache key generation."""
print("\nTesting cache key generation...")
scanner = LLMScanner(backend="gemini")
code1 = "import os; os.system('ls')"
code2 = "import os; os.system('ls')"
code3 = "import os; os.system('pwd')"
key1 = scanner._get_cache_key(code1, "test.py")
key2 = scanner._get_cache_key(code2, "test.py")
key3 = scanner._get_cache_key(code3, "test.py")
if key1 == key2:
print(" ✅ Same code produces same cache key")
else:
print(" ❌ Same code should produce same cache key")
if key1 != key3:
print(" ✅ Different code produces different cache key")
else:
print(" ❌ Different code should produce different cache key")
return True
def test_response_parsing():
"""Test LLM response parsing."""
print("\nTesting LLM response parsing...")
scanner = LLMScanner(backend="gemini")
# Test valid JSON response
valid_response = """```json
[
{
"line": 10,
"severity": "high",
"issue_text": "SQL Injection",
"cwe": "CWE-89",
"explanation": "This is a test",
"fix": "Use prepared statements"
}
]
```"""
result = scanner._parse_llm_response(valid_response, "test.py")
if len(result) == 1:
print(" ✅ Parsed 1 vulnerability")
vuln = result[0]
print(f" Scanner: {vuln.get('scanner')}")
print(f" Severity: {vuln.get('severity')}")
print(f" CWE: {vuln.get('cwe')}")
else:
print(f" ❌ Expected 1 vulnerability, got {len(result)}")
# Test empty response
empty_response = "[]"
result = scanner._parse_llm_response(empty_response, "test.py")
if len(result) == 0:
print(" ✅ Empty response parsed correctly")
else:
print(f" ❌ Expected 0 vulnerabilities, got {len(result)}")
return True
def main():
"""Run all tests."""
print("=" * 50)
print("LLM Scanner Module Tests")
print("=" * 50)
tests = [
test_scanner_initialization,
test_file_filtering,
test_language_detection,
test_cache_key_generation,
test_response_parsing,
]
results = []
for test in tests:
try:
result = test()
results.append(result)
except Exception as e:
print(f"❌ Test failed with exception: {e}")
import traceback
traceback.print_exc()
results.append(False)
print("\n" + "=" * 50)
print(f"Tests passed: {sum(results)}/{len(results)}")
print("=" * 50)
return all(results)
if __name__ == "__main__":
success = main()
exit(0 if success else 1)