From c784264fc1aaa6a22eb718020ef5886087abd525 Mon Sep 17 00:00:00 2001 From: Nathan Wagner Date: Tue, 12 Jun 2012 05:56:45 -0500 Subject: [PATCH] Added test framework --- Makefile | 21 +++++++++++------ internal.c | 46 +++++++++++++++++++++++++++++++++++++ pduuid.h | 7 ++++++ uuidgen.c | 67 ------------------------------------------------------ 4 files changed, 67 insertions(+), 74 deletions(-) delete mode 100644 uuidgen.c diff --git a/Makefile b/Makefile index 1b1c6bb..35516fe 100644 --- a/Makefile +++ b/Makefile @@ -3,33 +3,40 @@ OBJ = md5.o sha1.o rng.o internal.o CC=gcc +CFLAGS=-Wall -I. -L. # set the following for windows #LDFLAGS += -liphlpapi -all: uuidgen +all: libpduuid.a windows: libpduuid.a uuidgen.o - gcc $(CFLAGS) -Wall -L. -o $@ uuidgen.o -lpduuid $(LDFLAGS) -liphlpapi + $(CC) $(CFLAGS) -Wall -L. -o $@ uuidgen.o -lpduuid $(LDFLAGS) -liphlpapi libpduuid.a: $(OBJ) ar rcuv $@ $+ ranlib $@ +t/%.t: t/%.o t/ctap.o + $(CC) $(CFLAGS) -lpduuid -Wall -I. -o $@ $+ + +test: t/uuidgen.t libpduuid.a + prove t/*.t + md5.o: md5.c - gcc $(CFLAGS) -Wall -fPIC -DLTC_SMALL_CODE -c -o $@ $+ + $(CC) $(CFLAGS) -Wall -fPIC -DLTC_SMALL_CODE -c -o $@ $+ sha1.o: sha1.c - gcc $(CFLAGS) -Wall -fPIC -DLTC_SMALL_CODE -c -o $@ $+ + $(CC) $(CFLAGS) -Wall -fPIC -DLTC_SMALL_CODE -c -o $@ $+ rng.o: rng.c - gcc $(CFLAGS) -Wall -fPIC -c -o $@ $+ + $(CC) $(CFLAGS) -Wall -fPIC -c -o $@ $+ internal.o: internal.c - gcc $(CFLAGS) -Wall -fPIC -c -o $@ $+ + $(CC) $(CFLAGS) -Wall -fPIC -c -o $@ $+ uuidgen: libpduuid.a uuidgen.o - gcc $(CFLAGS) -Wall -L. -o $@ uuidgen.o -lpduuid $(LDFLAGS) + $(CC) $(CFLAGS) -Wall -L. -o $@ uuidgen.o -lpduuid $(LDFLAGS) clean: rm -f *.o *.a uuidgen postgres/*.o postgres/*.so diff --git a/internal.c b/internal.c index f2cdf87..0ad2b81 100644 --- a/internal.c +++ b/internal.c @@ -186,6 +186,7 @@ static uint64_t current_node(struct pd_uuid_state *st) { continue; } if (ioctl(s, SIOCGIFHWADDR, req) == 0) { + int j; data = (unsigned char *)req->ifr_hwaddr.sa_data; node = data[0]; node = node << 8; node += data[1]; @@ -457,3 +458,48 @@ int pd_uuid_set_string(pd_uuid_t *uuid, char *s) { } return 1; } + +/* pre-defined namespace uuids */ + +pd_uuid_t pd_uuid_ns_dns = { + { + 0x6b, 0xa7, 0xb8, 0x10, 0x9d, 0xad, 0x11, 0xd1, + 0x80, 0xb4, 0x00, 0xc0, 0x4f, 0xd4, 0x30, 0xc8 + } +}; + +pd_uuid_t pd_uuid_ns_url = { + { + 0x6b, 0xa7, 0xb8, 0x11, 0x9d, 0xad, 0x11, 0xd1, + 0x80, 0xb4, 0x00, 0xc0, 0x4f, 0xd4, 0x30, 0xc8 + } +}; + +pd_uuid_t pd_uuid_ns_oid = { + { + 0x6b, 0xa7, 0xb8, 0x12, 0x9d, 0xad, 0x11, 0xd1, + 0x80, 0xb4, 0x00, 0xc0, 0x4f, 0xd4, 0x30, 0xc8 + } +}; + +pd_uuid_t pd_uuid_ns_x500 = { + { + 0x6b, 0xa7, 0xb8, 0x14, 0x9d, 0xad, 0x11, 0xd1, + 0x80, 0xb4, 0x00, 0xc0, 0x4f, 0xd4, 0x30, 0xc8 + } +}; + +int pd_uuid_copy(pd_uuid_t *src, pd_uuid_t *dst) { + if (src && dst) { + memcpy(dst->data, src->data, 16); + return 1; + } + return 0; +} + +int pd_uuid_cmp(pd_uuid_t *a, pd_uuid_t *b) { + if (a && b) { + return memcmp(a, b, 16); + } + return 1; +} diff --git a/pduuid.h b/pduuid.h index 77cbcc1..86c5f6d 100644 --- a/pduuid.h +++ b/pduuid.h @@ -30,11 +30,18 @@ int pd_uuid_make_v4(struct pd_uuid_state *s, pd_uuid_t *uuid); int pd_uuid_make_v3(struct pd_uuid_state *s, pd_uuid_t *uuid, pd_uuid_t *ns, void *data, int len); int pd_uuid_make_v5(struct pd_uuid_state *s, pd_uuid_t *uuid, pd_uuid_t *ns, void *data, int len); +int pd_uuid_copy(pd_uuid_t *src, pd_uuid_t *dst); +int pd_uuid_cmp(pd_uuid_t *a, pd_uuid_t *b); + int pd_set_uuid_hash(pd_uuid_t *s, void *hash, int version); int pd_uuid_set_string(pd_uuid_t *uuid, char *s); unsigned long pd_uuid_rng_get_bytes(unsigned char *out, unsigned long outlen); +pd_uuid_t pd_uuid_ns_x500; +pd_uuid_t pd_uuid_ns_url; +pd_uuid_t pd_uuid_ns_dns; +pd_uuid_t pd_uuid_ns_oid; #endif diff --git a/uuidgen.c b/uuidgen.c deleted file mode 100644 index ada26d0..0000000 --- a/uuidgen.c +++ /dev/null @@ -1,67 +0,0 @@ -/* - * test program for uuid library - * - * written by nathan wagner and placed in the public domain - */ -#include -#include -#include - -#include "pduuid.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 main(int ac, char *av[]) { - pd_uuid_t uuid; - pd_uuid_t dns; - char fmt[37]; - -#ifdef WIN32 - srand(time(0)); -#else - srandom(time(0)); -#endif - - pd_uuid_make_v1(0, &uuid); - ufmt(&uuid, fmt); - printf("v1: %s\n", fmt); - - pd_uuid_make_v4(0, &uuid); - ufmt(&uuid, fmt); - printf("v4: %s\n", fmt); - - pd_uuid_set_string(&dns, "6ba7b810-9dad-11d1-80b4-00c04fd430c8"); - ufmt(&dns, fmt); - printf("dns: %s\n", fmt); - - pd_uuid_make_v3(0, &uuid, &dns, "granicus.if.org", strlen("granicus.if.org")); - ufmt(&uuid, fmt); - printf("granicus.if.org (v3): %s\n", fmt); - - pd_uuid_make_v5(0, &uuid, &dns, "granicus.if.org", strlen("granicus.if.org")); - ufmt(&uuid, fmt); - printf("granicus.if.org (v5): %s\n", fmt); - - return 0; -} -- 2.40.0