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