diff --git a/src/test/check_getdns.c b/src/test/check_getdns.c index 00923caf..bc472ff3 100644 --- a/src/test/check_getdns.c +++ b/src/test/check_getdns.c @@ -20,6 +20,7 @@ #include "check_getdns_dict_get_data_type.h" #include "check_getdns_dict_get_dict.h" #include "check_getdns_dict_get_list.h" +#include "check_getdns_dict_get_bindata.h" int main (void) @@ -39,6 +40,7 @@ main (void) Suite *getdns_dict_get_data_type_suite(void); Suite *getdns_dict_get_dict_suite(void); Suite *getdns_dict_get_list_suite(void); + Suite *getdns_dict_get_bindata_suite(void); sr = srunner_create(getdns_general_suite()); srunner_add_suite(sr, getdns_general_sync_suite()); @@ -52,6 +54,7 @@ main (void) srunner_add_suite(sr, getdns_dict_get_data_type_suite()); srunner_add_suite(sr, getdns_dict_get_dict_suite()); srunner_add_suite(sr, getdns_dict_get_list_suite()); + srunner_add_suite(sr, getdns_dict_get_bindata_suite()); srunner_set_log(sr, "check_getdns.log"); srunner_run_all(sr, CK_NORMAL); diff --git a/src/test/check_getdns_dict_get_bindata.h b/src/test/check_getdns_dict_get_bindata.h new file mode 100644 index 00000000..637d7c2d --- /dev/null +++ b/src/test/check_getdns_dict_get_bindata.h @@ -0,0 +1,169 @@ +#ifndef _check_getdns_dict_get_bindata_h_ +#define _check_getdns_dict_get_bindata_h_ + + /* + ************************************************************************** + * * + * T E S T S F O R G E T D N S _ D I C T _ G E T _ B I N D A T A * + * * + ************************************************************************** + */ + + START_TEST (getdns_dict_get_bindata_1) + { + /* + * this_dict = NULL + * expect: GETDNS_RETURN_NO_SUCH_DICT_NAME + */ + struct getdns_dict *this_dict = NULL; + struct getdns_bindata *answer = NULL; + + ASSERT_RC(getdns_dict_get_bindata(this_dict, "key", &answer), + GETDNS_RETURN_NO_SUCH_DICT_NAME, "Return code from getdns_dict_get_bindata()"); + + } + END_TEST + + START_TEST (getdns_dict_get_bindata_2) + { + /* + * name = NULL + * expect: GETDNS_RETURN_NO_SUCH_DICT_NAME + */ + struct getdns_dict *this_dict = NULL; + struct getdns_bindata bindata = { 8, (void *)"bindata" }; + struct getdns_bindata *answer = NULL; + + DICT_CREATE(this_dict); + ASSERT_RC(getdns_dict_set_bindata(this_dict, "bindata", &bindata), + GETDNS_RETURN_GOOD, "Return code from getdns_dict_set_bindata()"); + + ASSERT_RC(getdns_dict_get_bindata(this_dict, NULL, &answer), + GETDNS_RETURN_NO_SUCH_DICT_NAME, "Return code from getdns_dict_get_bindata()"); + + DICT_DESTROY(this_dict); + } + END_TEST + + START_TEST (getdns_dict_get_bindata_3) + { + /* + * name does not exist in dict + * Create a dict + * Create some bindata containing "bindata" and add it to the dict with name = "bindata" + * Call getdns_dict_get_bindata() against the first dict with name = "bindata1" + * expect: GETDNS_RETURN_NO_SUCH_DICT_NAME + */ + struct getdns_dict *this_dict = NULL; + struct getdns_bindata bindata = { 8, (void *)"bindata" }; + struct getdns_bindata *answer = NULL; + + DICT_CREATE(this_dict); + ASSERT_RC(getdns_dict_set_bindata(this_dict, "bindata", &bindata), + GETDNS_RETURN_GOOD, "Return code from getdns_dict_set_bindata()"); + + ASSERT_RC(getdns_dict_get_bindata(this_dict, "bindata1", &answer), + GETDNS_RETURN_NO_SUCH_DICT_NAME, "Return code from getdns_dict_get_bindata()"); + + DICT_DESTROY(this_dict); + } + END_TEST + + START_TEST (getdns_dict_get_bindata_4) + { + /* + * data type at name is not bindata + * Create a dict with one int (name = "ten", value = 10) + * Call getdns_dict_get_bindata() with name = "ten" + * expect: GETDNS_RETURN_WRONG_TYPE_REQUESTED + */ + struct getdns_dict *this_dict = NULL; + struct getdns_bindata *answer = NULL; + + DICT_CREATE(this_dict); + + ASSERT_RC(getdns_dict_set_int(this_dict, "ten", 10), + GETDNS_RETURN_GOOD, "Return code from getdns_dict_set_int()"); + + ASSERT_RC(getdns_dict_get_bindata(this_dict, "ten", &answer), + GETDNS_RETURN_WRONG_TYPE_REQUESTED, "Return code from getdns_dict_get_bindata()"); + + DICT_DESTROY(this_dict); + } + END_TEST + + START_TEST (getdns_dict_get_bindata_5) + { + /* + * answer = NULL + * expect: GETDNS_RETURN_NO_SUCH_DICT_NAME + */ + struct getdns_dict *this_dict = NULL; + struct getdns_bindata bindata = { 8, (void *)"bindata" }; + + DICT_CREATE(this_dict); + + ASSERT_RC(getdns_dict_set_bindata(this_dict, "bindata", &bindata), + GETDNS_RETURN_GOOD, "Return code from getdns_dict_set_bindata()"); + + ASSERT_RC(getdns_dict_get_bindata(this_dict, "bindata", NULL), + GETDNS_RETURN_NO_SUCH_DICT_NAME, "Return code from getdns_dict_get_bindata()"); + + DICT_DESTROY(this_dict); + } + END_TEST + + START_TEST (getdns_dict_get_bindata_6) + { + /* + * successful get bindata + * Create a dict + * Create some bindata containing "bindata" and add it to the dict with name = "bindata" + * Call getdns_dict_get_bindata() against the first dict with name = "bindata" + * expect: retrieved bindata should == "bindata" + */ + struct getdns_dict *this_dict = NULL; + struct getdns_bindata bindata = { 8, (void *)"bindata" }; + struct getdns_bindata *answer = NULL; + + DICT_CREATE(this_dict); + + ASSERT_RC(getdns_dict_set_bindata(this_dict, "bindata", &bindata), + GETDNS_RETURN_GOOD, "Return code from getdns_dict_set_bindata()"); + + ASSERT_RC(getdns_dict_get_bindata(this_dict, "bindata", &answer), + GETDNS_RETURN_GOOD, "Return code from getdns_dict_get_bindata()"); + + ck_assert_msg(answer->size == bindata.size, "Expected bindata size == %d, got: %d", + bindata.size, answer->size); + ck_assert_msg(strcmp((char *)answer->data, (char *)bindata.data) == 0, + "Expected bindata data to be \"%s\", got: \"%s\"", + (char *)bindata.data, (char *)answer->data); + + DICT_DESTROY(this_dict); + } + END_TEST + + Suite * + getdns_dict_get_bindata_suite (void) + { + Suite *s = suite_create ("getdns_dict_get_bindata()"); + + /* Negative test caseis */ + TCase *tc_neg = tcase_create("Negative"); + tcase_add_test(tc_neg, getdns_dict_get_bindata_1); + tcase_add_test(tc_neg, getdns_dict_get_bindata_2); + tcase_add_test(tc_neg, getdns_dict_get_bindata_3); + tcase_add_test(tc_neg, getdns_dict_get_bindata_4); + tcase_add_test(tc_neg, getdns_dict_get_bindata_5); + suite_add_tcase(s, tc_neg); + + /* Positive test cases */ + TCase *tc_pos = tcase_create("Positive"); + tcase_add_test(tc_pos, getdns_dict_get_bindata_6); + suite_add_tcase(s, tc_pos); + + return s; + } + +#endif