1 /*
2  *
3  * Copyright 2017 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 /* This test verifies -
20  * 1) grpc_call_final_info passed to the filters on destroying a call contains
21  * the proper status.
22  * 2) If the response has both an HTTP status code and a gRPC status code, then
23  * we should prefer the gRPC status code as mentioned in
24  * https://github.com/grpc/grpc/blob/master/doc/http-grpc-status-mapping.md
25  */
26 
27 #include "test/core/end2end/end2end_tests.h"
28 
29 #include <limits.h>
30 #include <stdbool.h>
31 #include <stdio.h>
32 #include <string.h>
33 
34 #include <grpc/byte_buffer.h>
35 #include <grpc/support/alloc.h>
36 #include <grpc/support/log.h>
37 #include <grpc/support/time.h>
38 
39 #include "src/core/lib/channel/channel_stack_builder.h"
40 #include "src/core/lib/surface/call.h"
41 #include "src/core/lib/surface/channel_init.h"
42 #include "test/core/end2end/cq_verifier.h"
43 
44 static bool g_enable_filter = false;
45 static gpr_mu g_mu;
46 static grpc_call_stack* g_client_call_stack;
47 static grpc_call_stack* g_server_call_stack;
48 static bool g_client_code_recv;
49 static bool g_server_code_recv;
50 static gpr_cv g_client_code_cv;
51 static gpr_cv g_server_code_cv;
52 static grpc_status_code g_client_status_code;
53 static grpc_status_code g_server_status_code;
54 
tag(intptr_t t)55 static void* tag(intptr_t t) { return (void*)t; }
56 
begin_test(grpc_end2end_test_config config,const char * test_name,grpc_channel_args * client_args,grpc_channel_args * server_args)57 static grpc_end2end_test_fixture begin_test(grpc_end2end_test_config config,
58                                             const char* test_name,
59                                             grpc_channel_args* client_args,
60                                             grpc_channel_args* server_args) {
61   grpc_end2end_test_fixture f;
62   gpr_log(GPR_INFO, "Running test: %s/%s", test_name, config.name);
63   f = config.create_fixture(client_args, server_args);
64   config.init_server(&f, server_args);
65   config.init_client(&f, client_args);
66   return f;
67 }
68 
n_seconds_from_now(int n)69 static gpr_timespec n_seconds_from_now(int n) {
70   return grpc_timeout_seconds_to_deadline(n);
71 }
72 
five_seconds_from_now(void)73 static gpr_timespec five_seconds_from_now(void) {
74   return n_seconds_from_now(5);
75 }
76 
drain_cq(grpc_completion_queue * cq)77 static void drain_cq(grpc_completion_queue* cq) {
78   grpc_event ev;
79   do {
80     ev = grpc_completion_queue_next(cq, five_seconds_from_now(), nullptr);
81   } while (ev.type != GRPC_QUEUE_SHUTDOWN);
82 }
83 
shutdown_server(grpc_end2end_test_fixture * f)84 static void shutdown_server(grpc_end2end_test_fixture* f) {
85   if (!f->server) return;
86   grpc_server_shutdown_and_notify(f->server, f->shutdown_cq, tag(1000));
87   GPR_ASSERT(grpc_completion_queue_pluck(f->shutdown_cq, tag(1000),
88                                          grpc_timeout_seconds_to_deadline(5),
89                                          nullptr)
90                  .type == GRPC_OP_COMPLETE);
91   grpc_server_destroy(f->server);
92   f->server = nullptr;
93 }
94 
shutdown_client(grpc_end2end_test_fixture * f)95 static void shutdown_client(grpc_end2end_test_fixture* f) {
96   if (!f->client) return;
97   grpc_channel_destroy(f->client);
98   f->client = nullptr;
99 }
100 
end_test(grpc_end2end_test_fixture * f)101 static void end_test(grpc_end2end_test_fixture* f) {
102   shutdown_server(f);
103   shutdown_client(f);
104 
105   grpc_completion_queue_shutdown(f->cq);
106   drain_cq(f->cq);
107   grpc_completion_queue_destroy(f->cq);
108   grpc_completion_queue_destroy(f->shutdown_cq);
109 }
110 
111 // Simple request via a server filter that saves the reported status code.
test_request(grpc_end2end_test_config config)112 static void test_request(grpc_end2end_test_config config) {
113   grpc_call* c;
114   grpc_call* s;
115   grpc_end2end_test_fixture f =
116       begin_test(config, "filter_status_code", nullptr, nullptr);
117   cq_verifier* cqv = cq_verifier_create(f.cq);
118   grpc_op ops[6];
119   grpc_op* op;
120   grpc_metadata_array initial_metadata_recv;
121   grpc_metadata_array trailing_metadata_recv;
122   grpc_metadata_array request_metadata_recv;
123   grpc_call_details call_details;
124   grpc_status_code status;
125   grpc_call_error error;
126   grpc_slice details;
127   int was_cancelled = 2;
128 
129   gpr_mu_lock(&g_mu);
130   g_client_call_stack = nullptr;
131   g_server_call_stack = nullptr;
132   g_client_status_code = GRPC_STATUS_OK;
133   g_server_status_code = GRPC_STATUS_OK;
134   gpr_mu_unlock(&g_mu);
135 
136   gpr_timespec deadline = five_seconds_from_now();
137   c = grpc_channel_create_call(f.client, nullptr, GRPC_PROPAGATE_DEFAULTS, f.cq,
138                                grpc_slice_from_static_string("/foo"), nullptr,
139                                deadline, nullptr);
140   GPR_ASSERT(c);
141   gpr_mu_lock(&g_mu);
142   g_client_call_stack = grpc_call_get_call_stack(c);
143   gpr_mu_unlock(&g_mu);
144 
145   grpc_metadata_array_init(&initial_metadata_recv);
146   grpc_metadata_array_init(&trailing_metadata_recv);
147   grpc_metadata_array_init(&request_metadata_recv);
148   grpc_call_details_init(&call_details);
149 
150   memset(ops, 0, sizeof(ops));
151   op = ops;
152   op->op = GRPC_OP_SEND_INITIAL_METADATA;
153   op->data.send_initial_metadata.count = 0;
154   op->data.send_initial_metadata.metadata = nullptr;
155   op->flags = 0;
156   op->reserved = nullptr;
157   op++;
158   op->op = GRPC_OP_SEND_CLOSE_FROM_CLIENT;
159   op->flags = 0;
160   op->reserved = nullptr;
161   op++;
162   op->op = GRPC_OP_RECV_INITIAL_METADATA;
163   op->data.recv_initial_metadata.recv_initial_metadata = &initial_metadata_recv;
164   op->flags = 0;
165   op->reserved = nullptr;
166   op++;
167   op->op = GRPC_OP_RECV_STATUS_ON_CLIENT;
168   op->data.recv_status_on_client.trailing_metadata = &trailing_metadata_recv;
169   op->data.recv_status_on_client.status = &status;
170   op->data.recv_status_on_client.status_details = &details;
171   op->flags = 0;
172   op->reserved = nullptr;
173   op++;
174   error = grpc_call_start_batch(c, ops, static_cast<size_t>(op - ops), tag(1),
175                                 nullptr);
176   GPR_ASSERT(GRPC_CALL_OK == error);
177 
178   error =
179       grpc_server_request_call(f.server, &s, &call_details,
180                                &request_metadata_recv, f.cq, f.cq, tag(101));
181   GPR_ASSERT(GRPC_CALL_OK == error);
182 
183   CQ_EXPECT_COMPLETION(cqv, tag(101), 1);
184   cq_verify(cqv);
185 
186   gpr_mu_lock(&g_mu);
187   g_server_call_stack = grpc_call_get_call_stack(s);
188   gpr_mu_unlock(&g_mu);
189 
190   memset(ops, 0, sizeof(ops));
191   op = ops;
192   op->op = GRPC_OP_SEND_INITIAL_METADATA;
193   op->data.send_initial_metadata.count = 0;
194   op->flags = 0;
195   op->reserved = nullptr;
196   op++;
197   op->op = GRPC_OP_SEND_STATUS_FROM_SERVER;
198   op->data.send_status_from_server.trailing_metadata_count = 0;
199   op->data.send_status_from_server.status = GRPC_STATUS_UNIMPLEMENTED;
200   grpc_slice status_string = grpc_slice_from_static_string("xyz");
201   op->data.send_status_from_server.status_details = &status_string;
202   op->flags = 0;
203   op->reserved = nullptr;
204   op++;
205   op->op = GRPC_OP_RECV_CLOSE_ON_SERVER;
206   op->data.recv_close_on_server.cancelled = &was_cancelled;
207   op->flags = 0;
208   op->reserved = nullptr;
209   op++;
210   error = grpc_call_start_batch(s, ops, static_cast<size_t>(op - ops), tag(102),
211                                 nullptr);
212   GPR_ASSERT(GRPC_CALL_OK == error);
213 
214   CQ_EXPECT_COMPLETION(cqv, tag(102), 1);
215   CQ_EXPECT_COMPLETION(cqv, tag(1), 1);
216   cq_verify(cqv);
217 
218   GPR_ASSERT(status == GRPC_STATUS_UNIMPLEMENTED);
219   GPR_ASSERT(0 == grpc_slice_str_cmp(details, "xyz"));
220 
221   grpc_slice_unref(details);
222   grpc_metadata_array_destroy(&initial_metadata_recv);
223   grpc_metadata_array_destroy(&trailing_metadata_recv);
224   grpc_metadata_array_destroy(&request_metadata_recv);
225   grpc_call_details_destroy(&call_details);
226 
227   grpc_call_unref(s);
228   grpc_call_unref(c);
229 
230   cq_verifier_destroy(cqv);
231 
232   end_test(&f);
233   config.tear_down_data(&f);
234 
235   // Perform checks after test tear-down
236   // Guards against the case that there's outstanding channel-related work on a
237   // call prior to verification
238   gpr_mu_lock(&g_mu);
239   if (!g_client_code_recv) {
240     GPR_ASSERT(gpr_cv_wait(&g_client_code_cv, &g_mu,
241                            grpc_timeout_seconds_to_deadline(3)) == 0);
242   }
243   if (!g_server_code_recv) {
244     GPR_ASSERT(gpr_cv_wait(&g_server_code_cv, &g_mu,
245                            grpc_timeout_seconds_to_deadline(3)) == 0);
246   }
247   GPR_ASSERT(g_client_status_code == GRPC_STATUS_UNIMPLEMENTED);
248   GPR_ASSERT(g_server_status_code == GRPC_STATUS_UNIMPLEMENTED);
249   gpr_mu_unlock(&g_mu);
250 }
251 
252 /*******************************************************************************
253  * Test status_code filter
254  */
255 
256 typedef struct final_status_data {
257   grpc_call_stack* call;
258 } final_status_data;
259 
server_start_transport_stream_op_batch(grpc_call_element * elem,grpc_transport_stream_op_batch * op)260 static void server_start_transport_stream_op_batch(
261     grpc_call_element* elem, grpc_transport_stream_op_batch* op) {
262   auto* data = static_cast<final_status_data*>(elem->call_data);
263   gpr_mu_lock(&g_mu);
264   if (data->call == g_server_call_stack) {
265     if (op->send_initial_metadata) {
266       auto* batch = op->payload->send_initial_metadata.send_initial_metadata;
267       if (batch->idx.named.status != nullptr) {
268         /* Replace the HTTP status with 404 */
269         grpc_metadata_batch_substitute(batch, batch->idx.named.status,
270                                        GRPC_MDELEM_STATUS_404);
271       }
272     }
273   }
274   gpr_mu_unlock(&g_mu);
275   grpc_call_next_op(elem, op);
276 }
277 
init_call_elem(grpc_call_element * elem,const grpc_call_element_args * args)278 static grpc_error* init_call_elem(grpc_call_element* elem,
279                                   const grpc_call_element_args* args) {
280   final_status_data* data = static_cast<final_status_data*>(elem->call_data);
281   data->call = args->call_stack;
282   return GRPC_ERROR_NONE;
283 }
284 
client_destroy_call_elem(grpc_call_element * elem,const grpc_call_final_info * final_info,grpc_closure *)285 static void client_destroy_call_elem(grpc_call_element* elem,
286                                      const grpc_call_final_info* final_info,
287                                      grpc_closure* /*ignored*/) {
288   final_status_data* data = static_cast<final_status_data*>(elem->call_data);
289   gpr_mu_lock(&g_mu);
290   // Some fixtures, like proxies, will spawn intermidiate calls
291   // We only want the results from our explicit calls
292   if (data->call == g_client_call_stack) {
293     g_client_status_code = final_info->final_status;
294     g_client_code_recv = true;
295     gpr_cv_signal(&g_client_code_cv);
296   }
297   gpr_mu_unlock(&g_mu);
298 }
299 
server_destroy_call_elem(grpc_call_element * elem,const grpc_call_final_info * final_info,grpc_closure *)300 static void server_destroy_call_elem(grpc_call_element* elem,
301                                      const grpc_call_final_info* final_info,
302                                      grpc_closure* /*ignored*/) {
303   final_status_data* data = static_cast<final_status_data*>(elem->call_data);
304   gpr_mu_lock(&g_mu);
305   // Some fixtures, like proxies, will spawn intermidiate calls
306   // We only want the results from our explicit calls
307   if (data->call == g_server_call_stack) {
308     g_server_status_code = final_info->final_status;
309     g_server_code_recv = true;
310     gpr_cv_signal(&g_server_code_cv);
311   }
312   gpr_mu_unlock(&g_mu);
313 }
314 
init_channel_elem(grpc_channel_element *,grpc_channel_element_args *)315 static grpc_error* init_channel_elem(grpc_channel_element* /*elem*/,
316                                      grpc_channel_element_args* /*args*/) {
317   return GRPC_ERROR_NONE;
318 }
319 
destroy_channel_elem(grpc_channel_element *)320 static void destroy_channel_elem(grpc_channel_element* /*elem*/) {}
321 
322 static const grpc_channel_filter test_client_filter = {
323     grpc_call_next_op,
324     grpc_channel_next_op,
325     sizeof(final_status_data),
326     init_call_elem,
327     grpc_call_stack_ignore_set_pollset_or_pollset_set,
328     client_destroy_call_elem,
329     0,
330     init_channel_elem,
331     destroy_channel_elem,
332     grpc_channel_next_get_info,
333     "client_filter_status_code"};
334 
335 static const grpc_channel_filter test_server_filter = {
336     server_start_transport_stream_op_batch,
337     grpc_channel_next_op,
338     sizeof(final_status_data),
339     init_call_elem,
340     grpc_call_stack_ignore_set_pollset_or_pollset_set,
341     server_destroy_call_elem,
342     0,
343     init_channel_elem,
344     destroy_channel_elem,
345     grpc_channel_next_get_info,
346     "server_filter_status_code"};
347 
348 /*******************************************************************************
349  * Registration
350  */
351 
maybe_add_filter(grpc_channel_stack_builder * builder,void * arg)352 static bool maybe_add_filter(grpc_channel_stack_builder* builder, void* arg) {
353   grpc_channel_filter* filter = static_cast<grpc_channel_filter*>(arg);
354   if (g_enable_filter) {
355     // Want to add the filter as close to the end as possible, to make
356     // sure that all of the filters work well together.  However, we
357     // can't add it at the very end, because the
358     // connected_channel/client_channel filter must be the last one.
359     // So we add it right before the last one.
360     grpc_channel_stack_builder_iterator* it =
361         grpc_channel_stack_builder_create_iterator_at_last(builder);
362     GPR_ASSERT(grpc_channel_stack_builder_move_prev(it));
363     const bool retval = grpc_channel_stack_builder_add_filter_before(
364         it, filter, nullptr, nullptr);
365     grpc_channel_stack_builder_iterator_destroy(it);
366     return retval;
367   } else {
368     return true;
369   }
370 }
371 
init_plugin(void)372 static void init_plugin(void) {
373   gpr_mu_init(&g_mu);
374   gpr_cv_init(&g_client_code_cv);
375   gpr_cv_init(&g_server_code_cv);
376   g_client_code_recv = false;
377   g_server_code_recv = false;
378 
379   grpc_channel_init_register_stage(GRPC_CLIENT_CHANNEL, INT_MAX,
380                                    maybe_add_filter,
381                                    (void*)&test_client_filter);
382   grpc_channel_init_register_stage(GRPC_CLIENT_DIRECT_CHANNEL, INT_MAX,
383                                    maybe_add_filter,
384                                    (void*)&test_client_filter);
385   grpc_channel_init_register_stage(GRPC_SERVER_CHANNEL, INT_MAX,
386                                    maybe_add_filter,
387                                    (void*)&test_server_filter);
388 }
389 
destroy_plugin(void)390 static void destroy_plugin(void) {
391   gpr_cv_destroy(&g_client_code_cv);
392   gpr_cv_destroy(&g_server_code_cv);
393   gpr_mu_destroy(&g_mu);
394 }
395 
filter_status_code(grpc_end2end_test_config config)396 void filter_status_code(grpc_end2end_test_config config) {
397   g_enable_filter = true;
398   test_request(config);
399   g_enable_filter = false;
400 }
401 
filter_status_code_pre_init(void)402 void filter_status_code_pre_init(void) {
403   grpc_register_plugin(init_plugin, destroy_plugin);
404 }
405