local_util.cc 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. /*
  2. *
  3. * Copyright 2018 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 "test/core/end2end/fixtures/local_util.h"
  19. #include <grpc/support/alloc.h>
  20. #include <grpc/support/log.h>
  21. #include <grpc/support/string_util.h>
  22. #include <grpc/support/sync.h>
  23. #include "src/core/ext/filters/client_channel/client_channel.h"
  24. #include "src/core/ext/filters/http/server/http_server_filter.h"
  25. #include "src/core/ext/transport/chttp2/transport/chttp2_transport.h"
  26. #include "src/core/lib/channel/connected_channel.h"
  27. #include "src/core/lib/gpr/string.h"
  28. #include "src/core/lib/security/credentials/credentials.h"
  29. #include "src/core/lib/surface/server.h"
  30. #include "test/core/util/port.h"
  31. #include "test/core/util/test_config.h"
  32. grpc_end2end_test_fixture grpc_end2end_local_chttp2_create_fixture_fullstack() {
  33. grpc_end2end_test_fixture f;
  34. grpc_end2end_local_fullstack_fixture_data* ffd =
  35. new grpc_end2end_local_fullstack_fixture_data();
  36. memset(&f, 0, sizeof(f));
  37. f.fixture_data = ffd;
  38. f.cq = grpc_completion_queue_create_for_next(nullptr);
  39. f.shutdown_cq = grpc_completion_queue_create_for_pluck(nullptr);
  40. return f;
  41. }
  42. void grpc_end2end_local_chttp2_init_client_fullstack(
  43. grpc_end2end_test_fixture* f, const grpc_channel_args* client_args,
  44. grpc_local_connect_type type) {
  45. grpc_channel_credentials* creds = grpc_local_credentials_create(type);
  46. grpc_end2end_local_fullstack_fixture_data* ffd =
  47. static_cast<grpc_end2end_local_fullstack_fixture_data*>(f->fixture_data);
  48. f->client = grpc_channel_create(ffd->localaddr.c_str(), creds, client_args);
  49. GPR_ASSERT(f->client != nullptr);
  50. grpc_channel_credentials_release(creds);
  51. }
  52. /*
  53. * Check if server should fail auth check. If it is true, a different metadata
  54. * processor will be installed that always fails in processing client's
  55. * metadata.
  56. */
  57. static bool fail_server_auth_check(const grpc_channel_args* server_args) {
  58. size_t i;
  59. if (server_args == nullptr) return false;
  60. for (i = 0; i < server_args->num_args; i++) {
  61. if (strcmp(server_args->args[i].key, FAIL_AUTH_CHECK_SERVER_ARG_NAME) ==
  62. 0) {
  63. return true;
  64. }
  65. }
  66. return false;
  67. }
  68. static void process_auth_failure(void* state, grpc_auth_context* /*ctx*/,
  69. const grpc_metadata* /*md*/,
  70. size_t /*md_count*/,
  71. grpc_process_auth_metadata_done_cb cb,
  72. void* user_data) {
  73. GPR_ASSERT(state == nullptr);
  74. cb(user_data, nullptr, 0, nullptr, 0, GRPC_STATUS_UNAUTHENTICATED, nullptr);
  75. }
  76. void grpc_end2end_local_chttp2_init_server_fullstack(
  77. grpc_end2end_test_fixture* f, const grpc_channel_args* server_args,
  78. grpc_local_connect_type type) {
  79. grpc_server_credentials* creds = grpc_local_server_credentials_create(type);
  80. grpc_end2end_local_fullstack_fixture_data* ffd =
  81. static_cast<grpc_end2end_local_fullstack_fixture_data*>(f->fixture_data);
  82. if (f->server) {
  83. grpc_server_destroy(f->server);
  84. }
  85. f->server = grpc_server_create(server_args, nullptr);
  86. grpc_server_register_completion_queue(f->server, f->cq, nullptr);
  87. if (fail_server_auth_check(server_args)) {
  88. grpc_auth_metadata_processor processor = {process_auth_failure, nullptr,
  89. nullptr};
  90. grpc_server_credentials_set_auth_metadata_processor(creds, processor);
  91. }
  92. GPR_ASSERT(
  93. grpc_server_add_http2_port(f->server, ffd->localaddr.c_str(), creds));
  94. grpc_server_credentials_release(creds);
  95. grpc_server_start(f->server);
  96. }
  97. void grpc_end2end_local_chttp2_tear_down_fullstack(
  98. grpc_end2end_test_fixture* f) {
  99. grpc_end2end_local_fullstack_fixture_data* ffd =
  100. static_cast<grpc_end2end_local_fullstack_fixture_data*>(f->fixture_data);
  101. delete ffd;
  102. }