File size: 2,897 Bytes
caaf9d1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
#!/usr/bin/env python3
"""Comprehensive bug detection test"""
import sys
sys.path.insert(0, '.')
from main import analyze_code_comprehensively

# Test 1: SQL Injection
sql_injection_code = '''
def get_user(uid):
    q = f"SELECT * FROM users WHERE id={uid}"
    cursor.execute(q)
    return cursor.fetchone()
'''

print("=" * 60)
print("TEST 1: SQL Injection Detection")
print("=" * 60)
bugs = analyze_code_comprehensively(sql_injection_code, 'python')
print(f"Bugs found: {len(bugs)}")
for i, bug in enumerate(bugs, 1):
    print(f"\n  [{i}] {bug['comment']}")
    print(f"      Severity: {bug['severity']}, Confidence: {bug['confidence']}")
    print(f"      Category: {bug['category']}")

# Test 2: Multiple Security Issues
multi_issue_code = '''
import os
password = "admin123"
query = "SELECT * FROM users WHERE pwd=" + user_input
eval(user_code)
os.system(command)
'''

print("\n" + "=" * 60)
print("TEST 2: Multiple Security Issues")
print("=" * 60)
bugs = analyze_code_comprehensively(multi_issue_code, 'python')
print(f"Bugs found: {len(bugs)}")
for i, bug in enumerate(bugs, 1):
    print(f"\n  [{i}] {bug['comment']}")
    print(f"      Severity: {bug['severity']}, Confidence: {bug['confidence']}")
    print(f"      Category: {bug['category']}")

# Test 3: Logic Errors
logic_code = '''
items = [1, 2, 3, 4, 5]
for i in range(len(items)-1):
    items[i] = items[i] * 2

try:
    result = int("not a number")
except:
    pass
'''

print("\n" + "=" * 60)
print("TEST 3: Logic Errors")
print("=" * 60)
bugs = analyze_code_comprehensively(logic_code, 'python')
print(f"Bugs found: {len(bugs)}")
for i, bug in enumerate(bugs, 1):
    print(f"\n  [{i}] {bug['comment']}")
    print(f"      Severity: {bug['severity']}, Confidence: {bug['confidence']}")
    print(f"      Category: {bug['category']}")

# Test 4: Clean Code
clean_code = '''
def add(a, b):
    """Add two numbers."""
    return a + b

result = add(5, 3)
'''

print("\n" + "=" * 60)
print("TEST 4: Clean Code (Should be 0 bugs)")
print("=" * 60)
bugs = analyze_code_comprehensively(clean_code, 'python')
print(f"Bugs found: {len(bugs)}")
if bugs:
    for i, bug in enumerate(bugs, 1):
        print(f"\n  [{i}] {bug['comment']}")

# Test 5: Multi-language Support
test_cases = [
    ('js_code', 'var password = "admin123"; eval(userInput);', 'javascript'),
    ('java_code', 'String query = "SELECT * FROM users WHERE id=" + userId;', 'java'),
    ('go_code', 'cmd := "SELECT * FROM users WHERE id=" + userId', 'go'),
]

print("\n" + "=" * 60)
print("TEST 5: Multi-Language Support")
print("=" * 60)
for name, code, lang in test_cases:
    bugs = analyze_code_comprehensively(code, lang)
    print(f"\n{lang.upper()}: {len(bugs)} bugs")
    for bug in bugs[:2]:  # Show first 2
        print(f"  - {bug['comment'][:60]}... (conf: {bug['confidence']})")

print("\n" + "=" * 60)
print("✓ ALL TESTS COMPLETED")
print("=" * 60)