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
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>
29
30 #include "test/core/util/test_config.h"
31
32 namespace grpc {
33
34 namespace {
35
36 const char* kContent1 = "hello xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
37 const char* kContent2 = "yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy world";
38
39 class ByteBufferTest : public ::testing::Test {
40 protected:
SetUpTestSuite()41 static void SetUpTestSuite() { grpc_init(); }
42
TearDownTestSuite()43 static void TearDownTestSuite() { grpc_shutdown(); }
44 };
45
TEST_F(ByteBufferTest,CopyCtor)46 TEST_F(ByteBufferTest, CopyCtor) {
47 ByteBuffer buffer1;
48 EXPECT_FALSE(buffer1.Valid());
49 const ByteBuffer& buffer2 = buffer1;
50 EXPECT_FALSE(buffer2.Valid());
51 }
52
TEST_F(ByteBufferTest,CreateFromSingleSlice)53 TEST_F(ByteBufferTest, CreateFromSingleSlice) {
54 Slice s(kContent1);
55 ByteBuffer buffer(&s, 1);
56 EXPECT_EQ(strlen(kContent1), buffer.Length());
57 }
58
TEST_F(ByteBufferTest,CreateFromVector)59 TEST_F(ByteBufferTest, CreateFromVector) {
60 std::vector<Slice> slices;
61 slices.emplace_back(kContent1);
62 slices.emplace_back(kContent2);
63 ByteBuffer buffer(&slices[0], 2);
64 EXPECT_EQ(strlen(kContent1) + strlen(kContent2), buffer.Length());
65 }
66
TEST_F(ByteBufferTest,Clear)67 TEST_F(ByteBufferTest, Clear) {
68 Slice s(kContent1);
69 ByteBuffer buffer(&s, 1);
70 buffer.Clear();
71 EXPECT_EQ(0, buffer.Length());
72 }
73
TEST_F(ByteBufferTest,Length)74 TEST_F(ByteBufferTest, Length) {
75 std::vector<Slice> slices;
76 slices.emplace_back(kContent1);
77 slices.emplace_back(kContent2);
78 ByteBuffer buffer(&slices[0], 2);
79 EXPECT_EQ(strlen(kContent1) + strlen(kContent2), buffer.Length());
80 }
81
SliceEqual(const Slice & a,grpc_slice b)82 bool SliceEqual(const Slice& a, grpc_slice b) {
83 if (a.size() != GRPC_SLICE_LENGTH(b)) {
84 return false;
85 }
86 for (size_t i = 0; i < a.size(); i++) {
87 if (a.begin()[i] != GRPC_SLICE_START_PTR(b)[i]) {
88 return false;
89 }
90 }
91 return true;
92 }
93
TEST_F(ByteBufferTest,Dump)94 TEST_F(ByteBufferTest, Dump) {
95 grpc_slice hello = grpc_slice_from_copied_string(kContent1);
96 grpc_slice world = grpc_slice_from_copied_string(kContent2);
97 std::vector<Slice> slices;
98 slices.push_back(Slice(hello, Slice::STEAL_REF));
99 slices.push_back(Slice(world, Slice::STEAL_REF));
100 ByteBuffer buffer(&slices[0], 2);
101 slices.clear();
102 (void)buffer.Dump(&slices);
103 EXPECT_TRUE(SliceEqual(slices[0], hello));
104 EXPECT_TRUE(SliceEqual(slices[1], world));
105 }
106
TEST_F(ByteBufferTest,SerializationMakesCopy)107 TEST_F(ByteBufferTest, SerializationMakesCopy) {
108 grpc_slice hello = grpc_slice_from_copied_string(kContent1);
109 grpc_slice world = grpc_slice_from_copied_string(kContent2);
110 std::vector<Slice> slices;
111 slices.push_back(Slice(hello, Slice::STEAL_REF));
112 slices.push_back(Slice(world, Slice::STEAL_REF));
113 ByteBuffer send_buffer;
114 bool owned = false;
115 ByteBuffer buffer(&slices[0], 2);
116 slices.clear();
117 auto status =
118 SerializationTraits<ByteBuffer>::Serialize(buffer, &send_buffer, &owned);
119 EXPECT_TRUE(status.ok());
120 EXPECT_TRUE(owned);
121 EXPECT_TRUE(send_buffer.Valid());
122 }
123
TEST_F(ByteBufferTest,TrySingleSliceWithSingleSlice)124 TEST_F(ByteBufferTest, TrySingleSliceWithSingleSlice) {
125 std::vector<Slice> slices;
126 slices.emplace_back(kContent1);
127 ByteBuffer buffer(&slices[0], 1);
128 Slice slice;
129 EXPECT_TRUE(buffer.TrySingleSlice(&slice).ok());
130 EXPECT_EQ(slice.size(), slices[0].size());
131 EXPECT_EQ(memcmp(slice.begin(), slices[0].begin(), slice.size()), 0);
132 }
133
TEST_F(ByteBufferTest,TrySingleSliceWithMultipleSlices)134 TEST_F(ByteBufferTest, TrySingleSliceWithMultipleSlices) {
135 std::vector<Slice> slices;
136 slices.emplace_back(kContent1);
137 slices.emplace_back(kContent2);
138 ByteBuffer buffer(&slices[0], 2);
139 Slice slice;
140 EXPECT_FALSE(buffer.TrySingleSlice(&slice).ok());
141 }
142
TEST_F(ByteBufferTest,DumpToSingleSlice)143 TEST_F(ByteBufferTest, DumpToSingleSlice) {
144 std::vector<Slice> slices;
145 slices.emplace_back(kContent1);
146 slices.emplace_back(kContent2);
147 ByteBuffer buffer(&slices[0], 2);
148 Slice slice;
149 EXPECT_TRUE(buffer.DumpToSingleSlice(&slice).ok());
150 EXPECT_EQ(strlen(kContent1) + strlen(kContent2), slice.size());
151 }
152
153 } // namespace
154 } // namespace grpc
155
main(int argc,char ** argv)156 int main(int argc, char** argv) {
157 grpc::testing::TestEnvironment env(&argc, argv);
158 ::testing::InitGoogleTest(&argc, argv);
159 int ret = RUN_ALL_TESTS();
160 return ret;
161 }
162