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