1#!/bin/sh
2# Run FIPS CAVS tests
3# Copyright 2008 Free Software Foundation, Inc.
4#
5# This file is free software; as a special exception the author gives
6# unlimited permission to copy and/or distribute it, with or without
7# modifications, as long as this notice is preserved.
8#
9# This file is distributed in the hope that it will be useful, but
10# WITHOUT ANY WARRANTY, to the extent permitted by law; without even the
11# implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
12#
13# Instructions:
14#
15# 1. Cd to the libgcrypt/tests directory
16#
17# 2. Unpack the test vector tarball into subdirectory named "cavs".
18#    An example directory layout after unpacking might be:
19#      libgcrypt/tests/cavs/AES/req/CBCGFSbox128.req
20#      libgcrypt/tests/cavs/AES/req/CFB128MCT128.req
21#
22#    Note that below the "cavs" directory there should only be one
23#    directory part named "req".  Further avoid directory part
24#    names "resp".
25#
26# 3. Run this script from the libgcrypt/tests directory:
27#      ./cavs_tests.sh
28#
29# 4. Send the result file cavs/CAVS_results-*.zip to the testing lab.
30#
31
32# Stop script if something unexpected happens.
33set -e
34
35# A global flag to keep track of errors.
36errors_seen_file="$(pwd)/.#cavs_test.errors_seen.tmp"
37[ -f "$errors_seen_file" ] && rm "$errors_seen_file"
38continue_mode=no
39[ "$1" = "--continue" ] && continue_mode=yes
40
41
42# Function to run one test.
43# The argument is the request file name.
44function run_one_test () {
45    local reqfile="$1"
46    local rspfile
47    local tmprspfile
48    local respdir
49    local dflag=""
50
51    tmprspfile=$(echo "$reqfile" | sed 's,.req$,.rsp,')
52    rspfile=$(echo "$tmprspfile" | sed 's,/req/,/resp/,' )
53    respdir=$(dirname "$rspfile")
54    [ -f "$tmprspfile" ] && rm "$tmprspfile"
55    [ -d "$respdir" ] || mkdir "$respdir"
56    [ -f "$rspfile" ] &&  rm "$rspfile"
57
58    if echo "$reqfile" | grep '/DSA/req/' >/dev/null 2>/dev/null; then
59        dflag="-D"
60    fi
61
62    if ./cavs_driver.pl -I libgcrypt $dflag "$reqfile"; then
63      if [ -f "$tmprspfile" ]; then
64          mv "$tmprspfile" "$rspfile"
65      else
66          echo "failed test: $reqfile" >&2
67          : >"$errors_seen_file"
68      fi
69    else
70        echo "failed test: $reqfile rc=$?" >&2
71        : >"$errors_seen_file"
72    fi
73}
74
75
76
77# Save date and system architecure to construct the output archive name
78DATE=$(date +%Y%m%d)
79ARCH=$(arch || echo unknown)
80result_file="CAVS_results-$ARCH-$DATE.zip"
81
82for f in fipsdrv cavs_driver.pl; do
83    if [ ! -f "./$f" ]; then
84      echo "required program \"$f\" missing in current directory" >&2
85      exit 2
86    fi
87done
88if [ ! -d cavs ]; then
89    echo "required directory \"cavs\" missing below current directory" >&2
90    exit 2
91fi
92if [ ! zip -h >/dev/null 2>&1 ]; then
93    echo "required program \"zip\" is not installed on this system" >&2
94    exit 2
95fi
96
97# Set the PATH to this directory so that the perl script is able to
98# find the test drivers.
99PATH=.:$PATH
100
101# Check whether there are any stale response files
102find cavs -type f -name "*.rsp" | ( while read f ; do
103    echo "Stale response file: $f" >&2
104    any=yes
105done
106if [ "$any" = "yes" ]; then
107    echo "Stale response files found" >&2
108    if [ "$continue_mode" != "yes" ]; then
109       echo "use option --continue if that is not a problem" >&2
110       exit 1
111    fi
112fi
113) || exit 1
114
115
116# Find all test files and run the tests.
117find cavs -type f -name "*.req" | while read f ; do
118    echo "Running test file $f" >&2
119    run_one_test "$f"
120    if [ -f "$errors_seen_file" ]; then
121        break;
122    fi
123done
124
125if [ -f "$errors_seen_file" ]; then
126    rm "$errors_seen_file"
127    echo "Error encountered - not packing up response file" >&2
128    exit 1
129fi
130
131echo "Packing up all response files" >&2
132cd cavs
133find . -type f -name "*rsp" -print | zip -@ "$result_file"
134
135echo "Result file is: cavs/$result_file" >&2
136