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_closed ()
17{
18    # Check that the service does not listen on any TCP/UDP socket
19    # or has any active connection via a 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 -ne 0 ]
25    then
26        ((FAIL+=1))
27    fi
28
29    SERVICE_SOCKETS_CONNECTED=$(netstat -tupen 2> /dev/null | awk '{print $1 "\t"  $NF}' | grep $1 | wc -l)
30    if [ $SERVICE_SOCKETS_CONNECTED -ne 0 ]
31    then
32        ((FAIL+=1))
33    fi
34}
35
36# Start the service
37export VSOMEIP_APPLICATION_NAME=local_payload_test_service
38export VSOMEIP_CONFIGURATION=local_payload_test_service.json
39./payload_test_service &
40SERIVCE_PID=$!
41sleep 1;
42
43check_tcp_udp_sockets_are_closed  $SERIVCE_PID
44
45# Start the client
46export VSOMEIP_APPLICATION_NAME=local_payload_test_client
47export VSOMEIP_CONFIGURATION=local_payload_test_client.json
48./payload_test_client &
49CLIENT_PID=$!
50
51check_tcp_udp_sockets_are_closed  $SERIVCE_PID
52check_tcp_udp_sockets_are_closed  $CLIENT_PID
53
54# Wait until client and service are finished
55for job in $(jobs -p)
56do
57    # Fail gets incremented if either client or service exit
58    # with a non-zero exit code
59    wait $job || ((FAIL+=1))
60done
61
62# Check if client and server both exited sucessfully and the service didnt't
63# have any open tcp/udp sockets
64if [ $FAIL -eq 0 ]
65then
66    exit 0
67else
68    exit 1
69fi
70