Debian Codegraph @

test/core/end2end/tests/authority_not_supported.cc

raw
1 /*
2 *
3 * Copyright 2015 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 <stdint.h>
20 #include <string.h>
21
22 #include <grpc/byte_buffer.h>
23 #include <grpc/grpc.h>
24 #include <grpc/impl/codegen/propagation_bits.h>
25 #include <grpc/slice.h>
26 #include <grpc/status.h>
27 #include <grpc/support/log.h>
28
29 #include "test/core/end2end/cq_verifier.h"
30 #include "test/core/end2end/end2end_tests.h"
31 #include "test/core/util/test_config.h"
32
33 static void* tag(intptr_t t) { return reinterpret_cast<void*>(t); }
34
35 static grpc_end2end_test_fixture begin_test(grpc_end2end_test_config config,
36 const char* test_name,
37 grpc_channel_args* client_args,
38 grpc_channel_args* server_args) {
39 grpc_end2end_test_fixture f;
40 gpr_log(GPR_INFO, "Running test: %s/%s", test_name, config.name);
41 f = config.create_fixture(client_args, server_args);
42 config.init_server(&f, server_args);
43 config.init_client(&f, client_args);
44 return f;
45 }
46
47 static gpr_timespec n_seconds_from_now(int n) {
48 return grpc_timeout_seconds_to_deadline(n);
49 }
50
51 static gpr_timespec five_seconds_from_now(void) {
52 return n_seconds_from_now(5);
53 }
54
55 static void drain_cq(grpc_completion_queue* cq) {
56 grpc_event ev;
57 do {
58 ev = grpc_completion_queue_next(cq, five_seconds_from_now(), nullptr);
59 } while (ev.type != GRPC_QUEUE_SHUTDOWN);
60 }
61
62 static void shutdown_server(grpc_end2end_test_fixture* f) {
63 if (!f->server) return;
64 grpc_server_shutdown_and_notify(f->server, f->cq, tag(1000));
65 grpc_event ev;
66 do {
67 ev = grpc_completion_queue_next(f->cq, grpc_timeout_seconds_to_deadline(5),
68 nullptr);
69 } while (ev.type != GRPC_OP_COMPLETE || ev.tag != tag(1000));
70 grpc_server_destroy(f->server);
71 f->server = nullptr;
72 }
73
74 static void shutdown_client(grpc_end2end_test_fixture* f) {
75 if (!f->client) return;
76 grpc_channel_destroy(f->client);
77 f->client = nullptr;
78 }
79
80 static void end_test(grpc_end2end_test_fixture* f) {
81 shutdown_server(f);
82 shutdown_client(f);
83
84 grpc_completion_queue_shutdown(f->cq);
85 drain_cq(f->cq);
86 grpc_completion_queue_destroy(f->cq);
87 }
88
89 /* Request/response with metadata and payload.*/
90 static void test_with_authority_header(grpc_end2end_test_config config) {
91 grpc_call* c;
92 grpc_slice request_payload_slice =
93 grpc_slice_from_copied_string("hello world");
94 grpc_byte_buffer* request_payload =
95 grpc_raw_byte_buffer_create(&request_payload_slice, 1);
96 grpc_metadata meta_c[2] = {{grpc_slice_from_static_string("key1"),
97 grpc_slice_from_static_string("val1"),
98 {{nullptr, nullptr, nullptr, nullptr}}},
99 {grpc_slice_from_static_string("key2"),
100 grpc_slice_from_static_string("val2"),
101 {{nullptr, nullptr, nullptr, nullptr}}}};
102 grpc_end2end_test_fixture f =
103 begin_test(config, "test_with_authority_header", nullptr, nullptr);
104 grpc_core::CqVerifier cqv(f.cq);
105 grpc_op ops[6];
106 grpc_op* op;
107 grpc_metadata_array initial_metadata_recv;
108 grpc_metadata_array trailing_metadata_recv;
109 grpc_byte_buffer* response_payload_recv = nullptr;
110 grpc_status_code status;
111 grpc_call_error error;
112 grpc_slice details;
113
114 grpc_slice host = grpc_slice_from_static_string("foo.test.google.fr");
115 gpr_timespec deadline = five_seconds_from_now();
116 c = grpc_channel_create_call(f.client, nullptr, GRPC_PROPAGATE_DEFAULTS, f.cq,
117 grpc_slice_from_static_string("/foo"), &host,
118 deadline, nullptr);
119 GPR_ASSERT(c);
120
121 grpc_metadata_array_init(&initial_metadata_recv);
122 grpc_metadata_array_init(&trailing_metadata_recv);
123
124 memset(ops, 0, sizeof(ops));
125 op = ops;
126 op->op = GRPC_OP_SEND_INITIAL_METADATA;
127 op->data.send_initial_metadata.count = 2;
128 op->data.send_initial_metadata.metadata = meta_c;
129 op->flags = 0;
130 op->reserved = nullptr;
131 op++;
132 op->op = GRPC_OP_SEND_MESSAGE;
133 op->data.send_message.send_message = request_payload;
134 op->flags = 0;
135 op->reserved = nullptr;
136 op++;
137 op->op = GRPC_OP_SEND_CLOSE_FROM_CLIENT;
138 op->flags = 0;
139 op->reserved = nullptr;
140 op++;
141 op->op = GRPC_OP_RECV_INITIAL_METADATA;
142 op->data.recv_initial_metadata.recv_initial_metadata = &initial_metadata_recv;
143 op->flags = 0;
144 op->reserved = nullptr;
145 op++;
146 op->op = GRPC_OP_RECV_MESSAGE;
147 op->data.recv_message.recv_message = &response_payload_recv;
148 op->flags = 0;
149 op->reserved = nullptr;
150 op++;
151 op->op = GRPC_OP_RECV_STATUS_ON_CLIENT;
152 op->data.recv_status_on_client.trailing_metadata = &trailing_metadata_recv;
153 op->data.recv_status_on_client.status = &status;
154 op->data.recv_status_on_client.status_details = &details;
155 op->flags = 0;
156 op->reserved = nullptr;
157 op++;
158 error = grpc_call_start_batch(c, ops, static_cast<size_t>(op - ops), tag(1),
159 nullptr);
160 GPR_ASSERT(GRPC_CALL_OK == error);
161
162 cqv.Expect(tag(1), true);
163 cqv.Verify();
164
165 GPR_ASSERT(status == GRPC_STATUS_CANCELLED);
166
167 grpc_slice_unref(details);
168 grpc_metadata_array_destroy(&initial_metadata_recv);
169 grpc_metadata_array_destroy(&trailing_metadata_recv);
170
171 grpc_call_unref(c);
172
173 grpc_byte_buffer_destroy(request_payload);
174 grpc_byte_buffer_destroy(response_payload_recv);
175
176 end_test(&f);
177 config.tear_down_data(&f);
178 }
179
180 void authority_not_supported(grpc_end2end_test_config config) {
181 if (config.feature_mask & FEATURE_MASK_SUPPORTS_AUTHORITY_HEADER) {
182 return;
183 }
184 test_with_authority_header(config);
185 }
186
187 void authority_not_supported_pre_init(void) {}