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