CI: Add silent cmake SonarCloud scan
[ric-plt/lib/rmr.git] / test / app_test / run_neg_rmr_size_test.sh
1 #!/bin/bash
2 #/usr/bin/env ksh
3 # vim: ts=4 sw=4 noet :
4 #==================================================================================
5 #    Copyright (c) 2019-2021 Nokia
6 #    Copyright (c) 2018-2021 AT&T Intellectual Property.
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_app_test.ksh
24 #       Abstract:       This is a simple script to set up and run the basic send/receive
25 #                               processes for some library validation on top of nng.
26 #                               It should be possible to clone the repo, switch to this directory
27 #                               and execute  'ksh run -B'  which will build RMr, make the sender and
28 #                               recevier then  run the basic test.
29 #
30 #                               Example command line:
31 #                                       ksh ./run_app_test.ksh          # default 20 messages at 2 msg/sec
32 #                                       ksh ./run_app_test.ksh -d 100 -n 10000 # send 10k messages with 100ms delay between
33 #
34 #       Date:           22 April 2019
35 #       Author:         E. Scott Daniels
36 # ---------------------------------------------------------------------------------
37
38
39 # The sender and receiver are run asynch. Their exit statuses are captured in a
40 # file in order for the 'main' to pick them up easily.
41 #
42 function run_sender {
43
44 #       first we send an invalid message with a negative message length 
45 #       as per RIC-989 / CVE-2023-40998 processes used to  crash for such 
46 #       invalid size data. So this test case would fail with a crash
47 #       if for some reason the bug comes back.
48         bash ./create_invalid_request.sh
49         cat /tmp/invalid_data.bin |netcat -w 2 127.0.0.1 4560
50         
51 #   An invalid request with invalid header length information
52 #   As per RIC-919 / CVE-2023-40997 this caused a crash. This
53 #   test case is to make sure the bug does not come back
54         bash ./create_invalid_request2.sh
55         cat /tmp/invalid_data2.bin |netcat -w 2 127.0.0.1 4560
56   
57 #   now let's send still one valid message over another TCP 
58 #   connection
59         bash ./create_valid_request.sh
60         cat /tmp/valid_data.bin|netcat -w 2 127.0.0.1 4560
61
62         echo $? >/tmp/PID$$.src         # must communicate state back via file b/c asynch
63
64 }
65
66 function run_rcvr {
67         export RMR_LOG_VLEVEL=5
68         if (( mt_receiver ))
69         then
70                 echo "<TEST> testing with mt-receiver" >&2
71                 ./mt_receiver${si} $nmsg
72         else
73                 ./receiver${si} $nmsg
74         fi
75         echo $? >/tmp/PID$$.rrc
76 }
77
78 # snarf the first v4 IP (not the loopback) that belongs to this box/container/guest
79 function snarf_ip {
80         ip addr| sed -e '/inet /b c; d' -e ':c' -e '/127.0.0.1/d; s!/.*!!; s!^.* !!; q'
81 }
82
83 #       Drop a contrived route table in. This table should add a reference to our 
84 #       local IP to an entry to ensure that the route table collector code in RMr
85 #       is removing 'hairpin' loops. If RMr isn't removing the references to our
86 #       hostname and IP address when it builds the endpoint lists, the sender will
87 #       send messages to itself some of the time, causing the receiver to come up 
88 #       short when comparing messages received with expected count and thus failing.
89 #
90 function set_rt {
91         typeset port=4560                       # port the receiver listens on by default
92
93         cat <<endKat >app_test.rt
94                 newrt | start
95                         mse | 0 |  0 | 127.0.0.1:$port,$my_ip:43086
96                         mse | 1 | 10 | 127.0.0.1:$port,${my_host//.*/}:43086
97                         mse | 2 | 20 | 127.0.0.1:$port
98                         rte | 3 | 127.0.0.1:$port
99                         mse | 3 | 100 | 127.0.0.1:$port # special test to ensure that this does not affect previous entry
100                         rte | 4 | 127.0.0.1:$port
101                         rte | 5 | 127.0.0.1:$port
102                         rte | 6 | 127.0.0.1:$port
103                         rte | 7 | 127.0.0.1:$port
104                         rte | 8 | 127.0.0.1:$port
105                         rte | 9 | 127.0.0.1:$port
106                         rte | 10 | 127.0.0.1:$port
107                         rte | 11 | 127.0.0.1:$port
108                         rte | 12 | 127.0.0.1:$port
109                         rte | 13 | 127.0.0.1:$port
110                 newrt | end
111
112 head -3 app_test.rt
113
114 endKat
115
116 }
117
118 # ---------------------------------------------------------
119
120 nmsg=1                                          # total number of messages to be exchanged (-n value changes)
121                                                         # need two sent to each receiver to ensure hairpin entries were removed (will fail if they were not)
122 delay=100000                            # microsec sleep between msg 1,000,000 == 1s
123 wait=1
124 rebuild=0
125 nopull=""                                       # -b sets so that build does not pull
126 verbose=0
127 use_installed=0
128 my_ip=$(snarf_ip)                       # get an ip to insert into the route table
129 keep=0
130 mt_receiver=0                           # -m sets in order to test with multi-threaded receive stuff
131 force_make=0
132
133 echo --------------------
134 echo running test for negative message size
135 echo --------------------
136
137
138 while [[ $1 == -* ]]
139 do
140         case $1 in
141                 -B)     rebuild=1;;                                             # build with pull first
142                 -b)     rebuild=1; nopull="nopull";;    # buld without pull
143                 -d)     delay=$2; shift;;
144                 -k) keep=1;;
145                 -i) use_installed=1;;
146                 -M)     force_make=1;;
147                 -m)     mt_receiver=1;;
148                 -n)     nmsg=$2; shift;;
149                 -N)     ;;                                                      # ignored for back compat; NNG not supported; all binaries are si95
150                 -S)     ;;                                                      # ignored for back compat; all binaries are si95 and no _si suffix is used
151                 -v)     verbose=1;;
152
153                 *)      echo "unrecognised option: $1"
154                         echo "usage: $0 [-B] [-d micor-sec-delay] [-i] [-k] [-M] [-m] [-n num-msgs] [-S]"
155                         echo "  -B forces an RMR rebuild which will use .build"
156                         echo "  -i causes the installd libraries (/usr/local) to be referenced; -B is ignored if supplied"
157                         echo "  -k keeps the route table"
158                         echo "  -M force make on test applications"
159                         echo "  -m test with mt-receive mode"
160                         echo "  -S build/test SI95 based binaries"
161                         echo ""
162                         echo "total number of messages must > 20 to correctly test hairpin loop removal"
163                         exit 1
164                         ;;
165         esac
166
167         shift
168 done
169
170 if [[ ! -f app_test.rt ]]               # we need the real host name in the local.rt; build one from mask if not there
171 then
172         my_host=$(hostname)
173         set_rt
174         if (( verbose ))
175         then
176                 cat app_test.rt
177         fi
178 fi
179
180 if (( verbose ))
181 then
182         echo "4" >.verbose
183         export RMR_VCTL_FILE=".verbose"
184 fi
185
186
187 if (( use_installed ))                  # point at installed library rather than testing the build
188 then
189         export LD_LIBRARY_PATH=/usr/local/lib
190         export LIBRARY_PATH=$LD_LIBRARY_PATH
191 else
192         if (( rebuild ))
193         then
194                 set -e
195                 $SHELL ./rebuild.ksh $nopull | read build_path
196                 set +e
197         else
198                 build_path=${BUILD_PATH:-"../../.build"}        # we prefer .build at the root level, but allow user option
199         
200                 if [[ ! -d $build_path ]]
201                 then
202                         echo "[FAIL] cannot find build in: $build_path"
203                         echo "[FAIL] either create, and then build RMr, or set BUILD_PATH as an evironment var before running this"
204                         exit 1
205                 fi
206         fi
207
208         if [[ -z $LD_LIBRARY_PATH ]]            # the cmake test environment will set this; we must use if set
209         then
210                 if [[ -d $build_path/lib64 ]]
211                 then
212                         export LD_LIBRARY_PATH=$build_path:$build_path/lib64
213                 else
214                         export LD_LIBRARY_PATH=$build_path:$build_path/lib
215                 fi
216         fi
217         export LIBRARY_PATH=$LD_LIBRARY_PATH
218 fi
219
220 export RMR_SEED_RT=${RMR_SEED_RT:-./app_test.rt}                # allow easy testing with different rt
221
222 if (( force_make )) || [[ ! -f ./sender${si} || ! -f ./receiver${si} ]]
223 then
224         if ! make -B sender${si} receiver${si} >/tmp/PID$$.clog 2>&1
225         then
226                 echo "[FAIL] cannot find sender${si} and/or receiver${si} binary, and cannot make them.... humm?"
227                 echo "[INFO] ------------- PATH settings -----------------"
228                 env | grep PATH
229                 echo "[INFO] ------------- compiler output (head) -----------------"
230                 head -50 /tmp/PID$$.clog
231                 rm -fr /tmp/PID$$.*
232                 exit 1
233         fi
234 fi
235
236 #exit
237 run_rcvr &
238 sleep 2                         # if sender starts faster than rcvr we can drop msgs, so pause a bit
239 netstat -nptl|grep receiver 
240 netstat -nptl|grep sender 
241
242 run_sender &
243
244 wait
245 head -1 /tmp/PID$$.rrc | read rrc
246 head -1 /tmp/PID$$.src | read src
247
248 if (( !! (src + rrc) ))
249 then
250         echo "[FAIL] sender rc=$src  receiver rc=$rrc"
251 else
252         echo "[PASS] sender rc=$src  receiver rc=$rrc"
253 fi
254
255 if (( ! keep )) 
256 then
257         rm app_test.rt
258 fi
259
260 rm /tmp/PID$$.*
261 #rm -f .verbose
262
263 exit $(( !! (src + rrc) ))
264