1#!/bin/bash 2 3# test coap implementation for the ETSI CoAP Plugtest in March 2012 4# with test cases described in Plugtests Guide First Draft V0.0.16 5 6COAP_CLIENT=./coap-client 7tcpdump=/usr/sbin/tcpdump 8DEFAULTPORT=5683 9CLIENTPORT=61701 10# directory for logging 11LOGDIR=logs 12# set client's verbose level 13callopts=" -v 5" 14longtimeout=180 15clienttimeout=$longtimeout 16timeoutcmd=/usr/bin/timeout 17 18#URIREGEX=.*\/.* 19# resembles approximately an ip address 20IPADDRREGEX="^[1-2]?[0-9]{1,2}\.[1-2]?[0-9]{1,2}\.[1-2]?[0-9]{1,2}\.[1-2]?[0-9]{1,2}" 21# FIXME IPV6 address 22IP6REGEX=".*:.*:.*" 23 24# Testcase groups 25CORE=( TD_COAP_CORE_01 TD_COAP_CORE_02 TD_COAP_CORE_03 26TD_COAP_CORE_04 TD_COAP_CORE_05 TD_COAP_CORE_06 TD_COAP_CORE_07 27TD_COAP_CORE_08 TD_COAP_CORE_09 TD_COAP_CORE_10 TD_COAP_CORE_11 28TD_COAP_CORE_12 TD_COAP_CORE_13 TD_COAP_CORE_14 TD_COAP_CORE_15 ) 29LINK=( TD_COAP_LINK_01 TD_COAP_LINK_02 ) 30BLOCK=( TD_COAP_BLOCK_01 TD_COAP_BLOCK_02 TD_COAP_BLOCK_03 TD_COAP_BLOCK_04 ) 31OBS=( TD_COAP_OBS_01 TD_COAP_OBS_02 TD_COAP_OBS_03 TD_COAP_OBS_04 TD_COAP_OBS_05 ) 32 33testgroups=( CORE LINK BLOCK OBS ) 34 35# if no test cases are specified, we want to run all tests 36testnumber=-1 37group='' 38 39source `dirname $0`/etsi_testcases.sh 40 41function usage { 42echo "Usage: `basename $0` [-n testnumber] [-g groupname] [-t timeout] [-P server_port] [-p client port] [-d logdir] [-v] -i interface server_address" 1>&2 43echo "-n test case to be accomplished" 1>&2 44echo "-g group to be tested" 1>&2 45echo "-t time in seconds until timout for single test" 1>&2 46echo "-i interface to use for tcpdump" 1>&2 47echo "-P port of server" 1>&2 48echo "-p port client listens on" 1>&2 49echo "-d directory for logfiles" 1>&2 50echo "-v verbose level" 1>&2 51} 52 53function run_test { 54 tn=$1 55 clientopts='' 56 if [ -z $1 ]; then 57 echo "missing argument for run_test" 58 exit 1 59 fi 60 echo -e "running test: $tn" 61 if [ $(type -t $tn) ] ; then 62 $tn $tn 63 echo 64 else 65 echo "not implemented" 66 echo 67 fi 68} 69 70while getopts "n:g:t:i:P:p:d:v" OPTION; 71do 72# A missing argument for an option leads getopts to take the next 73# option as the parameter. We want to prevent that. 74case $OPTARG in 75-*) echo "Missing argument for option \"-$OPTION\"." 76echo $USAGE 77exit 1 78;; 79esac 80 81case $OPTION in 82n) # number of test case 83testnumber=$((OPTARG-1)) 84;; 85 86g) # name of test group 87# is there a group with that name? 88for i in "${testgroups[@]}" 89 do 90 # group doesn't have to be case sensitive 91 tmpgroup=$(echo $OPTARG | tr '[:lower:]' '[:upper:]') 92 if [ $i == $tmpgroup ] ; then 93 group=$tmpgroup 94 break 95 fi 96done 97if [ -z $group ] ; then 98 echo "No such group:" $OPTARG". Available groups are: ${testgroups[@]}" 99 exit 1 100fi 101;; 102 103t) 104# add timeout to client parameters 105clienttimeout=$((OPTARG)) 106callopts="$callopts -B $clienttimeout" 107;; 108 109i) 110# interface tcpdump listens on 111INTERFACE=$OPTARG 112;; 113 114P) 115# port the server listens on 116SERVERPORT=$((OPTARG)) 117;; 118 119p) 120# port the client listens on 121CLIENTPORT=$((OPTARG)) 122;; 123 124d) 125# directory tcpdump writes the logfiles into 126LOGDIR=$OPTARG 127;; 128 129v) 130verbose=1 131;; 132 133?) 134# any other option is invalid 135echo -e $USAGE 1>&2 136exit 1 137;; 138esac 139done 140 141# catch last argument: server address 142ARGS=$(($#+1)) 143SERVERADDRESS=${@: -1} 144 145if [[ ! $((ARGS-OPTIND)) -eq 1 ]]; then 146 echo -e "\nno server address specified" 147 usage 148 exit 1 149fi 150 151# if no port number was specified by user, the server address for the 152# coap-client is $SERVERADDRESS 153 154if [ -z $SERVERPORT ]; then 155 SERVERPORT=$DEFAULTPORT 156 if [[ $SERVERADDRESS =~ $IP6REGEX ]]; then 157 SERVERTUP=\[$SERVERADDRESS\] 158 else 159 SERVERTUP=$SERVERADDRESS 160 fi 161else 162 if [[ $SERVERADDRESS =~ $IP6REGEX ]]; then 163 SERVERTUP=\[$SERVERADDRESS\]:$SERVERPORT 164 else 165 SERVERTUP=$SERVERADDRESS:$SERVERPORT 166 fi 167fi 168 169# create directory for logging, if it's not already there 170if [[ ! -e $LOGDIR ]]; then 171 mkdir -p $LOGDIR 172 if [ $? ]; then 173 echo created directory \""$LOGDIR"\" for logging 174 fi 175fi 176 177# the interface for tcpdump is mandatory 178if [ -z $INTERFACE ]; then 179 echo -e "\nno interface given" 180 exit 1 181fi 182 183# determine which tests to run 184if [ -n "$group" ] ; then 185 echo group: $group 186 if [[ ! $testnumber -eq -1 ]] ; then 187 groupsize=$(eval "echo \${#$(echo $group)[@]}") 188 # is there a testcase with number $testnumber in group $group 189 if [ $testnumber -ge $groupsize -o $testnumber -lt 0 ] ; then 190 echo "No such testcase number: $OPTARG. Test cases numbers are 1 to" $groupsize 191 exit 1 192 else 193 # run test with group $group and number $testnumber 194 run_test $(eval "echo \${$(echo $group)[$testnumber]}") 195 fi 196 else 197 # if no testnumber was specified, we want to run all tests in that group 198 for i in $(eval "echo \${$(echo $group)[@]}") ; do 199 run_test $i 200 done 201 fi 202else 203 # run all tests of all groups 204 for j in ${testgroups[@]} ; do 205 echo "group: $j" 206 for k in $(eval "echo \${$(echo $j)[@]}") ; do 207 run_test $k 208 done 209 done 210fi 211