Craig Tiller | 7a29098 | 2015-05-19 12:49:54 -0700 | [diff] [blame] | 1 | /* |
| 2 | * |
Craig Tiller | 6169d5f | 2016-03-31 07:46:18 -0700 | [diff] [blame] | 3 | * Copyright 2015, Google Inc. |
Craig Tiller | 7a29098 | 2015-05-19 12:49:54 -0700 | [diff] [blame] | 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 <stdio.h> |
| 37 | #include <string.h> |
| 38 | |
Craig Tiller | 7a29098 | 2015-05-19 12:49:54 -0700 | [diff] [blame] | 39 | #include <grpc/byte_buffer.h> |
| 40 | #include <grpc/grpc.h> |
| 41 | #include <grpc/support/alloc.h> |
| 42 | #include <grpc/support/log.h> |
| 43 | #include <grpc/support/time.h> |
| 44 | #include <grpc/support/useful.h> |
Craig Tiller | 9533d04 | 2016-03-25 17:11:06 -0700 | [diff] [blame] | 45 | #include "src/core/lib/support/string.h" |
Craig Tiller | 7a29098 | 2015-05-19 12:49:54 -0700 | [diff] [blame] | 46 | #include "test/core/end2end/cq_verifier.h" |
| 47 | |
Muxi Yan | df1da9f | 2016-10-30 22:10:18 -0700 | [diff] [blame^] | 48 | static const char *authority; |
Muxi Yan | 6d68645 | 2016-09-19 16:48:36 -0700 | [diff] [blame] | 49 | |
Craig Tiller | 7536af0 | 2015-12-22 13:49:30 -0800 | [diff] [blame] | 50 | static void *tag(intptr_t t) { return (void *)t; } |
Craig Tiller | 7a29098 | 2015-05-19 12:49:54 -0700 | [diff] [blame] | 51 | |
| 52 | static grpc_end2end_test_fixture begin_test(grpc_end2end_test_config config, |
| 53 | const char *test_name, |
| 54 | grpc_channel_args *client_args, |
| 55 | grpc_channel_args *server_args) { |
| 56 | grpc_end2end_test_fixture f; |
| 57 | gpr_log(GPR_INFO, "%s/%s", test_name, config.name); |
| 58 | f = config.create_fixture(client_args, server_args); |
Craig Tiller | 7a29098 | 2015-05-19 12:49:54 -0700 | [diff] [blame] | 59 | config.init_server(&f, server_args); |
Craig Tiller | 27e5aa4 | 2015-11-24 16:28:54 -0800 | [diff] [blame] | 60 | config.init_client(&f, client_args); |
Craig Tiller | 7a29098 | 2015-05-19 12:49:54 -0700 | [diff] [blame] | 61 | return f; |
| 62 | } |
| 63 | |
| 64 | static gpr_timespec n_seconds_time(int n) { |
| 65 | return GRPC_TIMEOUT_SECONDS_TO_DEADLINE(n); |
| 66 | } |
| 67 | |
| 68 | static gpr_timespec five_seconds_time(void) { return n_seconds_time(5); } |
| 69 | |
| 70 | static void drain_cq(grpc_completion_queue *cq) { |
| 71 | grpc_event ev; |
| 72 | do { |
Nicolas "Pixel" Noble | 9a123df | 2015-07-29 23:45:08 +0200 | [diff] [blame] | 73 | ev = grpc_completion_queue_next(cq, five_seconds_time(), NULL); |
Craig Tiller | 7a29098 | 2015-05-19 12:49:54 -0700 | [diff] [blame] | 74 | } while (ev.type != GRPC_QUEUE_SHUTDOWN); |
| 75 | } |
| 76 | |
| 77 | static void shutdown_server(grpc_end2end_test_fixture *f) { |
| 78 | if (!f->server) return; |
Craig Tiller | ba2e755 | 2015-05-28 12:53:54 -0700 | [diff] [blame] | 79 | grpc_server_shutdown_and_notify(f->server, f->cq, tag(1000)); |
Craig Tiller | f40df23 | 2016-03-25 13:38:14 -0700 | [diff] [blame] | 80 | GPR_ASSERT(grpc_completion_queue_pluck( |
| 81 | f->cq, tag(1000), GRPC_TIMEOUT_SECONDS_TO_DEADLINE(5), NULL) |
| 82 | .type == GRPC_OP_COMPLETE); |
Craig Tiller | 7a29098 | 2015-05-19 12:49:54 -0700 | [diff] [blame] | 83 | grpc_server_destroy(f->server); |
| 84 | f->server = NULL; |
| 85 | } |
| 86 | |
| 87 | static void shutdown_client(grpc_end2end_test_fixture *f) { |
| 88 | if (!f->client) return; |
| 89 | grpc_channel_destroy(f->client); |
| 90 | f->client = NULL; |
| 91 | } |
| 92 | |
| 93 | static void end_test(grpc_end2end_test_fixture *f) { |
| 94 | shutdown_server(f); |
| 95 | shutdown_client(f); |
| 96 | |
Craig Tiller | dad88a7 | 2015-05-26 17:01:09 -0700 | [diff] [blame] | 97 | grpc_completion_queue_shutdown(f->cq); |
| 98 | drain_cq(f->cq); |
| 99 | grpc_completion_queue_destroy(f->cq); |
Craig Tiller | 7a29098 | 2015-05-19 12:49:54 -0700 | [diff] [blame] | 100 | } |
| 101 | |
| 102 | static void simple_request_body(grpc_end2end_test_fixture f) { |
| 103 | grpc_call *c; |
| 104 | grpc_call *s; |
| 105 | gpr_timespec deadline = five_seconds_time(); |
Craig Tiller | dad88a7 | 2015-05-26 17:01:09 -0700 | [diff] [blame] | 106 | cq_verifier *cqv = cq_verifier_create(f.cq); |
Craig Tiller | 7a29098 | 2015-05-19 12:49:54 -0700 | [diff] [blame] | 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; |
Nicolas "Pixel" Noble | 9a123df | 2015-07-29 23:45:08 +0200 | [diff] [blame] | 114 | grpc_call_error error; |
Craig Tiller | 7a29098 | 2015-05-19 12:49:54 -0700 | [diff] [blame] | 115 | char *details = NULL; |
| 116 | size_t details_capacity = 0; |
| 117 | int was_cancelled = 2; |
| 118 | |
Craig Tiller | e1b0e6e | 2015-07-31 17:07:31 -0700 | [diff] [blame] | 119 | c = grpc_channel_create_call(f.client, NULL, GRPC_PROPAGATE_DEFAULTS, f.cq, |
Muxi Yan | ed2be64 | 2016-10-13 14:04:39 -0700 | [diff] [blame] | 120 | "/foo", authority, deadline, NULL); |
Craig Tiller | 7a29098 | 2015-05-19 12:49:54 -0700 | [diff] [blame] | 121 | GPR_ASSERT(c); |
| 122 | |
| 123 | grpc_metadata_array_init(&initial_metadata_recv); |
| 124 | grpc_metadata_array_init(&trailing_metadata_recv); |
| 125 | grpc_metadata_array_init(&request_metadata_recv); |
| 126 | grpc_call_details_init(&call_details); |
| 127 | |
David Garcia Quintas | a301eaa | 2016-05-06 16:59:03 -0700 | [diff] [blame] | 128 | memset(ops, 0, sizeof(ops)); |
Craig Tiller | 7a29098 | 2015-05-19 12:49:54 -0700 | [diff] [blame] | 129 | op = ops; |
| 130 | op->op = GRPC_OP_SEND_INITIAL_METADATA; |
| 131 | op->data.send_initial_metadata.count = 0; |
David Garcia Quintas | 1d5aca5 | 2015-06-14 14:42:04 -0700 | [diff] [blame] | 132 | op->flags = 0; |
Nicolas "Pixel" Noble | 4960516 | 2015-08-01 00:12:01 +0200 | [diff] [blame] | 133 | op->reserved = NULL; |
Craig Tiller | 7a29098 | 2015-05-19 12:49:54 -0700 | [diff] [blame] | 134 | op++; |
| 135 | op->op = GRPC_OP_RECV_INITIAL_METADATA; |
| 136 | op->data.recv_initial_metadata = &initial_metadata_recv; |
David Garcia Quintas | 1d5aca5 | 2015-06-14 14:42:04 -0700 | [diff] [blame] | 137 | op->flags = 0; |
Nicolas "Pixel" Noble | 4960516 | 2015-08-01 00:12:01 +0200 | [diff] [blame] | 138 | op->reserved = NULL; |
Craig Tiller | 7a29098 | 2015-05-19 12:49:54 -0700 | [diff] [blame] | 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->data.recv_status_on_client.status_details_capacity = &details_capacity; |
David Garcia Quintas | 1d5aca5 | 2015-06-14 14:42:04 -0700 | [diff] [blame] | 145 | op->flags = 0; |
Nicolas "Pixel" Noble | 4960516 | 2015-08-01 00:12:01 +0200 | [diff] [blame] | 146 | op->reserved = NULL; |
Craig Tiller | 7a29098 | 2015-05-19 12:49:54 -0700 | [diff] [blame] | 147 | op++; |
Craig Tiller | 3121fd4 | 2015-09-10 09:56:20 -0700 | [diff] [blame] | 148 | error = grpc_call_start_batch(c, ops, (size_t)(op - ops), tag(1), NULL); |
Nicolas "Pixel" Noble | 9a123df | 2015-07-29 23:45:08 +0200 | [diff] [blame] | 149 | GPR_ASSERT(GRPC_CALL_OK == error); |
Craig Tiller | 7a29098 | 2015-05-19 12:49:54 -0700 | [diff] [blame] | 150 | |
Craig Tiller | d6c98df | 2015-08-18 09:33:44 -0700 | [diff] [blame] | 151 | error = |
| 152 | grpc_server_request_call(f.server, &s, &call_details, |
| 153 | &request_metadata_recv, f.cq, f.cq, tag(101)); |
Nicolas "Pixel" Noble | 9a123df | 2015-07-29 23:45:08 +0200 | [diff] [blame] | 154 | GPR_ASSERT(GRPC_CALL_OK == error); |
Mark D. Roth | 7187ab9 | 2016-08-24 13:49:22 -0700 | [diff] [blame] | 155 | CQ_EXPECT_COMPLETION(cqv, tag(101), 1); |
Craig Tiller | dad88a7 | 2015-05-26 17:01:09 -0700 | [diff] [blame] | 156 | cq_verify(cqv); |
Craig Tiller | 7a29098 | 2015-05-19 12:49:54 -0700 | [diff] [blame] | 157 | |
David Garcia Quintas | a301eaa | 2016-05-06 16:59:03 -0700 | [diff] [blame] | 158 | memset(ops, 0, sizeof(ops)); |
Craig Tiller | 7a29098 | 2015-05-19 12:49:54 -0700 | [diff] [blame] | 159 | op = ops; |
| 160 | op->op = GRPC_OP_SEND_INITIAL_METADATA; |
| 161 | op->data.send_initial_metadata.count = 0; |
David Garcia Quintas | 1d5aca5 | 2015-06-14 14:42:04 -0700 | [diff] [blame] | 162 | op->flags = 0; |
Nicolas "Pixel" Noble | 4960516 | 2015-08-01 00:12:01 +0200 | [diff] [blame] | 163 | op->reserved = NULL; |
Craig Tiller | 7a29098 | 2015-05-19 12:49:54 -0700 | [diff] [blame] | 164 | op++; |
| 165 | op->op = GRPC_OP_SEND_STATUS_FROM_SERVER; |
| 166 | op->data.send_status_from_server.trailing_metadata_count = 0; |
| 167 | op->data.send_status_from_server.status = GRPC_STATUS_UNIMPLEMENTED; |
| 168 | op->data.send_status_from_server.status_details = "xyz"; |
David Garcia Quintas | 1d5aca5 | 2015-06-14 14:42:04 -0700 | [diff] [blame] | 169 | op->flags = 0; |
Nicolas "Pixel" Noble | 4960516 | 2015-08-01 00:12:01 +0200 | [diff] [blame] | 170 | op->reserved = NULL; |
Craig Tiller | 7a29098 | 2015-05-19 12:49:54 -0700 | [diff] [blame] | 171 | op++; |
| 172 | op->op = GRPC_OP_RECV_CLOSE_ON_SERVER; |
| 173 | op->data.recv_close_on_server.cancelled = &was_cancelled; |
David Garcia Quintas | 1d5aca5 | 2015-06-14 14:42:04 -0700 | [diff] [blame] | 174 | op->flags = 0; |
Nicolas "Pixel" Noble | 4960516 | 2015-08-01 00:12:01 +0200 | [diff] [blame] | 175 | op->reserved = NULL; |
Craig Tiller | 7a29098 | 2015-05-19 12:49:54 -0700 | [diff] [blame] | 176 | op++; |
Craig Tiller | 3121fd4 | 2015-09-10 09:56:20 -0700 | [diff] [blame] | 177 | error = grpc_call_start_batch(s, ops, (size_t)(op - ops), tag(102), NULL); |
Nicolas "Pixel" Noble | 9a123df | 2015-07-29 23:45:08 +0200 | [diff] [blame] | 178 | GPR_ASSERT(GRPC_CALL_OK == error); |
Craig Tiller | 7a29098 | 2015-05-19 12:49:54 -0700 | [diff] [blame] | 179 | |
Mark D. Roth | 7187ab9 | 2016-08-24 13:49:22 -0700 | [diff] [blame] | 180 | CQ_EXPECT_COMPLETION(cqv, tag(102), 1); |
| 181 | CQ_EXPECT_COMPLETION(cqv, tag(1), 1); |
Craig Tiller | dad88a7 | 2015-05-26 17:01:09 -0700 | [diff] [blame] | 182 | cq_verify(cqv); |
Craig Tiller | 7a29098 | 2015-05-19 12:49:54 -0700 | [diff] [blame] | 183 | |
| 184 | GPR_ASSERT(status == GRPC_STATUS_UNIMPLEMENTED); |
| 185 | GPR_ASSERT(0 == strcmp(details, "xyz")); |
| 186 | GPR_ASSERT(0 == strcmp(call_details.method, "/foo")); |
Muxi Yan | 6d68645 | 2016-09-19 16:48:36 -0700 | [diff] [blame] | 187 | if (authority) { |
| 188 | GPR_ASSERT(0 == strcmp(call_details.host, authority)); |
| 189 | } |
Craig Tiller | aea081f | 2015-06-11 14:19:33 -0700 | [diff] [blame] | 190 | GPR_ASSERT(was_cancelled == 1); |
Craig Tiller | 7a29098 | 2015-05-19 12:49:54 -0700 | [diff] [blame] | 191 | |
| 192 | gpr_free(details); |
| 193 | grpc_metadata_array_destroy(&initial_metadata_recv); |
| 194 | grpc_metadata_array_destroy(&trailing_metadata_recv); |
| 195 | grpc_metadata_array_destroy(&request_metadata_recv); |
| 196 | grpc_call_details_destroy(&call_details); |
| 197 | |
| 198 | grpc_call_destroy(c); |
| 199 | grpc_call_destroy(s); |
| 200 | |
Craig Tiller | dad88a7 | 2015-05-26 17:01:09 -0700 | [diff] [blame] | 201 | cq_verifier_destroy(cqv); |
Craig Tiller | 7a29098 | 2015-05-19 12:49:54 -0700 | [diff] [blame] | 202 | } |
| 203 | |
| 204 | static void test_invoke_simple_request(grpc_end2end_test_config config) { |
| 205 | grpc_end2end_test_fixture f; |
| 206 | |
Craig Tiller | 3569619 | 2015-05-24 15:00:37 -0700 | [diff] [blame] | 207 | f = begin_test(config, "test_invoke_simple_request", NULL, NULL); |
Craig Tiller | 7a29098 | 2015-05-19 12:49:54 -0700 | [diff] [blame] | 208 | simple_request_body(f); |
| 209 | end_test(&f); |
| 210 | config.tear_down_data(&f); |
| 211 | } |
| 212 | |
Craig Tiller | 0fe5ee7 | 2015-12-22 12:50:36 -0800 | [diff] [blame] | 213 | void server_finishes_request(grpc_end2end_test_config config) { |
Muxi Yan | df1da9f | 2016-10-30 22:10:18 -0700 | [diff] [blame^] | 214 | authority = get_host_override_string("foo.test.google.fr:1234", config); |
Craig Tiller | 7a29098 | 2015-05-19 12:49:54 -0700 | [diff] [blame] | 215 | test_invoke_simple_request(config); |
| 216 | } |
Craig Tiller | 9e9edbc | 2016-04-04 10:38:49 -0700 | [diff] [blame] | 217 | |
| 218 | void server_finishes_request_pre_init(void) {} |