• 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 <grpcpp/impl/grpc_library.h>
21 
22 #include <cstring>
23 #include <vector>
24 
25 #include <grpc/grpc.h>
26 #include <grpc/slice.h>
27 #include <grpcpp/support/slice.h>
28 #include <gtest/gtest.h>
29 
30 namespace grpc {
31 
32 static internal::GrpcLibraryInitializer g_gli_initializer;
33 
34 namespace {
35 
36 const char* kContent1 = "hello xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
37 const char* kContent2 = "yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy world";
38 
39 class ByteBufferTest : public ::testing::Test {};
40 
TEST_F(ByteBufferTest,CopyCtor)41 TEST_F(ByteBufferTest, CopyCtor) {
42   ByteBuffer buffer1;
43   EXPECT_FALSE(buffer1.Valid());
44   const ByteBuffer& buffer2 = buffer1;
45   EXPECT_FALSE(buffer2.Valid());
46 }
47 
TEST_F(ByteBufferTest,CreateFromSingleSlice)48 TEST_F(ByteBufferTest, CreateFromSingleSlice) {
49   Slice s(kContent1);
50   ByteBuffer buffer(&s, 1);
51   EXPECT_EQ(strlen(kContent1), buffer.Length());
52 }
53 
TEST_F(ByteBufferTest,CreateFromVector)54 TEST_F(ByteBufferTest, CreateFromVector) {
55   std::vector<Slice> slices;
56   slices.emplace_back(kContent1);
57   slices.emplace_back(kContent2);
58   ByteBuffer buffer(&slices[0], 2);
59   EXPECT_EQ(strlen(kContent1) + strlen(kContent2), buffer.Length());
60 }
61 
TEST_F(ByteBufferTest,Clear)62 TEST_F(ByteBufferTest, Clear) {
63   Slice s(kContent1);
64   ByteBuffer buffer(&s, 1);
65   buffer.Clear();
66   EXPECT_EQ(static_cast<size_t>(0), buffer.Length());
67 }
68 
TEST_F(ByteBufferTest,Length)69 TEST_F(ByteBufferTest, Length) {
70   std::vector<Slice> slices;
71   slices.emplace_back(kContent1);
72   slices.emplace_back(kContent2);
73   ByteBuffer buffer(&slices[0], 2);
74   EXPECT_EQ(strlen(kContent1) + strlen(kContent2), buffer.Length());
75 }
76 
SliceEqual(const Slice & a,grpc_slice b)77 bool SliceEqual(const Slice& a, grpc_slice b) {
78   if (a.size() != GRPC_SLICE_LENGTH(b)) {
79     return false;
80   }
81   for (size_t i = 0; i < a.size(); i++) {
82     if (a.begin()[i] != GRPC_SLICE_START_PTR(b)[i]) {
83       return false;
84     }
85   }
86   return true;
87 }
88 
TEST_F(ByteBufferTest,Dump)89 TEST_F(ByteBufferTest, Dump) {
90   grpc_slice hello = grpc_slice_from_copied_string(kContent1);
91   grpc_slice world = grpc_slice_from_copied_string(kContent2);
92   std::vector<Slice> slices;
93   slices.push_back(Slice(hello, Slice::STEAL_REF));
94   slices.push_back(Slice(world, Slice::STEAL_REF));
95   ByteBuffer buffer(&slices[0], 2);
96   slices.clear();
97   (void)buffer.Dump(&slices);
98   EXPECT_TRUE(SliceEqual(slices[0], hello));
99   EXPECT_TRUE(SliceEqual(slices[1], world));
100 }
101 
TEST_F(ByteBufferTest,SerializationMakesCopy)102 TEST_F(ByteBufferTest, SerializationMakesCopy) {
103   grpc_slice hello = grpc_slice_from_copied_string(kContent1);
104   grpc_slice world = grpc_slice_from_copied_string(kContent2);
105   std::vector<Slice> slices;
106   slices.push_back(Slice(hello, Slice::STEAL_REF));
107   slices.push_back(Slice(world, Slice::STEAL_REF));
108   ByteBuffer send_buffer;
109   bool owned = false;
110   ByteBuffer buffer(&slices[0], 2);
111   slices.clear();
112   auto status = SerializationTraits<ByteBuffer, void>::Serialize(
113       buffer, &send_buffer, &owned);
114   EXPECT_TRUE(status.ok());
115   EXPECT_TRUE(owned);
116   EXPECT_TRUE(send_buffer.Valid());
117 }
118 
119 }  // namespace
120 }  // namespace grpc
121 
main(int argc,char ** argv)122 int main(int argc, char** argv) {
123   ::testing::InitGoogleTest(&argc, argv);
124   grpc_init();
125   int ret = RUN_ALL_TESTS();
126   grpc_shutdown();
127   return ret;
128 }
129