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