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