]> git.proxmox.com Git - mirror_qemu.git/blame - tests/test-io-channel-file.c
Merge remote-tracking branch 'remotes/mst/tags/for_upstream' into staging
[mirror_qemu.git] / tests / test-io-channel-file.c
CommitLineData
d6e48869
DB
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
681c28a3 21#include "qemu/osdep.h"
d6e48869 22#include "io/channel-file.h"
c767ae62 23#include "io/channel-util.h"
d6e48869 24#include "io-channel-helpers.h"
da34e65c 25#include "qapi/error.h"
d6e48869 26
902f6e14
RL
27#define TEST_FILE "tests/test-io-channel-file.txt"
28#define TEST_MASK 0600
29
30static void test_io_channel_file_helper(int flags)
d6e48869
DB
31{
32 QIOChannel *src, *dst;
33 QIOChannelTest *test;
902f6e14
RL
34 struct stat st;
35 mode_t mask;
36 int ret;
d6e48869 37
d6e48869
DB
38 unlink(TEST_FILE);
39 src = QIO_CHANNEL(qio_channel_file_new_path(
40 TEST_FILE,
902f6e14 41 flags, TEST_MASK,
d6e48869
DB
42 &error_abort));
43 dst = QIO_CHANNEL(qio_channel_file_new_path(
44 TEST_FILE,
45 O_RDONLY | O_BINARY, 0,
46 &error_abort));
47
48 test = qio_channel_test_new();
49 qio_channel_test_run_writer(test, src);
50 qio_channel_test_run_reader(test, dst);
51 qio_channel_test_validate(test);
52
902f6e14
RL
53 /* Check that the requested mode took effect. */
54 mask = umask(0);
55 umask(mask);
56 ret = stat(TEST_FILE, &st);
57 g_assert_cmpint(ret, >, -1);
58 g_assert_cmpuint(TEST_MASK & ~mask, ==, st.st_mode & 0777);
59
d6e48869
DB
60 unlink(TEST_FILE);
61 object_unref(OBJECT(src));
62 object_unref(OBJECT(dst));
63}
64
902f6e14
RL
65static void test_io_channel_file(void)
66{
67 test_io_channel_file_helper(O_WRONLY | O_CREAT | O_TRUNC | O_BINARY);
68}
69
70static void test_io_channel_file_rdwr(void)
71{
72 test_io_channel_file_helper(O_RDWR | O_CREAT | O_TRUNC | O_BINARY);
73}
d6e48869 74
c767ae62
DB
75static void test_io_channel_fd(void)
76{
77 QIOChannel *ioc;
78 int fd = -1;
79
c767ae62
DB
80 fd = open(TEST_FILE, O_CREAT | O_TRUNC | O_WRONLY, 0600);
81 g_assert_cmpint(fd, >, -1);
82
83 ioc = qio_channel_new_fd(fd, &error_abort);
84
85 g_assert_cmpstr(object_get_typename(OBJECT(ioc)),
86 ==,
87 TYPE_QIO_CHANNEL_FILE);
88
89 unlink(TEST_FILE);
90 object_unref(OBJECT(ioc));
91}
92
93
d6e48869
DB
94#ifndef _WIN32
95static void test_io_channel_pipe(bool async)
96{
97 QIOChannel *src, *dst;
98 QIOChannelTest *test;
99 int fd[2];
100
101 if (pipe(fd) < 0) {
102 perror("pipe");
103 abort();
104 }
105
106 src = QIO_CHANNEL(qio_channel_file_new_fd(fd[1]));
107 dst = QIO_CHANNEL(qio_channel_file_new_fd(fd[0]));
108
109 test = qio_channel_test_new();
110 qio_channel_test_run_threads(test, async, src, dst);
111 qio_channel_test_validate(test);
112
113 object_unref(OBJECT(src));
114 object_unref(OBJECT(dst));
115}
116
117
118static void test_io_channel_pipe_async(void)
119{
120 test_io_channel_pipe(true);
121}
122
123static void test_io_channel_pipe_sync(void)
124{
125 test_io_channel_pipe(false);
126}
127#endif /* ! _WIN32 */
128
129
130int main(int argc, char **argv)
131{
132 module_call_init(MODULE_INIT_QOM);
133
134 g_test_init(&argc, &argv, NULL);
135
136 g_test_add_func("/io/channel/file", test_io_channel_file);
902f6e14 137 g_test_add_func("/io/channel/file/rdwr", test_io_channel_file_rdwr);
c767ae62 138 g_test_add_func("/io/channel/file/fd", test_io_channel_fd);
d6e48869
DB
139#ifndef _WIN32
140 g_test_add_func("/io/channel/pipe/sync", test_io_channel_pipe_sync);
141 g_test_add_func("/io/channel/pipe/async", test_io_channel_pipe_async);
142#endif
143 return g_test_run();
144}