-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_analyzer.py
More file actions
68 lines (50 loc) · 2.17 KB
/
test_analyzer.py
File metadata and controls
68 lines (50 loc) · 2.17 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
#!/usr/bin/env python3
"""Test the code analyzer locally without GitHub"""
from app.models.code_analyzer import SmartCodeAnalyzer
from app.security.vulnerability_scanner import AdvancedSecurityScanner
def test_code_analyzer():
print("🧪 Testing Smart Code Analyzer...")
analyzer = SmartCodeAnalyzer()
security_scanner = AdvancedSecurityScanner()
# Test code with issues
test_code = '''
import subprocess
import os
password = "hardcoded_secret_123"
def vulnerable_function(user_input):
# TODO: Fix this
print(f"Processing: {user_input}")
if user_input:
if len(user_input) > 10:
if user_input.startswith("admin"):
if ";" in user_input:
# SQL injection vulnerability
query = "SELECT * FROM users WHERE name = '" + user_input + "'"
# Command injection
result = subprocess.run(f"cat {user_input}", shell=True)
try:
dangerous = eval(user_input)
except:
pass
return query, result
return None
'''
print("\n🔍 Running code quality analysis...")
insights = analyzer.analyze_code_quality(test_code, "test.py")
print(f"Found {len(insights)} insights:")
for insight in insights:
severity_emoji = {"warning": "⚠️", "info": "ℹ️", "error": "❌"}
emoji = severity_emoji.get(insight['severity'], "🔍")
print(f" {emoji} [{insight['type']}] {insight['message']}")
print("\n🔒 Running security analysis...")
vulnerabilities = security_scanner.scan_for_vulnerabilities(
test_code, "test.py")
print(f"Found {len(vulnerabilities)} security issues:")
for vuln in vulnerabilities:
severity_emoji = {"high": "🔴", "medium": "🟡", "low": "🟢"}
emoji = severity_emoji.get(vuln['severity'], "🔍")
print(f" {emoji} [{vuln['severity']}] {vuln['description']}")
print(f" 💡 {vuln['recommendation']}")
print("\n✅ Test completed!")
if __name__ == "__main__":
test_code_analyzer()