1 /* Copyright (c) 2022 Nordic Semiconductor ASA
2 * SPDX-License-Identifier: Apache-2.0
3 */
4
5 #undef _POSIX_C_SOURCE
6 #define _POSIX_C_SOURCE 200809L
7
8 #include <stdbool.h>
9 #include <string.h>
10
11 #include <zephyr/ztest.h>
12
13 #include <zephyr/bluetooth/uuid.h>
14
15 ZTEST_SUITE(bt_uuid_to_str, NULL, NULL, NULL, NULL, NULL);
16
is_null_terminated(char * str,size_t size)17 static bool is_null_terminated(char *str, size_t size)
18 {
19 return strnlen(str, size) < size;
20 }
21
result_is_null_terminated(const struct bt_uuid * uuid)22 static void result_is_null_terminated(const struct bt_uuid *uuid)
23 {
24 char str[BT_UUID_STR_LEN];
25
26 memset(str, 1, sizeof(str));
27 bt_uuid_to_str(uuid, str, sizeof(str));
28 zassert_true(is_null_terminated(str, sizeof(str)), "Result is not null-terminated.");
29 }
30
result_str_is(const struct bt_uuid * uuid,const char * expected_str)31 static void result_str_is(const struct bt_uuid *uuid, const char *expected_str)
32 {
33 char str[BT_UUID_STR_LEN] = {};
34
35 bt_uuid_to_str(uuid, str, sizeof(str));
36 zassume_true(is_null_terminated(str, sizeof(str)), "Result is not a string.");
37 zassert_true((strcmp(str, expected_str) == 0),
38 "Unexpected result.\n Found: %s\nExpected: %s", str, expected_str);
39 }
40
ZTEST(bt_uuid_to_str,test_null_terminated_type_16)41 ZTEST(bt_uuid_to_str, test_null_terminated_type_16)
42 {
43 result_is_null_terminated(BT_UUID_DECLARE_16(0));
44 }
45
ZTEST(bt_uuid_to_str,test_null_terminated_type_32)46 ZTEST(bt_uuid_to_str, test_null_terminated_type_32)
47 {
48 result_is_null_terminated(BT_UUID_DECLARE_32(0));
49 }
50
ZTEST(bt_uuid_to_str,test_null_terminated_type_128)51 ZTEST(bt_uuid_to_str, test_null_terminated_type_128)
52 {
53 result_is_null_terminated(BT_UUID_DECLARE_128(BT_UUID_128_ENCODE(0l, 0, 0, 0, 0ll)));
54 }
55
ZTEST(bt_uuid_to_str,test_padding_type_16)56 ZTEST(bt_uuid_to_str, test_padding_type_16)
57 {
58 result_str_is(BT_UUID_DECLARE_16(0), "0000");
59 }
60
ZTEST(bt_uuid_to_str,test_padding_type_32)61 ZTEST(bt_uuid_to_str, test_padding_type_32)
62 {
63 result_str_is(BT_UUID_DECLARE_32(0), "00000000");
64 }
65
ZTEST(bt_uuid_to_str,test_padding_type_128)66 ZTEST(bt_uuid_to_str, test_padding_type_128)
67 {
68 result_str_is(BT_UUID_DECLARE_128(BT_UUID_128_ENCODE(0l, 0, 0, 0, 0ll)),
69 "00000000-0000-0000-0000-000000000000");
70 }
71
ZTEST(bt_uuid_to_str,test_ordering_type_16)72 ZTEST(bt_uuid_to_str, test_ordering_type_16)
73 {
74 result_str_is(BT_UUID_DECLARE_16(0xabcd), "abcd");
75 }
76
77
ZTEST(bt_uuid_to_str,test_ordering_type_32)78 ZTEST(bt_uuid_to_str, test_ordering_type_32)
79 {
80 result_str_is(BT_UUID_DECLARE_32(0xabcdef12), "abcdef12");
81 }
82
ZTEST(bt_uuid_to_str,test_ordering_type_128)83 ZTEST(bt_uuid_to_str, test_ordering_type_128)
84 {
85 result_str_is(BT_UUID_DECLARE_128(BT_UUID_128_ENCODE(
86 0xabcdef12, 0x3456, 0x9999, 0x9999, 0x999999999999)),
87 "abcdef12-3456-9999-9999-999999999999");
88
89 result_str_is(BT_UUID_DECLARE_128(BT_UUID_128_ENCODE(
90 0x99999999, 0x9999, 0xabcd, 0xef12, 0x999999999999)),
91 "99999999-9999-abcd-ef12-999999999999");
92
93 result_str_is(BT_UUID_DECLARE_128(BT_UUID_128_ENCODE(
94 0x99999999, 0x9999, 0x9999, 0x9999, 0xabcdef123456)),
95 "99999999-9999-9999-9999-abcdef123456");
96 }
97