-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest.sh
More file actions
executable file
·118 lines (95 loc) · 3.19 KB
/
test.sh
File metadata and controls
executable file
·118 lines (95 loc) · 3.19 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
#! /bin/bash
# Color codes
RED='\033[0;31m'
GREEN='\033[0;32m'
NC='\033[0m' # No Color
# Test counters
TESTS_PASSED=0
TESTS_FAILED=0
# test STRING_TO_SEND EXPECTED_RESPONSE
function test {
local string_to_send="$1"
local expected_response="$2"
if [ $# -ne 2 ]; then
echo "Usage: test STRING_TO_SEND EXPECTED_RESPONSE"
return 1
fi
echo "========================"
echo
echo "Testing: '$string_to_send'"
# Send the string to ircserv and capture the response
# Use timeout and -w flag to ensure connection closes properly
local actual_response=$(echo "$string_to_send" | timeout 1 nc -w 2 localhost 6667 | tr -d '\0')
echo ""
echo -e "Expected: '$expected_response\n'"
echo -e "Actual: '$actual_response\n'"
# Compare the responses
if [ "$actual_response" = "$expected_response" ]; then
echo -e "${GREEN}✓ TEST PASSED${NC}"
((TESTS_PASSED++))
return 0
else
echo -e "${RED}✗ TEST FAILED${NC}"
((TESTS_FAILED++))
echo ""
return 1
fi
}
# Function to display test report and exit appropriately
function report_and_exit {
local total_tests=$((TESTS_PASSED + TESTS_FAILED))
echo "========================"
echo "TEST REPORT"
echo "========================"
echo "Total tests: $total_tests"
echo -e "Passed: ${GREEN}$TESTS_PASSED${NC}"
echo -e "Failed: ${RED}$TESTS_FAILED${NC}"
echo "========================"
if [ $TESTS_FAILED -gt 0 ]; then
echo -e "${RED}Some tests failed!${NC}"
exit 1
else
echo -e "${GREEN}All tests passed!${NC}"
exit 0
fi
}
# Start ircserv in background
echo "Starting IRC server..."
./ircserv 6667 password &
IRCSERV_PID=$!
# Wait a moment for the server to start
sleep 2
# Check if the server is still running
if ! kill -0 $IRCSERV_PID 2>/dev/null; then
echo "✗ ERROR: IRC server failed to start or exited immediately"
exit 1
fi
echo "IRC server started with PID: $IRCSERV_PID"
# Run the tests
WELCOME=$'001 tester :Welcome to the Internet Relay Network tester!\r
002 tester :Your host is AwsomeServer.\r
003 tester :This server was created today.\r'
test $'PASS\r\nQUIT\r\n' $'461 * PASS :Not enough parameters\r\nERROR :quit\r'
test $'PASS password\r\nNICK tester\r\nUSER username 2 3 4\r\nQUIT\r\n' "$WELCOME"$'\nERROR :quit\r'
test $'PASS wrong_password\r\nNICK tester565\r\nUSER username 2 3 4\r\nQUIT\r\n' $'464 tester565 :Password incorrect\r\nERROR :quit\r'
test $'PING :hello\r\nQUIT\r\n' $'PONG AwsomeServer :hello\r\nERROR :quit\r'
test $'CAP LS 302\r\nQUIT\r\n' $'CAP * LS :\r\nERROR :quit\r'
# Check if the server is still running after tests
if ! kill -0 $IRCSERV_PID 2>/dev/null; then
echo "✗ ERROR: IRC server exited unexpectedly during tests"
exit 1
fi
# Terminate the IRC server
echo "Terminating IRC server..."
kill $IRCSERV_PID 2>/dev/null
# Wait for the server to terminate gracefully
sleep 1
# Force kill if it's still running
if kill -0 $IRCSERV_PID 2>/dev/null; then
echo "Force killing IRC server..."
kill -9 $IRCSERV_PID 2>/dev/null
fi
echo "IRC server terminated successfully"
echo
# Display final report and exit
report_and_exit