4 * Copyright (c) 2016 Red Hat, Inc.
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
21 #include "qemu/osdep.h"
22 #include "qemu/uuid.h"
27 bool uuidstr_is_valid
;
29 } uuid_test_data
[] = {
31 "586ece27-7f09-41e0-9e74-e901317e9d42",
33 0x58, 0x6e, 0xce, 0x27, 0x7f, 0x09, 0x41, 0xe0,
34 0x9e, 0x74, 0xe9, 0x01, 0x31, 0x7e, 0x9d, 0x42,
38 "00000000-0000-0000-0000-000000000000",
42 "0CC6C752-3961-4028-A286-C05CC616D396",
44 0x0c, 0xc6, 0xc7, 0x52, 0x39, 0x61, 0x40, 0x28,
45 0xa2, 0x86, 0xc0, 0x5c, 0xc6, 0x16, 0xd3, 0x96,
49 "0CC6C752-3961-4028-a286-c05cc616D396",
51 0x0c, 0xc6, 0xc7, 0x52, 0x39, 0x61, 0x40, 0x28,
52 0xa2, 0x86, 0xc0, 0x5c, 0xc6, 0x16, 0xd3, 0x96,
60 "abcdefgh-0000-0000-0000-000000000000",
62 "0cc6c75239614028a286c05cc616d396",
63 }, { /* '-' in wrong position */
64 "0cc6c-7523961-4028-a286-c05cc616d396",
66 "0cc6c752--3961-4028-a286-c05cc616d396",
68 "0000000000000000000000000000000000000000000000",
69 }, { /* Invalid char in the beginning */
70 ")cc6c752-3961-4028-a286-c05cc616d396",
71 }, { /* Invalid char in the beginning, in extra */
72 ")0cc6c752-3961-4028-a286-c05cc616d396",
73 }, { /* Invalid char in the middle */
74 "0cc6c752-39*1-4028-a286-c05cc616d396",
75 }, { /* Invalid char in the middle, in extra */
76 "0cc6c752-39*61-4028-a286-c05cc616d396",
77 }, { /* Invalid char in the end */
78 "0cc6c752-3961-4028-a286-c05cc616d39&",
79 }, { /* Invalid char in the end, in extra */
80 "0cc6c752-3961-4028-a286-c05cc616d396&",
81 }, { /* Short end and trailing space */
82 "0cc6c752-3961-4028-a286-c05cc616d39 ",
83 }, { /* Leading space and short end */
84 " 0cc6c752-3961-4028-a286-c05cc616d39",
88 static inline bool uuid_is_valid(QemuUUID
*uuid
)
90 return qemu_uuid_is_null(uuid
) ||
91 ((uuid
->data
[6] & 0xf0) == 0x40 && (uuid
->data
[8] & 0xc0) == 0x80);
94 static void test_uuid_generate(void)
96 QemuUUID uuid_not_null
= { { {
97 0x58, 0x6e, 0xce, 0x27, 0x7f, 0x09, 0x41, 0xe0,
98 0x9e, 0x74, 0xe9, 0x01, 0x31, 0x7e, 0x9d, 0x42
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
));
111 static void test_uuid_is_null(void)
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
118 QemuUUID uuid_not_null_2
= { { { 1 } } };
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
));
125 static void test_uuid_parse(void)
129 for (i
= 0; i
< ARRAY_SIZE(uuid_test_data
); i
++) {
131 bool is_valid
= uuid_test_data
[i
].uuidstr_is_valid
;
133 r
= qemu_uuid_parse(uuid_test_data
[i
].uuidstr
, &uuid
);
134 g_assert_cmpint(!r
, ==, 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
));
143 static void test_uuid_unparse(void)
147 for (i
= 0; i
< ARRAY_SIZE(uuid_test_data
); i
++) {
148 char out
[UUID_STR_LEN
];
150 if (!uuid_test_data
[i
].check_unparse
) {
153 qemu_uuid_unparse(&uuid_test_data
[i
].uuid
, out
);
154 g_assert_cmpstr(uuid_test_data
[i
].uuidstr
, ==, out
);
158 static void test_uuid_unparse_strdup(void)
162 for (i
= 0; i
< ARRAY_SIZE(uuid_test_data
); i
++) {
165 if (!uuid_test_data
[i
].check_unparse
) {
168 out
= qemu_uuid_unparse_strdup(&uuid_test_data
[i
].uuid
);
169 g_assert_cmpstr(uuid_test_data
[i
].uuidstr
, ==, out
);
174 static void test_uuid_hash(void)
179 for (i
= 0; i
< 100; i
++) {
180 qemu_uuid_generate(&uuid
);
181 /* Obtain the UUID hash */
182 uint32_t hash_a
= qemu_uuid_hash(&uuid
);
183 int data_idx
= g_random_int_range(0, 15);
184 /* Change a single random byte of the UUID */
185 if (uuid
.data
[data_idx
] < 0xFF) {
186 uuid
.data
[data_idx
]++;
188 uuid
.data
[data_idx
]--;
190 /* Obtain the UUID hash again */
191 uint32_t hash_b
= qemu_uuid_hash(&uuid
);
193 * Both hashes shall be different (avoid collision)
194 * for any change in the UUID fields
196 g_assert_cmpint(hash_a
, !=, hash_b
);
200 int main(int argc
, char **argv
)
202 g_test_init(&argc
, &argv
, NULL
);
203 g_test_add_func("/uuid/is_null", test_uuid_is_null
);
204 g_test_add_func("/uuid/generate", test_uuid_generate
);
205 g_test_add_func("/uuid/parse", test_uuid_parse
);
206 g_test_add_func("/uuid/unparse", test_uuid_unparse
);
207 g_test_add_func("/uuid/unparse_strdup", test_uuid_unparse_strdup
);
208 g_test_add_func("/uuid/hash", test_uuid_hash
);