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 <grpc/support/port_platform.h>
20
21 #include <grpc/slice.h>
22
23 #include <inttypes.h>
24 #include <string.h>
25
26 #include <grpc/grpc.h>
27 #include <grpc/support/alloc.h>
28 #include <grpc/support/log.h>
29
30 #include "src/core/lib/gprpp/memory.h"
31 #include "src/core/lib/slice/slice_internal.h"
32 #include "src/core/lib/transport/static_metadata.h"
33 #include "test/core/util/test_config.h"
34
35 #define LOG_TEST_NAME(x) gpr_log(GPR_INFO, "%s", x);
36
test_slice_malloc_returns_something_sensible(void)37 static void test_slice_malloc_returns_something_sensible(void) {
38 /* Calls grpc_slice_create for various lengths and verifies the internals for
39 consistency. */
40 size_t length;
41 size_t i;
42 grpc_slice slice;
43
44 LOG_TEST_NAME("test_slice_malloc_returns_something_sensible");
45
46 for (length = 0; length <= 1024; length++) {
47 slice = grpc_slice_malloc(length);
48 /* If there is a length, slice.data must be non-NULL. If length is zero
49 we don't care. */
50 if (length > GRPC_SLICE_INLINED_SIZE) {
51 GPR_ASSERT(slice.data.refcounted.bytes);
52 }
53 /* Returned slice length must be what was requested. */
54 GPR_ASSERT(GRPC_SLICE_LENGTH(slice) == length);
55 /* We must be able to write to every byte of the data */
56 for (i = 0; i < length; i++) {
57 GRPC_SLICE_START_PTR(slice)[i] = static_cast<uint8_t>(i);
58 }
59 /* And finally we must succeed in destroying the slice */
60 grpc_slice_unref(slice);
61 }
62 }
63
do_nothing(void *)64 static void do_nothing(void* /*ignored*/) {}
65
test_slice_new_returns_something_sensible(void)66 static void test_slice_new_returns_something_sensible(void) {
67 uint8_t x;
68
69 grpc_slice slice = grpc_slice_new(&x, 1, do_nothing);
70 GPR_ASSERT(slice.refcount);
71 GPR_ASSERT(slice.data.refcounted.bytes == &x);
72 GPR_ASSERT(slice.data.refcounted.length == 1);
73 grpc_slice_unref(slice);
74 }
75
76 /* destroy function that sets a mark to indicate it was called. */
set_mark(void * p)77 static void set_mark(void* p) { *(static_cast<int*>(p)) = 1; }
78
test_slice_new_with_user_data(void)79 static void test_slice_new_with_user_data(void) {
80 int marker = 0;
81 uint8_t buf[2];
82 grpc_slice slice;
83
84 buf[0] = 0;
85 buf[1] = 1;
86 slice = grpc_slice_new_with_user_data(buf, 2, set_mark, &marker);
87 GPR_ASSERT(marker == 0);
88 GPR_ASSERT(GRPC_SLICE_LENGTH(slice) == 2);
89 GPR_ASSERT(GRPC_SLICE_START_PTR(slice)[0] == 0);
90 GPR_ASSERT(GRPC_SLICE_START_PTR(slice)[1] == 1);
91
92 /* unref should cause destroy function to run. */
93 grpc_slice_unref(slice);
94 GPR_ASSERT(marker == 1);
95 }
96
97 static int do_nothing_with_len_1_calls = 0;
98
do_nothing_with_len_1(void *,size_t len)99 static void do_nothing_with_len_1(void* /*ignored*/, size_t len) {
100 GPR_ASSERT(len == 1);
101 do_nothing_with_len_1_calls++;
102 }
103
test_slice_new_with_len_returns_something_sensible(void)104 static void test_slice_new_with_len_returns_something_sensible(void) {
105 uint8_t x;
106 int num_refs = 5; /* To test adding/removing an arbitrary number of refs */
107 int i;
108
109 grpc_slice slice = grpc_slice_new_with_len(&x, 1, do_nothing_with_len_1);
110 GPR_ASSERT(slice.refcount); /* ref count is initialized to 1 at this point */
111 GPR_ASSERT(slice.data.refcounted.bytes == &x);
112 GPR_ASSERT(slice.data.refcounted.length == 1);
113 GPR_ASSERT(do_nothing_with_len_1_calls == 0);
114
115 /* Add an arbitrary number of refs to the slice and remoe the refs. This is to
116 make sure that that the destroy callback (i.e do_nothing_with_len_1()) is
117 not called until the last unref operation */
118 for (i = 0; i < num_refs; i++) {
119 grpc_slice_ref(slice);
120 }
121 for (i = 0; i < num_refs; i++) {
122 grpc_slice_unref(slice);
123 }
124 GPR_ASSERT(do_nothing_with_len_1_calls == 0); /* Shouldn't be called yet */
125
126 /* last unref */
127 grpc_slice_unref(slice);
128 GPR_ASSERT(do_nothing_with_len_1_calls == 1);
129 }
130
test_slice_sub_works(unsigned length)131 static void test_slice_sub_works(unsigned length) {
132 grpc_slice slice;
133 grpc_slice sub;
134 unsigned i, j, k;
135
136 LOG_TEST_NAME("test_slice_sub_works");
137 gpr_log(GPR_INFO, "length=%d", length);
138
139 /* Create a slice in which each byte is equal to the distance from it to the
140 beginning of the slice. */
141 slice = grpc_slice_malloc(length);
142 for (i = 0; i < length; i++) {
143 GRPC_SLICE_START_PTR(slice)[i] = static_cast<uint8_t>(i);
144 }
145
146 /* Ensure that for all subsets length is correct and that we start on the
147 correct byte. Additionally check that no copies were made. */
148 for (i = 0; i < length; i++) {
149 for (j = i; j < length; j++) {
150 sub = grpc_slice_sub(slice, i, j);
151 GPR_ASSERT(GRPC_SLICE_LENGTH(sub) == j - i);
152 for (k = 0; k < j - i; k++) {
153 GPR_ASSERT(GRPC_SLICE_START_PTR(sub)[k] == (uint8_t)(i + k));
154 }
155 grpc_slice_unref(sub);
156 }
157 }
158 grpc_slice_unref(slice);
159 }
160
check_head_tail(grpc_slice slice,grpc_slice head,grpc_slice tail)161 static void check_head_tail(grpc_slice slice, grpc_slice head,
162 grpc_slice tail) {
163 GPR_ASSERT(GRPC_SLICE_LENGTH(slice) ==
164 GRPC_SLICE_LENGTH(head) + GRPC_SLICE_LENGTH(tail));
165 GPR_ASSERT(0 == memcmp(GRPC_SLICE_START_PTR(slice),
166 GRPC_SLICE_START_PTR(head), GRPC_SLICE_LENGTH(head)));
167 GPR_ASSERT(0 == memcmp(GRPC_SLICE_START_PTR(slice) + GRPC_SLICE_LENGTH(head),
168 GRPC_SLICE_START_PTR(tail), GRPC_SLICE_LENGTH(tail)));
169 }
170
test_slice_split_head_works(size_t length)171 static void test_slice_split_head_works(size_t length) {
172 grpc_slice slice;
173 grpc_slice head, tail;
174 size_t i;
175
176 LOG_TEST_NAME("test_slice_split_head_works");
177 gpr_log(GPR_INFO, "length=%" PRIuPTR, length);
178
179 /* Create a slice in which each byte is equal to the distance from it to the
180 beginning of the slice. */
181 slice = grpc_slice_malloc(length);
182 for (i = 0; i < length; i++) {
183 GRPC_SLICE_START_PTR(slice)[i] = static_cast<uint8_t>(i);
184 }
185
186 /* Ensure that for all subsets length is correct and that we start on the
187 correct byte. Additionally check that no copies were made. */
188 for (i = 0; i < length; i++) {
189 tail = grpc_slice_ref(slice);
190 head = grpc_slice_split_head(&tail, i);
191 check_head_tail(slice, head, tail);
192 grpc_slice_unref(tail);
193 grpc_slice_unref(head);
194 }
195
196 grpc_slice_unref(slice);
197 }
198
test_slice_split_tail_works(size_t length)199 static void test_slice_split_tail_works(size_t length) {
200 grpc_slice slice;
201 grpc_slice head, tail;
202 size_t i;
203
204 LOG_TEST_NAME("test_slice_split_tail_works");
205 gpr_log(GPR_INFO, "length=%" PRIuPTR, length);
206
207 /* Create a slice in which each byte is equal to the distance from it to the
208 beginning of the slice. */
209 slice = grpc_slice_malloc(length);
210 for (i = 0; i < length; i++) {
211 GRPC_SLICE_START_PTR(slice)[i] = static_cast<uint8_t>(i);
212 }
213
214 /* Ensure that for all subsets length is correct and that we start on the
215 correct byte. Additionally check that no copies were made. */
216 for (i = 0; i < length; i++) {
217 head = grpc_slice_ref(slice);
218 tail = grpc_slice_split_tail(&head, i);
219 check_head_tail(slice, head, tail);
220 grpc_slice_unref(tail);
221 grpc_slice_unref(head);
222 }
223
224 grpc_slice_unref(slice);
225 }
226
test_slice_from_copied_string_works(void)227 static void test_slice_from_copied_string_works(void) {
228 static const char* text = "HELLO WORLD!";
229 grpc_slice slice;
230
231 LOG_TEST_NAME("test_slice_from_copied_string_works");
232
233 slice = grpc_slice_from_copied_string(text);
234 GPR_ASSERT(strlen(text) == GRPC_SLICE_LENGTH(slice));
235 GPR_ASSERT(
236 0 == memcmp(text, GRPC_SLICE_START_PTR(slice), GRPC_SLICE_LENGTH(slice)));
237 grpc_slice_unref(slice);
238 }
239
test_slice_interning(void)240 static void test_slice_interning(void) {
241 LOG_TEST_NAME("test_slice_interning");
242
243 grpc_init();
244 grpc_slice src1 = grpc_slice_from_copied_string("hello123456789123456789");
245 grpc_slice src2 = grpc_slice_from_copied_string("hello123456789123456789");
246
247 // Explicitly checking that the slices are at different addresses prevents
248 // failure with windows opt 64bit build.
249 // See https://github.com/grpc/grpc/issues/20519
250 GPR_ASSERT(&src1 != &src2);
251 GPR_ASSERT(GRPC_SLICE_START_PTR(src1) != GRPC_SLICE_START_PTR(src2));
252
253 grpc_slice interned1 = grpc_slice_intern(src1);
254 grpc_slice interned2 = grpc_slice_intern(src2);
255 GPR_ASSERT(GRPC_SLICE_START_PTR(interned1) ==
256 GRPC_SLICE_START_PTR(interned2));
257 GPR_ASSERT(GRPC_SLICE_START_PTR(interned1) != GRPC_SLICE_START_PTR(src1));
258 GPR_ASSERT(GRPC_SLICE_START_PTR(interned2) != GRPC_SLICE_START_PTR(src2));
259 grpc_slice_unref(src1);
260 grpc_slice_unref(src2);
261 grpc_slice_unref(interned1);
262 grpc_slice_unref(interned2);
263 grpc_shutdown();
264 }
265
test_static_slice_interning(void)266 static void test_static_slice_interning(void) {
267 LOG_TEST_NAME("test_static_slice_interning");
268
269 // grpc_init/grpc_shutdown deliberately omitted: they should not be necessary
270 // to intern a static slice
271
272 for (size_t i = 0; i < GRPC_STATIC_MDSTR_COUNT; i++) {
273 GPR_ASSERT(grpc_slice_is_equivalent(
274 grpc_static_slice_table()[i],
275 grpc_slice_intern(grpc_static_slice_table()[i])));
276 }
277 }
278
test_static_slice_copy_interning(void)279 static void test_static_slice_copy_interning(void) {
280 LOG_TEST_NAME("test_static_slice_copy_interning");
281
282 grpc_init();
283
284 for (size_t i = 0; i < GRPC_STATIC_MDSTR_COUNT; i++) {
285 grpc_slice copy = grpc_slice_dup(grpc_static_slice_table()[i]);
286 GPR_ASSERT(grpc_static_slice_table()[i].refcount != copy.refcount);
287 GPR_ASSERT(grpc_static_slice_table()[i].refcount ==
288 grpc_slice_intern(copy).refcount);
289 grpc_slice_unref(copy);
290 }
291
292 grpc_shutdown();
293 }
294
test_moved_string_slice(void)295 static void test_moved_string_slice(void) {
296 LOG_TEST_NAME("test_moved_string_slice");
297
298 grpc_init();
299
300 // Small string should be inlined.
301 constexpr char kSmallStr[] = "hello12345";
302 char* small_ptr = strdup(kSmallStr);
303 grpc_slice small =
304 grpc_slice_from_moved_string(grpc_core::UniquePtr<char>(small_ptr));
305 GPR_ASSERT(GRPC_SLICE_LENGTH(small) == strlen(kSmallStr));
306 GPR_ASSERT(GRPC_SLICE_START_PTR(small) !=
307 reinterpret_cast<uint8_t*>(small_ptr));
308 grpc_slice_unref(small);
309
310 // Large string should be move the reference.
311 constexpr char kSLargeStr[] = "hello123456789123456789123456789";
312 char* large_ptr = strdup(kSLargeStr);
313 grpc_slice large =
314 grpc_slice_from_moved_string(grpc_core::UniquePtr<char>(large_ptr));
315 GPR_ASSERT(GRPC_SLICE_LENGTH(large) == strlen(kSLargeStr));
316 GPR_ASSERT(GRPC_SLICE_START_PTR(large) ==
317 reinterpret_cast<uint8_t*>(large_ptr));
318 grpc_slice_unref(large);
319
320 // Moved buffer must respect the provided length not the actual length of the
321 // string.
322 large_ptr = strdup(kSLargeStr);
323 small = grpc_slice_from_moved_buffer(grpc_core::UniquePtr<char>(large_ptr),
324 strlen(kSmallStr));
325 GPR_ASSERT(GRPC_SLICE_LENGTH(small) == strlen(kSmallStr));
326 GPR_ASSERT(GRPC_SLICE_START_PTR(small) !=
327 reinterpret_cast<uint8_t*>(large_ptr));
328 grpc_slice_unref(small);
329
330 grpc_shutdown();
331 }
332
test_string_view_from_slice()333 void test_string_view_from_slice() {
334 constexpr char kStr[] = "foo";
335 absl::string_view sv(
336 grpc_core::StringViewFromSlice(grpc_slice_from_static_string(kStr)));
337 GPR_ASSERT(std::string(sv) == kStr);
338 }
339
main(int argc,char ** argv)340 int main(int argc, char** argv) {
341 unsigned length;
342 grpc::testing::TestEnvironment env(argc, argv);
343 grpc_init();
344 test_slice_malloc_returns_something_sensible();
345 test_slice_new_returns_something_sensible();
346 test_slice_new_with_user_data();
347 test_slice_new_with_len_returns_something_sensible();
348 for (length = 0; length < 128; length++) {
349 test_slice_sub_works(length);
350 test_slice_split_head_works(length);
351 test_slice_split_tail_works(length);
352 }
353 test_slice_from_copied_string_works();
354 test_slice_interning();
355 test_static_slice_interning();
356 test_static_slice_copy_interning();
357 test_moved_string_slice();
358 test_string_view_from_slice();
359 grpc_shutdown();
360 return 0;
361 }
362