3 * Copyright 2015 gRPC authors.
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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 #include <grpc/support/port_platform.h>
21 #include <grpc/slice.h>
26 #include <grpc/grpc.h>
27 #include <grpc/support/alloc.h>
28 #include <grpc/support/log.h>
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"
35 #define LOG_TEST_NAME(x) gpr_log(GPR_INFO, "%s", x);
37 static void test_slice_malloc_returns_something_sensible(void) {
38 /* Calls grpc_slice_create for various lengths and verifies the internals for
44 LOG_TEST_NAME("test_slice_malloc_returns_something_sensible");
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
50 if (length > GRPC_SLICE_INLINED_SIZE) {
51 GPR_ASSERT(slice.data.refcounted.bytes);
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);
59 /* And finally we must succeed in destroying the slice */
60 grpc_slice_unref(slice);
64 static void do_nothing(void* ignored) {}
66 static void test_slice_new_returns_something_sensible(void) {
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);
76 /* destroy function that sets a mark to indicate it was called. */
77 static void set_mark(void* p) { *(static_cast<int*>(p)) = 1; }
79 static void test_slice_new_with_user_data(void) {
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);
92 /* unref should cause destroy function to run. */
93 grpc_slice_unref(slice);
94 GPR_ASSERT(marker == 1);
97 static int do_nothing_with_len_1_calls = 0;
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++;
104 static void test_slice_new_with_len_returns_something_sensible(void) {
106 int num_refs = 5; /* To test adding/removing an arbitrary number of refs */
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);
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);
121 for (i = 0; i < num_refs; i++) {
122 grpc_slice_unref(slice);
124 GPR_ASSERT(do_nothing_with_len_1_calls == 0); /* Shouldn't be called yet */
127 grpc_slice_unref(slice);
128 GPR_ASSERT(do_nothing_with_len_1_calls == 1);
131 static void test_slice_sub_works(unsigned length) {
136 LOG_TEST_NAME("test_slice_sub_works");
137 gpr_log(GPR_INFO, "length=%d", length);
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);
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));
155 grpc_slice_unref(sub);
158 grpc_slice_unref(slice);
161 static void check_head_tail(grpc_slice slice, grpc_slice head,
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)));
171 static void test_slice_split_head_works(size_t length) {
173 grpc_slice head, tail;
176 LOG_TEST_NAME("test_slice_split_head_works");
177 gpr_log(GPR_INFO, "length=%" PRIuPTR, length);
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);
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);
196 grpc_slice_unref(slice);
199 static void test_slice_split_tail_works(size_t length) {
201 grpc_slice head, tail;
204 LOG_TEST_NAME("test_slice_split_tail_works");
205 gpr_log(GPR_INFO, "length=%" PRIuPTR, length);
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);
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);
224 grpc_slice_unref(slice);
227 static void test_slice_from_copied_string_works(void) {
228 static const char* text = "HELLO WORLD!";
231 LOG_TEST_NAME("test_slice_from_copied_string_works");
233 slice = grpc_slice_from_copied_string(text);
234 GPR_ASSERT(strlen(text) == GRPC_SLICE_LENGTH(slice));
236 0 == memcmp(text, GRPC_SLICE_START_PTR(slice), GRPC_SLICE_LENGTH(slice)));
237 grpc_slice_unref(slice);
240 static void test_slice_interning(void) {
241 LOG_TEST_NAME("test_slice_interning");
244 grpc_slice src1 = grpc_slice_from_copied_string("hello123456789123456789");
245 grpc_slice src2 = grpc_slice_from_copied_string("hello123456789123456789");
246 GPR_ASSERT(GRPC_SLICE_START_PTR(src1) != GRPC_SLICE_START_PTR(src2));
247 grpc_slice interned1 = grpc_slice_intern(src1);
248 grpc_slice interned2 = grpc_slice_intern(src2);
249 GPR_ASSERT(GRPC_SLICE_START_PTR(interned1) ==
250 GRPC_SLICE_START_PTR(interned2));
251 GPR_ASSERT(GRPC_SLICE_START_PTR(interned1) != GRPC_SLICE_START_PTR(src1));
252 GPR_ASSERT(GRPC_SLICE_START_PTR(interned2) != GRPC_SLICE_START_PTR(src2));
253 grpc_slice_unref(src1);
254 grpc_slice_unref(src2);
255 grpc_slice_unref(interned1);
256 grpc_slice_unref(interned2);
260 static void test_static_slice_interning(void) {
261 LOG_TEST_NAME("test_static_slice_interning");
263 // grpc_init/grpc_shutdown deliberately omitted: they should not be necessary
264 // to intern a static slice
266 for (size_t i = 0; i < GRPC_STATIC_MDSTR_COUNT; i++) {
267 GPR_ASSERT(grpc_slice_is_equivalent(
268 grpc_static_slice_table[i],
269 grpc_slice_intern(grpc_static_slice_table[i])));
273 static void test_static_slice_copy_interning(void) {
274 LOG_TEST_NAME("test_static_slice_copy_interning");
278 for (size_t i = 0; i < GRPC_STATIC_MDSTR_COUNT; i++) {
279 grpc_slice copy = grpc_slice_dup(grpc_static_slice_table[i]);
280 GPR_ASSERT(grpc_static_slice_table[i].refcount != copy.refcount);
281 GPR_ASSERT(grpc_static_slice_table[i].refcount ==
282 grpc_slice_intern(copy).refcount);
283 grpc_slice_unref(copy);
289 static void test_moved_string_slice(void) {
290 LOG_TEST_NAME("test_moved_string_slice");
294 // Small string should be inlined.
295 constexpr char kSmallStr[] = "hello12345";
296 char* small_ptr = strdup(kSmallStr);
298 grpc_slice_from_moved_string(grpc_core::UniquePtr<char>(small_ptr));
299 GPR_ASSERT(GRPC_SLICE_LENGTH(small) == strlen(kSmallStr));
300 GPR_ASSERT(GRPC_SLICE_START_PTR(small) !=
301 reinterpret_cast<uint8_t*>(small_ptr));
302 grpc_slice_unref(small);
304 // Large string should be move the reference.
305 constexpr char kSLargeStr[] = "hello123456789123456789123456789";
306 char* large_ptr = strdup(kSLargeStr);
308 grpc_slice_from_moved_string(grpc_core::UniquePtr<char>(large_ptr));
309 GPR_ASSERT(GRPC_SLICE_LENGTH(large) == strlen(kSLargeStr));
310 GPR_ASSERT(GRPC_SLICE_START_PTR(large) ==
311 reinterpret_cast<uint8_t*>(large_ptr));
312 grpc_slice_unref(large);
314 // Moved buffer must respect the provided length not the actual length of the
316 large_ptr = strdup(kSLargeStr);
317 small = grpc_slice_from_moved_buffer(grpc_core::UniquePtr<char>(large_ptr),
319 GPR_ASSERT(GRPC_SLICE_LENGTH(small) == strlen(kSmallStr));
320 GPR_ASSERT(GRPC_SLICE_START_PTR(small) !=
321 reinterpret_cast<uint8_t*>(large_ptr));
322 grpc_slice_unref(small);
327 int main(int argc, char** argv) {
329 grpc::testing::TestEnvironment env(argc, argv);
331 test_slice_malloc_returns_something_sensible();
332 test_slice_new_returns_something_sensible();
333 test_slice_new_with_user_data();
334 test_slice_new_with_len_returns_something_sensible();
335 for (length = 0; length < 128; length++) {
336 test_slice_sub_works(length);
337 test_slice_split_head_works(length);
338 test_slice_split_tail_works(length);
340 test_slice_from_copied_string_works();
341 test_slice_interning();
342 test_static_slice_interning();
343 test_static_slice_copy_interning();
344 test_moved_string_slice();