xref: /openbsd-src/regress/lib/libc/uuid/uuidtest.c (revision 03c8fe6abc17e7e068928ad55aee1415706debd7)
1 /*	$OpenBSD: uuidtest.c,v 1.2 2023/07/03 13:51:55 jasper Exp $	*/
2 /*
3  * Copyright (c) 2021, 2023 Jasper Lievisse Adriaanse <jasper@openbsd.org>
4  *
5  * Permission to use, copy, modify, and distribute this software for any
6  * purpose with or without fee is hereby granted, provided that the above
7  * copyright notice and this permission notice appear in all copies.
8  *
9  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16  */
17 
18 #include <assert.h>
19 #include <stdio.h>
20 #include <stdlib.h>
21 #include <string.h>
22 #include <uuid.h>
23 
24 #define ASSERT_EQ(a, b) assert((a) == (b))
25 
26 int
main(int argc,char ** argv)27 main(int argc, char **argv)
28 {
29 	struct uuid	uuid, uuid2, uuid_want;
30 	char		*uuid_str, *uuid_str_want;
31 	uint32_t	status;
32 	unsigned char	bin[16];
33 	int		rc, t = 1;
34 
35 	/* Test invalid input to uuid_from_string() */
36 	printf("[%d] uuid_from_string (invalid) ", t);
37 	uuid_str = "6fc3134d-011d-463d-a6b4-fe1f3a5e57dX";
38 	uuid_from_string(uuid_str, &uuid, &status);
39 	if (status != uuid_s_invalid_string_uuid) {
40 		printf("failed to return uuid_s_invalid_string_uuid for '%s'\n",
41 		    uuid_str);
42 		return 1;
43 	}
44 
45 	printf("ok\n");
46 	t++;
47 
48 	/* Test a bad version gets recognized */
49 	printf("[%d] uuid_from_string (bad version) ", t);
50 	uuid_str = "ffffffff-ffff-ffff-ffff-ffffffffffff";
51 	uuid_from_string(uuid_str, &uuid, &status);
52 	if (status != uuid_s_bad_version) {
53 		printf("failed to return uuid_s_bad_version for '%s'\n",
54 		    uuid_str);
55 		return 1;
56 	}
57 
58 	printf("ok\n");
59 	t++;
60 
61 	/* Test valid input to uuid_from_string() */
62 	printf("[%d] uuid_from_string ", t);
63 	uuid_str = "f81d4fae-7dec-11d0-a765-00a0c91e6bf6";
64 
65 	uuid_want.time_low = 0xf81d4fae;
66 	uuid_want.time_mid = 0x7dec;
67 	uuid_want.time_hi_and_version = 0x11d0;
68 	uuid_want.clock_seq_hi_and_reserved = 0xa7;
69 	uuid_want.clock_seq_low = 0x65;
70 	uuid_want.node[0] = 0x00;
71 	uuid_want.node[1] = 0xa0;
72 	uuid_want.node[2] = 0xc9;
73 	uuid_want.node[3] = 0x1e;
74 	uuid_want.node[4] = 0x6b;
75 	uuid_want.node[5] = 0xf6;
76 
77 	uuid_from_string(uuid_str, &uuid, &status);
78 	if (status != uuid_s_ok) {
79 		printf("failed to return uuid_s_ok for '%s', got %d\n", uuid_str, status);
80 		return 1;
81 	}
82 
83 	ASSERT_EQ(uuid.time_low, uuid_want.time_low);
84 	ASSERT_EQ(uuid.time_mid, uuid_want.time_mid);
85 	ASSERT_EQ(uuid.time_hi_and_version, uuid_want.time_hi_and_version);
86 	ASSERT_EQ(uuid.clock_seq_hi_and_reserved, uuid_want.clock_seq_hi_and_reserved);
87 	ASSERT_EQ(uuid.clock_seq_low, uuid_want.clock_seq_low);
88 	ASSERT_EQ(uuid.node[0], uuid_want.node[0]);
89 	ASSERT_EQ(uuid.node[1], uuid_want.node[1]);
90 	ASSERT_EQ(uuid.node[2], uuid_want.node[2]);
91 	ASSERT_EQ(uuid.node[3], uuid_want.node[3]);
92 	ASSERT_EQ(uuid.node[4], uuid_want.node[4]);
93 	ASSERT_EQ(uuid.node[5], uuid_want.node[5]);
94 
95 	printf("ok\n");
96 	t++;
97 
98 	printf("[%d] uuid_to_string ", t);
99 	/* re-use the handrolled struct uuid from the previous test. */
100 	uuid_str_want = "f81d4fae-7dec-11d0-a765-00a0c91e6bf6";
101 
102 	uuid_to_string(&uuid, &uuid_str, &status);
103 	if (status != uuid_s_ok) {
104 		printf("failed to return uuid_s_ok, got %d\n", status);
105 		return 1;
106 	}
107 
108 	if (strcmp(uuid_str, uuid_str_want) != 0) {
109 		printf("expected '%s', got '%s'\n", uuid_str_want, uuid_str);
110 		return 1;
111 	}
112 
113 	printf("ok\n");
114 	t++;
115 
116 	printf("[%d] uuid_create_nil ", t);
117 	uuid_create_nil(&uuid, &status);
118 	if (status != uuid_s_ok) {
119 		printf("failed to return uuid_s_ok, got: %d\n", status);
120 		return 1;
121 	}
122 
123 	/*
124 	 * At this point we've done a previous test of uuid_to_string already,
125 	 * so might as well use it again for uuid_create_nil() here.
126 	 */
127 	uuid_to_string(&uuid, &uuid_str, &status);
128 	if (status != uuid_s_ok) {
129 		printf("uuid_to_string failed to return uuid_s_ok, got %d\n",
130 		    status);
131 		return 1;
132 	}
133 
134 	uuid_str_want = "00000000-0000-0000-0000-000000000000";
135 	if (strcmp(uuid_str, uuid_str_want) != 0) {
136 		printf("expected '%s', got '%s'\n", uuid_str_want, uuid_str);
137 		return 1;
138 	}
139 
140 	printf("ok\n");
141 	t++;
142 
143 	/*
144 	 * Assuming the clock of the system running the test is ahead of the one
145 	 * where this test was written, we can test uuid_create along with
146 	 * uuid_compare here.
147 	 */
148 	printf("[%d] uuid_create ", t);
149 	uuid_create(&uuid, &status);
150 	if (status != uuid_s_ok) {
151 		printf("uuid_create failed to return uuid_s_ok, got %d\n",
152 		    status);
153 		return 1;
154 	}
155 
156 	printf("ok\n");
157 	t++;
158 
159 	printf("[%d] uuid_compare ", t);
160 	/* uuid was just generated, uuid2 was generated before. */
161 	uuid_from_string(uuid_str, &uuid2, &status);
162 	rc = uuid_compare(&uuid, &uuid2, &status);
163 	if ((status != uuid_s_ok) || (rc != 1)) {
164 		printf("uuid_compare failed, expected 1 got: %d and status: %d\n",
165 		    rc, status);
166 		return 1;
167 	}
168 
169 	printf("ok\n");
170 	t++;
171 
172 	printf("[%d] uuid_equal ", t);
173 	rc = uuid_equal(&uuid, &uuid, &status);
174 	if ((status != uuid_s_ok) || (rc != 1)) {
175 		printf("uuid_compare failed, expected 1 got: %d and status: %d\n",
176 		    rc, status);
177 		return 1;
178 	}
179 
180 	printf("ok\n");
181 	t++;
182 
183 	printf("[%d] uuid_equal (nil) ", t);
184 	uuid_create_nil(&uuid, &status);
185 	rc = uuid_equal(&uuid, &uuid2, &status);
186 	if ((status != uuid_s_ok) || (rc != 1)) {
187 		printf("uuid_compare failed, expected 1 got: %d and status: %d\n",
188 		    rc, status);
189 		return 1;
190 	}
191 
192 	printf("ok\n");
193 	t++;
194 
195 	printf("[%d] uuid_hash ", t);
196 	uint16_t hash = uuid_hash(&uuid_want, &status);
197 	if ((status != uuid_s_ok) || (hash != 0x4fae)) {
198 		printf("uuid_hash failed, expected 0x4fae got: 0x%04x and status: %d\n",
199 		    hash, status);
200 		return 1;
201 	}
202 
203 	printf("ok\n");
204 	t++;
205 
206 	uuid_str_want = "f81d4fae-7dec-11d0-a765-00a0c91e6bf6";
207 	printf("[%d] uuid_enc_le ", t);
208 	uuid_from_string(uuid_str_want, &uuid, &status);
209 	/*
210 	 * Check two fields to ensure they're in the right order.
211 	 * If these two are ok, it's safe to assum the rest are too.
212 	 */
213 	uuid_enc_le(bin, &uuid);
214 	if (bin[4] != 0xec || bin[5] != 0x7d) {
215 		uuid_to_string(&uuid, &uuid_str, &status);
216 		printf("uuid_enc_le failed, expected %s got %s\n",
217 		    uuid_str_want, uuid_str);
218 		return 1;
219 	}
220 
221 	printf("ok\n");
222 	t++;
223 
224 	printf("[%d] uuid_dec_le ", t);
225 	uuid_dec_le(bin, &uuid);
226 	if (uuid_equal(&uuid, &uuid_want, &status) == 0) {
227 		uuid_to_string(&uuid, &uuid_str, &status);
228 		printf("uuid_dec_le failed, expected %s got %s\n",
229 		    uuid_str_want, uuid_str);
230 		return 1;
231 	}
232 
233 	printf("ok\n");
234 	t++;
235 
236 	printf("[%d] uuid_enc_be ", t);
237 	uuid_enc_be(bin, &uuid);
238 	if (bin[4] != 0x7d || bin[5] != 0xec) {
239 		uuid_to_string(&uuid, &uuid_str, &status);
240 		printf("uuid_enc_be failed, expected %s got %s\n",
241 		    uuid_str_want, uuid_str);
242 		return 1;
243 	}
244 
245 	printf("ok\n");
246 	t++;
247 
248 	printf("[%d] uuid_dec_be ", t);
249 	uuid_dec_be(bin, &uuid);
250 	if (uuid_equal(&uuid, &uuid_want, &status) == 0) {
251 		uuid_to_string(&uuid, &uuid_str, &status);
252 		printf("uuid_dec_be failed, expected %s got %s\n",
253 		    uuid_str_want, uuid_str);
254 		return 1;
255 	}
256 
257 	printf("ok\n");
258 	t++;
259 
260 	return 0;
261 }
262