Skip to content

Commit 3959dd7

Browse files
committed
Bluetooth: Test: add unit tests for bt_str_to_uuid function
Add comprehensive unit tests for the new bt_str_to_uuid function to ensure proper conversion of string representations to Bluetooth UUID structures. Tests cover: - 16-bit UUID string conversion (e.g., "180d") - 32-bit UUID string conversion (e.g., "abcdef12") - 128-bit UUID string conversion with standard format - Invalid input cases (malformed strings, empty strings, wrong lengths) Includes proper validation of converted UUID values against expected results using bt_uuid_cmp(). Signed-off-by: Kai Cheng <chengkai@xiaomi.com>
1 parent daaa21e commit 3959dd7

File tree

1 file changed

+64
-0
lines changed

1 file changed

+64
-0
lines changed
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
/* Copyright (c) 2025 Xiaomi Corporation
2+
*
3+
* SPDX-License-Identifier: Apache-2.0
4+
*/
5+
6+
#include <stdint.h>
7+
#include <zephyr/ztest.h>
8+
#include <zephyr/bluetooth/uuid.h>
9+
10+
ZTEST_SUITE(bt_uuid_from_str, NULL, NULL, NULL, NULL, NULL);
11+
12+
ZTEST(bt_uuid_from_str, test_uuid_from_str_16)
13+
{
14+
struct bt_uuid_16 uuid = {0};
15+
int ret;
16+
17+
ret = bt_uuid_from_str(&uuid.uuid, "180d");
18+
zassert_true(ret == 0, "Failed to parse 16-bit UUID");
19+
zassert_true(bt_uuid_cmp(&uuid.uuid, BT_UUID_DECLARE_16(0x180d)) == 0,
20+
"Parsed UUID does not match expected 16-bit UUID");
21+
}
22+
23+
ZTEST(bt_uuid_from_str, test_uuid_from_str_32)
24+
{
25+
struct bt_uuid_32 uuid = {0};
26+
int ret;
27+
28+
ret = bt_uuid_from_str(&uuid.uuid, "abcdef12");
29+
zassert_true(ret == 0, "Failed to parse 32-bit UUID");
30+
zassert_true(bt_uuid_cmp(&uuid.uuid, BT_UUID_DECLARE_32(0xabcdef12)) == 0,
31+
"Parsed UUID does not match expected 32-bit UUID");
32+
}
33+
34+
ZTEST(bt_uuid_from_str, test_uuid_from_str_128)
35+
{
36+
struct bt_uuid_128 uuid = {0};
37+
int ret;
38+
39+
ret = bt_uuid_from_str(&uuid.uuid, "00001101-0000-1000-8000-00805f9b34fb");
40+
zassert_true(ret == 0, "Failed to parse 128-bit UUID");
41+
struct bt_uuid_128 expected =
42+
BT_UUID_INIT_128(0xfb, 0x34, 0x9b, 0x5f, 0x80, 0x00, 0x00, 0x80, 0x00, 0x10, 0x00,
43+
0x00, 0x01, 0x11, 0x00, 0x00);
44+
zassert_true(bt_uuid_cmp(&uuid.uuid, &expected.uuid) == 0,
45+
"Parsed UUID does not match expected 128-bit UUID");
46+
}
47+
48+
ZTEST(bt_uuid_from_str, test_uuid_from_str_invalid)
49+
{
50+
struct bt_uuid_128 uuid = {0};
51+
int ret;
52+
53+
ret = bt_uuid_from_str(&uuid.uuid, "not-a-uuid");
54+
zassert_true(ret < 0, "Invalid UUID string should fail");
55+
56+
ret = bt_uuid_from_str(&uuid.uuid, "");
57+
zassert_true(ret < 0, "Empty string should fail");
58+
59+
ret = bt_uuid_from_str(&uuid.uuid, "123");
60+
zassert_true(ret < 0, "Too short string should fail");
61+
62+
ret = bt_uuid_from_str(&uuid.uuid, "00001101-0000-1000-8000-00805f9b34fb00");
63+
zassert_true(ret < 0, "Too long 128-bit string should fail");
64+
}

0 commit comments

Comments
 (0)