Debian Codegraph @

test/core/slice/slice_test.cc

raw
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 "src/core/lib/slice/slice.h"
22
23 #include <inttypes.h>
24 #include <string.h>
25
26 #include <algorithm>
27 #include <memory>
28 #include <random>
29 #include <string>
30 #include <vector>
31
32 #include "absl/strings/string_view.h"
33 #include "gtest/gtest.h"
34
35 #include <grpc/slice.h>
36 #include <grpc/support/log.h>
37
38 #include "src/core/lib/gprpp/memory.h"
39 #include "src/core/lib/slice/slice_internal.h"
40 #include "test/core/util/build.h"
41
42 TEST(GrpcSliceTest, MallocReturnsSomethingSensible) {
43 /* Calls grpc_slice_create for various lengths and verifies the internals for
44 consistency. */
45 size_t length;
46 size_t i;
47 grpc_slice slice;
48
49 for (length = 0; length <= 1024; length++) {
50 slice = grpc_slice_malloc(length);
51 /* If there is a length, slice.data must be non-NULL. If length is zero
52 we don't care. */
53 if (length > GRPC_SLICE_INLINED_SIZE) {
54 EXPECT_NE(slice.data.refcounted.bytes, nullptr);
55 }
56 /* Returned slice length must be what was requested. */
57 EXPECT_EQ(GRPC_SLICE_LENGTH(slice), length);
58 /* We must be able to write to every byte of the data */
59 for (i = 0; i < length; i++) {
60 GRPC_SLICE_START_PTR(slice)[i] = static_cast<uint8_t>(i);
61 }
62 /* And finally we must succeed in destroying the slice */
63 grpc_slice_unref(slice);
64 }
65 }
66
67 static void do_nothing(void* /*ignored*/) {}
68
69 TEST(GrpcSliceTest, SliceNewReturnsSomethingSensible) {
70 uint8_t x;
71
72 grpc_slice slice = grpc_slice_new(&x, 1, do_nothing);
73 EXPECT_NE(slice.refcount, nullptr);
74 EXPECT_EQ(slice.data.refcounted.bytes, &x);
75 EXPECT_EQ(slice.data.refcounted.length, 1);
76 grpc_slice_unref(slice);
77 }
78
79 /* destroy function that sets a mark to indicate it was called. */
80 static void set_mark(void* p) { *(static_cast<int*>(p)) = 1; }
81
82 TEST(GrpcSliceTest, SliceNewWithUserData) {
83 int marker = 0;
84 uint8_t buf[2];
85 grpc_slice slice;
86
87 buf[0] = 0;
88 buf[1] = 1;
89 slice = grpc_slice_new_with_user_data(buf, 2, set_mark, &marker);
90 EXPECT_EQ(marker, 0);
91 EXPECT_EQ(GRPC_SLICE_LENGTH(slice), 2);
92 EXPECT_EQ(GRPC_SLICE_START_PTR(slice)[0], 0);
93 EXPECT_EQ(GRPC_SLICE_START_PTR(slice)[1], 1);
94
95 /* unref should cause destroy function to run. */
96 grpc_slice_unref(slice);
97 EXPECT_EQ(marker, 1);
98 }
99
100 static int do_nothing_with_len_1_calls = 0;
101
102 static void do_nothing_with_len_1(void* /*ignored*/, size_t len) {
103 EXPECT_EQ(len, 1);
104 do_nothing_with_len_1_calls++;
105 }
106
107 TEST(GrpcSliceTest, SliceNewWithLenReturnsSomethingSensible) {
108 uint8_t x;
109 int num_refs = 5; /* To test adding/removing an arbitrary number of refs */
110 int i;
111
112 grpc_slice slice = grpc_slice_new_with_len(&x, 1, do_nothing_with_len_1);
113 EXPECT_NE(slice.refcount,
114 nullptr); /* ref count is initialized to 1 at this point */
115 EXPECT_EQ(slice.data.refcounted.bytes, &x);
116 EXPECT_EQ(slice.data.refcounted.length, 1);
117 EXPECT_EQ(do_nothing_with_len_1_calls, 0);
118
119 /* Add an arbitrary number of refs to the slice and remoe the refs. This is to
120 make sure that that the destroy callback (i.e do_nothing_with_len_1()) is
121 not called until the last unref operation */
122 for (i = 0; i < num_refs; i++) {
123 grpc_slice_ref(slice);
124 }
125 for (i = 0; i < num_refs; i++) {
126 grpc_slice_unref(slice);
127 }
128 EXPECT_EQ(do_nothing_with_len_1_calls, 0); /* Shouldn't be called yet */
129
130 /* last unref */
131 grpc_slice_unref(slice);
132 EXPECT_EQ(do_nothing_with_len_1_calls, 1);
133 }
134
135 class GrpcSliceSizedTest : public ::testing::TestWithParam<size_t> {};
136
137 TEST_P(GrpcSliceSizedTest, SliceSubWorks) {
138 const auto length = GetParam();
139
140 grpc_slice slice;
141 grpc_slice sub;
142 unsigned i, j, k;
143
144 /* Create a slice in which each byte is equal to the distance from it to the
145 beginning of the slice. */
146 slice = grpc_slice_malloc(length);
147 for (i = 0; i < length; i++) {
148 GRPC_SLICE_START_PTR(slice)[i] = static_cast<uint8_t>(i);
149 }
150
151 /* Ensure that for all subsets length is correct and that we start on the
152 correct byte. Additionally check that no copies were made. */
153 for (i = 0; i < length; i++) {
154 for (j = i; j < length; j++) {
155 sub = grpc_slice_sub(slice, i, j);
156 EXPECT_EQ(GRPC_SLICE_LENGTH(sub), j - i);
157 for (k = 0; k < j - i; k++) {
158 EXPECT_EQ(GRPC_SLICE_START_PTR(sub)[k], (uint8_t)(i + k));
159 }
160 grpc_slice_unref(sub);
161 }
162 }
163 grpc_slice_unref(slice);
164 }
165
166 static void check_head_tail(grpc_slice slice, grpc_slice head,
167 grpc_slice tail) {
168 EXPECT_EQ(GRPC_SLICE_LENGTH(slice),
169 GRPC_SLICE_LENGTH(head) + GRPC_SLICE_LENGTH(tail));
170 EXPECT_EQ(0, memcmp(GRPC_SLICE_START_PTR(slice), GRPC_SLICE_START_PTR(head),
171 GRPC_SLICE_LENGTH(head)));
172 EXPECT_EQ(0, memcmp(GRPC_SLICE_START_PTR(slice) + GRPC_SLICE_LENGTH(head),
173 GRPC_SLICE_START_PTR(tail), GRPC_SLICE_LENGTH(tail)));
174 }
175
176 TEST_P(GrpcSliceSizedTest, SliceSplitHeadWorks) {
177 const auto length = GetParam();
178
179 grpc_slice slice;
180 grpc_slice head, tail;
181 size_t i;
182
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
205 TEST_P(GrpcSliceSizedTest, SliceSplitTailWorks) {
206 const auto length = GetParam();
207
208 grpc_slice slice;
209 grpc_slice head, tail;
210 size_t i;
211
212 gpr_log(GPR_INFO, "length=%" PRIuPTR, length);
213
214 /* Create a slice in which each byte is equal to the distance from it to the
215 beginning of the slice. */
216 slice = grpc_slice_malloc(length);
217 for (i = 0; i < length; i++) {
218 GRPC_SLICE_START_PTR(slice)[i] = static_cast<uint8_t>(i);
219 }
220
221 /* Ensure that for all subsets length is correct and that we start on the
222 correct byte. Additionally check that no copies were made. */
223 for (i = 0; i < length; i++) {
224 head = grpc_slice_ref(slice);
225 tail = grpc_slice_split_tail(&head, i);
226 check_head_tail(slice, head, tail);
227 grpc_slice_unref(tail);
228 grpc_slice_unref(head);
229 }
230
231 grpc_slice_unref(slice);
232 }
233
234 INSTANTIATE_TEST_SUITE_P(GrpcSliceSizedTest, GrpcSliceSizedTest,
235 ::testing::ValuesIn([] {
236 std::vector<size_t> out;
237 for (size_t i = 0; i < 128; i++) {
238 out.push_back(i);
239 }
240 return out;
241 }()),
242 [](const testing::TestParamInfo<size_t>& info) {
243 return std::to_string(info.param);
244 });
245
246 TEST(GrpcSliceTest, SliceFromCopiedString) {
247 static const char* text = "HELLO WORLD!";
248 grpc_slice slice;
249
250 slice = grpc_slice_from_copied_string(text);
251 EXPECT_EQ(strlen(text), GRPC_SLICE_LENGTH(slice));
252 EXPECT_EQ(
253 0, memcmp(text, GRPC_SLICE_START_PTR(slice), GRPC_SLICE_LENGTH(slice)));
254 grpc_slice_unref(slice);
255 }
256
257 TEST(GrpcSliceTest, MovedStringSlice) {
258 // Small string should be inlined.
259 constexpr char kSmallStr[] = "hello12345";
260 char* small_ptr = strdup(kSmallStr);
261 grpc_slice small =
262 grpc_slice_from_moved_string(grpc_core::UniquePtr<char>(small_ptr));
263 EXPECT_EQ(GRPC_SLICE_LENGTH(small), strlen(kSmallStr));
264 EXPECT_NE(GRPC_SLICE_START_PTR(small), reinterpret_cast<uint8_t*>(small_ptr));
265 grpc_slice_unref(small);
266
267 // Large string should be move the reference.
268 constexpr char kSLargeStr[] = "hello123456789123456789123456789";
269 char* large_ptr = strdup(kSLargeStr);
270 grpc_slice large =
271 grpc_slice_from_moved_string(grpc_core::UniquePtr<char>(large_ptr));
272 EXPECT_EQ(GRPC_SLICE_LENGTH(large), strlen(kSLargeStr));
273 EXPECT_EQ(GRPC_SLICE_START_PTR(large), reinterpret_cast<uint8_t*>(large_ptr));
274 grpc_slice_unref(large);
275
276 // Moved buffer must respect the provided length not the actual length of the
277 // string.
278 large_ptr = strdup(kSLargeStr);
279 small = grpc_slice_from_moved_buffer(grpc_core::UniquePtr<char>(large_ptr),
280 strlen(kSmallStr));
281 EXPECT_EQ(GRPC_SLICE_LENGTH(small), strlen(kSmallStr));
282 EXPECT_NE(GRPC_SLICE_START_PTR(small), reinterpret_cast<uint8_t*>(large_ptr));
283 grpc_slice_unref(small);
284 }
285
286 TEST(GrpcSliceTest, StringViewFromSlice) {
287 constexpr char kStr[] = "foo";
288 absl::string_view sv(
289 grpc_core::StringViewFromSlice(grpc_slice_from_static_string(kStr)));
290 EXPECT_EQ(sv, kStr);
291 }
292
293 namespace grpc_core {
294 namespace {
295
296 TEST(SliceTest, FromSmallCopiedString) {
297 Slice slice = Slice::FromCopiedString("hello");
298 EXPECT_EQ(slice[0], 'h');
299 EXPECT_EQ(slice[1], 'e');
300 EXPECT_EQ(slice[2], 'l');
301 EXPECT_EQ(slice[3], 'l');
302 EXPECT_EQ(slice[4], 'o');
303 EXPECT_EQ(slice.size(), 5);
304 EXPECT_EQ(slice.length(), 5);
305 EXPECT_EQ(slice.as_string_view(), "hello");
306 EXPECT_EQ(0, memcmp(slice.data(), "hello", 5));
307 }
308
309 class SliceSizedTest : public ::testing::TestWithParam<size_t> {};
310
311 std::string RandomString(size_t length) {
312 std::string str;
313 std::random_device r;
314 for (size_t i = 0; i < length; ++i) {
315 str.push_back(char(r()));
316 }
317 return str;
318 }
319
320 TEST_P(SliceSizedTest, FromCopiedString) {
321 const std::string str = RandomString(GetParam());
322 Slice slice = Slice::FromCopiedString(str);
323
324 EXPECT_EQ(slice.size(), str.size());
325 EXPECT_EQ(slice.length(), str.size());
326 EXPECT_EQ(slice.as_string_view(), str);
327 EXPECT_EQ(0, memcmp(slice.data(), str.data(), str.size()));
328 for (size_t i = 0; i < str.size(); ++i) {
329 EXPECT_EQ(slice[i], uint8_t(str[i]));
330 }
331
332 EXPECT_TRUE(slice.is_equivalent(slice.Ref()));
333 EXPECT_TRUE(slice.is_equivalent(slice.AsOwned()));
334 EXPECT_TRUE(slice.is_equivalent(slice.Ref().TakeOwned()));
335 }
336
337 INSTANTIATE_TEST_SUITE_P(SliceSizedTest, SliceSizedTest,
338 ::testing::ValuesIn([] {
339 std::vector<size_t> out;
340 size_t i = 1;
341 size_t j = 1;
342 while (i < 1024 * 1024) {
343 out.push_back(j);
344 size_t n = i + j;
345 i = j;
346 j = n;
347 }
348 return out;
349 }()),
350 [](const testing::TestParamInfo<size_t>& info) {
351 return std::to_string(info.param);
352 });
353
354 size_t SumSlice(const Slice& slice) {
355 size_t x = 0;
356 for (size_t i = 0; i < slice.size(); ++i) {
357 x += slice[i];
358 }
359 return x;
360 }
361
362 TEST(SliceTest, ExternalAsOwned) {
363 auto external_string = std::make_unique<std::string>(RandomString(1024));
364 Slice slice = Slice::FromExternalString(*external_string);
365 const auto initial_sum = SumSlice(slice);
366 Slice owned = slice.AsOwned();
367 EXPECT_EQ(initial_sum, SumSlice(owned));
368 external_string.reset();
369 // In ASAN (where we can be sure that it'll crash), go ahead and read the
370 // bytes we just deleted.
371 if (BuiltUnderAsan()) {
372 ASSERT_DEATH({ gpr_log(GPR_DEBUG, "%" PRIdPTR, SumSlice(slice)); }, "");
373 }
374 EXPECT_EQ(initial_sum, SumSlice(owned));
375 }
376
377 TEST(SliceTest, ExternalTakeOwned) {
378 std::unique_ptr<std::string> external_string(
379 new std::string(RandomString(1024)));
380 SumSlice(Slice::FromExternalString(*external_string).TakeOwned());
381 }
382
383 TEST(SliceTest, StaticSlice) {
384 static const char* hello = "hello";
385 StaticSlice slice = StaticSlice::FromStaticString(hello);
386 EXPECT_EQ(slice[0], 'h');
387 EXPECT_EQ(slice[1], 'e');
388 EXPECT_EQ(slice[2], 'l');
389 EXPECT_EQ(slice[3], 'l');
390 EXPECT_EQ(slice[4], 'o');
391 EXPECT_EQ(slice.size(), 5);
392 EXPECT_EQ(slice.length(), 5);
393 EXPECT_EQ(slice.as_string_view(), "hello");
394 EXPECT_EQ(0, memcmp(slice.data(), "hello", 5));
395 EXPECT_EQ(reinterpret_cast<const uint8_t*>(hello), slice.data());
396 }
397
398 TEST(SliceTest, SliceEquality) {
399 auto a = Slice::FromCopiedString(
400 "hello world 123456789123456789123456789123456789123456789");
401 auto b = Slice::FromCopiedString(
402 "hello world 123456789123456789123456789123456789123456789");
403 auto c = Slice::FromCopiedString(
404 "this is not the same as the other two strings!!!!!!!!!!!!");
405 EXPECT_FALSE(a.is_equivalent(b));
406 EXPECT_FALSE(b.is_equivalent(a));
407 EXPECT_EQ(a, b);
408 EXPECT_NE(a, c);
409 EXPECT_NE(b, c);
410 EXPECT_EQ(a, "hello world 123456789123456789123456789123456789123456789");
411 EXPECT_NE(a, "pfoooey");
412 EXPECT_EQ(c, "this is not the same as the other two strings!!!!!!!!!!!!");
413 EXPECT_EQ("hello world 123456789123456789123456789123456789123456789", a);
414 EXPECT_NE("pfoooey", a);
415 EXPECT_EQ("this is not the same as the other two strings!!!!!!!!!!!!", c);
416 }
417
418 TEST(SliceTest, LetsGetMutable) {
419 auto slice = MutableSlice::FromCopiedString("hello");
420 EXPECT_EQ(slice[0], 'h');
421 EXPECT_EQ(slice[1], 'e');
422 EXPECT_EQ(slice[2], 'l');
423 EXPECT_EQ(slice[3], 'l');
424 EXPECT_EQ(slice[4], 'o');
425 EXPECT_EQ(slice.size(), 5);
426 EXPECT_EQ(slice.length(), 5);
427 EXPECT_EQ(slice.as_string_view(), "hello");
428 EXPECT_EQ(0, memcmp(slice.data(), "hello", 5));
429 slice[2] = 'm';
430 EXPECT_EQ(slice.as_string_view(), "hemlo");
431 for (auto& c : slice) c++;
432 EXPECT_EQ(slice.as_string_view(), "ifnmp");
433 }
434
435 } // namespace
436 } // namespace grpc_core
437
438 int main(int argc, char** argv) {
439 ::testing::InitGoogleTest(&argc, argv);
440 return RUN_ALL_TESTS();
441 }