Yuchen Zeng | af5c505 | 2017-02-28 16:24:00 -0800 | [diff] [blame] | 1 | /* |
| 2 | * |
Jan Tattermusch | 7897ae9 | 2017-06-07 22:57:36 +0200 | [diff] [blame] | 3 | * Copyright 2017 gRPC authors. |
Yuchen Zeng | af5c505 | 2017-02-28 16:24:00 -0800 | [diff] [blame] | 4 | * |
Jan Tattermusch | 7897ae9 | 2017-06-07 22:57:36 +0200 | [diff] [blame] | 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 |
Yuchen Zeng | af5c505 | 2017-02-28 16:24:00 -0800 | [diff] [blame] | 8 | * |
Jan Tattermusch | 7897ae9 | 2017-06-07 22:57:36 +0200 | [diff] [blame] | 9 | * http://www.apache.org/licenses/LICENSE-2.0 |
Yuchen Zeng | af5c505 | 2017-02-28 16:24:00 -0800 | [diff] [blame] | 10 | * |
Jan Tattermusch | 7897ae9 | 2017-06-07 22:57:36 +0200 | [diff] [blame] | 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. |
Yuchen Zeng | af5c505 | 2017-02-28 16:24:00 -0800 | [diff] [blame] | 16 | * |
| 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 | |
| 34 | static void *tag(intptr_t t) { return (void *)t; } |
| 35 | |
| 36 | static void drain_cq(grpc_completion_queue *cq) { |
| 37 | grpc_event ev; |
| 38 | do { |
| 39 | ev = grpc_completion_queue_next(cq, grpc_timeout_seconds_to_deadline(5), |
| 40 | NULL); |
| 41 | } while (ev.type != GRPC_QUEUE_SHUTDOWN); |
| 42 | } |
| 43 | |
| 44 | static void shutdown_server(grpc_end2end_test_fixture *f) { |
| 45 | if (!f->server) return; |
| 46 | grpc_server_destroy(f->server); |
| 47 | f->server = NULL; |
| 48 | } |
| 49 | |
| 50 | static void shutdown_client(grpc_end2end_test_fixture *f) { |
| 51 | if (!f->client) return; |
| 52 | grpc_channel_destroy(f->client); |
| 53 | f->client = NULL; |
| 54 | } |
| 55 | |
| 56 | static void end_test(grpc_end2end_test_fixture *f) { |
| 57 | 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 Kuchibhotla | e1bfff0 | 2017-04-06 13:17:20 -0700 | [diff] [blame] | 63 | grpc_completion_queue_destroy(f->shutdown_cq); |
Yuchen Zeng | af5c505 | 2017-02-28 16:24:00 -0800 | [diff] [blame] | 64 | } |
| 65 | |
| 66 | static void test_bad_ping(grpc_end2end_test_config config) { |
| 67 | grpc_end2end_test_fixture f = config.create_fixture(NULL, NULL); |
| 68 | cq_verifier *cqv = cq_verifier_create(f.cq); |
Yuchen Zeng | ad32075 | 2017-05-15 15:38:06 -0700 | [diff] [blame^] | 69 | grpc_arg client_a[] = { |
| 70 | {.type = GRPC_ARG_INTEGER, |
| 71 | .key = GRPC_ARG_HTTP2_MIN_SENT_PING_INTERVAL_WITHOUT_DATA_MS, |
| 72 | .value.integer = 10}, |
| 73 | {.type = GRPC_ARG_INTEGER, |
| 74 | .key = GRPC_ARG_HTTP2_MAX_PINGS_WITHOUT_DATA, |
| 75 | .value.integer = 0}, |
| 76 | {.type = GRPC_ARG_INTEGER, |
| 77 | .key = GRPC_ARG_HTTP2_BDP_PROBE, |
| 78 | .value.integer = 0}}; |
Yuchen Zeng | af5c505 | 2017-02-28 16:24:00 -0800 | [diff] [blame] | 79 | grpc_arg server_a[] = { |
| 80 | {.type = GRPC_ARG_INTEGER, |
Yuchen Zeng | ad32075 | 2017-05-15 15:38:06 -0700 | [diff] [blame^] | 81 | .key = GRPC_ARG_HTTP2_MIN_RECV_PING_INTERVAL_WITHOUT_DATA_MS, |
Yuchen Zeng | c18d4b3 | 2017-03-29 15:04:42 -0700 | [diff] [blame] | 82 | .value.integer = 300000 /* 5 minutes */}, |
Yuchen Zeng | af5c505 | 2017-02-28 16:24:00 -0800 | [diff] [blame] | 83 | {.type = GRPC_ARG_INTEGER, |
| 84 | .key = GRPC_ARG_HTTP2_MAX_PING_STRIKES, |
Yuchen Zeng | 32e41b4 | 2017-06-23 16:34:34 -0700 | [diff] [blame] | 85 | .value.integer = MAX_PING_STRIKES}, |
| 86 | {.type = GRPC_ARG_INTEGER, |
| 87 | .key = GRPC_ARG_HTTP2_BDP_PROBE, |
| 88 | .value.integer = 0}}; |
Yuchen Zeng | af5c505 | 2017-02-28 16:24:00 -0800 | [diff] [blame] | 89 | grpc_channel_args client_args = {.num_args = GPR_ARRAY_SIZE(client_a), |
| 90 | .args = client_a}; |
| 91 | grpc_channel_args server_args = {.num_args = GPR_ARRAY_SIZE(server_a), |
| 92 | .args = server_a}; |
| 93 | |
| 94 | config.init_client(&f, &client_args); |
| 95 | config.init_server(&f, &server_args); |
| 96 | |
| 97 | grpc_call *c; |
| 98 | grpc_call *s; |
| 99 | gpr_timespec deadline = grpc_timeout_seconds_to_deadline(10); |
| 100 | grpc_op ops[6]; |
| 101 | grpc_op *op; |
| 102 | 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( |
| 112 | f.client, NULL, GRPC_PROPAGATE_DEFAULTS, f.cq, |
| 113 | grpc_slice_from_static_string("/foo"), |
| 114 | get_host_override_slice("foo.test.google.fr:1234", config), deadline, |
| 115 | NULL); |
| 116 | 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; |
| 127 | op->data.send_initial_metadata.metadata = NULL; |
| 128 | op->flags = 0; |
| 129 | op->reserved = NULL; |
| 130 | op++; |
| 131 | op->op = GRPC_OP_SEND_CLOSE_FROM_CLIENT; |
| 132 | op->flags = 0; |
| 133 | op->reserved = NULL; |
| 134 | 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; |
| 138 | op->reserved = NULL; |
| 139 | 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; |
| 145 | op->reserved = NULL; |
| 146 | op++; |
| 147 | error = grpc_call_start_batch(c, ops, (size_t)(op - ops), tag(1), NULL); |
| 148 | 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: |
| 158 | // The first ping is sent after data frames, it won't trigger a ping strike. |
| 159 | // Each of the following pings will trigger a ping strike, and we need at |
| 160 | // least (MAX_PING_STRIKES + 1) strikes to trigger the punishment. So |
| 161 | // (MAX_PING_STRIKES + 2) pings are needed here. |
| 162 | int i; |
| 163 | for (i = 200; i < 202 + MAX_PING_STRIKES; i++) { |
| 164 | grpc_channel_ping(f.client, f.cq, tag(i), NULL); |
| 165 | CQ_EXPECT_COMPLETION(cqv, tag(i), 1); |
| 166 | cq_verify(cqv); |
| 167 | } |
| 168 | |
| 169 | memset(ops, 0, sizeof(ops)); |
| 170 | op = ops; |
| 171 | op->op = GRPC_OP_SEND_INITIAL_METADATA; |
| 172 | op->data.send_initial_metadata.count = 0; |
| 173 | op->flags = 0; |
| 174 | op->reserved = NULL; |
| 175 | op++; |
| 176 | op->op = GRPC_OP_SEND_STATUS_FROM_SERVER; |
| 177 | op->data.send_status_from_server.trailing_metadata_count = 0; |
| 178 | op->data.send_status_from_server.status = GRPC_STATUS_UNIMPLEMENTED; |
| 179 | grpc_slice status_details = grpc_slice_from_static_string("xyz"); |
| 180 | op->data.send_status_from_server.status_details = &status_details; |
| 181 | op->flags = 0; |
| 182 | op->reserved = NULL; |
| 183 | op++; |
| 184 | op->op = GRPC_OP_RECV_CLOSE_ON_SERVER; |
| 185 | op->data.recv_close_on_server.cancelled = &was_cancelled; |
| 186 | op->flags = 0; |
| 187 | op->reserved = NULL; |
| 188 | op++; |
| 189 | error = grpc_call_start_batch(s, ops, (size_t)(op - ops), tag(102), NULL); |
| 190 | GPR_ASSERT(GRPC_CALL_OK == error); |
| 191 | |
| 192 | CQ_EXPECT_COMPLETION(cqv, tag(102), 1); |
| 193 | CQ_EXPECT_COMPLETION(cqv, tag(1), 1); |
| 194 | cq_verify(cqv); |
| 195 | |
| 196 | grpc_server_shutdown_and_notify(f.server, f.cq, tag(0xdead)); |
| 197 | CQ_EXPECT_COMPLETION(cqv, tag(0xdead), 1); |
| 198 | cq_verify(cqv); |
| 199 | |
Craig Tiller | 4a64714 | 2017-04-05 06:38:48 -0700 | [diff] [blame] | 200 | grpc_call_unref(s); |
Yuchen Zeng | af5c505 | 2017-02-28 16:24:00 -0800 | [diff] [blame] | 201 | |
| 202 | // The connection should be closed immediately after the misbehaved pings, |
| 203 | // the in-progress RPC should fail. |
| 204 | GPR_ASSERT(status == GRPC_STATUS_UNAVAILABLE); |
| 205 | GPR_ASSERT(0 == grpc_slice_str_cmp(details, "Endpoint read failed")); |
| 206 | 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 Tiller | 4a64714 | 2017-04-05 06:38:48 -0700 | [diff] [blame] | 216 | grpc_call_unref(c); |
Yuchen Zeng | af5c505 | 2017-02-28 16:24:00 -0800 | [diff] [blame] | 217 | cq_verifier_destroy(cqv); |
| 218 | end_test(&f); |
| 219 | config.tear_down_data(&f); |
| 220 | } |
| 221 | |
| 222 | void 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 | |
| 227 | void bad_ping_pre_init(void) {} |