• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2023 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 // Test to verify Fuzztest integration
16 
17 #include <stddef.h>
18 #include <stdint.h>
19 
20 #include <memory>
21 #include <optional>
22 #include <vector>
23 
24 #include "absl/random/random.h"
25 #include "fuzztest/fuzztest.h"
26 #include "gtest/gtest.h"
27 
28 #include "src/core/ext/transport/chttp2/transport/hpack_encoder.h"
29 #include "src/core/ext/transport/chttp2/transport/hpack_parser.h"
30 #include "src/core/lib/gprpp/time.h"
31 #include "src/core/lib/slice/slice_buffer.h"
32 #include "src/core/lib/transport/metadata_batch.h"
33 
34 namespace grpc_core {
35 
EncodeTimeouts(std::vector<uint32_t> timeouts)36 void EncodeTimeouts(std::vector<uint32_t> timeouts) {
37   absl::BitGen bitgen;
38   ScopedTimeCache time_cache;
39   time_cache.TestOnlySetNow(Timestamp::ProcessEpoch());
40   hpack_encoder_detail::TimeoutCompressorImpl timeout_compressor;
41   HPackCompressor compressor;
42   HPackParser parser;
43   for (size_t i = 0; i < timeouts.size(); i++) {
44     SliceBuffer encoded;
45     hpack_encoder_detail::Encoder encoder(&compressor, false, encoded);
46     timeout_compressor.EncodeWith(
47         "grpc-timeout",
48         Timestamp::ProcessEpoch() + Duration::Milliseconds(timeouts[i]),
49         &encoder);
50     grpc_metadata_batch b;
51     const uint32_t kMetadataSizeLimit = 3u * 1024 * 1024 * 1024;
52     parser.BeginFrame(
53         &b, kMetadataSizeLimit, kMetadataSizeLimit, HPackParser::Boundary::None,
54         HPackParser::Priority::None,
55         HPackParser::LogInfo{1, HPackParser::LogInfo::kHeaders, false});
56     for (size_t j = 0; j < encoded.Count(); j++) {
57       EXPECT_TRUE(parser
58                       .Parse(encoded.c_slice_at(j), j == encoded.Count() - 1,
59                              bitgen, nullptr)
60                       .ok());
61     }
62     auto parsed = b.get(GrpcTimeoutMetadata());
63     ASSERT_TRUE(parsed.has_value());
64     EXPECT_GE(*parsed,
65               Timestamp::ProcessEpoch() + Duration::Milliseconds(timeouts[i]));
66     EXPECT_LE(*parsed, Timestamp::ProcessEpoch() +
67                            Duration::Milliseconds(timeouts[i]) * 1.05 +
68                            Duration::Milliseconds(1));
69   }
70 }
71 FUZZ_TEST(MyTestSuite, EncodeTimeouts);
72 
73 }  // namespace grpc_core
74