#!/bin/sh

# Copyright (C) 2004, 2005, 2007, 2009, 2010 Free Software Foundation,
# Inc.
#
# Author: Simon Josefsson
#
# This file is part of GnuTLS.
#
# GnuTLS is free software; you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by the
# Free Software Foundation; either version 3 of the License, or (at
# your option) any later version.
#
# GnuTLS is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with GnuTLS; if not, write to the Free Software Foundation,
# Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.

CERTTOOL=../../src/certtool

SUCCESS=" 1 4 7 12 15 16 17 18 24 26 27 30 33 56 57 62 63 "
FAILURE=" 2 3 5 6 8 9 10 11 13 14 19 20 21 22 23 25 28 29 31 32 54 55 58 59 60 61 "

test -d X509tests || tar xfz x509tests.tgz

i=1
while test -d X509tests/test$i; do
    find X509tests/test$i -name *.crl -print0 |sort -r -z|xargs -n1 --null $CERTTOOL --crl-info --inder --infile > chain$i.pem  2>/dev/null
    find X509tests/test$i -name E*.crt -print0 |sort -r -z|xargs -n1 --null $CERTTOOL --certificate-info --inder --infile >> chain$i.pem 2>/dev/null
    if test "$i" -gt 1; then
	find X509tests/test$i -name I*.crt -print0 |sort -r -z|xargs -n1 --null $CERTTOOL --certificate-info --inder --infile >> chain$i.pem 2>/dev/null
    fi
    find X509tests/test$i -name T*.crt -print0 |sort -r -z|xargs -n1 --null $CERTTOOL --certificate-info --inder --infile >> chain$i.pem 2>/dev/null
    $CERTTOOL -e --infile chain$i.pem > out 2>&1
    rc=$?
    if test $rc != 0; then
	echo "Chain $i FATAL failure."
    else
	if echo "$SUCCESS" | grep " $i " > /dev/null 2>&1; then
	    if grep 'Chain verification output:' out | grep -v 'Chain verification output: Verified\.$' > /dev/null 2>&1; then
		echo "Chain $i verification failure UNEXPECTED."
	    else
		echo "Chain $i verification success as expected."
	    fi
	elif echo "$FAILURE" | grep " $i " >/dev/null 2>&1; then
	    if grep 'Chain verification output:' out | grep -v 'Chain verification output: Verified\.$' > /dev/null 2>&1; then
		echo "Chain $i verification failure as expected."
	    else
		echo "Chain $i verification success UNEXPECTED. "
	    fi
	else
	    echo "Chain $i unclassified."
	fi
    fi
    i=`expr $i + 1`
done
rm -f out
