CI: Add silent cmake SonarCloud scan
[ric-plt/lib/rmr.git] / test / app_test / run_ipv6_test.sh
1 #!/usr/bin/env ksh
2 # vim: ts=4 sw=4 noet :
3 #==================================================================================
4 #    Copyright (c) 2019-2021 Nokia
5 #    Copyright (c) 2018-2021 AT&T Intellectual Property
6 #    Copyright (c) 2023 Alexandre Huff
7 #
8 #   Licensed under the Apache License, Version 2.0 (the "License");
9 #   you may not use this file except in compliance with the License.
10 #   You may obtain a copy of the License at
11 #
12 #       http://www.apache.org/licenses/LICENSE-2.0
13 #
14 #   Unless required by applicable law or agreed to in writing, software
15 #   distributed under the License is distributed on an "AS IS" BASIS,
16 #   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 #   See the License for the specific language governing permissions and
18 #   limitations under the License.
19 #==================================================================================
20 #
21
22 # ---------------------------------------------------------------------------------
23 #       Mnemonic:       run_ipv6_test.sh
24 #       Abstract:       This is a simple script to set up and run the basic send/receive
25 #                               processes using IPv6 for some library validation on top of si95.
26 #                               This particular test starts a sender and a receiver application.
27 #                               All messages go to the receiver and an ack is sent back to the
28 #                               sending process.
29 #
30 #                               The number of message, and the delay between each message may be given
31 #                               on the command line.
32 #
33 #                               Example command line:
34 #                                       bash ./run_ipv6_test.sh         # default 10 messages at 1 msg/sec
35 #                                       bash ./run_ipv6_test.sh -n 10 -d 100000 # 10 messages, delay 100 ms
36 #
37 #       Date:           28 May 2023
38 #       Author:         Alexandre Huff
39 # ---------------------------------------------------------------------------------
40
41
42 # The sender and receiver are run asynch. Their exit statuses are captured in a
43 # file in order for the 'main' to pick them up easily.
44 #
45 function run_sender {
46         ./sender${si} $nmsg $delay
47         echo $? >/tmp/PID$$.src         # must communicate state back via file b/c asynch
48 }
49
50 # start receiver listening for nmsgs from each thread
51 function run_rcvr {
52         ./receiver${si} $nmsg
53         echo $? >/tmp/PID$$.rrc
54 }
55
56 # Generate a route table that is tailored to our needs.
57 #
58 function mk_rt {
59
60 cat <<endKat >ipv6.rt
61 # This is a route table to test IPv6 support with sender and receiver
62
63 newrt | start
64 mse | 0 |  0 | $localhost:4560
65 mse | 1 | 10 | $localhost:4560
66 mse | 2 | 20 | $localhost:4560
67 rte | 3 | $localhost:4560
68 rte | 4 | $localhost:4560
69 rte | 5 | $localhost:4560
70 rte | 6 | $localhost:4560
71 rte | 7 | $localhost:4560
72 rte | 8 | $localhost:4560
73 rte | 9 | $localhost:4560
74 newrt | end
75 endKat
76 }
77
78 function ensure_ipv6 {
79         v6=$(ip -6 address show lo)
80         if [[ ! -z $v6 ]]
81         then
82                 v4=$(ip -4 address show lo)
83                 if [[ ! -z $v4 ]]
84                 then
85                         export RMR_BIND_IF=$localhost   # force RMR to bind to an IPv6 address (defaults to IPv4 on dual stack)
86                         echo "[INFO] forcing RMR binding to $localhost"
87                 else
88                         echo "[INFO] using IPv6-only stack"     # RMR binds to any interface address, no need to specify RMR_BIND_IF
89                 fi
90         else
91                 echo "[WARN] skipping IPv6 test, unable to detect IPv6 stack"
92                 exit 0  # exit success in favor to allow overall tests to pass if all other tests have passed
93         fi
94 }
95
96 # ---------------------------------------------------------
97
98 nmsg=10                                         # total number of messages to be exchanged (-n value changes)
99 delay=1000000                           # microsec sleep between msg 1,000,000 == 1s
100 wait=1
101 rebuild=0
102 verbose=0
103 dev_base=1                                      # -D turns off to allow this to run on installed libs
104 force_make=0
105 si=""
106 localhost="[::1]"
107
108
109
110 while [[ $1 == -* ]]
111 do
112         case $1 in
113                 -B)     rebuild=1;;
114                 -d)     delay=$2; shift;;
115                 -D)     dev_base=0;;
116                 -n)     nmsg=$2; shift;;
117                 -M)     force_make=1;;
118                 -v)     verbose=1;;
119
120                 *)      echo "unrecognised option: $1"
121                         echo "usage: $0 [-B] [-M] [-d micro-sec-delay] [-n num-msgs]"
122                         echo "  -B forces an RMR rebuild"
123                         echo "  -M force test applications to rebuild"
124                         exit 1
125                         ;;
126         esac
127
128         shift
129 done
130
131 ensure_ipv6
132
133 if (( verbose ))
134 then
135         echo "2" >.verbose
136         export RMR_VCTL_FILE=".verbose"
137 fi
138
139 src_root="../.."
140 if [[ -z $BUILD_PATH ]]                                         # if not explicitly set, assume one of our standard spots
141 then
142         if [[ -d $src_root/.build ]]                    # look for build directory in expected places
143         then                                                                    # run scripts will honour this
144                 export BUILD_PATH=$src_root/.build
145         else
146                 if [[ -d $src_root/build ]]
147                 then
148                         export BUILD_PATH=$src_root/build
149                 else
150                         echo "[ERR]  BUILD_PATH not set and no logical build directory exists to use"
151                         echo "[INFO] tried: $src_root/build and $src_root/.build"
152                         exit 1
153                 fi
154         fi
155         echo "[INFO] using discovered build directory: $BUILD_PATH"
156 else
157         echo "[INFO] using externally supplied build directory: $BUILD_PATH"
158 fi
159
160 if (( rebuild ))
161 then
162         set -e
163         $SHELL ./rebuild.sh
164         set +e
165 fi
166
167 if [[ -z $LD_LIBRARY_PATH ]]                                    # cmake test will set and it must be honoured
168 then
169         if (( dev_base ))                                                       # assume we are testing against what we've built, not what is installed
170         then
171                 if [[ -d $BUILD_PATH/lib64 ]]
172                 then
173                         export LD_LIBRARY_PATH=$BUILD_PATH:$BUILD_PATH/lib64:$LD_LIBRARY_PATH
174                 else
175                         export LD_LIBRARY_PATH=$BUILD_PATH:$BUILD_PATH/lib:$LD_LIBRARY_PATH
176                 fi
177                 export LIBRARY_PATH=$LD_LIBRARY_PATH
178         else                                                                            # -D option gets us here to test an installed library
179                 export LD_LIBRARY_PATH=/usr/local/lib:$LD_LIBRARY_PATH
180                 export LIBRARY_PATH=$LD_LIBRARY_PATH
181         fi
182 fi
183
184 export RMR_SEED_RT=${RMR_SEED_RT:-./ipv6.rt}            # allow easy testing with different rt
185
186 if [[ ! -f $RMR_SEED_RT ]]                                                      # create special rt
187 then
188         mk_rt
189 fi
190
191 if (( rebuild || force_make )) || [[ ! -f ./sender${si} || ! -f ./receiver${si} ]]
192 then
193         if ! make -B sender${si} receiver${si} >/tmp/PID$$.log 2>&1
194         then
195                 echo "[FAIL] cannot find sender{$si} and/or receiver${si} binary, and cannot make them.... humm?"
196                 cat /tmp/PID$$.log
197                 rm -f /tmp/PID$$.*
198                 exit 1
199         fi
200 fi
201
202 run_rcvr &
203 sleep 2                         # if caller starts faster than rcvr we can drop, so pause a bit
204 run_sender &
205
206 wait
207 rrc=$(head -1 /tmp/PID$$.rrc)           # get pass/fail state from each
208 src=$(head -1 /tmp/PID$$.src)
209
210 if (( !! (src + rrc) ))
211 then
212         echo "[FAIL] sender rc=$src  receiver rc=$rrc"
213 else
214         echo "[PASS] sender rc=$src  receiver rc=$rrc"
215 fi
216
217 rm /tmp/PID$$.*
218 rm -f .verbose
219
220 exit $(( !! (src + rrc) ))
221