-
Notifications
You must be signed in to change notification settings - Fork 0
/
test-connection.sh
executable file
·155 lines (117 loc) · 2.7 KB
/
test-connection.sh
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
#!/bin/bash
TEST_FILE=test.txt
TEST_BUCKET_NAME=test-bucket
STD_REDDIR='/dev/null'
SERVER_PID=0
ERROR=0
clean_testing_files() {
rm -r -f $TEST_BUCKET_NAME
rm -f $TEST_FILE
}
start_server() {
# starts the server, sets the server PID
python src/start-server -P $1 -s $TEST_BUCKET_NAME >"$STD_REDDIR" 2>&1 &
SERVER_PID=$!
sleep 0.2
}
stop_server() {
# stops the server, sets the server PID to 0
if ps -p "$SERVER_PID" > /dev/null
then
kill "$SERVER_PID"
fi
SERVER_PID=0
}
check_server_status() {
# exits the program if the last background job exited
# regardless of status code
if ps -p "$SERVER_PID" > /dev/null
then
kill -0 "$SERVER_PID"
fi
if [ $? -ne 0 ]; then
echo "ERROR: Server exited unexpectedly"
exit 1
fi
}
generate_file() {
# generates a file with $1 characters
if [ $1 -lt 1000 ]; then
head -c $1 /dev/urandom > $TEST_FILE
else
dd if=/dev/urandom of=$TEST_FILE bs=1000 count=$(($1 / 1000)) 2> /dev/null
fi
}
client_upload_file() {
python src/upload-file -P $1 -s . -n $TEST_FILE >"$STD_REDDIR" 2>&1 &
wait $!
if [ $? -ne 0 ]; then
echo "ERROR: Client exited unexpectedly"
ERROR=1
fi
diff $TEST_FILE $TEST_BUCKET_NAME/$TEST_FILE >/dev/null 2>&1
if [ $? -ne 0 ]; then
echo "ERROR: Files differ."
ERROR=1
fi
sleep 0.1
}
client_download_file() {
python src/download-file -P $1 -d . -n $TEST_FILE >"$STD_REDDIR" 2>&1 &
wait $!
if [ $? -ne 0 ]; then
echo "ERROR: Client exited unexpectedly"
fi
diff $TEST_FILE $TEST_BUCKET_NAME/$TEST_FILE >/dev/null 2>&1
if [ $? -ne 0 ]; then
echo "ERROR: Files differ."
ERROR=1
fi
sleep 0.1
}
kill_system() {
jobs -p | xargs kill -9 >/dev/null 2>&1
}
test_system() {
# test system
# args: <PROTOCOL> <file size in bytes>
printf "Testing $1 with a random file of $2 bytes... "
clean_testing_files
start_server $1
check_server_status
generate_file $2
client_upload_file $1
check_server_status
client_download_file $1
check_server_status
stop_server
clean_testing_files
if [ $ERROR -eq 0 ]; then
echo "OK"
else
echo "FAIL"
return 1
fi
}
trap kill_system SIGINT
if [ "$#" == 0 ]; then
echo "Invalid parameter count"
echo "Usage: ./test-connection.sh <PROTOCOL> [-v]"
exit 1
fi
if [ "$2" == '-v' ]; then
STD_REDDIR='/dev/tty'
fi
for i in 100 500 1000 10000 100000 500000; do
test_system $1 $i
sleep 0.25
if [ $ERROR -ne 0 ]; then
break
fi
done
kill_system
if [ $ERROR -ne 0 ]; then
exit 1
else
exit 0
fi