• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //
2 // Copyright 2023 gRPC authors.
3 //
4 // Licensed under the Apache License, Version 2.0 (the "License");
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
7 //
8 //     http://www.apache.org/licenses/LICENSE-2.0
9 //
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
15 //
16 
17 #include <grpcpp/support/byte_buffer.h>
18 #include <grpcpp/support/proto_buffer_writer.h>
19 #include <gtest/gtest.h>
20 
21 #include "test/core/test_util/test_config.h"
22 
23 namespace grpc {
24 namespace {
25 
TEST(ProtoBufferWriterTest,Next)26 TEST(ProtoBufferWriterTest, Next) {
27   ByteBuffer buffer;
28   ProtoBufferWriter writer(&buffer, 16, 256);
29   // 1st next
30   void* data1;
31   int size1;
32   EXPECT_TRUE(writer.Next(&data1, &size1));
33   EXPECT_GT(size1, 0);
34   memset(data1, 1, size1);
35   // 2nd next
36   void* data2;
37   int size2;
38   EXPECT_TRUE(writer.Next(&data2, &size2));
39   EXPECT_GT(size2, 0);
40   memset(data2, 2, size2);
41   // Done
42   EXPECT_EQ(writer.ByteCount(), size1 + size2);
43   EXPECT_EQ(buffer.Length(), size1 + size2);
44   Slice slice;
45   EXPECT_TRUE(buffer.DumpToSingleSlice(&slice).ok());
46   EXPECT_EQ(memcmp(slice.begin(), data1, size1), 0);
47   EXPECT_EQ(memcmp(slice.begin() + size1, data2, size2), 0);
48 }
49 
50 #ifdef GRPC_PROTOBUF_CORD_SUPPORT_ENABLED
51 
TEST(ProtoBufferWriterTest,WriteCord)52 TEST(ProtoBufferWriterTest, WriteCord) {
53   ByteBuffer buffer;
54   ProtoBufferWriter writer(&buffer, 16, 4096);
55   // Cord
56   absl::Cord cord;
57   std::string str1 = std::string(1024, 'a');
58   cord.Append(str1);
59   std::string str2 = std::string(1024, 'b');
60   cord.Append(str2);
61   writer.WriteCord(cord);
62   // Done
63   EXPECT_EQ(writer.ByteCount(), str1.size() + str2.size());
64   EXPECT_EQ(buffer.Length(), str1.size() + str2.size());
65   Slice slice;
66   EXPECT_TRUE(buffer.DumpToSingleSlice(&slice).ok());
67   EXPECT_EQ(memcmp(slice.begin() + str1.size(), str2.c_str(), str2.size()), 0);
68 }
69 
70 #endif  // GRPC_PROTOBUF_CORD_SUPPORT_ENABLED
71 
72 }  // namespace
73 }  // namespace grpc
74 
main(int argc,char ** argv)75 int main(int argc, char** argv) {
76   grpc::testing::TestEnvironment env(&argc, argv);
77   ::testing::InitGoogleTest(&argc, argv);
78   return RUN_ALL_TESTS();
79 }
80