2 * tst_uuid.c --- test program from the UUID library
4 * Copyright (C) 1996, 1997, 1998 Theodore Ts'o.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, and the entire permission notice in its entirety,
12 * including the disclaimer of warranties.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. The name of the author may not be used to endorse or promote
17 * products derived from this software without specific prior
20 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
21 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
22 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, ALL OF
23 * WHICH ARE HEREBY DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE
24 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
26 * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
27 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
28 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
30 * USE OF THIS SOFTWARE, EVEN IF NOT ADVISED OF THE POSSIBILITY OF SUCH
38 #define _WIN32_WINNT 0x0500
46 #include <uuid/uuid.h>
48 static int test_uuid(const char * uuid, int isValid)
50 static const char * validStr[2] = {"invalid", "valid"};
54 parsedOk = uuid_parse(uuid, uuidBits) == 0;
56 printf("%s is %s", uuid, validStr[isValid]);
57 if (parsedOk != isValid) {
58 printf(" but uuid_parse says %s\n", validStr[parsedOk]);
66 #define ATTR(x) __attribute__(x)
72 main(int argc ATTR((unused)) , char **argv ATTR((unused)))
77 time_t time_reg, time_gen;
84 uuid_unparse(buf, str);
85 printf("UUID generate = %s\n", str);
87 for (i=0, cp = (unsigned char *) &buf; i < 16; i++) {
88 printf("%02x", *cp++);
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");
99 uuid_generate_random(buf);
100 uuid_unparse(buf, str);
101 printf("UUID random string = %s\n", str);
103 for (i=0, cp = (unsigned char *) &buf; i < 16; i++) {
104 printf("%02x", *cp++);
107 type = uuid_type(buf);
108 variant = uuid_variant(buf);
109 printf("UUID type = %d, UUID variant = %d\n", type, variant);
110 if (variant != UUID_VARIANT_DCE) {
111 printf("Incorrect UUID Variant; was expecting DCE!\n");
115 printf("Incorrect UUID type; was expecting "
116 "4 (random type)!\n");
122 uuid_generate_time(buf);
123 uuid_unparse(buf, str);
124 printf("UUID string = %s\n", str);
125 printf("UUID time: ");
126 for (i=0, cp = (unsigned char *) &buf; i < 16; i++) {
127 printf("%02x", *cp++);
130 type = uuid_type(buf);
131 variant = uuid_variant(buf);
132 printf("UUID type = %d, UUID variant = %d\n", type, variant);
133 if (variant != UUID_VARIANT_DCE) {
134 printf("Incorrect UUID Variant; was expecting DCE!\n");
138 printf("Incorrect UUID type; was expecting "
139 "1 (time-based type)!\\n");
145 time_reg = uuid_time(buf, &tv);
146 printf("UUID generated at %lu reports %lu (%ld.%ld)\n",
147 (unsigned long)time_gen, (unsigned long)time_reg,
148 (long)tv.tv_sec, (long)tv.tv_usec);
149 /* allow 1s margin in case of rollover between sampling
150 * the current time and when the UUID is generated. */
151 if (time_reg > time_gen + 1) {
152 printf("UUID time comparison failed!\n");
155 printf("UUID time comparison succeeded.\n");
158 if (uuid_parse(str, tst) < 0) {
159 printf("UUID parse failed\n");
162 if (!uuid_compare(buf, tst)) {
163 printf("UUID parse and compare succeeded.\n");
165 printf("UUID parse and compare failed!\n");
169 if (uuid_is_null(tst))
170 printf("UUID clear and is null succeeded.\n");
172 printf("UUID clear and is null failed!\n");
176 if (!uuid_compare(buf, tst))
177 printf("UUID copy and compare succeeded.\n");
179 printf("UUID copy and compare failed!\n");
183 failed += test_uuid("84949cc5-4701-4a84-895b-354c584a981b", 1);
184 failed += test_uuid("84949CC5-4701-4A84-895B-354C584A981B", 1);
185 failed += test_uuid("84949cc5-4701-4a84-895b-354c584a981bc", 0);
186 failed += test_uuid("84949cc5-4701-4a84-895b-354c584a981", 0);
187 failed += test_uuid("84949cc5x4701-4a84-895b-354c584a981b", 0);
188 failed += test_uuid("84949cc504701-4a84-895b-354c584a981b", 0);
189 failed += test_uuid("84949cc5-470104a84-895b-354c584a981b", 0);
190 failed += test_uuid("84949cc5-4701-4a840895b-354c584a981b", 0);
191 failed += test_uuid("84949cc5-4701-4a84-895b0354c584a981b", 0);
192 failed += test_uuid("g4949cc5-4701-4a84-895b-354c584a981b", 0);
193 failed += test_uuid("84949cc5-4701-4a84-895b-354c584a981g", 0);
196 printf("%d failures.\n", failed);