blob: a9cc1916736dd4ad0b185137f5d1e7bc74de7bb2 [file] [log] [blame]
Theodore Ts'o19c78dc1997-04-29 16:17:09 +00001/*
2 * tst_uuid.c --- test program from the UUID library
3 *
Theodore Ts'o1e0a2211998-12-04 08:13:52 +00004 * Copyright (C) 1996, 1997, 1998 Theodore Ts'o.
Theodore Ts'o19c78dc1997-04-29 16:17:09 +00005 *
6 * %Begin-Header%
7 * This file may be redistributed under the terms of the GNU Public
8 * License.
9 * %End-Header%
10 */
11
Theodore Ts'o1e3472c1997-04-29 14:53:37 +000012#include <stdio.h>
Matthias Andreeb969b1b2003-12-28 13:04:35 +010013#include <stdlib.h>
Theodore Ts'o1e3472c1997-04-29 14:53:37 +000014
15#include "uuid.h"
16
Theodore Ts'o8d7f4582002-07-15 23:49:57 -040017static int test_uuid(const char * uuid, int isValid)
18{
19 static const char * validStr[2] = {"invalid", "valid"};
20 uuid_t uuidBits;
21 int parsedOk;
22
23 parsedOk = uuid_parse(uuid, uuidBits) == 0;
Matthias Andreeb969b1b2003-12-28 13:04:35 +010024
Theodore Ts'o8d7f4582002-07-15 23:49:57 -040025 printf("%s is %s", uuid, validStr[isValid]);
26 if (parsedOk != isValid) {
27 printf(" but uuid_parse says %s\n", validStr[parsedOk]);
28 return 1;
29 }
30 printf(", OK\n");
31 return 0;
32}
33
Theodore Ts'o1e3472c1997-04-29 14:53:37 +000034int
Theodore Ts'o818180c1998-06-27 05:11:14 +000035main(int argc, char **argv)
Theodore Ts'o1e3472c1997-04-29 14:53:37 +000036{
37 uuid_t buf, tst;
38 char str[100];
Theodore Ts'o1e0a2211998-12-04 08:13:52 +000039 struct timeval tv;
40 time_t time_reg;
Theodore Ts'o1e3472c1997-04-29 14:53:37 +000041 unsigned char *cp;
42 int i;
43 int failed = 0;
Theodore Ts'ob19d1a91999-06-18 00:32:03 +000044 int type, variant;
Theodore Ts'o1e3472c1997-04-29 14:53:37 +000045
46 uuid_generate(buf);
47 uuid_unparse(buf, str);
Theodore Ts'ob19d1a91999-06-18 00:32:03 +000048 printf("UUID generate = %s\n", str);
Theodore Ts'o1e3472c1997-04-29 14:53:37 +000049 printf("UUID: ");
50 for (i=0, cp = (unsigned char *) &buf; i < 16; i++) {
51 printf("%02x", *cp++);
52 }
53 printf("\n");
Theodore Ts'ob19d1a91999-06-18 00:32:03 +000054 type = uuid_type(buf); variant = uuid_variant(buf);
55 printf("UUID type = %d, UUID variant = %d\n", type, variant);
56 if (variant != UUID_VARIANT_DCE) {
57 printf("Incorrect UUID Variant; was expecting DCE!\n");
58 failed++;
59 }
60 printf("\n");
61
62 uuid_generate_random(buf);
63 uuid_unparse(buf, str);
64 printf("UUID random string = %s\n", str);
65 printf("UUID: ");
66 for (i=0, cp = (unsigned char *) &buf; i < 16; i++) {
67 printf("%02x", *cp++);
68 }
69 printf("\n");
70 type = uuid_type(buf); variant = uuid_variant(buf);
71 printf("UUID type = %d, UUID variant = %d\n", type, variant);
72 if (variant != UUID_VARIANT_DCE) {
73 printf("Incorrect UUID Variant; was expecting DCE!\n");
74 failed++;
75 }
76 if (type != 4) {
77 printf("Incorrect UUID type; was expecting "
78 "4 (random type)!\n");
79 failed++;
80 }
81 printf("\n");
Matthias Andreeb969b1b2003-12-28 13:04:35 +010082
Theodore Ts'ob19d1a91999-06-18 00:32:03 +000083 uuid_generate_time(buf);
84 uuid_unparse(buf, str);
85 printf("UUID string = %s\n", str);
86 printf("UUID time: ");
87 for (i=0, cp = (unsigned char *) &buf; i < 16; i++) {
88 printf("%02x", *cp++);
89 }
90 printf("\n");
91 type = uuid_type(buf); variant = uuid_variant(buf);
92 printf("UUID type = %d, UUID variant = %d\n", type, variant);
93 if (variant != UUID_VARIANT_DCE) {
94 printf("Incorrect UUID Variant; was expecting DCE!\n");
95 failed++;
96 }
97 if (type != 1) {
98 printf("Incorrect UUID type; was expecting "
99 "1 (time-based type)!\\n");
100 failed++;
101 }
Theodore Ts'o1e0a2211998-12-04 08:13:52 +0000102 tv.tv_sec = 0;
103 tv.tv_usec = 0;
104 time_reg = uuid_time(buf, &tv);
Theodore Ts'o96394d12001-01-12 18:30:54 +0000105 printf("UUID time is: (%ld, %ld): %s\n", tv.tv_sec, tv.tv_usec,
Theodore Ts'o1e0a2211998-12-04 08:13:52 +0000106 ctime(&time_reg));
Theodore Ts'o1e3472c1997-04-29 14:53:37 +0000107 uuid_parse(str, tst);
Theodore Ts'ob19d1a91999-06-18 00:32:03 +0000108 if (!uuid_compare(buf, tst))
Theodore Ts'o1e3472c1997-04-29 14:53:37 +0000109 printf("UUID parse and compare succeeded.\n");
110 else {
111 printf("UUID parse and compare failed!\n");
112 failed++;
113 }
114 uuid_clear(tst);
115 if (uuid_is_null(tst))
116 printf("UUID clear and is null succeeded.\n");
117 else {
118 printf("UUID clear and is null failed!\n");
119 failed++;
120 }
121 uuid_copy(buf, tst);
Theodore Ts'ob19d1a91999-06-18 00:32:03 +0000122 if (!uuid_compare(buf, tst))
Theodore Ts'o1e3472c1997-04-29 14:53:37 +0000123 printf("UUID copy and compare succeeded.\n");
124 else {
125 printf("UUID copy and compare failed!\n");
126 failed++;
127 }
Theodore Ts'o8d7f4582002-07-15 23:49:57 -0400128 failed += test_uuid("84949cc5-4701-4a84-895b-354c584a981b", 1);
129 failed += test_uuid("84949CC5-4701-4A84-895B-354C584A981B", 1);
130 failed += test_uuid("84949cc5-4701-4a84-895b-354c584a981bc", 0);
131 failed += test_uuid("84949cc5-4701-4a84-895b-354c584a981", 0);
132 failed += test_uuid("84949cc5x4701-4a84-895b-354c584a981b", 0);
133 failed += test_uuid("84949cc504701-4a84-895b-354c584a981b", 0);
134 failed += test_uuid("84949cc5-470104a84-895b-354c584a981b", 0);
135 failed += test_uuid("84949cc5-4701-4a840895b-354c584a981b", 0);
136 failed += test_uuid("84949cc5-4701-4a84-895b0354c584a981b", 0);
137 failed += test_uuid("g4949cc5-4701-4a84-895b-354c584a981b", 0);
138 failed += test_uuid("84949cc5-4701-4a84-895b-354c584a981g", 0);
Matthias Andreeb969b1b2003-12-28 13:04:35 +0100139
Theodore Ts'o1e3472c1997-04-29 14:53:37 +0000140 if (failed) {
141 printf("%d failures.\n", failed);
142 exit(1);
143 }
144 return 0;
145}