Debian Codegraph @

src/objective-c/tests/CronetTests/CoreCronetEnd2EndTests.mm

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 /*
20 * This test file is derived from fixture h2_ssl.c in core end2end test
21 * (test/core/end2end/fixture/h2_ssl.c). The structure of the fixture is
22 * preserved as much as possible
23 *
24 * This fixture creates a server full stack using chttp2 and a client
25 * full stack using Cronet. End-to-end tests are run against this
26 * configuration
27 *
28 */
29
30 #import <XCTest/XCTest.h>
31 #include "test/core/end2end/end2end_tests.h"
32
33 #include <stdio.h>
34 #include <string.h>
35
36 #include <grpc/support/alloc.h>
37 #include <grpc/support/log.h>
38
39 #include "src/core/lib/channel/channel_args.h"
40 #include "src/core/lib/gpr/string.h"
41 #include "src/core/lib/gpr/tmpfile.h"
42 #include "src/core/lib/gprpp/host_port.h"
43 #include "src/core/lib/security/credentials/credentials.h"
44 #include "src/core/lib/security/security_connector/ssl_utils_config.h"
45 #include "test/core/end2end/data/ssl_test_data.h"
46 #include "test/core/util/port.h"
47 #include "test/core/util/test_config.h"
48
49 #import <Cronet/Cronet.h>
50 #include <grpc/grpc_cronet.h>
51
52 #import "../ConfigureCronet.h"
53
54 struct fullstack_secure_fixture_data {
55 std::string localaddr;
56 };
57
58 static grpc_end2end_test_fixture chttp2_create_fixture_secure_fullstack(
59 const grpc_channel_args *client_args, const grpc_channel_args *server_args) {
60 grpc_end2end_test_fixture f;
61 int port = grpc_pick_unused_port_or_die();
62 fullstack_secure_fixture_data *ffd = new fullstack_secure_fixture_data();
63 memset(&f, 0, sizeof(f));
64
65 ffd->localaddr = grpc_core::JoinHostPort("127.0.0.1", port);
66
67 f.fixture_data = ffd;
68 f.cq = grpc_completion_queue_create_for_next(NULL);
69
70 return f;
71 }
72
73 static void process_auth_failure(void *state, grpc_auth_context *ctx, const grpc_metadata *md,
74 size_t md_count, grpc_process_auth_metadata_done_cb cb,
75 void *user_data) {
76 GPR_ASSERT(state == NULL);
77 cb(user_data, NULL, 0, NULL, 0, GRPC_STATUS_UNAUTHENTICATED, NULL);
78 }
79
80 static void cronet_init_client_secure_fullstack(grpc_end2end_test_fixture *f,
81 const grpc_channel_args *client_args,
82 stream_engine *cronetEngine) {
83 fullstack_secure_fixture_data *ffd = (fullstack_secure_fixture_data *)f->fixture_data;
84 f->client =
85 grpc_cronet_secure_channel_create(cronetEngine, ffd->localaddr.c_str(), client_args, NULL);
86 GPR_ASSERT(f->client != NULL);
87 }
88
89 static void chttp2_init_server_secure_fullstack(grpc_end2end_test_fixture *f,
90 const grpc_channel_args *server_args,
91 grpc_server_credentials *server_creds) {
92 fullstack_secure_fixture_data *ffd = (fullstack_secure_fixture_data *)f->fixture_data;
93 if (f->server) {
94 grpc_server_destroy(f->server);
95 }
96 f->server = grpc_server_create(server_args, NULL);
97 grpc_server_register_completion_queue(f->server, f->cq, NULL);
98 GPR_ASSERT(grpc_server_add_http2_port(f->server, ffd->localaddr.c_str(), server_creds));
99 grpc_server_credentials_release(server_creds);
100 grpc_server_start(f->server);
101 }
102
103 static void chttp2_tear_down_secure_fullstack(grpc_end2end_test_fixture *f) {
104 fullstack_secure_fixture_data *ffd = (fullstack_secure_fixture_data *)f->fixture_data;
105 delete ffd;
106 }
107
108 static void cronet_init_client_simple_ssl_secure_fullstack(grpc_end2end_test_fixture *f,
109 const grpc_channel_args *client_args) {
110 grpc_core::ExecCtx exec_ctx;
111 stream_engine *cronetEngine = [Cronet getGlobalEngine];
112
113 const grpc_channel_args *new_client_args = grpc_channel_args_copy(client_args);
114 cronet_init_client_secure_fullstack(f, new_client_args, cronetEngine);
115 grpc_channel_args_destroy(new_client_args);
116 }
117
118 static int fail_server_auth_check(const grpc_channel_args *server_args) {
119 size_t i;
120 if (server_args == NULL) return 0;
121 for (i = 0; i < server_args->num_args; i++) {
122 if (strcmp(server_args->args[i].key, FAIL_AUTH_CHECK_SERVER_ARG_NAME) == 0) {
123 return 1;
124 }
125 }
126 return 0;
127 }
128
129 static void chttp2_init_server_simple_ssl_secure_fullstack(grpc_end2end_test_fixture *f,
130 const grpc_channel_args *server_args) {
131 grpc_ssl_pem_key_cert_pair pem_cert_key_pair = {test_server1_key, test_server1_cert};
132 grpc_server_credentials *ssl_creds =
133 grpc_ssl_server_credentials_create(NULL, &pem_cert_key_pair, 1, 0, NULL);
134 if (fail_server_auth_check(server_args)) {
135 grpc_auth_metadata_processor processor = {process_auth_failure, NULL, NULL};
136 grpc_server_credentials_set_auth_metadata_processor(ssl_creds, processor);
137 }
138 chttp2_init_server_secure_fullstack(f, server_args, ssl_creds);
139 }
140
141 /* All test configurations */
142
143 static grpc_end2end_test_config configs[] = {
144 {"chttp2/simple_ssl_fullstack",
145 FEATURE_MASK_SUPPORTS_DELAYED_CONNECTION | FEATURE_MASK_SUPPORTS_PER_CALL_CREDENTIALS, nullptr,
146 chttp2_create_fixture_secure_fullstack, cronet_init_client_simple_ssl_secure_fullstack,
147 chttp2_init_server_simple_ssl_secure_fullstack, chttp2_tear_down_secure_fullstack},
148 };
149
150 static char *roots_filename;
151
152 @interface CoreCronetEnd2EndTests : XCTestCase
153
154 @end
155
156 @implementation CoreCronetEnd2EndTests
157
158 // The setUp() function is run before the test cases run and only run once
159 + (void)setUp {
160 [super setUp];
161
162 FILE *roots_file;
163 size_t roots_size = strlen(test_root_cert);
164
165 char *argv[] = {(char *)"CoreCronetEnd2EndTests"};
166 int argc = 1;
167 grpc_test_init(&argc, argv);
168 grpc_end2end_tests_pre_init();
169
170 /* Set the SSL roots env var. */
171 roots_file = gpr_tmpfile("chttp2_simple_ssl_fullstack_test", &roots_filename);
172 GPR_ASSERT(roots_filename != NULL);
173 GPR_ASSERT(roots_file != NULL);
174 GPR_ASSERT(fwrite(test_root_cert, 1, roots_size, roots_file) == roots_size);
175 fclose(roots_file);
176 GPR_GLOBAL_CONFIG_SET(grpc_default_ssl_roots_file_path, roots_filename);
177
178 grpc_init();
179
180 configureCronet(/*enable_netlog=*/false);
181 }
182
183 // The tearDown() function is run after all test cases finish running
184 + (void)tearDown {
185 grpc_shutdown();
186
187 /* Cleanup. */
188 remove(roots_filename);
189 gpr_free(roots_filename);
190
191 [super tearDown];
192 }
193
194 - (void)testIndividualCase:(char *)test_case {
195 char *argv[] = {(char *)"h2_ssl", test_case};
196 for (int i = 0; i < sizeof(configs) / sizeof(*configs); i++) {
197 grpc_end2end_tests(2, argv, configs[i]);
198 }
199 }
200
201 // TODO(mxyan): Use NSStringFromSelector(_cmd) to acquire test name from the
202 // test case method name, so that bodies of test cases can stay identical
203 - (void)testAuthorityNotSupported {
204 [self testIndividualCase:(char *)"authority_not_supported"];
205 }
206
207 - (void)testBadHostname {
208 [self testIndividualCase:(char *)"bad_hostname"];
209 }
210
211 - (void)testBinaryMetadata {
212 [self testIndividualCase:(char *)"binary_metadata"];
213 }
214
215 - (void)testCallCreds {
216 // NOT SUPPORTED
217 // [self testIndividualCase:(char *)"call_creds"];
218 }
219
220 - (void)testCancelAfterAccept {
221 [self testIndividualCase:(char *)"cancel_after_accept"];
222 }
223
224 - (void)testCancelAfterClientDone {
225 [self testIndividualCase:(char *)"cancel_after_client_done"];
226 }
227
228 - (void)testCancelAfterInvoke {
229 [self testIndividualCase:(char *)"cancel_after_invoke"];
230 }
231
232 - (void)testCancelAfterRoundTrip {
233 [self testIndividualCase:(char *)"cancel_after_round_trip"];
234 }
235
236 - (void)testCancelBeforeInvoke {
237 [self testIndividualCase:(char *)"cancel_before_invoke"];
238 }
239
240 - (void)testCancelInAVacuum {
241 [self testIndividualCase:(char *)"cancel_in_a_vacuum"];
242 }
243
244 - (void)testCancelWithStatus {
245 [self testIndividualCase:(char *)"cancel_with_status"];
246 }
247
248 - (void)testCompressedPayload {
249 [self testIndividualCase:(char *)"compressed_payload"];
250 }
251
252 - (void)testConnectivity {
253 // NOT SUPPORTED
254 // [self testIndividualCase:(char *)"connectivity"];
255 }
256
257 - (void)testDefaultHost {
258 [self testIndividualCase:(char *)"default_host"];
259 }
260
261 - (void)testDisappearingServer {
262 [self testIndividualCase:(char *)"disappearing_server"];
263 }
264
265 - (void)testEmptyBatch {
266 [self testIndividualCase:(char *)"empty_batch"];
267 }
268
269 - (void)testFilterCausesClose {
270 // NOT SUPPORTED
271 // [self testIndividualCase:(char *)"filter_causes_close"];
272 }
273
274 - (void)testGracefulServerShutdown {
275 [self testIndividualCase:(char *)"graceful_server_shutdown"];
276 }
277
278 - (void)testHighInitialSeqno {
279 [self testIndividualCase:(char *)"high_initial_seqno"];
280 }
281
282 - (void)testHpackSize {
283 // NOT SUPPORTED
284 // [self testIndividualCase:(char *)"hpack_size"];
285 }
286
287 - (void)testIdempotentRequest {
288 // NOT SUPPORTED
289 // [self testIndividualCase:(char *)"idempotent_request"];
290 }
291
292 - (void)testInvokeLargeRequest {
293 // NOT SUPPORTED (frame size)
294 // [self testIndividualCase:(char *)"invoke_large_request"];
295 }
296
297 - (void)testLargeMetadata {
298 // NOT SUPPORTED
299 // [self testIndividualCase:(char *)"large_metadata"];
300 }
301
302 - (void)testMaxConcurrentStreams {
303 [self testIndividualCase:(char *)"max_concurrent_streams"];
304 }
305
306 - (void)testMaxMessageLength {
307 // NOT SUPPORTED (close_error)
308 // [self testIndividualCase:(char *)"max_message_length"];
309 }
310
311 - (void)testNegativeDeadline {
312 [self testIndividualCase:(char *)"negative_deadline"];
313 }
314
315 - (void)testNoOp {
316 [self testIndividualCase:(char *)"no_op"];
317 }
318
319 - (void)testPayload {
320 [self testIndividualCase:(char *)"payload"];
321 }
322
323 - (void)testPing {
324 // NOT SUPPORTED
325 // [self testIndividualCase:(char *)"ping"];
326 }
327
328 - (void)testPingPongStreaming {
329 [self testIndividualCase:(char *)"ping_pong_streaming"];
330 }
331
332 - (void)testRegisteredCall {
333 [self testIndividualCase:(char *)"registered_call"];
334 }
335
336 - (void)testRequestWithFlags {
337 // NOT SUPPORTED
338 // [self testIndividualCase:(char *)"request_with_flags"];
339 }
340
341 - (void)testRequestWithPayload {
342 [self testIndividualCase:(char *)"request_with_payload"];
343 }
344
345 - (void)testServerFinishesRequest {
346 [self testIndividualCase:(char *)"server_finishes_request"];
347 }
348
349 - (void)testServerStreaming {
350 [self testIndividualCase:(char *)"server_streaming"];
351 }
352
353 - (void)testShutdownFinishesCalls {
354 [self testIndividualCase:(char *)"shutdown_finishes_calls"];
355 }
356
357 - (void)testShutdownFinishesTags {
358 [self testIndividualCase:(char *)"shutdown_finishes_tags"];
359 }
360
361 - (void)testSimpleDelayedRequest {
362 [self testIndividualCase:(char *)"simple_delayed_request"];
363 }
364
365 - (void)testSimpleMetadata {
366 [self testIndividualCase:(char *)"simple_metadata"];
367 }
368
369 - (void)testSimpleRequest {
370 [self testIndividualCase:(char *)"simple_request"];
371 }
372
373 - (void)testStreamingErrorResponse {
374 [self testIndividualCase:(char *)"streaming_error_response"];
375 }
376
377 - (void)testTrailingMetadata {
378 [self testIndividualCase:(char *)"trailing_metadata"];
379 }
380
381 @end