LCOV - code coverage report
Current view: top level - gio/tests - converter.c (source / functions) Coverage Total Hit
Test: unnamed Lines: 100.0 % 48 48
Test Date: 2024-11-26 05:23:01 Functions: 100.0 % 3 3
Branches: - 0 0

             Branch data     Line data    Source code
       1                 :             : /* GLib testing framework examples and tests
       2                 :             :  * Copyright (C) 2024 Red Hat, Inc.
       3                 :             :  * Authors: Matthias Clasen
       4                 :             :  *
       5                 :             :  * SPDX-License-Identifier: LGPL-2.1-or-later
       6                 :             :  *
       7                 :             :  * This library is free software; you can redistribute it and/or
       8                 :             :  * modify it under the terms of the GNU Lesser General Public
       9                 :             :  * License as published by the Free Software Foundation; either
      10                 :             :  * version 2.1 of the License, or (at your option) any later version.
      11                 :             :  *
      12                 :             :  * This library is distributed in the hope that it will be useful,
      13                 :             :  * but WITHOUT ANY WARRANTY; without even the implied warranty of
      14                 :             :  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
      15                 :             :  * Lesser General Public License for more details.
      16                 :             :  *
      17                 :             :  * You should have received a copy of the GNU Lesser General
      18                 :             :  * Public License along with this library; if not, see <http://www.gnu.org/licenses/>.
      19                 :             :  */
      20                 :             : 
      21                 :             : #include <glib/glib.h>
      22                 :             : #include <gio/gio.h>
      23                 :             : #include <stdlib.h>
      24                 :             : #include <string.h>
      25                 :             : 
      26                 :             : static void
      27                 :           1 : test_extra_bytes_at_end (void)
      28                 :             : {
      29                 :             :   char data[1024];
      30                 :             :   gsize size;
      31                 :             :   GBytes *bytes;
      32                 :             :   GConverter *converter;
      33                 :           1 :   GError *error = NULL;
      34                 :             :   GBytes *result;
      35                 :             : 
      36                 :             :   /* Create some simple data to encode */
      37                 :           1 :   data[0] = 0;
      38                 :           1 :   bytes = g_bytes_new_static (data, 1);
      39                 :             : 
      40                 :             :   /* encode the data */
      41                 :           1 :   converter = G_CONVERTER (g_zlib_compressor_new (G_ZLIB_COMPRESSOR_FORMAT_GZIP, 9));
      42                 :           1 :   result = g_converter_convert_bytes (converter, bytes, &error);
      43                 :           1 :   g_assert_no_error (error);
      44                 :           1 :   g_assert_nonnull (result);
      45                 :           1 :   g_bytes_unref (bytes);
      46                 :           1 :   g_clear_object (&converter);
      47                 :             : 
      48                 :             :   /* Append a 0 byte to the encoded data */
      49                 :           1 :   size = g_bytes_get_size (result);
      50                 :           1 :   g_assert_cmpuint (size, <, G_N_ELEMENTS (data)); /* just to be very sure */
      51                 :           1 :   memcpy (data, g_bytes_get_data (result, NULL), size);
      52                 :           1 :   data[size] = 0;
      53                 :           1 :   bytes = g_bytes_new_static (data, size + 1);
      54                 :           1 :   g_bytes_unref (result);
      55                 :             : 
      56                 :             :   /* Decompress the just compressed bytes with the extra 0 */
      57                 :           1 :   converter = G_CONVERTER (g_zlib_decompressor_new (G_ZLIB_COMPRESSOR_FORMAT_GZIP));
      58                 :           1 :   result = g_converter_convert_bytes (converter, bytes, &error);
      59                 :           1 :   g_assert_error (error, G_IO_ERROR, G_IO_ERROR_MESSAGE_TOO_LARGE);
      60                 :           1 :   g_assert_null (result);
      61                 :           1 :   g_clear_error (&error);
      62                 :             : 
      63                 :           1 :   g_object_unref (converter);
      64                 :           1 :   g_bytes_unref (bytes);
      65                 :           1 : }
      66                 :             : 
      67                 :             : static void
      68                 :           1 : test_convert_bytes (void)
      69                 :             : {
      70                 :             :   char data[8192];
      71                 :             :   GBytes *bytes;
      72                 :             :   GConverter *converter;
      73                 :           1 :   GError *error = NULL;
      74                 :             :   GBytes *result;
      75                 :             : 
      76                 :        8193 :   for (gsize i = 0; i < sizeof (data); i++)
      77                 :        8192 :     data[i] = g_test_rand_int_range (0, 256);
      78                 :             : 
      79                 :           1 :   bytes = g_bytes_new_static (data, sizeof (data));
      80                 :             : 
      81                 :           1 :   converter = G_CONVERTER (g_zlib_compressor_new (G_ZLIB_COMPRESSOR_FORMAT_GZIP, 9));
      82                 :           1 :   result = g_converter_convert_bytes (converter, bytes, &error);
      83                 :           1 :   g_assert_no_error (error);
      84                 :           1 :   g_assert_nonnull (result);
      85                 :             : 
      86                 :           1 :   g_bytes_unref (result);
      87                 :           1 :   g_object_unref (converter);
      88                 :             : 
      89                 :           1 :   converter = G_CONVERTER (g_zlib_decompressor_new (G_ZLIB_COMPRESSOR_FORMAT_GZIP));
      90                 :           1 :   result = g_converter_convert_bytes (converter, bytes, &error);
      91                 :           1 :   g_assert_nonnull (error);
      92                 :           1 :   g_error_free (error);
      93                 :           1 :   g_assert_true (result == NULL);
      94                 :             : 
      95                 :           1 :   g_object_unref (converter);
      96                 :           1 :   g_bytes_unref (bytes);
      97                 :           1 : }
      98                 :             : 
      99                 :             : int
     100                 :           1 : main (int   argc,
     101                 :             :       char *argv[])
     102                 :             : {
     103                 :           1 :   g_test_init (&argc, &argv, NULL);
     104                 :             : 
     105                 :           1 :   g_test_add_func ("/converter/bytes", test_convert_bytes);
     106                 :           1 :   g_test_add_func ("/converter/extra-bytes-at-end", test_extra_bytes_at_end);
     107                 :             : 
     108                 :           1 :   return g_test_run();
     109                 :             : }
        

Generated by: LCOV version 2.0-1