summaryrefslogtreecommitdiff
path: root/glib/tests/io-channel.c
blob: c5dd01d04eb2c35ebf340cd50a221de6c274689f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
/* GLib testing framework examples and tests
 *
 * Copyright © 2001 Hidetoshi Tajima
 * Copyright © 2001 Ron Steinke
 * Copyright © 2001 Owen Taylor
 * Copyright © 2002 Manish Singh
 * Copyright © 2011 Sjoerd Simons
 * Copyright © 2012 Simon McVittie
 * Copyright © 2013 Stef Walter
 * Copyright © 2005, 2006, 2008, 2012, 2013 Matthias Clasen
 * Copyright © 2020 Endless Mobile, Inc.
 *
 * SPDX-License-Identifier: LGPL-2.1-or-later
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General
 * Public License along with this library; if not, see <http://www.gnu.org/licenses/>.
 *
 * Author: Philip Withnall <withnall@endlessm.com>
 */

#include <glib.h>
#include <glib/gstdio.h>

static void
test_small_writes (void)
{
  GIOChannel *io;
  GIOStatus status = G_IO_STATUS_ERROR;
  guint bytes_remaining;
  gchar tmp;
  GError *local_error = NULL;

  io = g_io_channel_new_file ("iochannel-test-outfile", "w", &local_error);
  g_assert_no_error (local_error);

  g_io_channel_set_encoding (io, NULL, NULL);
  g_io_channel_set_buffer_size (io, 1022);

  bytes_remaining = 2 * g_io_channel_get_buffer_size (io);
  tmp = 0;

  while (bytes_remaining)
    {
      status = g_io_channel_write_chars (io, &tmp, 1, NULL, NULL);
      if (status == G_IO_STATUS_ERROR)
        break;
      if (status == G_IO_STATUS_NORMAL)
        bytes_remaining--;
    }

  g_assert_cmpint (status, ==, G_IO_STATUS_NORMAL);

  g_io_channel_unref (io);
  g_remove ("iochannel-test-outfile");
}

static void
test_read_write (void)
{
  GIOChannel *gio_r, *gio_w ;
  GError *local_error = NULL;
  GString *buffer;
  char *filename;
  gint rlength = 0;
  glong wlength = 0;
  gsize length_out;
  const gchar *encoding = "EUC-JP";
  GIOStatus status;
  const gsize buffer_size_bytes = 1024;

  filename = g_test_build_filename (G_TEST_DIST, "iochannel-test-infile", NULL);

  setbuf (stdout, NULL); /* For debugging */

  gio_r = g_io_channel_new_file (filename, "r", &local_error);
  g_assert_no_error (local_error);

  gio_w = g_io_channel_new_file ("iochannel-test-outfile", "w", &local_error);
  g_assert_no_error (local_error);

  g_io_channel_set_encoding (gio_r, encoding, &local_error);
  g_assert_no_error (local_error);

  g_io_channel_set_buffer_size (gio_r, buffer_size_bytes);

  status = g_io_channel_set_flags (gio_r, G_IO_FLAG_NONBLOCK, &local_error);
  if (status == G_IO_STATUS_ERROR)
    {
#ifdef G_OS_WIN32
      g_test_message ("FIXME: not implemented on win32");
#else
      /* Errors should not happen */
      g_assert_no_error (local_error);
#endif
      g_clear_error (&local_error);
    }
  buffer = g_string_sized_new (buffer_size_bytes);

  while (TRUE)
    {
      do
        status = g_io_channel_read_line_string (gio_r, buffer, NULL, &local_error);
      while (status == G_IO_STATUS_AGAIN);
      if (status != G_IO_STATUS_NORMAL)
        break;

      rlength += buffer->len;

      do
        status = g_io_channel_write_chars (gio_w, buffer->str, buffer->len,
          &length_out, &local_error);
      while (status == G_IO_STATUS_AGAIN);
      if (status != G_IO_STATUS_NORMAL)
        break;

      wlength += length_out;

      /* Ensure the whole line was written */
      g_assert_cmpuint (length_out, ==, buffer->len);

      g_test_message ("%s", buffer->str);
      g_string_truncate (buffer, 0);
    }

  switch (status)
    {
      case G_IO_STATUS_EOF:
        break;
      case G_IO_STATUS_ERROR:
        /* Errors should not happen */
        g_assert_no_error (local_error);
        g_clear_error (&local_error);
        break;
      default:
        g_assert_not_reached ();
        break;
    }

  do
    status = g_io_channel_flush (gio_w, &local_error);
  while (status == G_IO_STATUS_AGAIN);

  if (status == G_IO_STATUS_ERROR)
    {
      /* Errors should not happen */
      g_assert_no_error (local_error);
      g_clear_error (&local_error);
    }

  g_test_message ("read %d bytes, wrote %ld bytes", rlength, wlength);

  g_io_channel_unref (gio_r);
  g_io_channel_unref (gio_w);

  test_small_writes ();

  g_free (filename);
  g_string_free (buffer, TRUE);
}

static void
test_read_line_embedded_nuls (void)
{
  const guint8 test_data[] = { 'H', 'i', '!', '\0', 'y', 'o', 'u', '\n', ':', ')', '\n' };
  gint fd;
  gchar *filename = NULL;
  GIOChannel *channel = NULL;
  GError *local_error = NULL;
  gchar *line = NULL;
  gsize line_length, terminator_pos;
  GIOStatus status;

  g_test_summary ("Test that reading a line containing embedded nuls works "
                  "when using non-standard line terminators.");

  /* Write out a temporary file. */
  fd = g_file_open_tmp ("glib-test-io-channel-XXXXXX", &filename, &local_error);
  g_assert_no_error (local_error);
  g_close (fd, NULL);
  fd = -1;

  g_file_set_contents (filename, (const gchar *) test_data, sizeof (test_data), &local_error);
  g_assert_no_error (local_error);

  /* Create the channel. */
  channel = g_io_channel_new_file (filename, "r", &local_error);
  g_assert_no_error (local_error);

  /* Only break on newline characters, not nuls.
   * Use length -1 here to exercise glib#2323; the case where length > 0
   * is covered in glib/tests/protocol.c. */
  g_io_channel_set_line_term (channel, "\n", -1);
  g_io_channel_set_encoding (channel, NULL, &local_error);
  g_assert_no_error (local_error);

  status = g_io_channel_read_line (channel, &line, &line_length,
                                   &terminator_pos, &local_error);
  g_assert_no_error (local_error);
  g_assert_cmpint (status, ==, G_IO_STATUS_NORMAL);
  g_assert_cmpuint (line_length, ==, 8);
  g_assert_cmpuint (terminator_pos, ==, 7);
  g_assert_cmpmem (line, line_length, test_data, 8);

  g_free (line);
  g_io_channel_unref (channel);
  g_free (filename);
}

int
main (int   argc,
      char *argv[])
{
  g_test_init (&argc, &argv, NULL);

  g_test_add_func ("/io-channel/read-write", test_read_write);
  g_test_add_func ("/io-channel/read-line/embedded-nuls", test_read_line_embedded_nuls);

  return g_test_run ();
}