Vboot Reference: Spring cleaning of test scripts.

Moved duplicated code to "common.sh". Make directory detection more robust.

Review URL: http://codereview.chromium.org/1101004
diff --git a/tests/gen_test_cases.sh b/tests/gen_test_cases.sh
index b15921d..bad7e33 100755
--- a/tests/gen_test_cases.sh
+++ b/tests/gen_test_cases.sh
@@ -6,49 +6,39 @@
 
 # Generate test cases for use for the RSA verify benchmark.
 
-KEY_DIR=testkeys
-TESTCASE_DIR=testcases
-UTIL_DIR=../utils/
-TEST_FILE=test_file 
+# Load common constants and variables.
+. "$(dirname "$0")/common.sh"
+
+TEST_FILE=${TESTCASE_DIR}/test_file
 TEST_FILE_SIZE=1000000
 
-hash_algos=( sha1 sha256 sha512 )
-key_lengths=( 1024 2048 4096 8192 ) 
-
-# Generate public key signatures and digest on an input file for 
-# various combinations of message digest algorithms and RSA key sizes.
+# Generate public key signatures on an input file for various combinations
+# of message digest algorithms and RSA key sizes.
 function generate_test_signatures {
+  echo "Generating test signatures..."
   algorithmcounter=0
   for keylen in ${key_lengths[@]}
   do
     for hashalgo in ${hash_algos[@]}
     do
-      openssl dgst -${hashalgo} -binary -out $1.${hashalgo}.digest $1
-      ${UTIL_DIR}/signature_digest $algorithmcounter $1 | openssl rsautl -sign \
-        -pkcs -inkey ${KEY_DIR}/key_rsa${keylen}.pem \
-        > $1.rsa${keylen}_${hashalgo}.sig
+      openssl dgst -${hashalgo} -binary ${TEST_FILE} > \
+        ${TEST_FILE}.${hashalgo}.digest
+      ${UTIL_DIR}/signature_digest_utility $algorithmcounter  \
+        ${TEST_FILE} | openssl rsautl \
+        -sign -pkcs -inkey ${TESTKEY_DIR}/key_rsa${keylen}.pem \
+        > ${TEST_FILE}.rsa${keylen}_${hashalgo}.sig
       let algorithmcounter=algorithmcounter+1
     done
   done
 }
 
-function pre_work {
-  # Generate a file with random bytes for signature tests.
+# Generate a file with random bytes for signature tests.
+function generate_test_file {
   echo "Generating test file..."
-  dd if=/dev/urandom of=${TESTCASE_DIR}/${TEST_FILE} bs=${TEST_FILE_SIZE} count=1
+  dd if=/dev/urandom of=${TEST_FILE} bs=${TEST_FILE_SIZE} count=1
 }
 
-if [ ! -d "$KEY_DIR" ]
-then
-  echo "You must run gen_test_cases.sh to generate test keys first."
-  exit 1
-fi
-
-if [ ! -d "$TESTCASE_DIR" ]
-then
-  mkdir  "$TESTCASE_DIR"
-fi
-
-pre_work
-echo "Generating test signatures..."
-generate_test_signatures ${TESTCASE_DIR}/$TEST_FILE
+mkdir -p ${TESTCASE_DIR}
+check_test_keys
+generate_test_file
+generate_test_signatures