-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_basic.py
More file actions
executable file
·152 lines (122 loc) · 4.17 KB
/
test_basic.py
File metadata and controls
executable file
·152 lines (122 loc) · 4.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
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
#!/usr/bin/env python3
"""
Test script for Linux Antivirus Scanner
Tests basic functionality without requiring VirusTotal API
"""
import os
import sys
import tempfile
import hashlib
from pathlib import Path
# Add current directory to path
sys.path.insert(0, os.path.dirname(__file__))
try:
from linux_av import VirusTotalScanner, FileScanner, create_default_config
print("✅ Successfully imported linux_av module")
except ImportError as e:
print(f"❌ Failed to import linux_av: {e}")
sys.exit(1)
def test_hash_calculation():
"""Test file hash calculation"""
print("\n📝 Testing hash calculation...")
# Create a temporary file
with tempfile.NamedTemporaryFile(mode='w', delete=False) as f:
f.write("test content")
temp_file = f.name
try:
scanner = VirusTotalScanner("test_key")
file_hash = scanner.get_file_hash(temp_file)
# Calculate expected hash
expected_hash = hashlib.sha256(b"test content").hexdigest()
if file_hash == expected_hash:
print(f" ✅ Hash calculation correct: {file_hash}")
return True
else:
print(f" ❌ Hash mismatch: {file_hash} != {expected_hash}")
return False
finally:
os.unlink(temp_file)
def test_extension_filter():
"""Test extension filtering"""
print("\n📝 Testing extension filter...")
scanner = FileScanner()
scanner.extensions = ['.deb', '.appimage']
tests = [
('/path/to/file.deb', True),
('/path/to/file.appimage', True),
('/path/to/file.txt', False),
('/path/to/file.exe', False),
('/path/to/file.DEB', True), # Case insensitive
]
all_passed = True
for filepath, expected in tests:
result = scanner.should_scan_file(filepath)
status = "✅" if result == expected else "❌"
print(f" {status} {filepath}: {result} (expected {expected})")
if result != expected:
all_passed = False
return all_passed
def test_config_creation():
"""Test configuration file creation"""
print("\n📝 Testing config creation...")
with tempfile.TemporaryDirectory() as tmpdir:
config_path = os.path.join(tmpdir, "test.conf")
create_default_config(config_path)
if os.path.exists(config_path):
with open(config_path, 'r') as f:
content = f.read()
if '[VirusTotal]' in content and '[Scanning]' in content:
print(f" ✅ Config file created successfully")
return True
else:
print(f" ❌ Config file missing sections")
return False
else:
print(f" ❌ Config file not created")
return False
def test_file_scanner_init():
"""Test FileScanner initialization"""
print("\n📝 Testing FileScanner initialization...")
try:
scanner = FileScanner()
print(f" ✅ FileScanner initialized")
print(f" - Extensions: {scanner.extensions or 'all'}")
print(f" - Config loaded: {bool(scanner.config)}")
return True
except Exception as e:
print(f" ❌ FileScanner initialization failed: {e}")
return False
def main():
"""Run all tests"""
print("="*60)
print("Linux Antivirus Scanner - Basic Tests")
print("="*60)
tests = [
test_hash_calculation,
test_extension_filter,
test_config_creation,
test_file_scanner_init,
]
results = []
for test in tests:
try:
result = test()
results.append(result)
except Exception as e:
print(f" ❌ Test failed with exception: {e}")
results.append(False)
# Summary
print("\n" + "="*60)
print("Test Summary")
print("="*60)
passed = sum(results)
total = len(results)
print(f"Passed: {passed}/{total}")
if passed == total:
print("✅ All tests passed!")
return 0
else:
print(f"❌ {total - passed} test(s) failed")
return 1
if __name__ == "__main__":
sys.exit(main())