Imported Upstream version 1.21.0
[platform/upstream/grpc.git] / src / objective-c / tests / CoreCronetEnd2EndTests / CoreCronetEnd2EndTests.mm
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/host_port.h"
41 #include "src/core/lib/gpr/string.h"
42 #include "src/core/lib/gpr/tmpfile.h"
43 #include "src/core/lib/security/credentials/credentials.h"
44 #include "src/core/lib/security/security_connector/ssl_utils.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 typedef struct fullstack_secure_fixture_data {
53   char *localaddr;
54 } fullstack_secure_fixture_data;
55
56 static grpc_end2end_test_fixture chttp2_create_fixture_secure_fullstack(
57     grpc_channel_args *client_args, grpc_channel_args *server_args) {
58   grpc_end2end_test_fixture f;
59   int port = grpc_pick_unused_port_or_die();
60   fullstack_secure_fixture_data *ffd =
61       (fullstack_secure_fixture_data *)gpr_malloc(sizeof(fullstack_secure_fixture_data));
62   memset(&f, 0, sizeof(f));
63
64   gpr_join_host_port(&ffd->localaddr, "127.0.0.1", port);
65
66   f.fixture_data = ffd;
67   f.cq = grpc_completion_queue_create_for_next(NULL);
68   f.shutdown_cq = grpc_completion_queue_create_for_pluck(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                                                 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 = grpc_cronet_secure_channel_create(cronetEngine, ffd->localaddr, client_args, NULL);
85   GPR_ASSERT(f->client != NULL);
86 }
87
88 static void chttp2_init_server_secure_fullstack(grpc_end2end_test_fixture *f,
89                                                 grpc_channel_args *server_args,
90                                                 grpc_server_credentials *server_creds) {
91   fullstack_secure_fixture_data *ffd = (fullstack_secure_fixture_data *)f->fixture_data;
92   if (f->server) {
93     grpc_server_destroy(f->server);
94   }
95   f->server = grpc_server_create(server_args, NULL);
96   grpc_server_register_completion_queue(f->server, f->cq, NULL);
97   GPR_ASSERT(grpc_server_add_secure_http2_port(f->server, ffd->localaddr, server_creds));
98   grpc_server_credentials_release(server_creds);
99   grpc_server_start(f->server);
100 }
101
102 static void chttp2_tear_down_secure_fullstack(grpc_end2end_test_fixture *f) {
103   fullstack_secure_fixture_data *ffd = (fullstack_secure_fixture_data *)f->fixture_data;
104   gpr_free(ffd->localaddr);
105   gpr_free(ffd);
106 }
107
108 static void cronet_init_client_simple_ssl_secure_fullstack(grpc_end2end_test_fixture *f,
109                                                            grpc_channel_args *client_args) {
110   grpc_core::ExecCtx exec_ctx;
111   stream_engine *cronetEngine = [Cronet getGlobalEngine];
112
113   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(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                                                            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   grpc_test_init(1, argv);
167   grpc_end2end_tests_pre_init();
168
169   /* Set the SSL roots env var. */
170   roots_file = gpr_tmpfile("chttp2_simple_ssl_fullstack_test", &roots_filename);
171   GPR_ASSERT(roots_filename != NULL);
172   GPR_ASSERT(roots_file != NULL);
173   GPR_ASSERT(fwrite(test_root_cert, 1, roots_size, roots_file) == roots_size);
174   fclose(roots_file);
175   GPR_GLOBAL_CONFIG_SET(grpc_default_ssl_roots_file_path, roots_filename);
176
177   grpc_init();
178
179   [Cronet setHttp2Enabled:YES];
180   [Cronet enableTestCertVerifierForTesting];
181   NSURL *url = [[[NSFileManager defaultManager] URLsForDirectory:NSDocumentDirectory
182                                                        inDomains:NSUserDomainMask] lastObject];
183   NSLog(@"Documents directory: %@", url);
184   [Cronet start];
185   [Cronet startNetLogToFile:@"cronet_netlog.json" logBytes:YES];
186 }
187
188 // The tearDown() function is run after all test cases finish running
189 + (void)tearDown {
190   grpc_shutdown();
191
192   /* Cleanup. */
193   remove(roots_filename);
194   gpr_free(roots_filename);
195
196   [super tearDown];
197 }
198
199 - (void)testIndividualCase:(char *)test_case {
200   char *argv[] = {(char *)"h2_ssl", test_case};
201
202   for (int i = 0; i < sizeof(configs) / sizeof(*configs); i++) {
203     grpc_end2end_tests(sizeof(argv) / sizeof(argv[0]), argv, configs[i]);
204   }
205 }
206
207 // TODO(mxyan): Use NSStringFromSelector(_cmd) to acquire test name from the
208 // test case method name, so that bodies of test cases can stay identical
209 - (void)testAuthorityNotSupported {
210   [self testIndividualCase:(char *)"authority_not_supported"];
211 }
212
213 - (void)testBadHostname {
214   [self testIndividualCase:(char *)"bad_hostname"];
215 }
216
217 - (void)testBinaryMetadata {
218   [self testIndividualCase:(char *)"binary_metadata"];
219 }
220
221 - (void)testCallCreds {
222   // NOT SUPPORTED
223   // [self testIndividualCase:(char *)"call_creds"];
224 }
225
226 - (void)testCancelAfterAccept {
227   [self testIndividualCase:(char *)"cancel_after_accept"];
228 }
229
230 - (void)testCancelAfterClientDone {
231   [self testIndividualCase:(char *)"cancel_after_client_done"];
232 }
233
234 - (void)testCancelAfterInvoke {
235   [self testIndividualCase:(char *)"cancel_after_invoke"];
236 }
237
238 - (void)testCancelAfterRoundTrip {
239   [self testIndividualCase:(char *)"cancel_after_round_trip"];
240 }
241
242 - (void)testCancelBeforeInvoke {
243   [self testIndividualCase:(char *)"cancel_before_invoke"];
244 }
245
246 - (void)testCancelInAVacuum {
247   [self testIndividualCase:(char *)"cancel_in_a_vacuum"];
248 }
249
250 - (void)testCancelWithStatus {
251   [self testIndividualCase:(char *)"cancel_with_status"];
252 }
253
254 - (void)testCompressedPayload {
255   [self testIndividualCase:(char *)"compressed_payload"];
256 }
257
258 - (void)testConnectivity {
259   // NOT SUPPORTED
260   // [self testIndividualCase:(char *)"connectivity"];
261 }
262
263 - (void)testDefaultHost {
264   [self testIndividualCase:(char *)"default_host"];
265 }
266
267 - (void)testDisappearingServer {
268   [self testIndividualCase:(char *)"disappearing_server"];
269 }
270
271 - (void)testEmptyBatch {
272   [self testIndividualCase:(char *)"empty_batch"];
273 }
274
275 - (void)testFilterCausesClose {
276   // NOT SUPPORTED
277   // [self testIndividualCase:(char *)"filter_causes_close"];
278 }
279
280 - (void)testGracefulServerShutdown {
281   [self testIndividualCase:(char *)"graceful_server_shutdown"];
282 }
283
284 - (void)testHighInitialSeqno {
285   [self testIndividualCase:(char *)"high_initial_seqno"];
286 }
287
288 - (void)testHpackSize {
289   // NOT SUPPORTED
290   // [self testIndividualCase:(char *)"hpack_size"];
291 }
292
293 - (void)testIdempotentRequest {
294   // NOT SUPPORTED
295   // [self testIndividualCase:(char *)"idempotent_request"];
296 }
297
298 - (void)testInvokeLargeRequest {
299   // NOT SUPPORTED (frame size)
300   // [self testIndividualCase:(char *)"invoke_large_request"];
301 }
302
303 - (void)testLargeMetadata {
304   // NOT SUPPORTED
305   // [self testIndividualCase:(char *)"large_metadata"];
306 }
307
308 - (void)testMaxConcurrentStreams {
309   [self testIndividualCase:(char *)"max_concurrent_streams"];
310 }
311
312 - (void)testMaxMessageLength {
313   // NOT SUPPORTED (close_error)
314   // [self testIndividualCase:(char *)"max_message_length"];
315 }
316
317 - (void)testNegativeDeadline {
318   [self testIndividualCase:(char *)"negative_deadline"];
319 }
320
321 - (void)testNoOp {
322   [self testIndividualCase:(char *)"no_op"];
323 }
324
325 - (void)testPayload {
326   [self testIndividualCase:(char *)"payload"];
327 }
328
329 - (void)testPing {
330   // NOT SUPPORTED
331   // [self testIndividualCase:(char *)"ping"];
332 }
333
334 - (void)testPingPongStreaming {
335   [self testIndividualCase:(char *)"ping_pong_streaming"];
336 }
337
338 - (void)testRegisteredCall {
339   [self testIndividualCase:(char *)"registered_call"];
340 }
341
342 - (void)testRequestWithFlags {
343   // NOT SUPPORTED
344   // [self testIndividualCase:(char *)"request_with_flags"];
345 }
346
347 - (void)testRequestWithPayload {
348   [self testIndividualCase:(char *)"request_with_payload"];
349 }
350
351 - (void)testServerFinishesRequest {
352   [self testIndividualCase:(char *)"server_finishes_request"];
353 }
354
355 - (void)testShutdownFinishesCalls {
356   [self testIndividualCase:(char *)"shutdown_finishes_calls"];
357 }
358
359 - (void)testShutdownFinishesTags {
360   [self testIndividualCase:(char *)"shutdown_finishes_tags"];
361 }
362
363 - (void)testSimpleDelayedRequest {
364   [self testIndividualCase:(char *)"simple_delayed_request"];
365 }
366
367 - (void)testSimpleMetadata {
368   [self testIndividualCase:(char *)"simple_metadata"];
369 }
370
371 - (void)testSimpleRequest {
372   [self testIndividualCase:(char *)"simple_request"];
373 }
374
375 - (void)testStreamingErrorResponse {
376   [self testIndividualCase:(char *)"streaming_error_response"];
377 }
378
379 - (void)testTrailingMetadata {
380   [self testIndividualCase:(char *)"trailing_metadata"];
381 }
382
383 @end