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