1*0baf9d8dSJessica Hamilton /*
2*0baf9d8dSJessica Hamilton * tst_uuid.c --- test program from the UUID library
3*0baf9d8dSJessica Hamilton *
4*0baf9d8dSJessica Hamilton * Copyright (C) 1996, 1997, 1998 Theodore Ts'o.
5*0baf9d8dSJessica Hamilton *
6*0baf9d8dSJessica Hamilton * %Begin-Header%
7*0baf9d8dSJessica Hamilton * Redistribution and use in source and binary forms, with or without
8*0baf9d8dSJessica Hamilton * modification, are permitted provided that the following conditions
9*0baf9d8dSJessica Hamilton * are met:
10*0baf9d8dSJessica Hamilton * 1. Redistributions of source code must retain the above copyright
11*0baf9d8dSJessica Hamilton * notice, and the entire permission notice in its entirety,
12*0baf9d8dSJessica Hamilton * including the disclaimer of warranties.
13*0baf9d8dSJessica Hamilton * 2. Redistributions in binary form must reproduce the above copyright
14*0baf9d8dSJessica Hamilton * notice, this list of conditions and the following disclaimer in the
15*0baf9d8dSJessica Hamilton * documentation and/or other materials provided with the distribution.
16*0baf9d8dSJessica Hamilton * 3. The name of the author may not be used to endorse or promote
17*0baf9d8dSJessica Hamilton * products derived from this software without specific prior
18*0baf9d8dSJessica Hamilton * written permission.
19*0baf9d8dSJessica Hamilton *
20*0baf9d8dSJessica Hamilton * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
21*0baf9d8dSJessica Hamilton * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
22*0baf9d8dSJessica Hamilton * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, ALL OF
23*0baf9d8dSJessica Hamilton * WHICH ARE HEREBY DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE
24*0baf9d8dSJessica Hamilton * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25*0baf9d8dSJessica Hamilton * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
26*0baf9d8dSJessica Hamilton * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
27*0baf9d8dSJessica Hamilton * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
28*0baf9d8dSJessica Hamilton * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29*0baf9d8dSJessica Hamilton * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
30*0baf9d8dSJessica Hamilton * USE OF THIS SOFTWARE, EVEN IF NOT ADVISED OF THE POSSIBILITY OF SUCH
31*0baf9d8dSJessica Hamilton * DAMAGE.
32*0baf9d8dSJessica Hamilton * %End-Header%
33*0baf9d8dSJessica Hamilton */
34*0baf9d8dSJessica Hamilton
35*0baf9d8dSJessica Hamilton #include "config.h"
36*0baf9d8dSJessica Hamilton
37*0baf9d8dSJessica Hamilton #ifdef _WIN32
38*0baf9d8dSJessica Hamilton #define _WIN32_WINNT 0x0500
39*0baf9d8dSJessica Hamilton #include <windows.h>
40*0baf9d8dSJessica Hamilton #define UUID MYUUID
41*0baf9d8dSJessica Hamilton #endif
42*0baf9d8dSJessica Hamilton
43*0baf9d8dSJessica Hamilton #include <stdio.h>
44*0baf9d8dSJessica Hamilton #include <stdlib.h>
45*0baf9d8dSJessica Hamilton
46*0baf9d8dSJessica Hamilton #include <uuid/uuid.h>
47*0baf9d8dSJessica Hamilton
test_uuid(const char * uuid,int isValid)48*0baf9d8dSJessica Hamilton static int test_uuid(const char * uuid, int isValid)
49*0baf9d8dSJessica Hamilton {
50*0baf9d8dSJessica Hamilton static const char * validStr[2] = {"invalid", "valid"};
51*0baf9d8dSJessica Hamilton uuid_t uuidBits;
52*0baf9d8dSJessica Hamilton int parsedOk;
53*0baf9d8dSJessica Hamilton
54*0baf9d8dSJessica Hamilton parsedOk = uuid_parse(uuid, uuidBits) == 0;
55*0baf9d8dSJessica Hamilton
56*0baf9d8dSJessica Hamilton printf("%s is %s", uuid, validStr[isValid]);
57*0baf9d8dSJessica Hamilton if (parsedOk != isValid) {
58*0baf9d8dSJessica Hamilton printf(" but uuid_parse says %s\n", validStr[parsedOk]);
59*0baf9d8dSJessica Hamilton return 1;
60*0baf9d8dSJessica Hamilton }
61*0baf9d8dSJessica Hamilton printf(", OK\n");
62*0baf9d8dSJessica Hamilton return 0;
63*0baf9d8dSJessica Hamilton }
64*0baf9d8dSJessica Hamilton
65*0baf9d8dSJessica Hamilton #ifdef __GNUC__
66*0baf9d8dSJessica Hamilton #define ATTR(x) __attribute__(x)
67*0baf9d8dSJessica Hamilton #else
68*0baf9d8dSJessica Hamilton #define ATTR(x)
69*0baf9d8dSJessica Hamilton #endif
70*0baf9d8dSJessica Hamilton
71*0baf9d8dSJessica Hamilton int
main(int argc ATTR ((unused)),char ** argv ATTR ((unused)))72*0baf9d8dSJessica Hamilton main(int argc ATTR((unused)) , char **argv ATTR((unused)))
73*0baf9d8dSJessica Hamilton {
74*0baf9d8dSJessica Hamilton uuid_t buf, tst;
75*0baf9d8dSJessica Hamilton char str[100];
76*0baf9d8dSJessica Hamilton struct timeval tv;
77*0baf9d8dSJessica Hamilton time_t time_reg, time_gen;
78*0baf9d8dSJessica Hamilton unsigned char *cp;
79*0baf9d8dSJessica Hamilton int i;
80*0baf9d8dSJessica Hamilton int failed = 0;
81*0baf9d8dSJessica Hamilton int type, variant;
82*0baf9d8dSJessica Hamilton
83*0baf9d8dSJessica Hamilton uuid_generate(buf);
84*0baf9d8dSJessica Hamilton uuid_unparse(buf, str);
85*0baf9d8dSJessica Hamilton printf("UUID generate = %s\n", str);
86*0baf9d8dSJessica Hamilton printf("UUID: ");
87*0baf9d8dSJessica Hamilton for (i=0, cp = (unsigned char *) &buf; i < 16; i++) {
88*0baf9d8dSJessica Hamilton printf("%02x", *cp++);
89*0baf9d8dSJessica Hamilton }
90*0baf9d8dSJessica Hamilton printf("\n");
91*0baf9d8dSJessica Hamilton type = uuid_type(buf); variant = uuid_variant(buf);
92*0baf9d8dSJessica Hamilton printf("UUID type = %d, UUID variant = %d\n", type, variant);
93*0baf9d8dSJessica Hamilton if (variant != UUID_VARIANT_DCE) {
94*0baf9d8dSJessica Hamilton printf("Incorrect UUID Variant; was expecting DCE!\n");
95*0baf9d8dSJessica Hamilton failed++;
96*0baf9d8dSJessica Hamilton }
97*0baf9d8dSJessica Hamilton printf("\n");
98*0baf9d8dSJessica Hamilton
99*0baf9d8dSJessica Hamilton uuid_generate_random(buf);
100*0baf9d8dSJessica Hamilton uuid_unparse(buf, str);
101*0baf9d8dSJessica Hamilton printf("UUID random string = %s\n", str);
102*0baf9d8dSJessica Hamilton printf("UUID: ");
103*0baf9d8dSJessica Hamilton for (i=0, cp = (unsigned char *) &buf; i < 16; i++) {
104*0baf9d8dSJessica Hamilton printf("%02x", *cp++);
105*0baf9d8dSJessica Hamilton }
106*0baf9d8dSJessica Hamilton printf("\n");
107*0baf9d8dSJessica Hamilton type = uuid_type(buf);
108*0baf9d8dSJessica Hamilton variant = uuid_variant(buf);
109*0baf9d8dSJessica Hamilton printf("UUID type = %d, UUID variant = %d\n", type, variant);
110*0baf9d8dSJessica Hamilton if (variant != UUID_VARIANT_DCE) {
111*0baf9d8dSJessica Hamilton printf("Incorrect UUID Variant; was expecting DCE!\n");
112*0baf9d8dSJessica Hamilton failed++;
113*0baf9d8dSJessica Hamilton }
114*0baf9d8dSJessica Hamilton if (type != 4) {
115*0baf9d8dSJessica Hamilton printf("Incorrect UUID type; was expecting "
116*0baf9d8dSJessica Hamilton "4 (random type)!\n");
117*0baf9d8dSJessica Hamilton failed++;
118*0baf9d8dSJessica Hamilton }
119*0baf9d8dSJessica Hamilton printf("\n");
120*0baf9d8dSJessica Hamilton
121*0baf9d8dSJessica Hamilton time_gen = time(0);
122*0baf9d8dSJessica Hamilton uuid_generate_time(buf);
123*0baf9d8dSJessica Hamilton uuid_unparse(buf, str);
124*0baf9d8dSJessica Hamilton printf("UUID string = %s\n", str);
125*0baf9d8dSJessica Hamilton printf("UUID time: ");
126*0baf9d8dSJessica Hamilton for (i=0, cp = (unsigned char *) &buf; i < 16; i++) {
127*0baf9d8dSJessica Hamilton printf("%02x", *cp++);
128*0baf9d8dSJessica Hamilton }
129*0baf9d8dSJessica Hamilton printf("\n");
130*0baf9d8dSJessica Hamilton type = uuid_type(buf);
131*0baf9d8dSJessica Hamilton variant = uuid_variant(buf);
132*0baf9d8dSJessica Hamilton printf("UUID type = %d, UUID variant = %d\n", type, variant);
133*0baf9d8dSJessica Hamilton if (variant != UUID_VARIANT_DCE) {
134*0baf9d8dSJessica Hamilton printf("Incorrect UUID Variant; was expecting DCE!\n");
135*0baf9d8dSJessica Hamilton failed++;
136*0baf9d8dSJessica Hamilton }
137*0baf9d8dSJessica Hamilton if (type != 1) {
138*0baf9d8dSJessica Hamilton printf("Incorrect UUID type; was expecting "
139*0baf9d8dSJessica Hamilton "1 (time-based type)!\\n");
140*0baf9d8dSJessica Hamilton failed++;
141*0baf9d8dSJessica Hamilton }
142*0baf9d8dSJessica Hamilton
143*0baf9d8dSJessica Hamilton tv.tv_sec = 0;
144*0baf9d8dSJessica Hamilton tv.tv_usec = 0;
145*0baf9d8dSJessica Hamilton time_reg = uuid_time(buf, &tv);
146*0baf9d8dSJessica Hamilton printf("UUID generated at %lu reports %lu (%ld.%ld)\n",
147*0baf9d8dSJessica Hamilton time_gen, time_reg, tv.tv_sec, (long)tv.tv_usec);
148*0baf9d8dSJessica Hamilton /* allow 1s margin in case of rollover between sampling
149*0baf9d8dSJessica Hamilton * the current time and when the UUID is generated. */
150*0baf9d8dSJessica Hamilton if (time_reg > time_gen + 1) {
151*0baf9d8dSJessica Hamilton printf("UUID time comparison failed!\n");
152*0baf9d8dSJessica Hamilton failed++;
153*0baf9d8dSJessica Hamilton } else {
154*0baf9d8dSJessica Hamilton printf("UUID time comparison succeeded.\n");
155*0baf9d8dSJessica Hamilton }
156*0baf9d8dSJessica Hamilton
157*0baf9d8dSJessica Hamilton if (uuid_parse(str, tst) < 0) {
158*0baf9d8dSJessica Hamilton printf("UUID parse failed\n");
159*0baf9d8dSJessica Hamilton failed++;
160*0baf9d8dSJessica Hamilton }
161*0baf9d8dSJessica Hamilton if (!uuid_compare(buf, tst)) {
162*0baf9d8dSJessica Hamilton printf("UUID parse and compare succeeded.\n");
163*0baf9d8dSJessica Hamilton } else {
164*0baf9d8dSJessica Hamilton printf("UUID parse and compare failed!\n");
165*0baf9d8dSJessica Hamilton failed++;
166*0baf9d8dSJessica Hamilton }
167*0baf9d8dSJessica Hamilton uuid_clear(tst);
168*0baf9d8dSJessica Hamilton if (uuid_is_null(tst))
169*0baf9d8dSJessica Hamilton printf("UUID clear and is null succeeded.\n");
170*0baf9d8dSJessica Hamilton else {
171*0baf9d8dSJessica Hamilton printf("UUID clear and is null failed!\n");
172*0baf9d8dSJessica Hamilton failed++;
173*0baf9d8dSJessica Hamilton }
174*0baf9d8dSJessica Hamilton uuid_copy(buf, tst);
175*0baf9d8dSJessica Hamilton if (!uuid_compare(buf, tst))
176*0baf9d8dSJessica Hamilton printf("UUID copy and compare succeeded.\n");
177*0baf9d8dSJessica Hamilton else {
178*0baf9d8dSJessica Hamilton printf("UUID copy and compare failed!\n");
179*0baf9d8dSJessica Hamilton failed++;
180*0baf9d8dSJessica Hamilton }
181*0baf9d8dSJessica Hamilton
182*0baf9d8dSJessica Hamilton failed += test_uuid("84949cc5-4701-4a84-895b-354c584a981b", 1);
183*0baf9d8dSJessica Hamilton failed += test_uuid("84949CC5-4701-4A84-895B-354C584A981B", 1);
184*0baf9d8dSJessica Hamilton failed += test_uuid("84949cc5-4701-4a84-895b-354c584a981bc", 0);
185*0baf9d8dSJessica Hamilton failed += test_uuid("84949cc5-4701-4a84-895b-354c584a981", 0);
186*0baf9d8dSJessica Hamilton failed += test_uuid("84949cc5x4701-4a84-895b-354c584a981b", 0);
187*0baf9d8dSJessica Hamilton failed += test_uuid("84949cc504701-4a84-895b-354c584a981b", 0);
188*0baf9d8dSJessica Hamilton failed += test_uuid("84949cc5-470104a84-895b-354c584a981b", 0);
189*0baf9d8dSJessica Hamilton failed += test_uuid("84949cc5-4701-4a840895b-354c584a981b", 0);
190*0baf9d8dSJessica Hamilton failed += test_uuid("84949cc5-4701-4a84-895b0354c584a981b", 0);
191*0baf9d8dSJessica Hamilton failed += test_uuid("g4949cc5-4701-4a84-895b-354c584a981b", 0);
192*0baf9d8dSJessica Hamilton failed += test_uuid("84949cc5-4701-4a84-895b-354c584a981g", 0);
193*0baf9d8dSJessica Hamilton
194*0baf9d8dSJessica Hamilton if (failed) {
195*0baf9d8dSJessica Hamilton printf("%d failures.\n", failed);
196*0baf9d8dSJessica Hamilton exit(1);
197*0baf9d8dSJessica Hamilton }
198*0baf9d8dSJessica Hamilton return 0;
199*0baf9d8dSJessica Hamilton }
200