• 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_reader.h>
19 #include <gtest/gtest.h>
20 
21 #include "test/core/test_util/test_config.h"
22 
23 namespace grpc {
24 namespace {
25 
ExpectBufferEqual(const ByteBuffer & a,const ByteBuffer & b)26 void ExpectBufferEqual(const ByteBuffer& a, const ByteBuffer& b) {
27   Slice a_slice;
28   EXPECT_TRUE(a.DumpToSingleSlice(&a_slice).ok());
29   Slice b_slice;
30   EXPECT_TRUE(b.DumpToSingleSlice(&b_slice).ok());
31   EXPECT_EQ(a_slice.size(), b_slice.size());
32   EXPECT_EQ(memcmp(a_slice.begin(), b_slice.begin(), a_slice.size()), 0);
33 }
34 
TEST(ProtoBufferReaderTest,Next)35 TEST(ProtoBufferReaderTest, Next) {
36   Slice slices[] = {
37       Slice(std::string(128, 'a')),
38       Slice(std::string(256, 'b')),
39   };
40   ByteBuffer buffer(slices, 2);
41   ProtoBufferReader reader(&buffer);
42   // read all data from buffer
43   std::vector<Slice> read_slices;
44   int read_size = 0;
45   while (read_size < static_cast<int>(buffer.Length())) {
46     const void* data;
47     int size = 0;
48     EXPECT_TRUE(reader.Next(&data, &size));
49     read_slices.emplace_back(data, size);
50     read_size += size;
51   }
52   EXPECT_EQ(reader.ByteCount(), read_size);
53   // check if read data is equal to original data
54   ByteBuffer read_buffer(&*read_slices.begin(), read_slices.size());
55   ExpectBufferEqual(read_buffer, buffer);
56 }
57 
58 #ifdef GRPC_PROTOBUF_CORD_SUPPORT_ENABLED
59 
TEST(ProtoBufferReaderTest,ReadCord)60 TEST(ProtoBufferReaderTest, ReadCord) {
61   std::string str1 = std::string(128, 'a');
62   std::string str2 = std::string(256, 'b');
63   Slice slices[] = {Slice(str1), Slice(str2)};
64   ByteBuffer buffer(slices, 2);
65   ProtoBufferReader reader(&buffer);
66   // read cords from buffer
67   absl::Cord cord1;
68   reader.ReadCord(&cord1, str1.size());
69   EXPECT_EQ(cord1.size(), str1.size());
70   EXPECT_EQ(std::string(cord1), str1);
71   absl::Cord cord2;
72   reader.ReadCord(&cord2, str2.size());
73   EXPECT_EQ(cord2.size(), str2.size());
74   EXPECT_EQ(std::string(cord2), str2);
75   EXPECT_EQ(reader.ByteCount(), cord1.size() + cord2.size());
76 }
77 
78 #endif  // GRPC_PROTOBUF_CORD_SUPPORT_ENABLED
79 
80 }  // namespace
81 }  // namespace grpc
82 
main(int argc,char ** argv)83 int main(int argc, char** argv) {
84   grpc::testing::TestEnvironment env(&argc, argv);
85   ::testing::InitGoogleTest(&argc, argv);
86   return RUN_ALL_TESTS();
87 }
88