blob: 4004c189eedd7fa5e5557c107cb1ae53b44dbe9f [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
Denis 'GNUtoo' Carikli79f5b602020-02-15 04:02:57 +070026PYTHON=python3
Philipp Maier7f340852018-07-10 16:21:54 +020027
28set -e
29
30echo "pysim-test - a test program to test pysim-prog.py"
31echo "================================================="
32
33# Generate a list of the cards we expect to see by checking which .ok files
34# are present
35function gen_card_list {
36 N_CARDS=0
37
38 echo "Expecting to see the following cards:"
39
40 for I in *.data ; do
41 CARD_NAMES[$N_CARDS]=${I%.*}
42 CARD_SEEN[$N_CARDS]=0
43 N_CARDS=$((N_CARDS+1))
44 done
45
46 for I in $(seq 0 $((N_CARDS-1))); do
47 echo ${CARD_NAMES[$I]}
48 done
49}
50
51# Increment counter in card list for a specified card name (type)
52function inc_card_list {
53 CARD_NAME=$1
54 for I in $(seq 0 $((N_CARDS-1))); do
55 if [ $CARD_NAME = ${CARD_NAMES[$I]} ]; then
56 CARD_SEEN[$I]=$((${CARD_NAMES[$I]}+1))
57 fi
58 done
59}
60
61# Check the card list, each card must be seen exactly one times
62function check_card_list {
63 for I in $(seq 0 $((N_CARDS-1))); do
64 if [ ${CARD_SEEN[$I]} -ne 1 ]; then
65 echo "Error: Card ${CARD_NAMES[$I]} seen ${CARD_SEEN[$I]} times!"
66 exit 1
67 fi
68 done
69
70 echo "All cards seen -- everything ok!"
71}
72
73# Verify the contents of a card by reading them and then diffing against the
74# previously created .ok file
75function check_card {
76 TERMINAL=$1
77 CARD_NAME=$2
78 echo "Verifying card ..."
79 stat ./$CARD_NAME.ok > /dev/null
Denis 'GNUtoo' Carikli79f5b602020-02-15 04:02:57 +070080 $PYTHON $PYSIM_READ -p $TERMINAL > $TEMPFILE
Philipp Maier7f340852018-07-10 16:21:54 +020081 set +e
Philipp Maierff84c232020-05-12 17:24:18 +020082 CARD_DIFF=$(diff $TEMPFILE ./$CARD_NAME.ok)
Philipp Maier7f340852018-07-10 16:21:54 +020083 set -e
84
85 if [ "$CARD_DIFF" != "" ]; then
86 echo "Card contents do not match the test data:"
87 echo "Expected: $CARD_NAME.ok"
88 echo "------------8<------------"
89 cat "$CARD_NAME.ok"
90 echo "------------8<------------"
91 echo "Got:"
92 echo "------------8<------------"
93 cat $TEMPFILE
94 echo "------------8<------------"
Philipp Maier07cd4812019-12-31 17:53:48 +010095 rm *.tmp
Philipp Maier7f340852018-07-10 16:21:54 +020096 exit 1
97 fi
98
99 inc_card_list $CARD_NAME
100
101 echo "Card contents match the test data -- success!"
Philipp Maierff84c232020-05-12 17:24:18 +0200102 rm $TEMPFILE
Philipp Maier7f340852018-07-10 16:21:54 +0200103}
104
105# Read out the card using pysim-read and store the result as .ok file. This
106# data will be used later in order to verify the results of our write tests.
107function gen_ok_file {
108 TERMINAL=$1
109 CARD_NAME=$2
Denis 'GNUtoo' Carikli79f5b602020-02-15 04:02:57 +0700110 $PYTHON $PYSIM_READ -p $TERMINAL > "$CARD_NAME.ok"
Philipp Maier7f340852018-07-10 16:21:54 +0200111 echo "Generated file: $CARD_NAME.ok"
112 echo "------------8<------------"
113 cat "$CARD_NAME.ok"
114 echo "------------8<------------"
115}
116
117# Find out the type (card name) of the card that is installed in the specified
118# reader
119function probe_card {
120 TERMINAL=$1
121 RESULT=$(timeout 5 $PYSIM_PROG -p $TERMINAL -T | cut -d ":" -f 2 | tail -n 1 | xargs)
122 echo $RESULT
123}
124
125# Read out all cards and store the results as .ok files
126function gen_ok_files {
127 echo "== OK FILE GENERATION =="
128 for I in $(seq 0 $((N_TERMINALS-1))); do
129 echo "Probing card in terminal #$I"
130 CARD_NAME=$(probe_card $I)
131 if [ -z "$CARD_NAME" ]; then
132 echo "Error: Unresponsive card!"
133 exit 1
134 fi
135 echo "Card is of type: $CARD_NAME"
136 gen_ok_file $I $CARD_NAME
137 done
138}
139
140# Execute tests. Each card is programmed and the contents are checked
141# afterwards.
142function run_test {
143 for I in $(seq 0 $((N_TERMINALS-1))); do
144 echo "== EXECUTING TEST =="
145 echo "Probing card in terminal #$I"
146 CARD_NAME=$(probe_card $I)
147 if [ -z "$CARD_NAME" ]; then
148 echo "Error: Unresponsive card!"
149 exit 1
150 fi
151 echo "Card is of type: $CARD_NAME"
152
153 # Make sure some default data is set
154 MCC=001
155 MNC=01
156 ICCID=1122334455667788990
157 KI=FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF
158 OPC=FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF
159 IMSI=001010000000001
Supreeth Herle5a541012019-12-22 08:59:16 +0100160 MSISDN=6766266
Philipp Maier7f340852018-07-10 16:21:54 +0200161 ADM=00000000
Philipp Maier4e724392019-12-12 17:02:22 +0100162 ADM_HEX=""
Daniel Willmannf432b2b2018-06-15 07:31:50 +0200163 ADM_OPT="-a"
Philipp Maier7f340852018-07-10 16:21:54 +0200164
Daniel Willmannf432b2b2018-06-15 07:31:50 +0200165 source "$CARD_NAME.data"
166 if [ -n "$ADM_HEX" ]; then
167 ADM_OPT="-A"
168 ADM=$ADM_HEX
169 fi
Denis 'GNUtoo' Carikli79f5b602020-02-15 04:02:57 +0700170 $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 +0200171 check_card $I $CARD_NAME
172 echo ""
173 done
174}
175
176function usage {
177 echo "Options:"
178 echo "-n: number of card terminals"
179 echo "-o: generate .ok files"
180}
181
182# Make sure that the pathes to the python scripts always work, regardless from
183# where the script is called.
184CURDIR=$PWD
185SCRIPTDIR=$(dirname $0)
186cd $SCRIPTDIR
187PYSIM_PROG=$(realpath $PYSIM_PROG)
188PYSIM_READ=$(realpath $PYSIM_READ)
189cd $CURDIR
190
191OPT_N_TERMINALS=0
192OPT_GEN_OK_FILES=0
193while getopts ":hon:" OPT; do
194 case $OPT in
195 h)
196 usage
197 exit 0
198 ;;
199 o)
200 OPT_GEN_OK_FILES=1
201 ;;
202 n)
203 OPT_N_TERMINALS=$OPTARG
204 ;;
205 \?)
206 echo "Invalid option: -$OPTARG" >&2
207 exit 1
208 ;;
209 esac
210done
211
212N_TERMINALS=$OPT_N_TERMINALS
213
214# Generate a list of available cards, if no explicit reader number is given
215# then the number of cards will be used as reader number.
216gen_card_list
217if [ $N_TERMINALS -eq 0 ]; then
218 N_TERMINALS=$N_CARDS
219fi
220echo "Number of card terminals installed: $N_TERMINALS"
221echo ""
222
223if [ $OPT_GEN_OK_FILES -eq 1 ]; then
224 gen_ok_files
225 exit 0
226else
227 run_test
228 check_card_list
229 exit 0
230fi