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
16# Parameter 2: number of TCP/UDP sockets the process should have open
17check_tcp_udp_sockets_are_open ()
18{
19    # Check that the passed pid/process does listen on at least one TCP/UDP socket
20    # awk is used to avoid the case when a inode number is the same as a PID. The awk
21    # program filters the netstat output down to the protocol (1st field) and
22    # the PID/Program name (last field) fields.
23    SERVICE_SOCKETS_LISTENING=$(netstat -tulpen 2> /dev/null | awk '{print $1 "\t"  $NF}' | grep $1 | wc -l)
24    if [ $SERVICE_SOCKETS_LISTENING -lt $2 ]
25    then
26        ((FAIL+=1))
27    fi
28}
29
30# Parameter 1: the pid to check
31check_tcp_udp_sockets_are_closed ()
32{
33    # Check that the passed pid/process does not listen on any TCP/UDP socket
34    # or has any active connection via a TCP/UDP socket
35    # awk is used to avoid the case when a inode number is the same as a PID. The awk
36    # program filters the netstat output down to the protocol (1st field) and
37    # the PID/Program name (last field) fields.
38    SERVICE_SOCKETS_LISTENING=$(netstat -tulpen 2> /dev/null | awk '{print $1 "\t"  $NF}' | grep $1 | wc -l)
39    if [ $SERVICE_SOCKETS_LISTENING -ne 0 ]
40    then
41        ((FAIL+=1))
42    fi
43
44    SERVICE_SOCKETS_CONNECTED=$(netstat -tupen 2> /dev/null | awk '{print $1 "\t"  $NF}' | grep $1 | wc -l)
45    if [ $SERVICE_SOCKETS_CONNECTED -ne 0 ]
46    then
47        ((FAIL+=1))
48    fi
49}
50
51# Start the service for payload test with UDP
52export VSOMEIP_APPLICATION_NAME=external_local_payload_test_service
53export VSOMEIP_CONFIGURATION=external_local_payload_test_service.json
54./payload_test_service --udp &
55SERIVCE_PID=$!
56
57# Display a message to show the user that he must now call the external client
58# to finish the test successfully
59if [ ! -z "$USE_LXC_TEST" ]; then
60    echo "starting external local payload on slave LXC"
61    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_payload_test_client_external_start.sh\"" &
62    echo "remote ssh job id: $!"
63elif [ ! -z "$USE_DOCKER" ]; then
64    docker exec $DOCKER_IMAGE sh -c "cd $DOCKER_TESTS && ./external_local_payload_test_client_external_start.sh" &
65elif [ ! -z "$JENKINS" ]; then
66    ssh -tt -i $PRV_KEY -o StrictHostKeyChecking=no jenkins@$IP_SLAVE "bash -ci \"set -m; cd $WS_ROOT/build/test; ./external_local_payload_test_client_external_start.sh\" >> $WS_ROOT/slave_test_output 2>&1" &
67
68else
69cat <<End-of-message
70*******************************************************************************
71*******************************************************************************
72** Please now run:
73** external_local_payload_test_client_external_start.sh
74** from an external host to successfully complete this test.
75**
76** You probably will need to adapt the 'unicast' settings in
77** external_local_payload_test_client_external.json and
78** external_local_payload_test_service.json to your personal setup.
79*******************************************************************************
80*******************************************************************************
81End-of-message
82fi
83
84# The service should listen on a TCP and UDP socket now
85sleep 1
86check_tcp_udp_sockets_are_open $SERIVCE_PID 2
87
88# Wait until service is finished
89# The client remotely shuts down the service if he has successfully transmitted
90# all the packets with different payloads. Therefore we can assume that everything
91# went well, even if we can only check the exit code of the service here.
92
93# Fail gets incremented if either client or service exit
94# with a non-zero exit code
95wait $SERIVCE_PID || ((FAIL+=1))
96
97
98# Start the service for payload test with tcp
99export VSOMEIP_APPLICATION_NAME=external_local_payload_test_service
100export VSOMEIP_CONFIGURATION=external_local_payload_test_service.json
101./payload_test_service --tcp &
102SERIVCE_PID=$!
103
104# The service should listen on a TCP and UDP socket now
105sleep 1
106check_tcp_udp_sockets_are_open $SERIVCE_PID 2
107
108
109# Wait until service is finished
110# The client remotely shuts down the service if he has successfully transmitted
111# all the packets with different payloads. Therefore we can assume that everything
112# went well, even if we can only check the exit code of the service here.
113
114# Fail gets incremented if either client or service exit
115# with a non-zero exit code
116wait $SERIVCE_PID || ((FAIL+=1))
117
118# Check if server exited sucessfully
119if [ $FAIL -eq 0 ]
120then
121    exit 0
122else
123    exit 1
124fi
125