xref: /aosp_15_r20/external/e2fsprogs/lib/uuid/tst_uuid.c (revision 6a54128f25917bfc36a8a6e9d722c04a0b4641b6)
1 /*
2  * tst_uuid.c --- test program from the UUID library
3  *
4  * Copyright (C) 1996, 1997, 1998 Theodore Ts'o.
5  *
6  * %Begin-Header%
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
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
18  *    written permission.
19  *
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
31  * DAMAGE.
32  * %End-Header%
33  */
34 
35 #include "config.h"
36 
37 #include <stdio.h>
38 #include <stdlib.h>
39 
40 #include <uuid/uuid.h>
41 
test_uuid(const char * uuid,int isValid)42 static int test_uuid(const char * uuid, int isValid)
43 {
44 	static const char * validStr[2] = {"invalid", "valid"};
45 	uuid_t uuidBits;
46 	int parsedOk;
47 
48 	parsedOk = uuid_parse(uuid, uuidBits) == 0;
49 
50 	printf("%s is %s", uuid, validStr[isValid]);
51 	if (parsedOk != isValid) {
52 		printf(" but uuid_parse says %s\n", validStr[parsedOk]);
53 		return 1;
54 	}
55 	printf(", OK\n");
56 	return 0;
57 }
58 
59 #ifdef __GNUC__
60 #define ATTR(x) __attribute__(x)
61 #else
62 #define ATTR(x)
63 #endif
64 
65 int
main(int argc ATTR ((unused)),char ** argv ATTR ((unused)))66 main(int argc ATTR((unused)) , char **argv ATTR((unused)))
67 {
68 	uuid_t		buf, tst;
69 	char		str[100];
70 	struct timeval	tv;
71 	time_t		time_reg, time_gen;
72 	unsigned char	*cp;
73 	int i;
74 	int failed = 0;
75 	int type, variant;
76 
77 	uuid_generate(buf);
78 	uuid_unparse(buf, str);
79 	printf("UUID generate = %s\n", str);
80 	printf("UUID: ");
81 	for (i=0, cp = (unsigned char *) &buf; i < 16; i++) {
82 		printf("%02x", *cp++);
83 	}
84 	printf("\n");
85 	type = uuid_type(buf); 	variant = uuid_variant(buf);
86 	printf("UUID type = %d, UUID variant = %d\n", type, variant);
87 	if (variant != UUID_VARIANT_DCE) {
88 		printf("Incorrect UUID Variant; was expecting DCE!\n");
89 		failed++;
90 	}
91 	printf("\n");
92 
93 	uuid_generate_random(buf);
94 	uuid_unparse(buf, str);
95 	printf("UUID random string = %s\n", str);
96 	printf("UUID: ");
97 	for (i=0, cp = (unsigned char *) &buf; i < 16; i++) {
98 		printf("%02x", *cp++);
99 	}
100 	printf("\n");
101 	type = uuid_type(buf);
102 	variant = uuid_variant(buf);
103 	printf("UUID type = %d, UUID variant = %d\n", type, variant);
104 	if (variant != UUID_VARIANT_DCE) {
105 		printf("Incorrect UUID Variant; was expecting DCE!\n");
106 		failed++;
107 	}
108 	if (type != 4) {
109 		printf("Incorrect UUID type; was expecting "
110 		       "4 (random type)!\n");
111 		failed++;
112 	}
113 	printf("\n");
114 
115 	time_gen = time(0);
116 	uuid_generate_time(buf);
117 	uuid_unparse(buf, str);
118 	printf("UUID string = %s\n", str);
119 	printf("UUID time: ");
120 	for (i=0, cp = (unsigned char *) &buf; i < 16; i++) {
121 		printf("%02x", *cp++);
122 	}
123 	printf("\n");
124 	type = uuid_type(buf);
125 	variant = uuid_variant(buf);
126 	printf("UUID type = %d, UUID variant = %d\n", type, variant);
127 	if (variant != UUID_VARIANT_DCE) {
128 		printf("Incorrect UUID Variant; was expecting DCE!\n");
129 		failed++;
130 	}
131 	if (type != 1) {
132 		printf("Incorrect UUID type; was expecting "
133 		       "1 (time-based type)!\\n");
134 		failed++;
135 	}
136 
137 	tv.tv_sec = 0;
138 	tv.tv_usec = 0;
139 	time_reg = uuid_time(buf, &tv);
140 	printf("UUID generated at %lu reports %lu (%ld.%ld)\n",
141 	       (unsigned long)time_gen, (unsigned long)time_reg,
142 	       (long)tv.tv_sec, (long)tv.tv_usec);
143 	/* allow 1s margin in case of rollover between sampling
144 	 * the current time and when the UUID is generated. */
145 	if (time_reg > time_gen + 1) {
146 		printf("UUID time comparison failed!\n");
147 		failed++;
148 	} else {
149 		printf("UUID time comparison succeeded.\n");
150 	}
151 
152 	if (uuid_parse(str, tst) < 0) {
153 		printf("UUID parse failed\n");
154 		failed++;
155 	}
156 	if (!uuid_compare(buf, tst)) {
157 		printf("UUID parse and compare succeeded.\n");
158 	} else {
159 		printf("UUID parse and compare failed!\n");
160 		failed++;
161 	}
162 	uuid_clear(tst);
163 	if (uuid_is_null(tst))
164 		printf("UUID clear and is null succeeded.\n");
165 	else {
166 		printf("UUID clear and is null failed!\n");
167 		failed++;
168 	}
169 	uuid_copy(buf, tst);
170 	if (!uuid_compare(buf, tst))
171 		printf("UUID copy and compare succeeded.\n");
172 	else {
173 		printf("UUID copy and compare failed!\n");
174 		failed++;
175 	}
176 
177 	failed += test_uuid("84949cc5-4701-4a84-895b-354c584a981b", 1);
178 	failed += test_uuid("84949CC5-4701-4A84-895B-354C584A981B", 1);
179 	failed += test_uuid("84949cc5-4701-4a84-895b-354c584a981bc", 0);
180 	failed += test_uuid("84949cc5-4701-4a84-895b-354c584a981", 0);
181 	failed += test_uuid("84949cc5x4701-4a84-895b-354c584a981b", 0);
182 	failed += test_uuid("84949cc504701-4a84-895b-354c584a981b", 0);
183 	failed += test_uuid("84949cc5-470104a84-895b-354c584a981b", 0);
184 	failed += test_uuid("84949cc5-4701-4a840895b-354c584a981b", 0);
185 	failed += test_uuid("84949cc5-4701-4a84-895b0354c584a981b", 0);
186 	failed += test_uuid("g4949cc5-4701-4a84-895b-354c584a981b", 0);
187 	failed += test_uuid("84949cc5-4701-4a84-895b-354c584a981g", 0);
188 
189 	if (failed) {
190 		printf("%d failures.\n", failed);
191 		exit(1);
192 	}
193 	return 0;
194 }
195