3 * Copyright 2015 gRPC authors.
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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
22 #include <gtest/gtest.h>
24 #include <grpc++/support/byte_buffer.h>
25 #include <grpc/grpc.h>
26 #include <grpc/slice.h>
27 #include <grpcpp/impl/grpc_library.h>
28 #include <grpcpp/support/slice.h>
30 #include "test/core/util/test_config.h"
34 static internal::GrpcLibraryInitializer g_gli_initializer;
38 const char* kContent1 = "hello xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
39 const char* kContent2 = "yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy world";
41 class ByteBufferTest : public ::testing::Test {
43 static void SetUpTestCase() { grpc_init(); }
45 static void TearDownTestCase() { grpc_shutdown(); }
48 TEST_F(ByteBufferTest, CopyCtor) {
50 EXPECT_FALSE(buffer1.Valid());
51 const ByteBuffer& buffer2 = buffer1;
52 EXPECT_FALSE(buffer2.Valid());
55 TEST_F(ByteBufferTest, CreateFromSingleSlice) {
57 ByteBuffer buffer(&s, 1);
58 EXPECT_EQ(strlen(kContent1), buffer.Length());
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());
69 TEST_F(ByteBufferTest, Clear) {
71 ByteBuffer buffer(&s, 1);
73 EXPECT_EQ(static_cast<size_t>(0), buffer.Length());
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());
84 bool SliceEqual(const Slice& a, grpc_slice b) {
85 if (a.size() != GRPC_SLICE_LENGTH(b)) {
88 for (size_t i = 0; i < a.size(); i++) {
89 if (a.begin()[i] != GRPC_SLICE_START_PTR(b)[i]) {
96 TEST_F(ByteBufferTest, Dump) {
97 grpc_slice hello = grpc_slice_from_copied_string(kContent1);
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);
104 (void)buffer.Dump(&slices);
105 EXPECT_TRUE(SliceEqual(slices[0], hello));
106 EXPECT_TRUE(SliceEqual(slices[1], world));
109 TEST_F(ByteBufferTest, SerializationMakesCopy) {
110 grpc_slice hello = grpc_slice_from_copied_string(kContent1);
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));
115 ByteBuffer send_buffer;
117 ByteBuffer buffer(&slices[0], 2);
119 auto status = SerializationTraits<ByteBuffer, void>::Serialize(
120 buffer, &send_buffer, &owned);
121 EXPECT_TRUE(status.ok());
123 EXPECT_TRUE(send_buffer.Valid());
126 TEST_F(ByteBufferTest, TrySingleSliceWithSingleSlice) {
127 std::vector<Slice> slices;
128 slices.emplace_back(kContent1);
129 ByteBuffer buffer(&slices[0], 1);
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);
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);
142 EXPECT_FALSE(buffer.TrySingleSlice(&slice).ok());
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);
151 EXPECT_TRUE(buffer.DumpToSingleSlice(&slice).ok());
152 EXPECT_EQ(strlen(kContent1) + strlen(kContent2), slice.size());
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();