LCOV - code coverage report
Current view: top level - test/core/surface - byte_buffer_reader_test.c (source / functions) Hit Total Coverage
Test: tmp.zDYK9MVh93 Lines: 97 97 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 <grpc/byte_buffer_reader.h>
      35             : #include <grpc/byte_buffer.h>
      36             : #include <grpc/support/slice.h>
      37             : #include <grpc/grpc.h>
      38             : 
      39             : #include <grpc/support/alloc.h>
      40             : #include <grpc/support/log.h>
      41             : #include <grpc/support/thd.h>
      42             : #include <grpc/support/time.h>
      43             : #include "test/core/util/test_config.h"
      44             : 
      45             : #include "src/core/compression/message_compress.h"
      46             : 
      47             : #include <string.h>
      48             : 
      49             : #define LOG_TEST(x) gpr_log(GPR_INFO, "%s", x)
      50             : 
      51           1 : static void test_read_one_slice(void) {
      52             :   gpr_slice slice;
      53             :   grpc_byte_buffer *buffer;
      54             :   grpc_byte_buffer_reader reader;
      55             :   gpr_slice first_slice, second_slice;
      56             :   int first_code, second_code;
      57             : 
      58           1 :   LOG_TEST("test_read_one_slice");
      59           1 :   slice = gpr_slice_from_copied_string("test");
      60           1 :   buffer = grpc_raw_byte_buffer_create(&slice, 1);
      61           1 :   gpr_slice_unref(slice);
      62           1 :   grpc_byte_buffer_reader_init(&reader, buffer);
      63           1 :   first_code = grpc_byte_buffer_reader_next(&reader, &first_slice);
      64           1 :   GPR_ASSERT(first_code != 0);
      65           1 :   GPR_ASSERT(memcmp(GPR_SLICE_START_PTR(first_slice), "test", 4) == 0);
      66           1 :   gpr_slice_unref(first_slice);
      67           1 :   second_code = grpc_byte_buffer_reader_next(&reader, &second_slice);
      68           1 :   GPR_ASSERT(second_code == 0);
      69           1 :   grpc_byte_buffer_destroy(buffer);
      70           1 : }
      71             : 
      72           1 : static void test_read_one_slice_malloc(void) {
      73             :   gpr_slice slice;
      74             :   grpc_byte_buffer *buffer;
      75             :   grpc_byte_buffer_reader reader;
      76             :   gpr_slice first_slice, second_slice;
      77             :   int first_code, second_code;
      78             : 
      79           1 :   LOG_TEST("test_read_one_slice_malloc");
      80           1 :   slice = gpr_slice_malloc(4);
      81           1 :   memcpy(GPR_SLICE_START_PTR(slice), "test", 4);
      82           1 :   buffer = grpc_raw_byte_buffer_create(&slice, 1);
      83           1 :   gpr_slice_unref(slice);
      84           1 :   grpc_byte_buffer_reader_init(&reader, buffer);
      85           1 :   first_code = grpc_byte_buffer_reader_next(&reader, &first_slice);
      86           1 :   GPR_ASSERT(first_code != 0);
      87           1 :   GPR_ASSERT(memcmp(GPR_SLICE_START_PTR(first_slice), "test", 4) == 0);
      88           1 :   gpr_slice_unref(first_slice);
      89           1 :   second_code = grpc_byte_buffer_reader_next(&reader, &second_slice);
      90           1 :   GPR_ASSERT(second_code == 0);
      91           1 :   grpc_byte_buffer_destroy(buffer);
      92           1 : }
      93             : 
      94           1 : static void test_read_none_compressed_slice(void) {
      95             :   gpr_slice slice;
      96             :   grpc_byte_buffer *buffer;
      97             :   grpc_byte_buffer_reader reader;
      98             :   gpr_slice first_slice, second_slice;
      99             :   int first_code, second_code;
     100             : 
     101           1 :   LOG_TEST("test_read_none_compressed_slice");
     102           1 :   slice = gpr_slice_from_copied_string("test");
     103           1 :   buffer = grpc_raw_byte_buffer_create(&slice, 1);
     104           1 :   gpr_slice_unref(slice);
     105           1 :   grpc_byte_buffer_reader_init(&reader, buffer);
     106           1 :   first_code = grpc_byte_buffer_reader_next(&reader, &first_slice);
     107           1 :   GPR_ASSERT(first_code != 0);
     108           1 :   GPR_ASSERT(memcmp(GPR_SLICE_START_PTR(first_slice), "test", 4) == 0);
     109           1 :   gpr_slice_unref(first_slice);
     110           1 :   second_code = grpc_byte_buffer_reader_next(&reader, &second_slice);
     111           1 :   GPR_ASSERT(second_code == 0);
     112           1 :   grpc_byte_buffer_destroy(buffer);
     113           1 : }
     114             : 
     115           2 : static void read_compressed_slice(grpc_compression_algorithm algorithm,
     116             :                                   size_t input_size) {
     117             :   gpr_slice input_slice;
     118             :   gpr_slice_buffer sliceb_in;
     119             :   gpr_slice_buffer sliceb_out;
     120             :   grpc_byte_buffer *buffer;
     121             :   grpc_byte_buffer_reader reader;
     122             :   gpr_slice read_slice;
     123           2 :   size_t read_count = 0;
     124             : 
     125           2 :   gpr_slice_buffer_init(&sliceb_in);
     126           2 :   gpr_slice_buffer_init(&sliceb_out);
     127             : 
     128           2 :   input_slice = gpr_slice_malloc(input_size);
     129           2 :   memset(GPR_SLICE_START_PTR(input_slice), 'a', input_size);
     130           2 :   gpr_slice_buffer_add(&sliceb_in, input_slice); /* takes ownership */
     131           2 :   GPR_ASSERT(grpc_msg_compress(algorithm, &sliceb_in, &sliceb_out));
     132             : 
     133           2 :   buffer = grpc_raw_compressed_byte_buffer_create(sliceb_out.slices,
     134             :                                                   sliceb_out.count, algorithm);
     135           2 :   grpc_byte_buffer_reader_init(&reader, buffer);
     136             : 
     137          10 :   while (grpc_byte_buffer_reader_next(&reader, &read_slice)) {
     138           6 :     GPR_ASSERT(memcmp(GPR_SLICE_START_PTR(read_slice),
     139             :                       GPR_SLICE_START_PTR(input_slice) + read_count,
     140             :                       GPR_SLICE_LENGTH(read_slice)) == 0);
     141           6 :     read_count += GPR_SLICE_LENGTH(read_slice);
     142           6 :     gpr_slice_unref(read_slice);
     143             :   }
     144           2 :   GPR_ASSERT(read_count == input_size);
     145           2 :   grpc_byte_buffer_reader_destroy(&reader);
     146           2 :   grpc_byte_buffer_destroy(buffer);
     147           2 :   gpr_slice_buffer_destroy(&sliceb_out);
     148           2 :   gpr_slice_buffer_destroy(&sliceb_in);
     149           2 : }
     150             : 
     151           1 : static void test_read_gzip_compressed_slice(void) {
     152           1 :   const size_t INPUT_SIZE = 2048;
     153           1 :   LOG_TEST("test_read_gzip_compressed_slice");
     154           1 :   read_compressed_slice(GRPC_COMPRESS_GZIP, INPUT_SIZE);
     155           1 : }
     156             : 
     157           1 : static void test_read_deflate_compressed_slice(void) {
     158           1 :   const size_t INPUT_SIZE = 2048;
     159           1 :   LOG_TEST("test_read_deflate_compressed_slice");
     160           1 :   read_compressed_slice(GRPC_COMPRESS_DEFLATE, INPUT_SIZE);
     161           1 : }
     162             : 
     163           1 : static void test_byte_buffer_from_reader(void) {
     164             :   gpr_slice slice;
     165             :   grpc_byte_buffer *buffer, *buffer_from_reader;
     166             :   grpc_byte_buffer_reader reader;
     167             : 
     168           1 :   LOG_TEST("test_byte_buffer_from_reader");
     169           1 :   slice = gpr_slice_malloc(4);
     170           1 :   memcpy(GPR_SLICE_START_PTR(slice), "test", 4);
     171           1 :   buffer = grpc_raw_byte_buffer_create(&slice, 1);
     172           1 :   gpr_slice_unref(slice);
     173           1 :   grpc_byte_buffer_reader_init(&reader, buffer);
     174             : 
     175           1 :   buffer_from_reader = grpc_raw_byte_buffer_from_reader(&reader);
     176           1 :   GPR_ASSERT(buffer->type == buffer_from_reader->type);
     177           1 :   GPR_ASSERT(buffer_from_reader->data.raw.compression == GRPC_COMPRESS_NONE);
     178           1 :   GPR_ASSERT(buffer_from_reader->data.raw.slice_buffer.count == 1);
     179           1 :   GPR_ASSERT(memcmp(GPR_SLICE_START_PTR(
     180             :                         buffer_from_reader->data.raw.slice_buffer.slices[0]),
     181             :                     "test", 4) == 0);
     182             : 
     183           1 :   grpc_byte_buffer_destroy(buffer);
     184           1 :   grpc_byte_buffer_destroy(buffer_from_reader);
     185           1 : }
     186             : 
     187           1 : int main(int argc, char **argv) {
     188           1 :   grpc_test_init(argc, argv);
     189           1 :   test_read_one_slice();
     190           1 :   test_read_one_slice_malloc();
     191           1 :   test_read_none_compressed_slice();
     192           1 :   test_read_gzip_compressed_slice();
     193           1 :   test_read_deflate_compressed_slice();
     194           1 :   test_byte_buffer_from_reader();
     195             : 
     196           1 :   return 0;
     197             : }

Generated by: LCOV version 1.10