test_httpauth.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. /* _
  2. * ___ __ _ __ _ _ _(_)
  3. * / __|/ _` |/ _` | | | | |
  4. * \__ \ (_| | (_| | |_| | |
  5. * |___/\__,_|\__, |\__,_|_|
  6. * |___/
  7. *
  8. * Cross-platform library which helps to develop web servers or frameworks.
  9. *
  10. * Copyright (C) 2016-2024 Silvio Clecio <[email protected]>
  11. *
  12. * Sagui library is free software; you can redistribute it and/or
  13. * modify it under the terms of the GNU Lesser General Public
  14. * License as published by the Free Software Foundation; either
  15. * version 2.1 of the License, or (at your option) any later version.
  16. *
  17. * Sagui library is distributed in the hope that it will be useful,
  18. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  20. * Lesser General Public License for more details.
  21. *
  22. * You should have received a copy of the GNU Lesser General Public
  23. * License along with Sagui library; if not, write to the Free Software
  24. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  25. */
  26. #include "sg_assert.h"
  27. #include <string.h>
  28. #include <microhttpd.h>
  29. #include <sagui.h>
  30. #include "sg_httpres.h"
  31. #include "sg_httpauth.h"
  32. static void test__httpauth_new(struct MHD_Connection *con) {
  33. struct sg_httpres *res = sg__httpres_new(con);
  34. struct sg_httpauth *auth = sg__httpauth_new(res);
  35. ASSERT(auth);
  36. sg__httpauth_free(auth);
  37. sg__httpres_free(res);
  38. }
  39. static void test__httpauth_free(void) {
  40. sg__httpauth_free(NULL);
  41. }
  42. static void test__httpauth_dispatch(struct sg_httpauth *auth) {
  43. const size_t len = 3;
  44. auth->res->ret = true;
  45. ASSERT(sg__httpauth_dispatch(auth));
  46. ASSERT(auth->res->ret == MHD_YES);
  47. auth->res->ret = false;
  48. auth->canceled = true;
  49. ASSERT(!sg__httpauth_dispatch(auth));
  50. ASSERT(auth->res->ret == MHD_NO);
  51. auth->res->ret = false;
  52. auth->canceled = true;
  53. auth->res->handle =
  54. MHD_create_response_from_buffer(len, "foo", MHD_RESPMEM_PERSISTENT);
  55. ASSERT(sg__httpauth_dispatch(auth));
  56. ASSERT(auth->res->ret == MHD_YES);
  57. MHD_destroy_response(auth->res->handle);
  58. auth->res->handle = NULL;
  59. auth->res->ret = false;
  60. auth->canceled = false;
  61. auth->res->handle = NULL;
  62. ASSERT(!sg__httpauth_dispatch(auth));
  63. ASSERT(auth->res->ret == MHD_NO);
  64. auth->res->ret = false;
  65. auth->canceled = false;
  66. auth->res->con = NULL;
  67. auth->res->handle =
  68. MHD_create_response_from_buffer(len, "foo", MHD_RESPMEM_PERSISTENT);
  69. ASSERT(!sg__httpauth_dispatch(auth));
  70. ASSERT(auth->res->ret == MHD_NO);
  71. MHD_destroy_response(auth->res->handle);
  72. auth->res->handle = NULL;
  73. }
  74. static void test_httpauth_set_realm(struct sg_httpauth *auth) {
  75. ASSERT(sg_httpauth_set_realm(NULL, "") == EINVAL);
  76. ASSERT(sg_httpauth_set_realm(auth, NULL) == EINVAL);
  77. ASSERT(sg_httpauth_set_realm(auth, "foo") == 0);
  78. ASSERT(sg_httpauth_set_realm(auth, "foo") == EALREADY);
  79. ASSERT(sg_httpauth_set_realm(auth, "bar") == EALREADY);
  80. ASSERT(strcmp(auth->realm, "foo") == 0);
  81. sg_free(auth->realm);
  82. auth->realm = NULL;
  83. }
  84. static void test_httpauth_realm(struct sg_httpauth *auth) {
  85. errno = 0;
  86. ASSERT(!sg_httpauth_realm(NULL));
  87. ASSERT(errno == EINVAL);
  88. ASSERT(sg_httpauth_set_realm(auth, "foo") == 0);
  89. ASSERT(strcmp(sg_httpauth_realm(auth), "foo") == 0);
  90. sg_free(auth->realm);
  91. auth->realm = NULL;
  92. }
  93. static void test_httpauth_deny2(struct sg_httpauth *auth) {
  94. ASSERT(sg_httpauth_deny2(NULL, "", "", 200) == EINVAL);
  95. ASSERT(sg_httpauth_deny2(auth, NULL, "", 200) == EINVAL);
  96. ASSERT(sg_httpauth_deny2(auth, "", NULL, 200) == EINVAL);
  97. ASSERT(sg_httpauth_deny2(auth, "", "", 99) == EINVAL);
  98. ASSERT(sg_httpauth_deny2(auth, "", "", 600) == EINVAL);
  99. ASSERT(sg_httpauth_deny2(auth, MHD_HTTP_HEADER_CONTENT_TYPE, "foo",
  100. MHD_HTTP_FORBIDDEN) == 0);
  101. ASSERT(auth->res->status == MHD_HTTP_FORBIDDEN);
  102. ASSERT(sg_httpauth_deny2(auth, MHD_HTTP_HEADER_CONTENT_TYPE, "bar", 200) ==
  103. EALREADY);
  104. ASSERT(sg_httpauth_deny2(auth, "bar", "foo", 200) == EALREADY);
  105. ASSERT(strcmp(sg_strmap_get(auth->res->headers, MHD_HTTP_HEADER_CONTENT_TYPE),
  106. "foo") == 0);
  107. MHD_destroy_response(auth->res->handle);
  108. auth->res->handle = NULL;
  109. sg_strmap_cleanup(&auth->res->headers);
  110. }
  111. static void test_httpauth_deny(struct sg_httpauth *auth) {
  112. ASSERT(sg_httpauth_deny(NULL, "", "") == EINVAL);
  113. ASSERT(sg_httpauth_deny(auth, NULL, "") == EINVAL);
  114. ASSERT(sg_httpauth_deny(auth, "", NULL) == EINVAL);
  115. ASSERT(sg_httpauth_deny(auth, MHD_HTTP_HEADER_CONTENT_TYPE, "foo") == 0);
  116. ASSERT(auth->res->status == MHD_HTTP_UNAUTHORIZED);
  117. ASSERT(sg_httpauth_deny(auth, MHD_HTTP_HEADER_CONTENT_TYPE, "bar") ==
  118. EALREADY);
  119. ASSERT(sg_httpauth_deny(auth, "bar", "foo") == EALREADY);
  120. ASSERT(strcmp(sg_strmap_get(auth->res->headers, MHD_HTTP_HEADER_CONTENT_TYPE),
  121. "foo") == 0);
  122. MHD_destroy_response(auth->res->handle);
  123. auth->res->handle = NULL;
  124. sg_strmap_cleanup(&auth->res->headers);
  125. }
  126. static void test_httpauth_cancel(struct sg_httpauth *auth) {
  127. ASSERT(sg_httpauth_cancel(NULL) == EINVAL);
  128. auth->canceled = false;
  129. ASSERT(!auth->canceled);
  130. ASSERT(sg_httpauth_cancel(auth) == 0);
  131. ASSERT(auth->canceled);
  132. }
  133. static void test_httpauth_usr(struct sg_httpauth *auth) {
  134. errno = 0;
  135. ASSERT(!sg_httpauth_usr(NULL));
  136. ASSERT(errno == EINVAL);
  137. auth->usr = "foo";
  138. ASSERT(strcmp(sg_httpauth_usr(auth), "foo") == 0);
  139. }
  140. static void test_httpauth_pwd(struct sg_httpauth *auth) {
  141. errno = 0;
  142. ASSERT(!sg_httpauth_pwd(NULL));
  143. ASSERT(errno == EINVAL);
  144. auth->pwd = "foo";
  145. ASSERT(strcmp(sg_httpauth_pwd(auth), "foo") == 0);
  146. }
  147. int main(void) {
  148. struct sg_httpauth *auth = sg_alloc(sizeof(struct sg_httpauth));
  149. ASSERT(auth);
  150. auth->res = sg_alloc(sizeof(struct sg_httpres));
  151. auth->res->con = sg_alloc(256);
  152. ASSERT(auth->res);
  153. ASSERT(auth->res->con);
  154. test__httpauth_new(auth->res->con);
  155. test__httpauth_free();
  156. test__httpauth_dispatch(auth);
  157. test_httpauth_set_realm(auth);
  158. test_httpauth_realm(auth);
  159. test_httpauth_deny2(auth);
  160. test_httpauth_deny(auth);
  161. test_httpauth_cancel(auth);
  162. test_httpauth_usr(auth);
  163. test_httpauth_pwd(auth);
  164. sg_free(auth->res->con);
  165. sg_free(auth->res);
  166. sg_free(auth);
  167. return EXIT_SUCCESS;
  168. }