Debian Codegraph @

test/core/end2end/fixtures/local_util.cc

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