• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "test/core/end2end/end2end_tests.h"
20 
21 #include <stdio.h>
22 #include <string.h>
23 
24 #include <grpc/byte_buffer.h>
25 #include <grpc/support/alloc.h>
26 #include <grpc/support/log.h>
27 #include <grpc/support/time.h>
28 #include "test/core/end2end/cq_verifier.h"
29 
tag(intptr_t t)30 static void* tag(intptr_t t) { return (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                               0,
96                               {{nullptr, nullptr, nullptr, nullptr}}},
97                              {grpc_slice_from_static_string("key2"),
98                               grpc_slice_from_static_string("val2"),
99                               0,
100                               {{nullptr, nullptr, nullptr, nullptr}}}};
101   grpc_end2end_test_fixture f =
102       begin_test(config, "test_with_authority_header", nullptr, nullptr);
103   cq_verifier* cqv = cq_verifier_create(f.cq);
104   grpc_op ops[6];
105   grpc_op* op;
106   grpc_metadata_array initial_metadata_recv;
107   grpc_metadata_array trailing_metadata_recv;
108   grpc_byte_buffer* response_payload_recv = nullptr;
109   grpc_status_code status;
110   grpc_call_error error;
111   grpc_slice details;
112 
113   grpc_slice host = grpc_slice_from_static_string("foo.test.google.fr");
114   gpr_timespec deadline = five_seconds_from_now();
115   c = grpc_channel_create_call(f.client, nullptr, GRPC_PROPAGATE_DEFAULTS, f.cq,
116                                grpc_slice_from_static_string("/foo"), &host,
117                                deadline, nullptr);
118   GPR_ASSERT(c);
119 
120   grpc_metadata_array_init(&initial_metadata_recv);
121   grpc_metadata_array_init(&trailing_metadata_recv);
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 = 2;
127   op->data.send_initial_metadata.metadata = meta_c;
128   op->flags = 0;
129   op->reserved = nullptr;
130   op++;
131   op->op = GRPC_OP_SEND_MESSAGE;
132   op->data.send_message.send_message = request_payload;
133   op->flags = 0;
134   op->reserved = nullptr;
135   op++;
136   op->op = GRPC_OP_SEND_CLOSE_FROM_CLIENT;
137   op->flags = 0;
138   op->reserved = nullptr;
139   op++;
140   op->op = GRPC_OP_RECV_INITIAL_METADATA;
141   op->data.recv_initial_metadata.recv_initial_metadata = &initial_metadata_recv;
142   op->flags = 0;
143   op->reserved = nullptr;
144   op++;
145   op->op = GRPC_OP_RECV_MESSAGE;
146   op->data.recv_message.recv_message = &response_payload_recv;
147   op->flags = 0;
148   op->reserved = nullptr;
149   op++;
150   op->op = GRPC_OP_RECV_STATUS_ON_CLIENT;
151   op->data.recv_status_on_client.trailing_metadata = &trailing_metadata_recv;
152   op->data.recv_status_on_client.status = &status;
153   op->data.recv_status_on_client.status_details = &details;
154   op->flags = 0;
155   op->reserved = nullptr;
156   op++;
157   error = grpc_call_start_batch(c, ops, static_cast<size_t>(op - ops), tag(1),
158                                 nullptr);
159   GPR_ASSERT(GRPC_CALL_OK == error);
160 
161   CQ_EXPECT_COMPLETION(cqv, tag(1), 1);
162   cq_verify(cqv);
163 
164   GPR_ASSERT(status == GRPC_STATUS_CANCELLED);
165 
166   grpc_slice_unref(details);
167   grpc_metadata_array_destroy(&initial_metadata_recv);
168   grpc_metadata_array_destroy(&trailing_metadata_recv);
169 
170   grpc_call_unref(c);
171 
172   cq_verifier_destroy(cqv);
173 
174   grpc_byte_buffer_destroy(request_payload);
175   grpc_byte_buffer_destroy(response_payload_recv);
176 
177   end_test(&f);
178   config.tear_down_data(&f);
179 }
180 
authority_not_supported(grpc_end2end_test_config config)181 void authority_not_supported(grpc_end2end_test_config config) {
182   if (config.feature_mask & FEATURE_MASK_SUPPORTS_AUTHORITY_HEADER) {
183     return;
184   }
185   test_with_authority_header(config);
186 }
187 
authority_not_supported_pre_init(void)188 void authority_not_supported_pre_init(void) {}
189