-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_runner.py
More file actions
452 lines (385 loc) · 15.1 KB
/
Copy pathtest_runner.py
File metadata and controls
452 lines (385 loc) · 15.1 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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
#!/usr/bin/env python3
"""
Test Runner for Web Application Security Testing Tool
Provides automated testing scenarios and validation
"""
import subprocess
import json
import time
import sys
from pathlib import Path
class TestRunner:
def __init__(self):
self.test_results = []
self.passed = 0
self.failed = 0
def run_test(self, name, command, expected_exit_code=0, timeout=60):
"""Run a single test case"""
print(f"🧪 Running test: {name}")
print(f" Command: {command}")
try:
start_time = time.time()
result = subprocess.run(
command,
shell=True,
capture_output=True,
text=True,
timeout=timeout
)
end_time = time.time()
success = result.returncode == expected_exit_code
test_result = {
'name': name,
'command': command,
'exit_code': result.returncode,
'expected_exit_code': expected_exit_code,
'success': success,
'duration': end_time - start_time,
'stdout': result.stdout,
'stderr': result.stderr
}
self.test_results.append(test_result)
if success:
print(f" ✅ PASSED ({test_result['duration']:.2f}s)")
self.passed += 1
else:
print(f" ❌ FAILED ({test_result['duration']:.2f}s)")
print(f" Expected exit code: {expected_exit_code}")
print(f" Actual exit code: {result.returncode}")
if result.stderr:
print(f" Error: {result.stderr[:200]}...")
self.failed += 1
except subprocess.TimeoutExpired:
print(f" ⏰ TIMEOUT after {timeout}s")
self.failed += 1
self.test_results.append({
'name': name,
'command': command,
'success': False,
'error': 'Timeout'
})
except Exception as e:
print(f" 💥 ERROR: {e}")
self.failed += 1
self.test_results.append({
'name': name,
'command': command,
'success': False,
'error': str(e)
})
def run_basic_tests(self):
"""Run basic functionality tests"""
print("\n📋 Running Basic Functionality Tests")
print("="*50)
# Test help output
self.run_test(
"Help Output",
"python3 enhanced_main.py --help",
expected_exit_code=0
)
# Test invalid URL
self.run_test(
"Invalid URL Handling",
"python3 enhanced_main.py --all invalid-url",
expected_exit_code=1
)
# Test missing test type
self.run_test(
"Missing Test Type",
"python3 enhanced_main.py http://example.com",
expected_exit_code=2 # argparse error
)
# Test configuration loading
self.run_test(
"Configuration Loading",
"python3 enhanced_main.py --config config/default_config.yml --all http://httpbin.org",
expected_exit_code=0
)
def run_authentication_tests(self):
"""Run authentication-related tests"""
print("\n🔐 Running Authentication Tests")
print("="*50)
# Test form authentication (simulated)
auth_config = {
"type": "form",
"login_url": "http://httpbin.org/post",
"username": "testuser",
"password": "testpass"
}
self.run_test(
"Form Authentication",
f"python3 enhanced_main.py --auth '{json.dumps(auth_config)}' --xss http://httpbin.org",
expected_exit_code=0
)
# Test cookie authentication
self.run_test(
"Cookie Authentication",
"python3 enhanced_main.py --cookies 'session=test123;user=admin' --xss http://httpbin.org",
expected_exit_code=0
)
# Test header authentication
self.run_test(
"Header Authentication",
"python3 enhanced_main.py --headers 'Authorization:Bearer token123' --xss http://httpbin.org",
expected_exit_code=0
)
def run_scanning_tests(self):
"""Run vulnerability scanning tests"""
print("\n🔍 Running Vulnerability Scanning Tests")
print("="*50)
# Test XSS scanning
self.run_test(
"XSS Scanning",
"python3 enhanced_main.py --xss --output test_xss http://httpbin.org",
expected_exit_code=0
)
# Test CSRF scanning
self.run_test(
"CSRF Scanning",
"python3 enhanced_main.py --csrf --output test_csrf http://httpbin.org",
expected_exit_code=0
)
# Test all vulnerabilities
self.run_test(
"All Vulnerability Tests",
"python3 enhanced_main.py --all --output test_all http://httpbin.org",
expected_exit_code=0
)
# Test with custom depth
self.run_test(
"Custom Crawl Depth",
"python3 enhanced_main.py --all --depth 1 --output test_depth http://httpbin.org",
expected_exit_code=0
)
# Test with delay
self.run_test(
"Request Delay",
"python3 enhanced_main.py --xss --delay 0.5 --output test_delay http://httpbin.org",
expected_exit_code=0
)
def run_output_tests(self):
"""Run output format tests"""
print("\n📊 Running Output Format Tests")
print("="*50)
# Test JSON output
self.run_test(
"JSON Output Format",
"python3 enhanced_main.py --all --format json --output test_json http://httpbin.org",
expected_exit_code=0
)
# Test HTML output
self.run_test(
"HTML Output Format",
"python3 enhanced_main.py --all --format html --output test_html http://httpbin.org",
expected_exit_code=0
)
# Test CSV output
self.run_test(
"CSV Output Format",
"python3 enhanced_main.py --all --format csv --output test_csv http://httpbin.org",
expected_exit_code=0
)
# Test multiple formats
self.run_test(
"Multiple Output Formats",
"python3 enhanced_main.py --all --format json,html,csv --output test_multi http://httpbin.org",
expected_exit_code=0
)
# Test quiet mode
self.run_test(
"Quiet Mode",
"python3 enhanced_main.py --all --quiet --output test_quiet http://httpbin.org",
expected_exit_code=0
)
# Test verbose mode
self.run_test(
"Verbose Mode",
"python3 enhanced_main.py --all --verbose --output test_verbose http://httpbin.org",
expected_exit_code=0
)
def run_session_tests(self):
"""Run session management tests"""
print("\n🔑 Running Session Management Tests")
print("="*50)
# Create a test session file
test_session = {
"cookies": {"PHPSESSID": "test123", "user": "admin"},
"headers": {"Authorization": "Bearer token123"},
"logged_in": True,
"login_url": "http://httpbin.org/post"
}
with open("test_session.json", "w") as f:
json.dump(test_session, f)
# Test session import
self.run_test(
"Session Import",
"python3 enhanced_main.py --import-session test_session.json --xss http://httpbin.org",
expected_exit_code=0
)
# Test session export
auth_config = {
"type": "cookies",
"cookies": "session=test123;user=admin"
}
self.run_test(
"Session Export",
f"python3 enhanced_main.py --auth '{json.dumps(auth_config)}' --export-session exported_session --xss http://httpbin.org",
expected_exit_code=0
)
def run_error_handling_tests(self):
"""Run error handling tests"""
print("\n⚠️ Running Error Handling Tests")
print("="*50)
# Test unreachable host
self.run_test(
"Unreachable Host",
"python3 enhanced_main.py --all --timeout 5 http://unreachable.invalid",
expected_exit_code=1
)
# Test invalid authentication config
self.run_test(
"Invalid Auth Config",
"python3 enhanced_main.py --auth 'invalid-json' --xss http://httpbin.org",
expected_exit_code=1
)
# Test missing session file
self.run_test(
"Missing Session File",
"python3 enhanced_main.py --import-session nonexistent.json --xss http://httpbin.org",
expected_exit_code=1
)
# Test invalid config file
self.run_test(
"Invalid Config File",
"python3 enhanced_main.py --config nonexistent.yml --all http://httpbin.org",
expected_exit_code=0 # Should use defaults and warn
)
def validate_output_files(self):
"""Validate that output files were created correctly"""
print("\n📁 Validating Output Files")
print("="*50)
expected_files = [
"test_xss.md",
"test_csrf.md",
"test_all.md",
"test_json.json",
"test_html.html",
"test_csv.csv",
"test_multi.json",
"test_multi.html",
"test_multi.csv",
"exported_session.json"
]
for filename in expected_files:
if Path(filename).exists():
print(f" ✅ {filename} exists")
# Basic validation of file content
try:
with open(filename, 'r') as f:
content = f.read()
if filename.endswith('.json'):
json.loads(content) # Validate JSON
print(f" Valid JSON format")
elif filename.endswith('.html'):
if '<html>' in content and '</html>' in content:
print(f" Valid HTML structure")
else:
print(f" ⚠️ HTML structure may be incomplete")
elif filename.endswith('.csv'):
lines = content.split('\n')
if len(lines) > 1: # Header + at least one row
print(f" CSV has {len(lines)-1} data rows")
else:
print(f" ⚠️ CSV appears empty")
elif filename.endswith('.md'):
if '# Web Application Security Test Report' in content:
print(f" Valid Markdown report")
else:
print(f" ⚠️ Markdown format may be incorrect")
except Exception as e:
print(f" ❌ File validation failed: {e}")
else:
print(f" ❌ {filename} missing")
def cleanup_test_files(self):
"""Clean up test files"""
print("\n🧹 Cleaning up test files...")
test_files = [
"test_xss.md", "test_csrf.md", "test_all.md", "test_depth.md", "test_delay.md",
"test_json.json", "test_html.html", "test_csv.csv", "test_quiet.md", "test_verbose.md",
"test_multi.json", "test_multi.html", "test_multi.csv", "test_multi.md",
"test_session.json", "exported_session.json"
]
for filename in test_files:
try:
Path(filename).unlink(missing_ok=True)
print(f" 🗑️ Removed {filename}")
except Exception as e:
print(f" ⚠️ Could not remove {filename}: {e}")
def generate_test_report(self):
"""Generate test execution report"""
print("\n📋 Test Execution Report")
print("="*50)
total_tests = len(self.test_results)
print(f"Total tests run: {total_tests}")
print(f"Passed: {self.passed}")
print(f"Failed: {self.failed}")
print(f"Success rate: {(self.passed/total_tests*100):.1f}%")
if self.failed > 0:
print(f"\n❌ Failed tests:")
for result in self.test_results:
if not result['success']:
print(f" - {result['name']}")
if 'error' in result:
print(f" Error: {result['error']}")
elif 'stderr' in result and result['stderr']:
print(f" Error: {result['stderr'][:100]}...")
# Save detailed report
report_file = f"test_report_{int(time.time())}.json"
with open(report_file, 'w') as f:
json.dump({
'summary': {
'total': total_tests,
'passed': self.passed,
'failed': self.failed,
'success_rate': self.passed/total_tests*100
},
'results': self.test_results
}, f, indent=2)
print(f"\nDetailed report saved to: {report_file}")
return self.failed == 0
def main():
"""Main test runner function"""
print("🚀 Starting Web Security Tool Test Suite")
print("="*60)
runner = TestRunner()
try:
# Run all test suites
runner.run_basic_tests()
runner.run_authentication_tests()
runner.run_scanning_tests()
runner.run_output_tests()
runner.run_session_tests()
runner.run_error_handling_tests()
# Validate outputs
runner.validate_output_files()
# Generate report
success = runner.generate_test_report()
# Cleanup
cleanup_input = input("\n🧹 Clean up test files? (y/N): ").strip().lower()
if cleanup_input == 'y':
runner.cleanup_test_files()
# Exit with appropriate code
if success:
print("\n🎉 All tests passed!")
sys.exit(0)
else:
print(f"\n💥 {runner.failed} test(s) failed!")
sys.exit(1)
except KeyboardInterrupt:
print("\n❌ Test suite interrupted by user")
sys.exit(1)
except Exception as e:
print(f"\n💥 Unexpected error in test suite: {e}")
sys.exit(1)
if __name__ == "__main__":
main()