blob: 30a1b8de7747297d1cf577740e05f2d5821caf9b [file] [log] [blame]
Yuchen Zengaf5c5052017-02-28 16:24:00 -08001/*
2 *
Jan Tattermusch7897ae92017-06-07 22:57:36 +02003 * Copyright 2017 gRPC authors.
Yuchen Zengaf5c5052017-02-28 16:24:00 -08004 *
Jan Tattermusch7897ae92017-06-07 22:57:36 +02005 * 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
Yuchen Zengaf5c5052017-02-28 16:24:00 -08008 *
Jan Tattermusch7897ae92017-06-07 22:57:36 +02009 * http://www.apache.org/licenses/LICENSE-2.0
Yuchen Zengaf5c5052017-02-28 16:24:00 -080010 *
Jan Tattermusch7897ae92017-06-07 22:57:36 +020011 * 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.
Yuchen Zengaf5c5052017-02-28 16:24:00 -080016 *
17 */
18
19#include "test/core/end2end/end2end_tests.h"
20
21#include <string.h>
22
23#include <grpc/support/alloc.h>
24#include <grpc/support/log.h>
25#include <grpc/support/sync.h>
26#include <grpc/support/thd.h>
27#include <grpc/support/time.h>
28#include <grpc/support/useful.h>
29
30#include "test/core/end2end/cq_verifier.h"
31
32#define MAX_PING_STRIKES 1
33
Craig Tillerbaa14a92017-11-03 09:09:36 -070034static void* tag(intptr_t t) { return (void*)t; }
Yuchen Zengaf5c5052017-02-28 16:24:00 -080035
Craig Tillerbaa14a92017-11-03 09:09:36 -070036static void drain_cq(grpc_completion_queue* cq) {
Yuchen Zengaf5c5052017-02-28 16:24:00 -080037 grpc_event ev;
38 do {
39 ev = grpc_completion_queue_next(cq, grpc_timeout_seconds_to_deadline(5),
Craig Tiller4ac2b8e2017-11-10 14:14:17 -080040 nullptr);
Yuchen Zengaf5c5052017-02-28 16:24:00 -080041 } while (ev.type != GRPC_QUEUE_SHUTDOWN);
42}
43
Craig Tillerbaa14a92017-11-03 09:09:36 -070044static void shutdown_server(grpc_end2end_test_fixture* f) {
Yuchen Zengaf5c5052017-02-28 16:24:00 -080045 if (!f->server) return;
46 grpc_server_destroy(f->server);
Craig Tiller4ac2b8e2017-11-10 14:14:17 -080047 f->server = nullptr;
Yuchen Zengaf5c5052017-02-28 16:24:00 -080048}
49
Craig Tillerbaa14a92017-11-03 09:09:36 -070050static void shutdown_client(grpc_end2end_test_fixture* f) {
Yuchen Zengaf5c5052017-02-28 16:24:00 -080051 if (!f->client) return;
52 grpc_channel_destroy(f->client);
Craig Tiller4ac2b8e2017-11-10 14:14:17 -080053 f->client = nullptr;
Yuchen Zengaf5c5052017-02-28 16:24:00 -080054}
55
Craig Tillerbaa14a92017-11-03 09:09:36 -070056static void end_test(grpc_end2end_test_fixture* f) {
Yuchen Zengaf5c5052017-02-28 16:24:00 -080057 shutdown_server(f);
58 shutdown_client(f);
59
60 grpc_completion_queue_shutdown(f->cq);
61 drain_cq(f->cq);
62 grpc_completion_queue_destroy(f->cq);
Sree Kuchibhotlae1bfff02017-04-06 13:17:20 -070063 grpc_completion_queue_destroy(f->shutdown_cq);
Yuchen Zengaf5c5052017-02-28 16:24:00 -080064}
65
66static void test_bad_ping(grpc_end2end_test_config config) {
Craig Tiller4ac2b8e2017-11-10 14:14:17 -080067 grpc_end2end_test_fixture f = config.create_fixture(nullptr, nullptr);
Craig Tillerbaa14a92017-11-03 09:09:36 -070068 cq_verifier* cqv = cq_verifier_create(f.cq);
Yash Tibrewal34a57d02017-10-23 15:33:21 -070069 grpc_arg client_a[3];
70 client_a[0].type = GRPC_ARG_INTEGER;
71 client_a[0].key =
Yash Tibrewal40422d52017-11-06 14:39:17 -080072 const_cast<char*>(GRPC_ARG_HTTP2_MIN_SENT_PING_INTERVAL_WITHOUT_DATA_MS);
Yash Tibrewal34a57d02017-10-23 15:33:21 -070073 client_a[0].value.integer = 10;
74 client_a[1].type = GRPC_ARG_INTEGER;
Yash Tibrewal40422d52017-11-06 14:39:17 -080075 client_a[1].key = const_cast<char*>(GRPC_ARG_HTTP2_MAX_PINGS_WITHOUT_DATA);
Yash Tibrewal34a57d02017-10-23 15:33:21 -070076 client_a[1].value.integer = 0;
77 client_a[2].type = GRPC_ARG_INTEGER;
Yash Tibrewal40422d52017-11-06 14:39:17 -080078 client_a[2].key = const_cast<char*>(GRPC_ARG_HTTP2_BDP_PROBE);
Yash Tibrewal34a57d02017-10-23 15:33:21 -070079 client_a[2].value.integer = 0;
80 grpc_arg server_a[3];
81 server_a[0].type = GRPC_ARG_INTEGER;
82 server_a[0].key =
Yash Tibrewal40422d52017-11-06 14:39:17 -080083 const_cast<char*>(GRPC_ARG_HTTP2_MIN_RECV_PING_INTERVAL_WITHOUT_DATA_MS);
Yash Tibrewal34a57d02017-10-23 15:33:21 -070084 server_a[0].value.integer = 300000 /* 5 minutes */;
85 server_a[1].type = GRPC_ARG_INTEGER;
Yash Tibrewal40422d52017-11-06 14:39:17 -080086 server_a[1].key = const_cast<char*>(GRPC_ARG_HTTP2_MAX_PING_STRIKES);
Yash Tibrewal34a57d02017-10-23 15:33:21 -070087 server_a[1].value.integer = MAX_PING_STRIKES;
88 server_a[2].type = GRPC_ARG_INTEGER;
Yash Tibrewal40422d52017-11-06 14:39:17 -080089 server_a[2].key = const_cast<char*>(GRPC_ARG_HTTP2_BDP_PROBE);
Yash Tibrewal34a57d02017-10-23 15:33:21 -070090 server_a[2].value.integer = 0;
91 grpc_channel_args client_args = {GPR_ARRAY_SIZE(client_a), client_a};
92 grpc_channel_args server_args = {GPR_ARRAY_SIZE(server_a), server_a};
Yuchen Zengaf5c5052017-02-28 16:24:00 -080093
94 config.init_client(&f, &client_args);
95 config.init_server(&f, &server_args);
96
Craig Tillerbaa14a92017-11-03 09:09:36 -070097 grpc_call* c;
98 grpc_call* s;
Yuchen Zengaf5c5052017-02-28 16:24:00 -080099 gpr_timespec deadline = grpc_timeout_seconds_to_deadline(10);
100 grpc_op ops[6];
Craig Tillerbaa14a92017-11-03 09:09:36 -0700101 grpc_op* op;
Yuchen Zengaf5c5052017-02-28 16:24:00 -0800102 grpc_metadata_array initial_metadata_recv;
103 grpc_metadata_array trailing_metadata_recv;
104 grpc_metadata_array request_metadata_recv;
105 grpc_call_details call_details;
106 grpc_status_code status;
107 grpc_call_error error;
108 grpc_slice details;
109 int was_cancelled = 2;
110
111 c = grpc_channel_create_call(
Craig Tiller4ac2b8e2017-11-10 14:14:17 -0800112 f.client, nullptr, GRPC_PROPAGATE_DEFAULTS, f.cq,
Yuchen Zengaf5c5052017-02-28 16:24:00 -0800113 grpc_slice_from_static_string("/foo"),
114 get_host_override_slice("foo.test.google.fr:1234", config), deadline,
Craig Tiller4ac2b8e2017-11-10 14:14:17 -0800115 nullptr);
Yuchen Zengaf5c5052017-02-28 16:24:00 -0800116 GPR_ASSERT(c);
117
118 grpc_metadata_array_init(&initial_metadata_recv);
119 grpc_metadata_array_init(&trailing_metadata_recv);
120 grpc_metadata_array_init(&request_metadata_recv);
121 grpc_call_details_init(&call_details);
122
123 memset(ops, 0, sizeof(ops));
124 op = ops;
125 op->op = GRPC_OP_SEND_INITIAL_METADATA;
126 op->data.send_initial_metadata.count = 0;
Craig Tiller4ac2b8e2017-11-10 14:14:17 -0800127 op->data.send_initial_metadata.metadata = nullptr;
Yuchen Zengaf5c5052017-02-28 16:24:00 -0800128 op->flags = 0;
Craig Tiller4ac2b8e2017-11-10 14:14:17 -0800129 op->reserved = nullptr;
Yuchen Zengaf5c5052017-02-28 16:24:00 -0800130 op++;
131 op->op = GRPC_OP_SEND_CLOSE_FROM_CLIENT;
132 op->flags = 0;
Craig Tiller4ac2b8e2017-11-10 14:14:17 -0800133 op->reserved = nullptr;
Yuchen Zengaf5c5052017-02-28 16:24:00 -0800134 op++;
135 op->op = GRPC_OP_RECV_INITIAL_METADATA;
136 op->data.recv_initial_metadata.recv_initial_metadata = &initial_metadata_recv;
137 op->flags = 0;
Craig Tiller4ac2b8e2017-11-10 14:14:17 -0800138 op->reserved = nullptr;
Yuchen Zengaf5c5052017-02-28 16:24:00 -0800139 op++;
140 op->op = GRPC_OP_RECV_STATUS_ON_CLIENT;
141 op->data.recv_status_on_client.trailing_metadata = &trailing_metadata_recv;
142 op->data.recv_status_on_client.status = &status;
143 op->data.recv_status_on_client.status_details = &details;
144 op->flags = 0;
Craig Tiller4ac2b8e2017-11-10 14:14:17 -0800145 op->reserved = nullptr;
Yuchen Zengaf5c5052017-02-28 16:24:00 -0800146 op++;
Craig Tiller4ac2b8e2017-11-10 14:14:17 -0800147 error = grpc_call_start_batch(c, ops, (size_t)(op - ops), tag(1), nullptr);
Yuchen Zengaf5c5052017-02-28 16:24:00 -0800148 GPR_ASSERT(GRPC_CALL_OK == error);
149
150 error =
151 grpc_server_request_call(f.server, &s, &call_details,
152 &request_metadata_recv, f.cq, f.cq, tag(101));
153 GPR_ASSERT(GRPC_CALL_OK == error);
154 CQ_EXPECT_COMPLETION(cqv, tag(101), 1);
155 cq_verify(cqv);
156
157 // Send too many pings to the server to trigger the punishment:
Craig Tiller07e81dd2017-07-20 13:55:58 -0700158 // Each ping will trigger a ping strike, and we need at least MAX_PING_STRIKES
159 // strikes to trigger the punishment. So (MAX_PING_STRIKES + 1) pings are
160 // needed here.
Yuchen Zengaf5c5052017-02-28 16:24:00 -0800161 int i;
Craig Tiller07e81dd2017-07-20 13:55:58 -0700162 for (i = 1; i <= MAX_PING_STRIKES + 1; i++) {
Craig Tiller4ac2b8e2017-11-10 14:14:17 -0800163 grpc_channel_ping(f.client, f.cq, tag(200 + i), nullptr);
Craig Tiller07e81dd2017-07-20 13:55:58 -0700164 CQ_EXPECT_COMPLETION(cqv, tag(200 + i), 1);
165 if (i == MAX_PING_STRIKES + 1) {
166 CQ_EXPECT_COMPLETION(cqv, tag(1), 1);
167 }
Yuchen Zengaf5c5052017-02-28 16:24:00 -0800168 cq_verify(cqv);
169 }
170
171 memset(ops, 0, sizeof(ops));
172 op = ops;
173 op->op = GRPC_OP_SEND_INITIAL_METADATA;
174 op->data.send_initial_metadata.count = 0;
175 op->flags = 0;
Craig Tiller4ac2b8e2017-11-10 14:14:17 -0800176 op->reserved = nullptr;
Yuchen Zengaf5c5052017-02-28 16:24:00 -0800177 op++;
178 op->op = GRPC_OP_SEND_STATUS_FROM_SERVER;
179 op->data.send_status_from_server.trailing_metadata_count = 0;
180 op->data.send_status_from_server.status = GRPC_STATUS_UNIMPLEMENTED;
181 grpc_slice status_details = grpc_slice_from_static_string("xyz");
182 op->data.send_status_from_server.status_details = &status_details;
183 op->flags = 0;
Craig Tiller4ac2b8e2017-11-10 14:14:17 -0800184 op->reserved = nullptr;
Yuchen Zengaf5c5052017-02-28 16:24:00 -0800185 op++;
186 op->op = GRPC_OP_RECV_CLOSE_ON_SERVER;
187 op->data.recv_close_on_server.cancelled = &was_cancelled;
188 op->flags = 0;
Craig Tiller4ac2b8e2017-11-10 14:14:17 -0800189 op->reserved = nullptr;
Yuchen Zengaf5c5052017-02-28 16:24:00 -0800190 op++;
Craig Tiller4ac2b8e2017-11-10 14:14:17 -0800191 error = grpc_call_start_batch(s, ops, (size_t)(op - ops), tag(102), nullptr);
Yuchen Zengaf5c5052017-02-28 16:24:00 -0800192 GPR_ASSERT(GRPC_CALL_OK == error);
193
194 CQ_EXPECT_COMPLETION(cqv, tag(102), 1);
Yuchen Zengaf5c5052017-02-28 16:24:00 -0800195 cq_verify(cqv);
196
197 grpc_server_shutdown_and_notify(f.server, f.cq, tag(0xdead));
198 CQ_EXPECT_COMPLETION(cqv, tag(0xdead), 1);
199 cq_verify(cqv);
200
Craig Tiller4a647142017-04-05 06:38:48 -0700201 grpc_call_unref(s);
Yuchen Zengaf5c5052017-02-28 16:24:00 -0800202
203 // The connection should be closed immediately after the misbehaved pings,
204 // the in-progress RPC should fail.
Craig Tiller1c3dd002017-10-12 16:08:56 -0700205 GPR_ASSERT(status == GRPC_STATUS_UNAVAILABLE);
Yuchen Zengaf5c5052017-02-28 16:24:00 -0800206 GPR_ASSERT(0 == grpc_slice_str_cmp(call_details.method, "/foo"));
207 validate_host_override_string("foo.test.google.fr:1234", call_details.host,
208 config);
209 GPR_ASSERT(was_cancelled == 1);
210
211 grpc_slice_unref(details);
212 grpc_metadata_array_destroy(&initial_metadata_recv);
213 grpc_metadata_array_destroy(&trailing_metadata_recv);
214 grpc_metadata_array_destroy(&request_metadata_recv);
215 grpc_call_details_destroy(&call_details);
Craig Tiller4a647142017-04-05 06:38:48 -0700216 grpc_call_unref(c);
Yuchen Zengaf5c5052017-02-28 16:24:00 -0800217 cq_verifier_destroy(cqv);
218 end_test(&f);
219 config.tear_down_data(&f);
220}
221
222void bad_ping(grpc_end2end_test_config config) {
223 GPR_ASSERT(config.feature_mask & FEATURE_MASK_SUPPORTS_DELAYED_CONNECTION);
224 test_bad_ping(config);
225}
226
227void bad_ping_pre_init(void) {}