1c87c5fbaSopenharmony_ci#!/bin/bash
2c87c5fbaSopenharmony_ci
3c87c5fbaSopenharmony_ci#
4c87c5fbaSopenharmony_ci# This script is used to run the oscore interop tests as specified in
5c87c5fbaSopenharmony_ci# https://core-wg.github.io/oscore/test-spec5.html
6c87c5fbaSopenharmony_ci#
7c87c5fbaSopenharmony_ci# By default, this script should be run in the examples directory.
8c87c5fbaSopenharmony_ci#
9c87c5fbaSopenharmony_ci# Run as
10c87c5fbaSopenharmony_ci#  ./oscore_testcases.sh [-h remote-target-IP] [-B port-B-OSCORE] \
11c87c5fbaSopenharmony_ci#                        [-D port-D-OSCORE] [-N port-NO-OSCORE] \
12c87c5fbaSopenharmony_ci#                        [-s executable-for-interop-server] \
13c87c5fbaSopenharmony_ci#                        [-c executable-for-client] \
14c87c5fbaSopenharmony_ci#                        [-P] [-F]
15c87c5fbaSopenharmony_ci#
16c87c5fbaSopenharmony_ci# -h remote-target-IP
17c87c5fbaSopenharmony_ci#  Remote server hosting interop tests if not running the interop server on this host.
18c87c5fbaSopenharmony_ci#
19c87c5fbaSopenharmony_ci# -B port-B-OSCORE
20c87c5fbaSopenharmony_ci#  Port that the server listening on providing B OSCORE security profile
21c87c5fbaSopenharmony_ci#
22c87c5fbaSopenharmony_ci# -D port-D-OSCORE
23c87c5fbaSopenharmony_ci#  Port that the server listening on providing D OSCORE security profile
24c87c5fbaSopenharmony_ci#
25c87c5fbaSopenharmony_ci# -N port-N-OSCORE
26c87c5fbaSopenharmony_ci#  Port that the server listening on providing no security profile
27c87c5fbaSopenharmony_ci#
28c87c5fbaSopenharmony_ci# -s executable-for-interop-server
29c87c5fbaSopenharmony_ci#  Exectuable to use for the interop server if not the default of ./oscore-interop-server.
30c87c5fbaSopenharmony_ci#
31c87c5fbaSopenharmony_ci# -c executable-for-client
32c87c5fbaSopenharmony_ci#  Exectuable to use for the coap client if not the default of ./coap-client.
33c87c5fbaSopenharmony_ci#
34c87c5fbaSopenharmony_ci# -P
35c87c5fbaSopenharmony_ci#  Output partial client logs
36c87c5fbaSopenharmony_ci#
37c87c5fbaSopenharmony_ci# -F
38c87c5fbaSopenharmony_ci#  Output full client logs
39c87c5fbaSopenharmony_ci#
40c87c5fbaSopenharmony_ci
41c87c5fbaSopenharmony_ciINDIR=`dirname $0`
42c87c5fbaSopenharmony_ci
43c87c5fbaSopenharmony_ci# Defaults
44c87c5fbaSopenharmony_ci
45c87c5fbaSopenharmony_ci# host running oscore interop server
46c87c5fbaSopenharmony_ciTARGET_IP=127.0.0.1
47c87c5fbaSopenharmony_ci# Server with B OSCORE Security
48c87c5fbaSopenharmony_ciS_PORT_B=5683
49c87c5fbaSopenharmony_ci# Server with D OSCORE Security
50c87c5fbaSopenharmony_ciS_PORT_D=5685
51c87c5fbaSopenharmony_ci# Server with no Security
52c87c5fbaSopenharmony_ciS_PORT_N=5687
53c87c5fbaSopenharmony_ci# Client app
54c87c5fbaSopenharmony_ciCLIENT=$INDIR/coap-client
55c87c5fbaSopenharmony_ci# SERVER app
56c87c5fbaSopenharmony_ciSERVER=$INDIR/oscore-interop-server
57c87c5fbaSopenharmony_ci# Partial Logs
58c87c5fbaSopenharmony_ciPARTIAL_LOGS=no
59c87c5fbaSopenharmony_ci# Full Logs
60c87c5fbaSopenharmony_ciFULL_LOGS=no
61c87c5fbaSopenharmony_ci
62c87c5fbaSopenharmony_ciwhile getopts "c:h:s:B:D:FN:P" OPTION; do
63c87c5fbaSopenharmony_ci  case $OPTION in
64c87c5fbaSopenharmony_ci    c)
65c87c5fbaSopenharmony_ci      CLIENT="$OPTARG"
66c87c5fbaSopenharmony_ci      ;;
67c87c5fbaSopenharmony_ci    h)
68c87c5fbaSopenharmony_ci      TARGET_IP="$OPTARG"
69c87c5fbaSopenharmony_ci      ;;
70c87c5fbaSopenharmony_ci    s)
71c87c5fbaSopenharmony_ci      SERVER="$OPTARG"
72c87c5fbaSopenharmony_ci      ;;
73c87c5fbaSopenharmony_ci    B)
74c87c5fbaSopenharmony_ci      S_PORT_B="$OPTARG"
75c87c5fbaSopenharmony_ci      ;;
76c87c5fbaSopenharmony_ci    D)
77c87c5fbaSopenharmony_ci      S_PORT_D="$OPTARG"
78c87c5fbaSopenharmony_ci      ;;
79c87c5fbaSopenharmony_ci    F)
80c87c5fbaSopenharmony_ci      FULL_LOGS=yes
81c87c5fbaSopenharmony_ci      ;;
82c87c5fbaSopenharmony_ci    N)
83c87c5fbaSopenharmony_ci      S_PORT_N="$OPTARG"
84c87c5fbaSopenharmony_ci      ;;
85c87c5fbaSopenharmony_ci    P)
86c87c5fbaSopenharmony_ci      PARTIAL_LOGS=yes
87c87c5fbaSopenharmony_ci      ;;
88c87c5fbaSopenharmony_ci    *)
89c87c5fbaSopenharmony_ci      echo Error in options detected
90c87c5fbaSopenharmony_ci      echo Run as
91c87c5fbaSopenharmony_ci      echo "$0 [-h remote-target-IP] [-B port-B-OSCORE]"
92c87c5fbaSopenharmony_ci      echo "      [-D port-D-OSCORE] [-N port-NO-OSCORE]"
93c87c5fbaSopenharmony_ci      echo "      [-s executable-for-interop-server]"
94c87c5fbaSopenharmony_ci      echo "      [-c executable-for-client]"
95c87c5fbaSopenharmony_ci      echo "      [-P] [-F]"
96c87c5fbaSopenharmony_ci      exit 1
97c87c5fbaSopenharmony_ci  esac
98c87c5fbaSopenharmony_cidone
99c87c5fbaSopenharmony_ci
100c87c5fbaSopenharmony_citimecheck () {
101c87c5fbaSopenharmony_ci  timeout $*
102c87c5fbaSopenharmony_ci  if [ $? = 124 ] ; then
103c87c5fbaSopenharmony_ci    echo "****** Timed Out ******"
104c87c5fbaSopenharmony_ci  fi
105c87c5fbaSopenharmony_ci}
106c87c5fbaSopenharmony_ci
107c87c5fbaSopenharmony_ciNO_PASS=0
108c87c5fbaSopenharmony_ciNO_FAIL=0
109c87c5fbaSopenharmony_ci# passfail count egrep-expression
110c87c5fbaSopenharmony_cipassfail () {
111c87c5fbaSopenharmony_ci  PASS=`cat /tmp/client_out | egrep "$2" | wc -l`
112c87c5fbaSopenharmony_ci  if [ "$PASS" = "$1" ] ; then
113c87c5fbaSopenharmony_ci    echo Pass
114c87c5fbaSopenharmony_ci    let "NO_PASS=$NO_PASS+1"
115c87c5fbaSopenharmony_ci  else
116c87c5fbaSopenharmony_ci    echo Fail
117c87c5fbaSopenharmony_ci    let "NO_FAIL=$NO_FAIL+1"
118c87c5fbaSopenharmony_ci  fi
119c87c5fbaSopenharmony_ci  if [ "$FULL_LOGS" = yes ] ; then
120c87c5fbaSopenharmony_ci    cat /tmp/client_out
121c87c5fbaSopenharmony_ci  elif [ "$PARTIAL_LOGS" = yes ] ; then
122c87c5fbaSopenharmony_ci    cat /tmp/client_out | egrep -v " DEBG | OSC  "
123c87c5fbaSopenharmony_ci  fi
124c87c5fbaSopenharmony_ci}
125c87c5fbaSopenharmony_ci
126c87c5fbaSopenharmony_ci$SERVER -E $INDIR/interop/b_server.conf -v8 -p $S_PORT_B > /tmp/server_b 2>&1 &
127c87c5fbaSopenharmony_ci$SERVER -E $INDIR/interop/d_server.conf -v8 -p $S_PORT_D > /tmp/server_d 2>&1 &
128c87c5fbaSopenharmony_ci$SERVER                                 -v8 -p $S_PORT_N > /tmp/server_n 2>&1 &
129c87c5fbaSopenharmony_ci
130c87c5fbaSopenharmony_cisleep 1
131c87c5fbaSopenharmony_ci
132c87c5fbaSopenharmony_ci# Reset sequence number counters
133c87c5fbaSopenharmony_cirm -f /tmp/client_a
134c87c5fbaSopenharmony_cirm -f /tmp/client_c
135c87c5fbaSopenharmony_ci
136c87c5fbaSopenharmony_ci# Test 0 General checkout
137c87c5fbaSopenharmony_ciecho -n "Test 0 - "
138c87c5fbaSopenharmony_citimecheck 10 $CLIENT -w -v8 coap://$TARGET_IP:$S_PORT_B/oscore/hello/coap 2>&1 | egrep -v " DEBG | OSC  " > /tmp/client_out
139c87c5fbaSopenharmony_cipassfail 1 "^Hello World"
140c87c5fbaSopenharmony_ci
141c87c5fbaSopenharmony_ci# Test 1
142c87c5fbaSopenharmony_ciecho -n "Test 1 - "
143c87c5fbaSopenharmony_citimeout 10 $CLIENT -w -v8 -E $INDIR/interop/a_client.conf,/tmp/client_a coap://$TARGET_IP:$S_PORT_B/oscore/hello/1 > /tmp/client_out 2>&1
144c87c5fbaSopenharmony_cipassfail 1 "^Hello World"
145c87c5fbaSopenharmony_ci
146c87c5fbaSopenharmony_ci# Test 2
147c87c5fbaSopenharmony_ciecho -n "Test 2 - "
148c87c5fbaSopenharmony_citimeout 10 $CLIENT -w -v8 -E $INDIR/interop/c_client.conf,/tmp/client_c coap://$TARGET_IP:$S_PORT_D/oscore/hello/1 > /tmp/client_out 2>&1
149c87c5fbaSopenharmony_cipassfail 1 "^Hello World"
150c87c5fbaSopenharmony_ci
151c87c5fbaSopenharmony_ci# Test 3
152c87c5fbaSopenharmony_ciecho -n "Test 3 - "
153c87c5fbaSopenharmony_citimeout 10 $CLIENT -w -v8 -E $INDIR/interop/a_client.conf,/tmp/client_a coap://$TARGET_IP:$S_PORT_B/oscore/hello/2?first=1 > /tmp/client_out 2>&1
154c87c5fbaSopenharmony_cipassfail 1 "^Hello World"
155c87c5fbaSopenharmony_ci
156c87c5fbaSopenharmony_ci# Test 4
157c87c5fbaSopenharmony_ciecho -n "Test 4 - "
158c87c5fbaSopenharmony_citimeout 10 $CLIENT -w -v8 -E $INDIR/interop/a_client.conf,/tmp/client_a -A 0 coap://$TARGET_IP:$S_PORT_B/oscore/hello/3 > /tmp/client_out 2>&1
159c87c5fbaSopenharmony_cipassfail 1 "^Hello World"
160c87c5fbaSopenharmony_ci
161c87c5fbaSopenharmony_ci# Test 5
162c87c5fbaSopenharmony_ciecho -n "Test 5 - "
163c87c5fbaSopenharmony_citimeout 10 $CLIENT -w -v8 -E $INDIR/interop/a_client.conf,/tmp/client_a -s 2 coap://$TARGET_IP:$S_PORT_B/oscore/hello/1 > /tmp/client_out 2>&1
164c87c5fbaSopenharmony_cipassfail 1 "^Hello World"
165c87c5fbaSopenharmony_ci
166c87c5fbaSopenharmony_ci# Test 6
167c87c5fbaSopenharmony_ciecho -n "Test 6 - "
168c87c5fbaSopenharmony_citimeout 10 $CLIENT -w -v8 -E $INDIR/interop/a_client.conf,/tmp/client_a -s 4 coap://$TARGET_IP:$S_PORT_B/oscore/observe1 > /tmp/client_out > /tmp/client_out 2>&1
169c87c5fbaSopenharmony_cipassfail 3 "^one|^two|^5.00 Terminate Observe"
170c87c5fbaSopenharmony_ci
171c87c5fbaSopenharmony_ci# Test 7
172c87c5fbaSopenharmony_ciecho -n "Test 7 - "
173c87c5fbaSopenharmony_citimeout 10 $CLIENT -w -v8 -E $INDIR/interop/a_client.conf,/tmp/client_a -s 2 coap://$TARGET_IP:$S_PORT_B/oscore/observe2 > /tmp/client_out 2>&1
174c87c5fbaSopenharmony_cipassfail 3 "^one|^two"
175c87c5fbaSopenharmony_ci
176c87c5fbaSopenharmony_ci# Test 8
177c87c5fbaSopenharmony_ciecho -n "Test 8 - "
178c87c5fbaSopenharmony_citimeout 10 $CLIENT -w -v8 -E $INDIR/interop/a_client.conf,/tmp/client_a -m post -e "%4a" -t 0 coap://$TARGET_IP:$S_PORT_B/oscore/hello/6 > /tmp/client_out 2>&1
179c87c5fbaSopenharmony_cipassfail 1 "^J$"
180c87c5fbaSopenharmony_ci
181c87c5fbaSopenharmony_ci# Test 9
182c87c5fbaSopenharmony_ciecho -n "Test 9 - "
183c87c5fbaSopenharmony_citimeout 10 $CLIENT -w -v8 -E $INDIR/interop/a_client.conf,/tmp/client_a -m put -e "%7a" -t 0 -O 1,0x7b coap://$TARGET_IP:$S_PORT_B/oscore/hello/7 > /tmp/client_out 2>&1
184c87c5fbaSopenharmony_cipassfail 1 "^z"
185c87c5fbaSopenharmony_ci
186c87c5fbaSopenharmony_ci# Test 10
187c87c5fbaSopenharmony_ciecho -n "Test 10 - "
188c87c5fbaSopenharmony_citimeout 10 $CLIENT -w -v8 -E $INDIR/interop/a_client.conf,/tmp/client_a -m put -e "%8a" -t 0 -O 5 coap://$TARGET_IP:$S_PORT_B/oscore/hello/7 > /tmp/client_out 2>&1
189c87c5fbaSopenharmony_cipassfail 1 "^4.12 Precondition Failed"
190c87c5fbaSopenharmony_ci
191c87c5fbaSopenharmony_ci# Test 11
192c87c5fbaSopenharmony_ciif [ "$SUPPRESS" = no ] ; then
193c87c5fbaSopenharmony_ci  echo
194c87c5fbaSopenharmony_cifi
195c87c5fbaSopenharmony_ciecho -n "Test 11 - "
196c87c5fbaSopenharmony_citimeout 10 $CLIENT -w -v8 -E $INDIR/interop/a_client.conf,/tmp/client_a -m delete coap://$TARGET_IP:$S_PORT_B/oscore/test > /tmp/client_out 2>&1
197c87c5fbaSopenharmony_cipassfail 1 "^v:1 t:CON c:2.02 i:"
198c87c5fbaSopenharmony_ci
199c87c5fbaSopenharmony_ci# Test 12
200c87c5fbaSopenharmony_ciif [ "$SUPPRESS" = no ] ; then
201c87c5fbaSopenharmony_ci  echo
202c87c5fbaSopenharmony_cifi
203c87c5fbaSopenharmony_ciecho -n "Test 12 - "
204c87c5fbaSopenharmony_citimeout 10 $CLIENT -w -v8 -E $INDIR/interop/e_client.conf,/tmp/client_a coap://$TARGET_IP:$S_PORT_B/oscore/hello/1 > /tmp/client_out 2>&1
205c87c5fbaSopenharmony_cipassfail 1 "^4.01 Security context not found"
206c87c5fbaSopenharmony_ci
207c87c5fbaSopenharmony_ci# Test 13
208c87c5fbaSopenharmony_ciif [ "$SUPPRESS" = no ] ; then
209c87c5fbaSopenharmony_ci  echo
210c87c5fbaSopenharmony_cifi
211c87c5fbaSopenharmony_ciecho -n "Test 13 - "
212c87c5fbaSopenharmony_citimeout 10 $CLIENT -w -v8 -E $INDIR/interop/f_client.conf,/tmp/client_a coap://$TARGET_IP:$S_PORT_B/oscore/hello/1 > /tmp/client_out 2>&1
213c87c5fbaSopenharmony_cipassfail 1 "^4.00 Decryption failed"
214c87c5fbaSopenharmony_ci
215c87c5fbaSopenharmony_ci# Test 14
216c87c5fbaSopenharmony_ciif [ "$SUPPRESS" = no ] ; then
217c87c5fbaSopenharmony_ci  echo
218c87c5fbaSopenharmony_cifi
219c87c5fbaSopenharmony_ciecho -n "Test 14 - "
220c87c5fbaSopenharmony_citimeout 10 $CLIENT -w -v8 -E $INDIR/interop/g_client.conf,/tmp/client_a coap://$TARGET_IP:$S_PORT_B/oscore/hello/1 > /tmp/client_out 2>&1
221c87c5fbaSopenharmony_cipassfail 1 "WARN OSCORE: Decryption Failure, result code: -5"
222c87c5fbaSopenharmony_ci
223c87c5fbaSopenharmony_ci# Test 15
224c87c5fbaSopenharmony_ciif [ "$SUPPRESS" = no ] ; then
225c87c5fbaSopenharmony_ci  echo
226c87c5fbaSopenharmony_cifi
227c87c5fbaSopenharmony_ciecho -n "Test 15 - "
228c87c5fbaSopenharmony_citimeout 10 $CLIENT -w -v8 -E $INDIR/interop/a_client.conf coap://$TARGET_IP:$S_PORT_B/oscore/hello/1 > /tmp/client_out 2>&1
229c87c5fbaSopenharmony_cipassfail 1 "^4.01 Replay detected"
230c87c5fbaSopenharmony_ci
231c87c5fbaSopenharmony_ci# Test 16
232c87c5fbaSopenharmony_ciif [ "$SUPPRESS" = no ] ; then
233c87c5fbaSopenharmony_ci  echo
234c87c5fbaSopenharmony_cifi
235c87c5fbaSopenharmony_ciecho -n "Test 16 - "
236c87c5fbaSopenharmony_citimeout 10 $CLIENT -w -v8 -E $INDIR/interop/e_client.conf,/tmp/client_a coap://$TARGET_IP:$S_PORT_N/oscore/hello/coap > /tmp/client_out 2>&1
237c87c5fbaSopenharmony_cipassfail 1 "^4.02 Bad Option"
238c87c5fbaSopenharmony_ci
239c87c5fbaSopenharmony_ci# Test 17
240c87c5fbaSopenharmony_ciif [ "$SUPPRESS" = no ] ; then
241c87c5fbaSopenharmony_ci  echo
242c87c5fbaSopenharmony_cifi
243c87c5fbaSopenharmony_ciecho -n "Test 17 - "
244c87c5fbaSopenharmony_citimeout 10 $CLIENT -w -v8 coap://$TARGET_IP:$S_PORT_N/oscore/hello/1 > /tmp/client_out 2>&1
245c87c5fbaSopenharmony_cipassfail 1 "^4.01 Unauthorized"
246c87c5fbaSopenharmony_ci
247c87c5fbaSopenharmony_ciKILL_SERVER=`basename $SERVER`
248c87c5fbaSopenharmony_ciif [ ! -z "$KILL_SERVER" ] ; then
249c87c5fbaSopenharmony_ci  killall $KILL_SERVER
250c87c5fbaSopenharmony_cifi
251c87c5fbaSopenharmony_ci
252c87c5fbaSopenharmony_ciecho
253c87c5fbaSopenharmony_ciecho ===============
254c87c5fbaSopenharmony_ciecho Pass:  $NO_PASS
255c87c5fbaSopenharmony_ciecho Fail:  $NO_FAIL
256c87c5fbaSopenharmony_ci#Starts with test 0
257c87c5fbaSopenharmony_ciecho Total: 18
258c87c5fbaSopenharmony_ci
259c87c5fbaSopenharmony_ciif [ "$NO_FAIL" != 0 ] ; then
260c87c5fbaSopenharmony_ci  exit 1
261c87c5fbaSopenharmony_cifi
262