• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1/*
2 *
3 * Copyright 2016 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#import <XCTest/XCTest.h>
20#import <netinet/in.h>
21#import <sys/socket.h>
22
23#import "../ConfigureCronet.h"
24
25#import <Cronet/Cronet.h>
26#import <grpc/grpc.h>
27#import <grpc/grpc_cronet.h>
28#import "test/core/end2end/cq_verifier.h"
29#import "test/core/util/port.h"
30
31#import <grpc/support/alloc.h>
32#import <grpc/support/log.h>
33
34#import "src/core/lib/channel/channel_args.h"
35#import "src/core/lib/gpr/env.h"
36#import "src/core/lib/gpr/string.h"
37#import "src/core/lib/gpr/tmpfile.h"
38#import "src/core/lib/gprpp/host_port.h"
39#import "test/core/end2end/data/ssl_test_data.h"
40#import "test/core/util/test_config.h"
41
42#import <openssl_grpc/ssl.h>
43
44static void drain_cq(grpc_completion_queue *cq) {
45  grpc_event ev;
46  do {
47    ev = grpc_completion_queue_next(cq, grpc_timeout_seconds_to_deadline(5), NULL);
48  } while (ev.type != GRPC_QUEUE_SHUTDOWN);
49}
50
51@interface CronetUnitTests : XCTestCase
52
53@end
54
55@implementation CronetUnitTests
56
57+ (void)setUp {
58  [super setUp];
59
60  char *argv[] = {(char *)"CoreCronetEnd2EndTests"};
61  grpc_test_init(1, argv);
62
63  grpc_init();
64  configureCronet(/*enable_netlog=*/false);
65  init_ssl();
66}
67
68+ (void)tearDown {
69  grpc_shutdown();
70  cleanup_ssl();
71
72  [super tearDown];
73}
74
75void init_ssl(void) {
76  SSL_load_error_strings();
77  OpenSSL_add_ssl_algorithms();
78}
79
80void cleanup_ssl(void) { EVP_cleanup(); }
81
82int alpn_cb(SSL *ssl, const unsigned char **out, unsigned char *outlen, const unsigned char *in,
83            unsigned int inlen, void *arg) {
84  // Always select "h2" as the ALPN protocol to be used
85  *out = (const unsigned char *)"h2";
86  *outlen = 2;
87  return SSL_TLSEXT_ERR_OK;
88}
89
90void init_ctx(SSL_CTX *ctx) {
91  // Install server certificate
92  BIO *pem = BIO_new_mem_buf((void *)test_server1_cert, (int)strlen(test_server1_cert));
93  X509 *cert = PEM_read_bio_X509_AUX(pem, NULL, NULL, (char *)"");
94  SSL_CTX_use_certificate(ctx, cert);
95  X509_free(cert);
96  BIO_free(pem);
97
98  // Install server private key
99  pem = BIO_new_mem_buf((void *)test_server1_key, (int)strlen(test_server1_key));
100  EVP_PKEY *key = PEM_read_bio_PrivateKey(pem, NULL, NULL, (char *)"");
101  SSL_CTX_use_PrivateKey(ctx, key);
102  EVP_PKEY_free(key);
103  BIO_free(pem);
104
105  // Select cipher suite
106  SSL_CTX_set_cipher_list(ctx, "ECDHE-RSA-AES128-GCM-SHA256");
107
108  // Select ALPN protocol
109  SSL_CTX_set_alpn_select_cb(ctx, alpn_cb, NULL);
110}
111
112unsigned int parse_h2_length(const char *field) {
113  return ((unsigned int)(unsigned char)(field[0])) * 65536 +
114         ((unsigned int)(unsigned char)(field[1])) * 256 +
115         ((unsigned int)(unsigned char)(field[2]));
116}
117
118- (void)testInternalError {
119  grpc_call *c;
120  grpc_slice request_payload_slice = grpc_slice_from_copied_string("hello world");
121  grpc_byte_buffer *request_payload = grpc_raw_byte_buffer_create(&request_payload_slice, 1);
122  gpr_timespec deadline = grpc_timeout_seconds_to_deadline(5);
123  grpc_metadata meta_c[2] = {{grpc_slice_from_static_string("key1"),
124                              grpc_slice_from_static_string("val1"),
125                              0,
126                              {{NULL, NULL, NULL, NULL}}},
127                             {grpc_slice_from_static_string("key2"),
128                              grpc_slice_from_static_string("val2"),
129                              0,
130                              {{NULL, NULL, NULL, NULL}}}};
131
132  int port = grpc_pick_unused_port_or_die();
133  std::string addr = grpc_core::JoinHostPort("127.0.0.1", port);
134  grpc_completion_queue *cq = grpc_completion_queue_create_for_next(NULL);
135  stream_engine *cronetEngine = [Cronet getGlobalEngine];
136  grpc_channel *client = grpc_cronet_secure_channel_create(cronetEngine, addr.c_str(), NULL, NULL);
137
138  cq_verifier *cqv = cq_verifier_create(cq);
139  grpc_op ops[6];
140  grpc_op *op;
141  grpc_metadata_array initial_metadata_recv;
142  grpc_metadata_array trailing_metadata_recv;
143  grpc_metadata_array request_metadata_recv;
144  grpc_byte_buffer *response_payload_recv = NULL;
145  grpc_call_details call_details;
146  grpc_status_code status;
147  grpc_call_error error;
148  grpc_slice details;
149
150  c = grpc_channel_create_call(client, NULL, GRPC_PROPAGATE_DEFAULTS, cq,
151                               grpc_slice_from_static_string("/foo"), NULL, deadline, NULL);
152  GPR_ASSERT(c);
153
154  grpc_metadata_array_init(&initial_metadata_recv);
155  grpc_metadata_array_init(&trailing_metadata_recv);
156  grpc_metadata_array_init(&request_metadata_recv);
157  grpc_call_details_init(&call_details);
158
159  int sl = socket(AF_INET, SOCK_STREAM, 0);
160  GPR_ASSERT(sl >= 0);
161
162  // Make an TCP endpoint to accept the connection
163  struct sockaddr_in s_addr;
164  memset(&s_addr, 0, sizeof(s_addr));
165  s_addr.sin_family = AF_INET;
166  s_addr.sin_addr.s_addr = htonl(INADDR_ANY);
167  s_addr.sin_port = htons(port);
168  GPR_ASSERT(0 == bind(sl, (struct sockaddr *)&s_addr, sizeof(s_addr)));
169  GPR_ASSERT(0 == listen(sl, 5));
170
171  memset(ops, 0, sizeof(ops));
172  op = ops;
173  op->op = GRPC_OP_SEND_INITIAL_METADATA;
174  op->data.send_initial_metadata.count = 2;
175  op->data.send_initial_metadata.metadata = meta_c;
176  op->flags = 0;
177  op->reserved = NULL;
178  op++;
179  op->op = GRPC_OP_SEND_MESSAGE;
180  op->data.send_message.send_message = request_payload;
181  op->flags = 0;
182  op->reserved = NULL;
183  op++;
184  op->op = GRPC_OP_SEND_CLOSE_FROM_CLIENT;
185  op->flags = 0;
186  op->reserved = NULL;
187  op++;
188  op->op = GRPC_OP_RECV_INITIAL_METADATA;
189  op->data.recv_initial_metadata.recv_initial_metadata = &initial_metadata_recv;
190  op->flags = 0;
191  op->reserved = NULL;
192  op++;
193  op->op = GRPC_OP_RECV_MESSAGE;
194  op->data.recv_message.recv_message = &response_payload_recv;
195  op->flags = 0;
196  op->reserved = NULL;
197  op++;
198  op->op = GRPC_OP_RECV_STATUS_ON_CLIENT;
199  op->data.recv_status_on_client.trailing_metadata = &trailing_metadata_recv;
200  op->data.recv_status_on_client.status = &status;
201  op->data.recv_status_on_client.status_details = &details;
202  op->flags = 0;
203  op->reserved = NULL;
204  op++;
205  error = grpc_call_start_batch(c, ops, (size_t)(op - ops), (void *)1, NULL);
206  GPR_ASSERT(GRPC_CALL_OK == error);
207
208  dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
209    int s = accept(sl, NULL, NULL);
210    GPR_ASSERT(s >= 0);
211
212    // Close the connection after 1 second to trigger Cronet's on_failed()
213    sleep(1);
214    close(s);
215    close(sl);
216  });
217
218  CQ_EXPECT_COMPLETION(cqv, (void *)1, 1);
219  cq_verify(cqv);
220
221  GPR_ASSERT(status == GRPC_STATUS_UNAVAILABLE);
222
223  grpc_slice_unref(details);
224  grpc_metadata_array_destroy(&initial_metadata_recv);
225  grpc_metadata_array_destroy(&trailing_metadata_recv);
226  grpc_metadata_array_destroy(&request_metadata_recv);
227  grpc_call_details_destroy(&call_details);
228
229  grpc_call_unref(c);
230
231  cq_verifier_destroy(cqv);
232
233  grpc_byte_buffer_destroy(request_payload);
234  grpc_byte_buffer_destroy(response_payload_recv);
235
236  grpc_channel_destroy(client);
237  grpc_completion_queue_shutdown(cq);
238  drain_cq(cq);
239  grpc_completion_queue_destroy(cq);
240}
241
242- (void)packetCoalescing:(BOOL)useCoalescing {
243  grpc_arg arg;
244  arg.key = (char *)GRPC_ARG_USE_CRONET_PACKET_COALESCING;
245  arg.type = GRPC_ARG_INTEGER;
246  arg.value.integer = useCoalescing ? 1 : 0;
247  grpc_channel_args *args = grpc_channel_args_copy_and_add(NULL, &arg, 1);
248
249  grpc_call *c;
250  grpc_slice request_payload_slice = grpc_slice_from_copied_string("hello world");
251  grpc_byte_buffer *request_payload = grpc_raw_byte_buffer_create(&request_payload_slice, 1);
252  gpr_timespec deadline = grpc_timeout_seconds_to_deadline(5);
253  grpc_metadata meta_c[2] = {{grpc_slice_from_static_string("key1"),
254                              grpc_slice_from_static_string("val1"),
255                              0,
256                              {{NULL, NULL, NULL, NULL}}},
257                             {grpc_slice_from_static_string("key2"),
258                              grpc_slice_from_static_string("val2"),
259                              0,
260                              {{NULL, NULL, NULL, NULL}}}};
261
262  int port = grpc_pick_unused_port_or_die();
263  std::string addr = grpc_core::JoinHostPort("127.0.0.1", port);
264  grpc_completion_queue *cq = grpc_completion_queue_create_for_next(NULL);
265  stream_engine *cronetEngine = [Cronet getGlobalEngine];
266  grpc_channel *client = grpc_cronet_secure_channel_create(cronetEngine, addr.c_str(), args, NULL);
267
268  cq_verifier *cqv = cq_verifier_create(cq);
269  grpc_op ops[6];
270  grpc_op *op;
271  grpc_metadata_array initial_metadata_recv;
272  grpc_metadata_array trailing_metadata_recv;
273  grpc_metadata_array request_metadata_recv;
274  grpc_byte_buffer *response_payload_recv = NULL;
275  grpc_call_details call_details;
276  grpc_status_code status;
277  grpc_call_error error;
278  grpc_slice details;
279
280  c = grpc_channel_create_call(client, NULL, GRPC_PROPAGATE_DEFAULTS, cq,
281                               grpc_slice_from_static_string("/foo"), NULL, deadline, NULL);
282  GPR_ASSERT(c);
283
284  grpc_metadata_array_init(&initial_metadata_recv);
285  grpc_metadata_array_init(&trailing_metadata_recv);
286  grpc_metadata_array_init(&request_metadata_recv);
287  grpc_call_details_init(&call_details);
288
289  __weak XCTestExpectation *expectation = [self expectationWithDescription:@"Coalescing"];
290
291  int sl = socket(AF_INET, SOCK_STREAM, 0);
292  GPR_ASSERT(sl >= 0);
293  struct sockaddr_in s_addr;
294  memset(&s_addr, 0, sizeof(s_addr));
295  s_addr.sin_family = AF_INET;
296  s_addr.sin_addr.s_addr = htonl(INADDR_ANY);
297  s_addr.sin_port = htons(port);
298  GPR_ASSERT(0 == bind(sl, (struct sockaddr *)&s_addr, sizeof(s_addr)));
299  GPR_ASSERT(0 == listen(sl, 5));
300
301  dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
302    int s = accept(sl, NULL, NULL);
303    GPR_ASSERT(s >= 0);
304    struct timeval tv;
305    tv.tv_sec = 2;
306    tv.tv_usec = 0;
307    setsockopt(s, SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof(tv));
308
309    // Make an TLS endpoint to receive Cronet's transmission
310    SSL_CTX *ctx = SSL_CTX_new(TLSv1_2_server_method());
311    init_ctx(ctx);
312    SSL *ssl = SSL_new(ctx);
313    SSL_set_fd(ssl, s);
314    SSL_accept(ssl);
315
316    const char magic[] = "PRI * HTTP/2.0\r\n\r\nSM\r\n\r\n";
317
318    char buf[4096];
319    long len;
320    BOOL coalesced = NO;
321    while ((len = SSL_read(ssl, buf, sizeof(buf))) > 0) {
322      gpr_log(GPR_DEBUG, "Read len: %ld", len);
323
324      // Analyze the HTTP/2 frames in the same TLS PDU to identify if
325      // coalescing is successful
326      unsigned int p = 0;
327      while (p < len) {
328        if (len - p >= 24 && 0 == memcmp(&buf[p], magic, 24)) {
329          p += 24;
330          continue;
331        }
332
333        if (buf[p + 3] == 0 &&                   // Type is DATA
334            parse_h2_length(&buf[p]) == 0x10 &&  // Length is correct
335            (buf[p + 4] & 1) != 0 &&             // EOS bit is set
336            0 == memcmp("hello world", &buf[p + 14],
337                        11)) {  // Message is correct
338          coalesced = YES;
339          break;
340        }
341        p += (parse_h2_length(&buf[p]) + 9);
342      }
343      if (coalesced) {
344        break;
345      }
346    }
347
348    XCTAssert(coalesced == useCoalescing);
349    SSL_free(ssl);
350    SSL_CTX_free(ctx);
351    close(s);
352    close(sl);
353    [expectation fulfill];
354  });
355
356  memset(ops, 0, sizeof(ops));
357  op = ops;
358  op->op = GRPC_OP_SEND_INITIAL_METADATA;
359  op->data.send_initial_metadata.count = 2;
360  op->data.send_initial_metadata.metadata = meta_c;
361  op->flags = 0;
362  op->reserved = NULL;
363  op++;
364  op->op = GRPC_OP_SEND_MESSAGE;
365  op->data.send_message.send_message = request_payload;
366  op->flags = 0;
367  op->reserved = NULL;
368  op++;
369  op->op = GRPC_OP_SEND_CLOSE_FROM_CLIENT;
370  op->flags = 0;
371  op->reserved = NULL;
372  op++;
373  op->op = GRPC_OP_RECV_INITIAL_METADATA;
374  op->data.recv_initial_metadata.recv_initial_metadata = &initial_metadata_recv;
375  op->flags = 0;
376  op->reserved = NULL;
377  op++;
378  op->op = GRPC_OP_RECV_MESSAGE;
379  op->data.recv_message.recv_message = &response_payload_recv;
380  op->flags = 0;
381  op->reserved = NULL;
382  op++;
383  op->op = GRPC_OP_RECV_STATUS_ON_CLIENT;
384  op->data.recv_status_on_client.trailing_metadata = &trailing_metadata_recv;
385  op->data.recv_status_on_client.status = &status;
386  op->data.recv_status_on_client.status_details = &details;
387  op->flags = 0;
388  op->reserved = NULL;
389  op++;
390  error = grpc_call_start_batch(c, ops, (size_t)(op - ops), (void *)1, NULL);
391  GPR_ASSERT(GRPC_CALL_OK == error);
392
393  CQ_EXPECT_COMPLETION(cqv, (void *)1, 1);
394  cq_verify(cqv);
395
396  grpc_slice_unref(details);
397  grpc_metadata_array_destroy(&initial_metadata_recv);
398  grpc_metadata_array_destroy(&trailing_metadata_recv);
399  grpc_metadata_array_destroy(&request_metadata_recv);
400  grpc_call_details_destroy(&call_details);
401
402  grpc_call_unref(c);
403
404  cq_verifier_destroy(cqv);
405
406  grpc_byte_buffer_destroy(request_payload);
407  grpc_byte_buffer_destroy(response_payload_recv);
408
409  grpc_channel_destroy(client);
410  grpc_completion_queue_shutdown(cq);
411  drain_cq(cq);
412  grpc_completion_queue_destroy(cq);
413
414  [self waitForExpectationsWithTimeout:4 handler:nil];
415}
416
417- (void)testPacketCoalescing {
418  [self packetCoalescing:YES];
419  [self packetCoalescing:NO];
420}
421
422@end
423