3 * Copyright 2015 gRPC authors.
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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
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
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
30 #import <XCTest/XCTest.h>
31 #include "test/core/end2end/end2end_tests.h"
36 #include <grpc/support/alloc.h>
37 #include <grpc/support/log.h>
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"
49 #import <Cronet/Cronet.h>
50 #include <grpc/grpc_cronet.h>
52 #import "../ConfigureCronet.h"
54 typedef struct fullstack_secure_fixture_data {
56 } fullstack_secure_fixture_data;
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));
66 gpr_join_host_port(&ffd->localaddr, "127.0.0.1", port);
69 f.cq = grpc_completion_queue_create_for_next(NULL);
70 f.shutdown_cq = grpc_completion_queue_create_for_pluck(NULL);
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,
78 GPR_ASSERT(state == NULL);
79 cb(user_data, NULL, 0, NULL, 0, GRPC_STATUS_UNAUTHENTICATED, NULL);
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);
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;
95 grpc_server_destroy(f->server);
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);
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);
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];
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);
120 static int fail_server_auth_check(grpc_channel_args *server_args) {
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) {
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);
140 chttp2_init_server_secure_fullstack(f, server_args, ssl_creds);
143 /* All test configurations */
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},
152 static char *roots_filename;
154 @interface CoreCronetEnd2EndTests : XCTestCase
158 @implementation CoreCronetEnd2EndTests
160 // The setUp() function is run before the test cases run and only run once
165 size_t roots_size = strlen(test_root_cert);
167 char *argv[] = {(char *)"CoreCronetEnd2EndTests"};
168 grpc_test_init(1, argv);
169 grpc_end2end_tests_pre_init();
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);
177 GPR_GLOBAL_CONFIG_SET(grpc_default_ssl_roots_file_path, roots_filename);
184 // The tearDown() function is run after all test cases finish running
189 remove(roots_filename);
190 gpr_free(roots_filename);
195 - (void)testIndividualCase:(char *)test_case {
196 char *argv[] = {(char *)"h2_ssl", test_case};
198 for (int i = 0; i < sizeof(configs) / sizeof(*configs); i++) {
199 grpc_end2end_tests(sizeof(argv) / sizeof(argv[0]), argv, configs[i]);
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"];
209 - (void)testBadHostname {
210 [self testIndividualCase:(char *)"bad_hostname"];
213 - (void)testBinaryMetadata {
214 [self testIndividualCase:(char *)"binary_metadata"];
217 - (void)testCallCreds {
219 // [self testIndividualCase:(char *)"call_creds"];
222 - (void)testCancelAfterAccept {
223 [self testIndividualCase:(char *)"cancel_after_accept"];
226 - (void)testCancelAfterClientDone {
227 [self testIndividualCase:(char *)"cancel_after_client_done"];
230 - (void)testCancelAfterInvoke {
231 [self testIndividualCase:(char *)"cancel_after_invoke"];
234 - (void)testCancelAfterRoundTrip {
235 [self testIndividualCase:(char *)"cancel_after_round_trip"];
238 - (void)testCancelBeforeInvoke {
239 [self testIndividualCase:(char *)"cancel_before_invoke"];
242 - (void)testCancelInAVacuum {
243 [self testIndividualCase:(char *)"cancel_in_a_vacuum"];
246 - (void)testCancelWithStatus {
247 [self testIndividualCase:(char *)"cancel_with_status"];
250 - (void)testCompressedPayload {
251 [self testIndividualCase:(char *)"compressed_payload"];
254 - (void)testConnectivity {
256 // [self testIndividualCase:(char *)"connectivity"];
259 - (void)testDefaultHost {
260 [self testIndividualCase:(char *)"default_host"];
263 - (void)testDisappearingServer {
264 [self testIndividualCase:(char *)"disappearing_server"];
267 - (void)testEmptyBatch {
268 [self testIndividualCase:(char *)"empty_batch"];
271 - (void)testFilterCausesClose {
273 // [self testIndividualCase:(char *)"filter_causes_close"];
276 - (void)testGracefulServerShutdown {
277 [self testIndividualCase:(char *)"graceful_server_shutdown"];
280 - (void)testHighInitialSeqno {
281 [self testIndividualCase:(char *)"high_initial_seqno"];
284 - (void)testHpackSize {
286 // [self testIndividualCase:(char *)"hpack_size"];
289 - (void)testIdempotentRequest {
291 // [self testIndividualCase:(char *)"idempotent_request"];
294 - (void)testInvokeLargeRequest {
295 // NOT SUPPORTED (frame size)
296 // [self testIndividualCase:(char *)"invoke_large_request"];
299 - (void)testLargeMetadata {
301 // [self testIndividualCase:(char *)"large_metadata"];
304 - (void)testMaxConcurrentStreams {
305 [self testIndividualCase:(char *)"max_concurrent_streams"];
308 - (void)testMaxMessageLength {
309 // NOT SUPPORTED (close_error)
310 // [self testIndividualCase:(char *)"max_message_length"];
313 - (void)testNegativeDeadline {
314 [self testIndividualCase:(char *)"negative_deadline"];
318 [self testIndividualCase:(char *)"no_op"];
321 - (void)testPayload {
322 [self testIndividualCase:(char *)"payload"];
327 // [self testIndividualCase:(char *)"ping"];
330 - (void)testPingPongStreaming {
331 [self testIndividualCase:(char *)"ping_pong_streaming"];
334 - (void)testRegisteredCall {
335 [self testIndividualCase:(char *)"registered_call"];
338 - (void)testRequestWithFlags {
340 // [self testIndividualCase:(char *)"request_with_flags"];
343 - (void)testRequestWithPayload {
344 [self testIndividualCase:(char *)"request_with_payload"];
347 - (void)testServerFinishesRequest {
348 [self testIndividualCase:(char *)"server_finishes_request"];
351 - (void)testShutdownFinishesCalls {
352 [self testIndividualCase:(char *)"shutdown_finishes_calls"];
355 - (void)testShutdownFinishesTags {
356 [self testIndividualCase:(char *)"shutdown_finishes_tags"];
359 - (void)testSimpleDelayedRequest {
360 [self testIndividualCase:(char *)"simple_delayed_request"];
363 - (void)testSimpleMetadata {
364 [self testIndividualCase:(char *)"simple_metadata"];
367 - (void)testSimpleRequest {
368 [self testIndividualCase:(char *)"simple_request"];
371 - (void)testStreamingErrorResponse {
372 [self testIndividualCase:(char *)"streaming_error_response"];
375 - (void)testTrailingMetadata {
376 [self testIndividualCase:(char *)"trailing_metadata"];