-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_fuzzing.php
More file actions
executable file
·277 lines (243 loc) · 7.92 KB
/
test_fuzzing.php
File metadata and controls
executable file
·277 lines (243 loc) · 7.92 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
<?php
/**
* Fuzzing tests for Opus extension
* Attempts to find crashes with random/malformed input
*/
echo "=== Opus Fuzzing Tests ===\n\n";
echo "Running 1000 random tests...\n";
$crashes = 0;
$errors = 0;
$successes = 0;
// Test with random sample rates
echo "\n[1/5] Fuzzing sample rates and channels...\n";
for ($i = 0; $i < 200; $i++) {
try {
$rate = [8000, 12000, 16000, 24000, 48000][array_rand([8000, 12000, 16000, 24000, 48000])];
$ch = rand(1, 2);
$opus = new opusChannel($rate, $ch);
$opus->destroy();
$successes++;
} catch (Throwable $e) {
$errors++;
}
}
echo " Successes: $successes, Errors: $errors, Crashes: $crashes\n";
// Test with random PCM data
echo "\n[2/5] Fuzzing encode with random PCM...\n";
$errors = 0;
$successes = 0;
for ($i = 0; $i < 200; $i++) {
try {
$opus = new opusChannel(48000, 1);
// Random valid frame sizes
$valid_sizes = [120, 240, 480, 960, 1920, 2880, 3840, 4800, 5760];
$size = $valid_sizes[array_rand($valid_sizes)];
$pcm = '';
for ($j = 0; $j < $size; $j++) {
$pcm .= pack('s', rand(-32768, 32767));
}
$encoded = $opus->encode($pcm);
$successes++;
} catch (Throwable $e) {
$errors++;
}
}
echo " Successes: $successes, Errors: $errors, Crashes: $crashes\n";
// Test with random opus packets
echo "\n[3/5] Fuzzing decode with random data...\n";
$errors = 0;
$successes = 0;
for ($i = 0; $i < 200; $i++) {
try {
$opus = new opusChannel(48000, 1);
// Random data of various sizes
$size = rand(10, 1000);
$random_data = random_bytes($size);
try {
$decoded = $opus->decode($random_data);
$successes++;
} catch (Throwable $e) {
// Decode errors are expected with random data
// We just want to ensure it doesn't crash
$errors++;
}
} catch (Throwable $e) {
$errors++;
}
}
echo " Successes: $successes, Expected Errors: $errors, Crashes: $crashes\n";
// Test with random resample parameters
echo "\n[4/5] Fuzzing resample operations...\n";
$errors = 0;
$successes = 0;
for ($i = 0; $i < 200; $i++) {
try {
$opus = new opusChannel(48000, 1);
$pcm_sizes = [100, 480, 960, 1920, 4800];
$size = $pcm_sizes[array_rand($pcm_sizes)];
$pcm = str_repeat(pack('s', rand(-16000, 16000)), $size);
$rates = [8000, 16000, 24000, 48000];
$src = $rates[array_rand($rates)];
$dst = $rates[array_rand($rates)];
$resampled = $opus->resample($pcm, $src, $dst);
$successes++;
} catch (Throwable $e) {
$errors++;
}
}
echo " Successes: $successes, Errors: $errors, Crashes: $crashes\n";
// Test with random enhancement parameters
echo "\n[5/5] Fuzzing audio enhancement...\n";
$errors = 0;
$successes = 0;
for ($i = 0; $i < 200; $i++) {
try {
$opus = new opusChannel(48000, rand(1, 2));
$size = rand(100, 5000);
$pcm = str_repeat(pack('s', rand(-32768, 32767)), $size);
$op = rand(0, 1);
if ($op === 0) {
// Enhance voice clarity with random intensity
$intensity = rand(0, 200) / 100.0; // 0.0 to 2.0
$result = $opus->enhanceVoiceClarity($pcm, $intensity);
} else {
// Spatial stereo with random parameters
$width = rand(0, 200) / 100.0; // 0.0 to 2.0
$depth = rand(0, 100) / 100.0; // 0.0 to 1.0
$result = $opus->spatialStereoEnhance($pcm, $width, $depth);
}
$successes++;
} catch (Throwable $e) {
$errors++;
}
}
echo " Successes: $successes, Errors: $errors, Crashes: $crashes\n";
// Boundary testing
echo "\n=== Boundary Value Testing ===\n";
$boundary_tests = [
'Zero-length PCM' => function() {
$opus = new opusChannel(48000, 1);
return $opus->resample('', 48000, 8000);
},
'Single sample' => function() {
$opus = new opusChannel(48000, 1);
try {
$opus->encode(pack('s', 0));
} catch (Throwable $e) {
return true;
}
return false;
},
'Max int16 chain' => function() {
$opus = new opusChannel(48000, 1);
$pcm = str_repeat(pack('s', 32767), 960);
$encoded = $opus->encode($pcm);
$decoded = $opus->decode($encoded);
$enhanced = $opus->enhanceVoiceClarity($decoded, 2.0);
$spatial = $opus->spatialStereoEnhance($enhanced, 2.0, 1.0);
return strlen($spatial) > 0;
},
'Min int16 chain' => function() {
$opus = new opusChannel(48000, 1);
$pcm = str_repeat(pack('s', -32768), 960);
$encoded = $opus->encode($pcm);
$decoded = $opus->decode($encoded);
$enhanced = $opus->enhanceVoiceClarity($decoded, 2.0);
$spatial = $opus->spatialStereoEnhance($enhanced, 2.0, 1.0);
return strlen($spatial) > 0;
},
'Alternating extremes' => function() {
$opus = new opusChannel(48000, 1);
$pcm = '';
for ($i = 0; $i < 960; $i++) {
$pcm .= pack('s', $i % 2 ? 32767 : -32768);
}
$encoded = $opus->encode($pcm);
$decoded = $opus->decode($encoded);
return strlen($decoded) > 0;
},
'Rapid transitions' => function() {
$opus = new opusChannel(48000, 1);
$pcm = '';
for ($i = 0; $i < 960; $i++) {
$pcm .= pack('s', $i % 4 < 2 ? 16000 : -16000);
}
$encoded = $opus->encode($pcm);
$decoded = $opus->decode($encoded);
$enhanced = $opus->enhanceVoiceClarity($decoded);
return strlen($enhanced) > 0;
},
];
$boundary_passed = 0;
$boundary_failed = 0;
foreach ($boundary_tests as $name => $test) {
echo " $name... ";
try {
$result = $test();
if ($result !== false) {
echo "✓ PASS\n";
$boundary_passed++;
} else {
echo "✗ FAIL\n";
$boundary_failed++;
}
} catch (Throwable $e) {
echo "✗ ERROR: " . $e->getMessage() . "\n";
$boundary_failed++;
}
}
// Pattern-based testing
echo "\n=== Pattern-Based Testing ===\n";
$patterns = [
'Silence (zeros)' => array_fill(0, 960, 0),
'DC offset (constant)' => array_fill(0, 960, 10000),
'Linear ramp' => range(-16000, 16000, 32000 / 960 + 0.0001),
'Saw wave' => array_map(function($i) {
return (($i % 100) - 50) * 650;
}, range(0, 959)),
'Square wave' => array_map(function($i) {
return $i % 100 < 50 ? 16000 : -16000;
}, range(0, 959)),
'Random noise' => array_map(function() {
return rand(-32768, 32767);
}, range(0, 959)),
];
$pattern_passed = 0;
$pattern_failed = 0;
foreach ($patterns as $name => $pattern) {
echo " $name... ";
try {
$opus = new opusChannel(48000, 1);
$pcm = '';
foreach ($pattern as $sample) {
$pcm .= pack('s', (int)$sample);
}
$encoded = $opus->encode($pcm);
$decoded = $opus->decode($encoded);
$enhanced = $opus->enhanceVoiceClarity($decoded);
if (strlen($enhanced) > 0) {
echo "✓ PASS\n";
$pattern_passed++;
} else {
echo "✗ FAIL (empty output)\n";
$pattern_failed++;
}
} catch (Throwable $e) {
echo "✗ ERROR: " . $e->getMessage() . "\n";
$pattern_failed++;
}
}
// Summary
echo "\n=== Fuzzing Summary ===\n";
echo "Random tests completed: 1000\n";
echo "Boundary tests: $boundary_passed passed, $boundary_failed failed\n";
echo "Pattern tests: $pattern_passed passed, $pattern_failed failed\n";
echo "Total crashes detected: $crashes\n";
if ($crashes === 0 && $boundary_failed === 0 && $pattern_failed === 0) {
echo "\n✓ No crashes or failures detected!\n";
echo "Extension handles random/malformed input safely.\n";
exit(0);
} else {
echo "\n⚠ Some issues detected. Review above.\n";
exit(1);
}