xref: /openbmc/qemu/tests/unit/test-uuid.c (revision bb59f3548f0df66689b3fef676b2ac29ca00973c)
1da668aa1SThomas Huth /*
2da668aa1SThomas Huth  * QEMU UUID Library
3da668aa1SThomas Huth  *
4da668aa1SThomas Huth  * Copyright (c) 2016 Red Hat, Inc.
5da668aa1SThomas Huth  *
6da668aa1SThomas Huth  * This library is free software; you can redistribute it and/or
7da668aa1SThomas Huth  * modify it under the terms of the GNU Lesser General Public
8da668aa1SThomas Huth  * License as published by the Free Software Foundation; either
9da668aa1SThomas Huth  * version 2.1 of the License, or (at your option) any later version.
10da668aa1SThomas Huth  *
11da668aa1SThomas Huth  * This library is distributed in the hope that it will be useful,
12da668aa1SThomas Huth  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13da668aa1SThomas Huth  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14da668aa1SThomas Huth  * Lesser General Public License for more details.
15da668aa1SThomas Huth  *
16da668aa1SThomas Huth  * You should have received a copy of the GNU Lesser General Public
17da668aa1SThomas Huth  * License along with this library; if not, see <http://www.gnu.org/licenses/>.
18da668aa1SThomas Huth  *
19da668aa1SThomas Huth  */
20da668aa1SThomas Huth 
21da668aa1SThomas Huth #include "qemu/osdep.h"
22da668aa1SThomas Huth #include "qemu/uuid.h"
23da668aa1SThomas Huth 
24da668aa1SThomas Huth struct {
25da668aa1SThomas Huth     const char *uuidstr;
26da668aa1SThomas Huth     QemuUUID uuid;
27da668aa1SThomas Huth     bool uuidstr_is_valid;
28da668aa1SThomas Huth     bool check_unparse;
29da668aa1SThomas Huth } uuid_test_data[] = {
30da668aa1SThomas Huth     {    /* Normal */
31da668aa1SThomas Huth         "586ece27-7f09-41e0-9e74-e901317e9d42",
32da668aa1SThomas Huth         { { {
33da668aa1SThomas Huth              0x58, 0x6e, 0xce, 0x27, 0x7f, 0x09, 0x41, 0xe0,
34da668aa1SThomas Huth              0x9e, 0x74, 0xe9, 0x01, 0x31, 0x7e, 0x9d, 0x42,
35da668aa1SThomas Huth         } } },
36da668aa1SThomas Huth         true, true,
37da668aa1SThomas Huth     }, { /* NULL */
38da668aa1SThomas Huth         "00000000-0000-0000-0000-000000000000",
39da668aa1SThomas Huth         { },
40da668aa1SThomas Huth         true, true,
41da668aa1SThomas Huth     }, { /* Upper case */
42da668aa1SThomas Huth         "0CC6C752-3961-4028-A286-C05CC616D396",
43da668aa1SThomas Huth         { { {
44da668aa1SThomas Huth              0x0c, 0xc6, 0xc7, 0x52, 0x39, 0x61, 0x40, 0x28,
45da668aa1SThomas Huth              0xa2, 0x86, 0xc0, 0x5c, 0xc6, 0x16, 0xd3, 0x96,
46da668aa1SThomas Huth         } } },
47da668aa1SThomas Huth         true, false,
48da668aa1SThomas Huth     }, { /* Mixed case */
49da668aa1SThomas Huth         "0CC6C752-3961-4028-a286-c05cc616D396",
50da668aa1SThomas Huth         { { {
51da668aa1SThomas Huth              0x0c, 0xc6, 0xc7, 0x52, 0x39, 0x61, 0x40, 0x28,
52da668aa1SThomas Huth              0xa2, 0x86, 0xc0, 0x5c, 0xc6, 0x16, 0xd3, 0x96,
53da668aa1SThomas Huth         } } },
54da668aa1SThomas Huth         true, false,
55da668aa1SThomas Huth     }, { /* Empty */
56da668aa1SThomas Huth         ""
57da668aa1SThomas Huth     }, { /* Too short */
58da668aa1SThomas Huth         "abc",
59da668aa1SThomas Huth     }, { /* Non-hex */
60da668aa1SThomas Huth         "abcdefgh-0000-0000-0000-000000000000",
61da668aa1SThomas Huth     }, { /* No '-' */
62da668aa1SThomas Huth         "0cc6c75239614028a286c05cc616d396",
63da668aa1SThomas Huth     }, { /* '-' in wrong position */
64da668aa1SThomas Huth         "0cc6c-7523961-4028-a286-c05cc616d396",
65da668aa1SThomas Huth     }, { /* Double '-' */
66da668aa1SThomas Huth         "0cc6c752--3961-4028-a286-c05cc616d396",
67da668aa1SThomas Huth     }, { /* Too long */
68da668aa1SThomas Huth         "0000000000000000000000000000000000000000000000",
69da668aa1SThomas Huth     }, { /* Invalid char in the beginning */
70da668aa1SThomas Huth         ")cc6c752-3961-4028-a286-c05cc616d396",
71da668aa1SThomas Huth     }, { /* Invalid char in the beginning, in extra */
72da668aa1SThomas Huth         ")0cc6c752-3961-4028-a286-c05cc616d396",
73da668aa1SThomas Huth     }, { /* Invalid char in the middle */
74da668aa1SThomas Huth         "0cc6c752-39*1-4028-a286-c05cc616d396",
75da668aa1SThomas Huth     }, { /* Invalid char in the middle, in extra */
76da668aa1SThomas Huth         "0cc6c752-39*61-4028-a286-c05cc616d396",
77da668aa1SThomas Huth     }, { /* Invalid char in the end */
78da668aa1SThomas Huth         "0cc6c752-3961-4028-a286-c05cc616d39&",
79da668aa1SThomas Huth     }, { /* Invalid char in the end, in extra */
80da668aa1SThomas Huth         "0cc6c752-3961-4028-a286-c05cc616d396&",
81da668aa1SThomas Huth     }, { /* Short end and trailing space */
82da668aa1SThomas Huth         "0cc6c752-3961-4028-a286-c05cc616d39 ",
83da668aa1SThomas Huth     }, { /* Leading space and short end */
84da668aa1SThomas Huth         " 0cc6c752-3961-4028-a286-c05cc616d39",
85da668aa1SThomas Huth     },
86da668aa1SThomas Huth };
87da668aa1SThomas Huth 
uuid_is_valid(QemuUUID * uuid)88da668aa1SThomas Huth static inline bool uuid_is_valid(QemuUUID *uuid)
89da668aa1SThomas Huth {
90da668aa1SThomas Huth     return qemu_uuid_is_null(uuid) ||
91da668aa1SThomas Huth             ((uuid->data[6] & 0xf0) == 0x40 && (uuid->data[8] & 0xc0) == 0x80);
92da668aa1SThomas Huth }
93da668aa1SThomas Huth 
test_uuid_generate(void)94da668aa1SThomas Huth static void test_uuid_generate(void)
95da668aa1SThomas Huth {
96da668aa1SThomas Huth     QemuUUID uuid_not_null = { { {
97da668aa1SThomas Huth         0x58, 0x6e, 0xce, 0x27, 0x7f, 0x09, 0x41, 0xe0,
98da668aa1SThomas Huth         0x9e, 0x74, 0xe9, 0x01, 0x31, 0x7e, 0x9d, 0x42
99da668aa1SThomas Huth     } } };
100da668aa1SThomas Huth     QemuUUID uuid;
101da668aa1SThomas Huth     int i;
102da668aa1SThomas Huth 
103da668aa1SThomas Huth     for (i = 0; i < 100; ++i) {
104da668aa1SThomas Huth         qemu_uuid_generate(&uuid);
105da668aa1SThomas Huth         g_assert(uuid_is_valid(&uuid));
106da668aa1SThomas Huth         g_assert_false(qemu_uuid_is_null(&uuid));
107da668aa1SThomas Huth         g_assert_false(qemu_uuid_is_equal(&uuid_not_null, &uuid));
108da668aa1SThomas Huth     }
109da668aa1SThomas Huth }
110da668aa1SThomas Huth 
test_uuid_is_null(void)111da668aa1SThomas Huth static void test_uuid_is_null(void)
112da668aa1SThomas Huth {
113da668aa1SThomas Huth     QemuUUID uuid_null = { };
114da668aa1SThomas Huth     QemuUUID uuid_not_null = { { {
115da668aa1SThomas Huth         0x58, 0x6e, 0xce, 0x27, 0x7f, 0x09, 0x41, 0xe0,
116da668aa1SThomas Huth         0x9e, 0x74, 0xe9, 0x01, 0x31, 0x7e, 0x9d, 0x42
117da668aa1SThomas Huth     } } };
118da668aa1SThomas Huth     QemuUUID uuid_not_null_2 = { { { 1 } } };
119da668aa1SThomas Huth 
120da668aa1SThomas Huth     g_assert(qemu_uuid_is_null(&uuid_null));
121da668aa1SThomas Huth     g_assert_false(qemu_uuid_is_null(&uuid_not_null));
122da668aa1SThomas Huth     g_assert_false(qemu_uuid_is_null(&uuid_not_null_2));
123da668aa1SThomas Huth }
124da668aa1SThomas Huth 
test_uuid_parse(void)125da668aa1SThomas Huth static void test_uuid_parse(void)
126da668aa1SThomas Huth {
127da668aa1SThomas Huth     int i, r;
128da668aa1SThomas Huth 
129da668aa1SThomas Huth     for (i = 0; i < ARRAY_SIZE(uuid_test_data); i++) {
130da668aa1SThomas Huth         QemuUUID uuid;
131da668aa1SThomas Huth         bool is_valid = uuid_test_data[i].uuidstr_is_valid;
132da668aa1SThomas Huth 
133da668aa1SThomas Huth         r = qemu_uuid_parse(uuid_test_data[i].uuidstr, &uuid);
134da668aa1SThomas Huth         g_assert_cmpint(!r, ==, is_valid);
135da668aa1SThomas Huth         if (is_valid) {
136da668aa1SThomas Huth             g_assert_cmpint(is_valid, ==, uuid_is_valid(&uuid));
137da668aa1SThomas Huth             g_assert_cmpmem(&uuid_test_data[i].uuid, sizeof(uuid),
138da668aa1SThomas Huth                             &uuid, sizeof(uuid));
139da668aa1SThomas Huth         }
140da668aa1SThomas Huth     }
141da668aa1SThomas Huth }
142da668aa1SThomas Huth 
test_uuid_unparse(void)143da668aa1SThomas Huth static void test_uuid_unparse(void)
144da668aa1SThomas Huth {
145da668aa1SThomas Huth     int i;
146da668aa1SThomas Huth 
147da668aa1SThomas Huth     for (i = 0; i < ARRAY_SIZE(uuid_test_data); i++) {
148*721da039SCédric Le Goater         char out[UUID_STR_LEN];
149da668aa1SThomas Huth 
150da668aa1SThomas Huth         if (!uuid_test_data[i].check_unparse) {
151da668aa1SThomas Huth             continue;
152da668aa1SThomas Huth         }
153da668aa1SThomas Huth         qemu_uuid_unparse(&uuid_test_data[i].uuid, out);
154da668aa1SThomas Huth         g_assert_cmpstr(uuid_test_data[i].uuidstr, ==, out);
155da668aa1SThomas Huth     }
156da668aa1SThomas Huth }
157da668aa1SThomas Huth 
test_uuid_unparse_strdup(void)158da668aa1SThomas Huth static void test_uuid_unparse_strdup(void)
159da668aa1SThomas Huth {
160da668aa1SThomas Huth     int i;
161da668aa1SThomas Huth 
162da668aa1SThomas Huth     for (i = 0; i < ARRAY_SIZE(uuid_test_data); i++) {
163da668aa1SThomas Huth         char *out;
164da668aa1SThomas Huth 
165da668aa1SThomas Huth         if (!uuid_test_data[i].check_unparse) {
166da668aa1SThomas Huth             continue;
167da668aa1SThomas Huth         }
168da668aa1SThomas Huth         out = qemu_uuid_unparse_strdup(&uuid_test_data[i].uuid);
169da668aa1SThomas Huth         g_assert_cmpstr(uuid_test_data[i].uuidstr, ==, out);
170da668aa1SThomas Huth         g_free(out);
171da668aa1SThomas Huth     }
172da668aa1SThomas Huth }
173da668aa1SThomas Huth 
test_uuid_hash(void)174a6ceee59SAlbert Esteve static void test_uuid_hash(void)
175a6ceee59SAlbert Esteve {
176a6ceee59SAlbert Esteve     QemuUUID uuid;
177a6ceee59SAlbert Esteve     int i;
178a6ceee59SAlbert Esteve 
179a6ceee59SAlbert Esteve     for (i = 0; i < 100; i++) {
180a6ceee59SAlbert Esteve         qemu_uuid_generate(&uuid);
181a6ceee59SAlbert Esteve         /* Obtain the UUID hash */
182a6ceee59SAlbert Esteve         uint32_t hash_a = qemu_uuid_hash(&uuid);
183a6ceee59SAlbert Esteve         int data_idx = g_random_int_range(0, 15);
184a6ceee59SAlbert Esteve         /* Change a single random byte of the UUID */
185a6ceee59SAlbert Esteve         if (uuid.data[data_idx] < 0xFF) {
186a6ceee59SAlbert Esteve             uuid.data[data_idx]++;
187a6ceee59SAlbert Esteve         } else {
188a6ceee59SAlbert Esteve             uuid.data[data_idx]--;
189a6ceee59SAlbert Esteve         }
190a6ceee59SAlbert Esteve         /* Obtain the UUID hash again */
191a6ceee59SAlbert Esteve         uint32_t hash_b = qemu_uuid_hash(&uuid);
192a6ceee59SAlbert Esteve         /*
193a6ceee59SAlbert Esteve          * Both hashes shall be different (avoid collision)
194a6ceee59SAlbert Esteve          * for any change in the UUID fields
195a6ceee59SAlbert Esteve          */
196a6ceee59SAlbert Esteve         g_assert_cmpint(hash_a, !=, hash_b);
197a6ceee59SAlbert Esteve     }
198a6ceee59SAlbert Esteve }
199a6ceee59SAlbert Esteve 
main(int argc,char ** argv)200da668aa1SThomas Huth int main(int argc, char **argv)
201da668aa1SThomas Huth {
202da668aa1SThomas Huth     g_test_init(&argc, &argv, NULL);
203da668aa1SThomas Huth     g_test_add_func("/uuid/is_null", test_uuid_is_null);
204da668aa1SThomas Huth     g_test_add_func("/uuid/generate", test_uuid_generate);
205da668aa1SThomas Huth     g_test_add_func("/uuid/parse", test_uuid_parse);
206da668aa1SThomas Huth     g_test_add_func("/uuid/unparse", test_uuid_unparse);
207da668aa1SThomas Huth     g_test_add_func("/uuid/unparse_strdup", test_uuid_unparse_strdup);
208a6ceee59SAlbert Esteve     g_test_add_func("/uuid/hash", test_uuid_hash);
209da668aa1SThomas Huth 
210da668aa1SThomas Huth     return g_test_run();
211da668aa1SThomas Huth }
212