forked from Talishar/Talishar
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsimple_test_runner.php
More file actions
258 lines (208 loc) · 7.49 KB
/
simple_test_runner.php
File metadata and controls
258 lines (208 loc) · 7.49 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
<?php
/**
* Simple Test Runner for Talishar
* A basic test runner that doesn't require PHPUnit or Docker
*/
echo "🧪 Talishar Security Test Suite\n";
echo "===============================\n\n";
// Test results
$results = [
'total' => 0,
'passed' => 0,
'failed' => 0,
'errors' => []
];
/**
* Simple assertion function
*/
function assertTrue($condition, $message = '') {
global $results;
$results['total']++;
if ($condition) {
$results['passed']++;
echo " ✅ $message\n";
return true;
} else {
$results['failed']++;
$results['errors'][] = $message;
echo " ❌ $message\n";
return false;
}
}
function assertFalse($condition, $message = '') {
return assertTrue(!$condition, $message);
}
function assertEquals($expected, $actual, $message = '') {
return assertTrue($expected === $actual, $message ?: "Expected: $expected, Got: $actual");
}
function assertStringContainsString($needle, $haystack, $message = '') {
return assertTrue(strpos($haystack, $needle) !== false, $message ?: "String '$haystack' should contain '$needle'");
}
function assertStringNotContainsString($needle, $haystack, $message = '') {
return assertTrue(strpos($haystack, $needle) === false, $message ?: "String '$haystack' should not contain '$needle'");
}
/**
* Test SQL Injection Prevention
*/
function testSQLInjectionPrevention() {
echo "🔒 Testing SQL Injection Prevention...\n";
// Test malicious inputs
$maliciousInputs = [
"'; DROP TABLE users; --",
"' OR '1'='1",
"' UNION SELECT * FROM users --",
"'; INSERT INTO users VALUES ('hacker', 'password'); --"
];
foreach ($maliciousInputs as $input) {
// Test username validation
$isValid = !empty($input) && is_string($input) && ctype_alnum($input);
assertFalse($isValid, "Malicious input should be rejected: " . substr($input, 0, 20) . "...");
}
// Test valid usernames
$validUsernames = ['testuser', 'player123', 'admin'];
foreach ($validUsernames as $username) {
$isValid = !empty($username) && is_string($username) && ctype_alnum($username);
assertTrue($isValid, "Valid username should be accepted: $username");
}
echo "\n";
}
/**
* Test XSS Prevention
*/
function testXSSPrevention() {
echo "🛡️ Testing XSS Prevention...\n";
$xssPayloads = [
"<script>alert('XSS')</script>",
"<img src=x onerror=alert('XSS')>",
"<iframe src=javascript:alert('XSS')></iframe>"
];
foreach ($xssPayloads as $payload) {
$escaped = htmlspecialchars($payload, ENT_QUOTES, 'UTF-8');
assertStringNotContainsString('<script>', $escaped, "Script tags should be escaped");
assertStringNotContainsString('onerror=', $escaped, "Event handlers should be escaped");
assertStringContainsString('<', $escaped, "Less than should be escaped");
assertStringContainsString('>', $escaped, "Greater than should be escaped");
}
echo "\n";
}
/**
* Test CSRF Protection
*/
function testCSRFProtection() {
echo "🔐 Testing CSRF Protection...\n";
// Test token generation
$token1 = bin2hex(random_bytes(32));
$token2 = bin2hex(random_bytes(32));
assertTrue(strlen($token1) === 64, "CSRF token should be 64 characters");
assertTrue(ctype_xdigit($token1), "CSRF token should be valid hex");
assertTrue($token1 !== $token2, "CSRF tokens should be unique");
// Test token validation
assertTrue(hash_equals($token1, $token1), "Valid token should pass validation");
assertFalse(hash_equals($token1, $token2), "Invalid token should fail validation");
assertFalse(hash_equals($token1, 'invalid'), "Invalid token should fail validation");
echo "\n";
}
/**
* Test Input Validation
*/
function testInputValidation() {
echo "✅ Testing Input Validation...\n";
// Test username validation
assertTrue(validateUsername('testuser'), "Valid username should pass");
assertFalse(validateUsername(''), "Empty username should fail");
assertFalse(validateUsername('ab'), "Short username should fail");
assertFalse(validateUsername('user@domain'), "Username with special chars should fail");
// Test email validation
assertTrue(validateEmail('test@example.com'), "Valid email should pass");
assertFalse(validateEmail('invalid-email'), "Invalid email should fail");
assertFalse(validateEmail(''), "Empty email should fail");
// Test password validation
assertTrue(validatePassword('password123'), "Valid password should pass");
assertFalse(validatePassword('1234567'), "Short password should fail");
assertFalse(validatePassword(''), "Empty password should fail");
echo "\n";
}
/**
* Test Session Management
*/
function testSessionManagement() {
echo "🔑 Testing Session Management...\n";
// Test session start
if (session_status() === PHP_SESSION_NONE) {
session_start();
}
assertTrue(session_status() === PHP_SESSION_ACTIVE, "Session should be active");
// Test session data
$_SESSION['test'] = 'value';
assertTrue(isset($_SESSION['test']), "Session data should be stored");
assertEquals('value', $_SESSION['test'], "Session data should be retrievable");
// Test session regeneration
$oldId = session_id();
session_regenerate_id(true);
$newId = session_id();
assertTrue($oldId !== $newId, "Session ID should be regenerated");
echo "\n";
}
/**
* Validation functions (simplified versions)
*/
function validateUsername($username) {
if (empty($username) || !is_string($username)) {
return false;
}
if (strlen($username) < 3 || strlen($username) > 20) {
return false;
}
if (!ctype_alnum($username)) {
return false;
}
return true;
}
function validateEmail($email) {
if (empty($email) || !is_string($email)) {
return false;
}
return filter_var($email, FILTER_VALIDATE_EMAIL) !== false;
}
function validatePassword($password) {
if (empty($password) || !is_string($password)) {
return false;
}
if (strlen($password) < 8) {
return false;
}
return true;
}
// Run all tests
testSQLInjectionPrevention();
testXSSPrevention();
testCSRFProtection();
testInputValidation();
testSessionManagement();
// Display results
echo "📊 Test Results Summary\n";
echo "=======================\n";
echo "Total Tests: " . $results['total'] . "\n";
echo "Passed: " . $results['passed'] . "\n";
echo "Failed: " . $results['failed'] . "\n";
echo "Success Rate: " . ($results['total'] > 0 ? round(($results['passed'] / $results['total']) * 100, 2) : 0) . "%\n\n";
if ($results['failed'] > 0) {
echo "❌ Failed Tests:\n";
foreach ($results['errors'] as $error) {
echo " - $error\n";
}
echo "\n";
}
if ($results['failed'] === 0 && $results['total'] > 0) {
echo "🎉 All tests passed! Your security fixes are working correctly.\n";
} elseif ($results['total'] === 0) {
echo "⚠️ No tests were run. Please check your test files.\n";
} else {
echo "⚠️ Some tests failed. Please review the errors above.\n";
}
echo "\n";
echo "💡 This is a simplified test runner. For full testing capabilities:\n";
echo " 1. Install PHPUnit: composer install --dev\n";
echo " 2. Run full test suite: ./vendor/bin/phpunit\n";
echo " 3. Or use Docker: docker compose exec web-server php run_tests.php\n\n";
?>