1 /* Copyright (c) 2023 Nordic Semiconductor ASA
2 * SPDX-License-Identifier: Apache-2.0
3 */
4
5 #include <zephyr/bluetooth/addr.h>
6 #include <zephyr/ztest.h>
7
8 ZTEST_SUITE(bt_addr_from_str, NULL, NULL, NULL, NULL, NULL);
9
ZTEST(bt_addr_from_str,test_reject_empty_string)10 ZTEST(bt_addr_from_str, test_reject_empty_string)
11 {
12 char *addr_str = "";
13 bt_addr_t a;
14
15 zassert_equal(bt_addr_from_str(addr_str, &a), -EINVAL);
16 }
17
ZTEST(bt_addr_from_str,test_reject_missing_octet)18 ZTEST(bt_addr_from_str, test_reject_missing_octet)
19 {
20 char *addr_str = "ab:ab:ab:ab:ab";
21 bt_addr_t a;
22
23 zassert_equal(bt_addr_from_str(addr_str, &a), -EINVAL);
24 }
25
ZTEST(bt_addr_from_str,test_reject_empty_octet)26 ZTEST(bt_addr_from_str, test_reject_empty_octet)
27 {
28 char *addr_str = "ab:ab:ab:ab::ab";
29 bt_addr_t a;
30
31 zassert_equal(bt_addr_from_str(addr_str, &a), -EINVAL);
32 }
33
ZTEST(bt_addr_from_str,test_reject_short_octet)34 ZTEST(bt_addr_from_str, test_reject_short_octet)
35 {
36 char *addr_str = "ab:ab:ab:ab:b:ab";
37 bt_addr_t a;
38
39 zassert_equal(bt_addr_from_str(addr_str, &a), -EINVAL);
40 }
41
ZTEST(bt_addr_from_str,test_reject_trailing_colon)42 ZTEST(bt_addr_from_str, test_reject_trailing_colon)
43 {
44 char *addr_str = "ab:ab:ab:ab:ab:ab:";
45 bt_addr_t a;
46
47 zassert_equal(bt_addr_from_str(addr_str, &a), -EINVAL);
48 }
49
ZTEST(bt_addr_from_str,test_reject_octet_colon_a)50 ZTEST(bt_addr_from_str, test_reject_octet_colon_a)
51 {
52 char *addr_str = "ab:ab:ab:ab:a::ab";
53 bt_addr_t a;
54
55 zassert_equal(bt_addr_from_str(addr_str, &a), -EINVAL);
56 }
57
ZTEST(bt_addr_from_str,test_reject_octet_colon_b)58 ZTEST(bt_addr_from_str, test_reject_octet_colon_b)
59 {
60 char *addr_str = "ab:ab:ab:ab::b:ab";
61 bt_addr_t a;
62
63 zassert_equal(bt_addr_from_str(addr_str, &a), -EINVAL);
64 }
65
ZTEST(bt_addr_from_str,test_reject_octet_space_a)66 ZTEST(bt_addr_from_str, test_reject_octet_space_a)
67 {
68 char *addr_str = "ab:ab:ab:ab: b:ab";
69 bt_addr_t a;
70
71 zassert_equal(bt_addr_from_str(addr_str, &a), -EINVAL);
72 }
73
ZTEST(bt_addr_from_str,test_reject_octet_space_b)74 ZTEST(bt_addr_from_str, test_reject_octet_space_b)
75 {
76 char *addr_str = "ab:ab:ab:ab:a :ab";
77 bt_addr_t a;
78
79 zassert_equal(bt_addr_from_str(addr_str, &a), -EINVAL);
80 }
81
ZTEST(bt_addr_from_str,test_reject_extra_space_before)82 ZTEST(bt_addr_from_str, test_reject_extra_space_before)
83 {
84 char *addr_str = " 00:00:00:00:00:00";
85 bt_addr_t a;
86
87 zassert_equal(bt_addr_from_str(addr_str, &a), -EINVAL);
88 }
89
ZTEST(bt_addr_from_str,test_reject_extra_space_after)90 ZTEST(bt_addr_from_str, test_reject_extra_space_after)
91 {
92 char *addr_str = "00:00:00:00:00:00 ";
93 bt_addr_t a;
94
95 zassert_equal(bt_addr_from_str(addr_str, &a), -EINVAL);
96 }
97
ZTEST(bt_addr_from_str,test_reject_replace_space_first)98 ZTEST(bt_addr_from_str, test_reject_replace_space_first)
99 {
100 char *addr_str = " 0:00:00:00:00:00";
101 bt_addr_t a;
102
103 zassert_equal(bt_addr_from_str(addr_str, &a), -EINVAL);
104 }
105
ZTEST(bt_addr_from_str,test_reject_replace_colon_first)106 ZTEST(bt_addr_from_str, test_reject_replace_colon_first)
107 {
108 char *addr_str = ":0:00:00:00:00:00";
109 bt_addr_t a;
110
111 zassert_equal(bt_addr_from_str(addr_str, &a), -EINVAL);
112 }
113
ZTEST(bt_addr_from_str,test_reject_non_hex)114 ZTEST(bt_addr_from_str, test_reject_non_hex)
115 {
116 char *addr_str = "00:00:00:00:g0:00";
117 bt_addr_t a;
118
119 zassert_equal(bt_addr_from_str(addr_str, &a), -EINVAL);
120 }
121
ZTEST(bt_addr_from_str,test_reject_bad_colon)122 ZTEST(bt_addr_from_str, test_reject_bad_colon)
123 {
124 char *addr_str = "00.00:00:00:00:00";
125 bt_addr_t a;
126
127 zassert_equal(bt_addr_from_str(addr_str, &a), -EINVAL);
128 }
129
ZTEST(bt_addr_from_str,test_order)130 ZTEST(bt_addr_from_str, test_order)
131 {
132 char *addr_str = "01:02:03:04:05:06";
133 bt_addr_t a;
134 bt_addr_t b = {{6, 5, 4, 3, 2, 1}};
135
136 zassert_equal(bt_addr_from_str(addr_str, &a), 0);
137 zassert_true(bt_addr_eq(&a, &b));
138 }
139
ZTEST(bt_addr_from_str,test_hex_case_equal)140 ZTEST(bt_addr_from_str, test_hex_case_equal)
141 {
142 char *addr_str_a = "ab:cd:ef:00:00:00";
143 char *addr_str_b = "AB:CD:EF:00:00:00";
144 bt_addr_t a;
145 bt_addr_t b;
146
147 zassert_equal(bt_addr_from_str(addr_str_a, &a), 0);
148 zassert_equal(bt_addr_from_str(addr_str_b, &b), 0);
149 zassert_true(bt_addr_eq(&a, &b));
150 }
151
ZTEST(bt_addr_from_str,test_hex_case_not_equal)152 ZTEST(bt_addr_from_str, test_hex_case_not_equal)
153 {
154 char *addr_str_a = "aa:aa:aa:00:00:00";
155 char *addr_str_b = "bb:bb:bb:00:00:00";
156 bt_addr_t a;
157 bt_addr_t b;
158
159 zassert_equal(bt_addr_from_str(addr_str_a, &a), 0);
160 zassert_equal(bt_addr_from_str(addr_str_b, &b), 0);
161 zassert_false(bt_addr_eq(&a, &b));
162 }
163