byte_buffer_test.cc
Go to the documentation of this file.
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 <cstring>
20 #include <vector>
21 
22 #include <gtest/gtest.h>
23 
25 #include <grpc/grpc.h>
26 #include <grpc/slice.h>
28 #include <grpcpp/support/slice.h>
29 
31 
32 namespace grpc {
33 
35 
36 namespace {
37 
38 const char* kContent1 = "hello xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
39 const char* kContent2 = "yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy world";
40 
41 class ByteBufferTest : public ::testing::Test {
42  protected:
43  static void SetUpTestCase() { grpc_init(); }
44 
45  static void TearDownTestCase() { grpc_shutdown(); }
46 };
47 
48 TEST_F(ByteBufferTest, CopyCtor) {
49  ByteBuffer buffer1;
50  EXPECT_FALSE(buffer1.Valid());
51  const ByteBuffer& buffer2 = buffer1;
52  EXPECT_FALSE(buffer2.Valid());
53 }
54 
55 TEST_F(ByteBufferTest, CreateFromSingleSlice) {
56  Slice s(kContent1);
57  ByteBuffer buffer(&s, 1);
58  EXPECT_EQ(strlen(kContent1), buffer.Length());
59 }
60 
61 TEST_F(ByteBufferTest, CreateFromVector) {
62  std::vector<Slice> slices;
63  slices.emplace_back(kContent1);
64  slices.emplace_back(kContent2);
65  ByteBuffer buffer(&slices[0], 2);
66  EXPECT_EQ(strlen(kContent1) + strlen(kContent2), buffer.Length());
67 }
68 
69 TEST_F(ByteBufferTest, Clear) {
70  Slice s(kContent1);
71  ByteBuffer buffer(&s, 1);
72  buffer.Clear();
73  EXPECT_EQ(static_cast<size_t>(0), buffer.Length());
74 }
75 
76 TEST_F(ByteBufferTest, Length) {
77  std::vector<Slice> slices;
78  slices.emplace_back(kContent1);
79  slices.emplace_back(kContent2);
80  ByteBuffer buffer(&slices[0], 2);
81  EXPECT_EQ(strlen(kContent1) + strlen(kContent2), buffer.Length());
82 }
83 
84 bool SliceEqual(const Slice& a, grpc_slice b) {
85  if (a.size() != GRPC_SLICE_LENGTH(b)) {
86  return false;
87  }
88  for (size_t i = 0; i < a.size(); i++) {
89  if (a.begin()[i] != GRPC_SLICE_START_PTR(b)[i]) {
90  return false;
91  }
92  }
93  return true;
94 }
95 
96 TEST_F(ByteBufferTest, Dump) {
98  grpc_slice world = grpc_slice_from_copied_string(kContent2);
99  std::vector<Slice> slices;
100  slices.push_back(Slice(hello, Slice::STEAL_REF));
101  slices.push_back(Slice(world, Slice::STEAL_REF));
102  ByteBuffer buffer(&slices[0], 2);
103  slices.clear();
104  (void)buffer.Dump(&slices);
105  EXPECT_TRUE(SliceEqual(slices[0], hello));
106  EXPECT_TRUE(SliceEqual(slices[1], world));
107 }
108 
109 TEST_F(ByteBufferTest, SerializationMakesCopy) {
111  grpc_slice world = grpc_slice_from_copied_string(kContent2);
112  std::vector<Slice> slices;
113  slices.push_back(Slice(hello, Slice::STEAL_REF));
114  slices.push_back(Slice(world, Slice::STEAL_REF));
116  bool owned = false;
117  ByteBuffer buffer(&slices[0], 2);
118  slices.clear();
120  buffer, &send_buffer, &owned);
121  EXPECT_TRUE(status.ok());
123  EXPECT_TRUE(send_buffer.Valid());
124 }
125 
126 TEST_F(ByteBufferTest, TrySingleSliceWithSingleSlice) {
127  std::vector<Slice> slices;
128  slices.emplace_back(kContent1);
129  ByteBuffer buffer(&slices[0], 1);
130  Slice slice;
131  EXPECT_TRUE(buffer.TrySingleSlice(&slice).ok());
132  EXPECT_EQ(slice.size(), slices[0].size());
133  EXPECT_EQ(memcmp(slice.begin(), slices[0].begin(), slice.size()), 0);
134 }
135 
136 TEST_F(ByteBufferTest, TrySingleSliceWithMultipleSlices) {
137  std::vector<Slice> slices;
138  slices.emplace_back(kContent1);
139  slices.emplace_back(kContent2);
140  ByteBuffer buffer(&slices[0], 2);
141  Slice slice;
142  EXPECT_FALSE(buffer.TrySingleSlice(&slice).ok());
143 }
144 
145 TEST_F(ByteBufferTest, DumpToSingleSlice) {
146  std::vector<Slice> slices;
147  slices.emplace_back(kContent1);
148  slices.emplace_back(kContent2);
149  ByteBuffer buffer(&slices[0], 2);
150  Slice slice;
151  EXPECT_TRUE(buffer.DumpToSingleSlice(&slice).ok());
152  EXPECT_EQ(strlen(kContent1) + strlen(kContent2), slice.size());
153 }
154 
155 } // namespace
156 } // namespace grpc
157 
158 int main(int argc, char** argv) {
159  grpc::testing::TestEnvironment env(&argc, argv);
160  ::testing::InitGoogleTest(&argc, argv);
161  int ret = RUN_ALL_TESTS();
162  return ret;
163 }
EXPECT_FALSE
#define EXPECT_FALSE(condition)
Definition: bloaty/third_party/googletest/googletest/include/gtest/gtest.h:1970
grpc::status
auto status
Definition: cpp/client/credentials_test.cc:200
generate.env
env
Definition: generate.py:37
send_buffer
static char * send_buffer
Definition: test-tcp-writealot.c:38
grpc
Definition: grpcpp/alarm.h:33
grpc_slice_from_copied_string
GPRAPI grpc_slice grpc_slice_from_copied_string(const char *source)
Definition: slice/slice.cc:177
slice.h
re2::Dump
static void Dump(StringPiece pattern, Regexp::ParseFlags flags, std::string *forward, std::string *reverse)
Definition: bloaty/third_party/re2/re2/testing/compile_test.cc:242
grpc::g_gli_initializer
static grpc::internal::GrpcLibraryInitializer g_gli_initializer
Definition: channel_cc.cc:52
slice.h
a
int a
Definition: abseil-cpp/absl/container/internal/hash_policy_traits_test.cc:88
grpc::EXPECT_TRUE
EXPECT_TRUE(status.ok())
testing::Test
Definition: bloaty/third_party/googletest/googletest/include/gtest/gtest.h:402
grpc::Slice::STEAL_REF
@ STEAL_REF
Definition: include/grpcpp/impl/codegen/slice.h:48
grpc::SerializationTraits
Definition: grpcpp/impl/codegen/serialization_traits.h:60
grpc::ByteBuffer::Valid
bool Valid() const
Is this ByteBuffer valid?
Definition: include/grpcpp/impl/codegen/byte_buffer.h:164
hello
static z_const char hello[]
Definition: bloaty/third_party/zlib/test/example.c:29
absl::Length
size_t Length(TestCordSize size)
Definition: abseil-cpp/absl/strings/cord_test_helpers.h:80
grpc::internal::GrpcLibraryInitializer
Instantiating this class ensures the proper initialization of gRPC.
Definition: grpcpp/impl/grpc_library.h:39
slice
grpc_slice slice
Definition: src/core/lib/surface/server.cc:467
grpc.h
GRPC_SLICE_START_PTR
#define GRPC_SLICE_START_PTR(slice)
Definition: include/grpc/impl/codegen/slice.h:101
grpc_slice
Definition: include/grpc/impl/codegen/slice.h:65
grpc::ByteBuffer
A sequence of bytes.
Definition: include/grpcpp/impl/codegen/byte_buffer.h:61
buffer
char buffer[1024]
Definition: libuv/docs/code/idle-compute/main.c:8
RUN_ALL_TESTS
int RUN_ALL_TESTS() GTEST_MUST_USE_RESULT_
Definition: bloaty/third_party/googletest/googletest/include/gtest/gtest.h:2471
b
uint64_t b
Definition: abseil-cpp/absl/container/internal/layout_test.cc:53
GRPC_SLICE_LENGTH
#define GRPC_SLICE_LENGTH(slice)
Definition: include/grpc/impl/codegen/slice.h:104
test_config.h
byte_buffer.h
testing::InitGoogleTest
GTEST_API_ void InitGoogleTest(int *argc, char **argv)
Definition: bloaty/third_party/googletest/googletest/src/gtest.cc:6106
grpc_library.h
google::protobuf.internal.python_message.Clear
Clear
Definition: bloaty/third_party/protobuf/python/google/protobuf/internal/python_message.py:1430
ret
UniquePtr< SSL_SESSION > ret
Definition: ssl_x509.cc:1029
grpc::testing::TestEnvironment
Definition: test/core/util/test_config.h:54
slices
SliceBuffer * slices
Definition: retry_filter.cc:631
main
int main(int argc, char **argv)
Definition: byte_buffer_test.cc:158
grpc::EXPECT_EQ
EXPECT_EQ(grpc::StatusCode::INVALID_ARGUMENT, status.error_code())
grpc::Slice
Definition: include/grpcpp/impl/codegen/slice.h:36
grpc_init
GRPCAPI void grpc_init(void)
Definition: init.cc:146
grpc_shutdown
GRPCAPI void grpc_shutdown(void)
Definition: init.cc:209
i
uint64_t i
Definition: abseil-cpp/absl/container/btree_benchmark.cc:230
owned
bool owned
Definition: src/php/ext/grpc/call.h:31
TEST_F
#define TEST_F(test_fixture, test_name)
Definition: bloaty/third_party/googletest/googletest/include/gtest/gtest.h:2367


grpc
Author(s):
autogenerated on Fri May 16 2025 02:57:51