Philipp Maier | 7f34085 | 2018-07-10 16:21:54 +0200 | [diff] [blame] | 1 | #!/bin/bash |
| 2 | |
Philipp Maier | 63e8a18 | 2023-08-01 16:09:29 +0200 | [diff] [blame] | 3 | # Utility to verify the functionality of pySim-prog.py |
Philipp Maier | 7f34085 | 2018-07-10 16:21:54 +0200 | [diff] [blame] | 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 | |
| 23 | PYSIM_PROG=../pySim-prog.py |
| 24 | PYSIM_READ=../pySim-read.py |
| 25 | TEMPFILE=temp.tmp |
Denis 'GNUtoo' Carikli | 79f5b60 | 2020-02-15 04:02:57 +0700 | [diff] [blame] | 26 | PYTHON=python3 |
Philipp Maier | 7f34085 | 2018-07-10 16:21:54 +0200 | [diff] [blame] | 27 | |
Philipp Maier | 4af63dc | 2023-10-26 12:17:32 +0200 | [diff] [blame^] | 28 | export PYSIM_INTEGRATION_TEST=1 |
Philipp Maier | 7f34085 | 2018-07-10 16:21:54 +0200 | [diff] [blame] | 29 | set -e |
| 30 | |
Philipp Maier | 63e8a18 | 2023-08-01 16:09:29 +0200 | [diff] [blame] | 31 | echo "pySim-prog_test - a test program to test pySim-prog.py" |
Philipp Maier | 1cdcbe4 | 2023-07-27 14:55:30 +0200 | [diff] [blame] | 32 | echo "======================================================" |
Philipp Maier | 7f34085 | 2018-07-10 16:21:54 +0200 | [diff] [blame] | 33 | |
| 34 | # Generate a list of the cards we expect to see by checking which .ok files |
| 35 | # are present |
| 36 | function 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) |
| 53 | function 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 |
| 63 | function 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 |
| 76 | function check_card { |
| 77 | TERMINAL=$1 |
| 78 | CARD_NAME=$2 |
| 79 | echo "Verifying card ..." |
| 80 | stat ./$CARD_NAME.ok > /dev/null |
Denis 'GNUtoo' Carikli | 79f5b60 | 2020-02-15 04:02:57 +0700 | [diff] [blame] | 81 | $PYTHON $PYSIM_READ -p $TERMINAL > $TEMPFILE |
Philipp Maier | 7f34085 | 2018-07-10 16:21:54 +0200 | [diff] [blame] | 82 | set +e |
Philipp Maier | ff84c23 | 2020-05-12 17:24:18 +0200 | [diff] [blame] | 83 | CARD_DIFF=$(diff $TEMPFILE ./$CARD_NAME.ok) |
Philipp Maier | 7f34085 | 2018-07-10 16:21:54 +0200 | [diff] [blame] | 84 | 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 Maier | 07cd481 | 2019-12-31 17:53:48 +0100 | [diff] [blame] | 96 | rm *.tmp |
Philipp Maier | 7f34085 | 2018-07-10 16:21:54 +0200 | [diff] [blame] | 97 | exit 1 |
| 98 | fi |
| 99 | |
| 100 | inc_card_list $CARD_NAME |
| 101 | |
| 102 | echo "Card contents match the test data -- success!" |
Philipp Maier | ff84c23 | 2020-05-12 17:24:18 +0200 | [diff] [blame] | 103 | rm $TEMPFILE |
Philipp Maier | 7f34085 | 2018-07-10 16:21:54 +0200 | [diff] [blame] | 104 | } |
| 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. |
| 108 | function gen_ok_file { |
| 109 | TERMINAL=$1 |
| 110 | CARD_NAME=$2 |
Denis 'GNUtoo' Carikli | 79f5b60 | 2020-02-15 04:02:57 +0700 | [diff] [blame] | 111 | $PYTHON $PYSIM_READ -p $TERMINAL > "$CARD_NAME.ok" |
Philipp Maier | 7f34085 | 2018-07-10 16:21:54 +0200 | [diff] [blame] | 112 | 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 |
| 120 | function 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 |
| 127 | function 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. |
| 143 | function 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 Herle | 5a54101 | 2019-12-22 08:59:16 +0100 | [diff] [blame] | 161 | MSISDN=6766266 |
Philipp Maier | 7f34085 | 2018-07-10 16:21:54 +0200 | [diff] [blame] | 162 | ADM=00000000 |
Philipp Maier | 4e72439 | 2019-12-12 17:02:22 +0100 | [diff] [blame] | 163 | ADM_HEX="" |
Daniel Willmann | f432b2b | 2018-06-15 07:31:50 +0200 | [diff] [blame] | 164 | ADM_OPT="-a" |
Philipp Maier | 7f34085 | 2018-07-10 16:21:54 +0200 | [diff] [blame] | 165 | |
Daniel Willmann | f432b2b | 2018-06-15 07:31:50 +0200 | [diff] [blame] | 166 | source "$CARD_NAME.data" |
| 167 | if [ -n "$ADM_HEX" ]; then |
| 168 | ADM_OPT="-A" |
| 169 | ADM=$ADM_HEX |
| 170 | fi |
Denis 'GNUtoo' Carikli | 79f5b60 | 2020-02-15 04:02:57 +0700 | [diff] [blame] | 171 | $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 Maier | 7f34085 | 2018-07-10 16:21:54 +0200 | [diff] [blame] | 172 | check_card $I $CARD_NAME |
| 173 | echo "" |
| 174 | done |
| 175 | } |
| 176 | |
| 177 | function 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. |
| 185 | CURDIR=$PWD |
| 186 | SCRIPTDIR=$(dirname $0) |
| 187 | cd $SCRIPTDIR |
| 188 | PYSIM_PROG=$(realpath $PYSIM_PROG) |
| 189 | PYSIM_READ=$(realpath $PYSIM_READ) |
| 190 | cd $CURDIR |
| 191 | |
| 192 | OPT_N_TERMINALS=0 |
| 193 | OPT_GEN_OK_FILES=0 |
| 194 | while 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 |
| 211 | done |
| 212 | |
| 213 | N_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. |
| 217 | gen_card_list |
| 218 | if [ $N_TERMINALS -eq 0 ]; then |
| 219 | N_TERMINALS=$N_CARDS |
| 220 | fi |
| 221 | echo "Number of card terminals installed: $N_TERMINALS" |
| 222 | echo "" |
| 223 | |
| 224 | if [ $OPT_GEN_OK_FILES -eq 1 ]; then |
| 225 | gen_ok_files |
| 226 | exit 0 |
| 227 | else |
| 228 | run_test |
| 229 | check_card_list |
| 230 | exit 0 |
| 231 | fi |