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/lib/slice/slice_string_helpers.h"
20
21 #include <limits.h>
22 #include <stddef.h>
23 #include <stdlib.h>
24 #include <string.h>
25
26 #include <grpc/support/alloc.h>
27 #include <grpc/support/log.h>
28 #include <grpc/support/string_util.h>
29
30 #include "src/core/lib/gpr/string.h"
31 #include "test/core/util/test_config.h"
32
33 #define LOG_TEST_NAME(x) gpr_log(GPR_INFO, "%s", x)
34
expect_slice_dump(grpc_slice slice,uint32_t flags,const char * result)35 static void expect_slice_dump(grpc_slice slice, uint32_t flags,
36 const char* result) {
37 char* got = grpc_dump_slice(slice, flags);
38 GPR_ASSERT(0 == strcmp(got, result));
39 gpr_free(got);
40 grpc_slice_unref(slice);
41 }
42
test_dump_slice(void)43 static void test_dump_slice(void) {
44 static const char* text = "HELLO WORLD!";
45 static const char* long_text =
46 "It was a bright cold day in April, and the clocks were striking "
47 "thirteen. Winston Smith, his chin nuzzled into his breast in an effort "
48 "to escape the vile wind, slipped quickly through the glass doors of "
49 "Victory Mansions, though not quickly enough to prevent a swirl of "
50 "gritty dust from entering along with him.";
51
52 LOG_TEST_NAME("test_dump_slice");
53
54 expect_slice_dump(grpc_slice_from_copied_string(text), GPR_DUMP_ASCII, text);
55 expect_slice_dump(grpc_slice_from_copied_string(long_text), GPR_DUMP_ASCII,
56 long_text);
57 expect_slice_dump(grpc_slice_from_copied_buffer("\x01", 1), GPR_DUMP_HEX,
58 "01");
59 expect_slice_dump(grpc_slice_from_copied_buffer("\x01", 1),
60 GPR_DUMP_HEX | GPR_DUMP_ASCII, "01 '.'");
61 }
62
test_strsplit(void)63 static void test_strsplit(void) {
64 grpc_slice_buffer* parts;
65 grpc_slice str;
66
67 LOG_TEST_NAME("test_strsplit");
68
69 parts =
70 static_cast<grpc_slice_buffer*>(gpr_malloc(sizeof(grpc_slice_buffer)));
71 grpc_slice_buffer_init(parts);
72
73 str = grpc_slice_from_copied_string("one, two, three, four");
74 grpc_slice_split(str, ", ", parts);
75 GPR_ASSERT(4 == parts->count);
76 GPR_ASSERT(0 == grpc_slice_str_cmp(parts->slices[0], "one"));
77 GPR_ASSERT(0 == grpc_slice_str_cmp(parts->slices[1], "two"));
78 GPR_ASSERT(0 == grpc_slice_str_cmp(parts->slices[2], "three"));
79 GPR_ASSERT(0 == grpc_slice_str_cmp(parts->slices[3], "four"));
80 grpc_slice_buffer_reset_and_unref(parts);
81 grpc_slice_unref(str);
82
83 /* separator not present in string */
84 str = grpc_slice_from_copied_string("one two three four");
85 grpc_slice_split(str, ", ", parts);
86 GPR_ASSERT(1 == parts->count);
87 GPR_ASSERT(0 == grpc_slice_str_cmp(parts->slices[0], "one two three four"));
88 grpc_slice_buffer_reset_and_unref(parts);
89 grpc_slice_unref(str);
90
91 /* separator at the end */
92 str = grpc_slice_from_copied_string("foo,");
93 grpc_slice_split(str, ",", parts);
94 GPR_ASSERT(2 == parts->count);
95 GPR_ASSERT(0 == grpc_slice_str_cmp(parts->slices[0], "foo"));
96 GPR_ASSERT(0 == grpc_slice_str_cmp(parts->slices[1], ""));
97 grpc_slice_buffer_reset_and_unref(parts);
98 grpc_slice_unref(str);
99
100 /* separator at the beginning */
101 str = grpc_slice_from_copied_string(",foo");
102 grpc_slice_split(str, ",", parts);
103 GPR_ASSERT(2 == parts->count);
104 GPR_ASSERT(0 == grpc_slice_str_cmp(parts->slices[0], ""));
105 GPR_ASSERT(0 == grpc_slice_str_cmp(parts->slices[1], "foo"));
106 grpc_slice_buffer_reset_and_unref(parts);
107 grpc_slice_unref(str);
108
109 /* standalone separator */
110 str = grpc_slice_from_copied_string(",");
111 grpc_slice_split(str, ",", parts);
112 GPR_ASSERT(2 == parts->count);
113 GPR_ASSERT(0 == grpc_slice_str_cmp(parts->slices[0], ""));
114 GPR_ASSERT(0 == grpc_slice_str_cmp(parts->slices[1], ""));
115 grpc_slice_buffer_reset_and_unref(parts);
116 grpc_slice_unref(str);
117
118 /* empty input */
119 str = grpc_slice_from_copied_string("");
120 grpc_slice_split(str, ", ", parts);
121 GPR_ASSERT(1 == parts->count);
122 GPR_ASSERT(0 == grpc_slice_str_cmp(parts->slices[0], ""));
123 grpc_slice_buffer_reset_and_unref(parts);
124 grpc_slice_unref(str);
125
126 grpc_slice_buffer_destroy(parts);
127 gpr_free(parts);
128 }
129
test_strsplit_nospace(void)130 static void test_strsplit_nospace(void) {
131 grpc_slice_buffer* parts;
132 grpc_slice str;
133
134 LOG_TEST_NAME("test_strsplit_nospace");
135
136 parts =
137 static_cast<grpc_slice_buffer*>(gpr_malloc(sizeof(grpc_slice_buffer)));
138 grpc_slice_buffer_init(parts);
139
140 str = grpc_slice_from_copied_string("one ,two, three , four");
141 grpc_slice_split_without_space(str, ",", parts);
142 GPR_ASSERT(4 == parts->count);
143 GPR_ASSERT(0 == grpc_slice_str_cmp(parts->slices[0], "one"));
144 GPR_ASSERT(0 == grpc_slice_str_cmp(parts->slices[1], "two"));
145 GPR_ASSERT(0 == grpc_slice_str_cmp(parts->slices[2], "three"));
146 GPR_ASSERT(0 == grpc_slice_str_cmp(parts->slices[3], "four"));
147 grpc_slice_buffer_reset_and_unref(parts);
148 grpc_slice_unref(str);
149
150 /* separator not present in string */
151 str = grpc_slice_from_copied_string("one two three four ");
152 grpc_slice_split_without_space(str, ",", parts);
153 GPR_ASSERT(1 == parts->count);
154 GPR_ASSERT(0 == grpc_slice_str_cmp(parts->slices[0], "one two three four"));
155 grpc_slice_buffer_reset_and_unref(parts);
156 grpc_slice_unref(str);
157
158 /* separator at the end */
159 str = grpc_slice_from_copied_string("foo,");
160 grpc_slice_split_without_space(str, ",", parts);
161 GPR_ASSERT(2 == parts->count);
162 GPR_ASSERT(0 == grpc_slice_str_cmp(parts->slices[0], "foo"));
163 GPR_ASSERT(0 == grpc_slice_str_cmp(parts->slices[1], ""));
164 grpc_slice_buffer_reset_and_unref(parts);
165 grpc_slice_unref(str);
166
167 /* separator at the beginning */
168 str = grpc_slice_from_copied_string(" , foo");
169 grpc_slice_split_without_space(str, ",", parts);
170 GPR_ASSERT(2 == parts->count);
171 GPR_ASSERT(0 == grpc_slice_str_cmp(parts->slices[0], ""));
172 GPR_ASSERT(0 == grpc_slice_str_cmp(parts->slices[1], "foo"));
173 grpc_slice_buffer_reset_and_unref(parts);
174 grpc_slice_unref(str);
175
176 /* standalone separator */
177 str = grpc_slice_from_copied_string(", ");
178 grpc_slice_split_without_space(str, ", ", parts);
179 GPR_ASSERT(2 == parts->count);
180 GPR_ASSERT(0 == grpc_slice_str_cmp(parts->slices[0], ""));
181 GPR_ASSERT(0 == grpc_slice_str_cmp(parts->slices[1], ""));
182 grpc_slice_buffer_reset_and_unref(parts);
183 grpc_slice_unref(str);
184
185 /* empty input */
186 str = grpc_slice_from_copied_string("");
187 grpc_slice_split_without_space(str, ",", parts);
188 GPR_ASSERT(1 == parts->count);
189 GPR_ASSERT(0 == grpc_slice_str_cmp(parts->slices[0], ""));
190 grpc_slice_buffer_reset_and_unref(parts);
191 grpc_slice_unref(str);
192
193 grpc_slice_buffer_destroy(parts);
194 gpr_free(parts);
195 }
196
main(int argc,char ** argv)197 int main(int argc, char** argv) {
198 grpc_test_init(argc, argv);
199 test_dump_slice();
200 test_strsplit();
201 test_strsplit_nospace();
202 return 0;
203 }
204