LCOV - code coverage report
Current view: top level - test/core/channel - channel_args_test.c (source / functions) Hit Total Coverage
Test: tmp.zDYK9MVh93 Lines: 56 56 100.0 %
Date: 2015-10-10 Functions: 4 4 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 <string.h>
      35             : 
      36             : #include <grpc/support/log.h>
      37             : #include <grpc/support/useful.h>
      38             : 
      39             : #include "src/core/channel/channel_args.h"
      40             : 
      41             : #include "test/core/util/test_config.h"
      42             : 
      43           1 : static void test_create(void) {
      44             :   grpc_arg arg_int;
      45             :   grpc_arg arg_string;
      46             :   grpc_arg to_add[2];
      47             :   grpc_channel_args *ch_args;
      48             : 
      49           1 :   arg_int.key = "int_arg";
      50           1 :   arg_int.type = GRPC_ARG_INTEGER;
      51           1 :   arg_int.value.integer = 123;
      52             : 
      53           1 :   arg_string.key = "str key";
      54           1 :   arg_string.type = GRPC_ARG_STRING;
      55           1 :   arg_string.value.string = "str value";
      56             : 
      57           1 :   to_add[0] = arg_int;
      58           1 :   to_add[1] = arg_string;
      59           1 :   ch_args = grpc_channel_args_copy_and_add(NULL, to_add, 2);
      60             : 
      61           1 :   GPR_ASSERT(ch_args->num_args == 2);
      62           1 :   GPR_ASSERT(strcmp(ch_args->args[0].key, arg_int.key) == 0);
      63           1 :   GPR_ASSERT(ch_args->args[0].type == arg_int.type);
      64           1 :   GPR_ASSERT(ch_args->args[0].value.integer == arg_int.value.integer);
      65             : 
      66           1 :   GPR_ASSERT(strcmp(ch_args->args[1].key, arg_string.key) == 0);
      67           1 :   GPR_ASSERT(ch_args->args[1].type == arg_string.type);
      68           1 :   GPR_ASSERT(strcmp(ch_args->args[1].value.string, arg_string.value.string) ==
      69             :              0);
      70             : 
      71           1 :   grpc_channel_args_destroy(ch_args);
      72           1 : }
      73             : 
      74           1 : static void test_set_compression_algorithm(void) {
      75             :   grpc_channel_args *ch_args;
      76             : 
      77           1 :   ch_args =
      78             :       grpc_channel_args_set_compression_algorithm(NULL, GRPC_COMPRESS_GZIP);
      79           1 :   GPR_ASSERT(ch_args->num_args == 1);
      80           1 :   GPR_ASSERT(strcmp(ch_args->args[0].key, GRPC_COMPRESSION_ALGORITHM_ARG) == 0);
      81           1 :   GPR_ASSERT(ch_args->args[0].type == GRPC_ARG_INTEGER);
      82             : 
      83           1 :   grpc_channel_args_destroy(ch_args);
      84           1 : }
      85             : 
      86           1 : static void test_compression_algorithm_states(void) {
      87             :   grpc_channel_args *ch_args, *ch_args_wo_gzip, *ch_args_wo_gzip_deflate;
      88             :   unsigned states_bitset;
      89             :   size_t i;
      90             : 
      91           1 :   ch_args = grpc_channel_args_copy_and_add(NULL, NULL, 0);
      92             :   /* by default, all enabled */
      93           1 :   states_bitset =
      94           1 :       (unsigned)grpc_channel_args_compression_algorithm_get_states(ch_args);
      95             : 
      96           4 :   for (i = 0; i < GRPC_COMPRESS_ALGORITHMS_COUNT; i++) {
      97           3 :     GPR_ASSERT(GPR_BITGET(states_bitset, i));
      98             :   }
      99             : 
     100             :   /* disable gzip and deflate */
     101           1 :   ch_args_wo_gzip = grpc_channel_args_compression_algorithm_set_state(
     102             :       &ch_args, GRPC_COMPRESS_GZIP, 0);
     103           1 :   GPR_ASSERT(ch_args == ch_args_wo_gzip);
     104           1 :   ch_args_wo_gzip_deflate = grpc_channel_args_compression_algorithm_set_state(
     105             :       &ch_args_wo_gzip, GRPC_COMPRESS_DEFLATE, 0);
     106           1 :   GPR_ASSERT(ch_args_wo_gzip == ch_args_wo_gzip_deflate);
     107             : 
     108           1 :   states_bitset = (unsigned)grpc_channel_args_compression_algorithm_get_states(
     109             :       ch_args_wo_gzip_deflate);
     110           4 :   for (i = 0; i < GRPC_COMPRESS_ALGORITHMS_COUNT; i++) {
     111           3 :     if (i == GRPC_COMPRESS_GZIP || i == GRPC_COMPRESS_DEFLATE) {
     112           2 :       GPR_ASSERT(GPR_BITGET(states_bitset, i) == 0);
     113             :     } else {
     114           1 :       GPR_ASSERT(GPR_BITGET(states_bitset, i) != 0);
     115             :     }
     116             :   }
     117             : 
     118             :   /* re-enabled gzip only */
     119           1 :   ch_args_wo_gzip = grpc_channel_args_compression_algorithm_set_state(
     120             :       &ch_args_wo_gzip_deflate, GRPC_COMPRESS_GZIP, 1);
     121           1 :   GPR_ASSERT(ch_args_wo_gzip == ch_args_wo_gzip_deflate);
     122             : 
     123           1 :   states_bitset = (unsigned)grpc_channel_args_compression_algorithm_get_states(
     124             :       ch_args_wo_gzip);
     125           4 :   for (i = 0; i < GRPC_COMPRESS_ALGORITHMS_COUNT; i++) {
     126           3 :     if (i == GRPC_COMPRESS_DEFLATE) {
     127           1 :       GPR_ASSERT(GPR_BITGET(states_bitset, i) == 0);
     128             :     } else {
     129           2 :       GPR_ASSERT(GPR_BITGET(states_bitset, i) != 0);
     130             :     }
     131             :   }
     132             : 
     133           1 :   grpc_channel_args_destroy(ch_args);
     134           1 : }
     135             : 
     136           1 : int main(int argc, char **argv) {
     137           1 :   grpc_test_init(argc, argv);
     138           1 :   test_create();
     139           1 :   test_set_compression_algorithm();
     140           1 :   test_compression_algorithm_states();
     141           1 :   return 0;
     142             : }

Generated by: LCOV version 1.10