1 /*
2  *
3  * Copyright 2016 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/byte_buffer.h>
25 #include <grpc/grpc.h>
26 #include <grpc/support/alloc.h>
27 #include <grpc/support/log.h>
28 #include <grpc/support/string_util.h>
29 #include <grpc/support/time.h>
30 #include "src/core/lib/gpr/string.h"
31 #include "src/core/lib/iomgr/error.h"
32 #include "test/core/end2end/cq_verifier.h"
33 
34 enum { TIMEOUT = 200000 };
35 
tag(intptr_t t)36 static void* tag(intptr_t t) { return (void*)t; }
37 
38 void gpr_default_log(gpr_log_func_args* args);
39 
test_no_log(gpr_log_func_args * args)40 static void test_no_log(gpr_log_func_args* args) {
41   char* message = nullptr;
42   gpr_asprintf(&message, "Unwanted log: %s", args->message);
43   args->message = message;
44   gpr_default_log(args);
45   gpr_free(message);
46   abort();
47 }
48 
test_no_error_log(gpr_log_func_args * args)49 static void test_no_error_log(gpr_log_func_args* args) {
50   if (args->severity == GPR_LOG_SEVERITY_ERROR) {
51     test_no_log(args);
52   }
53 }
54 
55 static gpr_atm g_log_func = (gpr_atm)gpr_default_log;
56 
log_dispatcher_func(gpr_log_func_args * args)57 static void log_dispatcher_func(gpr_log_func_args* args) {
58   gpr_log_func log_func = (gpr_log_func)gpr_atm_no_barrier_load(&g_log_func);
59   log_func(args);
60 }
61 
begin_test(grpc_end2end_test_config config,const char * test_name,grpc_channel_args * client_args,grpc_channel_args * server_args)62 static grpc_end2end_test_fixture begin_test(grpc_end2end_test_config config,
63                                             const char* test_name,
64                                             grpc_channel_args* client_args,
65                                             grpc_channel_args* server_args) {
66   grpc_end2end_test_fixture f;
67   gpr_log(GPR_INFO, "Running test: %s/%s", test_name, config.name);
68   f = config.create_fixture(client_args, server_args);
69   config.init_server(&f, server_args);
70   config.init_client(&f, client_args);
71   return f;
72 }
73 
n_seconds_from_now(int n)74 static gpr_timespec n_seconds_from_now(int n) {
75   return grpc_timeout_seconds_to_deadline(n);
76 }
77 
five_seconds_from_now(void)78 static gpr_timespec five_seconds_from_now(void) {
79   return n_seconds_from_now(5);
80 }
81 
drain_cq(grpc_completion_queue * cq)82 static void drain_cq(grpc_completion_queue* cq) {
83   grpc_event ev;
84   do {
85     ev = grpc_completion_queue_next(cq, five_seconds_from_now(), nullptr);
86   } while (ev.type != GRPC_QUEUE_SHUTDOWN);
87 }
88 
shutdown_server(grpc_end2end_test_fixture * f)89 static void shutdown_server(grpc_end2end_test_fixture* f) {
90   if (!f->server) return;
91   grpc_server_shutdown_and_notify(f->server, f->shutdown_cq, tag(1000));
92   GPR_ASSERT(grpc_completion_queue_pluck(f->shutdown_cq, tag(1000),
93                                          grpc_timeout_seconds_to_deadline(5),
94                                          nullptr)
95                  .type == GRPC_OP_COMPLETE);
96   grpc_server_destroy(f->server);
97   f->server = nullptr;
98 }
99 
shutdown_client(grpc_end2end_test_fixture * f)100 static void shutdown_client(grpc_end2end_test_fixture* f) {
101   if (!f->client) return;
102   grpc_channel_destroy(f->client);
103   f->client = nullptr;
104 }
105 
end_test(grpc_end2end_test_fixture * f)106 static void end_test(grpc_end2end_test_fixture* f) {
107   shutdown_server(f);
108   shutdown_client(f);
109 
110   grpc_completion_queue_shutdown(f->cq);
111   drain_cq(f->cq);
112   grpc_completion_queue_destroy(f->cq);
113   grpc_completion_queue_destroy(f->shutdown_cq);
114 }
115 
simple_request_body(grpc_end2end_test_config,grpc_end2end_test_fixture f)116 static void simple_request_body(grpc_end2end_test_config /*config*/,
117                                 grpc_end2end_test_fixture f) {
118   grpc_call* c;
119   grpc_call* s;
120   cq_verifier* cqv = cq_verifier_create(f.cq);
121   grpc_op ops[6];
122   grpc_op* op;
123   grpc_metadata_array initial_metadata_recv;
124   grpc_metadata_array trailing_metadata_recv;
125   grpc_metadata_array request_metadata_recv;
126   grpc_call_details call_details;
127   grpc_status_code status;
128   grpc_call_error error;
129   grpc_slice details;
130   int was_cancelled = 2;
131   char* peer;
132 
133   gpr_timespec deadline = five_seconds_from_now();
134   c = grpc_channel_create_call(f.client, nullptr, GRPC_PROPAGATE_DEFAULTS, f.cq,
135                                grpc_slice_from_static_string("/foo"), nullptr,
136                                deadline, nullptr);
137   GPR_ASSERT(c);
138 
139   peer = grpc_call_get_peer(c);
140   GPR_ASSERT(peer != nullptr);
141   gpr_free(peer);
142 
143   grpc_metadata_array_init(&initial_metadata_recv);
144   grpc_metadata_array_init(&trailing_metadata_recv);
145   grpc_metadata_array_init(&request_metadata_recv);
146   grpc_call_details_init(&call_details);
147 
148   memset(ops, 0, sizeof(ops));
149   op = ops;
150   op->op = GRPC_OP_SEND_INITIAL_METADATA;
151   op->data.send_initial_metadata.count = 0;
152   op->flags = 0;
153   op->reserved = nullptr;
154   op++;
155   op->op = GRPC_OP_SEND_CLOSE_FROM_CLIENT;
156   op->flags = 0;
157   op->reserved = nullptr;
158   op++;
159   op->op = GRPC_OP_RECV_INITIAL_METADATA;
160   op->data.recv_initial_metadata.recv_initial_metadata = &initial_metadata_recv;
161   op->flags = 0;
162   op->reserved = nullptr;
163   op++;
164   op->op = GRPC_OP_RECV_STATUS_ON_CLIENT;
165   op->data.recv_status_on_client.trailing_metadata = &trailing_metadata_recv;
166   op->data.recv_status_on_client.status = &status;
167   op->data.recv_status_on_client.status_details = &details;
168   op->flags = 0;
169   op->reserved = nullptr;
170   op++;
171   error = grpc_call_start_batch(c, ops, static_cast<size_t>(op - ops), tag(1),
172                                 nullptr);
173   GPR_ASSERT(GRPC_CALL_OK == error);
174 
175   error =
176       grpc_server_request_call(f.server, &s, &call_details,
177                                &request_metadata_recv, f.cq, f.cq, tag(101));
178   GPR_ASSERT(GRPC_CALL_OK == error);
179   CQ_EXPECT_COMPLETION(cqv, tag(101), 1);
180   cq_verify(cqv);
181 
182   peer = grpc_call_get_peer(s);
183   GPR_ASSERT(peer != nullptr);
184   gpr_free(peer);
185   peer = grpc_call_get_peer(c);
186   GPR_ASSERT(peer != nullptr);
187   gpr_free(peer);
188 
189   memset(ops, 0, sizeof(ops));
190   op = ops;
191   op->op = GRPC_OP_SEND_INITIAL_METADATA;
192   op->data.send_initial_metadata.count = 0;
193   op->flags = 0;
194   op->reserved = nullptr;
195   op++;
196   op->op = GRPC_OP_SEND_STATUS_FROM_SERVER;
197   op->data.send_status_from_server.trailing_metadata_count = 0;
198   op->data.send_status_from_server.status = GRPC_STATUS_UNIMPLEMENTED;
199   grpc_slice status_details = grpc_slice_from_static_string("xyz");
200   op->data.send_status_from_server.status_details = &status_details;
201   op->flags = 0;
202   op->reserved = nullptr;
203   op++;
204   op->op = GRPC_OP_RECV_CLOSE_ON_SERVER;
205   op->data.recv_close_on_server.cancelled = &was_cancelled;
206   op->flags = 0;
207   op->reserved = nullptr;
208   op++;
209   error = grpc_call_start_batch(s, ops, static_cast<size_t>(op - ops), tag(102),
210                                 nullptr);
211   GPR_ASSERT(GRPC_CALL_OK == error);
212 
213   CQ_EXPECT_COMPLETION(cqv, tag(102), 1);
214   CQ_EXPECT_COMPLETION(cqv, tag(1), 1);
215   cq_verify(cqv);
216 
217   GPR_ASSERT(status == GRPC_STATUS_UNIMPLEMENTED);
218   GPR_ASSERT(0 == grpc_slice_str_cmp(details, "xyz"));
219   GPR_ASSERT(0 == grpc_slice_str_cmp(call_details.method, "/foo"));
220   GPR_ASSERT(0 == call_details.flags);
221   GPR_ASSERT(was_cancelled == 1);
222 
223   grpc_slice_unref(details);
224   grpc_metadata_array_destroy(&initial_metadata_recv);
225   grpc_metadata_array_destroy(&trailing_metadata_recv);
226   grpc_metadata_array_destroy(&request_metadata_recv);
227   grpc_call_details_destroy(&call_details);
228 
229   grpc_call_unref(c);
230   grpc_call_unref(s);
231 
232   cq_verifier_destroy(cqv);
233 }
234 
test_invoke_simple_request(grpc_end2end_test_config config)235 static void test_invoke_simple_request(grpc_end2end_test_config config) {
236   grpc_end2end_test_fixture f;
237 
238   f = begin_test(config, "test_invoke_simple_request_with_no_error_logging",
239                  nullptr, nullptr);
240   simple_request_body(config, f);
241   end_test(&f);
242   config.tear_down_data(&f);
243 }
244 
test_invoke_10_simple_requests(grpc_end2end_test_config config)245 static void test_invoke_10_simple_requests(grpc_end2end_test_config config) {
246   int i;
247   grpc_end2end_test_fixture f =
248       begin_test(config, "test_invoke_10_simple_requests_with_no_error_logging",
249                  nullptr, nullptr);
250   for (i = 0; i < 10; i++) {
251     simple_request_body(config, f);
252     gpr_log(GPR_INFO, "Passed simple request %d", i);
253   }
254   simple_request_body(config, f);
255   end_test(&f);
256   config.tear_down_data(&f);
257 }
258 
test_no_error_logging_in_entire_process(grpc_end2end_test_config config)259 static void test_no_error_logging_in_entire_process(
260     grpc_end2end_test_config config) {
261   int i;
262   gpr_atm_no_barrier_store(&g_log_func, (gpr_atm)test_no_error_log);
263   for (i = 0; i < 10; i++) {
264     test_invoke_simple_request(config);
265   }
266   test_invoke_10_simple_requests(config);
267   gpr_atm_no_barrier_store(&g_log_func, (gpr_atm)gpr_default_log);
268 }
269 
test_no_logging_in_one_request(grpc_end2end_test_config config)270 static void test_no_logging_in_one_request(grpc_end2end_test_config config) {
271   int i;
272   grpc_end2end_test_fixture f =
273       begin_test(config, "test_no_logging_in_last_request", nullptr, nullptr);
274   for (i = 0; i < 10; i++) {
275     simple_request_body(config, f);
276   }
277   gpr_atm_no_barrier_store(&g_log_func, (gpr_atm)test_no_log);
278   simple_request_body(config, f);
279   gpr_atm_no_barrier_store(&g_log_func, (gpr_atm)gpr_default_log);
280   end_test(&f);
281   config.tear_down_data(&f);
282 }
283 
no_logging(grpc_end2end_test_config config)284 void no_logging(grpc_end2end_test_config config) {
285   gpr_set_log_verbosity(GPR_LOG_SEVERITY_DEBUG);
286   grpc_tracer_set_enabled("all", 0);
287   gpr_set_log_function(log_dispatcher_func);
288   test_no_logging_in_one_request(config);
289   test_no_error_logging_in_entire_process(config);
290   gpr_set_log_function(gpr_default_log);
291 }
292 
no_logging_pre_init(void)293 void no_logging_pre_init(void) {}
294