test_extra.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. /* _
  2. * ___ __ _ __ _ _ _(_)
  3. * / __|/ _` |/ _` | | | | |
  4. * \__ \ (_| | (_| | |_| | |
  5. * |___/\__,_|\__, |\__,_|_|
  6. * |___/
  7. *
  8. * Cross-platform library which helps to develop web servers or frameworks.
  9. *
  10. * Copyright (C) 2016-2020 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. #ifdef SG_HTTP_COMPRESSION
  30. #include "zlib.h"
  31. #endif /* SG_HTTP_COMPRESSION */
  32. #include "sg_macros.h"
  33. #include "sg_strmap.h"
  34. #include "sg_extra.h"
  35. #include <sagui.h>
  36. static void test__convals_iter(void) {
  37. struct sg_strmap *map = NULL;
  38. ASSERT(sg__convals_iter(NULL, MHD_HEADER_KIND, "foo", "bar") == MHD_YES);
  39. ASSERT(!map);
  40. ASSERT(sg__convals_iter(&map, MHD_HEADER_KIND, "foo", "bar") == MHD_YES);
  41. ASSERT(map);
  42. ASSERT(strcmp(sg_strmap_get(map, "foo"), "bar") == 0);
  43. sg_strmap_cleanup(&map);
  44. }
  45. static void test__strmap_iter(void) {
  46. struct sg_strmap *header = sg_alloc(sizeof(struct sg_strmap));
  47. struct MHD_Response *res = sg_alloc(64);
  48. header->name = "";
  49. header->val = "";
  50. ASSERT(sg__strmap_iter(res, header) == 0);
  51. sg_free(header);
  52. sg_free(res);
  53. }
  54. static void test_eor(void) {
  55. ASSERT(sg_eor(false) == (ssize_t) MHD_CONTENT_READER_END_OF_STREAM);
  56. ASSERT(sg_eor(true) == (ssize_t) MHD_CONTENT_READER_END_WITH_ERROR);
  57. }
  58. #ifdef SG_HTTP_COMPRESSION
  59. static int sg__uncompress2(Bytef *dest, uLongf *destLen, const Bytef *source,
  60. uLong *sourceLen) {
  61. z_stream stream;
  62. const uInt max = (uInt) -1;
  63. uLong len, left;
  64. int err;
  65. Byte buf[1];
  66. len = *sourceLen;
  67. if (*destLen) {
  68. left = *destLen;
  69. *destLen = 0;
  70. } else {
  71. left = 1;
  72. dest = buf;
  73. }
  74. stream.next_in = (z_const Bytef *) source;
  75. stream.avail_in = 0;
  76. stream.zalloc = (alloc_func) 0;
  77. stream.zfree = (free_func) 0;
  78. stream.opaque = (voidpf) 0;
  79. err = inflateInit2(&stream, -MAX_WBITS);
  80. if (err != Z_OK)
  81. return err;
  82. stream.next_out = dest;
  83. stream.avail_out = 0;
  84. do {
  85. if (stream.avail_out == 0) {
  86. stream.avail_out = left > (uLong) max ? max : (uInt) left;
  87. left -= stream.avail_out;
  88. }
  89. if (stream.avail_in == 0) {
  90. stream.avail_in = len > (uLong) max ? max : (uInt) len;
  91. len -= stream.avail_in;
  92. }
  93. err = inflate(&stream, Z_NO_FLUSH);
  94. } while (err == Z_OK);
  95. *sourceLen -= len + stream.avail_in;
  96. if (dest != buf)
  97. *destLen = stream.total_out;
  98. else if (stream.total_out && err == Z_BUF_ERROR)
  99. left = 1;
  100. inflateEnd(&stream);
  101. return err == Z_STREAM_END ?
  102. Z_OK :
  103. err == Z_NEED_DICT ?
  104. Z_DATA_ERROR :
  105. err == Z_BUF_ERROR && (left + stream.avail_out) != 0 ? Z_DATA_ERROR :
  106. err;
  107. }
  108. static void test__zcompress(void) {
  109. const char *text = "ffffffffffoooooooooobbbbbbbbbbaaaaaaaaaarrrrrrrrrr";
  110. char src[100], dest[100];
  111. size_t src_size, dest_size;
  112. sprintf(src, "%s", text);
  113. src_size = strlen(src);
  114. dest_size = compressBound(src_size);
  115. ASSERT(sg__zcompress(NULL, src_size, (Bytef *) dest, (uLong *) &dest_size,
  116. -10) != Z_OK);
  117. dest_size = compressBound(src_size);
  118. ASSERT(sg__zcompress((Bytef *) src, src_size, (Bytef *) dest,
  119. (uLong *) &dest_size, 9) == Z_OK);
  120. ASSERT(dest_size == 15);
  121. memset(src, 0, sizeof(src));
  122. memcpy(src, dest, dest_size);
  123. memset(dest, 0, sizeof(dest));
  124. src_size = dest_size;
  125. dest_size = sizeof(dest);
  126. ASSERT(sg__uncompress2((Bytef *) dest, (uLongf *) &dest_size, (Bytef *) src,
  127. (uLong *) &src_size) == Z_OK);
  128. ASSERT(dest_size == 50);
  129. dest[dest_size] = '\0';
  130. ASSERT(strcmp(dest, text) == 0);
  131. }
  132. static void test__zdeflate(void) {
  133. const char *text = "ffffffffffoooooooooobbbbbbbbbbaaaaaaaaaarrrrrrrrrr";
  134. z_stream stream;
  135. char src[100], zbuf[SG__ZLIB_CHUNK];
  136. char *dest;
  137. size_t src_size, dest_size;
  138. memset(&stream, 0, sizeof(z_stream));
  139. ASSERT(deflateInit2(&stream, Z_BEST_COMPRESSION, Z_DEFLATED, -MAX_WBITS,
  140. MAX_MEM_LEVEL, Z_DEFAULT_STRATEGY) == Z_OK);
  141. sprintf(src, "%s", text);
  142. src_size = strlen(src);
  143. ASSERT(sg__zdeflate(&stream, (Bytef *) zbuf, Z_FINISH, (Bytef *) src,
  144. (uInt) src_size, (Bytef **) &dest, &dest_size) == Z_OK);
  145. ASSERT(deflateEnd(&stream) == Z_OK);
  146. ASSERT(dest_size == 15);
  147. memset(src, 0, sizeof(src));
  148. memcpy(src, dest, dest_size);
  149. free(dest);
  150. dest = malloc(100);
  151. src_size = dest_size;
  152. dest_size = 100;
  153. ASSERT(sg__uncompress2((Bytef *) dest, (uLongf *) &dest_size, (Bytef *) src,
  154. (uLong *) &src_size) == Z_OK);
  155. ASSERT(dest_size == 50);
  156. if (dest) {
  157. dest[dest_size] = '\0';
  158. ASSERT(strcmp(dest, text) == 0);
  159. }
  160. free(dest);
  161. memset(&stream, 0, sizeof(z_stream));
  162. ASSERT(deflateInit2(&stream, Z_BEST_COMPRESSION, Z_DEFLATED, -MAX_WBITS, 8,
  163. Z_DEFAULT_STRATEGY) == Z_OK);
  164. sprintf(src, "%s", text);
  165. src_size = strlen(src);
  166. ASSERT(sg__zdeflate(&stream, (Bytef *) zbuf, Z_NO_FLUSH, (Bytef *) src,
  167. (uInt) src_size, (Bytef **) &dest, &dest_size) == Z_OK);
  168. free(dest);
  169. ASSERT(dest_size == 0);
  170. ASSERT(sg__zdeflate(&stream, (Bytef *) zbuf, Z_FINISH, (Bytef *) src, 0,
  171. (Bytef **) &dest, &dest_size) == Z_OK);
  172. ASSERT(deflateEnd(&stream) == Z_OK);
  173. ASSERT(dest_size == 15);
  174. memset(src, 0, sizeof(src));
  175. memcpy(src, dest, dest_size);
  176. free(dest);
  177. dest = malloc(100);
  178. src_size = dest_size;
  179. dest_size = 100;
  180. ASSERT(sg__uncompress2((Bytef *) dest, (uLongf *) &dest_size, (Bytef *) src,
  181. (uLong *) &src_size) == Z_OK);
  182. ASSERT(dest_size == 50);
  183. if (dest) {
  184. dest[dest_size] = '\0';
  185. ASSERT(strcmp(dest, text) == 0);
  186. }
  187. free(dest);
  188. }
  189. #endif /* SG_HTTP_COMPRESSION */
  190. int main(void) {
  191. test__convals_iter();
  192. test__strmap_iter();
  193. test_eor();
  194. #ifdef SG_HTTP_COMPRESSION
  195. test__zcompress();
  196. test__zdeflate();
  197. #endif /* SG_HTTP_COMPRESSION */
  198. return EXIT_SUCCESS;
  199. }