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