From 5ee112d5a01767b1bcceb12fa5c057b3dcf40890 Mon Sep 17 00:00:00 2001 From: Nathan Wagner Date: Thu, 5 Apr 2012 06:54:35 -0700 Subject: [PATCH 1/1] Added namespace uuids and test program --- t/ctap.c | 239 ++++++++++++++++++++++++++++++++++++++++++++++++++++ t/ctap.h | 25 ++++++ t/uuidgen.c | 94 +++++++++++++++++++++ 3 files changed, 358 insertions(+) create mode 100644 t/ctap.c create mode 100644 t/ctap.h create mode 100644 t/uuidgen.c diff --git a/t/ctap.c b/t/ctap.c new file mode 100644 index 0000000..9bdf9f3 --- /dev/null +++ b/t/ctap.c @@ -0,0 +1,239 @@ +#include +#include +#include +#include +#include +#include + +#include "ctap.h" + +/* global variable testnum? */ +static int test = 0; /* the test number */ +static int planned = 0; +static int intodo = 0; + +void begin_todo(void) { + intodo = 1; +} + +void end_todo(void) { + intodo = 0; +} + +void plan(int tests) { + test = 0; + planned = tests; + printf("1..%d\n", tests); +} + +static void print_lazy_plan(void) { + printf("1..%d\n", test); + fflush(stdout); +} + +void plan_lazy(void) { + test = 0; + planned = 0; + atexit(print_lazy_plan); +} + +void skip_all(const char *why, ...) { + printf("1..0"); + if (why) { + va_list args; + printf(" # SKIP "); + va_start(args, why); + vfprintf(stdout, why, args); + va_end(args); + } + printf("\n"); +} + +static void vfmtline(int pass, const char *directive, const char *fmt, va_list args) { + printf("%sok %d", pass ? "" : "not ", ++test); + if (fmt && !directive) { + printf(" -"); + } + if (directive) { + printf(" # %s", directive); + } + if (fmt) { + printf(" "); + vfprintf(stdout, fmt, args); + } + printf("\n"); +} + +#if 0 +static void fmtline(int pass, const char *info, const char *fmt, ...) { + va_list args; + + va_start(args,fmt); + vfmtline(pass, info, fmt, args); + va_end(args); +} +#endif + +void okv(int pass, const char *fmt, va_list args) { + vfmtline(pass, intodo ? "TODO" : 0, fmt, args); +} + +void ok(int pass, char *fmt, ...) { + va_list args; + + va_start(args, fmt); + okv(pass, fmt, args); + va_end(args); +} + +void ok_block(unsigned long count, int pass, const char *fmt, ...) { + va_list args; + va_list copy; + + if (count == 0) { + return; + } + + va_start(args, fmt); + while (count--) { + va_copy(copy, args); + okv(pass, fmt, copy); + va_end(copy); + } + va_end(args); +} + +void skip(const char *why, ...) { + va_list args; + + va_start(args, why); + vfmtline(1, "SKIP", why, args); + va_end(args); +} + +void skip_block(unsigned long count, const char *why, ...) { + va_list args; + va_list copy; + va_start(args, why); + + while (count--) { + va_copy(copy, args); + vfmtline(1, "SKIP", why, args); + va_end(copy); + } + + va_end(args); +} + +void bail(const char *fmt, ...) { + va_list args; + printf("Bail out!"); + if (fmt) { + va_start(args, fmt); + vfprintf(stdout, fmt, args); + va_end(args); + } + printf("\n"); + fflush(stdout); + exit(1); +} + +void sysbail(const char *fmt, ...) { + va_list args; + printf("Bail out!"); + if (fmt) { + va_start(args, fmt); + vfprintf(stdout, fmt, args); + va_end(args); + } + printf(": %s\n", strerror(errno)); + fflush(stdout); + exit(1); +} + +void sysdiag(const char *fmt, ...) { + va_list args; + if (!fmt) { return; } + printf("# "); + va_start(args, fmt); + vfprintf(stdout, fmt, args); + va_end(args); + printf(": %s\n", strerror(errno)); +} + +void diag(const char *fmt, ...) { + va_list args; + if (!fmt) { return; } + printf("# "); + va_start(args, fmt); + vfprintf(stdout, fmt, args); + va_end(args); + printf("\n"); +} + +void is_hex(unsigned long wanted, unsigned long seen, const char *fmt, ...) { + va_list args; + if (fmt) { + va_start(args, fmt); + okv(wanted == seen, fmt, args); + va_end(args); + } else { + ok(wanted == seen, NULL); + } + if (wanted != seen) { + diag("wanted: %ld", wanted); + diag("got : %ld", seen); + } +} + + +void is_int(long wanted, long seen, const char *fmt, ...) { + va_list args; + if (fmt) { + va_start(args, fmt); + okv(wanted == seen, fmt, args); + va_end(args); + } else { + ok(wanted == seen, NULL); + } + if (wanted != seen) { + diag("wanted: %ld", wanted); + diag("got : %ld", seen); + } +} + +void is_double(double wanted, double seen, double eps, const char *fmt, ...) { + int pass; + va_list args; + + pass = fabs(wanted - seen) <= eps; + if (fmt) { + va_start(args, fmt); + okv(pass, fmt, args); + va_end(args); + } else { + ok(wanted == seen, NULL); + } + if (!pass) { + diag("wanted: %f", wanted); + diag("got : %f", seen); + } +} + +void is_string(const char *wanted, const char *seen, const char *fmt, ...) { + int pass; + va_list args; + + pass = !strcmp(wanted,seen); + if (fmt) { + va_start(args, fmt); + okv(pass, fmt, args); + va_end(args); + } else { + ok(wanted == seen, NULL); + } + if (!pass) { + diag("wanted: %s", wanted); + diag("got : %s", seen); + } +} diff --git a/t/ctap.h b/t/ctap.h new file mode 100644 index 0000000..c641d2f --- /dev/null +++ b/t/ctap.h @@ -0,0 +1,25 @@ +#ifndef TAP_H_ +#define TAP_H_ 1 + +#include + +void begin_todo(void); +void end_todo(void); +void plan(int tests); +void plan_lazy(void); +void skip_all(const char *why, ...); +void okv(int pass, const char *fmt, va_list args); +void ok(int pass, char *fmt, ...); +void ok_block(unsigned long count, int pass, const char *fmt, ...); +void skip(const char *why, ...); +void skip_block(unsigned long count, const char *why, ...); +void bail(const char *fmt, ...); +void sysbail(const char *fmt, ...); +void sysdiag(const char *fmt, ...); +void diag(const char *fmt, ...); +void is_hex(unsigned long wanted, unsigned long seen, const char *fmt, ...); +void is_int(long wanted, long seen, const char *fmt, ...); +void is_double(double wanted, double seen, double eps, const char *fmt, ...); +void is_string(const char *wanted, const char *seen, const char *fmt, ...); + +#endif diff --git a/t/uuidgen.c b/t/uuidgen.c new file mode 100644 index 0000000..6fb3c56 --- /dev/null +++ b/t/uuidgen.c @@ -0,0 +1,94 @@ +/* + * test program for uuid library + * + * written by nathan wagner and placed in the public domain + */ +#include +#include +#include +#include + +#include "pduuid.h" +#include "ctap.h" + +void ufmt(pd_uuid_t *uuid, char *s) { + int i; + for (i=0;i<4;i++) { + s += sprintf(s, "%02x", uuid->data[i]); + } + s += sprintf(s, "-"); + for (;i<6;i++) { + s += sprintf(s, "%02x", uuid->data[i]); + } + s += sprintf(s, "-"); + for (;i<8;i++) { + s += sprintf(s, "%02x", uuid->data[i]); + } + s += sprintf(s, "-"); + for (;i<10;i++) { + s += sprintf(s, "%02x", uuid->data[i]); + } + s += sprintf(s, "-"); + for (;i<16;i++) { + s += sprintf(s, "%02x", uuid->data[i]); + } +} + +int str_ok(pd_uuid_t *uuid, char *s, char *name) { + char fmt[37]; + ufmt(uuid, fmt); + is_string(s, fmt, name); + return 0; +} + +int main(int ac, char *av[]) { + pd_uuid_t uuid; + char fmt[37]; + + char *url = "6ba7b811-9dad-11d1-80b4-00c04fd430c8"; + char *dns = "6ba7b810-9dad-11d1-80b4-00c04fd430c8"; + char *oid = "6ba7b812-9dad-11d1-80b4-00c04fd430c8"; + char *x500 = "6ba7b814-9dad-11d1-80b4-00c04fd430c8"; + +#ifdef WIN32 + srand(time(0)); +#else + srandom(time(0)); +#endif + + plan(7); + + pd_uuid_make_v1(0, &uuid); + ufmt(&uuid, fmt); + diag("v1: %s", fmt); + + pd_uuid_make_v4(0, &uuid); + ufmt(&uuid, fmt); + diag("v4: %s", fmt); + + pd_uuid_set_string(&uuid, "6ba7b810-9dad-11d1-80b4-00c04fd430c8"); + ufmt(&uuid, fmt); + is_string("6ba7b810-9dad-11d1-80b4-00c04fd430c8", fmt, "t/f string"); + + ufmt(&pd_uuid_ns_x500, fmt); + is_string(x500, fmt, "pd_uuid_ns_x500()"); + + ufmt(&pd_uuid_ns_oid, fmt); + is_string(oid, fmt, "pd_uuid_ns_oid()"); + + ufmt(&pd_uuid_ns_url, fmt); + is_string(url, fmt, "pd_uuid_ns_url()"); + + ufmt(&pd_uuid_ns_dns, fmt); + is_string(dns, fmt, "pd_uuid_ns_dns()"); + + pd_uuid_make_v3(0, &uuid, &pd_uuid_ns_dns, "granicus.if.org", strlen("granicus.if.org")); + ufmt(&uuid, fmt); + is_string("e6e8e9cb-78bd-33b3-b8ae-d442456e8169", fmt, "granicus.if.org (v3)"); + + pd_uuid_make_v5(0, &uuid, &pd_uuid_ns_dns, "granicus.if.org", strlen("granicus.if.org")); + ufmt(&uuid, fmt); + is_string("fc1d1ec9-f731-5bfb-854e-e38a4dbd9dd3", fmt, "granicus.if.org (v5)"); + + return 0; +} -- 2.40.0