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    echo "\t -x [/ld/library/path32] set the LD_LIBRARY_PATH for servers32 and client32"
6    echo "\t -y [/ld/library/path64] set the LD_LIBRARY_PATH for servers64 and client64"
7}
8
9failed_test_cases=()
10ran_test_cases=0
11
12function run_test() {
13    if [ $1 -eq 32 ]; then
14        CLIENT_PATH=$CLIENT_PATH32
15        LD_LIB_PATH_CLIENT=$LD_LIB_PATH32
16    else
17        CLIENT_PATH=$CLIENT_PATH64
18        LD_LIB_PATH_CLIENT=$LD_LIB_PATH64
19    fi
20
21    if [ $2 -eq 32 ]; then
22        SERVER_PATH=$SERVER_PATH32
23        LD_LIB_PATH_SERVER=$LD_LIB_PATH32
24    else
25        SERVER_PATH=$SERVER_PATH64
26        LD_LIB_PATH_SERVER=$LD_LIB_PATH64
27    fi
28
29    if [ ! -f $SERVER_PATH ] || [ ! -f $CLIENT_PATH ]; then
30        echo "Not running $CLIENT_PATH with $SERVER_PATH since one doesn't exist"
31        return
32    fi
33
34    echo "Running $1-bit client with $2-bit servers"
35    echo "$LD_LIB_PATH_SERVER=$LD_LIB_PATH_SERVER"
36    LD_LIBRARY_PATH=$LD_LIB_PATH_SERVER:$LD_LIBRARY_PATH $SERVER_PATH &
37    SERVER_PID=$!
38    LD_LIBRARY_PATH=$LD_LIB_PATH_CLIENT:$LD_LIBRARY_PATH $CLIENT_PATH
39    if [ $? -ne 0 ]; then
40        failed_test_cases+=("$1-bit client with $2-bit servers")
41    fi
42    kill $SERVER_PID
43    ran_test_cases=1
44}
45
46function check_env() {
47    if [ -z ${TEST_NAME+x} ]; then
48        echo "TEST_NAME is unset";
49        exit 1
50    fi
51
52    if [ -z ${CLIENT_PATH32+x} ]; then
53        echo "CLIENT_PATH32 is unset";
54        exit 1
55    fi
56
57    if [ -z ${CLIENT_PATH64+x} ]; then
58        echo "CLIENT_PATH64 is unset";
59        exit 1
60    fi
61
62    if [ -z ${SERVER_PATH32+x} ]; then
63        echo "SERVER_PATH32 is unset";
64        exit 1
65    fi
66
67    if [ -z ${SERVER_PATH64+x} ]; then
68        echo "SERVER_PATH64 is unset";
69        exit 1
70    fi
71}
72
73#usage: run_all_tests server_path32 server_path64 client_path32 client_path64 test_name [-h/-c config/-x lib32/-y ldlib64]
74function run_all_tests() {
75    SERVER_PATH32=$1
76    SERVER_PATH64=$2
77    CLIENT_PATH32=$3
78    CLIENT_PATH64=$4
79    TEST_NAME=$5
80    check_env
81    shift 5
82
83    while getopts hc:x:y: opt;
84    do
85        case $opt in
86            h)
87                usage
88                exit 0
89                ;;
90            c)
91                case $OPTARG in
92                    CLIENT64_SERVER64)
93                        run_test 64 64
94                        ;;
95                    CLIENT32_SERVER32)
96                        run_test 32 32
97                        ;;
98                    CLIENT64_SERVER32)
99                        run_test 64 32
100                        ;;
101                    CLIENT32_SERVER64)
102                        run_test 32 64
103                        ;;
104                    *)
105                        echo "Error: unknown config value \"$OPTARG\""
106                        exit 1
107                        ;;
108                esac
109                exit 0
110                ;;
111            x)
112                LD_LIB_PATH32=$OPTARG
113                ;;
114            y)
115                LD_LIB_PATH64=$OPTARG
116                ;;
117            *)
118                echo "Error: unknown param \"$opt\""
119                usage
120                exit 1
121                ;;
122        esac
123    done
124
125    for i in 32 64
126        do
127            for j in 32 64
128                do
129                    run_test $i $j
130                done
131        done
132
133    if [ $ran_test_cases -ne 1 ]; then
134        failed_test_cases+=("No tests ran")
135    fi
136
137    count_failed_tests=${#failed_test_cases[@]}
138    echo "*********************************************************"
139    echo "    $TEST_NAME Final Summary:\n"
140    if [ $count_failed_tests -gt 0 ]; then
141        echo "    $TEST_NAME failed for the following cases:\n\t"
142        for each in "${failed_test_cases[@]}"
143            do
144                echo "\t$each"
145            done
146    else
147        echo "    $TEST_NAME passed for all cases!"
148    fi
149    echo "*********************************************************"
150
151    exit ${#failed_test_cases[@]}
152}
153