-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_sample_code.py
More file actions
executable file
·174 lines (136 loc) · 5.15 KB
/
test_sample_code.py
File metadata and controls
executable file
·174 lines (136 loc) · 5.15 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
#!/usr/bin/env python3
"""
Quick test script for CodeGuard AI using sample vulnerable code.
This script helps you test the vulnerability scanner without needing GitHub.
"""
import sys
import os
import asyncio
from pathlib import Path
# Add the project root to the path
sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
try:
from sandbox_agent.agent import VulnerabilityScanner
except ImportError:
print("Error: Could not import VulnerabilityScanner. Make sure sandbox_agent/agent.py exists.")
sys.exit(1)
def test_vulnerability_detection():
"""Test vulnerability detection on sample files."""
print("=" * 60)
print("CodeGuard AI - Local Vulnerability Detection Test")
print("=" * 60)
print()
sample_dir = Path(__file__).parent / "sample_vulnerable_code"
if not sample_dir.exists():
print(f"Error: Sample code directory not found at {sample_dir}")
return
# Get all Python files in the sample directory
test_files = list(sample_dir.glob("*.py"))
if not test_files:
print("No Python test files found in sample_vulnerable_code/")
return
print(f"Found {len(test_files)} test files:")
for f in test_files:
print(f" - {f.name}")
print()
# Initialize scanner
scanner = VulnerabilityScanner()
total_vulnerabilities = 0
results_by_file = {}
# Scan each file
for test_file in test_files:
print(f"\n📄 Scanning: {test_file.name}")
print("-" * 60)
try:
with open(test_file, 'r') as f:
code = f.read()
# Scan for vulnerabilities
vulnerabilities = scanner.scan_code(code, test_file.name)
if vulnerabilities:
print(f"✅ Found {len(vulnerabilities)} vulnerabilities:")
results_by_file[test_file.name] = vulnerabilities
for vuln in vulnerabilities:
print(f"\n 🔴 {vuln['type'].upper()}")
print(f" Line {vuln['line']}: {vuln['code'][:80]}...")
print(f" Pattern: {vuln['pattern'][:60]}...")
total_vulnerabilities += len(vulnerabilities)
else:
print(" ℹ️ No vulnerabilities detected")
results_by_file[test_file.name] = []
except Exception as e:
print(f" ❌ Error scanning file: {e}")
continue
# Print summary
print("\n" + "=" * 60)
print("SUMMARY")
print("=" * 60)
print(f"Files scanned: {len(test_files)}")
print(f"Total vulnerabilities found: {total_vulnerabilities}")
print()
print("Breakdown by file:")
for filename, vulns in results_by_file.items():
vuln_types = {}
for v in vulns:
vuln_types[v['type']] = vuln_types.get(v['type'], 0) + 1
print(f"\n {filename}:")
if vuln_types:
for vtype, count in vuln_types.items():
print(f" - {vtype}: {count}")
else:
print(f" - No vulnerabilities")
print("\n" + "=" * 60)
# Expected results
expected_counts = {
'sql_injection.py': 4,
'xss_vulnerabilities.py': 6,
'command_injection.py': 8,
'path_traversal.py': 9
}
print("\nExpected vs Actual:")
for filename, expected in expected_counts.items():
actual = len(results_by_file.get(filename, []))
status = "✅" if actual >= expected else "⚠️"
print(f" {status} {filename}: {actual}/{expected}")
print()
if total_vulnerabilities >= sum(expected_counts.values()):
print("🎉 All expected vulnerabilities detected!")
print("✅ CodeGuard AI is working correctly!")
else:
print("⚠️ Some vulnerabilities were not detected.")
print("💡 This may indicate the detection patterns need adjustment.")
print()
def print_usage():
"""Print usage instructions."""
print("""
CodeGuard AI Test Script
========================
This script tests the vulnerability scanner against sample vulnerable code.
Usage:
python test_sample_code.py
What it does:
1. Scans all .py files in sample_vulnerable_code/
2. Detects SQL injection, XSS, command injection, and path traversal
3. Prints detailed results for each file
4. Shows summary statistics
5. Compares against expected vulnerability counts
Note: This only tests the vulnerability detection component.
For full end-to-end testing (including exploit generation and GitHub integration),
use the Streamlit dashboard or orchestrator.py with a real GitHub PR.
To test the full pipeline:
1. Push sample files to a GitHub repo
2. Create a pull request
3. Run: streamlit run dashboard.py
4. Or: python orchestrator.py <owner> <repo> <pr_number> <token>
""")
if __name__ == "__main__":
if len(sys.argv) > 1 and sys.argv[1] in ['-h', '--help', 'help']:
print_usage()
else:
try:
test_vulnerability_detection()
except KeyboardInterrupt:
print("\n\nTest interrupted by user.")
except Exception as e:
print(f"\n❌ Unexpected error: {e}")
import traceback
traceback.print_exc()