Skip to content

Commit 25c94a3

Browse files
sorru94kartben
authored andcommitted
uuid: Add tests for UUID utilities
Add some tests for the UUID utilities. Signed-off-by: Simone Orru <[email protected]>
1 parent bb39048 commit 25c94a3

File tree

4 files changed

+323
-0
lines changed

4 files changed

+323
-0
lines changed

tests/lib/uuid/CMakeLists.txt

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# SPDX-License-Identifier: Apache-2.0
2+
3+
cmake_minimum_required(VERSION 3.20.0)
4+
find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE})
5+
project(uuid)
6+
7+
FILE(GLOB app_sources src/*.c)
8+
target_sources(app PRIVATE ${app_sources})

tests/lib/uuid/prj.conf

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
CONFIG_ZTEST=y
2+
CONFIG_UUID=y

tests/lib/uuid/src/main.c

Lines changed: 294 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,294 @@
1+
/*
2+
* Copyright (c) 2024, SECO Mind Srl
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*/
6+
#include <zephyr/ztest.h>
7+
#include <zephyr/sys/uuid.h>
8+
9+
#ifdef CONFIG_UUID_V4
10+
ZTEST(uuid, test_uuid_v4)
11+
{
12+
struct uuid gen_uuid = {0};
13+
14+
int result = uuid_generate_v4(&gen_uuid);
15+
/* Check version and variant fields */
16+
zassert_true(result == 0, "uuid_generate_v4 returned an error");
17+
zassert_equal(gen_uuid.val[6U] >> 4U, 4U,
18+
"Generated UUID v4 contains an incorrect 'ver' field");
19+
zassert_equal(gen_uuid.val[8U] >> 6U, 2U,
20+
"Generated UUID v4 contains an incorrect 'var' field");
21+
}
22+
#else
23+
ZTEST(uuid, test_uuid_v4)
24+
{
25+
ztest_test_skip();
26+
}
27+
#endif
28+
29+
#ifdef CONFIG_UUID_V5
30+
ZTEST(uuid, test_uuid_v5)
31+
{
32+
struct uuid namespace;
33+
int result;
34+
struct uuid gen_uuid;
35+
char uuid_str[UUID_STR_LEN];
36+
37+
result = uuid_from_string("6ba7b810-9dad-11d1-80b4-00c04fd430c8", &namespace);
38+
zassert_true(result == 0, "uuid_from_string returned an error");
39+
result = uuid_generate_v5(&namespace, "www.example.com", 15, &gen_uuid);
40+
zassert_true(result == 0, "uuid_generate_v5 returned an error");
41+
result = uuid_to_string(&gen_uuid, uuid_str);
42+
zassert_true(result == 0, "uuid_to_string returned an error");
43+
zassert_str_equal("2ed6657d-e927-568b-95e1-2665a8aea6a2", uuid_str,
44+
"uuid_str != 2ed6657d-e927-568b-95e1-2665a8aea6a2");
45+
}
46+
#else
47+
ZTEST(uuid, test_uuid_v5)
48+
{
49+
ztest_test_skip();
50+
}
51+
#endif
52+
53+
ZTEST(uuid, test_uuid_copy)
54+
{
55+
int result;
56+
const char *data_str = "6ba7b810-9dad-11d1-80b4-00c04fd430c8";
57+
struct uuid data;
58+
struct uuid out;
59+
char out_str[UUID_STR_LEN] = {0};
60+
61+
result = uuid_from_string(data_str, &data);
62+
zassert_true(result == 0, "uuid_from_string returned an error");
63+
result = uuid_copy(&data, &out);
64+
zassert_true(result == 0, "uuid_copy returned an error");
65+
result = uuid_to_string(&out, out_str);
66+
zassert_true(result == 0, "uuid_to_string returned an error");
67+
zassert_str_equal(out_str, data_str, "Expected %s, gotten: %s", data_str, out_str);
68+
}
69+
70+
ZTEST(uuid, test_uuid_from_buffer)
71+
{
72+
uint8_t uuid_buffer[UUID_SIZE] = {0x44, 0xb3, 0x5f, 0x73, 0xcf, 0xbd, 0x43, 0xb4,
73+
0x8f, 0xef, 0xca, 0x7b, 0xae, 0xa1, 0x37, 0x5f};
74+
struct uuid gen_uuid;
75+
char uuid_string[UUID_STR_LEN] = {0};
76+
const char *expected_uuid_string = "44b35f73-cfbd-43b4-8fef-ca7baea1375f";
77+
int res;
78+
79+
res = uuid_from_buffer(uuid_buffer, &gen_uuid);
80+
zassert_equal(0, res, "uuid_from_buffer returned an error");
81+
res = uuid_to_string(&gen_uuid, uuid_string);
82+
zassert_equal(0, res, "uuid_to_string returned an error");
83+
zassert_str_equal(expected_uuid_string, uuid_string, "expected %s", expected_uuid_string);
84+
}
85+
86+
ZTEST(uuid, test_uuid_from_string)
87+
{
88+
const char *first_uuid_v4_string = "44b35f73-cfbd-43b4-8fef-ca7baea1375f";
89+
const char *second_uuid_v4_string = "6f2fd4cb-94a0-41c7-8d27-864c6b13b8c0";
90+
const char *third_uuid_v4_string = "8f65dbbc-5868-4015-8523-891cc0bffa58";
91+
const char *first_uuid_v5_string = "0575a569-51eb-575c-afe4-ce7fc03bcdc5";
92+
93+
struct uuid first_uuid_v4;
94+
struct uuid second_uuid_v4;
95+
struct uuid third_uuid_v4;
96+
struct uuid first_uuid_v5;
97+
98+
uint8_t expected_first_uuid_v4_byte_array[UUID_SIZE] = {0x44, 0xb3, 0x5f, 0x73, 0xcf, 0xbd,
99+
0x43, 0xb4, 0x8f, 0xef, 0xca, 0x7b,
100+
0xae, 0xa1, 0x37, 0x5f};
101+
uint8_t expected_second_uuid_v4_byte_array[UUID_SIZE] = {0x6f, 0x2f, 0xd4, 0xcb, 0x94, 0xa0,
102+
0x41, 0xc7, 0x8d, 0x27, 0x86, 0x4c,
103+
0x6b, 0x13, 0xb8, 0xc0};
104+
uint8_t expected_third_uuid_v4_byte_array[UUID_SIZE] = {0x8f, 0x65, 0xdb, 0xbc, 0x58, 0x68,
105+
0x40, 0x15, 0x85, 0x23, 0x89, 0x1c,
106+
0xc0, 0xbf, 0xfa, 0x58};
107+
uint8_t expected_first_uuid_v5_byte_array[UUID_SIZE] = {0x05, 0x75, 0xa5, 0x69, 0x51, 0xeb,
108+
0x57, 0x5c, 0xaf, 0xe4, 0xce, 0x7f,
109+
0xc0, 0x3b, 0xcd, 0xc5};
110+
111+
int res;
112+
113+
res = uuid_from_string(first_uuid_v4_string, &first_uuid_v4);
114+
zassert_equal(0, res, "uuid_from_string returned an error");
115+
zassert_mem_equal(first_uuid_v4.val, expected_first_uuid_v4_byte_array, UUID_SIZE,
116+
"first_uuid != from expected value");
117+
118+
res = uuid_from_string(second_uuid_v4_string, &second_uuid_v4);
119+
zassert_equal(0, res, "uuid_from_string returned an error");
120+
zassert_mem_equal(second_uuid_v4.val, expected_second_uuid_v4_byte_array, UUID_SIZE,
121+
"second_uuid != from expected value");
122+
123+
res = uuid_from_string(third_uuid_v4_string, &third_uuid_v4);
124+
zassert_equal(0, res, "uuid_from_string returned an error");
125+
zassert_mem_equal(third_uuid_v4.val, expected_third_uuid_v4_byte_array, UUID_SIZE,
126+
"third_uuid != from expected value");
127+
128+
res = uuid_from_string(first_uuid_v5_string, &first_uuid_v5);
129+
zassert_equal(0, res, "uuid_from_string returned an error");
130+
zassert_mem_equal(first_uuid_v5.val, expected_first_uuid_v5_byte_array, UUID_SIZE,
131+
"uuid_v5 != from expected value");
132+
}
133+
134+
ZTEST(uuid, test_uuid_from_string_errors)
135+
{
136+
const char *uuid_string_missing_hyphen = "44b35f73-cfbd-43b4-8fef0ca7baea1375f";
137+
const char *uuid_string_non_hex_digit = "44b35f73-cfLd-43b4-8fef-ca7baea1375f";
138+
139+
int res;
140+
struct uuid gen_uuid;
141+
142+
res = uuid_from_string(NULL, &gen_uuid);
143+
zassert_equal(-EINVAL, res, "uuid_from_string returned incorrect error");
144+
145+
res = uuid_from_string(uuid_string_missing_hyphen, &gen_uuid);
146+
zassert_equal(-EINVAL, res, "uuid_from_string returned incorrect error");
147+
148+
res = uuid_from_string(uuid_string_non_hex_digit, &gen_uuid);
149+
zassert_equal(-EINVAL, res, "uuid_from_string returned incorrect error");
150+
}
151+
152+
ZTEST(uuid, test_uuid_to_buffer)
153+
{
154+
int result;
155+
const char *input_str = "44b35f73-cfbd-43b4-8fef-ca7baea1375f";
156+
struct uuid input;
157+
uint8_t buffer[UUID_SIZE] = {0};
158+
uint8_t expected_buffer[UUID_SIZE] = {0x44, 0xb3, 0x5f, 0x73, 0xcf, 0xbd, 0x43, 0xb4,
159+
0x8f, 0xef, 0xca, 0x7b, 0xae, 0xa1, 0x37, 0x5f};
160+
161+
result = uuid_from_string(input_str, &input);
162+
zassert_true(result == 0, "uuid_from_string returned an error");
163+
result = uuid_to_buffer(&input, buffer);
164+
zassert_true(result == 0, "uuid_to_buffer returned an error");
165+
zassert_mem_equal(buffer, expected_buffer, UUID_SIZE,
166+
"Incorrect buffer converted to buffer");
167+
}
168+
169+
ZTEST(uuid, test_uuid_to_string)
170+
{
171+
struct uuid first_uuid_v4 = {.val = {0x44, 0xb3, 0x5f, 0x73, 0xcf, 0xbd, 0x43, 0xb4, 0x8f,
172+
0xef, 0xca, 0x7b, 0xae, 0xa1, 0x37, 0x5f}};
173+
struct uuid second_uuid_v4 = {.val = {0x6f, 0x2f, 0xd4, 0xcb, 0x94, 0xa0, 0x41, 0xc7, 0x8d,
174+
0x27, 0x86, 0x4c, 0x6b, 0x13, 0xb8, 0xc0}};
175+
struct uuid first_uuid_v5 = {.val = {0x05, 0x75, 0xa5, 0x69, 0x51, 0xeb, 0x57, 0x5c, 0xaf,
176+
0xe4, 0xce, 0x7f, 0xc0, 0x3b, 0xcd, 0xc5}};
177+
178+
char first_uuid_v4_string[UUID_STR_LEN];
179+
char second_uuid_v4_string[UUID_STR_LEN];
180+
char first_uuid_v5_string[UUID_STR_LEN];
181+
182+
const char *expected_first_uuid_v4_string = "44b35f73-cfbd-43b4-8fef-ca7baea1375f";
183+
const char *expected_second_uuid_v4_string = "6f2fd4cb-94a0-41c7-8d27-864c6b13b8c0";
184+
const char *expected_first_uuid_v5_string = "0575a569-51eb-575c-afe4-ce7fc03bcdc5";
185+
186+
int err;
187+
188+
err = uuid_to_string(&first_uuid_v4, first_uuid_v4_string);
189+
zassert_equal(0, err, "uuid_to_string returned an error");
190+
zassert_str_equal(expected_first_uuid_v4_string, first_uuid_v4_string, "expected %s",
191+
expected_first_uuid_v4_string);
192+
193+
err = uuid_to_string(&second_uuid_v4, second_uuid_v4_string);
194+
zassert_equal(0, err, "uuid_to_string returned an error");
195+
zassert_str_equal(expected_second_uuid_v4_string, second_uuid_v4_string, "expected %s",
196+
expected_second_uuid_v4_string);
197+
198+
err = uuid_to_string(&first_uuid_v5, first_uuid_v5_string);
199+
zassert_equal(0, err, "uuid_to_string returned an error");
200+
zassert_str_equal(expected_first_uuid_v5_string, first_uuid_v5_string, "expected %s",
201+
expected_first_uuid_v5_string);
202+
}
203+
204+
#ifdef CONFIG_UUID_BASE64
205+
ZTEST(uuid, test_uuid_to_base64)
206+
{
207+
char first_uuid_v4_base64[UUID_BASE64_LEN] = {0};
208+
char second_uuid_v4_base64[UUID_BASE64_LEN] = {0};
209+
char first_uuid_v5_base64[UUID_BASE64_LEN] = {0};
210+
211+
struct uuid first_uuid_v4 = {.val = {0x44, 0xb3, 0x5f, 0x73, 0xcf, 0xbd, 0x43, 0xb4, 0x8f,
212+
0xef, 0xca, 0x7b, 0xae, 0xa1, 0x37, 0x5f}};
213+
struct uuid second_uuid_v4 = {.val = {0x6f, 0x2f, 0xd4, 0xcb, 0x94, 0xa0, 0x41, 0xc7, 0x8d,
214+
0x27, 0x86, 0x4c, 0x6b, 0x13, 0xb8, 0xc0}};
215+
struct uuid first_uuid_v5 = {.val = {0x05, 0x75, 0xa5, 0x69, 0x51, 0xeb, 0x57, 0x5c, 0xaf,
216+
0xe4, 0xce, 0x7f, 0xc0, 0x3b, 0xcd, 0xc5}};
217+
218+
char expected_first_uuid_v4_base64[] = "RLNfc8+9Q7SP78p7rqE3Xw==";
219+
char expected_second_uuid_v4_base64[] = "by/Uy5SgQceNJ4ZMaxO4wA==";
220+
char expected_first_uuid_v5_base64[] = "BXWlaVHrV1yv5M5/wDvNxQ==";
221+
222+
int err;
223+
224+
err = uuid_to_base64(&first_uuid_v4, first_uuid_v4_base64);
225+
zassert_equal(0, err, "uuid_to_base64 returned an error");
226+
zassert_str_equal(expected_first_uuid_v4_base64, first_uuid_v4_base64,
227+
"expected: '%s', gotten: '%s'", expected_first_uuid_v4_base64,
228+
first_uuid_v4_base64);
229+
230+
err = uuid_to_base64(&second_uuid_v4, second_uuid_v4_base64);
231+
zassert_equal(0, err, "uuid_to_base64 returned an error");
232+
zassert_str_equal(expected_second_uuid_v4_base64, second_uuid_v4_base64,
233+
"expected: '%s', gotten: '%s'", expected_second_uuid_v4_base64,
234+
second_uuid_v4_base64);
235+
236+
err = uuid_to_base64(&first_uuid_v5, first_uuid_v5_base64);
237+
zassert_equal(0, err, "uuid_to_base64 returned an error");
238+
zassert_str_equal(expected_first_uuid_v5_base64, first_uuid_v5_base64,
239+
"expected: '%s', gotten: '%s'", expected_first_uuid_v5_base64,
240+
first_uuid_v5_base64);
241+
}
242+
#else
243+
ZTEST(uuid, test_uuid_to_base64)
244+
{
245+
ztest_test_skip();
246+
}
247+
#endif
248+
249+
#ifdef CONFIG_UUID_BASE64
250+
ZTEST(uuid, test_uuid_to_base64url)
251+
{
252+
char first_uuid_v4_base64url[UUID_BASE64URL_LEN] = {0};
253+
char second_uuid_v4_base64url[UUID_BASE64URL_LEN] = {0};
254+
char first_uuid_v5_base64url[UUID_BASE64URL_LEN] = {0};
255+
256+
struct uuid first_uuid_v4 = {.val = {0x44, 0xb3, 0x5f, 0x73, 0xcf, 0xbd, 0x43, 0xb4, 0x8f,
257+
0xef, 0xca, 0x7b, 0xae, 0xa1, 0x37, 0x5f}};
258+
struct uuid second_uuid_v4 = {.val = {0x6f, 0x2f, 0xd4, 0xcb, 0x94, 0xa0, 0x41, 0xc7, 0x8d,
259+
0x27, 0x86, 0x4c, 0x6b, 0x13, 0xb8, 0xc0}};
260+
struct uuid first_uuid_v5 = {.val = {0x05, 0x75, 0xa5, 0x69, 0x51, 0xeb, 0x57, 0x5c, 0xaf,
261+
0xe4, 0xce, 0x7f, 0xc0, 0x3b, 0xcd, 0xc5}};
262+
263+
char expected_first_uuid_v4_base64url[] = "RLNfc8-9Q7SP78p7rqE3Xw";
264+
char expected_second_uuid_v4_base64url[] = "by_Uy5SgQceNJ4ZMaxO4wA";
265+
char expected_first_uuid_v5_base64url[] = "BXWlaVHrV1yv5M5_wDvNxQ";
266+
267+
int err;
268+
269+
err = uuid_to_base64url(&first_uuid_v4, first_uuid_v4_base64url);
270+
zassert_equal(0, err, "uuid_to_base64url returned an error");
271+
zassert_str_equal(expected_first_uuid_v4_base64url, first_uuid_v4_base64url,
272+
"expected: '%s', gotten: '%s'", expected_first_uuid_v4_base64url,
273+
first_uuid_v4_base64url);
274+
275+
err = uuid_to_base64url(&second_uuid_v4, second_uuid_v4_base64url);
276+
zassert_equal(0, err, "uuid_to_base64url returned an error");
277+
zassert_str_equal(expected_second_uuid_v4_base64url, second_uuid_v4_base64url,
278+
"expected: '%s', gotten: '%s'", expected_second_uuid_v4_base64url,
279+
second_uuid_v4_base64url);
280+
281+
err = uuid_to_base64url(&first_uuid_v5, first_uuid_v5_base64url);
282+
zassert_equal(0, err, "uuid_to_base64url returned an error");
283+
zassert_str_equal(expected_first_uuid_v5_base64url, first_uuid_v5_base64url,
284+
"expected: '%s', gotten: '%s'", expected_first_uuid_v5_base64url,
285+
first_uuid_v5_base64url);
286+
}
287+
#else
288+
ZTEST(uuid, test_uuid_to_base64url)
289+
{
290+
ztest_test_skip();
291+
}
292+
#endif
293+
294+
ZTEST_SUITE(uuid, NULL, NULL, NULL, NULL, NULL);

tests/lib/uuid/testcase.yaml

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
common:
2+
tags: uuid
3+
integration_platforms:
4+
- native_sim
5+
6+
tests:
7+
libraries.uuid.base:
8+
extra_configs:
9+
- CONFIG_UUID_V5=y
10+
- CONFIG_MBEDTLS=y
11+
- CONFIG_MBEDTLS_MD=y
12+
- CONFIG_MBEDTLS_SHA1=y
13+
- CONFIG_UUID_BASE64=y
14+
- CONFIG_BASE64=y
15+
libraries.uuid.v4:
16+
extra_configs:
17+
- CONFIG_UUID_V4=y
18+
- CONFIG_ENTROPY_GENERATOR=y
19+
filter: CONFIG_ENTROPY_HAS_DRIVER

0 commit comments

Comments
 (0)