h2_fakesec.cc 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. /*
  2. *
  3. * Copyright 2015 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 <stdio.h>
  19. #include <string.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 "src/core/lib/security/credentials/fake/fake_credentials.h"
  25. #include "test/core/end2end/end2end_tests.h"
  26. #include "test/core/util/port.h"
  27. #include "test/core/util/test_config.h"
  28. struct fullstack_secure_fixture_data {
  29. std::string localaddr;
  30. };
  31. static grpc_end2end_test_fixture chttp2_create_fixture_secure_fullstack(
  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. fullstack_secure_fixture_data* ffd = new fullstack_secure_fixture_data();
  37. memset(&f, 0, sizeof(f));
  38. ffd->localaddr = grpc_core::JoinHostPort("localhost", port);
  39. f.fixture_data = ffd;
  40. f.cq = grpc_completion_queue_create_for_next(nullptr);
  41. f.shutdown_cq = grpc_completion_queue_create_for_pluck(nullptr);
  42. return f;
  43. }
  44. static void process_auth_failure(void* state, grpc_auth_context* /*ctx*/,
  45. const grpc_metadata* /*md*/,
  46. size_t /*md_count*/,
  47. grpc_process_auth_metadata_done_cb cb,
  48. void* user_data) {
  49. GPR_ASSERT(state == nullptr);
  50. cb(user_data, nullptr, 0, nullptr, 0, GRPC_STATUS_UNAUTHENTICATED, nullptr);
  51. }
  52. static void chttp2_init_client_secure_fullstack(
  53. grpc_end2end_test_fixture* f, const grpc_channel_args* client_args,
  54. grpc_channel_credentials* creds) {
  55. fullstack_secure_fixture_data* ffd =
  56. static_cast<fullstack_secure_fixture_data*>(f->fixture_data);
  57. f->client = grpc_channel_create(ffd->localaddr.c_str(), creds, client_args);
  58. GPR_ASSERT(f->client != nullptr);
  59. grpc_channel_credentials_release(creds);
  60. }
  61. static void chttp2_init_server_secure_fullstack(
  62. grpc_end2end_test_fixture* f, const grpc_channel_args* server_args,
  63. grpc_server_credentials* server_creds) {
  64. fullstack_secure_fixture_data* ffd =
  65. static_cast<fullstack_secure_fixture_data*>(f->fixture_data);
  66. if (f->server) {
  67. grpc_server_destroy(f->server);
  68. }
  69. f->server = grpc_server_create(server_args, nullptr);
  70. grpc_server_register_completion_queue(f->server, f->cq, nullptr);
  71. GPR_ASSERT(grpc_server_add_http2_port(f->server, ffd->localaddr.c_str(),
  72. server_creds));
  73. grpc_server_credentials_release(server_creds);
  74. grpc_server_start(f->server);
  75. }
  76. void chttp2_tear_down_secure_fullstack(grpc_end2end_test_fixture* f) {
  77. fullstack_secure_fixture_data* ffd =
  78. static_cast<fullstack_secure_fixture_data*>(f->fixture_data);
  79. delete ffd;
  80. }
  81. static void chttp2_init_client_fake_secure_fullstack(
  82. grpc_end2end_test_fixture* f, const grpc_channel_args* client_args) {
  83. grpc_channel_credentials* fake_ts_creds =
  84. grpc_fake_transport_security_credentials_create();
  85. chttp2_init_client_secure_fullstack(f, client_args, fake_ts_creds);
  86. }
  87. static int fail_server_auth_check(const grpc_channel_args* server_args) {
  88. size_t i;
  89. if (server_args == nullptr) return 0;
  90. for (i = 0; i < server_args->num_args; i++) {
  91. if (strcmp(server_args->args[i].key, FAIL_AUTH_CHECK_SERVER_ARG_NAME) ==
  92. 0) {
  93. return 1;
  94. }
  95. }
  96. return 0;
  97. }
  98. static void chttp2_init_server_fake_secure_fullstack(
  99. grpc_end2end_test_fixture* f, const grpc_channel_args* server_args) {
  100. grpc_server_credentials* fake_ts_creds =
  101. grpc_fake_transport_security_server_credentials_create();
  102. if (fail_server_auth_check(server_args)) {
  103. grpc_auth_metadata_processor processor = {process_auth_failure, nullptr,
  104. nullptr};
  105. grpc_server_credentials_set_auth_metadata_processor(fake_ts_creds,
  106. processor);
  107. }
  108. chttp2_init_server_secure_fullstack(f, server_args, fake_ts_creds);
  109. }
  110. /* All test configurations */
  111. static grpc_end2end_test_config configs[] = {
  112. {"chttp2/fake_secure_fullstack",
  113. FEATURE_MASK_SUPPORTS_DELAYED_CONNECTION |
  114. FEATURE_MASK_SUPPORTS_CLIENT_CHANNEL |
  115. FEATURE_MASK_SUPPORTS_AUTHORITY_HEADER |
  116. FEATURE_MASK_SUPPORTS_PER_CALL_CREDENTIALS_LEVEL_INSECURE,
  117. nullptr, chttp2_create_fixture_secure_fullstack,
  118. chttp2_init_client_fake_secure_fullstack,
  119. chttp2_init_server_fake_secure_fullstack,
  120. chttp2_tear_down_secure_fullstack},
  121. };
  122. int main(int argc, char** argv) {
  123. size_t i;
  124. grpc::testing::TestEnvironment env(argc, argv);
  125. grpc_end2end_tests_pre_init();
  126. grpc_init();
  127. for (i = 0; i < sizeof(configs) / sizeof(*configs); i++) {
  128. grpc_end2end_tests(argc, argv, configs[i]);
  129. }
  130. grpc_shutdown();
  131. return 0;
  132. }