qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
From: "Marc-André Lureau" <marcandre.lureau@redhat.com>
To: qemu-devel@nongnu.org
Cc: pbonzini@redhat.com, berrange@redhat.com,
	imbrenda@linux.vnet.ibm.com,
	"Marc-André Lureau" <marcandre.lureau@redhat.com>
Subject: [Qemu-devel] [PATCH 9/9] tests: start chardev unit tests
Date: Thu, 13 Oct 2016 15:14:49 +0400	[thread overview]
Message-ID: <20161013111449.29387-10-marcandre.lureau@redhat.com> (raw)
In-Reply-To: <20161013111449.29387-1-marcandre.lureau@redhat.com>

Signed-off-by: Marc-André Lureau <marcandre.lureau@redhat.com>
---
 tests/test-char.c      | 238 +++++++++++++++++++++++++++++++++++++++++++++++++
 tests/Makefile.include |   4 +
 2 files changed, 242 insertions(+)
 create mode 100644 tests/test-char.c

diff --git a/tests/test-char.c b/tests/test-char.c
new file mode 100644
index 0000000..fe1ccf2
--- /dev/null
+++ b/tests/test-char.c
@@ -0,0 +1,238 @@
+#include "qemu/osdep.h"
+
+#include "qemu-common.h"
+#include "qemu/config-file.h"
+#include "sysemu/char.h"
+#include "sysemu/sysemu.h"
+#include "qapi/error.h"
+#include "qmp-commands.h"
+
+typedef struct FeHandler {
+    char read_buf[128];
+    int read_count;
+    int last_event;
+} FeHandler;
+
+static int fe_can_read(void *opaque)
+{
+    FeHandler *h = opaque;
+
+    return sizeof(h->read_buf) - h->read_count;
+}
+
+static void fe_read(void *opaque, const uint8_t *buf, int size)
+{
+    FeHandler *h = opaque;
+
+    g_assert_cmpint(size, <=, fe_can_read(opaque));
+
+    memcpy(h->read_buf + h->read_count, buf, size);
+    h->read_count += size;
+}
+
+static void fe_event(void *opaque, int event)
+{
+    FeHandler *h = opaque;
+
+    h->last_event = event;
+}
+
+#ifdef CONFIG_HAS_GLIB_SUBPROCESS_TESTS
+static void char_stdio_test_subprocess(void)
+{
+    CharDriverState *chr;
+    int ret;
+
+    chr = qemu_chr_new("label", "stdio", NULL);
+    g_assert_nonnull(chr);
+
+    qemu_chr_fe_set_open(chr, true);
+    ret = qemu_chr_fe_write(chr, (void *)"buf", 4);
+    g_assert_cmpint(ret, ==, 4);
+
+    qemu_chr_delete(chr);
+}
+
+static void char_stdio_test(void)
+{
+    g_test_trap_subprocess("/char/stdio/subprocess", 0, 0);
+    g_test_trap_assert_passed();
+    g_test_trap_assert_stdout("buf");
+}
+#endif
+
+
+static void char_ringbuf_test(void)
+{
+    QemuOpts *opts;
+    CharDriverState *chr;
+    char *data;
+    int ret;
+
+    opts = qemu_opts_create(qemu_find_opts("chardev"), "ringbuf-label",
+                            1, &error_abort);
+    qemu_opt_set(opts, "backend", "ringbuf", &error_abort);
+
+    qemu_opt_set(opts, "size", "5", &error_abort);
+    chr = qemu_chr_new_from_opts(opts, NULL, NULL);
+    g_assert_null(chr);
+    qemu_opts_del(opts);
+
+    opts = qemu_opts_create(qemu_find_opts("chardev"), "ringbuf-label",
+                            1, &error_abort);
+    qemu_opt_set(opts, "backend", "ringbuf", &error_abort);
+    qemu_opt_set(opts, "size", "2", &error_abort);
+    chr = qemu_chr_new_from_opts(opts, NULL, &error_abort);
+    g_assert_nonnull(chr);
+    qemu_opts_del(opts);
+
+    ret = qemu_chr_fe_write(chr, (void *)"buff", 4);
+    g_assert_cmpint(ret, ==, 4);
+
+    data = qmp_ringbuf_read("ringbuf-label", 4, false, 0, &error_abort);
+    g_assert_cmpstr(data, ==, "ff");
+    g_free(data);
+
+    data = qmp_ringbuf_read("ringbuf-label", 4, false, 0, &error_abort);
+    g_assert_cmpstr(data, ==, "");
+    g_free(data);
+
+    qemu_chr_delete(chr);
+}
+
+static void char_mux_test(void)
+{
+    QemuOpts *opts;
+    CharDriverState *chr, *base;
+    char *data;
+    int tag1, tag2;
+    FeHandler h1 = { 0, }, h2 = { 0, };
+
+    opts = qemu_opts_create(qemu_find_opts("chardev"), "mux-label",
+                            1, &error_abort);
+    qemu_opt_set(opts, "backend", "ringbuf", &error_abort);
+    qemu_opt_set(opts, "size", "128", &error_abort);
+    qemu_opt_set(opts, "mux", "on", &error_abort);
+    chr = qemu_chr_new_from_opts(opts, NULL, &error_abort);
+    g_assert_nonnull(chr);
+    qemu_opts_del(opts);
+
+    tag1 = qemu_chr_add_handlers(chr,
+                                 fe_can_read,
+                                 fe_read,
+                                 fe_event,
+                                 &h1,
+                                 NULL,
+                                 &error_abort);
+    g_assert_cmpint(tag1, !=, -1);
+
+    tag2 = qemu_chr_add_handlers(chr,
+                                 fe_can_read,
+                                 fe_read,
+                                 fe_event,
+                                 &h2,
+                                 NULL,
+                                 &error_abort);
+    g_assert_cmpint(tag2, !=, -1);
+
+    g_assert_cmpint(qemu_chr_be_can_write(chr), !=, 0);
+
+    base = qemu_chr_find("mux-label-base");
+
+    /* the last handler has the focus */
+    qemu_chr_be_write(base, (void *)"hello", 6);
+    g_assert_cmpint(h1.read_count, ==, 0);
+    g_assert_cmpint(h2.read_count, ==, 6);
+    g_assert_cmpstr(h2.read_buf, ==, "hello");
+    h2.read_count = 0;
+
+    /* switch focus */
+    qemu_chr_be_write(base, (void *)"\1c", 2);
+
+    qemu_chr_be_write(base, (void *)"hello", 6);
+    g_assert_cmpint(h2.read_count, ==, 0);
+    g_assert_cmpint(h1.read_count, ==, 6);
+    g_assert_cmpstr(h1.read_buf, ==, "hello");
+    h1.read_count = 0;
+
+    /* remove first handler */
+    qemu_chr_remove_handlers(chr, tag1);
+    qemu_chr_be_write(base, (void *)"hello", 6);
+    g_assert_cmpint(h1.read_count, ==, 0);
+    g_assert_cmpint(h2.read_count, ==, 0);
+
+    qemu_chr_be_write(base, (void *)"\1c", 2);
+    qemu_chr_be_write(base, (void *)"hello", 6);
+    g_assert_cmpint(h1.read_count, ==, 0);
+    g_assert_cmpint(h2.read_count, ==, 6);
+    g_assert_cmpstr(h2.read_buf, ==, "hello");
+    h2.read_count = 0;
+
+    /* print help */
+    qemu_chr_be_write(base, (void *)"\1?", 2);
+    data = qmp_ringbuf_read("mux-label-base", 128, false, 0, &error_abort);
+    g_assert_cmpint(strlen(data), !=, 0);
+    g_free(data);
+
+    qemu_chr_remove_handlers(chr, tag1);
+    qemu_chr_remove_handlers(chr, tag2);
+    qemu_chr_delete(chr);
+}
+
+static void char_null_test(void)
+{
+    CharDriverState *chr;
+    int ret, tag;
+
+    chr = qemu_chr_find("label-null");
+    g_assert_null(chr);
+
+    chr = qemu_chr_new("label-null", "null", NULL);
+    chr = qemu_chr_find("label-null");
+    g_assert_nonnull(chr);
+
+    qemu_chr_fe_claim_no_fail(chr);
+    ret = qemu_chr_fe_claim(chr);
+    g_assert_cmpint(ret, ==, -1);
+
+    g_assert(qemu_chr_has_feature(chr,
+                 QEMU_CHAR_FEATURE_FD_PASS) == false);
+    g_assert(qemu_chr_has_feature(chr,
+                 QEMU_CHAR_FEATURE_RECONNECTABLE) == false);
+
+    qemu_chr_fe_set_open(chr, true);
+
+    tag = qemu_chr_add_handlers(chr,
+                                fe_can_read,
+                                fe_read,
+                                fe_event,
+                                NULL,
+                                NULL,
+                                &error_abort);
+    g_assert_cmpint(tag, !=, -1);
+
+    ret = qemu_chr_fe_write(chr, (void *)"buf", 4);
+    g_assert_cmpint(ret, ==, 4);
+
+    qemu_chr_remove_handlers(chr, tag);
+    qemu_chr_fe_release(chr);
+    qemu_chr_delete(chr);
+}
+
+int main(int argc, char **argv)
+{
+    g_test_init(&argc, &argv, NULL);
+
+    module_call_init(MODULE_INIT_QOM);
+    qemu_add_opts(&qemu_chardev_opts);
+
+    g_test_add_func("/char/null", char_null_test);
+    g_test_add_func("/char/ringbuf", char_ringbuf_test);
+    g_test_add_func("/char/mux", char_mux_test);
+#ifdef CONFIG_HAS_GLIB_SUBPROCESS_TESTS
+    g_test_add_func("/char/stdio/subprocess", char_stdio_test_subprocess);
+    g_test_add_func("/char/stdio", char_stdio_test);
+#endif
+
+    return g_test_run();
+}
diff --git a/tests/Makefile.include b/tests/Makefile.include
index a77777c..c42eca5 100644
--- a/tests/Makefile.include
+++ b/tests/Makefile.include
@@ -8,6 +8,8 @@ SYSEMU_TARGET_LIST := $(subst -softmmu.mak,,$(notdir \
 
 check-unit-y = tests/check-qdict$(EXESUF)
 gcov-files-check-qdict-y = qobject/qdict.c
+check-unit-y = tests/test-char$(EXESUF)
+gcov-files-check-qdict-y = qemu-char.c
 check-unit-y += tests/check-qfloat$(EXESUF)
 gcov-files-check-qfloat-y = qobject/qfloat.c
 check-unit-y += tests/check-qint$(EXESUF)
@@ -479,6 +481,8 @@ tests/check-qnull$(EXESUF): tests/check-qnull.o $(test-util-obj-y)
 tests/check-qjson$(EXESUF): tests/check-qjson.o $(test-util-obj-y)
 tests/check-qom-interface$(EXESUF): tests/check-qom-interface.o $(test-qom-obj-y)
 tests/check-qom-proplist$(EXESUF): tests/check-qom-proplist.o $(test-qom-obj-y)
+
+tests/test-char$(EXESUF): tests/test-char.o qemu-char.o qemu-timer.o $(test-util-obj-y) $(qtest-obj-y) $(test-io-obj-y)
 tests/test-coroutine$(EXESUF): tests/test-coroutine.o $(test-block-obj-y)
 tests/test-aio$(EXESUF): tests/test-aio.o $(test-block-obj-y)
 tests/test-rfifolock$(EXESUF): tests/test-rfifolock.o $(test-util-obj-y)
-- 
2.10.0

  parent reply	other threads:[~2016-10-13 11:15 UTC|newest]

Thread overview: 14+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2016-10-13 11:14 [Qemu-devel] [PATCH 0/9] Fix mux regression (commit 949055a2) Marc-André Lureau
2016-10-13 11:14 ` [Qemu-devel] [PATCH 1/9] Revert "char: use a fixed idx for child muxed chr" Marc-André Lureau
2016-10-13 11:14 ` [Qemu-devel] [PATCH 2/9] char: return a tag when adding the fe handlers Marc-André Lureau
2016-10-13 11:14 ` [Qemu-devel] [PATCH 3/9] char: add qemu_chr_remove_handlers() Marc-André Lureau
2016-10-13 11:14 ` [Qemu-devel] [PATCH 4/9] char: keep track of qemu_chr_add_handlers() Marc-André Lureau
2016-10-13 11:14 ` [Qemu-devel] [PATCH 5/9] char: warn on unused qemu_chr_add_handlers() result Marc-André Lureau
2016-10-13 11:14 ` [Qemu-devel] [PATCH 6/9] qdev: remove call to qemu_chr_add_handlers() Marc-André Lureau
2016-10-13 11:14 ` [Qemu-devel] [PATCH 7/9] char: handle qemu_chr_add_handlers() error Marc-André Lureau
2016-10-13 11:14 ` [Qemu-devel] [PATCH 8/9] ringbuf: fix chr_write return value Marc-André Lureau
2016-10-13 11:14 ` Marc-André Lureau [this message]
2016-10-13 11:36 ` [Qemu-devel] [PATCH 0/9] Fix mux regression (commit 949055a2) Paolo Bonzini
2016-10-13 11:50   ` Marc-André Lureau
2016-10-13 11:58     ` Paolo Bonzini
2016-10-13 13:27 ` Peter Maydell

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20161013111449.29387-10-marcandre.lureau@redhat.com \
    --to=marcandre.lureau@redhat.com \
    --cc=berrange@redhat.com \
    --cc=imbrenda@linux.vnet.ibm.com \
    --cc=pbonzini@redhat.com \
    --cc=qemu-devel@nongnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).