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

           Branch data     Line data    Source code
       1                 :            : #include <glib/glib.h>
       2                 :            : #include <glib/gstdio.h>
       3                 :            : #include <gio/gio.h>
       4                 :            : #include <string.h>
       5                 :            : 
       6                 :            : #ifdef G_OS_UNIX
       7                 :            : #include <unistd.h>
       8                 :            : #endif
       9                 :            : #ifdef G_OS_WIN32
      10                 :            : #include <io.h> /* for close() */
      11                 :            : #endif
      12                 :            : 
      13                 :            : static const char *original_data = "This is some test data that we can put in a file...";
      14                 :            : static const char *new_data = "new data..";
      15                 :            : 
      16                 :            : static void
      17                 :         33 : verify_pos (GIOStream *iostream, goffset expected_pos)
      18                 :            : {
      19                 :            :   goffset pos;
      20                 :            : 
      21                 :         33 :   pos = g_seekable_tell (G_SEEKABLE (iostream));
      22                 :         33 :   g_assert_cmpint (pos, ==, expected_pos);
      23                 :            : 
      24                 :         33 :   pos = g_seekable_tell (G_SEEKABLE (g_io_stream_get_input_stream (iostream)));
      25                 :         33 :   g_assert_cmpint (pos, ==, expected_pos);
      26                 :            : 
      27                 :         33 :   pos = g_seekable_tell (G_SEEKABLE (g_io_stream_get_output_stream (iostream)));
      28                 :         33 :   g_assert_cmpint (pos, ==, expected_pos);
      29                 :         33 : }
      30                 :            : 
      31                 :            : static void
      32                 :          3 : verify_iostream (GFileIOStream *file_iostream)
      33                 :            : {
      34                 :            :   gboolean res;
      35                 :            :   gssize skipped;
      36                 :            :   GIOStream *iostream;
      37                 :            :   GError *error;
      38                 :            :   GInputStream *in;
      39                 :            :   GOutputStream *out;
      40                 :            :   char buffer[1024];
      41                 :            :   gsize n_bytes;
      42                 :            :   char *modified_data;
      43                 :            : 
      44                 :          3 :   iostream = G_IO_STREAM (file_iostream);
      45                 :            : 
      46                 :          3 :   verify_pos (iostream, 0);
      47                 :            : 
      48                 :          3 :   in = g_io_stream_get_input_stream (iostream);
      49                 :          3 :   out = g_io_stream_get_output_stream (iostream);
      50                 :            : 
      51                 :          3 :   res = g_input_stream_read_all (in, buffer, 20, &n_bytes, NULL, NULL);
      52                 :          3 :   g_assert (res);
      53                 :          3 :   g_assert_cmpmem (buffer, n_bytes, original_data, 20);
      54                 :            : 
      55                 :          3 :   verify_pos (iostream, 20);
      56                 :            : 
      57                 :          3 :   res = g_seekable_seek (G_SEEKABLE (iostream),
      58                 :            :                          -10, G_SEEK_END,
      59                 :            :                          NULL, NULL);
      60                 :          3 :   g_assert (res);
      61                 :          3 :   verify_pos (iostream, strlen (original_data) - 10);
      62                 :            : 
      63                 :          3 :   res = g_input_stream_read_all (in, buffer, 20, &n_bytes, NULL, NULL);
      64                 :          3 :   g_assert (res);
      65                 :          3 :   g_assert_cmpmem (buffer, n_bytes, original_data + strlen (original_data) - 10, 10);
      66                 :            : 
      67                 :          3 :   verify_pos (iostream, strlen (original_data));
      68                 :            : 
      69                 :          3 :   res = g_seekable_seek (G_SEEKABLE (iostream),
      70                 :            :                          10, G_SEEK_SET,
      71                 :            :                          NULL, NULL);
      72                 :            : 
      73                 :          3 :   res = g_input_stream_skip (in, 5, NULL, NULL);
      74                 :          3 :   g_assert (res == 5);
      75                 :          3 :   verify_pos (iostream, 15);
      76                 :            : 
      77                 :          3 :   skipped = g_input_stream_skip (in, 10000, NULL, NULL);
      78                 :          3 :   g_assert_cmpint (skipped, >=, 0);
      79                 :          3 :   g_assert ((gsize) skipped == strlen (original_data) - 15);
      80                 :          3 :   verify_pos (iostream, strlen (original_data));
      81                 :            : 
      82                 :          3 :   res = g_seekable_seek (G_SEEKABLE (iostream),
      83                 :            :                          10, G_SEEK_SET,
      84                 :            :                          NULL, NULL);
      85                 :            : 
      86                 :          3 :   verify_pos (iostream, 10);
      87                 :            : 
      88                 :          3 :   res = g_output_stream_write_all (out, new_data, strlen (new_data),
      89                 :            :                                    &n_bytes, NULL, NULL);
      90                 :          3 :   g_assert (res);
      91                 :          3 :   g_assert_cmpint (n_bytes, ==, strlen (new_data));
      92                 :            : 
      93                 :          3 :   verify_pos (iostream, 10 + strlen (new_data));
      94                 :            : 
      95                 :          3 :   res = g_seekable_seek (G_SEEKABLE (iostream),
      96                 :            :                          0, G_SEEK_SET,
      97                 :            :                          NULL, NULL);
      98                 :          3 :   g_assert (res);
      99                 :          3 :   verify_pos (iostream, 0);
     100                 :            : 
     101                 :          3 :   res = g_input_stream_read_all (in, buffer, strlen (original_data), &n_bytes, NULL, NULL);
     102                 :          3 :   g_assert (res);
     103                 :          3 :   g_assert_cmpint ((int)n_bytes, ==, strlen (original_data));
     104                 :          3 :   buffer[n_bytes] = 0;
     105                 :            : 
     106                 :          3 :   modified_data = g_strdup (original_data);
     107                 :          3 :   memcpy (modified_data + 10, new_data, strlen (new_data));
     108                 :          3 :   g_assert_cmpstr (buffer, ==, modified_data);
     109                 :            : 
     110                 :          3 :   verify_pos (iostream, strlen (original_data));
     111                 :            : 
     112                 :          3 :   res = g_seekable_seek (G_SEEKABLE (iostream),
     113                 :            :                          0, G_SEEK_SET,
     114                 :            :                          NULL, NULL);
     115                 :          3 :   g_assert (res);
     116                 :          3 :   verify_pos (iostream, 0);
     117                 :            : 
     118                 :          3 :   res = g_output_stream_close (out, NULL, NULL);
     119                 :          3 :   g_assert (res);
     120                 :            : 
     121                 :          3 :   res = g_input_stream_read_all (in, buffer, 15, &n_bytes, NULL, NULL);
     122                 :          3 :   g_assert (res);
     123                 :          3 :   g_assert_cmpmem (buffer, n_bytes, modified_data, 15);
     124                 :            : 
     125                 :          3 :   error = NULL;
     126                 :          3 :   res = g_output_stream_write_all (out, new_data, strlen (new_data),
     127                 :            :                                    &n_bytes, NULL, &error);
     128                 :          3 :   g_assert (!res);
     129                 :          3 :   g_assert_error (error, G_IO_ERROR, G_IO_ERROR_CLOSED);
     130                 :          3 :   g_error_free (error);
     131                 :            : 
     132                 :          3 :   error = NULL;
     133                 :          3 :   res = g_io_stream_close (iostream, NULL, &error);
     134                 :          3 :   g_assert (res);
     135                 :          3 :   g_assert_no_error (error);
     136                 :            : 
     137                 :          3 :   g_free (modified_data);
     138                 :          3 : }
     139                 :            : 
     140                 :            : static void
     141                 :          1 : test_g_file_open_readwrite (void)
     142                 :            : {
     143                 :            :   char *tmp_file;
     144                 :            :   int fd;
     145                 :            :   gboolean res;
     146                 :            :   GFileIOStream *file_iostream;
     147                 :            :   char *path;
     148                 :            :   GFile *file;
     149                 :            :   GError *error;
     150                 :            : 
     151                 :          1 :   fd = g_file_open_tmp ("readwrite_XXXXXX",
     152                 :            :                         &tmp_file, NULL);
     153                 :          1 :   g_assert (fd != -1);
     154                 :          1 :   close (fd);
     155                 :            : 
     156                 :          1 :   res = g_file_set_contents (tmp_file,
     157                 :            :                              original_data, -1, NULL);
     158                 :          1 :   g_assert (res);
     159                 :            : 
     160                 :          1 :   path = g_build_filename (g_get_tmp_dir (), "g-a-nonexisting-file", NULL);
     161                 :          1 :   file = g_file_new_for_path (path);
     162                 :          1 :   g_free (path);
     163                 :          1 :   error = NULL;
     164                 :          1 :   file_iostream = g_file_open_readwrite (file, NULL, &error);
     165                 :          1 :   g_assert (file_iostream == NULL);
     166                 :          1 :   g_assert (g_error_matches (error, G_IO_ERROR, G_IO_ERROR_NOT_FOUND));
     167                 :          1 :   g_error_free (error);
     168                 :          1 :   g_object_unref (file);
     169                 :            : 
     170                 :          1 :   file = g_file_new_for_path (tmp_file);
     171                 :          1 :   error = NULL;
     172                 :          1 :   file_iostream = g_file_open_readwrite (file, NULL, &error);
     173                 :          1 :   g_assert (file_iostream != NULL);
     174                 :          1 :   g_object_unref (file);
     175                 :            : 
     176                 :          1 :   verify_iostream (file_iostream);
     177                 :            : 
     178                 :          1 :   g_object_unref (file_iostream);
     179                 :            : 
     180                 :          1 :   g_unlink (tmp_file);
     181                 :          1 :   g_free (tmp_file);
     182                 :          1 : }
     183                 :            : 
     184                 :            : static void
     185                 :          1 : test_g_file_create_readwrite (void)
     186                 :            : {
     187                 :            :   char *tmp_file;
     188                 :            :   int fd;
     189                 :            :   gboolean res;
     190                 :            :   GFileIOStream *file_iostream;
     191                 :            :   GOutputStream *out;
     192                 :            :   GFile *file;
     193                 :            :   GError *error;
     194                 :            :   gsize n_bytes;
     195                 :            : 
     196                 :          1 :   fd = g_file_open_tmp ("readwrite_XXXXXX",
     197                 :            :                         &tmp_file, NULL);
     198                 :          1 :   g_assert (fd != -1);
     199                 :          1 :   close (fd);
     200                 :            : 
     201                 :          1 :   file = g_file_new_for_path (tmp_file);
     202                 :          1 :   error = NULL;
     203                 :          1 :   file_iostream = g_file_create_readwrite (file, 0, NULL, &error);
     204                 :          1 :   g_assert (file_iostream == NULL);
     205                 :          1 :   g_assert_error (error, G_IO_ERROR, G_IO_ERROR_EXISTS);
     206                 :          1 :   g_error_free (error);
     207                 :            : 
     208                 :          1 :   g_unlink (tmp_file);
     209                 :          1 :   file_iostream = g_file_create_readwrite (file, 0, NULL, &error);
     210                 :          1 :   g_assert (file_iostream != NULL);
     211                 :            : 
     212                 :          1 :   out = g_io_stream_get_output_stream (G_IO_STREAM (file_iostream));
     213                 :          1 :   res = g_output_stream_write_all (out, original_data, strlen (original_data),
     214                 :            :                                    &n_bytes, NULL, NULL);
     215                 :          1 :   g_assert (res);
     216                 :          1 :   g_assert_cmpint (n_bytes, ==, strlen (original_data));
     217                 :            : 
     218                 :          1 :   res = g_seekable_seek (G_SEEKABLE (file_iostream),
     219                 :            :                          0, G_SEEK_SET,
     220                 :            :                          NULL, NULL);
     221                 :          1 :   g_assert (res);
     222                 :            : 
     223                 :          1 :   verify_iostream (file_iostream);
     224                 :            : 
     225                 :          1 :   g_object_unref (file_iostream);
     226                 :          1 :   g_object_unref (file);
     227                 :            : 
     228                 :          1 :   g_unlink (tmp_file);
     229                 :          1 :   g_free (tmp_file);
     230                 :          1 : }
     231                 :            : 
     232                 :            : static void
     233                 :          1 : test_g_file_replace_readwrite (void)
     234                 :            : {
     235                 :            :   char *tmp_file, *backup, *data;
     236                 :            :   int fd;
     237                 :            :   gboolean res;
     238                 :            :   GFileIOStream *file_iostream;
     239                 :            :   GInputStream *in;
     240                 :            :   GOutputStream *out;
     241                 :            :   GFile *file;
     242                 :            :   GError *error;
     243                 :            :   char buffer[1024];
     244                 :            :   gsize n_bytes;
     245                 :            : 
     246                 :          1 :   fd = g_file_open_tmp ("readwrite_XXXXXX",
     247                 :            :                         &tmp_file, NULL);
     248                 :          1 :   g_assert (fd != -1);
     249                 :          1 :   close (fd);
     250                 :            : 
     251                 :          1 :   res = g_file_set_contents (tmp_file,
     252                 :            :                              new_data, -1, NULL);
     253                 :          1 :   g_assert (res);
     254                 :            : 
     255                 :          1 :   file = g_file_new_for_path (tmp_file);
     256                 :          1 :   error = NULL;
     257                 :          1 :   file_iostream = g_file_replace_readwrite (file, NULL,
     258                 :            :                                             TRUE, 0, NULL, &error);
     259                 :          1 :   g_assert (file_iostream != NULL);
     260                 :            : 
     261                 :          1 :   in = g_io_stream_get_input_stream (G_IO_STREAM (file_iostream));
     262                 :            : 
     263                 :            :   /* Ensure its empty */
     264                 :          1 :   res = g_input_stream_read_all (in, buffer, sizeof buffer, &n_bytes, NULL, NULL);
     265                 :          1 :   g_assert (res);
     266                 :          1 :   g_assert_cmpint ((int)n_bytes, ==, 0);
     267                 :            : 
     268                 :          1 :   out = g_io_stream_get_output_stream (G_IO_STREAM (file_iostream));
     269                 :          1 :   res = g_output_stream_write_all (out, original_data, strlen (original_data),
     270                 :            :                                    &n_bytes, NULL, NULL);
     271                 :          1 :   g_assert (res);
     272                 :          1 :   g_assert_cmpint (n_bytes, ==, strlen (original_data));
     273                 :            : 
     274                 :          1 :   res = g_seekable_seek (G_SEEKABLE (file_iostream),
     275                 :            :                          0, G_SEEK_SET,
     276                 :            :                          NULL, NULL);
     277                 :          1 :   g_assert (res);
     278                 :            : 
     279                 :          1 :   verify_iostream (file_iostream);
     280                 :            : 
     281                 :          1 :   g_object_unref (file_iostream);
     282                 :          1 :   g_object_unref (file);
     283                 :            : 
     284                 :          1 :   backup = g_strconcat (tmp_file, "~", NULL);
     285                 :          1 :   res = g_file_get_contents (backup,
     286                 :            :                              &data,
     287                 :            :                              NULL, NULL);
     288                 :          1 :   g_assert (res);
     289                 :          1 :   g_assert_cmpstr (data, ==, new_data);
     290                 :          1 :   g_free (data);
     291                 :          1 :   g_unlink (backup);
     292                 :          1 :   g_free (backup);
     293                 :            : 
     294                 :          1 :   g_unlink (tmp_file);
     295                 :          1 :   g_free (tmp_file);
     296                 :          1 : }
     297                 :            : 
     298                 :            : 
     299                 :            : int
     300                 :          1 : main (int   argc,
     301                 :            :       char *argv[])
     302                 :            : {
     303                 :          1 :   g_test_init (&argc, &argv, NULL);
     304                 :            : 
     305                 :          1 :   g_test_add_func ("/readwrite/test_g_file_open_readwrite",
     306                 :            :                    test_g_file_open_readwrite);
     307                 :          1 :   g_test_add_func ("/readwrite/test_g_file_create_readwrite",
     308                 :            :                    test_g_file_create_readwrite);
     309                 :          1 :   g_test_add_func ("/readwrite/test_g_file_replace_readwrite",
     310                 :            :                    test_g_file_replace_readwrite);
     311                 :            : 
     312                 :          1 :   return g_test_run();
     313                 :            : }

Generated by: LCOV version 1.14