diff --git a/tests/Makefile.am b/tests/Makefile.am index c4517a5..ef72198 100644 --- a/tests/Makefile.am +++ b/tests/Makefile.am @@ -1,74 +1,75 @@ # Makefile.am - for the KSBA regression tests # Copyright (C) 2001, 2003 g10 Code GmbH # # This file is part of KSBA. # # KSBA 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. # # KSBA 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 this program; if not, see . ## Process this file with automake to produce Makefile.in TESTS_ENVIRONMENT = test_certs = samples/cert_dfn_pca01.der samples/cert_dfn_pca15.der \ samples/cert_g10code_test1.der samples/crl_testpki_testpca.der \ samples/authority.crt samples/betsy.crt samples/bull.crt \ samples/ov-ocsp-server.crt samples/ov-userrev.crt \ samples/ov-root-ca-cert.crt samples/ov-serverrev.crt \ samples/ov-user.crt samples/ov-server.crt \ samples/ov2-root-ca-cert.crt samples/ov2-ocsp-server.crt \ samples/ov2-user.crt samples/ov2-userrev.crt \ samples/secp256r1-sha384_cert.crt \ samples/secp256r1-sha512_cert.crt \ samples/secp384r1-sha512_cert.crt \ samples/openssl-secp256r1ca.cert.crt \ samples/ed25519-rfc8410.crt \ samples/ed25519-ossl-1.crt \ samples/ed448-ossl-1.crt test_crls = samples/ov-test-crl.crl test_keys = samples/ov-server.p12 samples/ov-userrev.p12 \ samples/ov-serverrev.p12 samples/ov-user.p12 -EXTRA_DIST = $(test_certs) samples/README mkoidtbl.awk detached-sig.cms \ +EXTRA_DIST = $(test_certs) samples/README mkoidtbl.awk \ + samples/detached-sig.cms \ samples/rsa-sample1.p7m samples/rsa-sample1.p7m.asn \ samples/ecdh-sample1.p7m samples/ecdh-sample1.p7m.asn BUILT_SOURCES = oidtranstbl.h CLEANFILES = oidtranstbl.h TESTS = cert-basic t-crl-parser t-dnparser t-oid t-reader t-cms-parser AM_CFLAGS = $(GPG_ERROR_CFLAGS) $(COVERAGE_CFLAGS) AM_LDFLAGS = -no-install $(COVERAGE_LDFLAGS) noinst_HEADERS = t-common.h noinst_PROGRAMS = $(TESTS) t-ocsp LDADD = ../src/libksba.la $(GPG_ERROR_LIBS) @LDADD_FOR_TESTS_KLUDGE@ t_ocsp_SOURCES = t-ocsp.c sha1.c # Build the OID table: Note that the binary includes data from an # another program and we may not be allowed to distribute this. This # ain't no problem as the programs using this generated data are not # installed and thus not distributed. oidtranstbl.h: Makefile mkoidtbl.awk set -e; f="/dev/null"; \ for i in /etc/dumpasn1 /usr/local/bin /usr/local/share /usr/bin \ /usr/share ; do \ if test -f $$i/dumpasn1.cfg; then f=$$i/dumpasn1.cfg; break; fi; \ done; tr -d '\r' <$$f | $(AWK) -f $(srcdir)/mkoidtbl.awk >$@ LOG_COMPILER = $(VALGRIND) diff --git a/tests/samples/README b/tests/samples/README index 56c4590..db2a1d3 100644 --- a/tests/samples/README +++ b/tests/samples/README @@ -1,89 +1,90 @@ -Certificates and a CRL used for the initial development: +Sample data used for the initial development: cert_dfn_pca01.der cert_dfn_pca15.der cert_g10code_test1.der crl_testpki_testpca.der + detached_sig.cms Certificates downloaded from http://www.magmacom.com/~mbartel/iso/\ certificates/samples/sample_certificates.html on 2003-11-20: authority.crt A root certificate betsy.crt An "everyday" certificate. bull.crt Same but includes a BMPString. Note, that these certs use MD2. Certificates downloaded from http://www.openvalidation.org/download/downloadrootcertsCA1.htm and on 2003-11-20 and prefixed with "ov-": ov-root-ca-cert.crt Root certificate ov-ocsp-server.crt The certificate of the OCSP responder ov-user.crt User certificate ov-userrev.crt A user certificate revoked by OCSP ov-server.crt A server (SSL) certificate ov-serverrev.crt A server certificate revoked by OCSP ov-user.p12 Private keys for the above certificates, ov-userrev.p12 passphrase is "start". ov-server.p12 ov-serverrev.p12 ov-test-crl.crl The current CRL The responder adress is http://ocsp.openvalidation.org Certificates downloaded from http://www.openvalidation.org/en/test/ca2.html on 2006-08-30 and prefixed with "ov2-": ov2-root-ca-cert.crt Root certificate ov2-ocsp-server.crt The certificate of the OCSP responder ov2-user.crt User certificate ov2-userrev.crt A user certificate revoked by OCSP Certificates downloaded on 2007-04-05 from http://dev.experimentalstuff.com:8082/CIC_sample-certs_2006-06-22.zip and converted to binary format. These are signed with ECDSA-P256-SHA384, ECDSA-P256-SHA512, and ECDSA-P384-SHA512. secp256r1-sha384_cert.crt secp256r1-sha512_cert.crt secp384r1-sha512_cert.crt From http://dev.experimentalstuff.com:8082/certs/secp256r1ca.cert.pem openssl-secp256r1ca.cert.crt ECDH sample enveloped data from the Mozilla bug tracker: ecdh-sample1.p7m ecdh-sample1.p7m.asn Commented dump. RSA sample enveloped data created with gpgsm rsa-sample1.p7m rsa-sample1.p7m.asn Commented dump RSA sample signature created with gpgsm rsa-sample1.p7s rsa-sample1.p7s.asn Commented dump ECDSA sample signature created with Governikus Signer ecdsa-sample1.p7s signed data is hitchhiker.txt ecdsa-sample1.p7s.asn Commented dump ED25519 sample self-signed certificates ed25519-rfc8410.crt from RFC8410 ed25519-rfc8410.key ed25519-ossl-1.crt generated with OpenSSL ed25519-ossl-1.key ED448 sample self-signed certificate ed448-ossl-1.crt generated with OpenSSL ed448-ossl-1.key generated with OpenSSL diff --git a/tests/detached-sig.cms b/tests/samples/detached-sig.cms similarity index 100% rename from tests/detached-sig.cms rename to tests/samples/detached-sig.cms diff --git a/tests/t-cms-parser.c b/tests/t-cms-parser.c index f0737a5..3fe64fe 100644 --- a/tests/t-cms-parser.c +++ b/tests/t-cms-parser.c @@ -1,250 +1,307 @@ /* t-cms-parser.c - basic test for the CMS parser. * Copyright (C) 2001 g10 Code GmbH * * This file is part of KSBA. * * KSBA 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. * * KSBA 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 this program; if not, see . */ #include #include #include #include #include #include #include "../src/ksba.h" #include "t-common.h" + +static int quiet; +static int verbose; + + void dummy_hash_fnc (void *arg, const void *buffer, size_t length) { (void)arg; (void)buffer; (void)length; } static int dummy_writer_cb (void *cb_value, const void *buffer, size_t count) { (void)cb_value; (void)buffer; (void)count; return 0; } static void one_file (const char *fname) { gpg_error_t err; FILE *fp; ksba_reader_t r; ksba_writer_t w; ksba_cms_t cms; int i; const char *algoid; ksba_stop_reason_t stopreason; const char *s; size_t n; ksba_sexp_t p; char *dn; int idx; - printf ("*** checking `%s' ***\n", fname); + if (!quiet) + printf ("*** checking `%s' ***\n", fname); fp = fopen (fname, "r"); if (!fp) { fprintf (stderr, "%s:%d: can't open `%s': %s\n", __FILE__, __LINE__, fname, strerror (errno)); exit (1); } err = ksba_reader_new (&r); if (err) fail_if_err (err); err = ksba_reader_set_file (r, fp); fail_if_err (err); /* Also create a writer so that cms.c won't return an error when writing processed content. */ err = ksba_writer_new (&w); if (err) fail_if_err (err); err = ksba_writer_set_cb (w, dummy_writer_cb, NULL); fail_if_err (err); switch (ksba_cms_identify (r)) { case KSBA_CT_DATA: s = "data"; break; case KSBA_CT_SIGNED_DATA: s = "signed data"; break; case KSBA_CT_ENVELOPED_DATA: s = "enveloped data"; break; case KSBA_CT_DIGESTED_DATA: s = "digested data"; break; case KSBA_CT_ENCRYPTED_DATA: s = "encrypted data"; break; case KSBA_CT_AUTH_DATA: s = "auth data"; break; case KSBA_CT_SPC_IND_DATA_CTX:s = "spc indirect data context"; break; default: s = "unknown"; break; } - printf ("identified as: %s\n", s); + if (!quiet) + printf ("identified as: %s\n", s); err = ksba_cms_new (&cms); if (err) fail_if_err (err); err = ksba_cms_set_reader_writer (cms, r, w); fail_if_err (err); err = ksba_cms_parse (cms, &stopreason); fail_if_err2 (fname, err); - printf ("stop reason: %d\n", stopreason); + if (!quiet) + printf ("stop reason: %d\n", stopreason); s = ksba_cms_get_content_oid (cms, 0); - printf ("ContentType: %s\n", s?s:"none"); + if (!quiet) + printf ("ContentType: %s\n", s?s:"none"); err = ksba_cms_parse (cms, &stopreason); fail_if_err2 (fname, err); - printf ("stop reason: %d\n", stopreason); + if (!quiet) + printf ("stop reason: %d\n", stopreason); s = ksba_cms_get_content_oid (cms, 1); - printf ("EncapsulatedContentType: %s\n", s?s:"none"); - printf ("DigestAlgorithms:"); + if (!quiet) + { + printf ("EncapsulatedContentType: %s\n", s?s:"none"); + printf ("DigestAlgorithms:"); + } for (i=0; (algoid = ksba_cms_get_digest_algo_list (cms, i)); i++) - printf (" %s", algoid); - putchar('\n'); + if (!quiet) + printf (" %s", algoid); + if (!quiet) + putchar('\n'); if (stopreason == KSBA_SR_NEED_HASH) - printf("Detached signature\n"); + if (!quiet) + printf("Detached signature\n"); ksba_cms_set_hash_function (cms, dummy_hash_fnc, NULL); do { err = ksba_cms_parse (cms, &stopreason); fail_if_err2 (fname, err); - printf ("stop reason: %d\n", stopreason); + if (!quiet) + printf ("stop reason: %d\n", stopreason); } while (stopreason != KSBA_SR_READY); if (ksba_cms_get_content_type (cms, 0) == KSBA_CT_ENVELOPED_DATA) { for (idx=0; ; idx++) { err = ksba_cms_get_issuer_serial (cms, idx, &dn, &p); if (err == -1) break; /* ready */ fail_if_err2 (fname, err); - printf ("recipient %d - issuer: ", idx); - print_dn (dn); + if (!quiet) + { + printf ("recipient %d - issuer: ", idx); + print_dn (dn); + } ksba_free (dn); - putchar ('\n'); - printf ("recipient %d - serial: ", idx); - print_sexp_hex (p); + if (!quiet) + { + putchar ('\n'); + printf ("recipient %d - serial: ", idx); + print_sexp_hex (p); + putchar ('\n'); + } ksba_free (p); - putchar ('\n'); dn = ksba_cms_get_enc_val (cms, idx); - printf ("recipient %d - enc_val: ", idx); - print_sexp (dn); - putchar ('\n'); + if (!quiet) + { + printf ("recipient %d - enc_val: ", idx); + print_sexp (dn); + putchar ('\n'); + } ksba_free (dn); } } else { for (idx=0; idx < 1; idx++) { err = ksba_cms_get_issuer_serial (cms, idx, &dn, &p); if (gpg_err_code (err) == GPG_ERR_NO_DATA && !idx) { - printf ("this is a certs-only message\n"); + if (!quiet) + printf ("this is a certs-only message\n"); break; } fail_if_err2 (fname, err); - printf ("signer %d - issuer: ", idx); - print_dn (dn); + if (!quiet) + { + printf ("signer %d - issuer: ", idx); + print_dn (dn); + putchar ('\n'); + } ksba_free (dn); - putchar ('\n'); - printf ("signer %d - serial: ", idx); - print_sexp_hex (p); + + if (!quiet) + { + printf ("signer %d - serial: ", idx); + print_sexp_hex (p); + putchar ('\n'); + } ksba_free (p); - putchar ('\n'); err = ksba_cms_get_message_digest (cms, idx, &dn, &n); fail_if_err2 (fname, err); - printf ("signer %d - messageDigest: ", idx); - print_hex (dn, n); + if (!quiet) + { + printf ("signer %d - messageDigest: ", idx); + print_hex (dn, n); + putchar ('\n'); + } ksba_free (dn); - putchar ('\n'); err = ksba_cms_get_sigattr_oids (cms, idx, "1.2.840.113549.1.9.3",&dn); if (err && err != -1) fail_if_err2 (fname, err); if (err != -1) { char *tmp; for (tmp=dn; *tmp; tmp++) if (*tmp == '\n') *tmp = ' '; - printf ("signer %d - content-type: %s\n", idx, dn); + if (!quiet) + printf ("signer %d - content-type: %s\n", idx, dn); ksba_free (dn); } algoid = ksba_cms_get_digest_algo (cms, idx); - printf ("signer %d - digest algo: %s\n", idx, algoid?algoid:"?"); + if (!quiet) + printf ("signer %d - digest algo: %s\n", idx, algoid?algoid:"?"); dn = ksba_cms_get_sig_val (cms, idx); if (dn) { - printf ("signer %d - signature: ", idx); - print_sexp (dn); - putchar ('\n'); + if (!quiet) + { + printf ("signer %d - signature: ", idx); + print_sexp (dn); + putchar ('\n'); + } } else - printf ("signer %d - signature not found\n", idx); + { + if (!quiet) + printf ("signer %d - signature not found\n", idx); + } ksba_free (dn); } } ksba_cms_release (cms); ksba_writer_release (w); ksba_reader_release (r); fclose (fp); } int main (int argc, char **argv) { - if (argc > 1) - one_file (argv[1]); + if (argc) + { + argc--; argv++; + } + if (argc && !strcmp (*argv, "--verbose")) + { + verbose = 1; + argc--; argv++; + } + + if (argc) + one_file (argv[0]); else { - char *fname = prepend_srcdir ("detached-sig.cms"); + char *fname = prepend_srcdir ("samples/detached-sig.cms"); + if (!verbose) + quiet = 1; one_file (fname); free(fname); } /*one_file ("pkcs7-1.ber");*/ /*one_file ("root-cert-2.der"); should fail */ return 0; } diff --git a/tests/t-crl-parser.c b/tests/t-crl-parser.c index a923b10..5e23b55 100644 --- a/tests/t-crl-parser.c +++ b/tests/t-crl-parser.c @@ -1,350 +1,408 @@ /* t-crl-parser.c - basic test for the CRl parser. * Copyright (C) 2002, 2004, 2005 g10 Code GmbH * * This file is part of KSBA. * * KSBA 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. * * KSBA 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 this program; if not, see . */ #undef ENABLE_HASH_LOGGING #ifdef ENABLE_HASH_LOGGING #define _GNU_SOURCE 1 #endif #include #include #include #include #include #include #include "../src/ksba.h" #include "t-common.h" #include "oidtranstbl.h" + +static int quiet; +static int verbose; + + static void my_hasher (void *arg, const void *buffer, size_t length) { FILE *fp = arg; if (fp) { if ( fwrite (buffer, length, 1, fp) != 1 ) fail ("error writing to-be-hashed data"); } } /* Return the description for OID; if no description is available NULL is returned. */ static const char * get_oid_desc (const char *oid) { int i; if (oid) for (i=0; oidtranstbl[i].oid; i++) if (!strcmp (oidtranstbl[i].oid, oid)) return oidtranstbl[i].desc; return NULL; } static void print_names (int indent, ksba_name_t name) { int idx; const char *s; int indent_all; if ((indent_all = (indent < 0))) indent = - indent; if (!name) { - fputs ("none\n", stdout); + if (!quiet) + fputs ("none\n", stdout); return; } for (idx=0; (s = ksba_name_enum (name, idx)); idx++) { char *p = ksba_name_get_uri (name, idx); - printf ("%*s%s\n", idx||indent_all?indent:0, "", p?p:s); + if (!quiet) + printf ("%*s%s\n", idx||indent_all?indent:0, "", p?p:s); xfree (p); } } static void one_file (const char *fname) { gpg_error_t err; FILE *fp; ksba_reader_t r; ksba_crl_t crl; ksba_stop_reason_t stopreason; int count = 0; FILE *hashlog = NULL; #ifdef ENABLE_HASH_LOGGING + if (1) { char *buf; if (asprintf (&buf, "%s.hash.log", fname) < 0) fail ("asprintf failed"); hashlog = fopen (buf, "wb"); if (!hashlog) fail ("can't create log file"); free (buf); } #endif - printf ("*** checking `%s' ***\n", fname); + if (!quiet) + printf ("*** checking `%s' ***\n", fname); fp = fopen (fname, "rb"); if (!fp) { fprintf (stderr, "%s:%d: can't open `%s': %s\n", __FILE__, __LINE__, fname, strerror (errno)); exit (1); } err = ksba_reader_new (&r); if (err) fail_if_err (err); err = ksba_reader_set_file (r, fp); fail_if_err (err); err = ksba_crl_new (&crl); if (err) fail_if_err (err); err = ksba_crl_set_reader (crl, r); fail_if_err (err); if (hashlog) ksba_crl_set_hash_function (crl, my_hasher, hashlog); do { err = ksba_crl_parse (crl, &stopreason); fail_if_err2 (fname, err); switch (stopreason) { case KSBA_SR_BEGIN_ITEMS: { const char *algoid; char *issuer; ksba_isotime_t this, next; algoid = ksba_crl_get_digest_algo (crl); - printf ("digest algo: %s\n", algoid? algoid : "[none]"); + if (!quiet) + printf ("digest algo: %s\n", algoid? algoid : "[none]"); if (algoid && !strcmp (algoid, "1.2.840.113549.1.1.10")) { ksba_sexp_t pssparam; - printf (" pss para: "); + if (!quiet) + printf (" pss para: "); pssparam = ksba_crl_get_sig_val (crl); - print_sexp (pssparam); - putchar ('\n'); + if (!quiet) + { + print_sexp (pssparam); + putchar ('\n'); + } xfree (pssparam); } err = ksba_crl_get_issuer (crl, &issuer); fail_if_err2 (fname, err); - printf ("issuer: "); - print_dn (issuer); + if (!quiet) + { + printf ("issuer: "); + print_dn (issuer); + putchar ('\n'); + } xfree (issuer); - putchar ('\n'); + err = ksba_crl_get_update_times (crl, this, next); if (gpg_err_code (err) != GPG_ERR_INV_TIME) fail_if_err2 (fname, err); - printf ("thisUpdate: "); - print_time (this); - putchar ('\n'); - printf ("nextUpdate: "); - print_time (next); - putchar ('\n'); + if (!quiet) + { + printf ("thisUpdate: "); + print_time (this); + putchar ('\n'); + printf ("nextUpdate: "); + print_time (next); + putchar ('\n'); + } } break; case KSBA_SR_GOT_ITEM: { ksba_sexp_t serial; ksba_isotime_t rdate; ksba_crl_reason_t reason; err = ksba_crl_get_item (crl, &serial, rdate, &reason); fail_if_err2 (fname, err); - printf ("CRL entry %d: s=", ++count); - print_sexp_hex (serial); - printf (", t="); - print_time (rdate); - printf (", r=%x\n", reason); + if (!quiet) + { + printf ("CRL entry %d: s=", ++count); + print_sexp_hex (serial); + printf (", t="); + print_time (rdate); + printf (", r=%x\n", reason); + } xfree (serial); } break; case KSBA_SR_END_ITEMS: break; case KSBA_SR_READY: break; default: fail ("unknown stop reason"); } } while (stopreason != KSBA_SR_READY); if ( !ksba_crl_get_digest_algo (crl)) fail ("digest algorithm mismatch"); { ksba_name_t name1; ksba_sexp_t serial; ksba_sexp_t keyid; err = ksba_crl_get_auth_key_id (crl, &keyid, &name1, &serial); if (!err || gpg_err_code (err) == GPG_ERR_NO_DATA) { - fputs ("AuthorityKeyIdentifier: ", stdout); + if (!quiet) + fputs ("AuthorityKeyIdentifier: ", stdout); if (gpg_err_code (err) == GPG_ERR_NO_DATA) - fputs ("none\n", stdout); + { + if (!quiet) + fputs ("none\n", stdout); + } else { if (name1) { print_names (24, name1); ksba_name_release (name1); - fputs (" serial: ", stdout); - print_sexp_hex (serial); + if (!quiet) + { + fputs (" serial: ", stdout); + print_sexp_hex (serial); + } ksba_free (serial); } - putchar ('\n'); + if (!quiet) + putchar ('\n'); if (keyid) { - fputs (" keyIdentifier: ", stdout); - print_sexp (keyid); + if (!quiet) + { + fputs (" keyIdentifier: ", stdout); + print_sexp (keyid); + putchar ('\n'); + } ksba_free (keyid); - putchar ('\n'); } } } else fail_if_err (err); } { ksba_sexp_t serial; err = ksba_crl_get_crl_number (crl, &serial); if (!err || gpg_err_code (err) == GPG_ERR_NO_DATA) { - fputs ("crlNumber: ", stdout); + if (!quiet) + fputs ("crlNumber: ", stdout); if (gpg_err_code (err) == GPG_ERR_NO_DATA) - fputs ("none", stdout); + { + if (!quiet) + fputs ("none", stdout); + } else { - print_sexp (serial); + if (!quiet) + print_sexp (serial); ksba_free (serial); } - putchar ('\n'); + if (!quiet) + putchar ('\n'); } else fail_if_err (err); } { int idx, crit; const char *oid; size_t derlen; for (idx=0; !(err=ksba_crl_get_extension (crl, idx, &oid, &crit, NULL, &derlen)); idx++) { const char *s = get_oid_desc (oid); - printf ("%sExtn: %s%s%s%s (%lu octets)\n", - crit? "Crit":"", - s?" (":"", s?s:"", s?")":"", - oid, (unsigned long)derlen); + if (!quiet) + printf ("%sExtn: %s%s%s%s (%lu octets)\n", + crit? "Crit":"", + s?" (":"", s?s:"", s?")":"", + oid, (unsigned long)derlen); } if (err && gpg_err_code (err) != GPG_ERR_EOF && gpg_err_code (err) != GPG_ERR_NO_DATA ) fail_if_err (err); } { ksba_sexp_t sigval; sigval = ksba_crl_get_sig_val (crl); if (!sigval) fail ("signature value missing"); - print_sexp (sigval); - putchar ('\n'); + if (!quiet) + { + print_sexp (sigval); + putchar ('\n'); + } xfree (sigval); } ksba_crl_release (crl); ksba_reader_release (r); fclose (fp); if (hashlog) fclose (hashlog); } int main (int argc, char **argv) { const char *srcdir = getenv ("srcdir"); if (!srcdir) srcdir = "."; - if (argc > 1) + if (argc) + { + argc--; argv++; + } + if (argc && !strcmp (*argv, "--verbose")) { - for (argc--, argv++; argc; argc--, argv++) + verbose = 1; + argc--; argv++; + } + + if (argc) + { + for (; argc; argc--, argv++) one_file (*argv); } else { const char *files[] = { "crl_testpki_testpca.der", NULL }; int idx; + if (!verbose) + quiet = 1; + for (idx=0; files[idx]; idx++) { char *fname; fname = xmalloc (strlen (srcdir) + 10 + strlen (files[idx]) + 1); strcpy (fname, srcdir); strcat (fname, "/samples/"); strcat (fname, files[idx]); one_file (fname); xfree (fname); } } return 0; } diff --git a/tests/t-dnparser.c b/tests/t-dnparser.c index 3a099a2..ef97456 100644 --- a/tests/t-dnparser.c +++ b/tests/t-dnparser.c @@ -1,175 +1,192 @@ /* t-dnparser.c - basic test for the DN parser * Copyright (C) 2002, 2006 g10 Code GmbH * * This file is part of KSBA. * * KSBA 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. * * KSBA 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 this program; if not, see . */ #include #include #include #include #include #include #include "../src/ksba.h" #include "t-common.h" +static int quiet; +static int verbose; + static void test_0 (void) { static char *good_strings[] = { "C=de,O=g10 Code,OU=qa,CN=Pépé le Moko", "C= de, O=g10 Code , OU=qa ,CN=Pépé le Moko", "CN=www.gnupg.org", " CN=www.gnupg.org ", "C=fr,L=Paris,CN=Julien Duvivier,EMAIL=julien@example.org", NULL }; gpg_error_t err; int i; unsigned char *buf; size_t off, len; for (i=0; good_strings[i]; i++) { err = ksba_dn_str2der (good_strings[i], &buf, &len); if (err) { fprintf (stderr, "%s:%d: ksba_dn_str2der failed for `%s': %s\n", __FILE__,__LINE__, good_strings[i], gpg_strerror (err)); exit (1); } err = ksba_dn_teststr (good_strings[i], 0, &off, &len); if (err) { fprintf (stderr, "%s:%d: ksba_dn_teststr failed for `%s': %s\n", __FILE__,__LINE__, good_strings[i], gpg_strerror (err)); exit (1); } xfree (buf); } } static void test_1 (void) { static char *empty_elements[] = { "C=de,O=foo,OU=,CN=joe", "C=de,O=foo,OU= ,CN=joe", "C=de,O=foo,OU=\"\" ,CN=joe", "C=de,O=foo,OU=", "C=de,O=foo,OU= ", "C=,O=foo,OU=bar ", "C = ,O=foo,OU=bar ", "C=", NULL }; gpg_error_t err; int i; unsigned char *buf; size_t off, len; for (i=0; empty_elements[i]; i++) { err = ksba_dn_str2der (empty_elements[i], &buf, &len); if (gpg_err_code (err) != GPG_ERR_SYNTAX) fail ("empty element not detected"); err = ksba_dn_teststr (empty_elements[i], 0, &off, &len); if (!err) fail ("ksba_dn_teststr returned no error"); - printf ("string ->%s<- error at %lu.%lu (%.*s)\n", - empty_elements[i], (unsigned long)off, (unsigned long)len, - (int)len, empty_elements[i]+off); + if (!quiet) + printf ("string ->%s<- error at %lu.%lu (%.*s)\n", + empty_elements[i], (unsigned long)off, (unsigned long)len, + (int)len, empty_elements[i]+off); xfree (buf); } } static void test_2 (void) { static char *invalid_labels[] = { "C=de,FOO=something,O=bar", "Y=foo, C=baz", NULL }; gpg_error_t err; int i; unsigned char *buf; size_t off, len; for (i=0; invalid_labels[i]; i++) { err = ksba_dn_str2der (invalid_labels[i], &buf, &len); if (gpg_err_code (err) != GPG_ERR_UNKNOWN_NAME) fail ("invalid label not detected"); err = ksba_dn_teststr (invalid_labels[i], 0, &off, &len); if (!err) fail ("ksba_dn_test_str returned no error"); - printf ("string ->%s<- error at %lu.%lu (%.*s)\n", - invalid_labels[i], (unsigned long)off, (unsigned long)len, - (int)len, invalid_labels[i]+off); + if (!quiet) + printf ("string ->%s<- error at %lu.%lu (%.*s)\n", + invalid_labels[i], (unsigned long)off, (unsigned long)len, + (int)len, invalid_labels[i]+off); xfree (buf); } } int main (int argc, char **argv) { char inputbuf[4096]; unsigned char *buf; size_t len; gpg_error_t err; char *string; - if (argc == 2 && !strcmp (argv[1], "--to-str") ) + if (argc) + { + argc--; argv++; + } + if (argc && !strcmp (*argv, "--verbose")) + { + verbose = 1; + argc--; argv++; + } + + if (argc == 1 && !strcmp (argv[0], "--to-str") ) { /* Read the DER encoded DN from stdin write the string to stdout */ len = fread (inputbuf, 1, sizeof inputbuf, stdin); if (!feof (stdin)) fail ("read error or input too large"); err = ksba_dn_der2str (inputbuf, len, &string); fail_if_err (err); fputs (string, stdout); ksba_free (string); } - else if (argc == 2 && !strcmp (argv[1], "--to-der") ) + else if (argc == 1 && !strcmp (argv[0], "--to-der") ) { /* Read the String from stdin write the DER encoding to stdout */ len = fread (inputbuf, 1, sizeof inputbuf, stdin); if (!feof (stdin)) fail ("read error or input too large"); err = ksba_dn_str2der (inputbuf, &buf, &len); fail_if_err (err); fwrite (buf, len, 1, stdout); } - else if (argc == 1) + else if (!argc) { + if (!verbose) + quiet = 1; test_0 (); test_1 (); test_2 (); } else { fprintf (stderr, "usage: t-dnparser [--to-str|--to-der]\n"); return 1; } return 0; }