test-authz-list.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. /*
  2. * QEMU list file authorization object tests
  3. *
  4. * Copyright (c) 2018 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 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. #include "qemu/osdep.h"
  21. #include "authz/list.h"
  22. #include "qemu/module.h"
  23. static void test_authz_default_deny(void)
  24. {
  25. QAuthZList *auth = qauthz_list_new("auth0",
  26. QAUTHZ_LIST_POLICY_DENY,
  27. &error_abort);
  28. g_assert(!qauthz_is_allowed(QAUTHZ(auth), "fred", &error_abort));
  29. object_unparent(OBJECT(auth));
  30. }
  31. static void test_authz_default_allow(void)
  32. {
  33. QAuthZList *auth = qauthz_list_new("auth0",
  34. QAUTHZ_LIST_POLICY_ALLOW,
  35. &error_abort);
  36. g_assert(qauthz_is_allowed(QAUTHZ(auth), "fred", &error_abort));
  37. object_unparent(OBJECT(auth));
  38. }
  39. static void test_authz_explicit_deny(void)
  40. {
  41. QAuthZList *auth = qauthz_list_new("auth0",
  42. QAUTHZ_LIST_POLICY_ALLOW,
  43. &error_abort);
  44. qauthz_list_append_rule(auth, "fred", QAUTHZ_LIST_POLICY_DENY,
  45. QAUTHZ_LIST_FORMAT_EXACT, &error_abort);
  46. g_assert(!qauthz_is_allowed(QAUTHZ(auth), "fred", &error_abort));
  47. object_unparent(OBJECT(auth));
  48. }
  49. static void test_authz_explicit_allow(void)
  50. {
  51. QAuthZList *auth = qauthz_list_new("auth0",
  52. QAUTHZ_LIST_POLICY_DENY,
  53. &error_abort);
  54. qauthz_list_append_rule(auth, "fred", QAUTHZ_LIST_POLICY_ALLOW,
  55. QAUTHZ_LIST_FORMAT_EXACT, &error_abort);
  56. g_assert(qauthz_is_allowed(QAUTHZ(auth), "fred", &error_abort));
  57. object_unparent(OBJECT(auth));
  58. }
  59. static void test_authz_complex(void)
  60. {
  61. QAuthZList *auth = qauthz_list_new("auth0",
  62. QAUTHZ_LIST_POLICY_DENY,
  63. &error_abort);
  64. qauthz_list_append_rule(auth, "fred", QAUTHZ_LIST_POLICY_ALLOW,
  65. QAUTHZ_LIST_FORMAT_EXACT, &error_abort);
  66. qauthz_list_append_rule(auth, "bob", QAUTHZ_LIST_POLICY_ALLOW,
  67. QAUTHZ_LIST_FORMAT_EXACT, &error_abort);
  68. qauthz_list_append_rule(auth, "dan", QAUTHZ_LIST_POLICY_DENY,
  69. QAUTHZ_LIST_FORMAT_EXACT, &error_abort);
  70. qauthz_list_append_rule(auth, "dan*", QAUTHZ_LIST_POLICY_ALLOW,
  71. QAUTHZ_LIST_FORMAT_GLOB, &error_abort);
  72. g_assert(qauthz_is_allowed(QAUTHZ(auth), "fred", &error_abort));
  73. g_assert(qauthz_is_allowed(QAUTHZ(auth), "bob", &error_abort));
  74. g_assert(!qauthz_is_allowed(QAUTHZ(auth), "dan", &error_abort));
  75. g_assert(qauthz_is_allowed(QAUTHZ(auth), "danb", &error_abort));
  76. object_unparent(OBJECT(auth));
  77. }
  78. static void test_authz_add_remove(void)
  79. {
  80. QAuthZList *auth = qauthz_list_new("auth0",
  81. QAUTHZ_LIST_POLICY_ALLOW,
  82. &error_abort);
  83. g_assert_cmpint(qauthz_list_append_rule(auth, "fred",
  84. QAUTHZ_LIST_POLICY_ALLOW,
  85. QAUTHZ_LIST_FORMAT_EXACT,
  86. &error_abort),
  87. ==, 0);
  88. g_assert_cmpint(qauthz_list_append_rule(auth, "bob",
  89. QAUTHZ_LIST_POLICY_ALLOW,
  90. QAUTHZ_LIST_FORMAT_EXACT,
  91. &error_abort),
  92. ==, 1);
  93. g_assert_cmpint(qauthz_list_append_rule(auth, "dan",
  94. QAUTHZ_LIST_POLICY_DENY,
  95. QAUTHZ_LIST_FORMAT_EXACT,
  96. &error_abort),
  97. ==, 2);
  98. g_assert_cmpint(qauthz_list_append_rule(auth, "frank",
  99. QAUTHZ_LIST_POLICY_DENY,
  100. QAUTHZ_LIST_FORMAT_EXACT,
  101. &error_abort),
  102. ==, 3);
  103. g_assert(!qauthz_is_allowed(QAUTHZ(auth), "dan", &error_abort));
  104. g_assert_cmpint(qauthz_list_delete_rule(auth, "dan"),
  105. ==, 2);
  106. g_assert(qauthz_is_allowed(QAUTHZ(auth), "dan", &error_abort));
  107. g_assert_cmpint(qauthz_list_insert_rule(auth, "dan",
  108. QAUTHZ_LIST_POLICY_DENY,
  109. QAUTHZ_LIST_FORMAT_EXACT,
  110. 2,
  111. &error_abort),
  112. ==, 2);
  113. g_assert(!qauthz_is_allowed(QAUTHZ(auth), "dan", &error_abort));
  114. object_unparent(OBJECT(auth));
  115. }
  116. int main(int argc, char **argv)
  117. {
  118. g_test_init(&argc, &argv, NULL);
  119. module_call_init(MODULE_INIT_QOM);
  120. g_test_add_func("/auth/list/default/deny", test_authz_default_deny);
  121. g_test_add_func("/auth/list/default/allow", test_authz_default_allow);
  122. g_test_add_func("/auth/list/explicit/deny", test_authz_explicit_deny);
  123. g_test_add_func("/auth/list/explicit/allow", test_authz_explicit_allow);
  124. g_test_add_func("/auth/list/complex", test_authz_complex);
  125. g_test_add_func("/auth/list/add-remove", test_authz_add_remove);
  126. return g_test_run();
  127. }