1#!/bin/bash
2# Copyright (C) 2015-2017 Bayerische Motoren Werke Aktiengesellschaft (BMW AG)
3# This Source Code Form is subject to the terms of the Mozilla Public
4# License, v. 2.0. If a copy of the MPL was not distributed with this
5# file, You can obtain one at http://mozilla.org/MPL/2.0/.
6
7# Purpose: This script is needed to start the client and service with
8# one command. This is necessary as ctest - which is used to run the
9# tests - isn't able to start two binaries for one testcase. Therefore
10# the testcase simply executes this script. This script then runs client
11# and service and checks that both exit sucessfully.
12
13FAIL=0
14
15# Parameter 1: the pid to check
16check_tcp_udp_sockets_are_open ()
17{
18    # Check that the service does listen on at least one TCP/UDP socket
19    # awk is used to avoid the case when a inode number is the same as a PID. The awk
20    # program filters the netstat output down to the protocol (1st field) and
21    # the PID/Program name (last field) fields.
22    SERVICE_SOCKETS_LISTENING=$(netstat -tulpen 2> /dev/null | awk '{print $1 "\t"  $NF}' | grep $1 | wc -l)
23    if [ $SERVICE_SOCKETS_LISTENING -lt 1 ]
24    then
25        ((FAIL+=1))
26    fi
27}
28
29# Parameter 1: the pid to check
30check_tcp_udp_sockets_are_closed ()
31{
32    # Check that the service does not listen on any TCP/UDP socket
33    # or has any active connection via a TCP/UDP socket
34    # awk is used to avoid the case when a inode number is the same as a PID. The awk
35    # program filters the netstat output down to the protocol (1st field) and
36    # the PID/Program name (last field) fields.
37    SERVICE_SOCKETS_LISTENING=$(netstat -tulpen 2> /dev/null | awk '{print $1 "\t"  $NF}' | grep $1 | wc -l)
38    if [ $SERVICE_SOCKETS_LISTENING -ne 0 ]
39    then
40        ((FAIL+=1))
41    fi
42
43    SERVICE_SOCKETS_CONNECTED=$(netstat -tupen 2> /dev/null | awk '{print $1 "\t"  $NF}' | grep $1 | wc -l)
44    if [ $SERVICE_SOCKETS_CONNECTED -ne 0 ]
45    then
46        ((FAIL+=1))
47    fi
48}
49
50# Start the service
51export VSOMEIP_APPLICATION_NAME=external_local_routing_test_service
52export VSOMEIP_CONFIGURATION=external_local_routing_test_service.json
53./external_local_routing_test_service &
54SERIVCE_PID=$!
55sleep 1;
56
57check_tcp_udp_sockets_are_open $SERIVCE_PID
58
59# Start the client (we reuse the one from the local_routing_test to check
60# the local routing functionality).
61export VSOMEIP_APPLICATION_NAME=local_routing_test_client
62export VSOMEIP_CONFIGURATION=local_routing_test_client.json
63./local_routing_test_client &
64CLIENT_PID=$!
65
66check_tcp_udp_sockets_are_open $SERIVCE_PID
67sleep 1
68check_tcp_udp_sockets_are_closed $CLIENT_PID
69
70# wait that local client finishes
71sleep 2
72
73# Display a message to show the user that he must now call the external client
74# to finish the test successfully
75kill -0 $CLIENT_PID &> /dev/null
76CLIENT_STILL_THERE=$?
77if [ $CLIENT_STILL_THERE -ne 0 ]
78then
79    if [ ! -z "$USE_LXC_TEST" ]; then
80        echo "starting external_local_routing_test_starter.sh on slave LXC"
81        ssh -tt -i $SANDBOX_ROOT_DIR/commonapi_main/lxc-config/.ssh/mgc_lxc/rsa_key_file.pub -o StrictHostKeyChecking=no root@$LXC_TEST_SLAVE_IP "bash -ci \"set -m; cd \\\$SANDBOX_TARGET_DIR/vsomeip_lib/test; ./external_local_routing_test_client_external_start.sh\"" &
82        echo "remote ssh job id: $!"
83    elif [ ! -z "$USE_DOCKER" ]; then
84        docker exec $DOCKER_IMAGE sh -c "cd $DOCKER_TESTS && ./external_local_routing_test_client_external_start.sh" &
85    elif [ ! -z "$JENKINS" ]; then
86        ssh -tt -i $PRV_KEY -o StrictHostKeyChecking=no jenkins@$IP_SLAVE "bash -ci \"set -m; cd $WS_ROOT/build/test; ./external_local_routing_test_client_external_start.sh\" >> $WS_ROOT/slave_test_output 2>&1" &
87
88    else
89        cat <<End-of-message
90*******************************************************************************
91*******************************************************************************
92** Please now run:
93** external_local_routing_test_client_external_start.sh
94** from an external host to successfully complete this test.
95**
96** You probably will need to adapt the 'unicast' settings in
97** external_local_routing_test_client_external.json and
98** external_local_routing_test_service.json to your personal setup.
99*******************************************************************************
100*******************************************************************************
101End-of-message
102    fi
103fi
104
105if [ ! -z "$USE_DOCKER" ]; then
106    FAIL=0
107fi
108
109# Wait until client and service are finished
110for job in $(jobs -p)
111do
112    # Fail gets incremented if either client or service exit
113    # with a non-zero exit code
114    wait $job || ((FAIL+=1))
115done
116
117# Check if client and server both exited sucessfully and the service didnt't
118# have any open 
119if [ $FAIL -eq 0 ]
120then
121    exit 0
122else
123    exit 1
124fi
125