1#! /bin/sh 2# 3# Copyright (c) International Business Machines Corp., 2005 4# 5# This program is free software; you can redistribute it and#or modify 6# it under the terms of the GNU General Public License as published by 7# the Free Software Foundation; either version 2 of the License, or 8# (at your option) any later version. 9# 10# This program is distributed in the hope that it will be useful, but 11# WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY 12# or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 13# for more details. 14# 15# You should have received a copy of the GNU General Public License 16# along with this program; if not, write to the Free Software 17# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 18# 19 20setup() 21{ 22 RC=0 # Return code from commands. 23 24 if [ -z "$LTPTMP" ] && [ -z "$TMPBASE" ] 25 then 26 LTPTMP="/tmp" 27 else 28 LTPTMP="$TMPBASE" 29 fi 30 31 export TPM_TMPFILE="$LTPTMP/tst_tpm.err" 32 rm -f $TPM_TMPFILE 2>&1 33 34 # Set the cert, key and combined cert and key filenames 35 export TPM_CERTFILE="$LTPTMP/tst_tpm.cert" 36 rm -f $TPM_CERTFILE 2>&1 37 export TPM_KEYFILE="$LTPTMP/tst_tpm.key" 38 rm -f $TPM_KEYFILE 2>&1 39 export TPM_COMBFILE="$LTPTMP/tst_tpm.comb" 40 rm -f $TPM_COMBFILE 2>&1 41 42 # Set the OpenSSL configuration file 43 export TPM_SSLCONF="$LTPBIN/tpmtoken_import_openssl.cnf" 44 45 # Set known password values 46 if [ -z "$P11_SO_PWD" ] 47 then 48 export P11_SO_PWD="P11 SO PWD" 49 fi 50 if [ -z "$P11_USER_PWD" ] 51 then 52 export P11_USER_PWD="P11 USER PWD" 53 fi 54 # This password needs to correspond to the passwords 55 # in the supplied OpenSSL configuration file 56 if [ -z "$SSL_PWD" ] 57 then 58 export SSL_PWD="SSL PWD" 59 fi 60 61 tst_resm TINFO "INIT: Inititalizing tests." 62 63 which tpmtoken_import 1>$TPM_TMPFILE 2>&1 || RC=$? 64 if [ $RC -ne 0 ] 65 then 66 tst_brk TBROK $TPM_TMPFILE NULL \ 67 "Test: tpmtoken_import command does not exist. Reason:" 68 return $RC 69 fi 70 71 which openssl 1>$TPM_TMPFILE 2>&1 || RC=$? 72 if [ $RC -ne 0 ] 73 then 74 tst_brk TBROK $TPM_TMPFILE NULL \ 75 "Setup: openssl command does not exist. Reason:" 76 return $RC 77 fi 78 openssl req -x509 -new -out $TPM_CERTFILE \ 79 -newkey rsa:2048 -keyout $TPM_KEYFILE \ 80 -sha1 -batch -config $TPM_SSLCONF 81 if [ $RC -ne 0 ] 82 then 83 tst_brk TBROK $TPM_TMPFILE NULL \ 84 "Setup: unable to create certificate and/or key file. Reason:" 85 return $RC 86 fi 87 cat $TPM_CERTFILE $TPM_KEYFILE 1>$TPM_COMBFILE 2>&1 || RC=$? 88 if [ $RC -ne 0 ] 89 then 90 tst_brk TBROK $TPM_COMBFILE NULL \ 91 "Setup: unable to create combined certficate and key file. Reason:" 92 return $RC 93 fi 94 95 return $RC 96} 97 98test01() 99{ 100 RC=0 # Return value from commands 101 export TCID=tpmtoken_import01 # Test ID 102 export TST_COUNT=1 # Test number 103 104 tpmtoken_import_tests_exp01.sh 1>$TPM_TMPFILE 2>&1 || RC=$? 105 if [ $RC -eq 0 ] 106 then 107 tst_resm TPASS "'tpmtoken_import -n $TCID $TPM_COMBFILE' passed." 108 RC=0 109 else 110 tst_res TFAIL $TPM_TMPFILE "'tpmtoken_import -n $TCID $TPM_COMBFILE' failed." 111 RC=1 112 fi 113 return $RC 114} 115 116test02() 117{ 118 RC=0 # Return value from commands 119 export TCID=tpmtoken_import02 # Test ID 120 export TST_COUNT=2 # Test number 121 122 tpmtoken_import_tests_exp02.sh 1>$TPM_TMPFILE 2>&1 || RC=$? 123 if [ $RC -eq 0 ] 124 then 125 tst_resm TPASS "'tpmtoken_import -n $TCID $TPM_COMBFILE' passed." 126 RC=0 127 else 128 tst_res TFAIL $TPM_TMPFILE "'tpmtoken_import -n $TCID $TPM_COMBFILE' failed." 129 RC=1 130 fi 131 return $RC 132} 133 134test03() 135{ 136 RC=0 # Return value from commands 137 export TCID=tpmtoken_import03 # Test ID 138 export TST_COUNT=3 # Test number 139 140 tpmtoken_import_tests_exp03.sh 1>$TPM_TMPFILE 2>&1 || RC=$? 141 if [ $RC -eq 0 ] 142 then 143 tst_resm TPASS "'tpmtoken_import -y -n $TCID $TPM_COMBFILE' passed." 144 RC=0 145 else 146 tst_res TFAIL $TPM_TMPFILE "'tpmtoken_import -y -n $TCID $TPM_COMBFILE' failed." 147 RC=1 148 fi 149 return $RC 150} 151 152test04() 153{ 154 RC=0 # Return value from commands 155 export TCID=tpmtoken_import04 # Test ID 156 export TST_COUNT=4 # Test number 157 158 tpmtoken_import_tests_exp04.sh 1>$TPM_TMPFILE 2>&1 || RC=$? 159 if [ $RC -eq 0 ] 160 then 161 tst_resm TPASS "'tpmtoken_import -p -n $TCID $TPM_COMBFILE' passed." 162 RC=0 163 else 164 tst_res TFAIL $TPM_TMPFILE "'tpmtoken_import -p -n $TCID $TPM_COMBFILE' failed." 165 RC=1 166 fi 167 return $RC 168} 169 170test05() 171{ 172 RC=0 # Return value from commands 173 export TCID=tpmtoken_import05 # Test ID 174 export TST_COUNT=5 # Test number 175 176 tpmtoken_import_tests_exp05.sh 1>$TPM_TMPFILE 2>&1 || RC=$? 177 if [ $RC -eq 0 ] 178 then 179 tst_resm TPASS "'tpmtoken_import -p -n $TCID $TPM_COMBFILE' passed." 180 RC=0 181 else 182 tst_res TFAIL $TPM_TMPFILE "'tpmtoken_import -p -n $TCID $TPM_COMBFILE' failed." 183 RC=1 184 fi 185 return $RC 186} 187 188test06() 189{ 190 RC=0 # Return value from commands 191 export TCID=tpmtoken_import06 # Test ID 192 export TST_COUNT=6 # Test number 193 194 tpmtoken_import_tests_exp06.sh 1>$TPM_TMPFILE 2>&1 || RC=$? 195 if [ $RC -eq 0 ] 196 then 197 tst_resm TPASS "'tpmtoken_import -y -p -n $TCID $TPM_COMBFILE' passed." 198 RC=0 199 else 200 tst_res TFAIL $TPM_TMPFILE "'tpmtoken_import -y -p -n $TCID $TPM_COMBFILE' failed." 201 RC=1 202 fi 203 return $RC 204} 205 206test07() 207{ 208 RC=0 # Return value from commands 209 export TCID=tpmtoken_import07 # Test ID 210 export TST_COUNT=7 # Test number 211 212 tpmtoken_import_tests_exp07.sh 1>$TPM_TMPFILE 2>&1 || RC=$? 213 if [ $RC -eq 0 ] 214 then 215 tst_resm TPASS "'tpmtoken_import -n $TCID $TPM_CERTFILE' passed." 216 RC=0 217 else 218 tst_res TFAIL $TPM_TMPFILE "'tpmtoken_import -n $TCID $TPM_CERTFILE' failed." 219 RC=1 220 fi 221 return $RC 222} 223 224test08() 225{ 226 RC=0 # Return value from commands 227 export TCID=tpmtoken_import08 # Test ID 228 export TST_COUNT=8 # Test number 229 230 tpmtoken_import_tests_exp08.sh 1>$TPM_TMPFILE 2>&1 || RC=$? 231 if [ $RC -eq 0 ] 232 then 233 tst_resm TPASS "'tpmtoken_import -n $TCID $TPM_KEYFILE' passed." 234 RC=0 235 else 236 tst_res TFAIL $TPM_TMPFILE "'tpmtoken_import -n $TCID $TPM_KEYFILE' failed." 237 RC=1 238 fi 239 return $RC 240} 241 242test09() 243{ 244 RC=0 # Return value from commands 245 export TCID=tpmtoken_import09 # Test ID 246 export TST_COUNT=9 # Test number 247 248 # The command should fail in this test case! 249 tpmtoken_import -t key -n $TCID $TPM_CERTFILE 1>$TPM_TMPFILE 2>&1 || RC=$? 250 if [ $RC -ne 0 ] 251 then 252 tst_resm TPASS "'tpmtoken_import -t key -n $TCID $TPM_CERTFILE' passed." 253 RC=0 254 else 255 tst_res TFAIL $TPM_TMPFILE "'tpmtoken_import -t key -n $TCID $TPM_CERTFILE' failed." 256 RC=1 257 fi 258 return $RC 259} 260 261test10() 262{ 263 RC=0 # Return value from commands 264 export TCID=tpmtoken_import10 # Test ID 265 export TST_COUNT=10 # Test number 266 267 # The command should fail in this test case! 268 tpmtoken_import -t cert -n $TCID $TPM_KEYFILE 1>$TPM_TMPFILE 2>&1 || RC=$? 269 if [ $RC -ne 0 ] 270 then 271 tst_resm TPASS "'tpmtoken_import -t cert -n $TCID $TPM_KEYFILE' passed." 272 RC=0 273 else 274 tst_res TFAIL $TPM_TMPFILE "'tpmtoken_import -t cert -n $TCID $TPM_KEYFILE' failed." 275 RC=1 276 fi 277 return $RC 278} 279 280cleanup() 281{ 282 rm -f $TPM_TMPFILE 2>&1 283 rm -f $TPM_CERTFILE 2>&1 284 rm -f $TPM_KEYFILE 2>&1 285 rm -f $TPM_COMBFILE 2>&1 286} 287 288# Function: main 289# 290# Description: - Execute all tests, report results. 291# 292# Exit: - zero on success 293# - non-zero on failure. 294 295TFAILCNT=0 # Set TFAILCNT to 0, increment on failure. 296RC=0 # Return code from tests. 297 298export TCID=tpmtoken_import # Test ID 299export TST_TOTAL=10 # Total numner of tests in this file. 300export TST_COUNT=0 # Initialize identifier 301 302if [ -n "$TPM_NOPKCS11" ] 303then 304 tst_resm TINFO "'tpmtoken_import' skipped." 305 exit $TFAILCNT 306fi 307 308setup || exit $RC # Exit if initializing testcases fails. 309 310test01 || TFAILCNT=$(($TFAILCNT+1)) 311test02 || TFAILCNT=$(($TFAILCNT+1)) 312test03 || TFAILCNT=$(($TFAILCNT+1)) 313test04 || TFAILCNT=$(($TFAILCNT+1)) 314test05 || TFAILCNT=$(($TFAILCNT+1)) 315test06 || TFAILCNT=$(($TFAILCNT+1)) 316test07 || TFAILCNT=$(($TFAILCNT+1)) 317test08 || TFAILCNT=$(($TFAILCNT+1)) 318test09 || TFAILCNT=$(($TFAILCNT+1)) 319test10 || TFAILCNT=$(($TFAILCNT+1)) 320 321cleanup 322 323exit $TFAILCNT 324