1function usage() {
2    echo "runs $TEST_NAME"
3    echo "\t -h help"
4    echo "\t -c [CLIENT64_SERVER64 | CLIENT32_SERVER32 | CLIENT64_SERVER32 | CLIENT32_SERVER64] configures whether to run 32-bit or 64-bit versions of the servers and client"
5}
6
7failed_test_cases=()
8
9function run_test() {
10    if [ $1 -eq 32 ]; then
11        CLIENT_PATH=$CLIENT_PATH32
12    else
13        CLIENT_PATH=$CLIENT_PATH64
14    fi
15
16    if [ $2 -eq 32 ]; then
17        SERVER_PATH=$SERVER_PATH32
18    else
19        SERVER_PATH=$SERVER_PATH64
20    fi
21
22    echo "Running $1-bit client with $2-bit servers"
23    $SERVER_PATH &
24    sleep 1
25    SERVER_PID=$!
26    $CLIENT_PATH
27    if [ $? -ne 0 ]; then
28        failed_test_cases+=("$1-bit client with $2-bit servers")
29    fi
30    kill $SERVER_PID
31}
32
33function check_env() {
34    if [ -z ${TEST_NAME+x} ]; then
35        echo "TEST_NAME is unset";
36        exit 1
37    fi
38
39    if [ -z ${CLIENT_PATH32+x} ]; then
40        echo "CLIENT_PATH32 is unset";
41        exit 1
42    fi
43
44    if [ -z ${CLIENT_PATH64+x} ]; then
45        echo "CLIENT_PATH64 is unset";
46        exit 1
47    fi
48
49    if [ -z ${SERVER_PATH32+x} ]; then
50        echo "SERVER_PATH32 is unset";
51        exit 1
52    fi
53
54    if [ -z ${SERVER_PATH64+x} ]; then
55        echo "SERVER_PATH64 is unset";
56        exit 1
57    fi
58}
59
60#usage: run_all_tests server_path32 server_path64 client_path32 client_path64 test_name [-h/-c config]
61function run_all_tests() {
62    SERVER_PATH32=$1
63    SERVER_PATH64=$2
64    CLIENT_PATH32=$3
65    CLIENT_PATH64=$4
66    TEST_NAME=$5
67    check_env
68    shift 5
69
70    if [ $# -eq 0 ]; then
71        for i in 32 64
72            do
73                for j in 32 64
74                    do
75                        run_test $i $j
76                    done
77            done
78        count_failed_tests=${#failed_test_cases[@]}
79        echo "*********************************************************"
80        echo "    $TEST_NAME Final Summary:\n"
81        if [ $count_failed_tests -gt 0 ]; then
82            echo "    $TEST_NAME failed for the following cases:\n\t"
83            for each in "${failed_test_cases[@]}"
84                do
85                    echo "\t$each"
86                done
87        else
88            echo "    $TEST_NAME passed for all cases!"
89        fi
90        echo "*********************************************************"
91    fi
92
93    while getopts hc: opt;
94    do
95        case $opt in
96            h)
97                usage
98                exit 0
99                ;;
100            c)
101                case $OPTARG in
102                    CLIENT64_SERVER64)
103                        run_test 64 64
104                        ;;
105                    CLIENT32_SERVER32)
106                        run_test 32 32
107                        ;;
108                    CLIENT64_SERVER32)
109                        run_test 64 32
110                        ;;
111                    CLIENT32_SERVER64)
112                        run_test 32 64
113                        ;;
114                    *)
115                        echo "Error: unknown config value \"$OPTARG\""
116                        exit 1
117                        ;;
118                esac
119                ;;
120            *)
121                echo "Error: unknown param \"$opt\""
122                usage
123                exit 1
124                ;;
125        esac
126    done
127    exit ${#failed_test_cases[@]}
128}
129