mirror of https://github.com/grpc/grpc.git
The C based gRPC (C++, Python, Ruby, Objective-C, PHP, C#)
https://grpc.io/
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
266 lines
8.7 KiB
266 lines
8.7 KiB
10 years ago
|
/*
|
||
|
*
|
||
10 years ago
|
* Copyright 2015, Google Inc.
|
||
10 years ago
|
* All rights reserved.
|
||
|
*
|
||
|
* Redistribution and use in source and binary forms, with or without
|
||
|
* modification, are permitted provided that the following conditions are
|
||
|
* met:
|
||
|
*
|
||
|
* * Redistributions of source code must retain the above copyright
|
||
|
* notice, this list of conditions and the following disclaimer.
|
||
|
* * Redistributions in binary form must reproduce the above
|
||
|
* copyright notice, this list of conditions and the following disclaimer
|
||
|
* in the documentation and/or other materials provided with the
|
||
|
* distribution.
|
||
|
* * Neither the name of Google Inc. nor the names of its
|
||
|
* contributors may be used to endorse or promote products derived from
|
||
|
* this software without specific prior written permission.
|
||
|
*
|
||
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
||
|
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
||
|
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
||
|
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
||
|
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
||
|
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
||
|
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
||
|
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
||
|
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||
|
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
||
|
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||
|
*
|
||
|
*/
|
||
|
|
||
|
#include <grpc/support/slice.h>
|
||
|
|
||
|
#include <string.h>
|
||
|
|
||
10 years ago
|
#include <grpc/support/alloc.h>
|
||
10 years ago
|
#include <grpc/support/log.h>
|
||
|
#include "test/core/util/test_config.h"
|
||
|
|
||
10 years ago
|
#define LOG_TEST_NAME(x) gpr_log(GPR_INFO, "%s", x);
|
||
10 years ago
|
|
||
10 years ago
|
static void test_slice_malloc_returns_something_sensible(void) {
|
||
8 years ago
|
/* Calls grpc_slice_create for various lengths and verifies the internals for
|
||
10 years ago
|
consistency. */
|
||
|
size_t length;
|
||
|
size_t i;
|
||
8 years ago
|
grpc_slice slice;
|
||
10 years ago
|
|
||
10 years ago
|
LOG_TEST_NAME("test_slice_malloc_returns_something_sensible");
|
||
10 years ago
|
|
||
|
for (length = 0; length <= 1024; length++) {
|
||
8 years ago
|
slice = grpc_slice_malloc(length);
|
||
10 years ago
|
/* If there is a length, slice.data must be non-NULL. If length is zero
|
||
|
we don't care. */
|
||
|
if (length) {
|
||
|
GPR_ASSERT(GPR_SLICE_START_PTR(slice));
|
||
|
}
|
||
|
/* Returned slice length must be what was requested. */
|
||
|
GPR_ASSERT(GPR_SLICE_LENGTH(slice) == length);
|
||
|
/* If the slice has a refcount, it must be destroyable. */
|
||
|
if (slice.refcount) {
|
||
|
GPR_ASSERT(slice.refcount->ref != NULL);
|
||
|
GPR_ASSERT(slice.refcount->unref != NULL);
|
||
|
}
|
||
|
/* We must be able to write to every byte of the data */
|
||
|
for (i = 0; i < length; i++) {
|
||
9 years ago
|
GPR_SLICE_START_PTR(slice)[i] = (uint8_t)i;
|
||
10 years ago
|
}
|
||
|
/* And finally we must succeed in destroying the slice */
|
||
8 years ago
|
grpc_slice_unref(slice);
|
||
10 years ago
|
}
|
||
|
}
|
||
|
|
||
|
static void do_nothing(void *ignored) {}
|
||
|
|
||
10 years ago
|
static void test_slice_new_returns_something_sensible(void) {
|
||
9 years ago
|
uint8_t x;
|
||
10 years ago
|
|
||
8 years ago
|
grpc_slice slice = grpc_slice_new(&x, 1, do_nothing);
|
||
10 years ago
|
GPR_ASSERT(slice.refcount);
|
||
|
GPR_ASSERT(slice.data.refcounted.bytes == &x);
|
||
|
GPR_ASSERT(slice.data.refcounted.length == 1);
|
||
8 years ago
|
grpc_slice_unref(slice);
|
||
10 years ago
|
}
|
||
|
|
||
8 years ago
|
/* destroy function that sets a mark to indicate it was called. */
|
||
|
static void set_mark(void *p) { *((int *)p) = 1; }
|
||
|
|
||
|
static void test_slice_new_with_user_data(void) {
|
||
|
int marker = 0;
|
||
|
uint8_t buf[2];
|
||
8 years ago
|
grpc_slice slice;
|
||
8 years ago
|
|
||
|
buf[0] = 0;
|
||
|
buf[1] = 1;
|
||
8 years ago
|
slice = grpc_slice_new_with_user_data(buf, 2, set_mark, &marker);
|
||
8 years ago
|
GPR_ASSERT(marker == 0);
|
||
|
GPR_ASSERT(GPR_SLICE_LENGTH(slice) == 2);
|
||
|
GPR_ASSERT(GPR_SLICE_START_PTR(slice)[0] == 0);
|
||
|
GPR_ASSERT(GPR_SLICE_START_PTR(slice)[1] == 1);
|
||
|
|
||
|
/* unref should cause destroy function to run. */
|
||
8 years ago
|
grpc_slice_unref(slice);
|
||
8 years ago
|
GPR_ASSERT(marker == 1);
|
||
|
}
|
||
|
|
||
10 years ago
|
static int do_nothing_with_len_1_calls = 0;
|
||
|
|
||
|
static void do_nothing_with_len_1(void *ignored, size_t len) {
|
||
|
GPR_ASSERT(len == 1);
|
||
|
do_nothing_with_len_1_calls++;
|
||
|
}
|
||
|
|
||
10 years ago
|
static void test_slice_new_with_len_returns_something_sensible(void) {
|
||
9 years ago
|
uint8_t x;
|
||
9 years ago
|
int num_refs = 5; /* To test adding/removing an arbitrary number of refs */
|
||
|
int i;
|
||
10 years ago
|
|
||
8 years ago
|
grpc_slice slice = grpc_slice_new_with_len(&x, 1, do_nothing_with_len_1);
|
||
9 years ago
|
GPR_ASSERT(slice.refcount); /* ref count is initialized to 1 at this point */
|
||
10 years ago
|
GPR_ASSERT(slice.data.refcounted.bytes == &x);
|
||
|
GPR_ASSERT(slice.data.refcounted.length == 1);
|
||
|
GPR_ASSERT(do_nothing_with_len_1_calls == 0);
|
||
9 years ago
|
|
||
|
/* Add an arbitrary number of refs to the slice and remoe the refs. This is to
|
||
|
make sure that that the destroy callback (i.e do_nothing_with_len_1()) is
|
||
|
not called until the last unref operation */
|
||
|
for (i = 0; i < num_refs; i++) {
|
||
8 years ago
|
grpc_slice_ref(slice);
|
||
9 years ago
|
}
|
||
|
for (i = 0; i < num_refs; i++) {
|
||
8 years ago
|
grpc_slice_unref(slice);
|
||
9 years ago
|
}
|
||
|
GPR_ASSERT(do_nothing_with_len_1_calls == 0); /* Shouldn't be called yet */
|
||
|
|
||
|
/* last unref */
|
||
8 years ago
|
grpc_slice_unref(slice);
|
||
10 years ago
|
GPR_ASSERT(do_nothing_with_len_1_calls == 1);
|
||
|
}
|
||
|
|
||
10 years ago
|
static void test_slice_sub_works(unsigned length) {
|
||
8 years ago
|
grpc_slice slice;
|
||
|
grpc_slice sub;
|
||
10 years ago
|
unsigned i, j, k;
|
||
10 years ago
|
|
||
10 years ago
|
LOG_TEST_NAME("test_slice_sub_works");
|
||
10 years ago
|
gpr_log(GPR_INFO, "length=%d", length);
|
||
|
|
||
|
/* Create a slice in which each byte is equal to the distance from it to the
|
||
|
beginning of the slice. */
|
||
8 years ago
|
slice = grpc_slice_malloc(length);
|
||
10 years ago
|
for (i = 0; i < length; i++) {
|
||
9 years ago
|
GPR_SLICE_START_PTR(slice)[i] = (uint8_t)i;
|
||
10 years ago
|
}
|
||
|
|
||
|
/* Ensure that for all subsets length is correct and that we start on the
|
||
|
correct byte. Additionally check that no copies were made. */
|
||
|
for (i = 0; i < length; i++) {
|
||
|
for (j = i; j < length; j++) {
|
||
8 years ago
|
sub = grpc_slice_sub(slice, i, j);
|
||
10 years ago
|
GPR_ASSERT(GPR_SLICE_LENGTH(sub) == j - i);
|
||
|
for (k = 0; k < j - i; k++) {
|
||
9 years ago
|
GPR_ASSERT(GPR_SLICE_START_PTR(sub)[k] == (uint8_t)(i + k));
|
||
10 years ago
|
}
|
||
8 years ago
|
grpc_slice_unref(sub);
|
||
10 years ago
|
}
|
||
|
}
|
||
8 years ago
|
grpc_slice_unref(slice);
|
||
10 years ago
|
}
|
||
|
|
||
8 years ago
|
static void check_head_tail(grpc_slice slice, grpc_slice head, grpc_slice tail) {
|
||
10 years ago
|
GPR_ASSERT(GPR_SLICE_LENGTH(slice) ==
|
||
|
GPR_SLICE_LENGTH(head) + GPR_SLICE_LENGTH(tail));
|
||
|
GPR_ASSERT(0 == memcmp(GPR_SLICE_START_PTR(slice), GPR_SLICE_START_PTR(head),
|
||
|
GPR_SLICE_LENGTH(head)));
|
||
|
GPR_ASSERT(0 == memcmp(GPR_SLICE_START_PTR(slice) + GPR_SLICE_LENGTH(head),
|
||
|
GPR_SLICE_START_PTR(tail), GPR_SLICE_LENGTH(tail)));
|
||
|
}
|
||
|
|
||
9 years ago
|
static void test_slice_split_head_works(size_t length) {
|
||
8 years ago
|
grpc_slice slice;
|
||
|
grpc_slice head, tail;
|
||
9 years ago
|
size_t i;
|
||
10 years ago
|
|
||
10 years ago
|
LOG_TEST_NAME("test_slice_split_head_works");
|
||
9 years ago
|
gpr_log(GPR_INFO, "length=%" PRIuPTR, length);
|
||
10 years ago
|
|
||
|
/* Create a slice in which each byte is equal to the distance from it to the
|
||
|
beginning of the slice. */
|
||
8 years ago
|
slice = grpc_slice_malloc(length);
|
||
10 years ago
|
for (i = 0; i < length; i++) {
|
||
9 years ago
|
GPR_SLICE_START_PTR(slice)[i] = (uint8_t)i;
|
||
10 years ago
|
}
|
||
|
|
||
|
/* Ensure that for all subsets length is correct and that we start on the
|
||
|
correct byte. Additionally check that no copies were made. */
|
||
|
for (i = 0; i < length; i++) {
|
||
8 years ago
|
tail = grpc_slice_ref(slice);
|
||
|
head = grpc_slice_split_head(&tail, i);
|
||
10 years ago
|
check_head_tail(slice, head, tail);
|
||
8 years ago
|
grpc_slice_unref(tail);
|
||
|
grpc_slice_unref(head);
|
||
10 years ago
|
}
|
||
|
|
||
8 years ago
|
grpc_slice_unref(slice);
|
||
10 years ago
|
}
|
||
|
|
||
9 years ago
|
static void test_slice_split_tail_works(size_t length) {
|
||
8 years ago
|
grpc_slice slice;
|
||
|
grpc_slice head, tail;
|
||
9 years ago
|
size_t i;
|
||
10 years ago
|
|
||
10 years ago
|
LOG_TEST_NAME("test_slice_split_tail_works");
|
||
9 years ago
|
gpr_log(GPR_INFO, "length=%" PRIuPTR, length);
|
||
10 years ago
|
|
||
|
/* Create a slice in which each byte is equal to the distance from it to the
|
||
|
beginning of the slice. */
|
||
8 years ago
|
slice = grpc_slice_malloc(length);
|
||
10 years ago
|
for (i = 0; i < length; i++) {
|
||
9 years ago
|
GPR_SLICE_START_PTR(slice)[i] = (uint8_t)i;
|
||
10 years ago
|
}
|
||
|
|
||
|
/* Ensure that for all subsets length is correct and that we start on the
|
||
|
correct byte. Additionally check that no copies were made. */
|
||
|
for (i = 0; i < length; i++) {
|
||
8 years ago
|
head = grpc_slice_ref(slice);
|
||
|
tail = grpc_slice_split_tail(&head, i);
|
||
10 years ago
|
check_head_tail(slice, head, tail);
|
||
8 years ago
|
grpc_slice_unref(tail);
|
||
|
grpc_slice_unref(head);
|
||
10 years ago
|
}
|
||
|
|
||
8 years ago
|
grpc_slice_unref(slice);
|
||
10 years ago
|
}
|
||
|
|
||
10 years ago
|
static void test_slice_from_copied_string_works(void) {
|
||
10 years ago
|
static const char *text = "HELLO WORLD!";
|
||
8 years ago
|
grpc_slice slice;
|
||
10 years ago
|
|
||
10 years ago
|
LOG_TEST_NAME("test_slice_from_copied_string_works");
|
||
10 years ago
|
|
||
8 years ago
|
slice = grpc_slice_from_copied_string(text);
|
||
10 years ago
|
GPR_ASSERT(strlen(text) == GPR_SLICE_LENGTH(slice));
|
||
|
GPR_ASSERT(0 ==
|
||
|
memcmp(text, GPR_SLICE_START_PTR(slice), GPR_SLICE_LENGTH(slice)));
|
||
8 years ago
|
grpc_slice_unref(slice);
|
||
10 years ago
|
}
|
||
|
|
||
|
int main(int argc, char **argv) {
|
||
10 years ago
|
unsigned length;
|
||
10 years ago
|
grpc_test_init(argc, argv);
|
||
|
test_slice_malloc_returns_something_sensible();
|
||
|
test_slice_new_returns_something_sensible();
|
||
8 years ago
|
test_slice_new_with_user_data();
|
||
10 years ago
|
test_slice_new_with_len_returns_something_sensible();
|
||
|
for (length = 0; length < 128; length++) {
|
||
|
test_slice_sub_works(length);
|
||
|
test_slice_split_head_works(length);
|
||
|
test_slice_split_tail_works(length);
|
||
|
}
|
||
|
test_slice_from_copied_string_works();
|
||
|
return 0;
|
||
10 years ago
|
}
|