]> git.proxmox.com Git - mirror_qemu.git/blob - tests/test-io-channel-file.c
io: convert QIOChannelBuffer to use uint8_t instead of char
[mirror_qemu.git] / tests / test-io-channel-file.c
1 /*
2 * QEMU I/O channel file test
3 *
4 * Copyright (c) 2015 Red Hat, Inc.
5 *
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
10 *
11 * This library 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. See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
18 *
19 */
20
21 #include "io/channel-file.h"
22 #include "io/channel-util.h"
23 #include "io-channel-helpers.h"
24
25
26 static void test_io_channel_file(void)
27 {
28 QIOChannel *src, *dst;
29 QIOChannelTest *test;
30
31 #define TEST_FILE "tests/test-io-channel-file.txt"
32 unlink(TEST_FILE);
33 src = QIO_CHANNEL(qio_channel_file_new_path(
34 TEST_FILE,
35 O_WRONLY | O_CREAT | O_TRUNC | O_BINARY, 0600,
36 &error_abort));
37 dst = QIO_CHANNEL(qio_channel_file_new_path(
38 TEST_FILE,
39 O_RDONLY | O_BINARY, 0,
40 &error_abort));
41
42 test = qio_channel_test_new();
43 qio_channel_test_run_writer(test, src);
44 qio_channel_test_run_reader(test, dst);
45 qio_channel_test_validate(test);
46
47 unlink(TEST_FILE);
48 object_unref(OBJECT(src));
49 object_unref(OBJECT(dst));
50 }
51
52
53 static void test_io_channel_fd(void)
54 {
55 QIOChannel *ioc;
56 int fd = -1;
57
58 #define TEST_FILE "tests/test-io-channel-file.txt"
59 fd = open(TEST_FILE, O_CREAT | O_TRUNC | O_WRONLY, 0600);
60 g_assert_cmpint(fd, >, -1);
61
62 ioc = qio_channel_new_fd(fd, &error_abort);
63
64 g_assert_cmpstr(object_get_typename(OBJECT(ioc)),
65 ==,
66 TYPE_QIO_CHANNEL_FILE);
67
68 unlink(TEST_FILE);
69 object_unref(OBJECT(ioc));
70 }
71
72
73 #ifndef _WIN32
74 static void test_io_channel_pipe(bool async)
75 {
76 QIOChannel *src, *dst;
77 QIOChannelTest *test;
78 int fd[2];
79
80 if (pipe(fd) < 0) {
81 perror("pipe");
82 abort();
83 }
84
85 src = QIO_CHANNEL(qio_channel_file_new_fd(fd[1]));
86 dst = QIO_CHANNEL(qio_channel_file_new_fd(fd[0]));
87
88 test = qio_channel_test_new();
89 qio_channel_test_run_threads(test, async, src, dst);
90 qio_channel_test_validate(test);
91
92 object_unref(OBJECT(src));
93 object_unref(OBJECT(dst));
94 }
95
96
97 static void test_io_channel_pipe_async(void)
98 {
99 test_io_channel_pipe(true);
100 }
101
102 static void test_io_channel_pipe_sync(void)
103 {
104 test_io_channel_pipe(false);
105 }
106 #endif /* ! _WIN32 */
107
108
109 int main(int argc, char **argv)
110 {
111 module_call_init(MODULE_INIT_QOM);
112
113 g_test_init(&argc, &argv, NULL);
114
115 g_test_add_func("/io/channel/file", test_io_channel_file);
116 g_test_add_func("/io/channel/file/fd", test_io_channel_fd);
117 #ifndef _WIN32
118 g_test_add_func("/io/channel/pipe/sync", test_io_channel_pipe_sync);
119 g_test_add_func("/io/channel/pipe/async", test_io_channel_pipe_async);
120 #endif
121 return g_test_run();
122 }