2
0

test-authz-listfile.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. /*
  2. * QEMU list 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 "qemu/main-loop.h"
  22. #include "qemu/module.h"
  23. #include "authz/listfile.h"
  24. static char *workdir;
  25. static gchar *qemu_authz_listfile_test_save(const gchar *name,
  26. const gchar *cfg)
  27. {
  28. gchar *path = g_strdup_printf("%s/default-deny.cfg", workdir);
  29. GError *gerr = NULL;
  30. if (!g_file_set_contents(path, cfg, -1, &gerr)) {
  31. g_printerr("Unable to save config %s: %s\n",
  32. path, gerr->message);
  33. g_error_free(gerr);
  34. g_free(path);
  35. rmdir(workdir);
  36. abort();
  37. }
  38. return path;
  39. }
  40. static void test_authz_default_deny(void)
  41. {
  42. gchar *file = qemu_authz_listfile_test_save(
  43. "default-deny.cfg",
  44. "{ \"policy\": \"deny\" }");
  45. Error *local_err = NULL;
  46. QAuthZListFile *auth = qauthz_list_file_new("auth0",
  47. file, false,
  48. &local_err);
  49. unlink(file);
  50. g_free(file);
  51. g_assert(local_err == NULL);
  52. g_assert(!qauthz_is_allowed(QAUTHZ(auth), "fred", &error_abort));
  53. object_unparent(OBJECT(auth));
  54. }
  55. static void test_authz_default_allow(void)
  56. {
  57. gchar *file = qemu_authz_listfile_test_save(
  58. "default-allow.cfg",
  59. "{ \"policy\": \"allow\" }");
  60. Error *local_err = NULL;
  61. QAuthZListFile *auth = qauthz_list_file_new("auth0",
  62. file, false,
  63. &local_err);
  64. unlink(file);
  65. g_free(file);
  66. g_assert(local_err == NULL);
  67. g_assert(qauthz_is_allowed(QAUTHZ(auth), "fred", &error_abort));
  68. object_unparent(OBJECT(auth));
  69. }
  70. static void test_authz_explicit_deny(void)
  71. {
  72. gchar *file = qemu_authz_listfile_test_save(
  73. "explicit-deny.cfg",
  74. "{ \"rules\": [ "
  75. " { \"match\": \"fred\","
  76. " \"policy\": \"deny\","
  77. " \"format\": \"exact\" } ],"
  78. " \"policy\": \"allow\" }");
  79. Error *local_err = NULL;
  80. QAuthZListFile *auth = qauthz_list_file_new("auth0",
  81. file, false,
  82. &local_err);
  83. unlink(file);
  84. g_free(file);
  85. g_assert(local_err == NULL);
  86. g_assert(!qauthz_is_allowed(QAUTHZ(auth), "fred", &error_abort));
  87. object_unparent(OBJECT(auth));
  88. }
  89. static void test_authz_explicit_allow(void)
  90. {
  91. gchar *file = qemu_authz_listfile_test_save(
  92. "explicit-allow.cfg",
  93. "{ \"rules\": [ "
  94. " { \"match\": \"fred\","
  95. " \"policy\": \"allow\","
  96. " \"format\": \"exact\" } ],"
  97. " \"policy\": \"deny\" }");
  98. Error *local_err = NULL;
  99. QAuthZListFile *auth = qauthz_list_file_new("auth0",
  100. file, false,
  101. &local_err);
  102. unlink(file);
  103. g_free(file);
  104. g_assert(local_err == NULL);
  105. g_assert(qauthz_is_allowed(QAUTHZ(auth), "fred", &error_abort));
  106. object_unparent(OBJECT(auth));
  107. }
  108. static void test_authz_complex(void)
  109. {
  110. gchar *file = qemu_authz_listfile_test_save(
  111. "complex.cfg",
  112. "{ \"rules\": [ "
  113. " { \"match\": \"fred\","
  114. " \"policy\": \"allow\","
  115. " \"format\": \"exact\" },"
  116. " { \"match\": \"bob\","
  117. " \"policy\": \"allow\","
  118. " \"format\": \"exact\" },"
  119. " { \"match\": \"dan\","
  120. " \"policy\": \"deny\","
  121. " \"format\": \"exact\" },"
  122. " { \"match\": \"dan*\","
  123. " \"policy\": \"allow\","
  124. " \"format\": \"glob\" } ],"
  125. " \"policy\": \"deny\" }");
  126. Error *local_err = NULL;
  127. QAuthZListFile *auth = qauthz_list_file_new("auth0",
  128. file, false,
  129. &local_err);
  130. unlink(file);
  131. g_free(file);
  132. g_assert(local_err == NULL);
  133. g_assert(qauthz_is_allowed(QAUTHZ(auth), "fred", &error_abort));
  134. g_assert(qauthz_is_allowed(QAUTHZ(auth), "bob", &error_abort));
  135. g_assert(!qauthz_is_allowed(QAUTHZ(auth), "dan", &error_abort));
  136. g_assert(qauthz_is_allowed(QAUTHZ(auth), "danb", &error_abort));
  137. object_unparent(OBJECT(auth));
  138. }
  139. int main(int argc, char **argv)
  140. {
  141. int ret;
  142. GError *gerr = NULL;
  143. g_test_init(&argc, &argv, NULL);
  144. module_call_init(MODULE_INIT_QOM);
  145. workdir = g_dir_make_tmp("qemu-test-authz-listfile-XXXXXX",
  146. &gerr);
  147. if (!workdir) {
  148. g_printerr("Unable to create temporary dir: %s\n",
  149. gerr->message);
  150. g_error_free(gerr);
  151. abort();
  152. }
  153. g_test_add_func("/auth/list/default/deny", test_authz_default_deny);
  154. g_test_add_func("/auth/list/default/allow", test_authz_default_allow);
  155. g_test_add_func("/auth/list/explicit/deny", test_authz_explicit_deny);
  156. g_test_add_func("/auth/list/explicit/allow", test_authz_explicit_allow);
  157. g_test_add_func("/auth/list/complex", test_authz_complex);
  158. ret = g_test_run();
  159. rmdir(workdir);
  160. g_free(workdir);
  161. return ret;
  162. }