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::StringJoin;
31 using bluetooth::common::StringSplit;
32 using bluetooth::common::StringTrim;
33 using bluetooth::common::ToHexString;
34 using bluetooth::common::ToString;
35 using bluetooth::common::Uint64FromString;
36 
TEST(StringsTest,trim_string_test)37 TEST(StringsTest, trim_string_test) {
38   ASSERT_EQ(StringTrim("  aa bb"), "aa bb");
39   ASSERT_EQ(StringTrim("aa bb "), "aa bb");
40   ASSERT_EQ(StringTrim("  aa bb "), "aa bb");
41   ASSERT_EQ(StringTrim("  aa bb \n"), "aa bb");
42   ASSERT_EQ(StringTrim("  \raa bb\t \n"), "aa bb");
43 }
44 
TEST(StringsTest,split_string_test)45 TEST(StringsTest, split_string_test) {
46   ASSERT_THAT(StringSplit("", ","), ElementsAre(""));
47   ASSERT_THAT(StringSplit("1,2,3", ","), ElementsAre("1", "2", "3"));
48   ASSERT_THAT(StringSplit("1,2,3", "!"), ElementsAre("1,2,3"));
49   ASSERT_THAT(StringSplit("1,2,3", ",", 2), ElementsAre("1", "2,3"));
50   ASSERT_THAT(StringSplit("a,b,", ","), ElementsAre("a", "b", ""));
51   ASSERT_THAT(StringSplit("ab,", ",", 2), ElementsAre("ab", ""));
52   ASSERT_THAT(StringSplit("ab,,", ",", 2), ElementsAre("ab", ","));
53   ASSERT_THAT(StringSplit("ab,,", ",", 1), ElementsAre("ab,,"));
54   ASSERT_THAT(StringSplit("1,,2,,3", ",,"), ElementsAre("1", "2", "3"));
55   ASSERT_THAT(StringSplit("1,,2,,3,,", ",,"), ElementsAre("1", "2", "3", ""));
56   ASSERT_THAT(StringSplit("1,,2,,3,,", ",,", 2), ElementsAre("1", "2,,3,,"));
57   ASSERT_THAT(StringSplit("1", ",,", 2), ElementsAre("1"));
58   ASSERT_DEATH({ StringSplit("1,2,3", ""); }, "delim cannot be empty");
59 }
60 
TEST(StringsTest,join_string_test)61 TEST(StringsTest, join_string_test) {
62   ASSERT_THAT(StringJoin({{"1", "2", "3"}}, ","), StrEq("1,2,3"));
63   ASSERT_THAT(StringJoin({{}}, ","), StrEq(""));
64   ASSERT_THAT(StringJoin({{"1"}}, ","), StrEq("1"));
65   ASSERT_THAT(StringJoin({{"1", "2", "3"}}, ",,"), StrEq("1,,2,,3"));
66   ASSERT_THAT(StringJoin({{"1", ",", "3"}}, ",,"), StrEq("1,,,,,3"));
67 }
68 
TEST(StringsTest,to_hex_string_test)69 TEST(StringsTest, to_hex_string_test) {
70   // normal
71   ASSERT_THAT(ToHexString({0x12, 0x34, 0x56, 0xab}), Eq("123456ab"));
72   // empty
73   ASSERT_THAT(ToHexString({}), Eq(""));
74   // unary
75   ASSERT_THAT(ToHexString({0x12}), Eq("12"));
76   // half
77   ASSERT_THAT(ToHexString({0x6, 0x5, 0x56, 0xb}), Eq("0605560b"));
78   // other types
79   std::array<uint8_t, 2> a = {0x12, 0x56};
80   ASSERT_THAT(ToHexString(a.begin(), a.end()), Eq("1256"));
81   std::vector<uint8_t> b = {0x34, 0x78};
82   ASSERT_THAT(ToHexString(b.begin(), b.end()), Eq("3478"));
83 }
84 
TEST(StringsTest,from_hex_string_test)85 TEST(StringsTest, from_hex_string_test) {
86   // normal
87   ASSERT_THAT(FromHexString("aabbccdd1122"), Optional(ElementsAre(0xaa, 0xbb, 0xcc, 0xdd, 0x11, 0x22)));
88   // empty
89   ASSERT_THAT(FromHexString(""), Optional(IsEmpty()));
90   // unary
91   ASSERT_THAT(FromHexString("aa"), Optional(ElementsAre(0xaa)));
92   // half
93   ASSERT_THAT(FromHexString("0605560b"), Optional(ElementsAre(0x6, 0x5, 0x56, 0xb)));
94   // upper case letter
95   ASSERT_THAT(FromHexString("AABBCC"), Optional(ElementsAre(0xaa, 0xbb, 0xcc)));
96   // upper and lower case letter mixed
97   ASSERT_THAT(FromHexString("aAbbCC"), Optional(ElementsAre(0xaa, 0xbb, 0xcc)));
98   // Error: odd length
99   ASSERT_FALSE(FromHexString("0605560"));
100   // Error: non hex char
101   ASSERT_FALSE(FromHexString("060u560b"));
102 }
103 
TEST(StringsTest,int64_from_and_to_string_test)104 TEST(StringsTest, int64_from_and_to_string_test) {
105   ASSERT_THAT(Int64FromString("42"), Optional(Eq(int64_t(42))));
106   ASSERT_THAT(Int64FromString("-42"), Optional(Eq(int64_t(-42))));
107   ASSERT_THAT(Int64FromString("0"), Optional(Eq(int64_t(0))));
108   ASSERT_FALSE(Int64FromString(""));
109   // only base 10 is supported
110   ASSERT_FALSE(Int64FromString("0x42ab"));
111   ASSERT_FALSE(Int64FromString("-0x42"));
112   // floating point not supported
113   ASSERT_FALSE(Int64FromString("42.0"));
114   ASSERT_FALSE(Int64FromString("-42.0"));
115   ASSERT_FALSE(Int64FromString("42abc"));
116   ASSERT_FALSE(Int64FromString(""));
117   // INT32_MAX + 1
118   ASSERT_THAT(Int64FromString("2147483648"), Optional(Eq(int64_t(2147483648))));
119   ASSERT_THAT(ToString(int64_t(2147483648)), StrEq("2147483648"));
120   // INT32_MIN - 1
121   ASSERT_THAT(Int64FromString("-2147483649"), Optional(Eq(int64_t(-2147483649))));
122   ASSERT_THAT(ToString(int64_t(-2147483649)), StrEq("-2147483649"));
123   // INT64_MAX
124   ASSERT_THAT(Int64FromString("9223372036854775807"), Optional(Eq(int64_t(9223372036854775807))));
125   ASSERT_THAT(ToString(int64_t(9223372036854775807)), StrEq("9223372036854775807"));
126   // INT64_MAX+1
127   ASSERT_FALSE(Int64FromString("9223372036854775808"));
128   // INT64_MIN
129   ASSERT_THAT(Int64FromString("-9223372036854775808"), Optional(Eq(int64_t(-9223372036854775807LL - 1))));
130   ASSERT_THAT(ToString(int64_t(-9223372036854775807LL - 1)), StrEq("-9223372036854775808"));
131   // INT64_MIN-1
132   ASSERT_FALSE(Int64FromString("-9223372036854775809"));
133 }
134 
TEST(StringsTest,uint64_from_and_to_string_test)135 TEST(StringsTest, uint64_from_and_to_string_test) {
136   ASSERT_THAT(Uint64FromString("42"), Optional(Eq(uint64_t(42))));
137   ASSERT_THAT(Uint64FromString("0"), Optional(Eq(uint64_t(0))));
138   ASSERT_FALSE(Uint64FromString(""));
139   // only base 10 is supported
140   ASSERT_FALSE(Uint64FromString("0x42ab"));
141   // only positive number is supported
142   ASSERT_FALSE(Uint64FromString("-42"));
143   // floating point not supported
144   ASSERT_FALSE(Uint64FromString("42.0"));
145   ASSERT_FALSE(Uint64FromString("-42.0"));
146   ASSERT_FALSE(Uint64FromString("42abc"));
147   ASSERT_FALSE(Uint64FromString(""));
148   // UINT32_MAX + 1
149   ASSERT_THAT(Uint64FromString("4294967295"), Optional(Eq(uint64_t(4294967295))));
150   ASSERT_THAT(ToString(uint64_t(4294967295)), StrEq("4294967295"));
151   // UINT64_MAX
152   ASSERT_THAT(Uint64FromString("18446744073709551615"), Optional(Eq(uint64_t(18446744073709551615ULL))));
153   ASSERT_THAT(ToString(uint64_t(18446744073709551615ULL)), StrEq("18446744073709551615"));
154   // UINT64_MAX+1
155   ASSERT_FALSE(Uint64FromString("18446744073709551616"));
156 }
157 
TEST(StringsTest,bool_from_and_to_string_test)158 TEST(StringsTest, bool_from_and_to_string_test) {
159   ASSERT_THAT(BoolFromString("true"), Optional(IsTrue()));
160   ASSERT_THAT(BoolFromString("false"), Optional(IsFalse()));
161   ASSERT_FALSE(BoolFromString("abc"));
162   ASSERT_FALSE(BoolFromString("FALSE"));
163   ASSERT_FALSE(BoolFromString("TRUE"));
164   ASSERT_FALSE(BoolFromString(""));
165   ASSERT_THAT(ToString(true), StrEq("true"));
166   ASSERT_THAT(ToString(false), StrEq("false"));
167 }
168 
169 }  // namespace testing