LCOV - code coverage report
Current view: top level - test/core/security - secure_endpoint_test.c (source / functions) Hit Total Coverage
Test: tmp.zDYK9MVh93 Lines: 91 91 100.0 %
Date: 2015-10-10 Functions: 8 8 100.0 %

          Line data    Source code
       1             : /*
       2             :  *
       3             :  * Copyright 2015, Google Inc.
       4             :  * All rights reserved.
       5             :  *
       6             :  * Redistribution and use in source and binary forms, with or without
       7             :  * modification, are permitted provided that the following conditions are
       8             :  * met:
       9             :  *
      10             :  *     * Redistributions of source code must retain the above copyright
      11             :  * notice, this list of conditions and the following disclaimer.
      12             :  *     * Redistributions in binary form must reproduce the above
      13             :  * copyright notice, this list of conditions and the following disclaimer
      14             :  * in the documentation and/or other materials provided with the
      15             :  * distribution.
      16             :  *     * Neither the name of Google Inc. nor the names of its
      17             :  * contributors may be used to endorse or promote products derived from
      18             :  * this software without specific prior written permission.
      19             :  *
      20             :  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
      21             :  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
      22             :  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
      23             :  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
      24             :  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
      25             :  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
      26             :  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
      27             :  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
      28             :  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
      29             :  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
      30             :  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
      31             :  *
      32             :  */
      33             : 
      34             : #include "test/core/iomgr/endpoint_tests.h"
      35             : 
      36             : #include <fcntl.h>
      37             : #include <sys/types.h>
      38             : 
      39             : #include "src/core/security/secure_endpoint.h"
      40             : #include "src/core/iomgr/endpoint_pair.h"
      41             : #include "src/core/iomgr/iomgr.h"
      42             : #include <grpc/grpc.h>
      43             : #include <grpc/support/alloc.h>
      44             : #include <grpc/support/log.h>
      45             : #include "test/core/util/test_config.h"
      46             : #include "src/core/tsi/fake_transport_security.h"
      47             : 
      48             : static grpc_pollset g_pollset;
      49             : 
      50          34 : static grpc_endpoint_test_fixture secure_endpoint_create_fixture_tcp_socketpair(
      51             :     size_t slice_size, gpr_slice *leftover_slices, size_t leftover_nslices) {
      52          34 :   grpc_exec_ctx exec_ctx = GRPC_EXEC_CTX_INIT;
      53          34 :   tsi_frame_protector *fake_read_protector = tsi_create_fake_protector(NULL);
      54          34 :   tsi_frame_protector *fake_write_protector = tsi_create_fake_protector(NULL);
      55             :   grpc_endpoint_test_fixture f;
      56             :   grpc_endpoint_pair tcp;
      57             : 
      58          34 :   tcp = grpc_iomgr_create_endpoint_pair("fixture", slice_size);
      59          34 :   grpc_endpoint_add_to_pollset(&exec_ctx, tcp.client, &g_pollset);
      60          34 :   grpc_endpoint_add_to_pollset(&exec_ctx, tcp.server, &g_pollset);
      61             : 
      62          34 :   if (leftover_nslices == 0) {
      63          33 :     f.client_ep =
      64          33 :         grpc_secure_endpoint_create(fake_read_protector, tcp.client, NULL, 0);
      65             :   } else {
      66             :     unsigned i;
      67             :     tsi_result result;
      68             :     size_t still_pending_size;
      69           1 :     size_t total_buffer_size = 8192;
      70           1 :     size_t buffer_size = total_buffer_size;
      71           1 :     gpr_uint8 *encrypted_buffer = gpr_malloc(buffer_size);
      72           1 :     gpr_uint8 *cur = encrypted_buffer;
      73             :     gpr_slice encrypted_leftover;
      74           2 :     for (i = 0; i < leftover_nslices; i++) {
      75           1 :       gpr_slice plain = leftover_slices[i];
      76           1 :       gpr_uint8 *message_bytes = GPR_SLICE_START_PTR(plain);
      77           1 :       size_t message_size = GPR_SLICE_LENGTH(plain);
      78           3 :       while (message_size > 0) {
      79           1 :         size_t protected_buffer_size_to_send = buffer_size;
      80           1 :         size_t processed_message_size = message_size;
      81           1 :         result = tsi_frame_protector_protect(
      82             :             fake_write_protector, message_bytes, &processed_message_size, cur,
      83             :             &protected_buffer_size_to_send);
      84           1 :         GPR_ASSERT(result == TSI_OK);
      85           1 :         message_bytes += processed_message_size;
      86           1 :         message_size -= processed_message_size;
      87           1 :         cur += protected_buffer_size_to_send;
      88           1 :         GPR_ASSERT(buffer_size >= protected_buffer_size_to_send);
      89           1 :         buffer_size -= protected_buffer_size_to_send;
      90             :       }
      91           1 :       gpr_slice_unref(plain);
      92             :     }
      93             :     do {
      94           1 :       size_t protected_buffer_size_to_send = buffer_size;
      95           1 :       result = tsi_frame_protector_protect_flush(fake_write_protector, cur,
      96             :                                                  &protected_buffer_size_to_send,
      97             :                                                  &still_pending_size);
      98           1 :       GPR_ASSERT(result == TSI_OK);
      99           1 :       cur += protected_buffer_size_to_send;
     100           1 :       GPR_ASSERT(buffer_size >= protected_buffer_size_to_send);
     101           1 :       buffer_size -= protected_buffer_size_to_send;
     102           1 :     } while (still_pending_size > 0);
     103           1 :     encrypted_leftover = gpr_slice_from_copied_buffer(
     104             :         (const char *)encrypted_buffer, total_buffer_size - buffer_size);
     105           1 :     f.client_ep = grpc_secure_endpoint_create(fake_read_protector, tcp.client,
     106             :                                               &encrypted_leftover, 1);
     107           1 :     gpr_slice_unref(encrypted_leftover);
     108           1 :     gpr_free(encrypted_buffer);
     109             :   }
     110             : 
     111          34 :   f.server_ep =
     112          34 :       grpc_secure_endpoint_create(fake_write_protector, tcp.server, NULL, 0);
     113          34 :   grpc_exec_ctx_finish(&exec_ctx);
     114          34 :   return f;
     115             : }
     116             : 
     117             : static grpc_endpoint_test_fixture
     118          33 : secure_endpoint_create_fixture_tcp_socketpair_noleftover(size_t slice_size) {
     119          33 :   return secure_endpoint_create_fixture_tcp_socketpair(slice_size, NULL, 0);
     120             : }
     121             : 
     122             : static grpc_endpoint_test_fixture
     123           1 : secure_endpoint_create_fixture_tcp_socketpair_leftover(size_t slice_size) {
     124           1 :   gpr_slice s =
     125             :       gpr_slice_from_copied_string("hello world 12345678900987654321");
     126             :   grpc_endpoint_test_fixture f;
     127             : 
     128           1 :   f = secure_endpoint_create_fixture_tcp_socketpair(slice_size, &s, 1);
     129           1 :   return f;
     130             : }
     131             : 
     132          34 : static void clean_up(void) {}
     133             : 
     134             : static grpc_endpoint_test_config configs[] = {
     135             :     {"secure_ep/tcp_socketpair",
     136             :      secure_endpoint_create_fixture_tcp_socketpair_noleftover, clean_up},
     137             :     {"secure_ep/tcp_socketpair_leftover",
     138             :      secure_endpoint_create_fixture_tcp_socketpair_leftover, clean_up},
     139             : };
     140             : 
     141           1 : static void inc_call_ctr(grpc_exec_ctx *exec_ctx, void *arg, int success) {
     142           1 :   ++*(int *)arg;
     143           1 : }
     144             : 
     145           1 : static void test_leftover(grpc_endpoint_test_config config, size_t slice_size) {
     146           1 :   grpc_endpoint_test_fixture f = config.create_fixture(slice_size);
     147             :   gpr_slice_buffer incoming;
     148           1 :   gpr_slice s =
     149             :       gpr_slice_from_copied_string("hello world 12345678900987654321");
     150           1 :   grpc_exec_ctx exec_ctx = GRPC_EXEC_CTX_INIT;
     151           1 :   int n = 0;
     152             :   grpc_closure done_closure;
     153           1 :   gpr_log(GPR_INFO, "Start test left over");
     154             : 
     155           1 :   gpr_slice_buffer_init(&incoming);
     156           1 :   grpc_closure_init(&done_closure, inc_call_ctr, &n);
     157           1 :   grpc_endpoint_read(&exec_ctx, f.client_ep, &incoming, &done_closure);
     158           1 :   grpc_exec_ctx_finish(&exec_ctx);
     159           1 :   GPR_ASSERT(n == 1);
     160           1 :   GPR_ASSERT(incoming.count == 1);
     161           1 :   GPR_ASSERT(0 == gpr_slice_cmp(s, incoming.slices[0]));
     162             : 
     163           1 :   grpc_endpoint_shutdown(&exec_ctx, f.client_ep);
     164           1 :   grpc_endpoint_shutdown(&exec_ctx, f.server_ep);
     165           1 :   grpc_endpoint_destroy(&exec_ctx, f.client_ep);
     166           1 :   grpc_endpoint_destroy(&exec_ctx, f.server_ep);
     167           1 :   grpc_exec_ctx_finish(&exec_ctx);
     168           1 :   gpr_slice_unref(s);
     169           1 :   gpr_slice_buffer_destroy(&incoming);
     170             : 
     171           1 :   clean_up();
     172           1 : }
     173             : 
     174           1 : static void destroy_pollset(grpc_exec_ctx *exec_ctx, void *p, int success) {
     175           1 :   grpc_pollset_destroy(p);
     176           1 : }
     177             : 
     178           1 : int main(int argc, char **argv) {
     179             :   grpc_closure destroyed;
     180           1 :   grpc_exec_ctx exec_ctx = GRPC_EXEC_CTX_INIT;
     181           1 :   grpc_test_init(argc, argv);
     182             : 
     183           1 :   grpc_init();
     184           1 :   grpc_pollset_init(&g_pollset);
     185           1 :   grpc_endpoint_tests(configs[0], &g_pollset);
     186           1 :   test_leftover(configs[1], 1);
     187           1 :   grpc_closure_init(&destroyed, destroy_pollset, &g_pollset);
     188           1 :   grpc_pollset_shutdown(&exec_ctx, &g_pollset, &destroyed);
     189           1 :   grpc_exec_ctx_finish(&exec_ctx);
     190           1 :   grpc_shutdown();
     191             : 
     192           1 :   return 0;
     193             : }

Generated by: LCOV version 1.10