aboutsummaryrefslogtreecommitdiffstats
path: root/tests/pysim-test.sh
blob: 5af7a938cd071ba5f0bfaeda0175e1bf1b56fd92 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
#!/bin/bash

# Utility to verify the functionality of pysim-prog.py
#
# (C) 2018 by Sysmocom s.f.m.c. GmbH
# All Rights Reserved
#
# Author: Philipp Maier
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

PYSIM_PROG=../pySim-prog.py
PYSIM_READ=../pySim-read.py
TEMPFILE=temp.tmp

set -e

echo "pysim-test - a test program to test pysim-prog.py"
echo "================================================="

# Generate a list of the cards we expect to see by checking which .ok files
# are present
function gen_card_list {
    N_CARDS=0

    echo "Expecting to see the following cards:"

    for I in *.data ; do
	CARD_NAMES[$N_CARDS]=${I%.*}
	CARD_SEEN[$N_CARDS]=0
	N_CARDS=$((N_CARDS+1))
    done

    for I in $(seq 0 $((N_CARDS-1))); do
	echo ${CARD_NAMES[$I]}
    done
}

# Increment counter in card list for a specified card name (type)
function inc_card_list {
    CARD_NAME=$1
    for I in $(seq 0 $((N_CARDS-1))); do
	if [ $CARD_NAME = ${CARD_NAMES[$I]} ]; then
	    CARD_SEEN[$I]=$((${CARD_NAMES[$I]}+1))
	fi
    done
}

# Check the card list, each card must be seen exactly one times
function check_card_list {
    for I in $(seq 0 $((N_CARDS-1))); do
	if [ ${CARD_SEEN[$I]} -ne 1 ]; then
	    echo "Error: Card ${CARD_NAMES[$I]} seen ${CARD_SEEN[$I]} times!"
	    exit 1
	fi
    done

    echo "All cards seen -- everything ok!"
}

# Verify the contents of a card by reading them and then diffing against the
# previously created .ok file
function check_card {
    TERMINAL=$1
    CARD_NAME=$2
    echo "Verifying card ..."
    stat ./$CARD_NAME.ok > /dev/null
    python $PYSIM_READ -p $TERMINAL > $TEMPFILE
    set +e
    CARD_DIFF=$(diff $TEMPFILE ./$CARD_NAME.ok)
    set -e

    if [ "$CARD_DIFF" != "" ]; then
	echo "Card contents do not match the test data:"
	echo "Expected: $CARD_NAME.ok"
	echo "------------8<------------"
	cat "$CARD_NAME.ok"
	echo "------------8<------------"
	echo "Got:"
	echo "------------8<------------"
	cat $TEMPFILE
	echo "------------8<------------"
	exit 1
    fi

    inc_card_list $CARD_NAME

    echo "Card contents match the test data -- success!"
    rm $TEMPFILE
}

# Read out the card using pysim-read and store the result as .ok file. This
# data will be used later in order to verify the results of our write tests.
function gen_ok_file {
    TERMINAL=$1
    CARD_NAME=$2
    python $PYSIM_READ -p $TERMINAL > "$CARD_NAME.ok"
    echo "Generated file: $CARD_NAME.ok"
    echo "------------8<------------"
    cat "$CARD_NAME.ok"
    echo "------------8<------------"
}

# Find out the type (card name) of the card that is installed in the specified
# reader
function probe_card {
    TERMINAL=$1
    RESULT=$(timeout 5 $PYSIM_PROG -p $TERMINAL -T | cut -d ":" -f 2 | tail -n 1 | xargs)
    echo $RESULT
}

# Read out all cards and store the results as .ok files
function gen_ok_files {
    echo "== OK FILE GENERATION =="
    for I in $(seq 0 $((N_TERMINALS-1))); do
	echo "Probing card in terminal #$I"
	CARD_NAME=$(probe_card $I)
	if [ -z "$CARD_NAME" ]; then
	    echo "Error: Unresponsive card!"
	    exit 1
	fi
	echo "Card is of type: $CARD_NAME"
	gen_ok_file $I $CARD_NAME
    done
}

# Execute tests. Each card is programmed and the contents are checked
# afterwards.
function run_test {
    for I in $(seq 0 $((N_TERMINALS-1))); do
	echo "== EXECUTING TEST =="
	echo "Probing card in terminal #$I"
	CARD_NAME=$(probe_card $I)
	if [ -z "$CARD_NAME" ]; then
	    echo "Error: Unresponsive card!"
	    exit 1
	fi
	echo "Card is of type: $CARD_NAME"

	# Make sure some default data is set
	MCC=001
	MNC=01
	ICCID=1122334455667788990
	KI=FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF
	OPC=FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF
	IMSI=001010000000001
	ADM=00000000

	. "$CARD_NAME.data"
	python $PYSIM_PROG -p $I -t $CARD_NAME -o $OPC -k $KI -x $MCC -y $MNC -i $IMSI -s $ICCID -a $ADM
	check_card $I $CARD_NAME
	echo ""
    done
}

function usage {
    echo "Options:"
    echo "-n: number of card terminals"
    echo "-o: generate .ok files"
}

# Make sure that the pathes to the python scripts always work, regardless from
# where the script is called.
CURDIR=$PWD
SCRIPTDIR=$(dirname $0)
cd $SCRIPTDIR
PYSIM_PROG=$(realpath $PYSIM_PROG)
PYSIM_READ=$(realpath $PYSIM_READ)
cd $CURDIR

OPT_N_TERMINALS=0
OPT_GEN_OK_FILES=0
while getopts ":hon:" OPT; do
  case $OPT in
      h)
	  usage
	  exit 0
	  ;;
      o)
	  OPT_GEN_OK_FILES=1
	  ;;
      n)
	  OPT_N_TERMINALS=$OPTARG
	  ;;
      \?)
	  echo "Invalid option: -$OPTARG" >&2
	  exit 1
      ;;
  esac
done

N_TERMINALS=$OPT_N_TERMINALS

# Generate a list of available cards, if no explicit reader number is given
# then the number of cards will be used as reader number.
gen_card_list
if [ $N_TERMINALS -eq 0 ]; then
    N_TERMINALS=$N_CARDS
fi
echo "Number of card terminals installed: $N_TERMINALS"
echo ""

if [ $OPT_GEN_OK_FILES -eq 1 ]; then
    gen_ok_files
    exit 0
else
    run_test
    check_card_list
    exit 0
fi