• 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   protected:
SetUpTestCase()41    static void SetUpTestCase() { grpc_init(); }
42  
TearDownTestCase()43    static void TearDownTestCase() { 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(static_cast<size_t>(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 = SerializationTraits<ByteBuffer, void>::Serialize(
118        buffer, &send_buffer, &owned);
119    EXPECT_TRUE(status.ok());
120    EXPECT_TRUE(owned);
121    EXPECT_TRUE(send_buffer.Valid());
122  }
123  
124  }  // namespace
125  }  // namespace grpc
126  
main(int argc,char ** argv)127  int main(int argc, char** argv) {
128    ::testing::InitGoogleTest(&argc, argv);
129    int ret = RUN_ALL_TESTS();
130    return ret;
131  }
132