From 436d6860f384c6af50a789dcfed671a02adeaedd Mon Sep 17 00:00:00 2001 From: Nikos Mavrogiannopoulos Date: Tue, 19 Jul 2016 14:10:08 +0200 Subject: tests: enhanced DN decoding tests with encoding This adds unit tests for gnutls_x509_dn_set_str(). --- tests/x509-dn-decode.c | 124 +++++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 115 insertions(+), 9 deletions(-) (limited to 'tests/x509-dn-decode.c') diff --git a/tests/x509-dn-decode.c b/tests/x509-dn-decode.c index 0410883b18..d95349086e 100644 --- a/tests/x509-dn-decode.c +++ b/tests/x509-dn-decode.c @@ -34,6 +34,9 @@ #include "utils.h" +#define myfail(fmt, ...) \ + fail("%s: "fmt, test_name, ##__VA_ARGS__) + static void decode(const char *test_name, const gnutls_datum_t *raw, const char *expected) { int ret; @@ -42,32 +45,94 @@ static void decode(const char *test_name, const gnutls_datum_t *raw, const char ret = gnutls_x509_dn_init(&dn); if (ret < 0) { - fail("%s\n", gnutls_strerror(ret)); + myfail("%s\n", gnutls_strerror(ret)); } ret = gnutls_x509_dn_import(dn, raw); if (ret < 0) { - fail("%s\n", gnutls_strerror(ret)); + myfail("%s\n", gnutls_strerror(ret)); } ret = gnutls_x509_dn_get_str(dn, &out); if (ret < 0) { - fail("%s\n", gnutls_strerror(ret)); + myfail("%s\n", gnutls_strerror(ret)); } if (out.size != strlen(expected)) { - fail("The length of the output (%d) doesn't match the expected (%d)\n", (int)out.size, (int)strlen(expected)); + myfail("The length of the output (%d) doesn't match the expected (%d)\n", (int)out.size, (int)strlen(expected)); } if (memcmp(out.data, expected, out.size) != 0) { - fail("The string output (%s) doesn't match the expected (%s)\n", (char*)out.data, expected); + myfail("The string output (%s) doesn't match the expected (%s)\n", (char*)out.data, expected); } if (out.data[out.size] != 0) { - fail("The string output isn't null terminated\n"); + myfail("The string output isn't null terminated\n"); + } + + gnutls_free(out.data); + gnutls_x509_dn_deinit(dn); + + return; +} + +static void encode(const char *test_name, const gnutls_datum_t *raw, const char *str, int exp_error) +{ + int ret; + gnutls_datum_t out; + gnutls_x509_dn_t dn; + const char *err; + + ret = gnutls_x509_dn_init(&dn); + if (ret < 0) { + myfail("%s\n", gnutls_strerror(ret)); + } + + ret = gnutls_x509_dn_set_str(dn, str, &err); + + if (ret < 0) { + if (ret == exp_error) + goto cleanup; + + if (ret == GNUTLS_E_PARSING_ERROR) + myfail("error: %s: %s\n", gnutls_strerror(ret), err); + else + myfail("%s\n", gnutls_strerror(ret)); + } + + if (ret != exp_error) { + myfail("unexpected success in encoding (got: %d, exp: %d)\n", ret, exp_error); + } + + ret = gnutls_x509_dn_export2(dn, GNUTLS_X509_FMT_DER, &out); + if (ret < 0) { + myfail("%s\n", gnutls_strerror(ret)); + } + + if (out.size != raw->size) { + { + unsigned i; + fprintf(stderr, "got:\n"); + for (i=0;isize); + } + + if (memcmp(out.data, raw->data, out.size) != 0) { + { + unsigned i; + fprintf(stderr, "got:\n"); + for (i=0;i