diff options
Diffstat (limited to 'src/objective-c/tests/CronetUnitTests/CronetUnitTests.m')
-rw-r--r-- | src/objective-c/tests/CronetUnitTests/CronetUnitTests.m | 179 |
1 files changed, 88 insertions, 91 deletions
diff --git a/src/objective-c/tests/CronetUnitTests/CronetUnitTests.m b/src/objective-c/tests/CronetUnitTests/CronetUnitTests.m index 28414b8e39..a6dfb154a4 100644 --- a/src/objective-c/tests/CronetUnitTests/CronetUnitTests.m +++ b/src/objective-c/tests/CronetUnitTests/CronetUnitTests.m @@ -42,8 +42,7 @@ static void drain_cq(grpc_completion_queue *cq) { grpc_event ev; do { - ev = grpc_completion_queue_next(cq, grpc_timeout_seconds_to_deadline(5), - NULL); + ev = grpc_completion_queue_next(cq, grpc_timeout_seconds_to_deadline(5), NULL); } while (ev.type != GRPC_QUEUE_SHUTDOWN); } @@ -64,9 +63,8 @@ static void drain_cq(grpc_completion_queue *cq) { [Cronet setHttp2Enabled:YES]; [Cronet setSslKeyLogFileName:@"Documents/key"]; [Cronet enableTestCertVerifierForTesting]; - NSURL *url = [[[NSFileManager defaultManager] - URLsForDirectory:NSDocumentDirectory - inDomains:NSUserDomainMask] lastObject]; + NSURL *url = [[[NSFileManager defaultManager] URLsForDirectory:NSDocumentDirectory + inDomains:NSUserDomainMask] lastObject]; NSLog(@"Documents directory: %@", url); [Cronet start]; [Cronet startNetLogToFile:@"Documents/cronet_netlog.json" logBytes:YES]; @@ -88,8 +86,8 @@ void init_ssl(void) { void cleanup_ssl(void) { EVP_cleanup(); } -int alpn_cb(SSL *ssl, const unsigned char **out, unsigned char *outlen, - const unsigned char *in, unsigned int inlen, void *arg) { +int alpn_cb(SSL *ssl, const unsigned char **out, unsigned char *outlen, const unsigned char *in, + unsigned int inlen, void *arg) { // Always select "h2" as the ALPN protocol to be used *out = (const unsigned char *)"h2"; *outlen = 2; @@ -98,16 +96,14 @@ int alpn_cb(SSL *ssl, const unsigned char **out, unsigned char *outlen, void init_ctx(SSL_CTX *ctx) { // Install server certificate - BIO *pem = BIO_new_mem_buf((void *)test_server1_cert, - (int)strlen(test_server1_cert)); + BIO *pem = BIO_new_mem_buf((void *)test_server1_cert, (int)strlen(test_server1_cert)); X509 *cert = PEM_read_bio_X509_AUX(pem, NULL, NULL, (char *)""); SSL_CTX_use_certificate(ctx, cert); X509_free(cert); BIO_free(pem); // Install server private key - pem = - BIO_new_mem_buf((void *)test_server1_key, (int)strlen(test_server1_key)); + pem = BIO_new_mem_buf((void *)test_server1_key, (int)strlen(test_server1_key)); EVP_PKEY *key = PEM_read_bio_PrivateKey(pem, NULL, NULL, (char *)""); SSL_CTX_use_PrivateKey(ctx, key); EVP_PKEY_free(key); @@ -126,12 +122,18 @@ unsigned int parse_h2_length(const char *field) { ((unsigned int)(unsigned char)(field[2])); } +grpc_channel_args *add_disable_client_authority_filter_args(grpc_channel_args *args) { + grpc_arg arg; + arg.key = const_cast<char *>(GRPC_ARG_DISABLE_CLIENT_AUTHORITY_FILTER); + arg.type = GRPC_ARG_INTEGER; + arg.value.integer = 1; + return grpc_channel_args_copy_and_add(args, &arg, 1); +} + - (void)testInternalError { grpc_call *c; - grpc_slice request_payload_slice = - grpc_slice_from_copied_string("hello world"); - grpc_byte_buffer *request_payload = - grpc_raw_byte_buffer_create(&request_payload_slice, 1); + grpc_slice request_payload_slice = grpc_slice_from_copied_string("hello world"); + grpc_byte_buffer *request_payload = grpc_raw_byte_buffer_create(&request_payload_slice, 1); gpr_timespec deadline = grpc_timeout_seconds_to_deadline(5); grpc_metadata meta_c[2] = {{grpc_slice_from_static_string("key1"), grpc_slice_from_static_string("val1"), @@ -147,8 +149,9 @@ unsigned int parse_h2_length(const char *field) { gpr_join_host_port(&addr, "127.0.0.1", port); grpc_completion_queue *cq = grpc_completion_queue_create_for_next(NULL); stream_engine *cronetEngine = [Cronet getGlobalEngine]; - grpc_channel *client = - grpc_cronet_secure_channel_create(cronetEngine, addr, NULL, NULL); + grpc_channel_args *client_args = add_disable_client_authority_filter_args(NULL); + grpc_channel *client = grpc_cronet_secure_channel_create(cronetEngine, addr, client_args, NULL); + grpc_channel_args_destroy(client_args); cq_verifier *cqv = cq_verifier_create(cq); grpc_op ops[6]; @@ -163,8 +166,7 @@ unsigned int parse_h2_length(const char *field) { grpc_slice details; c = grpc_channel_create_call(client, NULL, GRPC_PROPAGATE_DEFAULTS, cq, - grpc_slice_from_static_string("/foo"), NULL, - deadline, NULL); + grpc_slice_from_static_string("/foo"), NULL, deadline, NULL); GPR_ASSERT(c); grpc_metadata_array_init(&initial_metadata_recv); @@ -221,16 +223,15 @@ unsigned int parse_h2_length(const char *field) { error = grpc_call_start_batch(c, ops, (size_t)(op - ops), (void *)1, NULL); GPR_ASSERT(GRPC_CALL_OK == error); - dispatch_async( - dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ - int s = accept(sl, NULL, NULL); - GPR_ASSERT(s >= 0); + dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ + int s = accept(sl, NULL, NULL); + GPR_ASSERT(s >= 0); - // Close the connection after 1 second to trigger Cronet's on_failed() - sleep(1); - close(s); - close(sl); - }); + // Close the connection after 1 second to trigger Cronet's on_failed() + sleep(1); + close(s); + close(sl); + }); CQ_EXPECT_COMPLETION(cqv, (void *)1, 1); cq_verify(cqv); @@ -262,11 +263,11 @@ unsigned int parse_h2_length(const char *field) { arg.type = GRPC_ARG_INTEGER; arg.value.integer = useCoalescing ? 1 : 0; grpc_channel_args *args = grpc_channel_args_copy_and_add(NULL, &arg, 1); + args = add_disable_client_authority_filter_args(args); + grpc_call *c; - grpc_slice request_payload_slice = - grpc_slice_from_copied_string("hello world"); - grpc_byte_buffer *request_payload = - grpc_raw_byte_buffer_create(&request_payload_slice, 1); + grpc_slice request_payload_slice = grpc_slice_from_copied_string("hello world"); + grpc_byte_buffer *request_payload = grpc_raw_byte_buffer_create(&request_payload_slice, 1); gpr_timespec deadline = grpc_timeout_seconds_to_deadline(5); grpc_metadata meta_c[2] = {{grpc_slice_from_static_string("key1"), grpc_slice_from_static_string("val1"), @@ -282,8 +283,7 @@ unsigned int parse_h2_length(const char *field) { gpr_join_host_port(&addr, "127.0.0.1", port); grpc_completion_queue *cq = grpc_completion_queue_create_for_next(NULL); stream_engine *cronetEngine = [Cronet getGlobalEngine]; - grpc_channel *client = - grpc_cronet_secure_channel_create(cronetEngine, addr, args, NULL); + grpc_channel *client = grpc_cronet_secure_channel_create(cronetEngine, addr, args, NULL); cq_verifier *cqv = cq_verifier_create(cq); grpc_op ops[6]; @@ -298,8 +298,7 @@ unsigned int parse_h2_length(const char *field) { grpc_slice details; c = grpc_channel_create_call(client, NULL, GRPC_PROPAGATE_DEFAULTS, cq, - grpc_slice_from_static_string("/foo"), NULL, - deadline, NULL); + grpc_slice_from_static_string("/foo"), NULL, deadline, NULL); GPR_ASSERT(c); grpc_metadata_array_init(&initial_metadata_recv); @@ -307,8 +306,7 @@ unsigned int parse_h2_length(const char *field) { grpc_metadata_array_init(&request_metadata_recv); grpc_call_details_init(&call_details); - __weak XCTestExpectation *expectation = - [self expectationWithDescription:@"Coalescing"]; + __weak XCTestExpectation *expectation = [self expectationWithDescription:@"Coalescing"]; int sl = socket(AF_INET, SOCK_STREAM, 0); GPR_ASSERT(sl >= 0); @@ -320,61 +318,60 @@ unsigned int parse_h2_length(const char *field) { GPR_ASSERT(0 == bind(sl, (struct sockaddr *)&s_addr, sizeof(s_addr))); GPR_ASSERT(0 == listen(sl, 5)); - dispatch_async( - dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ - int s = accept(sl, NULL, NULL); - GPR_ASSERT(s >= 0); - struct timeval tv; - tv.tv_sec = 2; - tv.tv_usec = 0; - setsockopt(s, SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof(tv)); - - // Make an TLS endpoint to receive Cronet's transmission - SSL_CTX *ctx = SSL_CTX_new(TLSv1_2_server_method()); - init_ctx(ctx); - SSL *ssl = SSL_new(ctx); - SSL_set_fd(ssl, s); - SSL_accept(ssl); - - const char magic[] = "PRI * HTTP/2.0\r\n\r\nSM\r\n\r\n"; - - char buf[4096]; - long len; - BOOL coalesced = NO; - while ((len = SSL_read(ssl, buf, sizeof(buf))) > 0) { - gpr_log(GPR_DEBUG, "Read len: %ld", len); - - // Analyze the HTTP/2 frames in the same TLS PDU to identify if - // coalescing is successful - unsigned int p = 0; - while (p < len) { - if (len - p >= 24 && 0 == memcmp(&buf[p], magic, 24)) { - p += 24; - continue; - } - - if (buf[p + 3] == 0 && // Type is DATA - parse_h2_length(&buf[p]) == 0x10 && // Length is correct - (buf[p + 4] & 1) != 0 && // EOS bit is set - 0 == memcmp("hello world", &buf[p + 14], - 11)) { // Message is correct - coalesced = YES; - break; - } - p += (parse_h2_length(&buf[p]) + 9); - } - if (coalesced) { - break; - } + dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ + int s = accept(sl, NULL, NULL); + GPR_ASSERT(s >= 0); + struct timeval tv; + tv.tv_sec = 2; + tv.tv_usec = 0; + setsockopt(s, SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof(tv)); + + // Make an TLS endpoint to receive Cronet's transmission + SSL_CTX *ctx = SSL_CTX_new(TLSv1_2_server_method()); + init_ctx(ctx); + SSL *ssl = SSL_new(ctx); + SSL_set_fd(ssl, s); + SSL_accept(ssl); + + const char magic[] = "PRI * HTTP/2.0\r\n\r\nSM\r\n\r\n"; + + char buf[4096]; + long len; + BOOL coalesced = NO; + while ((len = SSL_read(ssl, buf, sizeof(buf))) > 0) { + gpr_log(GPR_DEBUG, "Read len: %ld", len); + + // Analyze the HTTP/2 frames in the same TLS PDU to identify if + // coalescing is successful + unsigned int p = 0; + while (p < len) { + if (len - p >= 24 && 0 == memcmp(&buf[p], magic, 24)) { + p += 24; + continue; } - XCTAssert(coalesced == useCoalescing); - SSL_free(ssl); - SSL_CTX_free(ctx); - close(s); - close(sl); - [expectation fulfill]; - }); + if (buf[p + 3] == 0 && // Type is DATA + parse_h2_length(&buf[p]) == 0x10 && // Length is correct + (buf[p + 4] & 1) != 0 && // EOS bit is set + 0 == memcmp("hello world", &buf[p + 14], + 11)) { // Message is correct + coalesced = YES; + break; + } + p += (parse_h2_length(&buf[p]) + 9); + } + if (coalesced) { + break; + } + } + + XCTAssert(coalesced == useCoalescing); + SSL_free(ssl); + SSL_CTX_free(ctx); + close(s); + close(sl); + [expectation fulfill]; + }); memset(ops, 0, sizeof(ops)); op = ops; |