Improve test coverage.

* Test equality of different length strings.
* Add tab to json_pack whitespace test.
* Test json_sprintf with empty result and invalid UTF.
* Test json_get_alloc_funcs with NULL arguments.
* Test invalid arguments.
* Add test_chaos to test allocation failure code paths.
* Remove redundant json_is_string checks from json_string_equal and
  json_string_copy.  Both functions are static and can only be called
  with a json string.

Fixes to issues found by test_chaos:
* Fix crash on OOM in pack_unpack.c:read_string().
* Unconditionally free string in string_create upon allocation failure.
  Update load.c:parse_value() to reflect this.  This resolves a leak on
  allocation failure for pack_unpack.c:pack_string() and
  value.c:json_sprintf().

Although not visible from CodeCoverage these changes significantly
increase branch coverage.  Especially in src/value.c where we previously
covered 67.4% of branches and now cover 96.3% of branches.
This commit is contained in:
Corey Farrell
2018-02-13 04:35:37 -05:00
parent 6dddf687d8
commit 73c22de516
15 changed files with 444 additions and 14 deletions

View File

@@ -9,6 +9,56 @@
#include <jansson.h>
#include "util.h"
static void test_bad_args(void)
{
json_t *num = json_integer(1);
json_t *txt = json_string("test");
if (!num || !txt)
fail("failed to allocate test objects");
if(json_string_nocheck(NULL) != NULL)
fail("json_string_nocheck with NULL argument did not return NULL");
if(json_stringn_nocheck(NULL, 0) != NULL)
fail("json_stringn_nocheck with NULL argument did not return NULL");
if(json_string(NULL) != NULL)
fail("json_string with NULL argument did not return NULL");
if(json_stringn(NULL, 0) != NULL)
fail("json_stringn with NULL argument did not return NULL");
if(json_string_length(NULL) != 0)
fail("json_string_length with non-string argument did not return 0");
if(json_string_length(num) != 0)
fail("json_string_length with non-string argument did not return 0");
if(json_string_value(NULL) != NULL)
fail("json_string_value with non-string argument did not return NULL");
if(json_string_value(num) != NULL)
fail("json_string_value with non-string argument did not return NULL");
if(!json_string_setn_nocheck(NULL, "", 0))
fail("json_string_setn with non-string argument did not return error");
if(!json_string_setn_nocheck(num, "", 0))
fail("json_string_setn with non-string argument did not return error");
if(!json_string_setn_nocheck(txt, NULL, 0))
fail("json_string_setn_nocheck with NULL value did not return error");
if(!json_string_set_nocheck(txt, NULL))
fail("json_string_set_nocheck with NULL value did not return error");
if(!json_string_set(txt, NULL))
fail("json_string_set with NULL value did not return error");
if(!json_string_setn(txt, NULL, 0))
fail("json_string_setn with NULL value did not return error");
if(num->refcount != 1)
fail("unexpected reference count for num");
if(txt->refcount != 1)
fail("unexpected reference count for txt");
json_decref(num);
json_decref(txt);
}
/* Call the simple functions not covered by other tests of the public API */
static void run_tests()
{
@@ -237,4 +287,6 @@ static void run_tests()
fail("automatic decrement failed");
json_decref(value);
#endif
test_bad_args();
}