#!/usr/bin/env python3
import sys
from pathlib import Path
sys.path.insert(0, str(Path(__file__).parent.parent / "src"))
from database.db import init_db, insert_cve, get_cve, search_cves, get_stats
def test_database():
print("Testing database operations\n")
test_db_path = Path(__file__).parent / "test_cve.db"
if test_db_path.exists():
test_db_path.unlink()
print("1. Init database...")
conn = init_db(test_db_path)
print(" Database ready\n")
# 2. Insert test CVEs
print("2. Inserting test CVEs...")
test_cves = [
{
"cve_id": "CVE-2024-0001",
"description": "A critical vulnerability in Apache HTTP Server allows remote code execution.",
"severity": "CRITICAL",
"cvss_score": 9.8,
"published_date": "2024-01-15",
"modified_date": "2024-01-20",
"references_json": '["https://example.com/advisory"]'
},
{
"cve_id": "CVE-2024-0002",
"description": "SQL injection vulnerability in WordPress plugin.",
"severity": "HIGH",
"cvss_score": 8.5,
"published_date": "2024-02-10",
"modified_date": "2024-02-15",
"references_json": '["https://example.com/wp-vuln"]'
},
{
"cve_id": "CVE-2024-0003",
"description": "Cross-site scripting (XSS) in Apache Tomcat.",
"severity": "MEDIUM",
"cvss_score": 6.1,
"published_date": "2024-03-05",
"modified_date": "2024-03-10",
"references_json": '["https://example.com/tomcat"]'
}
]
for cve in test_cves:
insert_cve(conn, cve)
print(f" Inserted {cve['cve_id']}")
conn.commit()
print()
print("3. Retrieving CVE-2024-0001...")
result = get_cve(conn, "CVE-2024-0001")
if result and result['cve_id'] == "CVE-2024-0001":
print(f" Found: {result['cve_id']} - {result['severity']}\n")
else:
print(" Failed to retrieve CVE\n")
return False
print("4. Searching for 'Apache'...")
results = search_cves(conn, "Apache")
print(f" Found {len(results)} results")
for r in results:
print(f" - {r['cve_id']}: {r['description'][:50]}...")
print()
print("5. Getting stats...")
stats = get_stats(conn)
print(f" Total CVEs: {stats['total_cves']}")
print(f" Date range: {stats['oldest_cve']} to {stats['newest_cve']}\n")
conn.close()
test_db_path.unlink()
print("All tests passed!")
return True
if __name__ == "__main__":
success = test_database()
sys.exit(0 if success else 1)