xref: /openbsd/regress/lib/libc/uuid/uuidtest.c (revision 73471bf0)
1 /*	$OpenBSD: uuidtest.c,v 1.1 2021/08/31 09:57:27 jasper Exp $	*/
2 /*
3  * Copyright (c) 2021 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
27 main(int argc, char **argv)
28 {
29 	struct uuid	uuid, uuid_want;
30 	char		*uuid_str, *uuid_str_want;
31 	uint32_t	status;
32 	int		t = 1;
33 
34 	/* Test invalid input to uuid_from_string() */
35 	printf("[%d] uuid_from_string ", t);
36 	uuid_str = "6fc3134d-011d-463d-a6b4-fe1f3a5e57dX";
37 	uuid_from_string(uuid_str, &uuid, &status);
38 	if (status != uuid_s_invalid_string_uuid) {
39 		printf("failed to return uuid_s_invalid_string_uuid for '%s'\n",
40 		    uuid_str);
41 		return 1;
42 	}
43 
44 	printf("ok\n");
45 	t++;
46 
47 	/* Test valid input to uuid_from_string() */
48 	printf("[%d] uuid_from_string ", t);
49 	uuid_str = "f81d4fae-7dec-11d0-a765-00a0c91e6bf6";
50 
51 	uuid_want.time_low = 0xf81d4fae;
52 	uuid_want.time_mid = 0x7dec;
53 	uuid_want.time_hi_and_version = 0x11d0;
54 	uuid_want.clock_seq_hi_and_reserved = 0xa7;
55 	uuid_want.clock_seq_low = 0x65;
56 	uuid_want.node[0] = 0x00;
57 	uuid_want.node[1] = 0xa0;
58 	uuid_want.node[2] = 0xc9;
59 	uuid_want.node[3] = 0x1e;
60 	uuid_want.node[4] = 0x6b;
61 	uuid_want.node[5] = 0xf6;
62 
63 	uuid_from_string(uuid_str, &uuid, &status);
64 	if (status != uuid_s_ok) {
65 		printf("failed to return uuid_s_ok for '%s', got %d\n", uuid_str, status);
66 		return 1;
67 	}
68 	ASSERT_EQ(uuid.time_low, uuid_want.time_low);
69 	ASSERT_EQ(uuid.time_mid, uuid_want.time_mid);
70 	ASSERT_EQ(uuid.time_hi_and_version, uuid_want.time_hi_and_version);
71 	ASSERT_EQ(uuid.clock_seq_hi_and_reserved, uuid_want.clock_seq_hi_and_reserved);
72 	ASSERT_EQ(uuid.clock_seq_low, uuid_want.clock_seq_low);
73 	ASSERT_EQ(uuid.node[0], uuid_want.node[0]);
74 	ASSERT_EQ(uuid.node[1], uuid_want.node[1]);
75 	ASSERT_EQ(uuid.node[2], uuid_want.node[2]);
76 	ASSERT_EQ(uuid.node[3], uuid_want.node[3]);
77 	ASSERT_EQ(uuid.node[4], uuid_want.node[4]);
78 	ASSERT_EQ(uuid.node[5], uuid_want.node[5]);
79 
80 	printf("ok\n");
81 	t++;
82 
83 	printf("[%d] uuid_to_string ", t);
84 	/* re-use the handrolled struct uuid from the previous test. */
85 	uuid_str_want = "f81d4fae-7dec-11d0-a765-00a0c91e6bf6";
86 
87 	uuid_to_string(&uuid, &uuid_str, &status);
88 	if (status != uuid_s_ok) {
89 		printf("failed to return uuid_s_ok, got %d\n", status);
90 		return 1;
91 	}
92 
93 	if (strcmp(uuid_str, uuid_str_want) != 0) {
94 		printf("expected '%s', got '%s'\n", uuid_str_want, uuid_str);
95 		return 1;
96 	}
97 
98 	printf("ok\n");
99 	t++;
100 
101 	printf("[%d] uuid_create_nil ", t);
102 	uuid_create_nil(&uuid, &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 	/*
109 	 * At this point we've done a previous test of uuid_to_string already,
110 	 * so might as well use it again for uuid_create_nil() here.
111 	 */
112 	uuid_to_string(&uuid, &uuid_str, &status);
113 	if (status != uuid_s_ok) {
114 		printf("uuid_to_string failed to return uuid_s_ok, got %d\n",
115 		    status);
116 		return 1;
117 	}
118 
119 	uuid_str_want = "00000000-0000-0000-0000-000000000000";
120 	if (strcmp(uuid_str, uuid_str_want) != 0) {
121 		printf("expected '%s', got '%s'\n", uuid_str_want, uuid_str);
122 		return 1;
123 	}
124 
125 	printf("ok\n");
126 	t++;
127 
128 	return 0;
129 }
130