1 | /* GLib testing framework examples and tests |
2 | * |
3 | * Copyright © 2020 Endless Mobile, Inc. |
4 | * |
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. |
9 | * |
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. |
14 | * |
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/>. |
17 | * |
18 | * Author: Philip Withnall <withnall@endlessm.com> |
19 | */ |
20 | |
21 | #include <glib.h> |
22 | #include <glib/gstdio.h> |
23 | |
24 | static void |
25 | test_read_line_embedded_nuls (void) |
26 | { |
27 | const guint8 test_data[] = { 'H', 'i', '!', '\0', 'y', 'o', 'u', '\n', ':', ')', '\n' }; |
28 | gint fd; |
29 | gchar *filename = NULL; |
30 | GIOChannel *channel = NULL; |
31 | GError *local_error = NULL; |
32 | gchar *line = NULL; |
33 | gsize line_length, terminator_pos; |
34 | GIOStatus status; |
35 | |
36 | g_test_summary (summary: "Test that reading a line containing embedded nuls works " |
37 | "when using non-standard line terminators." ); |
38 | |
39 | /* Write out a temporary file. */ |
40 | fd = g_file_open_tmp (tmpl: "glib-test-io-channel-XXXXXX" , name_used: &filename, error: &local_error); |
41 | g_assert_no_error (local_error); |
42 | g_close (fd, NULL); |
43 | fd = -1; |
44 | |
45 | g_file_set_contents (filename, contents: (const gchar *) test_data, length: sizeof (test_data), error: &local_error); |
46 | g_assert_no_error (local_error); |
47 | |
48 | /* Create the channel. */ |
49 | channel = g_io_channel_new_file (filename, mode: "r" , error: &local_error); |
50 | g_assert_no_error (local_error); |
51 | |
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, line_term: "\n" , length: -1); |
56 | g_io_channel_set_encoding (channel, NULL, error: &local_error); |
57 | g_assert_no_error (local_error); |
58 | |
59 | status = g_io_channel_read_line (channel, str_return: &line, length: &line_length, |
60 | terminator_pos: &terminator_pos, error: &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); |
66 | |
67 | g_free (mem: line); |
68 | g_io_channel_unref (channel); |
69 | g_free (mem: filename); |
70 | } |
71 | |
72 | int |
73 | main (int argc, |
74 | char *argv[]) |
75 | { |
76 | g_test_init (argc: &argc, argv: &argv, NULL); |
77 | |
78 | g_test_add_func (testpath: "/io-channel/read-line/embedded-nuls" , test_func: test_read_line_embedded_nuls); |
79 | |
80 | return g_test_run (); |
81 | } |
82 | |