readahead_handshaker_server_ssl.cc 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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. #include <arpa/inet.h>
  19. #include <string.h>
  20. #include <sys/socket.h>
  21. #include <unistd.h>
  22. #include <openssl/err.h>
  23. #include <openssl/ssl.h>
  24. #include <grpc/grpc.h>
  25. #include <grpc/grpc_security.h>
  26. #include <grpc/support/alloc.h>
  27. #include <grpc/support/log.h>
  28. #include <grpc/support/string_util.h>
  29. #include <grpc/support/sync.h>
  30. #include "src/core/lib/channel/handshaker_factory.h"
  31. #include "src/core/lib/channel/handshaker_registry.h"
  32. #include "src/core/lib/config/core_configuration.h"
  33. #include "src/core/lib/iomgr/load_file.h"
  34. #include "src/core/lib/security/transport/security_handshaker.h"
  35. #include "test/core/handshake/server_ssl_common.h"
  36. #include "test/core/util/port.h"
  37. #include "test/core/util/test_config.h"
  38. /* The purpose of this test is to exercise the case when a
  39. * grpc *security_handshaker* begins its handshake with data already
  40. * in the read buffer of the handshaker arg. This scenario is created by
  41. * adding a fake "readahead" handshaker at the beginning of the server's
  42. * handshaker list, which just reads from the connection and then places
  43. * read bytes into the read buffer of the handshake arg (to be passed down
  44. * to the security_handshaker). This test is meant to protect code relying on
  45. * this functionality that lives outside of this repo. */
  46. namespace grpc_core {
  47. class ReadAheadHandshaker : public Handshaker {
  48. public:
  49. ~ReadAheadHandshaker() override {}
  50. const char* name() const override { return "read_ahead"; }
  51. void Shutdown(grpc_error_handle /*why*/) override {}
  52. void DoHandshake(grpc_tcp_server_acceptor* /*acceptor*/,
  53. grpc_closure* on_handshake_done,
  54. HandshakerArgs* args) override {
  55. grpc_endpoint_read(args->endpoint, args->read_buffer, on_handshake_done,
  56. /*urgent=*/false);
  57. }
  58. };
  59. class ReadAheadHandshakerFactory : public HandshakerFactory {
  60. public:
  61. void AddHandshakers(const grpc_channel_args* /*args*/,
  62. grpc_pollset_set* /*interested_parties*/,
  63. HandshakeManager* handshake_mgr) override {
  64. handshake_mgr->Add(MakeRefCounted<ReadAheadHandshaker>());
  65. }
  66. ~ReadAheadHandshakerFactory() override = default;
  67. };
  68. } // namespace grpc_core
  69. int main(int /*argc*/, char* /*argv*/[]) {
  70. grpc_core::CoreConfiguration::BuildSpecialConfiguration(
  71. [](grpc_core::CoreConfiguration::Builder* builder) {
  72. BuildCoreConfiguration(builder);
  73. builder->handshaker_registry()->RegisterHandshakerFactory(
  74. true /* at_start */, grpc_core::HANDSHAKER_SERVER,
  75. absl::make_unique<grpc_core::ReadAheadHandshakerFactory>());
  76. });
  77. grpc_init();
  78. const char* full_alpn_list[] = {"grpc-exp", "h2"};
  79. GPR_ASSERT(server_ssl_test(full_alpn_list, 2, "grpc-exp"));
  80. CleanupSslLibrary();
  81. grpc_shutdown();
  82. return 0;
  83. }