test_memory_funcs.c 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. #include <jansson.h>
  2. #include <string.h>
  3. #include "util.h"
  4. static int malloc_called = 0;
  5. static int free_called = 0;
  6. static size_t malloc_used = 0;
  7. /* helpers */
  8. static void create_and_free_complex_object() {
  9. json_t *obj;
  10. obj = json_pack("{s:i,s:n,s:b,s:b,s:{s:s},s:[i,i,i]}", "foo", 42, "bar", "baz", 1,
  11. "qux", 0, "alice", "bar", "baz", "bob", 9, 8, 7);
  12. json_decref(obj);
  13. }
  14. static void create_and_free_object_with_oom() {
  15. int i;
  16. char key[4];
  17. json_t *obj = json_object();
  18. for (i = 0; i < 10; i++) {
  19. snprintf(key, sizeof key, "%d", i);
  20. json_object_set_new(obj, key, json_integer(i));
  21. }
  22. json_decref(obj);
  23. }
  24. static void *my_malloc(size_t size) {
  25. malloc_called = 1;
  26. return malloc(size);
  27. }
  28. static void my_free(void *ptr) {
  29. free_called = 1;
  30. free(ptr);
  31. }
  32. static void test_simple() {
  33. json_malloc_t mfunc = NULL;
  34. json_free_t ffunc = NULL;
  35. json_set_alloc_funcs(my_malloc, my_free);
  36. json_get_alloc_funcs(&mfunc, &ffunc);
  37. create_and_free_complex_object();
  38. if (malloc_called != 1 || free_called != 1 || mfunc != my_malloc || ffunc != my_free)
  39. fail("Custom allocation failed");
  40. }
  41. static void *oom_malloc(size_t size) {
  42. if (malloc_used + size > 800)
  43. return NULL;
  44. malloc_used += size;
  45. return malloc(size);
  46. }
  47. static void oom_free(void *ptr) {
  48. free_called++;
  49. free(ptr);
  50. }
  51. static void test_oom() {
  52. free_called = 0;
  53. json_set_alloc_funcs(oom_malloc, oom_free);
  54. create_and_free_object_with_oom();
  55. if (free_called == 0)
  56. fail("Allocation with OOM failed");
  57. }
  58. /*
  59. Test the secure memory functions code given in the API reference
  60. documentation, but by using plain memset instead of
  61. guaranteed_memset().
  62. */
  63. static void *secure_malloc(size_t size) {
  64. /* Store the memory area size in the beginning of the block */
  65. void *ptr = malloc(size + 8);
  66. *((size_t *)ptr) = size;
  67. return (char *)ptr + 8;
  68. }
  69. static void secure_free(void *ptr) {
  70. size_t size;
  71. ptr = (char *)ptr - 8;
  72. size = *((size_t *)ptr);
  73. /*guaranteed_*/ memset(ptr, 0, size + 8);
  74. free(ptr);
  75. }
  76. static void test_secure_funcs(void) {
  77. json_set_alloc_funcs(secure_malloc, secure_free);
  78. create_and_free_complex_object();
  79. }
  80. static void test_bad_args(void) {
  81. /* The result of this test is not crashing. */
  82. json_get_alloc_funcs(NULL, NULL);
  83. }
  84. static void run_tests() {
  85. test_simple();
  86. test_secure_funcs();
  87. test_oom();
  88. test_bad_args();
  89. }