• 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 
29 #include "src/core/lib/gpr/useful.h"
30 #include "src/core/lib/transport/byte_stream.h"
31 #include "test/core/end2end/cq_verifier.h"
32 
tag(intptr_t t)33 static void* tag(intptr_t t) { return (void*)t; }
34 
begin_test(grpc_end2end_test_config config,const char * test_name,grpc_channel_args * client_args,grpc_channel_args * server_args)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 
n_seconds_from_now(int n)47 static gpr_timespec n_seconds_from_now(int n) {
48   return grpc_timeout_seconds_to_deadline(n);
49 }
50 
one_second_from_now(void)51 static gpr_timespec one_second_from_now(void) { return n_seconds_from_now(1); }
52 
drain_cq(grpc_completion_queue * cq)53 static void drain_cq(grpc_completion_queue* cq) {
54   grpc_event ev;
55   do {
56     ev = grpc_completion_queue_next(cq, one_second_from_now(), nullptr);
57   } while (ev.type != GRPC_QUEUE_SHUTDOWN);
58 }
59 
shutdown_server(grpc_end2end_test_fixture * f)60 static void shutdown_server(grpc_end2end_test_fixture* f) {
61   if (!f->server) return;
62   grpc_server_shutdown_and_notify(f->server, f->shutdown_cq, tag(1000));
63   GPR_ASSERT(grpc_completion_queue_pluck(f->shutdown_cq, tag(1000),
64                                          grpc_timeout_seconds_to_deadline(5),
65                                          nullptr)
66                  .type == GRPC_OP_COMPLETE);
67   grpc_server_destroy(f->server);
68   f->server = nullptr;
69 }
70 
shutdown_client(grpc_end2end_test_fixture * f)71 static void shutdown_client(grpc_end2end_test_fixture* f) {
72   if (!f->client) return;
73   grpc_channel_destroy(f->client);
74   f->client = nullptr;
75 }
76 
end_test(grpc_end2end_test_fixture * f)77 static void end_test(grpc_end2end_test_fixture* f) {
78   shutdown_server(f);
79   shutdown_client(f);
80 
81   grpc_completion_queue_shutdown(f->cq);
82   drain_cq(f->cq);
83   grpc_completion_queue_destroy(f->cq);
84   grpc_completion_queue_destroy(f->shutdown_cq);
85 }
86 
test_invoke_request_with_flags(grpc_end2end_test_config config,uint32_t * flags_for_op,grpc_call_error call_start_batch_expected_result)87 static void test_invoke_request_with_flags(
88     grpc_end2end_test_config config, uint32_t* flags_for_op,
89     grpc_call_error call_start_batch_expected_result) {
90   grpc_call* c;
91   grpc_slice request_payload_slice =
92       grpc_slice_from_copied_string("hello world");
93   grpc_byte_buffer* request_payload =
94       grpc_raw_byte_buffer_create(&request_payload_slice, 1);
95   grpc_end2end_test_fixture f =
96       begin_test(config, "test_invoke_request_with_flags", nullptr, nullptr);
97   cq_verifier* cqv = cq_verifier_create(f.cq);
98   grpc_op ops[6];
99   grpc_op* op;
100   grpc_metadata_array initial_metadata_recv;
101   grpc_metadata_array trailing_metadata_recv;
102   grpc_metadata_array request_metadata_recv;
103   grpc_byte_buffer* request_payload_recv = nullptr;
104   grpc_call_details call_details;
105   grpc_status_code status;
106   grpc_call_error error;
107   grpc_slice details;
108   grpc_call_error expectation;
109 
110   gpr_timespec deadline = one_second_from_now();
111   c = grpc_channel_create_call(f.client, nullptr, GRPC_PROPAGATE_DEFAULTS, f.cq,
112                                grpc_slice_from_static_string("/foo"), nullptr,
113                                deadline, nullptr);
114   GPR_ASSERT(c);
115 
116   grpc_metadata_array_init(&initial_metadata_recv);
117   grpc_metadata_array_init(&trailing_metadata_recv);
118   grpc_metadata_array_init(&request_metadata_recv);
119   grpc_call_details_init(&call_details);
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 = 0;
125   op->flags = flags_for_op[op->op];
126   op->reserved = nullptr;
127   op++;
128   op->op = GRPC_OP_SEND_MESSAGE;
129   op->data.send_message.send_message = request_payload;
130   op->flags = flags_for_op[op->op];
131   op->reserved = nullptr;
132   op++;
133   op->op = GRPC_OP_SEND_CLOSE_FROM_CLIENT;
134   op->flags = flags_for_op[op->op];
135   op->reserved = nullptr;
136   op++;
137   op->op = GRPC_OP_RECV_INITIAL_METADATA;
138   op->data.recv_initial_metadata.recv_initial_metadata = &initial_metadata_recv;
139   op->flags = flags_for_op[op->op];
140   op->reserved = nullptr;
141   op++;
142   op->op = GRPC_OP_RECV_STATUS_ON_CLIENT;
143   op->data.recv_status_on_client.trailing_metadata = &trailing_metadata_recv;
144   op->data.recv_status_on_client.status = &status;
145   op->data.recv_status_on_client.status_details = &details;
146   op->flags = flags_for_op[op->op];
147   op->reserved = nullptr;
148   op++;
149   expectation = call_start_batch_expected_result;
150   error = grpc_call_start_batch(c, ops, static_cast<size_t>(op - ops), tag(1),
151                                 nullptr);
152   GPR_ASSERT(expectation == error);
153 
154   if (expectation == GRPC_CALL_OK) {
155     CQ_EXPECT_COMPLETION(cqv, tag(1), 1);
156     cq_verify(cqv);
157     grpc_slice_unref(details);
158   }
159 
160   grpc_metadata_array_destroy(&initial_metadata_recv);
161   grpc_metadata_array_destroy(&trailing_metadata_recv);
162   grpc_metadata_array_destroy(&request_metadata_recv);
163   grpc_call_details_destroy(&call_details);
164 
165   grpc_call_unref(c);
166 
167   cq_verifier_destroy(cqv);
168 
169   grpc_byte_buffer_destroy(request_payload);
170   grpc_byte_buffer_destroy(request_payload_recv);
171 
172   end_test(&f);
173   config.tear_down_data(&f);
174 }
175 
request_with_flags(grpc_end2end_test_config config)176 void request_with_flags(grpc_end2end_test_config config) {
177   size_t i;
178   uint32_t flags_for_op[GRPC_OP_RECV_CLOSE_ON_SERVER + 1];
179 
180   {
181     /* check that all grpc_op_types fail when their flag value is set to an
182      * invalid value */
183     int indices[] = {GRPC_OP_SEND_INITIAL_METADATA, GRPC_OP_SEND_MESSAGE,
184                      GRPC_OP_SEND_CLOSE_FROM_CLIENT,
185                      GRPC_OP_RECV_INITIAL_METADATA,
186                      GRPC_OP_RECV_STATUS_ON_CLIENT};
187     for (i = 0; i < GPR_ARRAY_SIZE(indices); ++i) {
188       memset(flags_for_op, 0, sizeof(flags_for_op));
189       flags_for_op[indices[i]] = 0xDEADBEEF;
190       test_invoke_request_with_flags(config, flags_for_op,
191                                      GRPC_CALL_ERROR_INVALID_FLAGS);
192     }
193   }
194   {
195     /* check valid operation with allowed flags for GRPC_OP_SEND_BUFFER */
196     uint32_t flags[] = {GRPC_WRITE_BUFFER_HINT, GRPC_WRITE_NO_COMPRESS,
197                         GRPC_WRITE_INTERNAL_COMPRESS};
198     for (i = 0; i < GPR_ARRAY_SIZE(flags); ++i) {
199       memset(flags_for_op, 0, sizeof(flags_for_op));
200       flags_for_op[GRPC_OP_SEND_MESSAGE] = flags[i];
201       test_invoke_request_with_flags(config, flags_for_op, GRPC_CALL_OK);
202     }
203   }
204 }
205 
request_with_flags_pre_init(void)206 void request_with_flags_pre_init(void) {}
207