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