• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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/slice/slice_internal.h"
31 #include "src/core/lib/transport/static_metadata.h"
32 #include "test/core/util/test_config.h"
33 
34 #define LOG_TEST_NAME(x) gpr_log(GPR_INFO, "%s", x);
35 
test_slice_malloc_returns_something_sensible(void)36 static void test_slice_malloc_returns_something_sensible(void) {
37   /* Calls grpc_slice_create for various lengths and verifies the internals for
38      consistency. */
39   size_t length;
40   size_t i;
41   grpc_slice slice;
42 
43   LOG_TEST_NAME("test_slice_malloc_returns_something_sensible");
44 
45   for (length = 0; length <= 1024; length++) {
46     slice = grpc_slice_malloc(length);
47     /* If there is a length, slice.data must be non-NULL. If length is zero
48        we don't care. */
49     if (length > GRPC_SLICE_INLINED_SIZE) {
50       GPR_ASSERT(slice.data.refcounted.bytes);
51     }
52     /* Returned slice length must be what was requested. */
53     GPR_ASSERT(GRPC_SLICE_LENGTH(slice) == length);
54     /* If the slice has a refcount, it must be destroyable. */
55     if (slice.refcount) {
56       GPR_ASSERT(slice.refcount->vtable != nullptr);
57       GPR_ASSERT(slice.refcount->vtable->ref != nullptr);
58       GPR_ASSERT(slice.refcount->vtable->unref != nullptr);
59       GPR_ASSERT(slice.refcount->vtable->hash != nullptr);
60     }
61     /* We must be able to write to every byte of the data */
62     for (i = 0; i < length; i++) {
63       GRPC_SLICE_START_PTR(slice)[i] = static_cast<uint8_t>(i);
64     }
65     /* And finally we must succeed in destroying the slice */
66     grpc_slice_unref(slice);
67   }
68 }
69 
do_nothing(void * ignored)70 static void do_nothing(void* ignored) {}
71 
test_slice_new_returns_something_sensible(void)72 static void test_slice_new_returns_something_sensible(void) {
73   uint8_t x;
74 
75   grpc_slice slice = grpc_slice_new(&x, 1, do_nothing);
76   GPR_ASSERT(slice.refcount);
77   GPR_ASSERT(slice.data.refcounted.bytes == &x);
78   GPR_ASSERT(slice.data.refcounted.length == 1);
79   grpc_slice_unref(slice);
80 }
81 
82 /* destroy function that sets a mark to indicate it was called. */
set_mark(void * p)83 static void set_mark(void* p) { *(static_cast<int*>(p)) = 1; }
84 
test_slice_new_with_user_data(void)85 static void test_slice_new_with_user_data(void) {
86   int marker = 0;
87   uint8_t buf[2];
88   grpc_slice slice;
89 
90   buf[0] = 0;
91   buf[1] = 1;
92   slice = grpc_slice_new_with_user_data(buf, 2, set_mark, &marker);
93   GPR_ASSERT(marker == 0);
94   GPR_ASSERT(GRPC_SLICE_LENGTH(slice) == 2);
95   GPR_ASSERT(GRPC_SLICE_START_PTR(slice)[0] == 0);
96   GPR_ASSERT(GRPC_SLICE_START_PTR(slice)[1] == 1);
97 
98   /* unref should cause destroy function to run. */
99   grpc_slice_unref(slice);
100   GPR_ASSERT(marker == 1);
101 }
102 
103 static int do_nothing_with_len_1_calls = 0;
104 
do_nothing_with_len_1(void * ignored,size_t len)105 static void do_nothing_with_len_1(void* ignored, size_t len) {
106   GPR_ASSERT(len == 1);
107   do_nothing_with_len_1_calls++;
108 }
109 
test_slice_new_with_len_returns_something_sensible(void)110 static void test_slice_new_with_len_returns_something_sensible(void) {
111   uint8_t x;
112   int num_refs = 5; /* To test adding/removing an arbitrary number of refs */
113   int i;
114 
115   grpc_slice slice = grpc_slice_new_with_len(&x, 1, do_nothing_with_len_1);
116   GPR_ASSERT(slice.refcount); /* ref count is initialized to 1 at this point */
117   GPR_ASSERT(slice.data.refcounted.bytes == &x);
118   GPR_ASSERT(slice.data.refcounted.length == 1);
119   GPR_ASSERT(do_nothing_with_len_1_calls == 0);
120 
121   /* Add an arbitrary number of refs to the slice and remoe the refs. This is to
122      make sure that that the destroy callback (i.e do_nothing_with_len_1()) is
123      not called until the last unref operation */
124   for (i = 0; i < num_refs; i++) {
125     grpc_slice_ref(slice);
126   }
127   for (i = 0; i < num_refs; i++) {
128     grpc_slice_unref(slice);
129   }
130   GPR_ASSERT(do_nothing_with_len_1_calls == 0); /* Shouldn't be called yet */
131 
132   /* last unref */
133   grpc_slice_unref(slice);
134   GPR_ASSERT(do_nothing_with_len_1_calls == 1);
135 }
136 
test_slice_sub_works(unsigned length)137 static void test_slice_sub_works(unsigned length) {
138   grpc_slice slice;
139   grpc_slice sub;
140   unsigned i, j, k;
141 
142   LOG_TEST_NAME("test_slice_sub_works");
143   gpr_log(GPR_INFO, "length=%d", length);
144 
145   /* Create a slice in which each byte is equal to the distance from it to the
146      beginning of the slice. */
147   slice = grpc_slice_malloc(length);
148   for (i = 0; i < length; i++) {
149     GRPC_SLICE_START_PTR(slice)[i] = static_cast<uint8_t>(i);
150   }
151 
152   /* Ensure that for all subsets length is correct and that we start on the
153      correct byte. Additionally check that no copies were made. */
154   for (i = 0; i < length; i++) {
155     for (j = i; j < length; j++) {
156       sub = grpc_slice_sub(slice, i, j);
157       GPR_ASSERT(GRPC_SLICE_LENGTH(sub) == j - i);
158       for (k = 0; k < j - i; k++) {
159         GPR_ASSERT(GRPC_SLICE_START_PTR(sub)[k] == (uint8_t)(i + k));
160       }
161       grpc_slice_unref(sub);
162     }
163   }
164   grpc_slice_unref(slice);
165 }
166 
check_head_tail(grpc_slice slice,grpc_slice head,grpc_slice tail)167 static void check_head_tail(grpc_slice slice, grpc_slice head,
168                             grpc_slice tail) {
169   GPR_ASSERT(GRPC_SLICE_LENGTH(slice) ==
170              GRPC_SLICE_LENGTH(head) + GRPC_SLICE_LENGTH(tail));
171   GPR_ASSERT(0 == memcmp(GRPC_SLICE_START_PTR(slice),
172                          GRPC_SLICE_START_PTR(head), GRPC_SLICE_LENGTH(head)));
173   GPR_ASSERT(0 == memcmp(GRPC_SLICE_START_PTR(slice) + GRPC_SLICE_LENGTH(head),
174                          GRPC_SLICE_START_PTR(tail), GRPC_SLICE_LENGTH(tail)));
175 }
176 
test_slice_split_head_works(size_t length)177 static void test_slice_split_head_works(size_t length) {
178   grpc_slice slice;
179   grpc_slice head, tail;
180   size_t i;
181 
182   LOG_TEST_NAME("test_slice_split_head_works");
183   gpr_log(GPR_INFO, "length=%" PRIuPTR, length);
184 
185   /* Create a slice in which each byte is equal to the distance from it to the
186      beginning of the slice. */
187   slice = grpc_slice_malloc(length);
188   for (i = 0; i < length; i++) {
189     GRPC_SLICE_START_PTR(slice)[i] = static_cast<uint8_t>(i);
190   }
191 
192   /* Ensure that for all subsets length is correct and that we start on the
193      correct byte. Additionally check that no copies were made. */
194   for (i = 0; i < length; i++) {
195     tail = grpc_slice_ref(slice);
196     head = grpc_slice_split_head(&tail, i);
197     check_head_tail(slice, head, tail);
198     grpc_slice_unref(tail);
199     grpc_slice_unref(head);
200   }
201 
202   grpc_slice_unref(slice);
203 }
204 
test_slice_split_tail_works(size_t length)205 static void test_slice_split_tail_works(size_t length) {
206   grpc_slice slice;
207   grpc_slice head, tail;
208   size_t i;
209 
210   LOG_TEST_NAME("test_slice_split_tail_works");
211   gpr_log(GPR_INFO, "length=%" PRIuPTR, length);
212 
213   /* Create a slice in which each byte is equal to the distance from it to the
214      beginning of the slice. */
215   slice = grpc_slice_malloc(length);
216   for (i = 0; i < length; i++) {
217     GRPC_SLICE_START_PTR(slice)[i] = static_cast<uint8_t>(i);
218   }
219 
220   /* Ensure that for all subsets length is correct and that we start on the
221      correct byte. Additionally check that no copies were made. */
222   for (i = 0; i < length; i++) {
223     head = grpc_slice_ref(slice);
224     tail = grpc_slice_split_tail(&head, i);
225     check_head_tail(slice, head, tail);
226     grpc_slice_unref(tail);
227     grpc_slice_unref(head);
228   }
229 
230   grpc_slice_unref(slice);
231 }
232 
test_slice_from_copied_string_works(void)233 static void test_slice_from_copied_string_works(void) {
234   static const char* text = "HELLO WORLD!";
235   grpc_slice slice;
236 
237   LOG_TEST_NAME("test_slice_from_copied_string_works");
238 
239   slice = grpc_slice_from_copied_string(text);
240   GPR_ASSERT(strlen(text) == GRPC_SLICE_LENGTH(slice));
241   GPR_ASSERT(
242       0 == memcmp(text, GRPC_SLICE_START_PTR(slice), GRPC_SLICE_LENGTH(slice)));
243   grpc_slice_unref(slice);
244 }
245 
test_slice_interning(void)246 static void test_slice_interning(void) {
247   LOG_TEST_NAME("test_slice_interning");
248 
249   grpc_init();
250   grpc_slice src1 = grpc_slice_from_copied_string("hello123456789123456789");
251   grpc_slice src2 = grpc_slice_from_copied_string("hello123456789123456789");
252   GPR_ASSERT(GRPC_SLICE_START_PTR(src1) != GRPC_SLICE_START_PTR(src2));
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 
main(int argc,char ** argv)295 int main(int argc, char** argv) {
296   unsigned length;
297   grpc_test_init(argc, argv);
298   grpc_init();
299   test_slice_malloc_returns_something_sensible();
300   test_slice_new_returns_something_sensible();
301   test_slice_new_with_user_data();
302   test_slice_new_with_len_returns_something_sensible();
303   for (length = 0; length < 128; length++) {
304     test_slice_sub_works(length);
305     test_slice_split_head_works(length);
306     test_slice_split_tail_works(length);
307   }
308   test_slice_from_copied_string_works();
309   test_slice_interning();
310   test_static_slice_interning();
311   test_static_slice_copy_interning();
312   grpc_shutdown();
313   return 0;
314 }
315