From f2b1388e134ed20e5c15db56c1ef46e95194446e Mon Sep 17 00:00:00 2001 From: Albert Chu Date: Mon, 21 Aug 2017 17:23:24 -0700 Subject: [PATCH] libkvs/test: Add extra treeobj corner case tests --- src/common/libkvs/test/treeobj.c | 42 ++++++++++++++++++++++++++++++-- 1 file changed, 40 insertions(+), 2 deletions(-) diff --git a/src/common/libkvs/test/treeobj.c b/src/common/libkvs/test/treeobj.c index c5991587a6f3..38dc82d27678 100644 --- a/src/common/libkvs/test/treeobj.c +++ b/src/common/libkvs/test/treeobj.c @@ -88,6 +88,8 @@ void test_valref (void) errno = 0; ok (treeobj_get_blobref (valref, 0) == NULL && errno == EINVAL, "treeobj_get_blobref [0] fails with EINVAL"); + ok (treeobj_append_blobref (valref, "foo") < 0 && errno == EINVAL, + "treeobj_append_blobref returns EINVAL on bad blobref"); ok (treeobj_append_blobref (valref, blobrefs[0]) == 0, "treeobj_append_blobref works"); ok (treeobj_validate (valref) == 0, @@ -172,6 +174,13 @@ void test_val (void) ok (memcmp (buf, outbuf, outlen) == 0, "and returned data same as input"); free (outbuf); + ok (treeobj_decode_val (val, (void **)&outbuf, NULL) == 0, + "treeobj_decode_val works w/o len input"); + free (outbuf); + ok (treeobj_decode_val (val, NULL, &outlen) == 0, + "treeobj_decode_val works w/o data pointer input"); + ok (outlen == sizeof (buf), + "and returned size same as input"); ok ((val2 = treeobj_create_val (NULL, 0)) != NULL, "treeobj_create_val NULL, 0 works"); @@ -239,6 +248,8 @@ void test_dirref (void) errno = 0; ok (treeobj_get_blobref (dirref, 0) == NULL && errno == EINVAL, "treeobj_get_blobref [0] fails with EINVAL"); + ok (treeobj_append_blobref (dirref, "foo") < 0 && errno == EINVAL, + "treeobj_append_blobref returns EINVAL on bad blobref"); ok (treeobj_append_blobref (dirref, blobrefs[0]) == 0, "treeobj_append_blobref works"); ok (treeobj_validate (dirref) == 0, @@ -325,6 +336,15 @@ void test_dir (void) "treeobj_validate likes populated dir"); errno = 0; + ok (treeobj_get_entry (val1, "foo") == NULL && errno == EINVAL, + "treeobj_get_entry fails with EINVAL on non-dir treeobj"); + errno = 0; + ok (treeobj_delete_entry (val1, "foo") < 0 && errno == EINVAL, + "treeobj_delete_entry fails with EINVAL on non-dir treeobj"); + errno = 0; + ok (treeobj_insert_entry (val1, "foo", val1) < 0 && errno == EINVAL, + "treeobj_insert_entry fails with EINVAL on non-dir treeobj"); + errno = 0; ok (treeobj_insert_entry (dir, NULL, val1) < 0 && errno == EINVAL, "treeobj_insert_entry fails with EINVAL on NULL key"); errno = 0; @@ -449,21 +469,35 @@ void test_symlink (void) json_decref (o); } -void test_validate (void) +void test_corner_cases (void) { json_t *val, *valref, *dir, *symlink; json_t *array, *object; + char *outbuf; + int outlen; val = treeobj_create_val ("a", 1); if (!val) BAIL_OUT ("can't continue without test value"); + ok (treeobj_append_blobref (val, blobrefs[0]) < 0 && errno == EINVAL, + "treeobj_append_blobref returns EINVAL on bad treeobj"); + + ok (treeobj_get_blobref (val, 0) == NULL && errno == EINVAL, + "treeobj_get_blobref returns EINVAL on bad treeobj"); + /* Modify val to have bad type */ json_object_set_new (val, "type", json_string ("foo")); ok (treeobj_validate (val) < 0 && errno == EINVAL, "treeobj_validate detects invalid type"); + ok (treeobj_get_count (val) < 0 && errno == EINVAL, + "treeobj_get_count detects invalid type"); + + ok (treeobj_decode (treeobj_encode (val)) == NULL && errno == EPROTO, + "treeobj_decode returns EPROTO on bad treeobj"); + json_decref (val); valref = treeobj_create_valref (NULL); @@ -492,6 +526,10 @@ void test_validate (void) if (!dir) BAIL_OUT ("can't continue without test value"); + ok (treeobj_decode_val (dir, (void **)&outbuf, &outlen) < 0 + && errno == EINVAL, + "treeobj_decode_val returns EINVAL on non-val treeobj"); + /* Modify valref to have bad blobref */ object = json_object (); json_object_set_new (object, "a", json_string ("foo")); @@ -532,7 +570,7 @@ int main(int argc, char** argv) test_dir (); test_copy_dir (); test_symlink (); - test_validate (); + test_corner_cases (); test_codec ();