]> pd.if.org Git - uuid/commitdiff
Added test program for sha1.
authorNathan Wagner <nw@hydaspes.if.org>
Fri, 14 Sep 2012 08:05:58 +0000 (08:05 +0000)
committerNathan Wagner <nw@hydaspes.if.org>
Fri, 14 Sep 2012 08:05:58 +0000 (08:05 +0000)
t/sha1.c [new file with mode: 0644]

diff --git a/t/sha1.c b/t/sha1.c
new file mode 100644 (file)
index 0000000..4903d43
--- /dev/null
+++ b/t/sha1.c
@@ -0,0 +1,88 @@
+/*
+ * test program for uuid library
+ *
+ * written by nathan wagner and placed in the public domain
+ */
+#include <stdio.h>
+#include <string.h>
+#include <stdlib.h>
+#include <time.h>
+
+#include "hash.h"
+#include "ctap.h"
+
+static void bytestr(unsigned char *h, char *s, int len) {
+       int i;
+       for (i=0; i < len; i++) {
+               s+= sprintf(s, "%02x", h[i]);
+       }
+}
+
+static int strbytes(char *s, unsigned char *d) {
+        unsigned int byte;
+        int i = 0;
+
+       if (!s) return 0;
+        if (*s == 0) return 0;
+
+       while (*s) {
+                if (*s == '-' || *s == ':' || *s == ' ') s++;
+
+                if (sscanf(s, "%02x", &byte) != 1) {
+                        return 0;
+                }
+                s += 2;
+                d[i++] = byte & 0xff;
+        }
+        return i;
+}
+
+int main(int ac, char *av[]) {
+       unsigned char tmp[20];
+       hash_state md;
+       int i, j;
+       char want[64];
+       char have[64];
+
+       static const struct {
+               char *msg;
+               int repeat;
+               unsigned char hash[20];
+       } tests[] = {
+       { "abc", 1, 
+       { 0xa9, 0x99, 0x3e, 0x36, 0x47, 0x06, 0x81, 0x6a,
+       0xba, 0x3e, 0x25, 0x71, 0x78, 0x50, 0xc2, 0x6c,
+       0x9c, 0xd0, 0xd8, 0x9d }
+       },
+       { "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq", 1, 
+       { 0x84, 0x98, 0x3E, 0x44, 0x1C, 0x3B, 0xD2, 0x6E,
+       0xBA, 0xAE, 0x4A, 0xA1, 0xF9, 0x51, 0x29, 0xE5,
+       0xE5, 0x46, 0x70, 0xF1 }
+       },
+       { "a", 1000000, 
+       { 0x34, 0xAA, 0x97, 0x3C, 0xD4, 0xC4, 0xDA, 0xA4,
+       0xF6, 0x1E, 0xEB, 0x2B, 0xDB, 0xAD, 0x27, 0x31,
+               0x65, 0x34, 0x01, 0x6F },
+       },
+       { "0123456701234567012345670123456701234567012345670123456701234567", 10, 
+       {0xDE, 0xA3, 0x56, 0xA2, 0xCD, 0xDD, 0x90, 0xC7, 0xA7, 0xEC, 0xED, 0xC5, 0xEB, 0xB5, 0x63, 0x93, 0x4F, 0x46, 0x04, 0x52},
+       },
+    { NULL, { 0 } }
+  };
+
+       plan(8);
+
+       for (i = 0; tests[i].msg != NULL; i++) {
+               sha1_init(&md);
+               for (j=0; j<tests[i].repeat; j++) {
+               sha1_process(&md, (unsigned char *)tests[i].msg, (unsigned long)strlen(tests[i].msg));
+               }
+               sha1_done(&md, tmp);
+               ok(memcmp(tmp, tests[i].hash, 20) == 0, "sha1 %s", tests[i].msg);
+               bytestr(tmp, have, 20);
+               bytestr(tests[i].hash, want, 20);
+               is_string(want, have, "sha1 string %s", tests[1].msg);
+       }
+
+       return 0;
+}