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
52export VSOMEIP_APPLICATION_NAME=external_local_payload_test_service
53export VSOMEIP_CONFIGURATION=external_local_payload_test_service.json
54./payload_test_service &
55SERIVCE_PID=$!
56sleep 1;
57
58# The service should listen on a TCP and UDP socket now
59check_tcp_udp_sockets_are_open $SERIVCE_PID 2
60
61# Start the client
62export VSOMEIP_APPLICATION_NAME=external_local_payload_test_client_local
63export VSOMEIP_CONFIGURATION=external_local_payload_test_client_local.json
64./payload_test_client &
65CLIENT_PID=$!
66
67# The service should still listen on a TCP and UDP socket now
68check_tcp_udp_sockets_are_open  $SERIVCE_PID 2
69# The client should use the shortcut over a local UDS instead of TCP/UDP,
70# therefore he shouldn't have any open TCP/UDP sockets
71check_tcp_udp_sockets_are_closed  $CLIENT_PID
72
73if [ ! -z "$USE_DOCKER" ]; then
74    FAIL=0
75fi
76
77# Wait until client and service are finished
78for job in $(jobs -p)
79do
80    # Fail gets incremented if either client or service exit
81    # with a non-zero exit code
82    wait $job || ((FAIL+=1))
83done
84
85# Check if client and server both exited sucessfully
86if [ $FAIL -eq 0 ]
87then
88    exit 0
89else
90    exit 1
91fi
92