1
0

api_mem.c 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350
  1. /* vim: set tabstop=8 shiftwidth=4 softtabstop=4 expandtab smarttab colorcolumn=80: */
  2. /*
  3. * Copyright 2025 Red Hat, Inc.
  4. *
  5. * Licensed under the Apache License, Version 2.0 (the "License");
  6. * you may not use this file except in compliance with the License.
  7. * You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. */
  17. #include <jose/jose.h>
  18. #include <assert.h>
  19. #include <string.h>
  20. #include <stdlib.h>
  21. #include <stdio.h>
  22. #include <errno.h>
  23. static struct {
  24. size_t malloc_calls;
  25. size_t realloc_calls;
  26. size_t free_calls;
  27. size_t total_allocated;
  28. size_t total_freed;
  29. int fail_malloc;
  30. int fail_realloc;
  31. } alloc_stats = {0};
  32. static void*
  33. test_malloc(size_t size)
  34. {
  35. alloc_stats.malloc_calls++;
  36. if (alloc_stats.fail_malloc)
  37. return NULL;
  38. void *ptr = malloc(size);
  39. if (ptr) {
  40. alloc_stats.total_allocated += size;
  41. }
  42. return ptr;
  43. }
  44. static void*
  45. test_realloc(void *ptr, size_t size)
  46. {
  47. alloc_stats.realloc_calls++;
  48. if (alloc_stats.fail_realloc)
  49. return NULL;
  50. return realloc(ptr, size);
  51. }
  52. static void
  53. test_free(void *ptr)
  54. {
  55. alloc_stats.free_calls++;
  56. if (ptr) {
  57. free(ptr);
  58. }
  59. }
  60. static void*
  61. test_calloc(size_t nmemb, size_t size)
  62. {
  63. alloc_stats.malloc_calls++; // calloc is effectively malloc + zero
  64. if (alloc_stats.fail_malloc)
  65. return NULL;
  66. void *ptr = calloc(nmemb, size);
  67. if (ptr) {
  68. alloc_stats.total_allocated += nmemb * size;
  69. }
  70. return ptr;
  71. }
  72. static void
  73. reset_alloc_stats(void)
  74. {
  75. memset(&alloc_stats, 0, sizeof(alloc_stats));
  76. }
  77. static void
  78. test_allocator_set_get(void)
  79. {
  80. // Store original allocators
  81. jose_malloc_t orig_malloc;
  82. jose_realloc_t orig_realloc;
  83. jose_free_t orig_free;
  84. jose_calloc_t orig_calloc;
  85. jose_get_alloc(&orig_malloc, &orig_realloc, &orig_free, &orig_calloc);
  86. // Set our test allocators
  87. int ret = jose_set_alloc(test_malloc, test_realloc, test_free, test_calloc);
  88. assert(ret == 0);
  89. // Verify they were set correctly
  90. jose_malloc_t curr_malloc;
  91. jose_realloc_t curr_realloc;
  92. jose_free_t curr_free;
  93. jose_calloc_t curr_calloc;
  94. jose_get_alloc(&curr_malloc, &curr_realloc, &curr_free, &curr_calloc);
  95. assert(curr_malloc == test_malloc);
  96. assert(curr_realloc == test_realloc);
  97. assert(curr_free == test_free);
  98. assert(curr_calloc == test_calloc);
  99. // Reset to originals
  100. ret = jose_set_alloc(orig_malloc, orig_realloc, orig_free, orig_calloc);
  101. assert(ret == 0);
  102. // Verify reset worked
  103. jose_get_alloc(&curr_malloc, &curr_realloc, &curr_free, &curr_calloc);
  104. assert(curr_malloc == orig_malloc);
  105. assert(curr_realloc == orig_realloc);
  106. assert(curr_free == orig_free);
  107. assert(curr_calloc == orig_calloc);
  108. }
  109. static void
  110. test_allocator_io_operations(void)
  111. {
  112. jose_cfg_auto_t *cfg = jose_cfg();
  113. assert(cfg != NULL);
  114. int ret = jose_set_alloc(test_malloc, test_realloc, test_free, test_calloc);
  115. assert(ret == 0);
  116. reset_alloc_stats();
  117. void *buf = NULL;
  118. size_t len = 0;
  119. jose_io_t *io = jose_io_malloc(cfg, &buf, &len);
  120. assert(io != NULL);
  121. assert(alloc_stats.malloc_calls > 0);
  122. const char *test_data = "Hello, world! This is test data for the custom allocator.";
  123. size_t data_len = strlen(test_data);
  124. assert(io->feed(io, test_data, data_len));
  125. assert(alloc_stats.realloc_calls > 0);
  126. assert(io->done(io));
  127. assert(buf != NULL);
  128. assert(len == data_len);
  129. assert(memcmp(buf, test_data, data_len) == 0);
  130. size_t initial_free_calls = alloc_stats.free_calls;
  131. jose_io_decref(io);
  132. assert(alloc_stats.free_calls > initial_free_calls);
  133. printf("Allocator IO operations - malloc calls: %zu, realloc calls: %zu, free calls: %zu\n",
  134. alloc_stats.malloc_calls, alloc_stats.realloc_calls, alloc_stats.free_calls);
  135. }
  136. static void
  137. test_allocator_failures(void)
  138. {
  139. jose_cfg_auto_t *cfg = jose_cfg();
  140. assert(cfg != NULL);
  141. int ret = jose_set_alloc(test_malloc, test_realloc, test_free, test_calloc);
  142. assert(ret == 0);
  143. reset_alloc_stats();
  144. alloc_stats.fail_malloc = 1;
  145. void *buf = NULL;
  146. size_t len = 0;
  147. jose_io_t *io = jose_io_malloc(cfg, &buf, &len);
  148. assert(io == NULL);
  149. assert(alloc_stats.malloc_calls > 0);
  150. alloc_stats.fail_malloc = 0;
  151. reset_alloc_stats();
  152. printf("Allocator failure tests - malloc calls: %zu, realloc calls: %zu, free calls: %zu\n",
  153. alloc_stats.malloc_calls, alloc_stats.realloc_calls, alloc_stats.free_calls);
  154. }
  155. static void
  156. test_multiple_configs(void)
  157. {
  158. jose_cfg_auto_t *cfg1 = jose_cfg();
  159. jose_cfg_auto_t *cfg2 = jose_cfg();
  160. assert(cfg1 != NULL);
  161. assert(cfg2 != NULL);
  162. int ret1 = jose_set_alloc(test_malloc, test_realloc, test_free, test_calloc);
  163. assert(ret1 == 0);
  164. reset_alloc_stats();
  165. jose_malloc_t malloc_func1 = NULL, malloc_func2 = NULL;
  166. jose_get_alloc(&malloc_func1, NULL, NULL, NULL);
  167. jose_get_alloc(&malloc_func2, NULL, NULL, NULL);
  168. assert(malloc_func1 == test_malloc);
  169. assert(malloc_func2 == test_malloc);
  170. reset_alloc_stats();
  171. void *buf1 = NULL;
  172. size_t len1 = 0;
  173. jose_io_t *io1 = jose_io_malloc(cfg1, &buf1, &len1);
  174. assert(io1 != NULL);
  175. size_t custom_malloc_calls = alloc_stats.malloc_calls;
  176. assert(custom_malloc_calls > 0);
  177. void *buf2 = NULL;
  178. size_t len2 = 0;
  179. jose_io_t *io2 = jose_io_malloc(cfg2, &buf2, &len2);
  180. assert(io2 != NULL);
  181. assert(alloc_stats.malloc_calls > custom_malloc_calls);
  182. jose_io_decref(io1);
  183. jose_io_decref(io2);
  184. printf("Multiple configurations - malloc calls: %zu, realloc calls: %zu, free calls: %zu\n",
  185. alloc_stats.malloc_calls, alloc_stats.realloc_calls, alloc_stats.free_calls);
  186. }
  187. static void
  188. test_null_config(void)
  189. {
  190. jose_reset_alloc();
  191. void *buf = NULL;
  192. size_t len = 0;
  193. reset_alloc_stats();
  194. jose_io_t *io = jose_io_malloc(NULL, &buf, &len);
  195. assert(io != NULL);
  196. assert(alloc_stats.malloc_calls == 0);
  197. assert(alloc_stats.realloc_calls == 0);
  198. assert(alloc_stats.free_calls == 0);
  199. const char *test_data = "Test data";
  200. assert(io->feed(io, test_data, strlen(test_data)));
  201. assert(io->done(io));
  202. assert(alloc_stats.malloc_calls == 0);
  203. assert(alloc_stats.realloc_calls == 0);
  204. assert(alloc_stats.free_calls == 0);
  205. assert(buf != NULL);
  206. assert(len == strlen(test_data));
  207. assert(memcmp(buf, test_data, len) == 0);
  208. jose_io_decref(io);
  209. printf("NULL config - malloc calls: %zu, realloc calls: %zu, free calls: %zu\n",
  210. alloc_stats.malloc_calls, alloc_stats.realloc_calls, alloc_stats.free_calls);
  211. }
  212. static void
  213. test_allocator_with_jws(void)
  214. {
  215. jose_cfg_auto_t *cfg = jose_cfg();
  216. assert(cfg != NULL);
  217. int ret = jose_set_alloc(test_malloc, test_realloc, test_free, test_calloc);
  218. assert(ret == 0);
  219. reset_alloc_stats();
  220. json_auto_t *jwk = json_pack("{s:s}", "alg", "HS256");
  221. assert(jwk != NULL);
  222. assert(jose_jwk_gen(cfg, jwk));
  223. json_auto_t *jws = json_pack("{s:s}", "payload", "test payload");
  224. assert(jws != NULL);
  225. assert(jose_jws_sig(cfg, jws, NULL, jwk));
  226. assert(jose_jws_ver(cfg, jws, NULL, jwk, false));
  227. printf(" JWS operations - malloc calls: %zu, realloc calls: %zu, free calls: %zu\n",
  228. alloc_stats.malloc_calls, alloc_stats.realloc_calls, alloc_stats.free_calls);
  229. printf("JWS operations - malloc calls: %zu, realloc calls: %zu, free calls: %zu\n",
  230. alloc_stats.malloc_calls, alloc_stats.realloc_calls, alloc_stats.free_calls);
  231. }
  232. static void
  233. test_global_allocators_with_b64(void)
  234. {
  235. jose_cfg_auto_t *cfg = jose_cfg();
  236. assert(cfg != NULL);
  237. int ret = jose_set_alloc(test_malloc, test_realloc, test_free, test_calloc);
  238. assert(ret == 0);
  239. reset_alloc_stats();
  240. json_auto_t *test_json = json_pack("{s:s,s:i}", "message", "Hello, World!", "value", 42);
  241. assert(test_json != NULL);
  242. json_auto_t *encoded = jose_b64_enc_dump(test_json);
  243. assert(encoded != NULL);
  244. json_auto_t *decoded = jose_b64_dec_load(encoded);
  245. assert(decoded != NULL);
  246. const char *message = NULL;
  247. int value = 0;
  248. assert(json_unpack(decoded, "{s:s,s:i}", "message", &message, "value", &value) == 0);
  249. assert(strcmp(message, "Hello, World!") == 0);
  250. assert(value == 42);
  251. assert(alloc_stats.malloc_calls > 0);
  252. assert(alloc_stats.free_calls > 0);
  253. printf(" B64 operations - malloc calls: %zu, realloc calls: %zu, free calls: %zu\n",
  254. alloc_stats.malloc_calls, alloc_stats.realloc_calls, alloc_stats.free_calls);
  255. }
  256. int
  257. main(int argc, char *argv[])
  258. {
  259. test_allocator_set_get();
  260. test_allocator_io_operations();
  261. test_allocator_failures();
  262. test_multiple_configs();
  263. test_null_config();
  264. test_allocator_with_jws();
  265. test_global_allocators_with_b64();
  266. return EXIT_SUCCESS;
  267. }