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