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