This repository has been archived by the owner on Mar 13, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_script.sh
146 lines (98 loc) · 5.58 KB
/
test_script.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
#!/bin/bash
# How to read this file:
#
# This file is written so that the critical commands can be understood clearly. You will see that every thing that
# relates to formatting the test output (and cleaning up) is indented with four tabs. This is intentional to make
# sure that the commands that you may want to copy on your terminal when trying to debug something are clearly
# visible.
set -e # Comment this line if you want to keep your test running even if something failed (this will prevent the test from failing).
if [ -z "$var" ]; then export TERM=ansi; fi
function show_output {
tput bold; tput setaf 6; echo " [-] Showing server output"; tput sgr0;
echo
tput bold; tput setaf 6; echo "-------------------- Start of Server Output --------------------"; tput sgr0;
cat torrent_samples/server_output.tmp
tput bold; tput setaf 6; echo "-------------------- End of Server Output --------------------"; tput sgr0;
echo
tput bold; tput setaf 6; echo " [-] Showing client output"; tput sgr0;
echo
tput bold; tput setaf 6; echo "-------------------- Start of Client Output --------------------"; tput sgr0;
cat torrent_samples/client_output.tmp
tput bold; tput setaf 6; echo "-------------------- End of Client Output --------------------"; tput sgr0;
echo
}
function finish_before_log {
tput bold; tput setaf 1; echo "Execution failed: $BASH_COMMAND (the program crashed or returned a non-zero value; you may want to reproduce this failure outside of the test environment and debug your code)"; tput sgr0;
show_output
rm -f torrent_samples/client/test_file torrent_samples/client_output.tmp torrent_samples/server_output.tmp
tput bold; tput setaf 4;
echo "##################################################################"
echo "TESTS RESULT: $RESULT"
echo "##################################################################"
tput sgr0;
kill -9 "$SERVERPID" 2> /dev/null > /dev/null
}
function finish_log_ok {
rm -f torrent_samples/client/test_file torrent_samples/client_output.tmp torrent_samples/server_output.tmp
tput bold; tput setaf 4;
echo "##################################################################"
echo "TESTS RESULT: $RESULT"
echo "##################################################################"
tput sgr0;
}
# We start with a failed test and if everything works, we change this at the end of the test.
RESULT="FAIL"
tput bold; tput setaf 4; # This is just so that the output colored and readeable
echo "##################################################################"
echo "TRIVIAL TORRENT CLIENT (testing against reference server)"
echo "##################################################################"
tput sgr0; # Reset colors
trap finish_before_log EXIT
# Ensure file is not already there!
rm -f torrent_samples/client/test_file
tput bold; tput setaf 6; echo " [-] Starting Reference Server"; tput sgr0;
# Launch server
reference_binary/ttorrent -l 8080 torrent_samples/server/test_file_server.ttorrent > torrent_samples/server_output.tmp 2>&1 &
SERVERPID=$!
tput bold; tput setaf 6; echo " [-] Waiting one second for it to start"; tput sgr0;
sleep 1
tput bold; tput setaf 6; echo " [-] Starting Client"; tput sgr0;
# Launch client
bin/ttorrent torrent_samples/client/test_file.ttorrent > torrent_samples/client_output.tmp 2>&1
trap finish_log_ok EXIT
show_output
tput bold; tput setaf 6; echo " [-] Killing server..."; tput sgr0;
kill -9 "$SERVERPID" 2> /dev/null > /dev/null || { tput bold; tput setaf 1; echo "Server is dead but should be alive!"; tput sgr0; exit 1 ; }
tput bold; tput setaf 6; echo " [-] Comparing downloaded file"; tput sgr0;
# Compare result
cmp torrent_samples/server/test_file_server torrent_samples/client/test_file || {
tput bold; tput setaf 1; echo "ERROR: downloaded file does not match!"; tput sgr0; false;
}
rm -f torrent_samples/client/test_file torrent_samples/client_output.tmp torrent_samples/server_output.tmp
tput bold; tput setaf 4;
echo "##################################################################"
echo "TRIVIAL TORRENT SERVER (testing against reference client)"
echo "##################################################################"
tput sgr0;
trap finish_before_log EXIT
tput bold; tput setaf 6; echo " [-] Starting Server"; tput sgr0;
# Launch server
bin/ttorrent -l 8080 torrent_samples/server/test_file_server.ttorrent > torrent_samples/server_output.tmp 2>&1 &
SERVERPID=$!
tput bold; tput setaf 6; echo " [-] Waiting one second for it to start"; tput sgr0;
sleep 1
tput bold; tput setaf 6; echo " [-] Starting Reference Client"; tput sgr0;
# Launch client
reference_binary/ttorrent torrent_samples/client/test_file.ttorrent > torrent_samples/client_output.tmp 2>&1
trap finish_log_ok EXIT
show_output
tput bold; tput setaf 6; echo " [-] Killing server..."; tput sgr0;
kill -9 "$SERVERPID" 2> /dev/null > /dev/null || { tput bold; tput setaf 1; echo "Server is dead but should be alive!"; tput sgr0; exit 1 ; }
tput bold; tput setaf 6; echo " [-] Comparing downloaded file"; tput sgr0;
# Compare result
cmp torrent_samples/server/test_file_server torrent_samples/client/test_file || {
tput bold; tput setaf 1; echo "ERROR: downloaded file does not match!"; tput sgr0; false;
}
rm -f torrent_samples/client/test_file torrent_samples/client_output.tmp torrent_samples/server_output.tmp
# Test passed
RESULT="PASS"