1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21#include "qemu/osdep.h"
22#include "qemu/uuid.h"
23
24struct {
25 const char *uuidstr;
26 QemuUUID uuid;
27 bool uuidstr_is_valid;
28 bool check_unparse;
29} uuid_test_data[] = {
30 {
31 "586ece27-7f09-41e0-9e74-e901317e9d42",
32 { { {
33 0x58, 0x6e, 0xce, 0x27, 0x7f, 0x09, 0x41, 0xe0,
34 0x9e, 0x74, 0xe9, 0x01, 0x31, 0x7e, 0x9d, 0x42,
35 } } },
36 true, true,
37 }, {
38 "00000000-0000-0000-0000-000000000000",
39 { },
40 true, true,
41 }, {
42 "0CC6C752-3961-4028-A286-C05CC616D396",
43 { { {
44 0x0c, 0xc6, 0xc7, 0x52, 0x39, 0x61, 0x40, 0x28,
45 0xa2, 0x86, 0xc0, 0x5c, 0xc6, 0x16, 0xd3, 0x96,
46 } } },
47 true, false,
48 }, {
49 "0CC6C752-3961-4028-a286-c05cc616D396",
50 { { {
51 0x0c, 0xc6, 0xc7, 0x52, 0x39, 0x61, 0x40, 0x28,
52 0xa2, 0x86, 0xc0, 0x5c, 0xc6, 0x16, 0xd3, 0x96,
53 } } },
54 true, false,
55 }, {
56 ""
57 }, {
58 "abc",
59 }, {
60 "abcdefgh-0000-0000-0000-000000000000",
61 }, {
62 "0cc6c75239614028a286c05cc616d396",
63 }, {
64 "0cc6c-7523961-4028-a286-c05cc616d396",
65 }, {
66 "0cc6c752--3961-4028-a286-c05cc616d396",
67 }, {
68 "0000000000000000000000000000000000000000000000",
69 }, {
70 ")cc6c752-3961-4028-a286-c05cc616d396",
71 }, {
72 ")0cc6c752-3961-4028-a286-c05cc616d396",
73 }, {
74 "0cc6c752-39*1-4028-a286-c05cc616d396",
75 }, {
76 "0cc6c752-39*61-4028-a286-c05cc616d396",
77 }, {
78 "0cc6c752-3961-4028-a286-c05cc616d39&",
79 }, {
80 "0cc6c752-3961-4028-a286-c05cc616d396&",
81 }, {
82 "0cc6c752-3961-4028-a286-c05cc616d39 ",
83 }, {
84 " 0cc6c752-3961-4028-a286-c05cc616d39",
85 },
86};
87
88static inline bool uuid_is_valid(QemuUUID *uuid)
89{
90 return qemu_uuid_is_null(uuid) ||
91 ((uuid->data[6] & 0xf0) == 0x40 && (uuid->data[8] & 0xc0) == 0x80);
92}
93
94static void test_uuid_generate(void)
95{
96 QemuUUID uuid_not_null = { { {
97 0x58, 0x6e, 0xce, 0x27, 0x7f, 0x09, 0x41, 0xe0,
98 0x9e, 0x74, 0xe9, 0x01, 0x31, 0x7e, 0x9d, 0x42
99 } } };
100 QemuUUID uuid;
101 int i;
102
103 for (i = 0; i < 100; ++i) {
104 qemu_uuid_generate(&uuid);
105 g_assert(uuid_is_valid(&uuid));
106 g_assert_false(qemu_uuid_is_null(&uuid));
107 g_assert_false(qemu_uuid_is_equal(&uuid_not_null, &uuid));
108 }
109}
110
111static void test_uuid_is_null(void)
112{
113 QemuUUID uuid_null = { };
114 QemuUUID uuid_not_null = { { {
115 0x58, 0x6e, 0xce, 0x27, 0x7f, 0x09, 0x41, 0xe0,
116 0x9e, 0x74, 0xe9, 0x01, 0x31, 0x7e, 0x9d, 0x42
117 } } };
118 QemuUUID uuid_not_null_2 = { { { 1 } } };
119
120 g_assert(qemu_uuid_is_null(&uuid_null));
121 g_assert_false(qemu_uuid_is_null(&uuid_not_null));
122 g_assert_false(qemu_uuid_is_null(&uuid_not_null_2));
123}
124
125static void test_uuid_parse(void)
126{
127 int i, r;
128
129 for (i = 0; i < ARRAY_SIZE(uuid_test_data); i++) {
130 QemuUUID uuid;
131 bool is_valid = uuid_test_data[i].uuidstr_is_valid;
132
133 r = qemu_uuid_parse(uuid_test_data[i].uuidstr, &uuid);
134 g_assert_cmpint(!r, ==, is_valid);
135 if (is_valid) {
136 g_assert_cmpint(is_valid, ==, uuid_is_valid(&uuid));
137 g_assert_cmpmem(&uuid_test_data[i].uuid, sizeof(uuid),
138 &uuid, sizeof(uuid));
139 }
140 }
141}
142
143static void test_uuid_unparse(void)
144{
145 int i;
146
147 for (i = 0; i < ARRAY_SIZE(uuid_test_data); i++) {
148 char out[37];
149
150 if (!uuid_test_data[i].check_unparse) {
151 continue;
152 }
153 qemu_uuid_unparse(&uuid_test_data[i].uuid, out);
154 g_assert_cmpstr(uuid_test_data[i].uuidstr, ==, out);
155 }
156}
157
158static void test_uuid_unparse_strdup(void)
159{
160 int i;
161
162 for (i = 0; i < ARRAY_SIZE(uuid_test_data); i++) {
163 char *out;
164
165 if (!uuid_test_data[i].check_unparse) {
166 continue;
167 }
168 out = qemu_uuid_unparse_strdup(&uuid_test_data[i].uuid);
169 g_assert_cmpstr(uuid_test_data[i].uuidstr, ==, out);
170 g_free(out);
171 }
172}
173
174int main(int argc, char **argv)
175{
176 g_test_init(&argc, &argv, NULL);
177 g_test_add_func("/uuid/is_null", test_uuid_is_null);
178 g_test_add_func("/uuid/generate", test_uuid_generate);
179 g_test_add_func("/uuid/parse", test_uuid_parse);
180 g_test_add_func("/uuid/unparse", test_uuid_unparse);
181 g_test_add_func("/uuid/unparse_strdup", test_uuid_unparse_strdup);
182
183 return g_test_run();
184}
185