xref: /aosp_15_r20/external/grpc-grpc/test/core/transport/chaotic_good/frame_header_test.cc (revision cc02d7e222339f7a4f6ba5f422e6413f4bd931f2)
1 // Copyright 2022 gRPC authors.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 //     http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #include "src/core/ext/transport/chaotic_good/frame_header.h"
16 
17 #include <cstdint>
18 #include <vector>
19 
20 #include "absl/status/status.h"
21 #include "gtest/gtest.h"
22 
23 namespace grpc_core {
24 namespace chaotic_good {
25 namespace {
26 
Serialize(FrameHeader h)27 std::vector<uint8_t> Serialize(FrameHeader h) {
28   uint8_t buffer[24];
29   h.Serialize(buffer);
30   return std::vector<uint8_t>(buffer, buffer + 24);
31 }
32 
Deserialize(std::vector<uint8_t> data)33 absl::StatusOr<FrameHeader> Deserialize(std::vector<uint8_t> data) {
34   if (data.size() != 24) return absl::InvalidArgumentError("bad length");
35   return FrameHeader::Parse(data.data());
36 }
37 
TEST(FrameHeaderTest,SimpleSerialize)38 TEST(FrameHeaderTest, SimpleSerialize) {
39   EXPECT_EQ(Serialize(FrameHeader{FrameType::kCancel, BitSet<3>::FromInt(0),
40                                   0x01020304, 0x05060708, 0x090a0b0c,
41                                   0x00000034, 0x0d0e0f10}),
42             std::vector<uint8_t>({
43                 0x81, 0,    0,    0,     // type, flags
44                 0x04, 0x03, 0x02, 0x01,  // stream_id
45                 0x08, 0x07, 0x06, 0x05,  // header_length
46                 0x0c, 0x0b, 0x0a, 0x09,  // message_length
47                 0x34, 0x00, 0x00, 0x00,  // mesage_padding
48                 0x10, 0x0f, 0x0e, 0x0d   // trailer_length
49             }));
50 }
51 
TEST(FrameHeaderTest,SimpleDeserialize)52 TEST(FrameHeaderTest, SimpleDeserialize) {
53   EXPECT_EQ(Deserialize(std::vector<uint8_t>({
54                 0x81, 0,    0,    0,     // type, flags
55                 0x04, 0x03, 0x02, 0x01,  // stream_id
56                 0x08, 0x07, 0x06, 0x05,  // header_length
57                 0x0c, 0x0b, 0x0a, 0x09,  // message_length
58                 0x34, 0x00, 0x00, 0x00,  // mesage_padding
59                 0x10, 0x0f, 0x0e, 0x0d   // trailer_length
60             })),
61             absl::StatusOr<FrameHeader>(FrameHeader{
62                 FrameType::kCancel, BitSet<3>::FromInt(0), 0x01020304,
63                 0x05060708, 0x090a0b0c, 0x00000034, 0x0d0e0f10}));
64   EXPECT_EQ(Deserialize(std::vector<uint8_t>({
65                             0x81, 88,   88,   88,    // type, flags
66                             0x04, 0x03, 0x02, 0x01,  // stream_id
67                             0x08, 0x07, 0x06, 0x05,  // header_length
68                             0x0c, 0x0b, 0x0a, 0x09,  // message_length
69                             0x34, 0x00, 0x00, 0x00,  // mesage_padding
70                             0x10, 0x0f, 0x0e, 0x0d   // trailer_length
71                         }))
72                 .status(),
73             absl::InvalidArgumentError("Invalid flags"));
74 }
75 
TEST(FrameHeaderTest,GetFrameLength)76 TEST(FrameHeaderTest, GetFrameLength) {
77   EXPECT_EQ(
78       (FrameHeader{FrameType::kFragment, BitSet<3>::FromInt(5), 1, 0, 0, 0, 0})
79           .GetFrameLength(),
80       0);
81   EXPECT_EQ(
82       (FrameHeader{FrameType::kFragment, BitSet<3>::FromInt(5), 1, 14, 0, 0, 0})
83           .GetFrameLength(),
84       14);
85   EXPECT_EQ((FrameHeader{FrameType::kFragment, BitSet<3>::FromInt(5), 1, 0, 14,
86                          50, 0})
87                 .GetFrameLength(),
88             0);
89   EXPECT_EQ(
90       (FrameHeader{FrameType::kFragment, BitSet<3>::FromInt(5), 1, 0, 0, 0, 14})
91           .GetFrameLength(),
92       14);
93 }
94 
95 }  // namespace
96 }  // namespace chaotic_good
97 }  // namespace grpc_core
98 
main(int argc,char ** argv)99 int main(int argc, char** argv) {
100   ::testing::InitGoogleTest(&argc, argv);
101   return RUN_ALL_TESTS();
102 }
103