Daniel P. Berrangé | 87e4276 | 2022-06-20 12:01:45 +0100 | [diff] [blame] | 1 | /* |
| 2 | * QEMU I/O channel null test |
| 3 | * |
| 4 | * Copyright (c) 2022 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.1 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 "qemu/osdep.h" |
| 22 | #include "io/channel-null.h" |
| 23 | #include "qapi/error.h" |
| 24 | |
| 25 | static gboolean test_io_channel_watch(QIOChannel *ioc, |
| 26 | GIOCondition condition, |
| 27 | gpointer opaque) |
| 28 | { |
| 29 | GIOCondition *gotcond = opaque; |
| 30 | *gotcond = condition; |
| 31 | return G_SOURCE_REMOVE; |
| 32 | } |
| 33 | |
| 34 | static void test_io_channel_null_io(void) |
| 35 | { |
| 36 | g_autoptr(QIOChannelNull) null = qio_channel_null_new(); |
| 37 | char buf[1024]; |
| 38 | GIOCondition gotcond = 0; |
| 39 | Error *local_err = NULL; |
| 40 | |
| 41 | g_assert(qio_channel_write(QIO_CHANNEL(null), |
| 42 | "Hello World", 11, |
| 43 | &error_abort) == 11); |
| 44 | |
| 45 | g_assert(qio_channel_read(QIO_CHANNEL(null), |
| 46 | buf, sizeof(buf), |
| 47 | &error_abort) == 0); |
| 48 | |
| 49 | qio_channel_add_watch(QIO_CHANNEL(null), |
| 50 | G_IO_IN, |
| 51 | test_io_channel_watch, |
| 52 | &gotcond, |
| 53 | NULL); |
| 54 | |
| 55 | g_main_context_iteration(NULL, false); |
| 56 | |
| 57 | g_assert(gotcond == G_IO_IN); |
| 58 | |
| 59 | qio_channel_add_watch(QIO_CHANNEL(null), |
| 60 | G_IO_IN | G_IO_OUT, |
| 61 | test_io_channel_watch, |
| 62 | &gotcond, |
| 63 | NULL); |
| 64 | |
| 65 | g_main_context_iteration(NULL, false); |
| 66 | |
| 67 | g_assert(gotcond == (G_IO_IN | G_IO_OUT)); |
| 68 | |
| 69 | qio_channel_close(QIO_CHANNEL(null), &error_abort); |
| 70 | |
| 71 | g_assert(qio_channel_write(QIO_CHANNEL(null), |
| 72 | "Hello World", 11, |
| 73 | &local_err) == -1); |
| 74 | g_assert_nonnull(local_err); |
| 75 | |
| 76 | g_clear_pointer(&local_err, error_free); |
| 77 | |
| 78 | g_assert(qio_channel_read(QIO_CHANNEL(null), |
| 79 | buf, sizeof(buf), |
| 80 | &local_err) == -1); |
| 81 | g_assert_nonnull(local_err); |
| 82 | |
| 83 | g_clear_pointer(&local_err, error_free); |
| 84 | } |
| 85 | |
| 86 | int main(int argc, char **argv) |
| 87 | { |
| 88 | module_call_init(MODULE_INIT_QOM); |
| 89 | |
| 90 | g_test_init(&argc, &argv, NULL); |
| 91 | |
| 92 | g_test_add_func("/io/channel/null/io", test_io_channel_null_io); |
| 93 | |
| 94 | return g_test_run(); |
| 95 | } |