1 //
2 //
3 // Copyright 2015 gRPC authors.
4 //
5 // Licensed under the Apache License, Version 2.0 (the "License");
6 // you may not use this file except in compliance with the License.
7 // You may obtain a copy of the License at
8 //
9 // http://www.apache.org/licenses/LICENSE-2.0
10 //
11 // Unless required by applicable law or agreed to in writing, software
12 // distributed under the License is distributed on an "AS IS" BASIS,
13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 // See the License for the specific language governing permissions and
15 // limitations under the License.
16 //
17 //
18
19 #include "src/core/util/string.h"
20
21 #include <grpc/support/alloc.h>
22 #include <grpc/support/string_util.h>
23 #include <limits.h>
24 #include <stddef.h>
25 #include <string.h>
26
27 #include "gtest/gtest.h"
28 #include "test/core/test_util/test_config.h"
29
TEST(StringTest,Strdup)30 TEST(StringTest, Strdup) {
31 static const char* src1 = "hello world";
32 char* dst1;
33
34 dst1 = gpr_strdup(src1);
35 ASSERT_STREQ(src1, dst1);
36 gpr_free(dst1);
37
38 ASSERT_EQ(nullptr, gpr_strdup(nullptr));
39 }
40
expect_dump(const char * buf,size_t len,uint32_t flags,const char * result)41 static void expect_dump(const char* buf, size_t len, uint32_t flags,
42 const char* result) {
43 char* got = gpr_dump(buf, len, flags);
44 ASSERT_STREQ(got, result);
45 gpr_free(got);
46 }
47
TEST(StringTest,Dump)48 TEST(StringTest, Dump) {
49 expect_dump("\x01", 1, GPR_DUMP_HEX, "01");
50 expect_dump("\x01", 1, GPR_DUMP_HEX | GPR_DUMP_ASCII, "01 '.'");
51 expect_dump("\x01\x02", 2, GPR_DUMP_HEX, "01 02");
52 expect_dump("\x01\x23\x45\x67\x89\xab\xcd\xef", 8, GPR_DUMP_HEX,
53 "01 23 45 67 89 ab cd ef");
54 expect_dump("ab", 2, GPR_DUMP_HEX | GPR_DUMP_ASCII, "61 62 'ab'");
55 }
56
test_pu32_fail(const char * s)57 static void test_pu32_fail(const char* s) {
58 uint32_t out;
59 ASSERT_FALSE(gpr_parse_bytes_to_uint32(s, strlen(s), &out));
60 }
61
test_pu32_succeed(const char * s,uint32_t want)62 static void test_pu32_succeed(const char* s, uint32_t want) {
63 uint32_t out;
64 ASSERT_TRUE(gpr_parse_bytes_to_uint32(s, strlen(s), &out));
65 ASSERT_EQ(out, want);
66 }
67
TEST(StringTest,ParseUint32)68 TEST(StringTest, ParseUint32) {
69 test_pu32_fail("-1");
70 test_pu32_fail("a");
71 test_pu32_fail("");
72 test_pu32_succeed("0", 0);
73 test_pu32_succeed("1", 1);
74 test_pu32_succeed("2", 2);
75 test_pu32_succeed("3", 3);
76 test_pu32_succeed("4", 4);
77 test_pu32_succeed("5", 5);
78 test_pu32_succeed("6", 6);
79 test_pu32_succeed("7", 7);
80 test_pu32_succeed("8", 8);
81 test_pu32_succeed("9", 9);
82 test_pu32_succeed("10", 10);
83 test_pu32_succeed("11", 11);
84 test_pu32_succeed("12", 12);
85 test_pu32_succeed("13", 13);
86 test_pu32_succeed("14", 14);
87 test_pu32_succeed("15", 15);
88 test_pu32_succeed("16", 16);
89 test_pu32_succeed("17", 17);
90 test_pu32_succeed("18", 18);
91 test_pu32_succeed("19", 19);
92 test_pu32_succeed("1234567890", 1234567890);
93 test_pu32_succeed("4294967295", 4294967295u);
94 test_pu32_fail("4294967296");
95 test_pu32_fail("4294967297");
96 test_pu32_fail("4294967298");
97 test_pu32_fail("4294967299");
98 }
99
TEST(StringTest,Asprintf)100 TEST(StringTest, Asprintf) {
101 char* buf;
102 int i, j;
103
104 // Print an empty string.
105 ASSERT_EQ(gpr_asprintf(&buf, "%s", ""), 0);
106 ASSERT_EQ(buf[0], '\0');
107 gpr_free(buf);
108
109 // Print strings of various lengths.
110 for (i = 1; i < 100; i++) {
111 ASSERT_EQ(gpr_asprintf(&buf, "%0*d", i, 1), i);
112
113 // The buffer should resemble "000001\0".
114 for (j = 0; j < i - 2; j++) {
115 ASSERT_EQ(buf[j], '0');
116 }
117 ASSERT_EQ(buf[i - 1], '1');
118 ASSERT_EQ(buf[i], '\0');
119 gpr_free(buf);
120 }
121 }
122
TEST(StringTest,Strjoin)123 TEST(StringTest, Strjoin) {
124 const char* parts[4] = {"one", "two", "three", "four"};
125 size_t joined_len;
126 char* joined;
127
128 joined = gpr_strjoin(parts, 4, &joined_len);
129 ASSERT_STREQ("onetwothreefour", joined);
130 gpr_free(joined);
131
132 joined = gpr_strjoin(parts, 0, &joined_len);
133 ASSERT_STREQ("", joined);
134 gpr_free(joined);
135
136 joined = gpr_strjoin(parts, 1, &joined_len);
137 ASSERT_STREQ("one", joined);
138 gpr_free(joined);
139 }
140
TEST(StringTest,StrjoinSep)141 TEST(StringTest, StrjoinSep) {
142 const char* parts[4] = {"one", "two", "three", "four"};
143 size_t joined_len;
144 char* joined;
145
146 joined = gpr_strjoin_sep(parts, 4, ", ", &joined_len);
147 ASSERT_STREQ("one, two, three, four", joined);
148 gpr_free(joined);
149
150 // empty separator
151 joined = gpr_strjoin_sep(parts, 4, "", &joined_len);
152 ASSERT_STREQ("onetwothreefour", joined);
153 gpr_free(joined);
154
155 // degenerated case specifying zero input parts
156 joined = gpr_strjoin_sep(parts, 0, ", ", &joined_len);
157 ASSERT_STREQ("", joined);
158 gpr_free(joined);
159
160 // single part should have no separator
161 joined = gpr_strjoin_sep(parts, 1, ", ", &joined_len);
162 ASSERT_STREQ("one", joined);
163 gpr_free(joined);
164 }
165
TEST(StringTest,Ltoa)166 TEST(StringTest, Ltoa) {
167 char* str;
168 char buf[GPR_LTOA_MIN_BUFSIZE];
169
170 // zero
171 ASSERT_EQ(1, gpr_ltoa(0, buf));
172 ASSERT_STREQ("0", buf);
173
174 // positive number
175 ASSERT_EQ(3, gpr_ltoa(123, buf));
176 ASSERT_STREQ("123", buf);
177
178 // negative number
179 ASSERT_EQ(6, gpr_ltoa(-12345, buf));
180 ASSERT_STREQ("-12345", buf);
181
182 // large negative - we don't know the size of long in advance
183 ASSERT_TRUE(gpr_asprintf(&str, "%lld", (long long)LONG_MIN));
184 ASSERT_EQ(strlen(str), (size_t)gpr_ltoa(LONG_MIN, buf));
185 ASSERT_STREQ(str, buf);
186 gpr_free(str);
187 }
188
TEST(StringTest,Int64Toa)189 TEST(StringTest, Int64Toa) {
190 char buf[GPR_INT64TOA_MIN_BUFSIZE];
191
192 // zero
193 ASSERT_EQ(1, int64_ttoa(0, buf));
194 ASSERT_STREQ("0", buf);
195
196 // positive
197 ASSERT_EQ(3, int64_ttoa(123, buf));
198 ASSERT_STREQ("123", buf);
199
200 // large positive
201 ASSERT_EQ(19, int64_ttoa(9223372036854775807LL, buf));
202 ASSERT_STREQ("9223372036854775807", buf);
203
204 // large negative
205 ASSERT_EQ(20, int64_ttoa(-9223372036854775807LL - 1, buf));
206 ASSERT_STREQ("-9223372036854775808", buf);
207 }
208
TEST(StringTest,Leftpad)209 TEST(StringTest, Leftpad) {
210 char* padded;
211
212 padded = gpr_leftpad("foo", ' ', 5);
213 ASSERT_STREQ(" foo", padded);
214 gpr_free(padded);
215
216 padded = gpr_leftpad("foo", ' ', 4);
217 ASSERT_STREQ(" foo", padded);
218 gpr_free(padded);
219
220 padded = gpr_leftpad("foo", ' ', 3);
221 ASSERT_STREQ("foo", padded);
222 gpr_free(padded);
223
224 padded = gpr_leftpad("foo", ' ', 2);
225 ASSERT_STREQ("foo", padded);
226 gpr_free(padded);
227
228 padded = gpr_leftpad("foo", ' ', 1);
229 ASSERT_STREQ("foo", padded);
230 gpr_free(padded);
231
232 padded = gpr_leftpad("foo", ' ', 0);
233 ASSERT_STREQ("foo", padded);
234 gpr_free(padded);
235
236 padded = gpr_leftpad("foo", '0', 5);
237 ASSERT_STREQ("00foo", padded);
238 gpr_free(padded);
239 }
240
TEST(StringTest,Stricmp)241 TEST(StringTest, Stricmp) {
242 ASSERT_EQ(0, gpr_stricmp("hello", "hello"));
243 ASSERT_EQ(0, gpr_stricmp("HELLO", "hello"));
244 ASSERT_LT(gpr_stricmp("a", "b"), 0);
245 ASSERT_GT(gpr_stricmp("b", "a"), 0);
246 }
247
TEST(StringTest,Memrchr)248 TEST(StringTest, Memrchr) {
249 ASSERT_EQ(nullptr, gpr_memrchr(nullptr, 'a', 0));
250 ASSERT_EQ(nullptr, gpr_memrchr("", 'a', 0));
251 ASSERT_EQ(nullptr, gpr_memrchr("hello", 'b', 5));
252 ASSERT_STREQ((const char*)gpr_memrchr("hello", 'h', 5), "hello");
253 ASSERT_STREQ((const char*)gpr_memrchr("hello", 'o', 5), "o");
254 ASSERT_STREQ((const char*)gpr_memrchr("hello", 'l', 5), "lo");
255 }
256
TEST(StringTest,ParseBoolValue)257 TEST(StringTest, ParseBoolValue) {
258 bool ret;
259 ASSERT_TRUE(true == gpr_parse_bool_value("truE", &ret) && true == ret);
260 ASSERT_TRUE(true == gpr_parse_bool_value("falsE", &ret) && false == ret);
261 ASSERT_TRUE(true == gpr_parse_bool_value("1", &ret) && true == ret);
262 ASSERT_TRUE(true == gpr_parse_bool_value("0", &ret) && false == ret);
263 ASSERT_TRUE(true == gpr_parse_bool_value("Yes", &ret) && true == ret);
264 ASSERT_TRUE(true == gpr_parse_bool_value("No", &ret) && false == ret);
265 ASSERT_TRUE(true == gpr_parse_bool_value("Y", &ret) && true == ret);
266 ASSERT_TRUE(true == gpr_parse_bool_value("N", &ret) && false == ret);
267 ASSERT_EQ(false, gpr_parse_bool_value(nullptr, &ret));
268 ASSERT_EQ(false, gpr_parse_bool_value("", &ret));
269 }
270
main(int argc,char ** argv)271 int main(int argc, char** argv) {
272 grpc::testing::TestEnvironment env(&argc, argv);
273 ::testing::InitGoogleTest(&argc, argv);
274 return RUN_ALL_TESTS();
275 }
276