1 /*
2 * Copyright 2020 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 #include "common/strings.h"
18
19 #include <bluetooth/log.h>
20 #include <gmock/gmock.h>
21 #include <gtest/gtest.h>
22
23 #include <array>
24 #include <memory>
25
26 namespace testing {
27
28 using bluetooth::common::BoolFromString;
29 using bluetooth::common::FromHexString;
30 using bluetooth::common::Int64FromString;
31 using bluetooth::common::StringFormat;
32 using bluetooth::common::StringFormatTime;
33 using bluetooth::common::StringFormatTimeWithMilliseconds;
34 using bluetooth::common::StringJoin;
35 using bluetooth::common::StringSplit;
36 using bluetooth::common::StringTrim;
37 using bluetooth::common::ToHexString;
38 using bluetooth::common::ToString;
39 using bluetooth::common::Uint64FromString;
40
is_arch32()41 static inline bool is_arch32() {
42 return sizeof(long) == 4;
43 }
is_arch64()44 static inline bool is_arch64() {
45 return sizeof(long) == 8;
46 }
47
48 #pragma clang diagnostic push
49 #pragma clang diagnostic ignored "-Winteger-overflow"
TEST(StringsTest,to_hex_string_from_number)50 TEST(StringsTest, to_hex_string_from_number) {
51 ASSERT_EQ(ToHexString(0), "0x00000000");
52 ASSERT_EQ(ToHexString(3), "0x00000003");
53 ASSERT_EQ(ToHexString(25), "0x00000019");
54 ASSERT_EQ(ToHexString(-25), "-0x00000019");
55 ASSERT_EQ(ToHexString(INT_MIN + 1), "-0x7fffffff");
56 ASSERT_EQ(ToHexString(INT_MAX), "0x7fffffff");
57 ASSERT_EQ(ToHexString(INT_MIN), "INT_MIN");
58 if (is_arch32()) {
59 ASSERT_EQ(ToHexString(1 + INT_MAX), "INT_MIN");
60 ASSERT_EQ(ToHexString(2 + INT_MAX), "-0x7fffffff"); // Rolled over
61 ASSERT_EQ(ToHexString(-1 - INT_MIN), "0x7fffffff"); // Rolled over
62 ASSERT_EQ(ToHexString(LONG_MAX), "0x7fffffff");
63 ASSERT_EQ(ToHexString(LONG_MAX - 1L), "0x7ffffffe");
64 ASSERT_EQ(ToHexString(LONG_MIN), "LONG_MIN");
65 ASSERT_EQ(ToHexString(LONG_MIN + 1L), "-0x7fffffff");
66 } else if (is_arch64()) {
67 ASSERT_EQ(ToHexString((signed long)INT_MIN), "-0x0000000080000000");
68 ASSERT_EQ(ToHexString(1 + INT_MAX), "INT_MIN"); // Rolled over
69 ASSERT_EQ(ToHexString(2 + INT_MAX), "-0x7fffffff"); // Rolled over
70 ASSERT_EQ(ToHexString(1L + INT_MAX), "0x0000000080000000");
71 ASSERT_EQ(ToHexString(2L + INT_MAX), "0x0000000080000001");
72 ASSERT_EQ(ToHexString(-1L + INT_MIN), "-0x0000000080000001");
73 ASSERT_EQ(ToHexString(LONG_MAX), "0x7fffffffffffffff");
74 ASSERT_EQ(ToHexString(LONG_MAX - 1L), "0x7ffffffffffffffe");
75 ASSERT_EQ(ToHexString(LONG_MIN), "LONG_MIN");
76 ASSERT_EQ(ToHexString(LONG_MIN + 1L), "-0x7fffffffffffffff");
77 } else {
78 bluetooth::log::error("Unknown architecture");
79 ASSERT_TRUE(false);
80 }
81 ASSERT_EQ(ToHexString('a'), "0x61");
82 }
83
TEST(StringsTest,to_hex_string_from_number_unsigned_int)84 TEST(StringsTest, to_hex_string_from_number_unsigned_int) {
85 ASSERT_EQ(ToHexString(0U), "0x00000000");
86 ASSERT_EQ(ToHexString(1U), "0x00000001");
87 ASSERT_EQ(ToHexString(3U), "0x00000003");
88 ASSERT_EQ(ToHexString(25U), "0x00000019");
89 ASSERT_EQ(ToHexString(UINT_MAX), "0xffffffff");
90 ASSERT_EQ(ToHexString(1U + UINT_MAX), "0x00000000"); // Rolled over
91 ASSERT_EQ(ToHexString(2U + UINT_MAX), "0x00000001"); // Rolled over
92 }
93 #pragma clang diagnostic pop
94
TEST(StringsTest,trim_string_test)95 TEST(StringsTest, trim_string_test) {
96 ASSERT_EQ(StringTrim(" aa bb"), "aa bb");
97 ASSERT_EQ(StringTrim("aa bb "), "aa bb");
98 ASSERT_EQ(StringTrim(" aa bb "), "aa bb");
99 ASSERT_EQ(StringTrim(" aa bb \n"), "aa bb");
100 ASSERT_EQ(StringTrim(" \raa bb\t \n"), "aa bb");
101 }
102
TEST(StringsTest,split_string_test)103 TEST(StringsTest, split_string_test) {
104 ASSERT_THAT(StringSplit("", ","), ElementsAre(""));
105 ASSERT_THAT(StringSplit("1,2,3", ","), ElementsAre("1", "2", "3"));
106 ASSERT_THAT(StringSplit("1,2,3", "!"), ElementsAre("1,2,3"));
107 ASSERT_THAT(StringSplit("1,2,3", ",", 2), ElementsAre("1", "2,3"));
108 ASSERT_THAT(StringSplit("a,b,", ","), ElementsAre("a", "b", ""));
109 ASSERT_THAT(StringSplit("ab,", ",", 2), ElementsAre("ab", ""));
110 ASSERT_THAT(StringSplit("ab,,", ",", 2), ElementsAre("ab", ","));
111 ASSERT_THAT(StringSplit("ab,,", ",", 1), ElementsAre("ab,,"));
112 ASSERT_THAT(StringSplit("1,,2,,3", ",,"), ElementsAre("1", "2", "3"));
113 ASSERT_THAT(StringSplit("1,,2,,3,,", ",,"), ElementsAre("1", "2", "3", ""));
114 ASSERT_THAT(StringSplit("1,,2,,3,,", ",,", 2), ElementsAre("1", "2,,3,,"));
115 ASSERT_THAT(StringSplit("1", ",,", 2), ElementsAre("1"));
116 ASSERT_DEATH({ StringSplit("1,2,3", ""); }, "delim cannot be empty");
117 }
118
TEST(StringsTest,join_string_test)119 TEST(StringsTest, join_string_test) {
120 ASSERT_THAT(StringJoin({{"1", "2", "3"}}, ","), StrEq("1,2,3"));
121 ASSERT_THAT(StringJoin({{}}, ","), StrEq(""));
122 ASSERT_THAT(StringJoin({{"1"}}, ","), StrEq("1"));
123 ASSERT_THAT(StringJoin({{"1", "2", "3"}}, ",,"), StrEq("1,,2,,3"));
124 ASSERT_THAT(StringJoin({{"1", ",", "3"}}, ",,"), StrEq("1,,,,,3"));
125 }
126
TEST(StringsTest,to_hex_string_test)127 TEST(StringsTest, to_hex_string_test) {
128 // normal
129 ASSERT_THAT(ToHexString({0x12, 0x34, 0x56, 0xab}), Eq("123456ab"));
130 // empty
131 ASSERT_THAT(ToHexString({}), Eq(""));
132 // unary
133 ASSERT_THAT(ToHexString({0x12}), Eq("12"));
134 // half
135 ASSERT_THAT(ToHexString({0x6, 0x5, 0x56, 0xb}), Eq("0605560b"));
136 // other types
137 std::array<uint8_t, 2> a = {0x12, 0x56};
138 ASSERT_THAT(ToHexString(a.begin(), a.end()), Eq("1256"));
139 std::vector<uint8_t> b = {0x34, 0x78};
140 ASSERT_THAT(ToHexString(b.begin(), b.end()), Eq("3478"));
141 }
142
TEST(StringsTest,from_hex_string_test)143 TEST(StringsTest, from_hex_string_test) {
144 // normal
145 ASSERT_THAT(FromHexString("aabbccdd1122"), Optional(ElementsAre(0xaa, 0xbb, 0xcc, 0xdd, 0x11, 0x22)));
146 // empty
147 ASSERT_THAT(FromHexString(""), Optional(IsEmpty()));
148 // unary
149 ASSERT_THAT(FromHexString("aa"), Optional(ElementsAre(0xaa)));
150 // half
151 ASSERT_THAT(FromHexString("0605560b"), Optional(ElementsAre(0x6, 0x5, 0x56, 0xb)));
152 // upper case letter
153 ASSERT_THAT(FromHexString("AABBCC"), Optional(ElementsAre(0xaa, 0xbb, 0xcc)));
154 // upper and lower case letter mixed
155 ASSERT_THAT(FromHexString("aAbbCC"), Optional(ElementsAre(0xaa, 0xbb, 0xcc)));
156 // Error: odd length
157 ASSERT_FALSE(FromHexString("0605560"));
158 // Error: non hex char
159 ASSERT_FALSE(FromHexString("060u560b"));
160 }
161
TEST(StringsTest,int64_from_and_to_string_test)162 TEST(StringsTest, int64_from_and_to_string_test) {
163 ASSERT_THAT(Int64FromString("42"), Optional(Eq(int64_t(42))));
164 ASSERT_THAT(Int64FromString("-42"), Optional(Eq(int64_t(-42))));
165 ASSERT_THAT(Int64FromString("0"), Optional(Eq(int64_t(0))));
166 ASSERT_FALSE(Int64FromString(""));
167 // only base 10 is supported
168 ASSERT_FALSE(Int64FromString("0x42ab"));
169 ASSERT_FALSE(Int64FromString("-0x42"));
170 // floating point not supported
171 ASSERT_FALSE(Int64FromString("42.0"));
172 ASSERT_FALSE(Int64FromString("-42.0"));
173 ASSERT_FALSE(Int64FromString("42abc"));
174 ASSERT_FALSE(Int64FromString(""));
175 // INT32_MAX + 1
176 ASSERT_THAT(Int64FromString("2147483648"), Optional(Eq(int64_t(2147483648))));
177 ASSERT_THAT(ToString(int64_t(2147483648)), StrEq("2147483648"));
178 // INT32_MIN - 1
179 ASSERT_THAT(Int64FromString("-2147483649"), Optional(Eq(int64_t(-2147483649))));
180 ASSERT_THAT(ToString(int64_t(-2147483649)), StrEq("-2147483649"));
181 // INT64_MAX
182 ASSERT_THAT(Int64FromString("9223372036854775807"), Optional(Eq(int64_t(9223372036854775807))));
183 ASSERT_THAT(ToString(int64_t(9223372036854775807)), StrEq("9223372036854775807"));
184 // INT64_MAX+1
185 ASSERT_FALSE(Int64FromString("9223372036854775808"));
186 // INT64_MIN
187 ASSERT_THAT(Int64FromString("-9223372036854775808"), Optional(Eq(int64_t(-9223372036854775807LL - 1))));
188 ASSERT_THAT(ToString(int64_t(-9223372036854775807LL - 1)), StrEq("-9223372036854775808"));
189 // INT64_MIN-1
190 ASSERT_FALSE(Int64FromString("-9223372036854775809"));
191 }
192
TEST(StringsTest,uint64_from_and_to_string_test)193 TEST(StringsTest, uint64_from_and_to_string_test) {
194 ASSERT_THAT(Uint64FromString("42"), Optional(Eq(uint64_t(42))));
195 ASSERT_THAT(Uint64FromString("0"), Optional(Eq(uint64_t(0))));
196 ASSERT_FALSE(Uint64FromString(""));
197 // only base 10 is supported
198 ASSERT_FALSE(Uint64FromString("0x42ab"));
199 // only positive number is supported
200 ASSERT_FALSE(Uint64FromString("-42"));
201 // floating point not supported
202 ASSERT_FALSE(Uint64FromString("42.0"));
203 ASSERT_FALSE(Uint64FromString("-42.0"));
204 ASSERT_FALSE(Uint64FromString("42abc"));
205 ASSERT_FALSE(Uint64FromString(""));
206 // UINT32_MAX + 1
207 ASSERT_THAT(Uint64FromString("4294967295"), Optional(Eq(uint64_t(4294967295))));
208 ASSERT_THAT(ToString(uint64_t(4294967295)), StrEq("4294967295"));
209 // UINT64_MAX
210 ASSERT_THAT(Uint64FromString("18446744073709551615"), Optional(Eq(uint64_t(18446744073709551615ULL))));
211 ASSERT_THAT(ToString(uint64_t(18446744073709551615ULL)), StrEq("18446744073709551615"));
212 // UINT64_MAX+1
213 ASSERT_FALSE(Uint64FromString("18446744073709551616"));
214 }
215
TEST(StringsTest,bool_from_and_to_string_test)216 TEST(StringsTest, bool_from_and_to_string_test) {
217 ASSERT_THAT(BoolFromString("true"), Optional(IsTrue()));
218 ASSERT_THAT(BoolFromString("false"), Optional(IsFalse()));
219 ASSERT_FALSE(BoolFromString("abc"));
220 ASSERT_FALSE(BoolFromString("FALSE"));
221 ASSERT_FALSE(BoolFromString("TRUE"));
222 ASSERT_FALSE(BoolFromString(""));
223 ASSERT_THAT(ToString(true), StrEq("true"));
224 ASSERT_THAT(ToString(false), StrEq("false"));
225 }
226
TEST(StringsTest,string_format_test)227 TEST(StringsTest, string_format_test) {
228 ASSERT_THAT(StringFormat("%s", "hello"), StrEq("hello"));
229 ASSERT_THAT(StringFormat("%d", 42), StrEq("42"));
230 ASSERT_THAT(StringFormat("%s world", "hello"), StrEq("hello world"));
231 ASSERT_THAT(StringFormat("%d %.1f 0x%02x", 42, 43.123, 0x8), StrEq("42 43.1 0x08"));
232 }
233
TEST(StringsTest,string_format_time_test)234 TEST(StringsTest, string_format_time_test) {
235 std::string format("%Y-%m-%d %H:%M:%S");
236 time_t then = 123456789;
237 struct std::tm tm;
238 gmtime_r(&then, &tm);
239 ASSERT_THAT(StringFormatTime(format, tm), StrEq("1973-11-29 21:33:09"));
240 }
241
TEST(StringsTest,string_format_time_with_ms_in_the_beginning_test)242 TEST(StringsTest, string_format_time_with_ms_in_the_beginning_test) {
243 std::string format("%Y-%m-%d %H:%M:%S");
244 std::time_t from_time = 0;
245 std::chrono::time_point<std::chrono::system_clock> time_point = std::chrono::system_clock::from_time_t(from_time);
246
247 ASSERT_THAT(StringFormatTimeWithMilliseconds(format, time_point, gmtime), StrEq("1970-01-01 00:00:00.000"));
248 }
249
TEST(StringsTest,string_format_time_with_ms_test)250 TEST(StringsTest, string_format_time_with_ms_test) {
251 std::string format("%Y-%m-%d %H:%M:%S");
252 std::time_t from_time1 = 1234567890;
253 std::chrono::time_point<std::chrono::system_clock> time_point1 = std::chrono::system_clock::from_time_t(from_time1);
254 std::time_t from_time2 = 1234567890;
255 std::chrono::time_point<std::chrono::system_clock> time_point2 = std::chrono::system_clock::from_time_t(from_time2);
256
257 time_point2 += std::chrono::milliseconds(1);
258
259 ASSERT_THAT(StringFormatTimeWithMilliseconds(format, time_point1, gmtime), StrEq("2009-02-13 23:31:30.000"));
260 ASSERT_THAT(StringFormatTimeWithMilliseconds(format, time_point2, gmtime), StrEq("2009-02-13 23:31:30.001"));
261 }
262
263 class ExampleClass {};
operator <<(std::ostream & os,const ExampleClass &)264 std::ostream& operator<<(std::ostream& os, const ExampleClass& /* obj */) {
265 os << "ExampleClass";
266 return os;
267 }
268
TEST(StringsTest,example_class_to_string_test)269 TEST(StringsTest, example_class_to_string_test) {
270 ExampleClass obj;
271 ASSERT_THAT(ToString(obj), StrEq("ExampleClass"));
272 }
273
274 } // namespace testing
275