xref: /aosp_15_r20/external/grpc-grpc/test/core/handshake/readahead_handshaker_server_ssl.cc (revision cc02d7e222339f7a4f6ba5f422e6413f4bd931f2)
1 //
2 //
3 // Copyright 2016 gRPC authors.
4 //
5 // Licensed under the Apache License, Version 2.0 (the "License");
6 // you may not use this file except in compliance with the License.
7 // You may obtain a copy of the License at
8 //
9 //     http://www.apache.org/licenses/LICENSE-2.0
10 //
11 // Unless required by applicable law or agreed to in writing, software
12 // distributed under the License is distributed on an "AS IS" BASIS,
13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 // See the License for the specific language governing permissions and
15 // limitations under the License.
16 //
17 //
18 
19 #include <memory>
20 
21 #include "gtest/gtest.h"
22 
23 #include <grpc/grpc.h>
24 
25 #include "src/core/lib/channel/channel_args.h"
26 #include "src/core/lib/config/core_configuration.h"
27 #include "src/core/lib/gprpp/ref_counted_ptr.h"
28 #include "src/core/lib/iomgr/closure.h"
29 #include "src/core/lib/iomgr/endpoint.h"
30 #include "src/core/lib/iomgr/error.h"
31 #include "src/core/lib/iomgr/iomgr_fwd.h"
32 #include "src/core/lib/iomgr/tcp_server.h"
33 #include "src/core/lib/transport/handshaker.h"
34 #include "src/core/lib/transport/handshaker_factory.h"
35 #include "src/core/lib/transport/handshaker_registry.h"
36 #include "test/core/handshake/server_ssl_common.h"
37 #include "test/core/util/test_config.h"
38 
39 // The purpose of this test is to exercise the case when a
40 // grpc *security_handshaker* begins its handshake with data already
41 // in the read buffer of the handshaker arg. This scenario is created by
42 // adding a fake "readahead" handshaker at the beginning of the server's
43 // handshaker list, which just reads from the connection and then places
44 // read bytes into the read buffer of the handshake arg (to be passed down
45 // to the security_handshaker). This test is meant to protect code relying on
46 // this functionality that lives outside of this repo.
47 
48 namespace grpc_core {
49 
50 class ReadAheadHandshaker : public Handshaker {
51  public:
~ReadAheadHandshaker()52   ~ReadAheadHandshaker() override {}
name() const53   const char* name() const override { return "read_ahead"; }
Shutdown(grpc_error_handle)54   void Shutdown(grpc_error_handle /*why*/) override {}
DoHandshake(grpc_tcp_server_acceptor *,grpc_closure * on_handshake_done,HandshakerArgs * args)55   void DoHandshake(grpc_tcp_server_acceptor* /*acceptor*/,
56                    grpc_closure* on_handshake_done,
57                    HandshakerArgs* args) override {
58     grpc_endpoint_read(args->endpoint, args->read_buffer, on_handshake_done,
59                        /*urgent=*/false, /*min_progress_size=*/1);
60   }
61 };
62 
63 class ReadAheadHandshakerFactory : public HandshakerFactory {
64  public:
AddHandshakers(const ChannelArgs &,grpc_pollset_set *,HandshakeManager * handshake_mgr)65   void AddHandshakers(const ChannelArgs& /*args*/,
66                       grpc_pollset_set* /*interested_parties*/,
67                       HandshakeManager* handshake_mgr) override {
68     handshake_mgr->Add(MakeRefCounted<ReadAheadHandshaker>());
69   }
Priority()70   HandshakerPriority Priority() override {
71     return HandshakerPriority::kReadAheadSecurityHandshakers;
72   }
73   ~ReadAheadHandshakerFactory() override = default;
74 };
75 
76 }  // namespace grpc_core
77 
TEST(HandshakeServerWithReadaheadHandshakerTest,MainTest)78 TEST(HandshakeServerWithReadaheadHandshakerTest, MainTest) {
79   grpc_core::CoreConfiguration::WithSubstituteBuilder builder(
80       [](grpc_core::CoreConfiguration::Builder* builder) {
81         BuildCoreConfiguration(builder);
82         builder->handshaker_registry()->RegisterHandshakerFactory(
83             grpc_core::HANDSHAKER_SERVER,
84             std::make_unique<grpc_core::ReadAheadHandshakerFactory>());
85       });
86 
87   grpc_init();
88   const char* full_alpn_list[] = {"h2"};
89   ASSERT_TRUE(server_ssl_test(full_alpn_list, 1, "h2"));
90   CleanupSslLibrary();
91   grpc_shutdown();
92 }
93 
main(int argc,char ** argv)94 int main(int argc, char** argv) {
95   grpc::testing::TestEnvironment env(&argc, argv);
96   ::testing::InitGoogleTest(&argc, argv);
97   return RUN_ALL_TESTS();
98 }
99