Imported Upstream version 1.22.0
[platform/upstream/grpc.git] / test / core / slice / slice_test.cc
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
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
64 static void do_nothing(void* ignored) {}
65
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. */
77 static void set_mark(void* p) { *(static_cast<int*>(p)) = 1; }
78
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
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
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
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
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
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
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
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
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   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);
257   grpc_shutdown();
258 }
259
260 static void test_static_slice_interning(void) {
261   LOG_TEST_NAME("test_static_slice_interning");
262
263   // grpc_init/grpc_shutdown deliberately omitted: they should not be necessary
264   // to intern a static slice
265
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])));
270   }
271 }
272
273 static void test_static_slice_copy_interning(void) {
274   LOG_TEST_NAME("test_static_slice_copy_interning");
275
276   grpc_init();
277
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);
284   }
285
286   grpc_shutdown();
287 }
288
289 static void test_moved_string_slice(void) {
290   LOG_TEST_NAME("test_moved_string_slice");
291
292   grpc_init();
293
294   // Small string should be inlined.
295   constexpr char kSmallStr[] = "hello12345";
296   char* small_ptr = strdup(kSmallStr);
297   grpc_slice small =
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);
303
304   // Large string should be move the reference.
305   constexpr char kSLargeStr[] = "hello123456789123456789123456789";
306   char* large_ptr = strdup(kSLargeStr);
307   grpc_slice large =
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);
313
314   // Moved buffer must respect the provided length not the actual length of the
315   // string.
316   large_ptr = strdup(kSLargeStr);
317   small = grpc_slice_from_moved_buffer(grpc_core::UniquePtr<char>(large_ptr),
318                                        strlen(kSmallStr));
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);
323
324   grpc_shutdown();
325 }
326
327 int main(int argc, char** argv) {
328   unsigned length;
329   grpc::testing::TestEnvironment env(argc, argv);
330   grpc_init();
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);
339   }
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();
345   grpc_shutdown();
346   return 0;
347 }