readahead_handshaker_server_ssl.cc
Go to the documentation of this file.
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 <arpa/inet.h>
20 #include <string.h>
21 #include <sys/socket.h>
22 #include <unistd.h>
23 
24 #include <openssl/err.h>
25 #include <openssl/ssl.h>
26 
27 #include <grpc/grpc.h>
28 #include <grpc/grpc_security.h>
29 #include <grpc/support/alloc.h>
30 #include <grpc/support/log.h>
32 #include <grpc/support/sync.h>
33 
40 #include "test/core/util/port.h"
42 
43 /* The purpose of this test is to exercise the case when a
44  * grpc *security_handshaker* begins its handshake with data already
45  * in the read buffer of the handshaker arg. This scenario is created by
46  * adding a fake "readahead" handshaker at the beginning of the server's
47  * handshaker list, which just reads from the connection and then places
48  * read bytes into the read buffer of the handshake arg (to be passed down
49  * to the security_handshaker). This test is meant to protect code relying on
50  * this functionality that lives outside of this repo. */
51 
52 namespace grpc_core {
53 
55  public:
56  ~ReadAheadHandshaker() override {}
57  const char* name() const override { return "read_ahead"; }
58  void Shutdown(grpc_error_handle /*why*/) override {}
61  HandshakerArgs* args) override {
62  grpc_endpoint_read(args->endpoint, args->read_buffer, on_handshake_done,
63  /*urgent=*/false, /*min_progress_size=*/1);
64  }
65 };
66 
68  public:
69  void AddHandshakers(const grpc_channel_args* /*args*/,
70  grpc_pollset_set* /*interested_parties*/,
71  HandshakeManager* handshake_mgr) override {
72  handshake_mgr->Add(MakeRefCounted<ReadAheadHandshaker>());
73  }
74  ~ReadAheadHandshakerFactory() override = default;
75 };
76 
77 } // namespace grpc_core
78 
79 int main(int /*argc*/, char* /*argv*/[]) {
83  builder->handshaker_registry()->RegisterHandshakerFactory(
84  true /* at_start */, grpc_core::HANDSHAKER_SERVER,
85  absl::make_unique<grpc_core::ReadAheadHandshakerFactory>());
86  });
87 
88  grpc_init();
89  const char* full_alpn_list[] = {"grpc-exp", "h2"};
90  GPR_ASSERT(server_ssl_test(full_alpn_list, 2, "grpc-exp"));
92  grpc_shutdown();
93  return 0;
94 }
grpc_core::ReadAheadHandshaker::~ReadAheadHandshaker
~ReadAheadHandshaker() override
Definition: readahead_handshaker_server_ssl.cc:56
log.h
port.h
core_configuration.h
load_file.h
grpc_core
Definition: call_metric_recorder.h:31
grpc_core::CoreConfiguration::Builder
Definition: core_configuration.h:41
grpc_pollset_set
struct grpc_pollset_set grpc_pollset_set
Definition: iomgr_fwd.h:23
string.h
grpc_core::ReadAheadHandshakerFactory::~ReadAheadHandshakerFactory
~ReadAheadHandshakerFactory() override=default
grpc_endpoint_read
void grpc_endpoint_read(grpc_endpoint *ep, grpc_slice_buffer *slices, grpc_closure *cb, bool urgent, int min_progress_size)
Definition: endpoint.cc:25
grpc_core::ReadAheadHandshaker::Shutdown
void Shutdown(grpc_error_handle) override
Definition: readahead_handshaker_server_ssl.cc:58
grpc_security.h
grpc_core::ReadAheadHandshakerFactory
Definition: readahead_handshaker_server_ssl.cc:67
grpc_channel_args
Definition: grpc_types.h:132
grpc_core::Handshaker
Definition: handshaker.h:84
grpc_core::CoreConfiguration::BuildSpecialConfiguration
static void BuildSpecialConfiguration(BuildFunc build)
Definition: core_configuration.h:100
grpc_core::HANDSHAKER_SERVER
@ HANDSHAKER_SERVER
Definition: handshaker_registry.h:36
string_util.h
grpc_core::ReadAheadHandshakerFactory::AddHandshakers
void AddHandshakers(const grpc_channel_args *, grpc_pollset_set *, HandshakeManager *handshake_mgr) override
Definition: readahead_handshaker_server_ssl.cc:69
profile_analyzer.builder
builder
Definition: profile_analyzer.py:159
asyncio_get_stats.args
args
Definition: asyncio_get_stats.py:40
GPR_ASSERT
#define GPR_ASSERT(x)
Definition: include/grpc/impl/codegen/log.h:94
main
int main(int, char *[])
Definition: readahead_handshaker_server_ssl.cc:79
grpc_core::ReadAheadHandshaker::DoHandshake
void DoHandshake(grpc_tcp_server_acceptor *, grpc_closure *on_handshake_done, HandshakerArgs *args) override
Definition: readahead_handshaker_server_ssl.cc:59
grpc.h
handshaker_registry.h
err.h
grpc_core::HandshakeManager::Add
void Add(RefCountedPtr< Handshaker > handshaker)
Definition: src/core/lib/transport/handshaker.cc:63
grpc_core::ReadAheadHandshaker::name
const char * name() const override
Definition: readahead_handshaker_server_ssl.cc:57
ssl.h
test_config.h
security_handshaker.h
grpc_tcp_server_acceptor
Definition: tcp_server.h:36
handshaker_factory.h
alloc.h
grpc_core::BuildCoreConfiguration
void BuildCoreConfiguration(CoreConfiguration::Builder *builder)
Definition: grpc_plugin_registry.cc:109
server_ssl_common.h
server_ssl_test
bool server_ssl_test(const char *alpn_list[], unsigned int alpn_list_len, const char *alpn_expected)
Definition: server_ssl_common.cc:170
grpc_core::HandshakerArgs
Definition: handshaker.h:64
grpc_core::HandshakeManager
Definition: handshaker.h:98
grpc_core::HandshakerFactory
Definition: handshaker_factory.h:39
grpc_init
GRPCAPI void grpc_init(void)
Definition: init.cc:146
grpc_error
Definition: error_internal.h:42
grpc_core::ReadAheadHandshaker
Definition: readahead_handshaker_server_ssl.cc:54
sync.h
grpc_closure
Definition: closure.h:56
grpc_shutdown
GRPCAPI void grpc_shutdown(void)
Definition: init.cc:209
CleanupSslLibrary
void CleanupSslLibrary()
Definition: server_ssl_common.cc:283
on_handshake_done
static void on_handshake_done(void *arg, grpc_error_handle error)
Definition: ssl_server_fuzzer.cc:45


grpc
Author(s):
autogenerated on Fri May 16 2025 03:00:00