check: add some md_json_t refcount tests
This requires some header duplication to get at the json_t internals,
but it's probably worth it. Disable the Jansson free function so that we
can inspect the refcount after it goes to zero.
#include <stdlib.h>
#include "test_common.h"
-
#include "md_json.h"
+/*
+ * XXX To inspect pieces of the md_json_t struct, we need to include the jansson
+ * definition of json_t and duplicate the md_json_t definition.
+ */
+
+#include <jansson.h>
+
+/* duplicated from src/md_json.c */
+struct md_json_t {
+ apr_pool_t *p;
+ json_t *j;
+};
+
+/*
+ * Helpers
+ */
+
+/* A free function that does nothing. See md_json_setup(). */
+static void noop_free(void *unused)
+{
+ (void) unused;
+}
+
/*
* Test Fixture -- runs once per test
*/
@@ -29,6 +51,13 @@ static void md_json_setup(void)
if (apr_pool_create(&g_pool, NULL) != APR_SUCCESS) {
exit(1);
}
+
+ /*
+ * Disable the free function in Jansson so that we can safely inspect
+ * objects after their refcounts have fallen to zero. Otherwise we're
+ * relying on undefined behavior.
+ */
+ json_set_alloc_funcs(malloc, noop_free);
}
static void md_json_teardown(void)
@@ -40,10 +69,36 @@ static void md_json_teardown(void)
* Tests
*/
-START_TEST(first_test)
+START_TEST(json_create_makes_object_with_refcount_one)
+{
+ md_json_t *json = md_json_create(g_pool);
+
+ ck_assert(json);
+ ck_assert_int_eq(json->j->type, JSON_OBJECT);
+ ck_assert_int_eq(json->j->refcount, 1);
+}
+END_TEST
+
+START_TEST(json_destroy_releases_object)
+{
+ md_json_t *json = md_json_create(g_pool);
+ json_t *internal = json->j;
+
+ md_json_destroy(json);
+
+ ck_assert_int_eq(internal->refcount, 0);
+ ck_assert_ptr_eq(json->j, NULL);
+}
+END_TEST
+
+START_TEST(clearing_md_json_t_pool_releases_internal_object)
{
- md_json_t *j = md_json_create(g_pool);
- ck_assert(j);
+ md_json_t *json = md_json_create(g_pool);
+ json_t *internal = json->j;
+
+ apr_pool_clear(g_pool);
+
+ ck_assert_int_eq(internal->refcount, 0);
}
END_TEST
@@ -53,7 +108,9 @@ TCase *md_json_test_case(void)
tcase_add_checked_fixture(testcase, md_json_setup, md_json_teardown);
- tcase_add_test(testcase, first_test);
+ tcase_add_test(testcase, json_create_makes_object_with_refcount_one);
+ tcase_add_test(testcase, json_destroy_releases_object);
+ tcase_add_test(testcase, clearing_md_json_t_pool_releases_internal_object);
return testcase;
}
