Yuchen Zeng | af5c505 | 2017-02-28 16:24:00 -0800 | [diff] [blame^] | 1 | /* |
| 2 | * |
| 3 | * Copyright 2017, Google Inc. |
| 4 | * All rights reserved. |
| 5 | * |
| 6 | * Redistribution and use in source and binary forms, with or without |
| 7 | * modification, are permitted provided that the following conditions are |
| 8 | * met: |
| 9 | * |
| 10 | * * Redistributions of source code must retain the above copyright |
| 11 | * notice, this list of conditions and the following disclaimer. |
| 12 | * * Redistributions in binary form must reproduce the above |
| 13 | * copyright notice, this list of conditions and the following disclaimer |
| 14 | * in the documentation and/or other materials provided with the |
| 15 | * distribution. |
| 16 | * * Neither the name of Google Inc. nor the names of its |
| 17 | * contributors may be used to endorse or promote products derived from |
| 18 | * this software without specific prior written permission. |
| 19 | * |
| 20 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 21 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 22 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 23 | * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 24 | * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 25 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 26 | * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 27 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 28 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 29 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 30 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 31 | * |
| 32 | */ |
| 33 | |
| 34 | #include "test/core/end2end/end2end_tests.h" |
| 35 | |
| 36 | #include <string.h> |
| 37 | |
| 38 | #include <grpc/support/alloc.h> |
| 39 | #include <grpc/support/log.h> |
| 40 | #include <grpc/support/sync.h> |
| 41 | #include <grpc/support/thd.h> |
| 42 | #include <grpc/support/time.h> |
| 43 | #include <grpc/support/useful.h> |
| 44 | |
| 45 | #include "test/core/end2end/cq_verifier.h" |
| 46 | |
| 47 | #define MAX_PING_STRIKES 1 |
| 48 | |
| 49 | static void *tag(intptr_t t) { return (void *)t; } |
| 50 | |
| 51 | static void drain_cq(grpc_completion_queue *cq) { |
| 52 | grpc_event ev; |
| 53 | do { |
| 54 | ev = grpc_completion_queue_next(cq, grpc_timeout_seconds_to_deadline(5), |
| 55 | NULL); |
| 56 | } while (ev.type != GRPC_QUEUE_SHUTDOWN); |
| 57 | } |
| 58 | |
| 59 | static void shutdown_server(grpc_end2end_test_fixture *f) { |
| 60 | if (!f->server) return; |
| 61 | grpc_server_destroy(f->server); |
| 62 | f->server = NULL; |
| 63 | } |
| 64 | |
| 65 | static void shutdown_client(grpc_end2end_test_fixture *f) { |
| 66 | if (!f->client) return; |
| 67 | grpc_channel_destroy(f->client); |
| 68 | f->client = NULL; |
| 69 | } |
| 70 | |
| 71 | static void end_test(grpc_end2end_test_fixture *f) { |
| 72 | shutdown_server(f); |
| 73 | shutdown_client(f); |
| 74 | |
| 75 | grpc_completion_queue_shutdown(f->cq); |
| 76 | drain_cq(f->cq); |
| 77 | grpc_completion_queue_destroy(f->cq); |
| 78 | } |
| 79 | |
| 80 | static void test_bad_ping(grpc_end2end_test_config config) { |
| 81 | grpc_end2end_test_fixture f = config.create_fixture(NULL, NULL); |
| 82 | cq_verifier *cqv = cq_verifier_create(f.cq); |
| 83 | grpc_arg client_a[] = {{.type = GRPC_ARG_INTEGER, |
| 84 | .key = GRPC_ARG_HTTP2_MIN_TIME_BETWEEN_PINGS_MS, |
| 85 | .value.integer = 0}, |
| 86 | {.type = GRPC_ARG_INTEGER, |
| 87 | .key = GRPC_ARG_HTTP2_MAX_PINGS_WITHOUT_DATA, |
| 88 | .value.integer = 20}}; |
| 89 | grpc_arg server_a[] = { |
| 90 | {.type = GRPC_ARG_INTEGER, |
| 91 | .key = GRPC_ARG_HTTP2_MIN_PING_INTERVAL_WITHOUT_DATA_S, |
| 92 | .value.integer = 300}, |
| 93 | {.type = GRPC_ARG_INTEGER, |
| 94 | .key = GRPC_ARG_HTTP2_MAX_PING_STRIKES, |
| 95 | .value.integer = MAX_PING_STRIKES}}; |
| 96 | grpc_channel_args client_args = {.num_args = GPR_ARRAY_SIZE(client_a), |
| 97 | .args = client_a}; |
| 98 | grpc_channel_args server_args = {.num_args = GPR_ARRAY_SIZE(server_a), |
| 99 | .args = server_a}; |
| 100 | |
| 101 | config.init_client(&f, &client_args); |
| 102 | config.init_server(&f, &server_args); |
| 103 | |
| 104 | grpc_call *c; |
| 105 | grpc_call *s; |
| 106 | gpr_timespec deadline = grpc_timeout_seconds_to_deadline(10); |
| 107 | grpc_op ops[6]; |
| 108 | grpc_op *op; |
| 109 | grpc_metadata_array initial_metadata_recv; |
| 110 | grpc_metadata_array trailing_metadata_recv; |
| 111 | grpc_metadata_array request_metadata_recv; |
| 112 | grpc_call_details call_details; |
| 113 | grpc_status_code status; |
| 114 | grpc_call_error error; |
| 115 | grpc_slice details; |
| 116 | int was_cancelled = 2; |
| 117 | |
| 118 | c = grpc_channel_create_call( |
| 119 | f.client, NULL, GRPC_PROPAGATE_DEFAULTS, f.cq, |
| 120 | grpc_slice_from_static_string("/foo"), |
| 121 | get_host_override_slice("foo.test.google.fr:1234", config), deadline, |
| 122 | NULL); |
| 123 | GPR_ASSERT(c); |
| 124 | |
| 125 | grpc_metadata_array_init(&initial_metadata_recv); |
| 126 | grpc_metadata_array_init(&trailing_metadata_recv); |
| 127 | grpc_metadata_array_init(&request_metadata_recv); |
| 128 | grpc_call_details_init(&call_details); |
| 129 | |
| 130 | memset(ops, 0, sizeof(ops)); |
| 131 | op = ops; |
| 132 | op->op = GRPC_OP_SEND_INITIAL_METADATA; |
| 133 | op->data.send_initial_metadata.count = 0; |
| 134 | op->data.send_initial_metadata.metadata = NULL; |
| 135 | op->flags = 0; |
| 136 | op->reserved = NULL; |
| 137 | op++; |
| 138 | op->op = GRPC_OP_SEND_CLOSE_FROM_CLIENT; |
| 139 | op->flags = 0; |
| 140 | op->reserved = NULL; |
| 141 | op++; |
| 142 | op->op = GRPC_OP_RECV_INITIAL_METADATA; |
| 143 | op->data.recv_initial_metadata.recv_initial_metadata = &initial_metadata_recv; |
| 144 | op->flags = 0; |
| 145 | op->reserved = NULL; |
| 146 | op++; |
| 147 | op->op = GRPC_OP_RECV_STATUS_ON_CLIENT; |
| 148 | op->data.recv_status_on_client.trailing_metadata = &trailing_metadata_recv; |
| 149 | op->data.recv_status_on_client.status = &status; |
| 150 | op->data.recv_status_on_client.status_details = &details; |
| 151 | op->flags = 0; |
| 152 | op->reserved = NULL; |
| 153 | op++; |
| 154 | error = grpc_call_start_batch(c, ops, (size_t)(op - ops), tag(1), NULL); |
| 155 | GPR_ASSERT(GRPC_CALL_OK == error); |
| 156 | |
| 157 | error = |
| 158 | grpc_server_request_call(f.server, &s, &call_details, |
| 159 | &request_metadata_recv, f.cq, f.cq, tag(101)); |
| 160 | GPR_ASSERT(GRPC_CALL_OK == error); |
| 161 | CQ_EXPECT_COMPLETION(cqv, tag(101), 1); |
| 162 | cq_verify(cqv); |
| 163 | |
| 164 | // Send too many pings to the server to trigger the punishment: |
| 165 | // The first ping is sent after data frames, it won't trigger a ping strike. |
| 166 | // Each of the following pings will trigger a ping strike, and we need at |
| 167 | // least (MAX_PING_STRIKES + 1) strikes to trigger the punishment. So |
| 168 | // (MAX_PING_STRIKES + 2) pings are needed here. |
| 169 | int i; |
| 170 | for (i = 200; i < 202 + MAX_PING_STRIKES; i++) { |
| 171 | grpc_channel_ping(f.client, f.cq, tag(i), NULL); |
| 172 | CQ_EXPECT_COMPLETION(cqv, tag(i), 1); |
| 173 | cq_verify(cqv); |
| 174 | } |
| 175 | |
| 176 | memset(ops, 0, sizeof(ops)); |
| 177 | op = ops; |
| 178 | op->op = GRPC_OP_SEND_INITIAL_METADATA; |
| 179 | op->data.send_initial_metadata.count = 0; |
| 180 | op->flags = 0; |
| 181 | op->reserved = NULL; |
| 182 | op++; |
| 183 | op->op = GRPC_OP_SEND_STATUS_FROM_SERVER; |
| 184 | op->data.send_status_from_server.trailing_metadata_count = 0; |
| 185 | op->data.send_status_from_server.status = GRPC_STATUS_UNIMPLEMENTED; |
| 186 | grpc_slice status_details = grpc_slice_from_static_string("xyz"); |
| 187 | op->data.send_status_from_server.status_details = &status_details; |
| 188 | op->flags = 0; |
| 189 | op->reserved = NULL; |
| 190 | op++; |
| 191 | op->op = GRPC_OP_RECV_CLOSE_ON_SERVER; |
| 192 | op->data.recv_close_on_server.cancelled = &was_cancelled; |
| 193 | op->flags = 0; |
| 194 | op->reserved = NULL; |
| 195 | op++; |
| 196 | error = grpc_call_start_batch(s, ops, (size_t)(op - ops), tag(102), NULL); |
| 197 | GPR_ASSERT(GRPC_CALL_OK == error); |
| 198 | |
| 199 | CQ_EXPECT_COMPLETION(cqv, tag(102), 1); |
| 200 | CQ_EXPECT_COMPLETION(cqv, tag(1), 1); |
| 201 | cq_verify(cqv); |
| 202 | |
| 203 | grpc_server_shutdown_and_notify(f.server, f.cq, tag(0xdead)); |
| 204 | CQ_EXPECT_COMPLETION(cqv, tag(0xdead), 1); |
| 205 | cq_verify(cqv); |
| 206 | |
| 207 | grpc_call_destroy(s); |
| 208 | |
| 209 | // The connection should be closed immediately after the misbehaved pings, |
| 210 | // the in-progress RPC should fail. |
| 211 | GPR_ASSERT(status == GRPC_STATUS_UNAVAILABLE); |
| 212 | GPR_ASSERT(0 == grpc_slice_str_cmp(details, "Endpoint read failed")); |
| 213 | GPR_ASSERT(0 == grpc_slice_str_cmp(call_details.method, "/foo")); |
| 214 | validate_host_override_string("foo.test.google.fr:1234", call_details.host, |
| 215 | config); |
| 216 | GPR_ASSERT(was_cancelled == 1); |
| 217 | |
| 218 | grpc_slice_unref(details); |
| 219 | grpc_metadata_array_destroy(&initial_metadata_recv); |
| 220 | grpc_metadata_array_destroy(&trailing_metadata_recv); |
| 221 | grpc_metadata_array_destroy(&request_metadata_recv); |
| 222 | grpc_call_details_destroy(&call_details); |
| 223 | grpc_call_destroy(c); |
| 224 | cq_verifier_destroy(cqv); |
| 225 | end_test(&f); |
| 226 | config.tear_down_data(&f); |
| 227 | } |
| 228 | |
| 229 | void bad_ping(grpc_end2end_test_config config) { |
| 230 | GPR_ASSERT(config.feature_mask & FEATURE_MASK_SUPPORTS_DELAYED_CONNECTION); |
| 231 | test_bad_ping(config); |
| 232 | } |
| 233 | |
| 234 | void bad_ping_pre_init(void) {} |