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 |
| 26 | |
| 27 | set -e |
| 28 | |
| 29 | echo "pysim-test - a test program to test pysim-prog.py" |
| 30 | echo "=================================================" |
| 31 | |
| 32 | # Generate a list of the cards we expect to see by checking which .ok files |
| 33 | # are present |
| 34 | function 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) |
| 51 | function 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 |
| 61 | function 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 |
| 74 | function 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 |
| 81 | CARD_DIFF=$(diff $TEMPFILE ./$CARD_NAME.ok) |
| 82 | set -e |
| 83 | |
| 84 | if [ "$CARD_DIFF" != "" ]; then |
| 85 | echo "Card contents do not match the test data:" |
| 86 | echo "Expected: $CARD_NAME.ok" |
| 87 | echo "------------8<------------" |
| 88 | cat "$CARD_NAME.ok" |
| 89 | echo "------------8<------------" |
| 90 | echo "Got:" |
| 91 | echo "------------8<------------" |
| 92 | cat $TEMPFILE |
| 93 | echo "------------8<------------" |
| 94 | exit 1 |
| 95 | fi |
| 96 | |
| 97 | inc_card_list $CARD_NAME |
| 98 | |
| 99 | echo "Card contents match the test data -- success!" |
| 100 | rm $TEMPFILE |
| 101 | } |
| 102 | |
| 103 | # Read out the card using pysim-read and store the result as .ok file. This |
| 104 | # data will be used later in order to verify the results of our write tests. |
| 105 | function gen_ok_file { |
| 106 | TERMINAL=$1 |
| 107 | CARD_NAME=$2 |
| 108 | python $PYSIM_READ -p $TERMINAL > "$CARD_NAME.ok" |
| 109 | echo "Generated file: $CARD_NAME.ok" |
| 110 | echo "------------8<------------" |
| 111 | cat "$CARD_NAME.ok" |
| 112 | echo "------------8<------------" |
| 113 | } |
| 114 | |
| 115 | # Find out the type (card name) of the card that is installed in the specified |
| 116 | # reader |
| 117 | function probe_card { |
| 118 | TERMINAL=$1 |
| 119 | RESULT=$(timeout 5 $PYSIM_PROG -p $TERMINAL -T | cut -d ":" -f 2 | tail -n 1 | xargs) |
| 120 | echo $RESULT |
| 121 | } |
| 122 | |
| 123 | # Read out all cards and store the results as .ok files |
| 124 | function gen_ok_files { |
| 125 | echo "== OK FILE GENERATION ==" |
| 126 | for I in $(seq 0 $((N_TERMINALS-1))); do |
| 127 | echo "Probing card in terminal #$I" |
| 128 | CARD_NAME=$(probe_card $I) |
| 129 | if [ -z "$CARD_NAME" ]; then |
| 130 | echo "Error: Unresponsive card!" |
| 131 | exit 1 |
| 132 | fi |
| 133 | echo "Card is of type: $CARD_NAME" |
| 134 | gen_ok_file $I $CARD_NAME |
| 135 | done |
| 136 | } |
| 137 | |
| 138 | # Execute tests. Each card is programmed and the contents are checked |
| 139 | # afterwards. |
| 140 | function run_test { |
| 141 | for I in $(seq 0 $((N_TERMINALS-1))); do |
| 142 | echo "== EXECUTING TEST ==" |
| 143 | echo "Probing card in terminal #$I" |
| 144 | CARD_NAME=$(probe_card $I) |
| 145 | if [ -z "$CARD_NAME" ]; then |
| 146 | echo "Error: Unresponsive card!" |
| 147 | exit 1 |
| 148 | fi |
| 149 | echo "Card is of type: $CARD_NAME" |
| 150 | |
| 151 | # Make sure some default data is set |
| 152 | MCC=001 |
| 153 | MNC=01 |
| 154 | ICCID=1122334455667788990 |
| 155 | KI=FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF |
| 156 | OPC=FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF |
| 157 | IMSI=001010000000001 |
| 158 | ADM=00000000 |
| 159 | |
| 160 | . "$CARD_NAME.data" |
| 161 | python $PYSIM_PROG -p $I -t $CARD_NAME -o $OPC -k $KI -x $MCC -y $MNC -i $IMSI -s $ICCID -a $ADM |
| 162 | check_card $I $CARD_NAME |
| 163 | echo "" |
| 164 | done |
| 165 | } |
| 166 | |
| 167 | function usage { |
| 168 | echo "Options:" |
| 169 | echo "-n: number of card terminals" |
| 170 | echo "-o: generate .ok files" |
| 171 | } |
| 172 | |
| 173 | # Make sure that the pathes to the python scripts always work, regardless from |
| 174 | # where the script is called. |
| 175 | CURDIR=$PWD |
| 176 | SCRIPTDIR=$(dirname $0) |
| 177 | cd $SCRIPTDIR |
| 178 | PYSIM_PROG=$(realpath $PYSIM_PROG) |
| 179 | PYSIM_READ=$(realpath $PYSIM_READ) |
| 180 | cd $CURDIR |
| 181 | |
| 182 | OPT_N_TERMINALS=0 |
| 183 | OPT_GEN_OK_FILES=0 |
| 184 | while getopts ":hon:" OPT; do |
| 185 | case $OPT in |
| 186 | h) |
| 187 | usage |
| 188 | exit 0 |
| 189 | ;; |
| 190 | o) |
| 191 | OPT_GEN_OK_FILES=1 |
| 192 | ;; |
| 193 | n) |
| 194 | OPT_N_TERMINALS=$OPTARG |
| 195 | ;; |
| 196 | \?) |
| 197 | echo "Invalid option: -$OPTARG" >&2 |
| 198 | exit 1 |
| 199 | ;; |
| 200 | esac |
| 201 | done |
| 202 | |
| 203 | N_TERMINALS=$OPT_N_TERMINALS |
| 204 | |
| 205 | # Generate a list of available cards, if no explicit reader number is given |
| 206 | # then the number of cards will be used as reader number. |
| 207 | gen_card_list |
| 208 | if [ $N_TERMINALS -eq 0 ]; then |
| 209 | N_TERMINALS=$N_CARDS |
| 210 | fi |
| 211 | echo "Number of card terminals installed: $N_TERMINALS" |
| 212 | echo "" |
| 213 | |
| 214 | if [ $OPT_GEN_OK_FILES -eq 1 ]; then |
| 215 | gen_ok_files |
| 216 | exit 0 |
| 217 | else |
| 218 | run_test |
| 219 | check_card_list |
| 220 | exit 0 |
| 221 | fi |