test-logging.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. /*
  2. * logging unit-tests
  3. *
  4. * Copyright (C) 2016 Linaro Ltd.
  5. *
  6. * Author: Alex Bennée <alex.bennee@linaro.org>
  7. *
  8. * Permission is hereby granted, free of charge, to any person obtaining a copy
  9. * of this software and associated documentation files (the "Software"), to deal
  10. * in the Software without restriction, including without limitation the rights
  11. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  12. * copies of the Software, and to permit persons to whom the Software is
  13. * furnished to do so, subject to the following conditions:
  14. *
  15. * The above copyright notice and this permission notice shall be included in
  16. * all copies or substantial portions of the Software.
  17. *
  18. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  19. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  20. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  21. * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  22. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  23. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  24. * THE SOFTWARE.
  25. */
  26. #include "qemu/osdep.h"
  27. #include <glib/gstdio.h>
  28. #include "qemu-common.h"
  29. #include "qapi/error.h"
  30. #include "qemu/log.h"
  31. static void test_parse_range(void)
  32. {
  33. Error *err = NULL;
  34. qemu_set_dfilter_ranges("0x1000+0x100", &error_abort);
  35. g_assert_false(qemu_log_in_addr_range(0xfff));
  36. g_assert(qemu_log_in_addr_range(0x1000));
  37. g_assert(qemu_log_in_addr_range(0x1001));
  38. g_assert(qemu_log_in_addr_range(0x10ff));
  39. g_assert_false(qemu_log_in_addr_range(0x1100));
  40. qemu_set_dfilter_ranges("0x1000-0x100", &error_abort);
  41. g_assert_false(qemu_log_in_addr_range(0x1001));
  42. g_assert(qemu_log_in_addr_range(0x1000));
  43. g_assert(qemu_log_in_addr_range(0x0f01));
  44. g_assert_false(qemu_log_in_addr_range(0x0f00));
  45. qemu_set_dfilter_ranges("0x1000..0x1100", &error_abort);
  46. g_assert_false(qemu_log_in_addr_range(0xfff));
  47. g_assert(qemu_log_in_addr_range(0x1000));
  48. g_assert(qemu_log_in_addr_range(0x1100));
  49. g_assert_false(qemu_log_in_addr_range(0x1101));
  50. qemu_set_dfilter_ranges("0x1000..0x1000", &error_abort);
  51. g_assert_false(qemu_log_in_addr_range(0xfff));
  52. g_assert(qemu_log_in_addr_range(0x1000));
  53. g_assert_false(qemu_log_in_addr_range(0x1001));
  54. qemu_set_dfilter_ranges("0x1000+0x100,0x2100-0x100,0x3000..0x3100",
  55. &error_abort);
  56. g_assert(qemu_log_in_addr_range(0x1050));
  57. g_assert(qemu_log_in_addr_range(0x2050));
  58. g_assert(qemu_log_in_addr_range(0x3050));
  59. qemu_set_dfilter_ranges("0xffffffffffffffff-1", &error_abort);
  60. g_assert(qemu_log_in_addr_range(UINT64_MAX));
  61. g_assert_false(qemu_log_in_addr_range(UINT64_MAX - 1));
  62. qemu_set_dfilter_ranges("0..0xffffffffffffffff", &err);
  63. g_assert(qemu_log_in_addr_range(0));
  64. g_assert(qemu_log_in_addr_range(UINT64_MAX));
  65. qemu_set_dfilter_ranges("2..1", &err);
  66. error_free_or_abort(&err);
  67. qemu_set_dfilter_ranges("0x1000+onehundred", &err);
  68. error_free_or_abort(&err);
  69. qemu_set_dfilter_ranges("0x1000+0", &err);
  70. error_free_or_abort(&err);
  71. }
  72. static void set_log_path_tmp(char const *dir, char const *tpl, Error **errp)
  73. {
  74. gchar *file_path = g_build_filename(dir, tpl, NULL);
  75. qemu_set_log_filename(file_path, errp);
  76. g_free(file_path);
  77. }
  78. static void test_parse_path(gconstpointer data)
  79. {
  80. gchar const *tmp_path = data;
  81. Error *err = NULL;
  82. set_log_path_tmp(tmp_path, "qemu.log", &error_abort);
  83. set_log_path_tmp(tmp_path, "qemu-%d.log", &error_abort);
  84. set_log_path_tmp(tmp_path, "qemu.log.%d", &error_abort);
  85. set_log_path_tmp(tmp_path, "qemu-%d%d.log", &err);
  86. error_free_or_abort(&err);
  87. }
  88. /* Remove a directory and all its entries (non-recursive). */
  89. static void rmdir_full(gchar const *root)
  90. {
  91. GDir *root_gdir = g_dir_open(root, 0, NULL);
  92. gchar const *entry_name;
  93. g_assert_nonnull(root_gdir);
  94. while ((entry_name = g_dir_read_name(root_gdir)) != NULL) {
  95. gchar *entry_path = g_build_filename(root, entry_name, NULL);
  96. g_assert(g_remove(entry_path) == 0);
  97. g_free(entry_path);
  98. }
  99. g_dir_close(root_gdir);
  100. g_assert(g_rmdir(root) == 0);
  101. }
  102. int main(int argc, char **argv)
  103. {
  104. gchar *tmp_path = g_dir_make_tmp("qemu-test-logging.XXXXXX", NULL);
  105. int rc;
  106. g_test_init(&argc, &argv, NULL);
  107. g_assert_nonnull(tmp_path);
  108. g_test_add_func("/logging/parse_range", test_parse_range);
  109. g_test_add_data_func("/logging/parse_path", tmp_path, test_parse_path);
  110. rc = g_test_run();
  111. rmdir_full(tmp_path);
  112. g_free(tmp_path);
  113. return rc;
  114. }