(root)/
glib-2.79.0/
gio/
tests/
memory-output-stream.c
       1  /* GLib testing framework examples and tests
       2   * Copyright (C) 2008 Red Hat, Inc.
       3   * Author: Matthias Clasen
       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  #include <glib/glib.h>
      25  #include <gio/gio.h>
      26  #include <stdlib.h>
      27  #include <string.h>
      28  
      29  static void
      30  test_truncate (void)
      31  {
      32    GOutputStream *mo;
      33    GDataOutputStream *o;
      34    int i;
      35    GError *error = NULL;
      36    guint8 *data;
      37  
      38    g_test_bug ("https://bugzilla.gnome.org/show_bug.cgi?id=540423");
      39  
      40    mo = g_memory_output_stream_new_resizable ();
      41    g_assert (g_seekable_can_truncate (G_SEEKABLE (mo)));
      42    o = g_data_output_stream_new (mo);
      43    for (i = 0; i < 1000; i++)
      44      {
      45        g_data_output_stream_put_byte (o, 1, NULL, &error);
      46        g_assert_no_error (error);
      47      }
      48    g_assert_cmpint (g_memory_output_stream_get_data_size (G_MEMORY_OUTPUT_STREAM (mo)), ==, 1000);
      49    g_seekable_truncate (G_SEEKABLE (mo), 0, NULL, &error);
      50    g_assert_cmpuint (g_seekable_tell (G_SEEKABLE (mo)), ==, 1000);
      51    
      52    g_assert_no_error (error);
      53    g_assert_cmpint (g_memory_output_stream_get_data_size (G_MEMORY_OUTPUT_STREAM (mo)), ==, 0);
      54    for (i = 0; i < 2000; i++)
      55      {
      56        g_data_output_stream_put_byte (o, 2, NULL, &error);
      57        g_assert_no_error (error);
      58      }
      59    g_assert_cmpint (g_memory_output_stream_get_data_size (G_MEMORY_OUTPUT_STREAM (mo)), ==, 3000);
      60  
      61    data = (guint8 *)g_memory_output_stream_get_data (G_MEMORY_OUTPUT_STREAM (mo));
      62  
      63    /* The 1's written initially were lost when we truncated to 0
      64     * and then started writing at position 1000.
      65     */
      66    for (i = 0; i < 1000; i++)
      67      g_assert_cmpuint (data[i], ==, 0);
      68    for (i = 1000; i < 3000; i++)
      69      g_assert_cmpuint (data[i], ==, 2);
      70  
      71    g_test_bug ("https://bugzilla.gnome.org/show_bug.cgi?id=720080");
      72  
      73    g_seekable_truncate (G_SEEKABLE (mo), 8192, NULL, &error);
      74    g_assert_cmpint (g_memory_output_stream_get_data_size (G_MEMORY_OUTPUT_STREAM (mo)), ==, 8192);
      75  
      76    data = (guint8 *)g_memory_output_stream_get_data (G_MEMORY_OUTPUT_STREAM (mo));
      77    for (i = 3000; i < 8192; i++)
      78      g_assert_cmpuint (data[i], ==, 0);
      79  
      80    g_object_unref (o);
      81    g_object_unref (mo);
      82  }
      83  
      84  static void
      85  test_seek_fixed (void)
      86  {
      87    GOutputStream *mo;
      88    GError *error;
      89  
      90    mo = g_memory_output_stream_new (g_new (gchar, 100), 100, NULL, g_free);
      91  
      92    g_assert (G_IS_SEEKABLE (mo));
      93    g_assert (g_seekable_can_seek (G_SEEKABLE (mo)));
      94    g_assert_cmpint (g_seekable_tell (G_SEEKABLE (mo)), ==, 0);
      95  
      96    error = NULL;
      97    g_assert (!g_seekable_seek (G_SEEKABLE (mo), 222, G_SEEK_CUR, NULL, &error));
      98    g_assert_error (error, G_IO_ERROR, G_IO_ERROR_INVALID_ARGUMENT);
      99    g_clear_error (&error);
     100    g_assert_cmpint (g_seekable_tell (G_SEEKABLE (mo)), ==, 0);
     101  
     102    g_assert (g_seekable_seek (G_SEEKABLE (mo), 26, G_SEEK_SET, NULL, &error));
     103    g_assert_no_error (error);
     104    g_assert_cmpint (g_seekable_tell (G_SEEKABLE (mo)), ==, 26);
     105  
     106    g_assert (g_seekable_seek (G_SEEKABLE (mo), 20, G_SEEK_CUR, NULL, &error));
     107    g_assert_cmpint (g_seekable_tell (G_SEEKABLE (mo)), ==, 46);
     108    g_assert_no_error (error);
     109  
     110    g_assert (!g_seekable_seek (G_SEEKABLE (mo), 200, G_SEEK_CUR, NULL, &error));
     111    g_assert_error (error, G_IO_ERROR, G_IO_ERROR_INVALID_ARGUMENT);
     112    g_clear_error (&error);
     113    g_assert_cmpint (g_seekable_tell (G_SEEKABLE (mo)), ==, 46);
     114  
     115    g_assert (!g_seekable_seek (G_SEEKABLE (mo), 1, G_SEEK_END, NULL, &error));
     116    g_assert_error (error, G_IO_ERROR, G_IO_ERROR_INVALID_ARGUMENT);
     117    g_clear_error (&error);
     118    g_assert_cmpint (g_seekable_tell (G_SEEKABLE (mo)), ==, 46);
     119  
     120    g_assert (g_seekable_seek (G_SEEKABLE (mo), 0, G_SEEK_END, NULL, &error));
     121    g_assert_no_error (error);
     122    g_assert_cmpint (g_seekable_tell (G_SEEKABLE (mo)), ==, 100);
     123  
     124    g_assert (g_seekable_seek (G_SEEKABLE (mo), -1, G_SEEK_END, NULL, &error));
     125    g_assert_no_error (error);
     126    g_assert_cmpint (g_seekable_tell (G_SEEKABLE (mo)), ==, 99);
     127  
     128    g_object_unref (mo);
     129  }
     130  
     131  static void
     132  test_seek_resizable_stream (GOutputStream *mo)
     133  {
     134    GError *error;
     135  
     136    g_assert (G_IS_SEEKABLE (mo));
     137    g_assert (g_seekable_can_seek (G_SEEKABLE (mo)));
     138    g_assert_cmpint (g_seekable_tell (G_SEEKABLE (mo)), ==, 0);
     139  
     140    error = NULL;
     141    g_assert (g_seekable_seek (G_SEEKABLE (mo), 222, G_SEEK_CUR, NULL, &error));
     142    g_assert_no_error (error);
     143    g_assert_cmpint (g_seekable_tell (G_SEEKABLE (mo)), ==, 222);
     144  
     145    g_assert (g_seekable_seek (G_SEEKABLE (mo), 26, G_SEEK_SET, NULL, &error));
     146    g_assert_no_error (error);
     147    g_assert_cmpint (g_seekable_tell (G_SEEKABLE (mo)), ==, 26);
     148  
     149    g_assert (g_seekable_seek (G_SEEKABLE (mo), 20, G_SEEK_CUR, NULL, &error));
     150    g_assert_cmpint (g_seekable_tell (G_SEEKABLE (mo)), ==, 46);
     151    g_assert_no_error (error);
     152  
     153    g_assert (g_seekable_seek (G_SEEKABLE (mo), 200, G_SEEK_CUR, NULL, &error));
     154    g_assert_cmpint (g_seekable_tell (G_SEEKABLE (mo)), ==, 246);
     155    g_assert_no_error (error);
     156  
     157    g_assert (g_seekable_seek (G_SEEKABLE (mo), 1, G_SEEK_END, NULL, &error));
     158    g_assert_cmpint (g_seekable_tell (G_SEEKABLE (mo)), ==, 1);
     159    g_assert_no_error (error);
     160  
     161    g_assert (g_seekable_seek (G_SEEKABLE (mo), 0, G_SEEK_END, NULL, &error));
     162    g_assert_no_error (error);
     163    g_assert_cmpint (g_seekable_tell (G_SEEKABLE (mo)), ==, 0);
     164  
     165    /* The 'end' is still zero, so this should fail */
     166    g_assert (!g_seekable_seek (G_SEEKABLE (mo), -1, G_SEEK_END, NULL, &error));
     167    g_assert_error (error, G_IO_ERROR, G_IO_ERROR_INVALID_ARGUMENT);
     168    g_clear_error (&error);
     169    g_assert_cmpint (g_seekable_tell (G_SEEKABLE (mo)), ==, 0);
     170  }
     171  
     172  static void
     173  test_seek_resizable (void)
     174  {
     175    GOutputStream *mo;
     176    gint i;
     177  
     178    /* For resizable streams, the initially allocated size is purely an
     179     * implementation detail.  We should not be able to tell the
     180     * difference based on the seek API, so make a bunch of streams with
     181     * different sizes and subject them to the same test.
     182     */
     183    for (i = 0; i < 1024; i++)
     184      {
     185        mo = g_memory_output_stream_new (g_malloc (i), i, g_realloc, g_free);
     186  
     187        test_seek_resizable_stream (mo);
     188  
     189        g_assert_cmpint (g_memory_output_stream_get_data_size (G_MEMORY_OUTPUT_STREAM (mo)), ==, 0);
     190        /* No writes = no resizes */
     191        g_assert_cmpint (g_memory_output_stream_get_size (G_MEMORY_OUTPUT_STREAM (mo)), ==, i);
     192  
     193        g_object_unref (mo);
     194      }
     195  }
     196  
     197  static void
     198  test_data_size (void)
     199  {
     200    GOutputStream *mo;
     201    GDataOutputStream *o;
     202    int pos;
     203  
     204    g_test_bug ("https://bugzilla.gnome.org/show_bug.cgi?id=540459");
     205  
     206    mo = g_memory_output_stream_new_resizable ();
     207    o = g_data_output_stream_new (mo);
     208    g_data_output_stream_put_byte (o, 1, NULL, NULL);
     209    pos = g_memory_output_stream_get_data_size (G_MEMORY_OUTPUT_STREAM (mo));
     210    g_assert_cmpint (pos, ==, 1);
     211  
     212    g_seekable_seek (G_SEEKABLE (mo), 0, G_SEEK_CUR, NULL, NULL);
     213    pos = g_seekable_tell (G_SEEKABLE (mo));
     214    g_assert_cmpint (pos, ==, 1);
     215  
     216    g_test_bug ("https://bugzilla.gnome.org/show_bug.cgi?id=540461");
     217    
     218    g_seekable_seek (G_SEEKABLE (mo), 0, G_SEEK_SET, NULL, NULL);
     219    pos = g_seekable_tell (G_SEEKABLE (mo));
     220    g_assert_cmpint (pos, ==, 0);
     221    
     222    pos = g_memory_output_stream_get_data_size (G_MEMORY_OUTPUT_STREAM (mo));
     223    g_assert_cmpint (pos, ==, 1);
     224  
     225    g_assert_cmpint (g_memory_output_stream_get_size (G_MEMORY_OUTPUT_STREAM (mo)), ==, 16);
     226  
     227    g_object_unref (o);
     228    g_object_unref (mo);
     229  }
     230  
     231  static void
     232  test_properties (void)
     233  {
     234    GOutputStream *mo;
     235    GDataOutputStream *o;
     236    int i;
     237    GError *error = NULL;
     238    gsize data_size_fun;
     239    gsize data_size_prop = 0;
     240    gpointer data_fun;
     241    gpointer data_prop;
     242    gpointer func;
     243  
     244    g_test_bug ("https://bugzilla.gnome.org/show_bug.cgi?id=605733");
     245  
     246    mo = (GOutputStream*) g_object_new (G_TYPE_MEMORY_OUTPUT_STREAM,
     247                                        "realloc-function", g_realloc,
     248                                        "destroy-function", g_free,
     249                                        NULL);
     250    o = g_data_output_stream_new (mo);
     251  
     252    for (i = 0; i < 1000; i++)
     253      {
     254        g_data_output_stream_put_byte (o, 1, NULL, &error);
     255        g_assert_no_error (error);
     256      }
     257  
     258    data_size_fun = g_memory_output_stream_get_data_size (G_MEMORY_OUTPUT_STREAM (mo));
     259    g_object_get (mo, "data-size", &data_size_prop, NULL);
     260    g_assert_cmpint (data_size_fun, ==, data_size_prop);
     261  
     262    data_fun = g_memory_output_stream_get_data (G_MEMORY_OUTPUT_STREAM (mo));
     263    g_object_get (mo, "data", &data_prop, NULL);
     264    g_assert_cmphex (GPOINTER_TO_SIZE (data_fun), ==, GPOINTER_TO_SIZE (data_prop));
     265  
     266    g_object_get (mo, "realloc-function", &func, NULL);
     267    g_assert (func == g_realloc);
     268    g_object_get (mo, "destroy-function", &func, NULL);
     269    g_assert (func == g_free);
     270  
     271    data_size_fun = g_memory_output_stream_get_size (G_MEMORY_OUTPUT_STREAM (mo));
     272    g_object_get (mo, "size", &data_size_prop, NULL);
     273    g_assert_cmpint (data_size_fun, ==, data_size_prop);
     274  
     275    g_object_unref (o);
     276    g_object_unref (mo);
     277  }
     278  
     279  static void
     280  test_write_bytes (void)
     281  {
     282    GOutputStream *mo;
     283    GBytes *bytes, *bytes2;
     284    GError *error = NULL;
     285  
     286    mo = (GOutputStream*) g_object_new (G_TYPE_MEMORY_OUTPUT_STREAM,
     287                                        "realloc-function", g_realloc,
     288                                        "destroy-function", g_free,
     289                                        NULL);
     290    bytes = g_bytes_new_static ("hello world!", strlen ("hello world!") + 1);
     291    g_output_stream_write_bytes (mo, bytes, NULL, &error);
     292    g_assert_no_error (error);
     293  
     294    g_output_stream_close (mo, NULL, &error);
     295    g_assert_no_error (error);
     296  
     297    bytes2 = g_memory_output_stream_steal_as_bytes (G_MEMORY_OUTPUT_STREAM (mo));
     298    g_object_unref (mo);
     299    g_assert (g_bytes_equal (bytes, bytes2));
     300  
     301    g_bytes_unref (bytes);
     302    g_bytes_unref (bytes2);
     303  }
     304  
     305  static void
     306  test_write_null (void)
     307  {
     308    GOutputStream *mo;
     309    GError *error = NULL;
     310  
     311    g_test_bug ("https://gitlab.gnome.org/GNOME/glib/-/issues/2471");
     312  
     313    mo = g_memory_output_stream_new_resizable ();
     314    g_output_stream_write_all (mo, NULL, 0, NULL, NULL, &error);
     315    g_assert_no_error (error);
     316  
     317    g_assert_cmpint (0, ==, g_memory_output_stream_get_data_size (G_MEMORY_OUTPUT_STREAM (mo)));
     318  
     319    g_output_stream_close (mo, NULL, &error);
     320    g_assert_no_error (error);
     321    g_object_unref (mo);
     322  }
     323  
     324  /* Test that writev() works on #GMemoryOutputStream with a non-empty set of vectors. This
     325   * covers the default writev() implementation around write(). */
     326  static void
     327  test_writev (void)
     328  {
     329    GOutputStream *mo;
     330    GError *error = NULL;
     331    gboolean res;
     332    gsize bytes_written;
     333    GOutputVector vectors[3];
     334    const guint8 buffer[] = {1, 2, 3, 4, 5,
     335                             1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12,
     336                             1, 2, 3};
     337    guint8 *output_buffer;
     338  
     339    vectors[0].buffer = buffer;
     340    vectors[0].size = 5;
     341  
     342    vectors[1].buffer = buffer + vectors[0].size;
     343    vectors[1].size = 12;
     344  
     345    vectors[2].buffer = buffer + vectors[0].size + vectors[1].size;
     346    vectors[2].size = 3;
     347  
     348    mo = (GOutputStream*) g_object_new (G_TYPE_MEMORY_OUTPUT_STREAM,
     349                                        "realloc-function", g_realloc,
     350                                        "destroy-function", g_free,
     351                                        NULL);
     352    res = g_output_stream_writev_all (mo, vectors, G_N_ELEMENTS (vectors), &bytes_written, NULL, &error);
     353    g_assert_no_error (error);
     354    g_assert_true (res);
     355    g_assert_cmpuint (bytes_written, ==, sizeof buffer);
     356  
     357    g_output_stream_close (mo, NULL, &error);
     358    g_assert_no_error (error);
     359  
     360    g_assert_cmpuint (g_memory_output_stream_get_data_size (G_MEMORY_OUTPUT_STREAM (mo)), ==, sizeof buffer);
     361    output_buffer = g_memory_output_stream_get_data (G_MEMORY_OUTPUT_STREAM (mo));
     362    g_assert_cmpmem (output_buffer, sizeof buffer, buffer, sizeof buffer);
     363  
     364    g_object_unref (mo);
     365  }
     366  
     367  /* Test that writev_nonblocking() works on #GMemoryOutputStream with a non-empty set of vectors. This
     368   * covers the default writev_nonblocking() implementation around write_nonblocking(). */
     369  static void
     370  test_writev_nonblocking (void)
     371  {
     372    GOutputStream *mo;
     373    GError *error = NULL;
     374    gboolean res;
     375    gsize bytes_written;
     376    GOutputVector vectors[3];
     377    const guint8 buffer[] = {1, 2, 3, 4, 5,
     378                             1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12,
     379                             1, 2, 3};
     380    guint8 *output_buffer;
     381  
     382    vectors[0].buffer = buffer;
     383    vectors[0].size = 5;
     384  
     385    vectors[1].buffer = buffer + vectors[0].size;
     386    vectors[1].size = 12;
     387  
     388    vectors[2].buffer = buffer + vectors[0].size + vectors[1].size;
     389    vectors[2].size = 3;
     390  
     391    mo = (GOutputStream*) g_object_new (G_TYPE_MEMORY_OUTPUT_STREAM,
     392                                        "realloc-function", g_realloc,
     393                                        "destroy-function", g_free,
     394                                        NULL);
     395    res = g_pollable_output_stream_writev_nonblocking (G_POLLABLE_OUTPUT_STREAM (mo),
     396                                                       vectors, G_N_ELEMENTS (vectors),
     397                                                       &bytes_written, NULL, &error);
     398    g_assert_no_error (error);
     399    g_assert_cmpint (res, ==, G_POLLABLE_RETURN_OK);
     400    g_assert_cmpuint (bytes_written, ==, sizeof buffer);
     401  
     402    g_output_stream_close (mo, NULL, &error);
     403    g_assert_no_error (error);
     404  
     405    g_assert_cmpuint (g_memory_output_stream_get_data_size (G_MEMORY_OUTPUT_STREAM (mo)), ==, sizeof buffer);
     406    output_buffer = g_memory_output_stream_get_data (G_MEMORY_OUTPUT_STREAM (mo));
     407    g_assert_cmpmem (output_buffer, sizeof buffer, buffer, sizeof buffer);
     408  
     409    g_object_unref (mo);
     410  }
     411  
     412  static void
     413  test_steal_as_bytes (void)
     414  {
     415    GOutputStream *mo;
     416    GDataOutputStream *o;
     417    GError *error = NULL;
     418    GBytes *bytes;
     419    gsize size;
     420  
     421    mo = (GOutputStream*) g_object_new (G_TYPE_MEMORY_OUTPUT_STREAM,
     422                                        "realloc-function", g_realloc,
     423                                        "destroy-function", g_free,
     424                                        NULL);
     425    o = g_data_output_stream_new (mo);
     426  
     427    g_data_output_stream_put_string (o, "hello ", NULL, &error);
     428    g_assert_no_error (error);
     429  
     430    g_data_output_stream_put_string (o, "world!", NULL, &error);
     431    g_assert_no_error (error);
     432  
     433    g_data_output_stream_put_byte (o, '\0', NULL, &error);
     434    g_assert_no_error (error);
     435  
     436    g_output_stream_close ((GOutputStream*) o, NULL, &error);
     437    g_assert_no_error (error);
     438  
     439    bytes = g_memory_output_stream_steal_as_bytes ((GMemoryOutputStream*)mo);
     440    g_object_unref (mo);
     441  
     442    g_assert_cmpint (g_bytes_get_size (bytes), ==, strlen ("hello world!") + 1);
     443    g_assert_cmpstr (g_bytes_get_data (bytes, &size), ==, "hello world!");
     444  
     445    g_bytes_unref (bytes);
     446    g_object_unref (o);
     447  }
     448  
     449  int
     450  main (int   argc,
     451        char *argv[])
     452  {
     453    g_test_init (&argc, &argv, NULL);
     454  
     455    g_test_add_func ("/memory-output-stream/truncate", test_truncate);
     456    g_test_add_func ("/memory-output-stream/seek/fixed", test_seek_fixed);
     457    g_test_add_func ("/memory-output-stream/seek/resizable", test_seek_resizable);
     458    g_test_add_func ("/memory-output-stream/get-data-size", test_data_size);
     459    g_test_add_func ("/memory-output-stream/properties", test_properties);
     460    g_test_add_func ("/memory-output-stream/write-bytes", test_write_bytes);
     461    g_test_add_func ("/memory-output-stream/write-null", test_write_null);
     462    g_test_add_func ("/memory-output-stream/writev", test_writev);
     463    g_test_add_func ("/memory-output-stream/writev_nonblocking", test_writev_nonblocking);
     464    g_test_add_func ("/memory-output-stream/steal_as_bytes", test_steal_as_bytes);
     465  
     466    return g_test_run();
     467  }