Daniel P. Berrange | d6e4886 | 2015-02-27 18:25:25 +0000 | [diff] [blame] | 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 |
Chetan Pant | c8198bd | 2020-10-14 13:40:33 +0000 | [diff] [blame] | 9 | * version 2.1 of the License, or (at your option) any later version. |
Daniel P. Berrange | d6e4886 | 2015-02-27 18:25:25 +0000 | [diff] [blame] | 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 | |
Peter Maydell | 681c28a | 2016-02-08 18:08:51 +0000 | [diff] [blame] | 21 | #include "qemu/osdep.h" |
Daniel P. Berrange | d6e4886 | 2015-02-27 18:25:25 +0000 | [diff] [blame] | 22 | #include "io/channel-file.h" |
Daniel P. Berrange | c767ae6 | 2016-02-03 14:00:28 +0000 | [diff] [blame] | 23 | #include "io/channel-util.h" |
Daniel P. Berrange | d6e4886 | 2015-02-27 18:25:25 +0000 | [diff] [blame] | 24 | #include "io-channel-helpers.h" |
Markus Armbruster | da34e65 | 2016-03-14 09:01:28 +0100 | [diff] [blame] | 25 | #include "qapi/error.h" |
Markus Armbruster | 0b8fa32 | 2019-05-23 16:35:07 +0200 | [diff] [blame] | 26 | #include "qemu/module.h" |
Daniel P. Berrange | d6e4886 | 2015-02-27 18:25:25 +0000 | [diff] [blame] | 27 | |
Ross Lagerwall | 902f6e1 | 2017-11-01 14:25:24 +0000 | [diff] [blame] | 28 | #define TEST_FILE "tests/test-io-channel-file.txt" |
| 29 | #define TEST_MASK 0600 |
| 30 | |
Yonggang Luo | a92a783 | 2020-09-16 01:12:27 +0800 | [diff] [blame] | 31 | /* |
| 32 | * On Windows the stat() function in the C library checks only |
| 33 | * the FAT-style READONLY attribute and does not look at the ACL at all. |
| 34 | */ |
| 35 | #ifdef _WIN32 |
| 36 | #define TEST_MASK_EXPECT 0700 |
| 37 | #else |
| 38 | #define TEST_MASK_EXPECT 0777 |
| 39 | #endif |
| 40 | |
Ross Lagerwall | 902f6e1 | 2017-11-01 14:25:24 +0000 | [diff] [blame] | 41 | static void test_io_channel_file_helper(int flags) |
Daniel P. Berrange | d6e4886 | 2015-02-27 18:25:25 +0000 | [diff] [blame] | 42 | { |
| 43 | QIOChannel *src, *dst; |
| 44 | QIOChannelTest *test; |
Ross Lagerwall | 902f6e1 | 2017-11-01 14:25:24 +0000 | [diff] [blame] | 45 | struct stat st; |
| 46 | mode_t mask; |
| 47 | int ret; |
Daniel P. Berrange | d6e4886 | 2015-02-27 18:25:25 +0000 | [diff] [blame] | 48 | |
Daniel P. Berrange | d6e4886 | 2015-02-27 18:25:25 +0000 | [diff] [blame] | 49 | unlink(TEST_FILE); |
| 50 | src = QIO_CHANNEL(qio_channel_file_new_path( |
| 51 | TEST_FILE, |
Ross Lagerwall | 902f6e1 | 2017-11-01 14:25:24 +0000 | [diff] [blame] | 52 | flags, TEST_MASK, |
Daniel P. Berrange | d6e4886 | 2015-02-27 18:25:25 +0000 | [diff] [blame] | 53 | &error_abort)); |
| 54 | dst = QIO_CHANNEL(qio_channel_file_new_path( |
| 55 | TEST_FILE, |
| 56 | O_RDONLY | O_BINARY, 0, |
| 57 | &error_abort)); |
| 58 | |
| 59 | test = qio_channel_test_new(); |
| 60 | qio_channel_test_run_writer(test, src); |
| 61 | qio_channel_test_run_reader(test, dst); |
| 62 | qio_channel_test_validate(test); |
| 63 | |
Ross Lagerwall | 902f6e1 | 2017-11-01 14:25:24 +0000 | [diff] [blame] | 64 | /* Check that the requested mode took effect. */ |
| 65 | mask = umask(0); |
| 66 | umask(mask); |
| 67 | ret = stat(TEST_FILE, &st); |
| 68 | g_assert_cmpint(ret, >, -1); |
Yonggang Luo | a92a783 | 2020-09-16 01:12:27 +0800 | [diff] [blame] | 69 | g_assert_cmpuint(TEST_MASK & ~mask, ==, st.st_mode & TEST_MASK_EXPECT); |
Ross Lagerwall | 902f6e1 | 2017-11-01 14:25:24 +0000 | [diff] [blame] | 70 | |
Daniel P. Berrange | d6e4886 | 2015-02-27 18:25:25 +0000 | [diff] [blame] | 71 | unlink(TEST_FILE); |
| 72 | object_unref(OBJECT(src)); |
| 73 | object_unref(OBJECT(dst)); |
| 74 | } |
| 75 | |
Ross Lagerwall | 902f6e1 | 2017-11-01 14:25:24 +0000 | [diff] [blame] | 76 | static void test_io_channel_file(void) |
| 77 | { |
| 78 | test_io_channel_file_helper(O_WRONLY | O_CREAT | O_TRUNC | O_BINARY); |
| 79 | } |
| 80 | |
| 81 | static void test_io_channel_file_rdwr(void) |
| 82 | { |
| 83 | test_io_channel_file_helper(O_RDWR | O_CREAT | O_TRUNC | O_BINARY); |
| 84 | } |
Daniel P. Berrange | d6e4886 | 2015-02-27 18:25:25 +0000 | [diff] [blame] | 85 | |
Daniel P. Berrange | c767ae6 | 2016-02-03 14:00:28 +0000 | [diff] [blame] | 86 | static void test_io_channel_fd(void) |
| 87 | { |
| 88 | QIOChannel *ioc; |
| 89 | int fd = -1; |
| 90 | |
Daniel P. Berrange | c767ae6 | 2016-02-03 14:00:28 +0000 | [diff] [blame] | 91 | fd = open(TEST_FILE, O_CREAT | O_TRUNC | O_WRONLY, 0600); |
| 92 | g_assert_cmpint(fd, >, -1); |
| 93 | |
| 94 | ioc = qio_channel_new_fd(fd, &error_abort); |
| 95 | |
| 96 | g_assert_cmpstr(object_get_typename(OBJECT(ioc)), |
| 97 | ==, |
| 98 | TYPE_QIO_CHANNEL_FILE); |
| 99 | |
| 100 | unlink(TEST_FILE); |
| 101 | object_unref(OBJECT(ioc)); |
| 102 | } |
| 103 | |
| 104 | |
Daniel P. Berrange | d6e4886 | 2015-02-27 18:25:25 +0000 | [diff] [blame] | 105 | #ifndef _WIN32 |
| 106 | static void test_io_channel_pipe(bool async) |
| 107 | { |
| 108 | QIOChannel *src, *dst; |
| 109 | QIOChannelTest *test; |
| 110 | int fd[2]; |
| 111 | |
| 112 | if (pipe(fd) < 0) { |
| 113 | perror("pipe"); |
| 114 | abort(); |
| 115 | } |
| 116 | |
| 117 | src = QIO_CHANNEL(qio_channel_file_new_fd(fd[1])); |
| 118 | dst = QIO_CHANNEL(qio_channel_file_new_fd(fd[0])); |
| 119 | |
| 120 | test = qio_channel_test_new(); |
| 121 | qio_channel_test_run_threads(test, async, src, dst); |
| 122 | qio_channel_test_validate(test); |
| 123 | |
| 124 | object_unref(OBJECT(src)); |
| 125 | object_unref(OBJECT(dst)); |
| 126 | } |
| 127 | |
| 128 | |
| 129 | static void test_io_channel_pipe_async(void) |
| 130 | { |
| 131 | test_io_channel_pipe(true); |
| 132 | } |
| 133 | |
| 134 | static void test_io_channel_pipe_sync(void) |
| 135 | { |
| 136 | test_io_channel_pipe(false); |
| 137 | } |
| 138 | #endif /* ! _WIN32 */ |
| 139 | |
| 140 | |
| 141 | int main(int argc, char **argv) |
| 142 | { |
| 143 | module_call_init(MODULE_INIT_QOM); |
| 144 | |
| 145 | g_test_init(&argc, &argv, NULL); |
| 146 | |
| 147 | g_test_add_func("/io/channel/file", test_io_channel_file); |
Ross Lagerwall | 902f6e1 | 2017-11-01 14:25:24 +0000 | [diff] [blame] | 148 | g_test_add_func("/io/channel/file/rdwr", test_io_channel_file_rdwr); |
Daniel P. Berrange | c767ae6 | 2016-02-03 14:00:28 +0000 | [diff] [blame] | 149 | g_test_add_func("/io/channel/file/fd", test_io_channel_fd); |
Daniel P. Berrange | d6e4886 | 2015-02-27 18:25:25 +0000 | [diff] [blame] | 150 | #ifndef _WIN32 |
| 151 | g_test_add_func("/io/channel/pipe/sync", test_io_channel_pipe_sync); |
| 152 | g_test_add_func("/io/channel/pipe/async", test_io_channel_pipe_async); |
| 153 | #endif |
| 154 | return g_test_run(); |
| 155 | } |