1#!/bin/bash
2#
3# Copyright (c) 2020 Intel Corporation
4# SPDX-License-Identifier: Apache-2.0
5
6# Simple TCP tester that queries a large HTTP file and checks whether it
7# contains the expected data. The network sample to respond these queries is
8# samples/net/sockets/dumb_http_srv_mt. With this sample we are not testing
9# HTTP because this sample does not implement HTTP server but always returns
10# a static file.
11
12if [ `basename $0` == "https-get-file-test.sh" ]; then
13    PROTO=https
14    EXTRA_OPTS="--insecure"
15else
16    PROTO=http
17    EXTRA_OPTS=""
18fi
19
20SERVER=192.0.2.1
21PORT=8080
22
23# The file we are expecting to receive is found in
24# samples/net/sockets/dumb_http_server_mt/src/response_100k.html.bin
25EXPECTED_FILE_SIZE=108222
26EXPECTED_FILE_MD5SUM=d969441601d473db911f28dae62101b7
27
28# If user gives a parameter to this function, it is used as a count how
29# many times to ask the data. The value 0 (default) loops forever.
30
31if [ -z "$1" ]; then
32    COUNT=0
33else
34    COUNT=$1
35fi
36
37DOWNLOAD_FILE=/tmp/`basename $0`.$$
38MD5SUM_FILE=${DOWNLOAD_FILE}.md5sum
39
40echo "$EXPECTED_FILE_MD5SUM $DOWNLOAD_FILE" > $MD5SUM_FILE
41
42CONNECTION_TIMEOUT=0
43STATUS=0
44STOPPED=0
45trap ctrl_c INT TERM
46
47ctrl_c() {
48    STOPPED=1
49}
50
51while [ $STOPPED -eq 0 ]; do
52    curl ${PROTO}://${SERVER}:${PORT} $EXTRA_OPTS --max-time 5 --output $DOWNLOAD_FILE
53    STATUS=$?
54    if [ $STATUS -ne 0 ]; then
55	if [ $STATUS -eq 28 ]; then
56	    CONNECTION_TIMEOUT=1
57	fi
58
59	break
60    fi
61
62    FILE_SIZE=$(stat --printf=%s $DOWNLOAD_FILE)
63    if [ $FILE_SIZE -ne $EXPECTED_FILE_SIZE ]; then
64        echo "Wrong size, expected $EXPECTED_FILE_SIZE, got $FILE_SIZE"
65        STATUS=1
66	break
67    fi
68
69    md5sum --status -c $MD5SUM_FILE
70    STATUS=$?
71    if [ $STATUS -ne 0 ]; then
72	break
73    fi
74
75    if [ $COUNT -eq 0 ]; then
76	continue
77    fi
78
79    COUNT=$(expr $COUNT - 1)
80    if [ $COUNT -eq 0 ]; then
81	break
82    fi
83done
84
85rm -f $DOWNLOAD_FILE $MD5SUM_FILE
86
87if [ $CONNECTION_TIMEOUT -eq 0 ]; then
88    if [ $STATUS -eq 0 ]; then
89	curl -d "OK" -X POST $EXTRA_OPTS ${PROTO}://${SERVER}:${PORT}
90    else
91	curl -d "FAIL" -X POST $EXTRA_OPTS ${PROTO}://${SERVER}:${PORT}
92    fi
93fi
94
95exit $STATUS
96