1 /* GLib testing framework examples and tests
3 * Copyright © 2020 Endless Mobile, Inc.
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2.1 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General
16 * Public License along with this library; if not, see <http://www.gnu.org/licenses/>.
18 * Author: Philip Withnall <withnall@endlessm.com>
22 #include <glib/gstdio.h>
25 test_read_line_embedded_nuls (void)
27 const guint8 test_data[] = { 'H', 'i', '!', '\0', 'y', 'o', 'u', '\n', ':', ')', '\n' };
29 gchar *filename = NULL;
30 GIOChannel *channel = NULL;
31 GError *local_error = NULL;
33 gsize line_length, terminator_pos;
36 g_test_summary ("Test that reading a line containing embedded nuls works "
37 "when using non-standard line terminators.");
39 /* Write out a temporary file. */
40 fd = g_file_open_tmp ("glib-test-io-channel-XXXXXX", &filename, &local_error);
41 g_assert_no_error (local_error);
45 g_file_set_contents (filename, (const gchar *) test_data, sizeof (test_data), &local_error);
46 g_assert_no_error (local_error);
48 /* Create the channel. */
49 channel = g_io_channel_new_file (filename, "r", &local_error);
50 g_assert_no_error (local_error);
52 /* Only break on newline characters, not nuls.
53 * Use length -1 here to exercise glib#2323; the case where length > 0
54 * is covered in glib/tests/protocol.c. */
55 g_io_channel_set_line_term (channel, "\n", -1);
56 g_io_channel_set_encoding (channel, NULL, &local_error);
57 g_assert_no_error (local_error);
59 status = g_io_channel_read_line (channel, &line, &line_length,
60 &terminator_pos, &local_error);
61 g_assert_no_error (local_error);
62 g_assert_cmpint (status, ==, G_IO_STATUS_NORMAL);
63 g_assert_cmpuint (line_length, ==, 8);
64 g_assert_cmpuint (terminator_pos, ==, 7);
65 g_assert_cmpmem (line, line_length, test_data, 8);
68 g_io_channel_unref (channel);
76 g_test_init (&argc, &argv, NULL);
78 g_test_add_func ("/io-channel/read-line/embedded-nuls", test_read_line_embedded_nuls);