summaryrefslogtreecommitdiffstats
path: root/tests/unit/test-logging.c
diff options
context:
space:
mode:
authorPeter Maydell2021-03-14 16:13:53 +0100
committerPeter Maydell2021-03-14 16:13:53 +0100
commit757acb9a8295e8be4a37b2cfc1cd947e357fd29c (patch)
tree881fdcb812a8b8d067d5cb59832b3bb31ce9bcf9 /tests/unit/test-logging.c
parentMerge remote-tracking branch 'remotes/pmaydell/tags/pull-target-arm-20210314'... (diff)
parentREADME: Add Documentation blurb (diff)
downloadqemu-757acb9a8295e8be4a37b2cfc1cd947e357fd29c.tar.gz
qemu-757acb9a8295e8be4a37b2cfc1cd947e357fd29c.tar.xz
qemu-757acb9a8295e8be4a37b2cfc1cd947e357fd29c.zip
Merge remote-tracking branch 'remotes/thuth-gitlab/tags/pull-request-2021-03-12' into staging
* Move unit and bench tests into separate directories * Clean-up and improve gitlab-ci jobs * Drop the non-working "check-speed" makefile target * Minor documentation updates # gpg: Signature made Fri 12 Mar 2021 17:18:45 GMT # gpg: using RSA key 27B88847EEE0250118F3EAB92ED9D774FE702DB5 # gpg: issuer "thuth@redhat.com" # gpg: Good signature from "Thomas Huth <th.huth@gmx.de>" [full] # gpg: aka "Thomas Huth <thuth@redhat.com>" [full] # gpg: aka "Thomas Huth <huth@tuxfamily.org>" [full] # gpg: aka "Thomas Huth <th.huth@posteo.de>" [unknown] # Primary key fingerprint: 27B8 8847 EEE0 2501 18F3 EAB9 2ED9 D774 FE70 2DB5 * remotes/thuth-gitlab/tags/pull-request-2021-03-12: README: Add Documentation blurb MAINTAINERS: Merge the Gitlab-CI section into the generic CI section tests: remove "make check-speed" in favor of "make bench" gitlab-ci.yml: Merge check-crypto-old jobs into the build-crypto-old jobs gitlab-ci.yml: Merge one of the coroutine jobs with the tcg-disabled job gitlab-ci.yml: Add some missing dependencies to the jobs gitlab-ci.yml: Move build-tools-and-docs-debian to a better place tests: Move benchmarks into a separate folder tests: Move unit tests into a separate directory Signed-off-by: Peter Maydell <peter.maydell@linaro.org>
Diffstat (limited to 'tests/unit/test-logging.c')
-rw-r--r--tests/unit/test-logging.c218
1 files changed, 218 insertions, 0 deletions
diff --git a/tests/unit/test-logging.c b/tests/unit/test-logging.c
new file mode 100644
index 0000000000..ccb819f193
--- /dev/null
+++ b/tests/unit/test-logging.c
@@ -0,0 +1,218 @@
+/*
+ * logging unit-tests
+ *
+ * Copyright (C) 2016 Linaro Ltd.
+ *
+ * Author: Alex Bennée <alex.bennee@linaro.org>
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
+ * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ */
+
+#include "qemu/osdep.h"
+#include <glib/gstdio.h>
+
+#include "qemu-common.h"
+#include "qapi/error.h"
+#include "qemu/log.h"
+
+static void test_parse_range(void)
+{
+ Error *err = NULL;
+
+ qemu_set_dfilter_ranges("0x1000+0x100", &error_abort);
+
+ g_assert_false(qemu_log_in_addr_range(0xfff));
+ g_assert(qemu_log_in_addr_range(0x1000));
+ g_assert(qemu_log_in_addr_range(0x1001));
+ g_assert(qemu_log_in_addr_range(0x10ff));
+ g_assert_false(qemu_log_in_addr_range(0x1100));
+
+ qemu_set_dfilter_ranges("0x1000-0x100", &error_abort);
+
+ g_assert_false(qemu_log_in_addr_range(0x1001));
+ g_assert(qemu_log_in_addr_range(0x1000));
+ g_assert(qemu_log_in_addr_range(0x0f01));
+ g_assert_false(qemu_log_in_addr_range(0x0f00));
+
+ qemu_set_dfilter_ranges("0x1000..0x1100", &error_abort);
+
+ g_assert_false(qemu_log_in_addr_range(0xfff));
+ g_assert(qemu_log_in_addr_range(0x1000));
+ g_assert(qemu_log_in_addr_range(0x1100));
+ g_assert_false(qemu_log_in_addr_range(0x1101));
+
+ qemu_set_dfilter_ranges("0x1000..0x1000", &error_abort);
+
+ g_assert_false(qemu_log_in_addr_range(0xfff));
+ g_assert(qemu_log_in_addr_range(0x1000));
+ g_assert_false(qemu_log_in_addr_range(0x1001));
+
+ qemu_set_dfilter_ranges("0x1000+0x100,0x2100-0x100,0x3000..0x3100",
+ &error_abort);
+ g_assert(qemu_log_in_addr_range(0x1050));
+ g_assert(qemu_log_in_addr_range(0x2050));
+ g_assert(qemu_log_in_addr_range(0x3050));
+
+ qemu_set_dfilter_ranges("0xffffffffffffffff-1", &error_abort);
+ g_assert(qemu_log_in_addr_range(UINT64_MAX));
+ g_assert_false(qemu_log_in_addr_range(UINT64_MAX - 1));
+
+ qemu_set_dfilter_ranges("0..0xffffffffffffffff", &error_abort);
+ g_assert(qemu_log_in_addr_range(0));
+ g_assert(qemu_log_in_addr_range(UINT64_MAX));
+
+ qemu_set_dfilter_ranges("2..1", &err);
+ error_free_or_abort(&err);
+
+ qemu_set_dfilter_ranges("0x1000+onehundred", &err);
+ error_free_or_abort(&err);
+
+ qemu_set_dfilter_ranges("0x1000+0", &err);
+ error_free_or_abort(&err);
+}
+
+static void set_log_path_tmp(char const *dir, char const *tpl, Error **errp)
+{
+ gchar *file_path = g_build_filename(dir, tpl, NULL);
+
+ qemu_set_log_filename(file_path, errp);
+ g_free(file_path);
+}
+
+static void test_parse_path(gconstpointer data)
+{
+ gchar const *tmp_path = data;
+ Error *err = NULL;
+
+ set_log_path_tmp(tmp_path, "qemu.log", &error_abort);
+ set_log_path_tmp(tmp_path, "qemu-%d.log", &error_abort);
+ set_log_path_tmp(tmp_path, "qemu.log.%d", &error_abort);
+
+ set_log_path_tmp(tmp_path, "qemu-%d%d.log", &err);
+ error_free_or_abort(&err);
+}
+
+static void test_logfile_write(gconstpointer data)
+{
+ QemuLogFile *logfile;
+ QemuLogFile *logfile2;
+ gchar const *dir = data;
+ g_autofree gchar *file_path = NULL;
+ g_autofree gchar *file_path1 = NULL;
+ FILE *orig_fd;
+
+ /*
+ * Before starting test, set log flags, to ensure the file gets
+ * opened below with the call to qemu_set_log_filename().
+ * In cases where a logging backend other than log is used,
+ * this is needed.
+ */
+ qemu_set_log(CPU_LOG_TB_OUT_ASM);
+ file_path = g_build_filename(dir, "qemu_test_log_write0.log", NULL);
+ file_path1 = g_build_filename(dir, "qemu_test_log_write1.log", NULL);
+
+ /*
+ * Test that even if an open file handle is changed,
+ * our handle remains valid due to RCU.
+ */
+ qemu_set_log_filename(file_path, &error_abort);
+ rcu_read_lock();
+ logfile = qatomic_rcu_read(&qemu_logfile);
+ orig_fd = logfile->fd;
+ g_assert(logfile && logfile->fd);
+ fprintf(logfile->fd, "%s 1st write to file\n", __func__);
+ fflush(logfile->fd);
+
+ /* Change the logfile and ensure that the handle is still valid. */
+ qemu_set_log_filename(file_path1, &error_abort);
+ logfile2 = qatomic_rcu_read(&qemu_logfile);
+ g_assert(logfile->fd == orig_fd);
+ g_assert(logfile2->fd != logfile->fd);
+ fprintf(logfile->fd, "%s 2nd write to file\n", __func__);
+ fflush(logfile->fd);
+ rcu_read_unlock();
+}
+
+static void test_logfile_lock(gconstpointer data)
+{
+ FILE *logfile;
+ gchar const *dir = data;
+ g_autofree gchar *file_path = NULL;
+
+ file_path = g_build_filename(dir, "qemu_test_logfile_lock0.log", NULL);
+
+ /*
+ * Test the use of the logfile lock, such
+ * that even if an open file handle is closed,
+ * our handle remains valid for use due to RCU.
+ */
+ qemu_set_log_filename(file_path, &error_abort);
+ logfile = qemu_log_lock();
+ g_assert(logfile);
+ fprintf(logfile, "%s 1st write to file\n", __func__);
+ fflush(logfile);
+
+ /*
+ * Initiate a close file and make sure our handle remains
+ * valid since we still have the logfile lock.
+ */
+ qemu_log_close();
+ fprintf(logfile, "%s 2nd write to file\n", __func__);
+ fflush(logfile);
+ qemu_log_unlock(logfile);
+}
+
+/* Remove a directory and all its entries (non-recursive). */
+static void rmdir_full(gchar const *root)
+{
+ GDir *root_gdir = g_dir_open(root, 0, NULL);
+ gchar const *entry_name;
+
+ g_assert_nonnull(root_gdir);
+ while ((entry_name = g_dir_read_name(root_gdir)) != NULL) {
+ gchar *entry_path = g_build_filename(root, entry_name, NULL);
+ g_assert(g_remove(entry_path) == 0);
+ g_free(entry_path);
+ }
+ g_dir_close(root_gdir);
+ g_assert(g_rmdir(root) == 0);
+}
+
+int main(int argc, char **argv)
+{
+ g_autofree gchar *tmp_path = g_dir_make_tmp("qemu-test-logging.XXXXXX", NULL);
+ int rc;
+
+ g_test_init(&argc, &argv, NULL);
+ g_assert_nonnull(tmp_path);
+
+ g_test_add_func("/logging/parse_range", test_parse_range);
+ g_test_add_data_func("/logging/parse_path", tmp_path, test_parse_path);
+ g_test_add_data_func("/logging/logfile_write_path",
+ tmp_path, test_logfile_write);
+ g_test_add_data_func("/logging/logfile_lock_path",
+ tmp_path, test_logfile_lock);
+
+ rc = g_test_run();
+ qemu_log_close();
+ drain_call_rcu();
+
+ rmdir_full(tmp_path);
+ return rc;
+}