• 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 <stdlib.h>
22 #include <string.h>
23 #include <unistd.h>
24 
25 #include <grpc/support/alloc.h>
26 #include <grpc/support/log.h>
27 #include <grpc/support/string_util.h>
28 #include <grpc/support/sync.h>
29 
30 #include "src/core/ext/filters/client_channel/client_channel.h"
31 #include "src/core/ext/filters/http/server/http_server_filter.h"
32 #include "src/core/ext/transport/chttp2/transport/chttp2_transport.h"
33 #include "src/core/lib/channel/connected_channel.h"
34 #include "src/core/lib/gpr/host_port.h"
35 #include "src/core/lib/gpr/string.h"
36 #include "src/core/lib/security/credentials/credentials.h"
37 #include "src/core/lib/surface/channel.h"
38 #include "src/core/lib/surface/server.h"
39 #include "test/core/util/port.h"
40 #include "test/core/util/test_config.h"
41 
42 typedef struct fullstack_fixture_data {
43   char* localaddr;
44 } fullstack_fixture_data;
45 
46 static int unique = 1;
47 
chttp2_create_fixture_fullstack(grpc_channel_args * client_args,grpc_channel_args * server_args)48 static grpc_end2end_test_fixture chttp2_create_fixture_fullstack(
49     grpc_channel_args* client_args, grpc_channel_args* server_args) {
50   grpc_end2end_test_fixture f;
51   fullstack_fixture_data* ffd = static_cast<fullstack_fixture_data*>(
52       gpr_malloc(sizeof(fullstack_fixture_data)));
53   memset(&f, 0, sizeof(f));
54 
55   gpr_asprintf(&ffd->localaddr, "unix:/tmp/grpc_fullstack_test.%d.%d", getpid(),
56                unique++);
57 
58   f.fixture_data = ffd;
59   f.cq = grpc_completion_queue_create_for_next(nullptr);
60   f.shutdown_cq = grpc_completion_queue_create_for_pluck(nullptr);
61 
62   return f;
63 }
64 
chttp2_init_client_fullstack(grpc_end2end_test_fixture * f,grpc_channel_args * client_args)65 void chttp2_init_client_fullstack(grpc_end2end_test_fixture* f,
66                                   grpc_channel_args* client_args) {
67   grpc_channel_credentials* creds = grpc_local_credentials_create(UDS);
68   fullstack_fixture_data* ffd =
69       static_cast<fullstack_fixture_data*>(f->fixture_data);
70   f->client =
71       grpc_secure_channel_create(creds, ffd->localaddr, client_args, nullptr);
72   GPR_ASSERT(f->client != nullptr);
73   grpc_channel_credentials_release(creds);
74 }
75 
76 /*
77  * Check if server should fail auth check. If it is true, a different metadata
78  * processor will be installed that always fails in processing client's
79  * metadata.
80  */
fail_server_auth_check(grpc_channel_args * server_args)81 static bool fail_server_auth_check(grpc_channel_args* server_args) {
82   size_t i;
83   if (server_args == nullptr) return 0;
84   for (i = 0; i < server_args->num_args; i++) {
85     if (strcmp(server_args->args[i].key, FAIL_AUTH_CHECK_SERVER_ARG_NAME) ==
86         0) {
87       return true;
88     }
89   }
90   return false;
91 }
92 
process_auth_failure(void * state,grpc_auth_context * ctx,const grpc_metadata * md,size_t md_count,grpc_process_auth_metadata_done_cb cb,void * user_data)93 static void process_auth_failure(void* state, grpc_auth_context* ctx,
94                                  const grpc_metadata* md, size_t md_count,
95                                  grpc_process_auth_metadata_done_cb cb,
96                                  void* user_data) {
97   GPR_ASSERT(state == nullptr);
98   cb(user_data, nullptr, 0, nullptr, 0, GRPC_STATUS_UNAUTHENTICATED, nullptr);
99 }
100 
chttp2_init_server_fullstack(grpc_end2end_test_fixture * f,grpc_channel_args * server_args)101 void chttp2_init_server_fullstack(grpc_end2end_test_fixture* f,
102                                   grpc_channel_args* server_args) {
103   grpc_server_credentials* creds = grpc_local_server_credentials_create(UDS);
104   fullstack_fixture_data* ffd =
105       static_cast<fullstack_fixture_data*>(f->fixture_data);
106   if (f->server) {
107     grpc_server_destroy(f->server);
108   }
109   f->server = grpc_server_create(server_args, nullptr);
110   grpc_server_register_completion_queue(f->server, f->cq, nullptr);
111   if (fail_server_auth_check(server_args)) {
112     grpc_auth_metadata_processor processor = {process_auth_failure, nullptr,
113                                               nullptr};
114     grpc_server_credentials_set_auth_metadata_processor(creds, processor);
115   }
116   GPR_ASSERT(
117       grpc_server_add_secure_http2_port(f->server, ffd->localaddr, creds));
118   grpc_server_credentials_release(creds);
119   grpc_server_start(f->server);
120 }
121 
chttp2_tear_down_fullstack(grpc_end2end_test_fixture * f)122 void chttp2_tear_down_fullstack(grpc_end2end_test_fixture* f) {
123   fullstack_fixture_data* ffd =
124       static_cast<fullstack_fixture_data*>(f->fixture_data);
125   gpr_free(ffd->localaddr);
126   gpr_free(ffd);
127 }
128 
129 /* All test configurations */
130 static grpc_end2end_test_config configs[] = {
131     {"chttp2/fullstack_local",
132      FEATURE_MASK_SUPPORTS_DELAYED_CONNECTION |
133          FEATURE_MASK_SUPPORTS_CLIENT_CHANNEL |
134          FEATURE_MASK_SUPPORTS_AUTHORITY_HEADER |
135          FEATURE_MASK_SUPPORTS_PER_CALL_CREDENTIALS,
136      nullptr, chttp2_create_fixture_fullstack, chttp2_init_client_fullstack,
137      chttp2_init_server_fullstack, chttp2_tear_down_fullstack},
138 };
139 
main(int argc,char ** argv)140 int main(int argc, char** argv) {
141   size_t i;
142 
143   grpc_test_init(argc, argv);
144   grpc_end2end_tests_pre_init();
145   grpc_init();
146 
147   for (i = 0; i < sizeof(configs) / sizeof(*configs); i++) {
148     grpc_end2end_tests(argc, argv, configs[i]);
149   }
150 
151   grpc_shutdown();
152 
153   return 0;
154 }
155