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