test_memory_funcs.c 2.4 KB

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