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 <gmock/gmock.h>
20 #include <gtest/gtest.h>
21 
22 #include <array>
23 #include <memory>
24 
25 namespace testing {
26 
27 using bluetooth::common::BoolFromString;
28 using bluetooth::common::FromHexString;
29 using bluetooth::common::Int64FromString;
30 using bluetooth::common::StringFormat;
31 using bluetooth::common::StringFormatTime;
32 using bluetooth::common::StringFormatTimeWithMilliseconds;
33 using bluetooth::common::StringJoin;
34 using bluetooth::common::StringSplit;
35 using bluetooth::common::StringTrim;
36 using bluetooth::common::ToHexString;
37 using bluetooth::common::ToString;
38 using bluetooth::common::Uint64FromString;
39 
is_arch32()40 static inline bool is_arch32() {
41   return sizeof(long) == 4;
42 }
is_arch64()43 static inline bool is_arch64() {
44   return sizeof(long) == 8;
45 }
46 
TEST(StringsTest,to_hex_string_from_number)47 TEST(StringsTest, to_hex_string_from_number) {
48   ASSERT_EQ(ToHexString(0), "0x00000000");
49   ASSERT_EQ(ToHexString(3), "0x00000003");
50   ASSERT_EQ(ToHexString(25), "0x00000019");
51   ASSERT_EQ(ToHexString(-25), "-0x00000019");
52   ASSERT_EQ(ToHexString(INT_MIN + 1), "-0x7fffffff");
53   ASSERT_EQ(ToHexString(INT_MAX), "0x7fffffff");
54   ASSERT_EQ(ToHexString(INT_MIN), "INT_MIN");
55   if (is_arch32()) {
56     ASSERT_EQ(ToHexString(1 + INT_MAX), "INT_MIN");
57     ASSERT_EQ(ToHexString(2 + INT_MAX), "-0x7fffffff");  // Rolled over
58     ASSERT_EQ(ToHexString(-1 - INT_MIN), "0x7fffffff");  // Rolled over
59     ASSERT_EQ(ToHexString(LONG_MAX), "0x7fffffff");
60     ASSERT_EQ(ToHexString(LONG_MAX - 1L), "0x7ffffffe");
61     ASSERT_EQ(ToHexString(LONG_MIN), "LONG_MIN");
62     ASSERT_EQ(ToHexString(LONG_MIN + 1L), "-0x7fffffff");
63   } else if (is_arch64()) {
64     ASSERT_EQ(ToHexString((signed long)INT_MIN), "-0x0000000080000000");
65     ASSERT_EQ(ToHexString(1 + INT_MAX), "INT_MIN");      // Rolled over
66     ASSERT_EQ(ToHexString(2 + INT_MAX), "-0x7fffffff");  // Rolled over
67     ASSERT_EQ(ToHexString(1L + INT_MAX), "0x0000000080000000");
68     ASSERT_EQ(ToHexString(2L + INT_MAX), "0x0000000080000001");
69     ASSERT_EQ(ToHexString(-1L + INT_MIN), "-0x0000000080000001");
70     ASSERT_EQ(ToHexString(LONG_MAX), "0x7fffffffffffffff");
71     ASSERT_EQ(ToHexString(LONG_MAX - 1L), "0x7ffffffffffffffe");
72     ASSERT_EQ(ToHexString(LONG_MIN), "LONG_MIN");
73     ASSERT_EQ(ToHexString(LONG_MIN + 1L), "-0x7fffffffffffffff");
74   } else {
75     LOG_ERROR("Unknown architecture");
76     ASSERT_TRUE(false);
77   }
78   ASSERT_EQ(ToHexString('a'), "0x61");
79 }
80 
TEST(StringsTest,trim_string_test)81 TEST(StringsTest, trim_string_test) {
82   ASSERT_EQ(StringTrim("  aa bb"), "aa bb");
83   ASSERT_EQ(StringTrim("aa bb "), "aa bb");
84   ASSERT_EQ(StringTrim("  aa bb "), "aa bb");
85   ASSERT_EQ(StringTrim("  aa bb \n"), "aa bb");
86   ASSERT_EQ(StringTrim("  \raa bb\t \n"), "aa bb");
87 }
88 
TEST(StringsTest,split_string_test)89 TEST(StringsTest, split_string_test) {
90   ASSERT_THAT(StringSplit("", ","), ElementsAre(""));
91   ASSERT_THAT(StringSplit("1,2,3", ","), ElementsAre("1", "2", "3"));
92   ASSERT_THAT(StringSplit("1,2,3", "!"), ElementsAre("1,2,3"));
93   ASSERT_THAT(StringSplit("1,2,3", ",", 2), ElementsAre("1", "2,3"));
94   ASSERT_THAT(StringSplit("a,b,", ","), ElementsAre("a", "b", ""));
95   ASSERT_THAT(StringSplit("ab,", ",", 2), ElementsAre("ab", ""));
96   ASSERT_THAT(StringSplit("ab,,", ",", 2), ElementsAre("ab", ","));
97   ASSERT_THAT(StringSplit("ab,,", ",", 1), ElementsAre("ab,,"));
98   ASSERT_THAT(StringSplit("1,,2,,3", ",,"), ElementsAre("1", "2", "3"));
99   ASSERT_THAT(StringSplit("1,,2,,3,,", ",,"), ElementsAre("1", "2", "3", ""));
100   ASSERT_THAT(StringSplit("1,,2,,3,,", ",,", 2), ElementsAre("1", "2,,3,,"));
101   ASSERT_THAT(StringSplit("1", ",,", 2), ElementsAre("1"));
102   ASSERT_DEATH({ StringSplit("1,2,3", ""); }, "delim cannot be empty");
103 }
104 
TEST(StringsTest,join_string_test)105 TEST(StringsTest, join_string_test) {
106   ASSERT_THAT(StringJoin({{"1", "2", "3"}}, ","), StrEq("1,2,3"));
107   ASSERT_THAT(StringJoin({{}}, ","), StrEq(""));
108   ASSERT_THAT(StringJoin({{"1"}}, ","), StrEq("1"));
109   ASSERT_THAT(StringJoin({{"1", "2", "3"}}, ",,"), StrEq("1,,2,,3"));
110   ASSERT_THAT(StringJoin({{"1", ",", "3"}}, ",,"), StrEq("1,,,,,3"));
111 }
112 
TEST(StringsTest,to_hex_string_test)113 TEST(StringsTest, to_hex_string_test) {
114   // normal
115   ASSERT_THAT(ToHexString({0x12, 0x34, 0x56, 0xab}), Eq("123456ab"));
116   // empty
117   ASSERT_THAT(ToHexString({}), Eq(""));
118   // unary
119   ASSERT_THAT(ToHexString({0x12}), Eq("12"));
120   // half
121   ASSERT_THAT(ToHexString({0x6, 0x5, 0x56, 0xb}), Eq("0605560b"));
122   // other types
123   std::array<uint8_t, 2> a = {0x12, 0x56};
124   ASSERT_THAT(ToHexString(a.begin(), a.end()), Eq("1256"));
125   std::vector<uint8_t> b = {0x34, 0x78};
126   ASSERT_THAT(ToHexString(b.begin(), b.end()), Eq("3478"));
127 }
128 
TEST(StringsTest,from_hex_string_test)129 TEST(StringsTest, from_hex_string_test) {
130   // normal
131   ASSERT_THAT(FromHexString("aabbccdd1122"), Optional(ElementsAre(0xaa, 0xbb, 0xcc, 0xdd, 0x11, 0x22)));
132   // empty
133   ASSERT_THAT(FromHexString(""), Optional(IsEmpty()));
134   // unary
135   ASSERT_THAT(FromHexString("aa"), Optional(ElementsAre(0xaa)));
136   // half
137   ASSERT_THAT(FromHexString("0605560b"), Optional(ElementsAre(0x6, 0x5, 0x56, 0xb)));
138   // upper case letter
139   ASSERT_THAT(FromHexString("AABBCC"), Optional(ElementsAre(0xaa, 0xbb, 0xcc)));
140   // upper and lower case letter mixed
141   ASSERT_THAT(FromHexString("aAbbCC"), Optional(ElementsAre(0xaa, 0xbb, 0xcc)));
142   // Error: odd length
143   ASSERT_FALSE(FromHexString("0605560"));
144   // Error: non hex char
145   ASSERT_FALSE(FromHexString("060u560b"));
146 }
147 
TEST(StringsTest,int64_from_and_to_string_test)148 TEST(StringsTest, int64_from_and_to_string_test) {
149   ASSERT_THAT(Int64FromString("42"), Optional(Eq(int64_t(42))));
150   ASSERT_THAT(Int64FromString("-42"), Optional(Eq(int64_t(-42))));
151   ASSERT_THAT(Int64FromString("0"), Optional(Eq(int64_t(0))));
152   ASSERT_FALSE(Int64FromString(""));
153   // only base 10 is supported
154   ASSERT_FALSE(Int64FromString("0x42ab"));
155   ASSERT_FALSE(Int64FromString("-0x42"));
156   // floating point not supported
157   ASSERT_FALSE(Int64FromString("42.0"));
158   ASSERT_FALSE(Int64FromString("-42.0"));
159   ASSERT_FALSE(Int64FromString("42abc"));
160   ASSERT_FALSE(Int64FromString(""));
161   // INT32_MAX + 1
162   ASSERT_THAT(Int64FromString("2147483648"), Optional(Eq(int64_t(2147483648))));
163   ASSERT_THAT(ToString(int64_t(2147483648)), StrEq("2147483648"));
164   // INT32_MIN - 1
165   ASSERT_THAT(Int64FromString("-2147483649"), Optional(Eq(int64_t(-2147483649))));
166   ASSERT_THAT(ToString(int64_t(-2147483649)), StrEq("-2147483649"));
167   // INT64_MAX
168   ASSERT_THAT(Int64FromString("9223372036854775807"), Optional(Eq(int64_t(9223372036854775807))));
169   ASSERT_THAT(ToString(int64_t(9223372036854775807)), StrEq("9223372036854775807"));
170   // INT64_MAX+1
171   ASSERT_FALSE(Int64FromString("9223372036854775808"));
172   // INT64_MIN
173   ASSERT_THAT(Int64FromString("-9223372036854775808"), Optional(Eq(int64_t(-9223372036854775807LL - 1))));
174   ASSERT_THAT(ToString(int64_t(-9223372036854775807LL - 1)), StrEq("-9223372036854775808"));
175   // INT64_MIN-1
176   ASSERT_FALSE(Int64FromString("-9223372036854775809"));
177 }
178 
TEST(StringsTest,uint64_from_and_to_string_test)179 TEST(StringsTest, uint64_from_and_to_string_test) {
180   ASSERT_THAT(Uint64FromString("42"), Optional(Eq(uint64_t(42))));
181   ASSERT_THAT(Uint64FromString("0"), Optional(Eq(uint64_t(0))));
182   ASSERT_FALSE(Uint64FromString(""));
183   // only base 10 is supported
184   ASSERT_FALSE(Uint64FromString("0x42ab"));
185   // only positive number is supported
186   ASSERT_FALSE(Uint64FromString("-42"));
187   // floating point not supported
188   ASSERT_FALSE(Uint64FromString("42.0"));
189   ASSERT_FALSE(Uint64FromString("-42.0"));
190   ASSERT_FALSE(Uint64FromString("42abc"));
191   ASSERT_FALSE(Uint64FromString(""));
192   // UINT32_MAX + 1
193   ASSERT_THAT(Uint64FromString("4294967295"), Optional(Eq(uint64_t(4294967295))));
194   ASSERT_THAT(ToString(uint64_t(4294967295)), StrEq("4294967295"));
195   // UINT64_MAX
196   ASSERT_THAT(Uint64FromString("18446744073709551615"), Optional(Eq(uint64_t(18446744073709551615ULL))));
197   ASSERT_THAT(ToString(uint64_t(18446744073709551615ULL)), StrEq("18446744073709551615"));
198   // UINT64_MAX+1
199   ASSERT_FALSE(Uint64FromString("18446744073709551616"));
200 }
201 
TEST(StringsTest,bool_from_and_to_string_test)202 TEST(StringsTest, bool_from_and_to_string_test) {
203   ASSERT_THAT(BoolFromString("true"), Optional(IsTrue()));
204   ASSERT_THAT(BoolFromString("false"), Optional(IsFalse()));
205   ASSERT_FALSE(BoolFromString("abc"));
206   ASSERT_FALSE(BoolFromString("FALSE"));
207   ASSERT_FALSE(BoolFromString("TRUE"));
208   ASSERT_FALSE(BoolFromString(""));
209   ASSERT_THAT(ToString(true), StrEq("true"));
210   ASSERT_THAT(ToString(false), StrEq("false"));
211 }
212 
TEST(StringsTest,string_format_test)213 TEST(StringsTest, string_format_test) {
214   ASSERT_THAT(StringFormat("%s", "hello"), StrEq("hello"));
215   ASSERT_THAT(StringFormat("%d", 42), StrEq("42"));
216   ASSERT_THAT(StringFormat("%s world", "hello"), StrEq("hello world"));
217   ASSERT_THAT(StringFormat("%d %.1f 0x%02x", 42, 43.123, 0x8), StrEq("42 43.1 0x08"));
218 }
219 
TEST(StringsTest,string_format_time_test)220 TEST(StringsTest, string_format_time_test) {
221   std::string format("%Y-%m-%d %H:%M:%S");
222   time_t then = 123456789;
223   struct std::tm tm;
224   gmtime_r(&then, &tm);
225   ASSERT_THAT(StringFormatTime(format, tm), StrEq("1973-11-29 21:33:09"));
226 }
227 
TEST(StringsTest,string_format_time_with_ms_in_the_beginning_test)228 TEST(StringsTest, string_format_time_with_ms_in_the_beginning_test) {
229   std::string format("%Y-%m-%d %H:%M:%S");
230   std::time_t from_time = 0;
231   std::chrono::time_point<std::chrono::system_clock> time_point = std::chrono::system_clock::from_time_t(from_time);
232 
233   ASSERT_THAT(StringFormatTimeWithMilliseconds(format, time_point, gmtime), StrEq("1970-01-01 00:00:00.000"));
234 }
235 
TEST(StringsTest,string_format_time_with_ms_test)236 TEST(StringsTest, string_format_time_with_ms_test) {
237   std::string format("%Y-%m-%d %H:%M:%S");
238   std::time_t from_time1 = 1234567890;
239   std::chrono::time_point<std::chrono::system_clock> time_point1 = std::chrono::system_clock::from_time_t(from_time1);
240   std::time_t from_time2 = 1234567890;
241   std::chrono::time_point<std::chrono::system_clock> time_point2 = std::chrono::system_clock::from_time_t(from_time2);
242 
243   time_point2 += std::chrono::milliseconds(1);
244 
245   ASSERT_THAT(StringFormatTimeWithMilliseconds(format, time_point1, gmtime), StrEq("2009-02-13 23:31:30.000"));
246   ASSERT_THAT(StringFormatTimeWithMilliseconds(format, time_point2, gmtime), StrEq("2009-02-13 23:31:30.001"));
247 }
248 
249 }  // namespace testing
250