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