LCOV - code coverage report
Current view: top level - glib/gio/tests - io-stream.c (source / functions) Hit Total Coverage
Test: unnamed Lines: 71 71 100.0 %
Date: 2024-04-23 05:16:05 Functions: 6 6 100.0 %
Branches: 4 4 100.0 %

           Branch data     Line data    Source code
       1                 :            : /* GLib testing framework examples and tests
       2                 :            :  * Copyright (C) 2010 Collabora Ltd.
       3                 :            :  * Authors: Xavier Claessens <xclaesse@gmail.com>
       4                 :            :  *
       5                 :            :  * SPDX-License-Identifier: LicenseRef-old-glib-tests
       6                 :            :  *
       7                 :            :  * This work is provided "as is"; redistribution and modification
       8                 :            :  * in whole or in part, in any medium, physical or electronic is
       9                 :            :  * permitted without restriction.
      10                 :            :  *
      11                 :            :  * This work is distributed in the hope that it will be useful,
      12                 :            :  * but WITHOUT ANY WARRANTY; without even the implied warranty of
      13                 :            :  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
      14                 :            :  *
      15                 :            :  * In no event shall the authors or contributors be liable for any
      16                 :            :  * direct, indirect, incidental, special, exemplary, or consequential
      17                 :            :  * damages (including, but not limited to, procurement of substitute
      18                 :            :  * goods or services; loss of use, data, or profits; or business
      19                 :            :  * interruption) however caused and on any theory of liability, whether
      20                 :            :  * in contract, strict liability, or tort (including negligence or
      21                 :            :  * otherwise) arising in any way out of the use of this software, even
      22                 :            :  * if advised of the possibility of such damage.
      23                 :            :  */
      24                 :            : 
      25                 :            : #include <glib/glib.h>
      26                 :            : #include <gio/gio.h>
      27                 :            : #include <stdlib.h>
      28                 :            : #include <string.h>
      29                 :            : 
      30                 :            : typedef struct
      31                 :            : {
      32                 :            :   GMainLoop *main_loop;
      33                 :            :   const gchar *data1;
      34                 :            :   const gchar *data2;
      35                 :            :   GIOStream *iostream1;
      36                 :            :   GIOStream *iostream2;
      37                 :            : } TestCopyChunksData;
      38                 :            : 
      39                 :            : static void
      40                 :          1 : test_copy_chunks_splice_cb (GObject *source_object,
      41                 :            :     GAsyncResult *res,
      42                 :            :     gpointer user_data)
      43                 :            : {
      44                 :          1 :   TestCopyChunksData *data = user_data;
      45                 :            :   GMemoryOutputStream *ostream;
      46                 :            :   gchar *received_data;
      47                 :          1 :   GError *error = NULL;
      48                 :            : 
      49                 :          1 :   g_io_stream_splice_finish (res, &error);
      50                 :          1 :   g_assert_no_error (error);
      51                 :            : 
      52                 :          1 :   ostream = G_MEMORY_OUTPUT_STREAM (g_io_stream_get_output_stream (data->iostream1));
      53                 :          1 :   received_data = g_memory_output_stream_get_data (ostream);
      54                 :          1 :   g_assert_cmpstr (received_data, ==, data->data2);
      55                 :            : 
      56                 :          1 :   ostream = G_MEMORY_OUTPUT_STREAM (g_io_stream_get_output_stream (data->iostream2));
      57                 :          1 :   received_data = g_memory_output_stream_get_data (ostream);
      58                 :          1 :   g_assert_cmpstr (received_data, ==, data->data1);
      59                 :            : 
      60                 :          1 :   g_assert (g_io_stream_is_closed (data->iostream1));
      61                 :          1 :   g_assert (g_io_stream_is_closed (data->iostream2));
      62                 :            : 
      63                 :          1 :   g_main_loop_quit (data->main_loop);
      64                 :          1 : }
      65                 :            : 
      66                 :            : static void
      67                 :          1 : test_copy_chunks (void)
      68                 :            : {
      69                 :            :   TestCopyChunksData data;
      70                 :            :   GInputStream *istream;
      71                 :            :   GOutputStream *ostream;
      72                 :            : 
      73                 :          1 :   data.main_loop = g_main_loop_new (NULL, FALSE);
      74                 :          1 :   data.data1 = "abcdefghijklmnopqrstuvwxyz";
      75                 :          1 :   data.data2 = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
      76                 :            : 
      77                 :          1 :   istream = g_memory_input_stream_new_from_data (data.data1, -1, NULL);
      78                 :          1 :   ostream = g_memory_output_stream_new (NULL, 0, g_realloc, g_free);
      79                 :          1 :   data.iostream1 = g_simple_io_stream_new (istream, ostream);
      80                 :          1 :   g_object_unref (istream);
      81                 :          1 :   g_object_unref (ostream);
      82                 :            : 
      83                 :          1 :   istream = g_memory_input_stream_new_from_data (data.data2, -1, NULL);
      84                 :          1 :   ostream = g_memory_output_stream_new (NULL, 0, g_realloc, g_free);
      85                 :          1 :   data.iostream2 = g_simple_io_stream_new (istream, ostream);
      86                 :          1 :   g_object_unref (istream);
      87                 :          1 :   g_object_unref (ostream);
      88                 :            : 
      89                 :          1 :   g_io_stream_splice_async (data.iostream1, data.iostream2,
      90                 :            :       G_IO_STREAM_SPLICE_CLOSE_STREAM1 | G_IO_STREAM_SPLICE_CLOSE_STREAM2 |
      91                 :            :       G_IO_STREAM_SPLICE_WAIT_FOR_BOTH, G_PRIORITY_DEFAULT,
      92                 :            :       NULL, test_copy_chunks_splice_cb, &data);
      93                 :            : 
      94                 :            :   /* We do not hold a ref in data struct, this is to make sure the operation
      95                 :            :    * keeps the iostream objects alive until it finishes */
      96                 :          1 :   g_object_unref (data.iostream1);
      97                 :          1 :   g_object_unref (data.iostream2);
      98                 :            : 
      99                 :          1 :   g_main_loop_run (data.main_loop);
     100                 :          1 :   g_main_loop_unref (data.main_loop);
     101                 :          1 : }
     102                 :            : 
     103                 :            : static void
     104                 :          2 : close_async_done (GObject *source,
     105                 :            :                   GAsyncResult *result,
     106                 :            :                   gpointer user_data)
     107                 :            : {
     108                 :          2 :   gboolean *done = user_data;
     109                 :            : 
     110                 :          2 :   *done = TRUE;
     111                 :          2 : }
     112                 :            : 
     113                 :            : static void
     114                 :          1 : test_close_file (void)
     115                 :            : {
     116                 :            : #ifdef G_OS_UNIX
     117                 :            :   GFileIOStream *fios;
     118                 :            :   gboolean done;
     119                 :            :   GIOStream *io;
     120                 :            :   GFile *file;
     121                 :            : 
     122                 :          1 :   file = g_file_new_for_path ("/dev/null");
     123                 :          1 :   fios = g_file_open_readwrite (file, NULL, NULL);
     124                 :          1 :   g_object_unref (file);
     125                 :          1 :   g_assert (fios);
     126                 :            : 
     127                 :          1 :   io = g_simple_io_stream_new (g_io_stream_get_input_stream (G_IO_STREAM (fios)),
     128                 :          1 :                                g_io_stream_get_output_stream (G_IO_STREAM (fios)));
     129                 :          1 :   g_object_unref (fios);
     130                 :            : 
     131                 :          1 :   g_io_stream_close_async (io, 0, NULL, close_async_done, &done);
     132                 :          1 :   g_object_unref (io);
     133                 :            : 
     134                 :          1 :   done = FALSE;
     135         [ +  + ]:          2 :   while (!done)
     136                 :          1 :     g_main_context_iteration (NULL, TRUE);
     137                 :            : #endif
     138                 :          1 : }
     139                 :            : 
     140                 :            : static void
     141                 :          1 : test_close_memory (void)
     142                 :            : {
     143                 :            :   GInputStream *in;
     144                 :            :   GOutputStream *out;
     145                 :            :   gboolean done;
     146                 :            :   GIOStream *io;
     147                 :            : 
     148                 :          1 :   in = g_memory_input_stream_new ();
     149                 :          1 :   out = g_memory_output_stream_new_resizable ();
     150                 :          1 :   io = g_simple_io_stream_new (in, out);
     151                 :          1 :   g_object_unref (out);
     152                 :          1 :   g_object_unref (in);
     153                 :            : 
     154                 :          1 :   g_io_stream_close_async (io, 0, NULL, close_async_done, &done);
     155                 :          1 :   g_object_unref (io);
     156                 :            : 
     157                 :          1 :   done = FALSE;
     158         [ +  + ]:          2 :   while (!done)
     159                 :          1 :     g_main_context_iteration (NULL, TRUE);
     160                 :          1 : }
     161                 :            : 
     162                 :            : int
     163                 :          1 : main (int   argc,
     164                 :            :       char *argv[])
     165                 :            : {
     166                 :          1 :   g_test_init (&argc, &argv, NULL);
     167                 :            : 
     168                 :          1 :   g_test_add_func ("/io-stream/copy-chunks", test_copy_chunks);
     169                 :          1 :   g_test_add_func ("/io-stream/close/async/memory", test_close_memory);
     170                 :          1 :   g_test_add_func ("/io-stream/close/async/file", test_close_file);
     171                 :            : 
     172                 :          1 :   return g_test_run();
     173                 :            : }

Generated by: LCOV version 1.14