blob: a22c37264d0bb38235e5f9fa2fd96cda80a335b5 [file] [log] [blame]
Philipp Maier7f340852018-07-10 16:21:54 +02001#!/bin/bash
2
3# Utility to verify the functionality of pysim-prog.py
4#
5# (C) 2018 by Sysmocom s.f.m.c. GmbH
6# All Rights Reserved
7#
8# Author: Philipp Maier
9#
10# This program is free software: you can redistribute it and/or modify
11# it under the terms of the GNU General Public License as published by
12# the Free Software Foundation, either version 2 of the License, or
13# (at your option) any later version.
14#
15# This program is distributed in the hope that it will be useful,
16# but WITHOUT ANY WARRANTY; without even the implied warranty of
17# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18# GNU General Public License for more details.
19#
20# You should have received a copy of the GNU General Public License
21# along with this program. If not, see <http://www.gnu.org/licenses/>.
22
23PYSIM_PROG=../pySim-prog.py
24PYSIM_READ=../pySim-read.py
25TEMPFILE=temp.tmp
26
27set -e
28
29echo "pysim-test - a test program to test pysim-prog.py"
30echo "================================================="
31
32# Generate a list of the cards we expect to see by checking which .ok files
33# are present
34function gen_card_list {
35 N_CARDS=0
36
37 echo "Expecting to see the following cards:"
38
39 for I in *.data ; do
40 CARD_NAMES[$N_CARDS]=${I%.*}
41 CARD_SEEN[$N_CARDS]=0
42 N_CARDS=$((N_CARDS+1))
43 done
44
45 for I in $(seq 0 $((N_CARDS-1))); do
46 echo ${CARD_NAMES[$I]}
47 done
48}
49
50# Increment counter in card list for a specified card name (type)
51function inc_card_list {
52 CARD_NAME=$1
53 for I in $(seq 0 $((N_CARDS-1))); do
54 if [ $CARD_NAME = ${CARD_NAMES[$I]} ]; then
55 CARD_SEEN[$I]=$((${CARD_NAMES[$I]}+1))
56 fi
57 done
58}
59
60# Check the card list, each card must be seen exactly one times
61function check_card_list {
62 for I in $(seq 0 $((N_CARDS-1))); do
63 if [ ${CARD_SEEN[$I]} -ne 1 ]; then
64 echo "Error: Card ${CARD_NAMES[$I]} seen ${CARD_SEEN[$I]} times!"
65 exit 1
66 fi
67 done
68
69 echo "All cards seen -- everything ok!"
70}
71
72# Verify the contents of a card by reading them and then diffing against the
73# previously created .ok file
74function check_card {
75 TERMINAL=$1
76 CARD_NAME=$2
77 echo "Verifying card ..."
78 stat ./$CARD_NAME.ok > /dev/null
79 python $PYSIM_READ -p $TERMINAL > $TEMPFILE
80 set +e
Philipp Maier4e724392019-12-12 17:02:22 +010081 # Note: We ignore the first line of output in the diff because here
82 # pysim would print the device number of the reader and we do not
83 # want the test to fail just because the card is put into a different
84 # reader device.
Philipp Maier07cd4812019-12-31 17:53:48 +010085 tail -n +2 $CARD_NAME.ok > $CARD_NAME.ok.tmp
86 tail -n +2 $TEMPFILE > $CARD_NAME.chk.tmp
87 CARD_DIFF=$(diff $CARD_NAME.chk.tmp $CARD_NAME.ok.tmp)
Philipp Maier7f340852018-07-10 16:21:54 +020088 set -e
89
90 if [ "$CARD_DIFF" != "" ]; then
91 echo "Card contents do not match the test data:"
92 echo "Expected: $CARD_NAME.ok"
93 echo "------------8<------------"
94 cat "$CARD_NAME.ok"
95 echo "------------8<------------"
96 echo "Got:"
97 echo "------------8<------------"
98 cat $TEMPFILE
99 echo "------------8<------------"
Philipp Maier07cd4812019-12-31 17:53:48 +0100100 rm *.tmp
Philipp Maier7f340852018-07-10 16:21:54 +0200101 exit 1
102 fi
103
104 inc_card_list $CARD_NAME
105
106 echo "Card contents match the test data -- success!"
Philipp Maier07cd4812019-12-31 17:53:48 +0100107 rm *.tmp
Philipp Maier7f340852018-07-10 16:21:54 +0200108}
109
110# Read out the card using pysim-read and store the result as .ok file. This
111# data will be used later in order to verify the results of our write tests.
112function gen_ok_file {
113 TERMINAL=$1
114 CARD_NAME=$2
115 python $PYSIM_READ -p $TERMINAL > "$CARD_NAME.ok"
116 echo "Generated file: $CARD_NAME.ok"
117 echo "------------8<------------"
118 cat "$CARD_NAME.ok"
119 echo "------------8<------------"
120}
121
122# Find out the type (card name) of the card that is installed in the specified
123# reader
124function probe_card {
125 TERMINAL=$1
126 RESULT=$(timeout 5 $PYSIM_PROG -p $TERMINAL -T | cut -d ":" -f 2 | tail -n 1 | xargs)
127 echo $RESULT
128}
129
130# Read out all cards and store the results as .ok files
131function gen_ok_files {
132 echo "== OK FILE GENERATION =="
133 for I in $(seq 0 $((N_TERMINALS-1))); do
134 echo "Probing card in terminal #$I"
135 CARD_NAME=$(probe_card $I)
136 if [ -z "$CARD_NAME" ]; then
137 echo "Error: Unresponsive card!"
138 exit 1
139 fi
140 echo "Card is of type: $CARD_NAME"
141 gen_ok_file $I $CARD_NAME
142 done
143}
144
145# Execute tests. Each card is programmed and the contents are checked
146# afterwards.
147function run_test {
148 for I in $(seq 0 $((N_TERMINALS-1))); do
149 echo "== EXECUTING TEST =="
150 echo "Probing card in terminal #$I"
151 CARD_NAME=$(probe_card $I)
152 if [ -z "$CARD_NAME" ]; then
153 echo "Error: Unresponsive card!"
154 exit 1
155 fi
156 echo "Card is of type: $CARD_NAME"
157
158 # Make sure some default data is set
159 MCC=001
160 MNC=01
161 ICCID=1122334455667788990
162 KI=FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF
163 OPC=FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF
164 IMSI=001010000000001
Supreeth Herle5a541012019-12-22 08:59:16 +0100165 MSISDN=6766266
Philipp Maier7f340852018-07-10 16:21:54 +0200166 ADM=00000000
Philipp Maier4e724392019-12-12 17:02:22 +0100167 ADM_HEX=""
Daniel Willmannf432b2b2018-06-15 07:31:50 +0200168 ADM_OPT="-a"
Philipp Maier7f340852018-07-10 16:21:54 +0200169
Daniel Willmannf432b2b2018-06-15 07:31:50 +0200170 source "$CARD_NAME.data"
171 if [ -n "$ADM_HEX" ]; then
172 ADM_OPT="-A"
173 ADM=$ADM_HEX
174 fi
Supreeth Herle5a541012019-12-22 08:59:16 +0100175 python $PYSIM_PROG -p $I -t $CARD_NAME -o $OPC -k $KI -x $MCC -y $MNC -i $IMSI -s $ICCID --msisdn $MSISDN $ADM_OPT $ADM
Philipp Maier7f340852018-07-10 16:21:54 +0200176 check_card $I $CARD_NAME
177 echo ""
178 done
179}
180
181function usage {
182 echo "Options:"
183 echo "-n: number of card terminals"
184 echo "-o: generate .ok files"
185}
186
187# Make sure that the pathes to the python scripts always work, regardless from
188# where the script is called.
189CURDIR=$PWD
190SCRIPTDIR=$(dirname $0)
191cd $SCRIPTDIR
192PYSIM_PROG=$(realpath $PYSIM_PROG)
193PYSIM_READ=$(realpath $PYSIM_READ)
194cd $CURDIR
195
196OPT_N_TERMINALS=0
197OPT_GEN_OK_FILES=0
198while getopts ":hon:" OPT; do
199 case $OPT in
200 h)
201 usage
202 exit 0
203 ;;
204 o)
205 OPT_GEN_OK_FILES=1
206 ;;
207 n)
208 OPT_N_TERMINALS=$OPTARG
209 ;;
210 \?)
211 echo "Invalid option: -$OPTARG" >&2
212 exit 1
213 ;;
214 esac
215done
216
217N_TERMINALS=$OPT_N_TERMINALS
218
219# Generate a list of available cards, if no explicit reader number is given
220# then the number of cards will be used as reader number.
221gen_card_list
222if [ $N_TERMINALS -eq 0 ]; then
223 N_TERMINALS=$N_CARDS
224fi
225echo "Number of card terminals installed: $N_TERMINALS"
226echo ""
227
228if [ $OPT_GEN_OK_FILES -eq 1 ]; then
229 gen_ok_files
230 exit 0
231else
232 run_test
233 check_card_list
234 exit 0
235fi