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
27static void test_io_channel_file(void)
28{
29 QIOChannel *src, *dst;
30 QIOChannelTest *test;
31
32#define TEST_FILE "tests/test-io-channel-file.txt"
33 unlink(TEST_FILE);
34 src = QIO_CHANNEL(qio_channel_file_new_path(
35 TEST_FILE,
36 O_WRONLY | O_CREAT | O_TRUNC | O_BINARY, 0600,
37 &error_abort));
38 dst = QIO_CHANNEL(qio_channel_file_new_path(
39 TEST_FILE,
40 O_RDONLY | O_BINARY, 0,
41 &error_abort));
42
43 test = qio_channel_test_new();
44 qio_channel_test_run_writer(test, src);
45 qio_channel_test_run_reader(test, dst);
46 qio_channel_test_validate(test);
47
48 unlink(TEST_FILE);
49 object_unref(OBJECT(src));
50 object_unref(OBJECT(dst));
51}
52
53
54static void test_io_channel_fd(void)
55{
56 QIOChannel *ioc;
57 int fd = -1;
58
59#define TEST_FILE "tests/test-io-channel-file.txt"
60 fd = open(TEST_FILE, O_CREAT | O_TRUNC | O_WRONLY, 0600);
61 g_assert_cmpint(fd, >, -1);
62
63 ioc = qio_channel_new_fd(fd, &error_abort);
64
65 g_assert_cmpstr(object_get_typename(OBJECT(ioc)),
66 ==,
67 TYPE_QIO_CHANNEL_FILE);
68
69 unlink(TEST_FILE);
70 object_unref(OBJECT(ioc));
71}
72
73
74#ifndef _WIN32
75static void test_io_channel_pipe(bool async)
76{
77 QIOChannel *src, *dst;
78 QIOChannelTest *test;
79 int fd[2];
80
81 if (pipe(fd) < 0) {
82 perror("pipe");
83 abort();
84 }
85
86 src = QIO_CHANNEL(qio_channel_file_new_fd(fd[1]));
87 dst = QIO_CHANNEL(qio_channel_file_new_fd(fd[0]));
88
89 test = qio_channel_test_new();
90 qio_channel_test_run_threads(test, async, src, dst);
91 qio_channel_test_validate(test);
92
93 object_unref(OBJECT(src));
94 object_unref(OBJECT(dst));
95}
96
97
98static void test_io_channel_pipe_async(void)
99{
100 test_io_channel_pipe(true);
101}
102
103static void test_io_channel_pipe_sync(void)
104{
105 test_io_channel_pipe(false);
106}
107#endif
108
109
110int main(int argc, char **argv)
111{
112 module_call_init(MODULE_INIT_QOM);
113
114 g_test_init(&argc, &argv, NULL);
115
116 g_test_add_func("/io/channel/file", test_io_channel_file);
117 g_test_add_func("/io/channel/file/fd", test_io_channel_fd);
118#ifndef _WIN32
119 g_test_add_func("/io/channel/pipe/sync", test_io_channel_pipe_sync);
120 g_test_add_func("/io/channel/pipe/async", test_io_channel_pipe_async);
121#endif
122 return g_test_run();
123}
124