5d6c1837e4d1c328cc5f7f587baa870d5c5a6458
[platform/upstream/grpc.git] / test / cpp / util / byte_buffer_test.cc
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 #include "test/core/util/test_config.h"
31
32 namespace grpc {
33
34 static internal::GrpcLibraryInitializer g_gli_initializer;
35
36 namespace {
37
38 const char* kContent1 = "hello xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
39 const char* kContent2 = "yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy world";
40
41 class ByteBufferTest : public ::testing::Test {
42  protected:
43   static void SetUpTestCase() { grpc_init(); }
44
45   static void TearDownTestCase() { grpc_shutdown(); }
46 };
47
48 TEST_F(ByteBufferTest, CopyCtor) {
49   ByteBuffer buffer1;
50   EXPECT_FALSE(buffer1.Valid());
51   const ByteBuffer& buffer2 = buffer1;
52   EXPECT_FALSE(buffer2.Valid());
53 }
54
55 TEST_F(ByteBufferTest, CreateFromSingleSlice) {
56   Slice s(kContent1);
57   ByteBuffer buffer(&s, 1);
58   EXPECT_EQ(strlen(kContent1), buffer.Length());
59 }
60
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());
67 }
68
69 TEST_F(ByteBufferTest, Clear) {
70   Slice s(kContent1);
71   ByteBuffer buffer(&s, 1);
72   buffer.Clear();
73   EXPECT_EQ(static_cast<size_t>(0), buffer.Length());
74 }
75
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());
82 }
83
84 bool SliceEqual(const Slice& a, grpc_slice b) {
85   if (a.size() != GRPC_SLICE_LENGTH(b)) {
86     return false;
87   }
88   for (size_t i = 0; i < a.size(); i++) {
89     if (a.begin()[i] != GRPC_SLICE_START_PTR(b)[i]) {
90       return false;
91     }
92   }
93   return true;
94 }
95
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);
103   slices.clear();
104   (void)buffer.Dump(&slices);
105   EXPECT_TRUE(SliceEqual(slices[0], hello));
106   EXPECT_TRUE(SliceEqual(slices[1], world));
107 }
108
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;
116   bool owned = false;
117   ByteBuffer buffer(&slices[0], 2);
118   slices.clear();
119   auto status = SerializationTraits<ByteBuffer, void>::Serialize(
120       buffer, &send_buffer, &owned);
121   EXPECT_TRUE(status.ok());
122   EXPECT_TRUE(owned);
123   EXPECT_TRUE(send_buffer.Valid());
124 }
125
126 TEST_F(ByteBufferTest, TrySingleSliceWithSingleSlice) {
127   std::vector<Slice> slices;
128   slices.emplace_back(kContent1);
129   ByteBuffer buffer(&slices[0], 1);
130   Slice slice;
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);
134 }
135
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);
141   Slice slice;
142   EXPECT_FALSE(buffer.TrySingleSlice(&slice).ok());
143 }
144
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);
150   Slice slice;
151   EXPECT_TRUE(buffer.DumpToSingleSlice(&slice).ok());
152   EXPECT_EQ(strlen(kContent1) + strlen(kContent2), slice.size());
153 }
154
155 }  // namespace
156 }  // namespace grpc
157
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();
162   return ret;
163 }