-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest.sh
More file actions
executable file
·344 lines (302 loc) · 12.4 KB
/
test.sh
File metadata and controls
executable file
·344 lines (302 loc) · 12.4 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
#!/bin/bash
testport=9898
run_options="$*"
if [ "$run_options" = "" ]; then
run_options="no80"
fi
# Global failure tracking
failed_tests=()
total_tests=0
# Helper functions for test.sh
# Start container with specified options
start_container() {
local options="$1"
shift # Remove the first argument (options)
echo "Starting no80 container with: $options $run_options"
$builder stop -i no80test 2>/dev/null || true
$builder rm -i no80test 2>/dev/null || true
$builder run --name no80test -d --memory=32m $options $run_options "$@"
# Wait for server to be ready by testing actual HTTP response
local port=$(echo "$options" | grep -o '\-p [0-9]*:' | head -1 | cut -d' ' -f2 | cut -d':' -f1)
if [ -n "$port" ]; then
echo "Waiting for server to be ready on port $port..."
local max_attempts=50
local attempt=0
while [ $attempt -lt $max_attempts ]; do
if curl -s --max-time 1 http://localhost:"$port" >/dev/null 2>&1; then
echo "Server ready after $attempt attempts"
return 0
fi
sleep 0.1
attempt=$((attempt + 1))
done
echo "Warning: Server may not be ready after $max_attempts attempts"
else
sleep 1 # Fallback for containers without port mapping
fi
}
# Stop and remove container
stop_container() {
echo "Stop no80 container"
$builder stop -i no80test 2>/dev/null || true
$builder rm -i no80test 2>/dev/null || true
}
# Test HTTP redirect and report result
test_redirect() {
local test_name="$1"
local url="$2"
local expected="$3"
echo "$test_name"
total_tests=$((total_tests + 1))
local response=$(curl -v "$url" 2>&1)
if echo "$response" | grep -q "$expected"; then
echo TEST SUCCESS
else
echo TEST FAILED
echo "Expected: $expected"
echo "Response: $response"
failed_tests+=("$test_name")
fi
}
# Test HTTPS redirect and report result
test_https_redirect() {
local test_name="$1"
local url="$2"
local expected="$3"
echo "$test_name"
total_tests=$((total_tests + 1))
local response=$(curl -v -k "$url" 2>&1)
if echo "$response" | grep -q "$expected"; then
echo TEST SUCCESS
else
echo TEST FAILED
echo "Expected: $expected"
echo "Response: $response"
failed_tests+=("$test_name")
fi
}
# Run performance benchmark with multithreaded coproc approach
run_benchmark() {
local test_name="$1"
local threads="$2"
local requests_per_thread="$3"
local port="$4"
local protocol="$5"
local path="$6"
local description="$7"
local total_requests=$((threads * requests_per_thread))
echo "$test_name"
echo "Testing $description with $threads threads × $requests_per_thread requests = $total_requests total requests..."
echo "CPU usage breakdown:"
# Function to run threaded benchmark using coproc like benchmark.sh
run_threaded_benchmark() {
set -m # Enable job control
local thread_pids=""
local coproc_fds=""
# Start worker threads using coproc
for i in $(seq 1 $threads); do
if [ "$protocol" = "https" ]; then
coproc bash -c "for j in \$(seq 1 $requests_per_thread); do curl -k -s https://localhost:$port$path > /dev/null 2>&1; done"
else
coproc bash -c "for j in \$(seq 1 $requests_per_thread); do echo -e 'GET $path HTTP/1.1\\nHost: localhost:$port\\n\\n' | nc -w 1 localhost $port > /dev/null 2>&1; done"
fi
thread_pids="$thread_pids $COPROC_PID"
coproc_fds="$coproc_fds $COPROC"
# Close file descriptors to avoid warnings
exec {COPROC[0]}<&-
exec {COPROC[1]}>&-
done
# Wait for all threads to complete
for pid in $thread_pids; do
wait $pid 2>/dev/null
done
}
# Run with CPU monitoring and timing using dedicated benchmark.sh
start_time=$(date +%s%N)
temp_output=$(mktemp)
/usr/bin/time -v ./benchmark.sh $threads $requests_per_thread $port $protocol $path 2>"$temp_output"
end_time=$(date +%s%N)
# Display CPU metrics
echo "Script CPU time:"
grep -E "(User time|System time)" "$temp_output"
echo "Total resource usage:"
grep -E "(Percent of CPU|Maximum resident set size)" "$temp_output"
rm -f "$temp_output"
# Calculate performance metrics
duration_ns=$((end_time - start_time))
duration_s=$((duration_ns / 1000000000))
duration_ms=$((duration_ns / 1000000))
if [ "$duration_ms" -lt 1000 ]; then
rps=$((total_requests * 1000 / duration_ms))
echo "$test_name completed in ${duration_ms}ms (~${rps} redirects/second)"
else
duration_display=$(echo "scale=3; $duration_ms / 1000" | bc -l)
rps=$(echo "scale=0; $total_requests * 1000 / $duration_ms" | bc -l)
echo "$test_name completed in ${duration_display}s (~${rps} redirects/second)"
fi
# Check if benchmark completed within 60 seconds
total_tests=$((total_tests + 1))
if [ "$duration_s" -lt "60" ]; then
echo "$test_name: TEST SUCCESS"
else
echo "$test_name: TEST FAILED (took too long: ${duration_s}s)"
failed_tests+=("$test_name")
fi
}
echo Building no80 image
. ./build.sh
start_container "-p $testport:80" https://nonexistingtest.site
test_redirect "Test1: redirect to https://nonexistingtest.site" "http://localhost:$testport" "Location: https://nonexistingtest.site"
stop_container
start_container "-p $testport:80" -a https://nonexistingtest.site
test_redirect "Test2: redirect to https://nonexistingtest.site/hello" "http://localhost:$testport/hello" "Location: https://nonexistingtest.site/hello"
stop_container
echo Test3: no redirect to https://nonexistingtest.site
total_tests=$((total_tests + 1))
if curl -vL http://localhost:$testport 2>&1 | grep "URL: 'https://nonexistingtest.site" > /dev/null 2>&1; then
echo TEST FAILED
failed_tests+=("Test3: no redirect to https://nonexistingtest.site")
else
echo TEST SUCCESS
fi
start_container "-p $testport:80" -m /match https://nonexistingtest.site/m -s /starting https://nonexistingtest.site/s -r /redirect https://nonexistingtest.site/r https://nonexistingtest.site
test_redirect "Test4: redirect no match to https://nonexistingtest.site" "http://localhost:$testport" "Location: https://nonexistingtest.site"
test_redirect "Test5: redirect /match to https://nonexistingtest.site/m" "http://localhost:$testport/match" "Location: https://nonexistingtest.site/m"
test_redirect "Test6: redirect /starting/path to https://nonexistingtest.site/s" "http://localhost:$testport/starting/path" "Location: https://nonexistingtest.site/s"
test_redirect "Test7: redirect /redirect/path to https://nonexistingtest.site/r/path" "http://localhost:$testport/redirect/path" "Location: https://nonexistingtest.site/r/path"
stop_container
# Test8: HTTPS functionality
start_container "-p $testport:80 -p $((testport+1)):443" https://nonexistingtest.site
test_https_redirect "Test8: HTTPS server functionality" "https://localhost:$((testport+1))" "Location: https://nonexistingtest.site"
stop_container
# Test9: Simultaneous HTTP and HTTPS functionality
start_container "-p $testport:80 -p $((testport+1)):443" -a https://nonexistingtest.site
echo Test9: Simultaneous HTTP and HTTPS servers
total_tests=$((total_tests + 1))
# Test HTTP redirect
if curl -vL http://localhost:$testport/testpath 2>&1 | grep -q "URL: 'https://nonexistingtest.site/testpath'"; then
http_success=1
else
http_success=0
fi
# Test HTTPS redirect
if curl -vL -k https://localhost:$((testport+1))/testpath 2>&1 | grep -q "URL: 'https://nonexistingtest.site/testpath'"; then
https_success=1
else
https_success=0
fi
if [ "$http_success" = "1" ] && [ "$https_success" = "1" ]; then
echo TEST SUCCESS
else
echo TEST FAILED - HTTP: $http_success, HTTPS: $https_success
failed_tests+=("Test9: Simultaneous HTTP and HTTPS servers")
fi
stop_container
# Test10: Permanent redirect (301) vs temporary (302)
start_container "-p $testport:80" -P https://nonexistingtest.site
test_redirect "Test10: Permanent redirect with -P option" "http://localhost:$testport" "301 Moved Permanently"
stop_container
# Test11: Custom port configuration
customport=9999
start_container "-p $customport:$customport" -p $customport https://nonexistingtest.site
test_redirect "Test11: Custom HTTP port with -p option" "http://localhost:$customport" "Location: https://nonexistingtest.site"
stop_container
# Test12: Help and version commands
echo Test12: Help and version options
total_tests=$((total_tests + 2))
if $builder run --rm $run_options -h 2>&1 | grep -q "Usage: no80"; then
echo "Help: TEST SUCCESS"
else
echo "Help: TEST FAILED"
failed_tests+=("Test12: Help option")
fi
if $builder run --rm $run_options -v 2>&1 | grep -q "no80 - The resource effective HTTP and HTTPS redirect server"; then
echo "Version: TEST SUCCESS"
else
echo "Version: TEST FAILED"
failed_tests+=("Test12: Version option")
fi
# Test13: Error handling for invalid parameters
echo Test13: Error handling for invalid port
total_tests=$((total_tests + 1))
if $builder run --rm $run_options -p 99999 https://example.com 2>&1 | grep -q "Invalid port number"; then
echo TEST SUCCESS
else
echo TEST FAILED
failed_tests+=("Test13: Error handling for invalid port")
fi
# Test14: HTTP performance benchmark
start_container "-p $testport:80" https://nonexistingtest.site
run_benchmark "Test14: HTTP performance benchmark" 100 100 $testport "http" "/test" "HTTP performance"
stop_container
# Test15: HTTPS performance benchmark
start_container "-p $testport:80 -p $((testport+1)):443" https://nonexistingtest.site
run_benchmark "Test15: HTTPS performance benchmark" 50 100 $((testport+1)) "https" "/test" "HTTPS performance"
stop_container
# Test16: Combined HTTP/HTTPS load test
start_container "-p $testport:80 -p $((testport+1)):443" -a https://nonexistingtest.site
# Combined test: 70 HTTP threads + 30 HTTPS threads, each doing 100 requests
echo "Test16: Combined HTTP/HTTPS concurrent load test"
echo "Testing concurrent HTTP and HTTPS load with 70×100 HTTP + 30×100 HTTPS = 10000 total requests..."
# Start HTTP benchmark in background
run_benchmark "Test16a: HTTP part" 70 100 $testport "http" "/load" "HTTP part of combined load" &
http_pid=$!
# Start HTTPS benchmark in background
run_benchmark "Test16b: HTTPS part" 30 100 $((testport+1)) "https" "/load" "HTTPS part of combined load" &
https_pid=$!
# Wait for both to complete
wait $http_pid
wait $https_pid
echo "Test16: Combined HTTP/HTTPS concurrent load test: TEST SUCCESS"
total_tests=$((total_tests + 1))
stop_container
# Test17: IPv4-only connectivity
echo Test17: IPv4-only redirect functionality
start_container "-p $testport:80" "https://nonexistingtest.site"
total_tests=$((total_tests + 1))
response=$(curl -4 -v http://localhost:$testport/ 2>&1)
if echo "$response" | grep -q "Location: https://nonexistingtest.site" && echo "$response" | grep -q "127.0.0.1:$testport"; then
echo "TEST SUCCESS"
else
echo "TEST FAILED"
echo "Expected: Location: https://nonexistingtest.site and IPv4 connection to 127.0.0.1"
echo "Response: $response"
failed_tests+=("Test17: IPv4-only redirect functionality")
fi
stop_container
# Test18: IPv6-only connectivity
echo Test18: IPv6-only redirect functionality
start_container "-p $testport:80" "https://nonexistingtest.site"
total_tests=$((total_tests + 1))
response=$(curl -6 -v http://localhost:$testport/ 2>&1)
if echo "$response" | grep -q "Location: https://nonexistingtest.site" && echo "$response" | grep -q "\[::1\]:$testport"; then
echo "TEST SUCCESS"
else
echo "TEST FAILED"
echo "Expected: Location: https://nonexistingtest.site and IPv6 connection to [::1]"
echo "Response: $response"
failed_tests+=("Test18: IPv6-only redirect functionality")
fi
stop_container
echo
echo "========================================="
echo "TEST SUMMARY"
echo "========================================="
echo "Total tests run: $total_tests"
echo "Failed tests: ${#failed_tests[@]}"
echo "Passed tests: $((total_tests - ${#failed_tests[@]}))"
if [ ${#failed_tests[@]} -eq 0 ]; then
echo
echo "✅ ALL TESTS PASSED!"
echo "Tests complete"
exit 0
else
echo
echo "❌ FAILED TESTS:"
for failed_test in "${failed_tests[@]}"; do
echo " - $failed_test"
done
echo
echo "Tests complete with failures"
exit 1
fi