h2_insecure.cc 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. //
  2. //
  3. // Copyright 2020 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 <string.h>
  19. #include <grpc/grpc_security.h>
  20. #include <grpc/support/alloc.h>
  21. #include <grpc/support/log.h>
  22. #include "src/core/lib/channel/channel_args.h"
  23. #include "src/core/lib/gprpp/host_port.h"
  24. #include "test/core/end2end/end2end_tests.h"
  25. #include "test/core/util/port.h"
  26. #include "test/core/util/test_config.h"
  27. namespace {
  28. struct Chttp2InsecureFullstackFixtureData {
  29. std::string localaddr;
  30. };
  31. grpc_end2end_test_fixture Chttp2CreateFixtureInsecureFullstack(
  32. const grpc_channel_args* /*client_args*/,
  33. const grpc_channel_args* /*server_args*/) {
  34. grpc_end2end_test_fixture f;
  35. int port = grpc_pick_unused_port_or_die();
  36. Chttp2InsecureFullstackFixtureData* ffd =
  37. new Chttp2InsecureFullstackFixtureData();
  38. memset(&f, 0, sizeof(f));
  39. ffd->localaddr = grpc_core::JoinHostPort("localhost", port);
  40. f.fixture_data = ffd;
  41. f.cq = grpc_completion_queue_create_for_next(nullptr);
  42. f.shutdown_cq = grpc_completion_queue_create_for_pluck(nullptr);
  43. return f;
  44. }
  45. void Chttp2InitClientInsecureFullstack(grpc_end2end_test_fixture* f,
  46. const grpc_channel_args* client_args) {
  47. Chttp2InsecureFullstackFixtureData* ffd =
  48. static_cast<Chttp2InsecureFullstackFixtureData*>(f->fixture_data);
  49. grpc_channel_credentials* creds = grpc_insecure_credentials_create();
  50. f->client = grpc_channel_create(ffd->localaddr.c_str(), creds, client_args);
  51. grpc_channel_credentials_release(creds);
  52. GPR_ASSERT(f->client);
  53. }
  54. void ProcessAuthFailure(void* state, grpc_auth_context* /*ctx*/,
  55. const grpc_metadata* /*md*/, size_t /*md_count*/,
  56. grpc_process_auth_metadata_done_cb cb,
  57. void* user_data) {
  58. GPR_ASSERT(state == nullptr);
  59. cb(user_data, nullptr, 0, nullptr, 0, GRPC_STATUS_UNAUTHENTICATED, nullptr);
  60. }
  61. void Chttp2InitServerInsecureFullstack(grpc_end2end_test_fixture* f,
  62. const grpc_channel_args* server_args) {
  63. Chttp2InsecureFullstackFixtureData* ffd =
  64. static_cast<Chttp2InsecureFullstackFixtureData*>(f->fixture_data);
  65. if (f->server) {
  66. grpc_server_destroy(f->server);
  67. }
  68. f->server = grpc_server_create(server_args, nullptr);
  69. grpc_server_register_completion_queue(f->server, f->cq, nullptr);
  70. grpc_server_credentials* server_creds =
  71. grpc_insecure_server_credentials_create();
  72. if (grpc_channel_args_find(server_args, FAIL_AUTH_CHECK_SERVER_ARG_NAME) !=
  73. nullptr) {
  74. grpc_auth_metadata_processor processor = {ProcessAuthFailure, nullptr,
  75. nullptr};
  76. grpc_server_credentials_set_auth_metadata_processor(server_creds,
  77. processor);
  78. }
  79. GPR_ASSERT(grpc_server_add_http2_port(f->server, ffd->localaddr.c_str(),
  80. server_creds));
  81. grpc_server_credentials_release(server_creds);
  82. grpc_server_start(f->server);
  83. }
  84. void Chttp2TearDownInsecureFullstack(grpc_end2end_test_fixture* f) {
  85. Chttp2InsecureFullstackFixtureData* ffd =
  86. static_cast<Chttp2InsecureFullstackFixtureData*>(f->fixture_data);
  87. delete ffd;
  88. }
  89. /* All test configurations */
  90. grpc_end2end_test_config configs[] = {
  91. {"chttp2/insecure_fullstack",
  92. FEATURE_MASK_SUPPORTS_DELAYED_CONNECTION |
  93. FEATURE_MASK_SUPPORTS_CLIENT_CHANNEL |
  94. FEATURE_MASK_SUPPORTS_AUTHORITY_HEADER |
  95. FEATURE_MASK_SUPPORTS_PER_CALL_CREDENTIALS_LEVEL_INSECURE,
  96. nullptr, Chttp2CreateFixtureInsecureFullstack,
  97. Chttp2InitClientInsecureFullstack, Chttp2InitServerInsecureFullstack,
  98. Chttp2TearDownInsecureFullstack},
  99. };
  100. } // namespace
  101. int main(int argc, char** argv) {
  102. size_t i;
  103. grpc::testing::TestEnvironment env(argc, argv);
  104. grpc_end2end_tests_pre_init();
  105. grpc_init();
  106. for (i = 0; i < sizeof(configs) / sizeof(*configs); i++) {
  107. grpc_end2end_tests(argc, argv, configs[i]);
  108. }
  109. grpc_shutdown();
  110. return 0;
  111. }