From c71eaebcccb2f5ef3b15d26ec14a1064249763db Mon Sep 17 00:00:00 2001 From: Nitin Singh Date: Thu, 23 Jan 2014 10:26:13 -0500 Subject: [PATCH 1/5] add new test for getdns_list --- src/test/check_getdns.c | 16 ++ src/test/check_getdns_list_get_bindata.h | 145 +++++++++++++ src/test/check_getdns_list_get_data_type.h | 4 +- src/test/check_getdns_list_get_dict.h | 162 +++++++++++++++ src/test/check_getdns_list_get_int.h | 140 +++++++++++++ src/test/check_getdns_list_get_list.h | 150 ++++++++++++++ src/test/check_getdns_pretty_print_dict.h | 224 +++++++++++++++++++++ 7 files changed, 839 insertions(+), 2 deletions(-) create mode 100644 src/test/check_getdns_list_get_bindata.h create mode 100644 src/test/check_getdns_list_get_dict.h create mode 100644 src/test/check_getdns_list_get_int.h create mode 100644 src/test/check_getdns_list_get_list.h create mode 100644 src/test/check_getdns_pretty_print_dict.h diff --git a/src/test/check_getdns.c b/src/test/check_getdns.c index 8a5bb315..e123f320 100644 --- a/src/test/check_getdns.c +++ b/src/test/check_getdns.c @@ -16,6 +16,10 @@ #include "check_getdns_address_sync.h" #include "check_getdns_list_get_length.h" #include "check_getdns_list_get_data_type.h" +#include "check_getdns_list_get_dict.h" +#include "check_getdns_list_get_list.h" +#include "check_getdns_list_get_int.h" +#include "check_getdns_list_get_bindata.h" #include "check_getdns_dict_get_names.h" #include "check_getdns_dict_get_data_type.h" #include "check_getdns_dict_get_dict.h" @@ -29,6 +33,7 @@ #include "check_getdns_dict_set_int.h" #include "check_getdns_convert_ulabel_to_alabel.h" #include "check_getdns_convert_alabel_to_ulabel.h" +#include "check_getdns_pretty_print_dict.h" int main (void) @@ -43,11 +48,16 @@ main (void) Suite *getdns_context_destroy_suite(void); Suite *getdns_cancel_callback_suite(void); Suite *getdns_list_get_length_suite(void); + Suite *getdns_list_get_dict_suite(void); + Suite *getdns_list_get_list_suite(void); + Suite *getdns_list_get_int_suite(void); Suite *getdns_list_get_data_type_suite(void); + Suite *getdns_list_get_bindata_suite(void); Suite *getdns_dict_get_names_suite(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_dict_suite(void); Suite *getdns_dict_get_bindata_suite(void); Suite *getdns_dict_get_int_suite(void); Suite *getdns_dict_destroy_suite(void); @@ -57,6 +67,7 @@ main (void) Suite *getdns_dict_set_int_suite(void); Suite *getdns_convert_ulabel_to_alabel_suite(void); Suite *getdns_convert_alabel_to_ulabel_suite(void); + Suite *getdns_pretty_print_dict_suite(void); sr = srunner_create(getdns_general_suite()); srunner_add_suite(sr, getdns_general_sync_suite()); @@ -66,6 +77,10 @@ main (void) srunner_add_suite(sr, getdns_cancel_callback_suite()); srunner_add_suite(sr, getdns_list_get_length_suite()); srunner_add_suite(sr, getdns_list_get_data_type_suite()); + srunner_add_suite(sr, getdns_list_get_dict_suite()); + srunner_add_suite(sr, getdns_list_get_list_suite()); + srunner_add_suite(sr, getdns_list_get_int_suite()); + srunner_add_suite(sr, getdns_list_get_bindata_suite()); srunner_add_suite(sr, getdns_dict_get_names_suite()); srunner_add_suite(sr, getdns_dict_get_data_type_suite()); srunner_add_suite(sr, getdns_dict_get_dict_suite()); @@ -79,6 +94,7 @@ main (void) srunner_add_suite(sr, getdns_dict_set_int_suite()); srunner_add_suite(sr, getdns_convert_ulabel_to_alabel_suite()); srunner_add_suite(sr, getdns_convert_alabel_to_ulabel_suite()); + srunner_add_suite(sr, getdns_pretty_print_dict_suite()); srunner_set_log(sr, "check_getdns.log"); srunner_run_all(sr, CK_NORMAL); diff --git a/src/test/check_getdns_list_get_bindata.h b/src/test/check_getdns_list_get_bindata.h new file mode 100644 index 00000000..ff953111 --- /dev/null +++ b/src/test/check_getdns_list_get_bindata.h @@ -0,0 +1,145 @@ +#ifndef _check_getdns_list_get_bindata_h_ +#define _check_getdns_list_get_bindata_h_ + + /* + ************************************************************************** + * * + * T E S T S F O R G E T D N S _ L I S T _ G E T _ N A M E * + * * + ************************************************************************** + */ + + START_TEST (getdns_list_get_bindata_1) + { + /* + * list = NULL + * expect: GETDNS_RETURN_INVALID_PARAMETER + */ + struct getdns_list *list = NULL; + size_t index = 0; + struct getdns_bindata *answer = NULL; + + ASSERT_RC(getdns_list_get_bindata(list, index, &answer), + GETDNS_RETURN_INVALID_PARAMETER, "Return code from getdns_list_get_names()"); + + } + END_TEST + + START_TEST (getdns_list_get_bindata_2) + { + /* index is out of range + * create a list and and set index 0 to an int with a value of 100 + * Call getdns_get_list() for index 1 + * expect: GETDNS_RETURN_INVALID_PARAMETER + */ + struct getdns_list *list = NULL; + size_t index = 0; + + LIST_CREATE(list); + + ASSERT_RC(getdns_list_set_int(list, index, 1), + GETDNS_RETURN_GOOD, "Return code from getdns_list_set_int()"); + + index++; + ASSERT_RC(getdns_list_get_bindata(list, index, NULL), + GETDNS_RETURN_INVALID_PARAMETER, "Return code from getdns_list_get_bindata()"); + + LIST_DESTROY(list); + } + END_TEST + + START_TEST (getdns_list_get_bindata_3) + { + /* data type at index is not int + * create a list and set index 0 to an int with a value of 100 + * Call getdns_list_get_bindata() for index 0 + * expect: GETDNS_RETURN_WRONG_TYPE_REQUESTED + */ + struct getdns_list *list = NULL; + size_t index = 0; + struct getdns_bindata *answer = NULL; + + LIST_CREATE(list); + + ASSERT_RC(getdns_list_set_int(list, index, 1), + GETDNS_RETURN_GOOD, "Return code from getdns_list_set_int()"); + + + ASSERT_RC(getdns_list_get_bindata(list, index, &answer), + GETDNS_RETURN_WRONG_TYPE_REQUESTED, "Return code from getdns_list_get_bindata()"); + + LIST_DESTROY(list); + } + END_TEST + + START_TEST (getdns_list_get_bindata_4) + { + /* answer is NULL + * expect: GETDNS_RETURN_INVALID_PARAMETER + */ + struct getdns_list *list = NULL; + size_t index = 0; + + ASSERT_RC(getdns_list_get_bindata(list, index, NULL), + GETDNS_RETURN_INVALID_PARAMETER, "Return code from getdns_list_get_bindata()"); + } + END_TEST + + START_TEST (getdns_list_get_bindata_5) + { + /* + * create a list + * Create some bindata containing "bindata" and add it to the list with name = "bindata" + * Set list value at index 0 via getdns_list_set_list() to the bindata + * Call getdns_list_get_list() for index 0 + * expect: GETDNS_RETURN_GOOD + */ + struct getdns_list *list = NULL; + struct getdns_bindata bindata = { 8, (void *)"bindata" }; + size_t index = 0; + struct getdns_bindata *answer = NULL; + + LIST_CREATE(list); + + ASSERT_RC(getdns_list_set_bindata(list, index, &bindata), + GETDNS_RETURN_GOOD, "Return code from getdns_list_set_bindata()"); + + + ASSERT_RC(getdns_list_get_bindata(list, index, &answer), + GETDNS_RETURN_GOOD, "Return code from getdns_list_get_bindata()"); + + 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); + + LIST_DESTROY(list); + } + END_TEST + + + + + + + Suite * + getdns_list_get_bindata_suite (void) + { + Suite *s = suite_create ("getdns_list_get_bindata()"); + + /* Negative test caseis */ + TCase *tc_neg = tcase_create("Negative"); + tcase_add_test(tc_neg, getdns_list_get_bindata_1); + tcase_add_test(tc_neg, getdns_list_get_bindata_2); + tcase_add_test(tc_neg, getdns_list_get_bindata_3); + tcase_add_test(tc_neg, getdns_list_get_bindata_4); + suite_add_tcase(s, tc_neg); + + /* Positive test cases */ + TCase *tc_pos = tcase_create("Positive"); + tcase_add_test(tc_pos, getdns_list_get_bindata_5); + suite_add_tcase(s, tc_pos); + + return s; + } + +#endif diff --git a/src/test/check_getdns_list_get_data_type.h b/src/test/check_getdns_list_get_data_type.h index eb968533..a8e43979 100644 --- a/src/test/check_getdns_list_get_data_type.h +++ b/src/test/check_getdns_list_get_data_type.h @@ -43,7 +43,7 @@ index++; ASSERT_RC(getdns_list_get_data_type(list, index, &answer), - GETDNS_RETURN_NO_SUCH_LIST_ITEM, "Return code from getdns_list_get_data_type()"); + GETDNS_RETURN_INVALID_PARAMETER, "Return code from getdns_list_get_data_type()"); LIST_DESTROY(list); } @@ -84,7 +84,7 @@ LIST_CREATE(list); ASSERT_RC(getdns_list_get_data_type(list, index, &answer), - GETDNS_RETURN_NO_SUCH_LIST_ITEM, "Return code from getdns_list_get_data_type()"); + GETDNS_RETURN_INVALID_PARAMETER, "Return code from getdns_list_get_data_type()"); LIST_DESTROY(list); } diff --git a/src/test/check_getdns_list_get_dict.h b/src/test/check_getdns_list_get_dict.h new file mode 100644 index 00000000..41e6cabc --- /dev/null +++ b/src/test/check_getdns_list_get_dict.h @@ -0,0 +1,162 @@ +#ifndef _check_getdns_list_get_dict_h_ +#define _check_getdns_list_get_dict_h_ + + /* + ************************************************************************** + * * + * T E S T S F O R G E T D N S _ L I S T _ G E T _ D I C T * + * * + ************************************************************************** + */ + + START_TEST (getdns_list_get_dict_1) + { + /* + * list = NULL + * expect: GETDNS_RETURN_INVALID_PARAMETER + */ + struct getdns_list *list = NULL; + size_t index = 0; + struct getdns_dict *answer = NULL; + + ASSERT_RC(getdns_list_get_dict(list, index, &answer), + GETDNS_RETURN_INVALID_PARAMETER, "Return code from getdns_list_get_dict()"); + } + END_TEST + + START_TEST (getdns_list_get_dict_2) + { + /* + * index is out of range + * Create a list, add an dict to it, and then attempt + * expect: GETDNS_RETURN_INVALID_PARAMETER + */ + struct getdns_list *list = NULL; + size_t index = 0; + struct getdns_dict *answer = NULL; + + LIST_CREATE(list); + DICT_CREATE(answer); + + ASSERT_RC(getdns_list_set_int(list, index, 1), GETDNS_RETURN_GOOD, + "Return code from getdns_list_set_int()"); + + index++; + ASSERT_RC(getdns_list_get_dict(list, index, NULL), + GETDNS_RETURN_INVALID_PARAMETER, "Return code from getdns_list_get_dict()"); + + + LIST_DESTROY(list); + DICT_DESTROY(answer); + } + END_TEST + + START_TEST (getdns_list_get_dict_3) + { + /* + * data type at index is not dict + * create a list + * expect: GETDNS_RETURN_WRONG_TYPE_REQUESTED + */ + struct getdns_list *list = NULL; + struct getdns_dict *answer = NULL; + size_t index = 0; + + LIST_CREATE(list); + DICT_CREATE(answer); + + ASSERT_RC(getdns_list_set_int(list, index, 10), GETDNS_RETURN_GOOD, + "Return code from getdns_list_set_int()"); + + ASSERT_RC(getdns_list_get_dict(list, index, &answer), + GETDNS_RETURN_WRONG_TYPE_REQUESTED, "Return code from getdns_list_get_dict()"); + + LIST_DESTROY(list); + DICT_DESTROY(answer); + } + END_TEST + + + START_TEST (getdns_list_get_dict_4) + { + /* + * answer = NULL + * expect: GETDNS_RETURN_INVALID_PARAMETER + */ + struct getdns_list *list = NULL; + size_t index = 0; + + ASSERT_RC(getdns_list_get_dict(list, index, NULL), + GETDNS_RETURN_INVALID_PARAMETER, "Return code from getdns_list_get_dict()"); + } + END_TEST + + + + + + START_TEST (getdns_list_get_dict_5) + { + /* + * create a list + * Create a dict with one int (name = "ten", value = 10) + * Call getdns_dict_get_list() with name = "ten" + * expect: GETDNS_RETURN_WRONG_TYPE_REQUESTED + */ + struct getdns_dict *dict = NULL; + size_t index = 0; + struct getdns_dict *answer = NULL; + + struct getdns_list *list = NULL; + uint32_t value; + + LIST_CREATE(list); + DICT_CREATE(dict); + + ASSERT_RC(getdns_dict_set_int(dict, "ten", 10), + GETDNS_RETURN_GOOD, "Return code from getdns_dict_set_int()"); + + ASSERT_RC(getdns_list_set_dict(list, index, dict), GETDNS_RETURN_GOOD, + "Return code from getdns_list_set_dict()"); + + ASSERT_RC(getdns_list_get_dict(list, index, &answer), + GETDNS_RETURN_GOOD, "Return code from getdns_list_get_dict()"); + + ASSERT_RC(getdns_dict_get_int(answer, "ten", &value), + GETDNS_RETURN_GOOD, "Return code from getdns_dict_get_int()"); + + ck_assert_msg(value == 10, "Expected retrieved int == 10, got: %d", + value); + + + LIST_DESTROY(list); + DICT_DESTROY(dict); + } + END_TEST + + + + + + Suite * + getdns_list_get_dict_suite (void) + { + Suite *s = suite_create ("getdns_list_get_dict()"); + + /* Negative test caseis */ + TCase *tc_neg = tcase_create("Negative"); + tcase_add_test(tc_neg, getdns_list_get_dict_1); + tcase_add_test(tc_neg, getdns_list_get_dict_2); + tcase_add_test(tc_neg, getdns_list_get_dict_3); + tcase_add_test(tc_neg, getdns_list_get_dict_4); + suite_add_tcase(s, tc_neg); + + /* Positive test cases */ + TCase *tc_pos = tcase_create("Positive"); + tcase_add_test(tc_pos, getdns_list_get_dict_5); + suite_add_tcase(s, tc_pos); + + return s; + } + +#endif diff --git a/src/test/check_getdns_list_get_int.h b/src/test/check_getdns_list_get_int.h new file mode 100644 index 00000000..d99ca7a6 --- /dev/null +++ b/src/test/check_getdns_list_get_int.h @@ -0,0 +1,140 @@ +#ifndef _check_getdns_list_get_int_h_ +#define _check_getdns_list_get_int_h_ + + /* + ************************************************************************** + * * + * T E S T S F O R G E T D N S _ L I S T _ G E T _ I N T * + * * + ************************************************************************** + */ + + START_TEST (getdns_list_get_int_1) + { + /* + * list = NULL + * expect: GETDNS_RETURN_INVALID_PARAMETER + */ + struct getdns_list *list = NULL; + size_t index = 0; + uint32_t answer; + + ASSERT_RC(getdns_list_get_int(list, index, &answer), + GETDNS_RETURN_INVALID_PARAMETER, "Return code from getdns_list_get_int()"); + } + END_TEST + + START_TEST (getdns_list_get_int_2) + { + /* index is out of range + * create a list and set index 0 to an int with a value of 100 + * Call getdns_get_list() for index 1 + * expect: GETDNS_RETURN_INVALID_PARAMETER + */ + struct getdns_list *list = NULL; + size_t index = 0; + + LIST_CREATE(list); + + ASSERT_RC(getdns_list_set_int(list, index, 1), + GETDNS_RETURN_GOOD, "Return code from getdns_list_set_int()"); + + index++; + ASSERT_RC(getdns_list_get_list(list, index, NULL), + GETDNS_RETURN_INVALID_PARAMETER, "Return code from getdns_list_get_list()"); + + LIST_DESTROY(list); + } + END_TEST + + START_TEST (getdns_list_get_int_3) + { + /* data type at index is not int + * create a list + * Create some bindata containing "bindata" and add it to the list with name = "bindata" + * expect: GETDNS_RETURN_INVALID_PARAMETER + */ + struct getdns_list *list = NULL; + struct getdns_bindata bindata = { 8, (void *)"bindata" }; + size_t index = 0; + uint32_t answer; + + LIST_CREATE(list); + + ASSERT_RC(getdns_list_set_bindata(list, index, &bindata), + GETDNS_RETURN_GOOD, "Return code from getdns_list_set_bindata()"); + + + ASSERT_RC(getdns_list_get_int(list, index, &answer), + GETDNS_RETURN_WRONG_TYPE_REQUESTED, "Return code from getdns_list_get_list()"); + + LIST_DESTROY(list); + } + END_TEST + + START_TEST (getdns_list_get_int_4) + { + /* answer is NULL + * expect: GETDNS_RETURN_INVALID_PARAMETER + */ + struct getdns_list *list = NULL; + size_t index = 0; + + ASSERT_RC(getdns_list_get_int(list, index, NULL), + GETDNS_RETURN_INVALID_PARAMETER, "Return code from getdns_list_get_int()"); + } + END_TEST + + START_TEST (getdns_list_get_int_5) + { + /* create a list + * expect: GETDNS_RETURN_GOOD + */ + struct getdns_list *list = NULL; + size_t index = 0; + uint32_t answer; + + + LIST_CREATE(list); + + ASSERT_RC(getdns_list_set_int(list, index, 1), + GETDNS_RETURN_GOOD, "Return code from getdns_list_set_int()"); + + + ASSERT_RC(getdns_list_get_int(list, index, &answer), + GETDNS_RETURN_GOOD, "Return code from getdns_list_get_int()"); + + ck_assert_msg(answer == 100, "Expected retrieved int == 100, got: %d", + answer); + + LIST_DESTROY(list); + } + END_TEST + + + + + + + Suite * + getdns_list_get_int_suite (void) + { + Suite *s = suite_create ("getdns_list_get_int()"); + + /* Negative test caseis */ + TCase *tc_neg = tcase_create("Negative"); + tcase_add_test(tc_neg, getdns_list_get_int_1); + tcase_add_test(tc_neg, getdns_list_get_int_2); + tcase_add_test(tc_neg, getdns_list_get_int_3); + tcase_add_test(tc_neg, getdns_list_get_int_4); + suite_add_tcase(s, tc_neg); + + /* Positive test cases */ + TCase *tc_pos = tcase_create("Positive"); + tcase_add_test(tc_pos, getdns_list_get_int_5); + suite_add_tcase(s, tc_pos); + + return s; + } + +#endif diff --git a/src/test/check_getdns_list_get_list.h b/src/test/check_getdns_list_get_list.h new file mode 100644 index 00000000..d923bcb2 --- /dev/null +++ b/src/test/check_getdns_list_get_list.h @@ -0,0 +1,150 @@ +#ifndef _check_getdns_list_get_list_h_ +#define _check_getdns_list_get_list_h_ + + /* + ************************************************************************** + * * + * T E S T S F O R G E T D N S _ L I S T _ G E T _ L I S T * + * * + ************************************************************************** + */ + + START_TEST (getdns_list_get_list_1) + { + /* + * list = NULL + * expect: GETDNS_RETURN_INVALID_PARAMETER + */ + struct getdns_list *list = NULL; + size_t index = 0; + struct getdns_list *answer = NULL; + + ASSERT_RC(getdns_list_get_list(list, index, &answer), + GETDNS_RETURN_INVALID_PARAMETER, "Return code from getdns_list_get_list()"); + } + END_TEST + + START_TEST (getdns_list_get_list_2) + { + /* index is out of range + * create a list and set index 0 to an int with a value of 100 + * Call getdns_get_list() for index 1 + * expect: GETDNS_RETURN_INVALID_PARAMETER + */ + struct getdns_list *list = NULL; + size_t index = 0; + + LIST_CREATE(list); + + ASSERT_RC(getdns_list_set_int(list, index, 1), + GETDNS_RETURN_GOOD, "Return code from getdns_list_set_int()"); + + index++; + ASSERT_RC(getdns_list_get_list(list,index,NULL), + GETDNS_RETURN_INVALID_PARAMETER, "Return code from getdns_list_get_list()"); + + LIST_DESTROY(list); + } + END_TEST + + + START_TEST (getdns_list_get_list_3) + { + /* data type at index is not a list + * create a list and set index 0 to an int with a value of 100 + * expect: GETDNS_RETURN_WRONG_TYPE_REQUESTED + */ + struct getdns_list *list = NULL; + size_t index = 0; + struct getdns_list *answer = NULL; + + LIST_CREATE(list); + + ASSERT_RC(getdns_list_set_int(list, index, 1), + GETDNS_RETURN_GOOD, "Return code from getdns_list_set_int()"); + + ASSERT_RC(getdns_list_get_list(list, index, &answer), + GETDNS_RETURN_WRONG_TYPE_REQUESTED, "Return code from getdns_list_get_list()"); + + LIST_DESTROY(list); + } + END_TEST + + START_TEST (getdns_list_get_list_4) + { + /* answer == NULL + * expect: GETDNS_RETURN_INVALID_PARAMETER + */ + struct getdns_list *list = NULL; + size_t index = 0; + + ASSERT_RC(getdns_list_get_list(list, index, NULL), + GETDNS_RETURN_INVALID_PARAMETER, "Return code from getdns_list_get_list()"); + } + END_TEST + + START_TEST (getdns_list_get_list_5) + { + /* create a list. Also create a second list + * Set list value at index 0 in the second list via getdns_list_set_int() to 45 + * Set list value at index 0 in the first list via getdns_list_set_list() to the second list + * Call getdns_list_get_list() for index 0 against the first list + * Call getdns_list_get_int() for index 0 against retrieved list + * expect: GETDNS_RETURN_GOOD + */ + struct getdns_list *list = NULL; + struct getdns_list *second_list = NULL; + size_t index = 0; + struct getdns_list *answer = NULL; + uint32_t value; + + LIST_CREATE(list); + LIST_CREATE(second_list); + + ASSERT_RC(getdns_list_set_int(second_list, index, 45), + GETDNS_RETURN_GOOD, "Return code from getdns_list_set_int()"); + + ASSERT_RC(getdns_list_set_list(list, index, second_list), + GETDNS_RETURN_GOOD, "Return code from getdns_list_set_list()"); + + ASSERT_RC(getdns_list_get_list(list, index, &answer), + GETDNS_RETURN_GOOD, "Return code from getdns_list_get_list()"); + + ASSERT_RC(getdns_list_get_int(answer, index, &value), + GETDNS_RETURN_GOOD, "Return code from getdns_list_get_int()"); + + ck_assert_msg(value == 45, "Expected retrieved int == 45, got: %d", + value); + + LIST_DESTROY(list); + LIST_DESTROY(second_list); + } + END_TEST + + + + + + + Suite * + getdns_list_get_list_suite (void) + { + Suite *s = suite_create ("getdns_list_get_list()"); + + /* Negative test caseis */ + TCase *tc_neg = tcase_create("Negative"); + tcase_add_test(tc_neg, getdns_list_get_list_1); + tcase_add_test(tc_neg, getdns_list_get_list_2); + tcase_add_test(tc_neg, getdns_list_get_list_3); + tcase_add_test(tc_neg, getdns_list_get_list_4); + suite_add_tcase(s, tc_neg); + + /* Positive test cases */ + TCase *tc_pos = tcase_create("Positive"); + tcase_add_test(tc_pos, getdns_list_get_list_5); + suite_add_tcase(s, tc_pos); + + return s; + } + +#endif diff --git a/src/test/check_getdns_pretty_print_dict.h b/src/test/check_getdns_pretty_print_dict.h new file mode 100644 index 00000000..c7d215df --- /dev/null +++ b/src/test/check_getdns_pretty_print_dict.h @@ -0,0 +1,224 @@ +#ifndef _check_getdns_pretty_print_dict_h_ +#define _check_getdns_pretty_print_dict_h_ + + /* + ************************************************************************** + * * + * T E S T S F O R G E T D N S _ D I C T _ D E S T R O Y * + * * + ************************************************************************** + */ + + START_TEST (getdns_pretty_print_dict_1) + { + /* + * this_dict = NULL + * expect: nothing + */ + struct getdns_dict *some_dict = NULL; + + DICT_DESTROY(some_dict); + } + END_TEST + + START_TEST (getdns_pretty_print_dict_2) + { + /* + * build a complex dict and then print it + * + * dict1-> "int" = 1 + * -> "bindata" = { 8, "bindata" } + * -> "dict" = dict2->"int" = 2 + * -> "bindata" = { 8, "bindata" } + * -> "dict" = dict3 -> "int" = 3 + * -> "bindata" = { 8, "bindata" } + * -> "dict" = dict4 -> "int" = 4 + * -> "list" = list1 0: int = 5 + * -> "list" = list2 0: int = 6 + * 1: bindata = { 8, "bindata" } + * 2: dict = dict5 -> "bindata" = { 8, "bindata" } + * 3: list = list3 0: bindata = { 8, "bindata" } + * -> "list" = list4 0: int = 6 + * 1: bindata = { 8, "bindata" } + * 2: dict6 -> "int" = 8 + * -> "bindata" = { 8, "bindata" } + * -> "dict" = dict7 -> "int" = 9 + * -> "list" = list5 0: int = 10 + * 3: list6 0: int = 11 + * 1: bindata = { 8, "bindata" } + * 2: dict8 -> "bindata" = { 8, "bindata" } + * 3: list7 0: bindata = { 8, "bindata" } + * + * expect: nothing + */ + struct getdns_bindata bindata = { 8, (void *)"bindata" }; + struct getdns_list *list7; + struct getdns_dict *dict8; + struct getdns_list *list6; + struct getdns_list *list5; + struct getdns_dict *dict7; + struct getdns_dict *dict6; + struct getdns_list *list4; + struct getdns_list *list3; + struct getdns_dict *dict5; + struct getdns_list *list2; + struct getdns_list *list1; + struct getdns_dict *dict4; + struct getdns_dict *dict3; + struct getdns_dict *dict2; + struct getdns_dict *dict1; + + /* + * Build it backwards, with the deepest elements first. + */ + + LIST_CREATE(list7); + ASSERT_RC(getdns_list_set_bindata(list7, 0, &bindata), + GETDNS_RETURN_GOOD, "Return code from getdns_dict_set_bindata()"); + + DICT_CREATE(dict8); + ASSERT_RC(getdns_dict_set_bindata(dict8, "bindata", &bindata), + GETDNS_RETURN_GOOD, "Return code from getdns_dict_set_bindata()"); + + LIST_CREATE(list6); + ASSERT_RC(getdns_list_set_int(list6, 0, 11), + GETDNS_RETURN_GOOD, "Return code from getdns_list_set_int()"); + ASSERT_RC(getdns_list_set_bindata(list6, 1, &bindata), + GETDNS_RETURN_GOOD, "Return code from getdns_list_set_bindata()"); + ASSERT_RC(getdns_list_set_dict(list6, 2, dict8), + GETDNS_RETURN_GOOD, "Return code from getdns_list_set_dict()"); + ASSERT_RC(getdns_list_set_list(list6, 3, list7), + GETDNS_RETURN_GOOD, "Return code from getdns_list_set_list()"); + + LIST_CREATE(list5); + ASSERT_RC(getdns_list_set_int(list5, 0, 10), + GETDNS_RETURN_GOOD, "Return code from getdns_list_set_int()"); + + DICT_CREATE(dict7); + ASSERT_RC(getdns_dict_set_int(dict7, "int", 9), + GETDNS_RETURN_GOOD, "Return code from getdns_dict_set_int()"); + + DICT_CREATE(dict6); + ASSERT_RC(getdns_dict_set_int(dict6, "int", 8), + GETDNS_RETURN_GOOD, "Return code from getdns_dict_set_int()"); + ASSERT_RC(getdns_dict_set_bindata(dict6, "bindata", &bindata), + GETDNS_RETURN_GOOD, "Return code from getdns_dict_set_bindata()"); + ASSERT_RC(getdns_dict_set_dict(dict6, "dict", dict7), + GETDNS_RETURN_GOOD, "Return code from getdns_dict_set_dict()"); + ASSERT_RC(getdns_dict_set_list(dict6, "list", list5), + GETDNS_RETURN_GOOD, "Return code from getdns_dict_set_list()"); + + LIST_CREATE(list4); + ASSERT_RC(getdns_list_set_int(list4, 0, 7), + GETDNS_RETURN_GOOD, "Return code from getdns_list_set_int()"); + ASSERT_RC(getdns_list_set_bindata(list4, 1, &bindata), + GETDNS_RETURN_GOOD, "Return code from getdns_list_set_bindata()"); + ASSERT_RC(getdns_list_set_dict(list4, 2, dict6), + GETDNS_RETURN_GOOD, "Return code from getdns_list_set_dict()"); + ASSERT_RC(getdns_list_set_list(list4, 3, list5), + GETDNS_RETURN_GOOD, "Return code from getdns_list_set_list()"); + + LIST_CREATE(list3); + ASSERT_RC(getdns_list_set_bindata(list3, 0, &bindata), + GETDNS_RETURN_GOOD, "Return code from getdns_list_set_bindata()"); + + DICT_CREATE(dict5); + ASSERT_RC(getdns_dict_set_bindata(dict5, "bindata", &bindata), + GETDNS_RETURN_GOOD, "Return code from getdns_dict_set_bindata()"); + + LIST_CREATE(list2); + ASSERT_RC(getdns_list_set_int(list2, 0, 6), + GETDNS_RETURN_GOOD, "Return code from getdns_list_set_int()"); + ASSERT_RC(getdns_list_set_bindata(list2, 1, &bindata), + GETDNS_RETURN_GOOD, "Return code from getdns_list_set_bindata()"); + ASSERT_RC(getdns_list_set_dict(list2, 2, dict5), + GETDNS_RETURN_GOOD, "Return code from getdns_list_set_dict()"); + ASSERT_RC(getdns_list_set_list(list2, 3, list3), + GETDNS_RETURN_GOOD, "Return code from getdns_list_set_list()"); + + LIST_CREATE(list1); + ASSERT_RC(getdns_list_set_int(list1, 0, 5), + GETDNS_RETURN_GOOD, "Return code from getdns_list_set_int()"); + + DICT_CREATE(dict4); + ASSERT_RC(getdns_dict_set_int(dict4, "int", 4), + GETDNS_RETURN_GOOD, "Return code from getdns_dict_set_int()"); + + DICT_CREATE(dict3); + ASSERT_RC(getdns_dict_set_int(dict3, "int", 3), + GETDNS_RETURN_GOOD, "Return code from getdns_dict_set_int()"); + ASSERT_RC(getdns_dict_set_bindata(dict3, "bindata", &bindata), + GETDNS_RETURN_GOOD, "Return code from getdns_dict_set_bindata()"); + ASSERT_RC(getdns_dict_set_dict(dict3, "dict", dict4), + GETDNS_RETURN_GOOD, "Return code from getdns_dict_set_dict()"); + ASSERT_RC(getdns_dict_set_list(dict3, "list", list1), + GETDNS_RETURN_GOOD, "Return code from getdns_dict_set_list()"); + + DICT_CREATE(dict2); + ASSERT_RC(getdns_dict_set_int(dict2, "int", 2), + GETDNS_RETURN_GOOD, "Return code from getdns_dict_set_int()"); + ASSERT_RC(getdns_dict_set_bindata(dict2, "bindata", &bindata), + GETDNS_RETURN_GOOD, "Return code from getdns_dict_set_bindata()"); + ASSERT_RC(getdns_dict_set_dict(dict2, "dict", dict3), + GETDNS_RETURN_GOOD, "Return code from getdns_dict_set_dict()"); + ASSERT_RC(getdns_dict_set_list(dict2, "list", list2), + GETDNS_RETURN_GOOD, "Return code from getdns_dict_set_list()"); + + DICT_CREATE(dict1); + ASSERT_RC(getdns_dict_set_int(dict1, "int", 1), + GETDNS_RETURN_GOOD, "Return code from getdns_dict_set_int()"); + ASSERT_RC(getdns_dict_set_bindata(dict1, "bindata", &bindata), + GETDNS_RETURN_GOOD, "Return code from getdns_dict_set_bindata()"); + ASSERT_RC(getdns_dict_set_dict(dict1, "dict", dict2), + GETDNS_RETURN_GOOD, "Return code from getdns_dict_set_dict()"); + ASSERT_RC(getdns_dict_set_list(dict1, "list", list4), + GETDNS_RETURN_GOOD, "Return code from getdns_dict_set_list()"); + + // ck_assert_msg(answer == 100, "Expected retrieved int == 100, got: %d", + // answer); + + + /* + * Destroy all of the sub-dicts and sub-lists + */ + LIST_DESTROY(list7); + DICT_DESTROY(dict8); + LIST_DESTROY(list6); + LIST_DESTROY(list5); + DICT_DESTROY(dict7); + DICT_DESTROY(dict6); + LIST_DESTROY(list4); + LIST_DESTROY(list3); + DICT_DESTROY(dict5); + LIST_DESTROY(list2); + LIST_DESTROY(list1); + DICT_DESTROY(dict4); + DICT_DESTROY(dict3); + DICT_DESTROY(dict2); + + /* + * And now destroy the mother of all ints, bindata, dicts, and lists + */ + DICT_DESTROY(dict1); + } + END_TEST + + Suite * + getdns_pretty_print_dict_suite (void) + { + Suite *s = suite_create ("getdns_pretty_print_dict()"); + + /* Negative test caseis */ + TCase *tc_neg = tcase_create("Negative"); + tcase_add_test(tc_neg, getdns_pretty_print_dict_1); + suite_add_tcase(s, tc_neg); + + /* Positive test cases */ + TCase *tc_pos = tcase_create("Positive"); + tcase_add_test(tc_pos, getdns_pretty_print_dict_2); + suite_add_tcase(s, tc_pos); + + return s; + } + +#endif From 90e465ba9f72c37ae93f24146275cbf15b542d65 Mon Sep 17 00:00:00 2001 From: Nitin Singh Date: Thu, 23 Jan 2014 15:52:18 -0500 Subject: [PATCH 2/5] fix code --- src/test/check_getdns_list_get_int.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/test/check_getdns_list_get_int.h b/src/test/check_getdns_list_get_int.h index d99ca7a6..d28f5087 100644 --- a/src/test/check_getdns_list_get_int.h +++ b/src/test/check_getdns_list_get_int.h @@ -104,7 +104,7 @@ ASSERT_RC(getdns_list_get_int(list, index, &answer), GETDNS_RETURN_GOOD, "Return code from getdns_list_get_int()"); - ck_assert_msg(answer == 100, "Expected retrieved int == 100, got: %d", + ck_assert_msg(answer == 1, "Expected retrieved int == 1, got: %d", answer); LIST_DESTROY(list); From 01013c6be8f9e8cb2df0e41efdc39d4ecdf808f9 Mon Sep 17 00:00:00 2001 From: Neel Goyal Date: Fri, 24 Jan 2014 11:21:49 -0500 Subject: [PATCH 3/5] Tweak makefile to only build libgetdns on all --- src/Makefile.in | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/Makefile.in b/src/Makefile.in index e8e39616..3bd2c639 100644 --- a/src/Makefile.in +++ b/src/Makefile.in @@ -46,7 +46,11 @@ GETDNS_OBJ=sync.lo context.lo list.lo dict.lo convert.lo general.lo \ default: all all: libgetdns.la + +test: cd test && $(MAKE) $@ + +example: cd example && $(MAKE) $@ install: libgetdns.la From ed62358d0175d3a59804280b128327e36b483e38 Mon Sep 17 00:00:00 2001 From: Bryan Graham Date: Mon, 27 Jan 2014 19:43:12 +0000 Subject: [PATCH 4/5] fixed assert on return code for NULL input values --- src/test/check_getdns_convert_alabel_to_ulabel.h | 4 ++-- src/test/check_getdns_convert_ulabel_to_alabel.h | 5 +++-- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/src/test/check_getdns_convert_alabel_to_ulabel.h b/src/test/check_getdns_convert_alabel_to_ulabel.h index 060d545b..e824ef7d 100644 --- a/src/test/check_getdns_convert_alabel_to_ulabel.h +++ b/src/test/check_getdns_convert_alabel_to_ulabel.h @@ -17,8 +17,8 @@ */ char *alabel = NULL; - ck_assert_msg(strcmp( getdns_convert_alabel_to_ulabel( alabel ), "nil" ) == 0, - "Was not expecting %s from getdns_convert_alabel_to_ulabel()", getdns_convert_alabel_to_ulabel( alabel ) ); + ck_assert_msg( getdns_convert_alabel_to_ulabel( alabel ) == 0, + "Was not expecting %d from getdns_convert_alabel_to_ulabel()", getdns_convert_alabel_to_ulabel( alabel ) ); } END_TEST diff --git a/src/test/check_getdns_convert_ulabel_to_alabel.h b/src/test/check_getdns_convert_ulabel_to_alabel.h index 06ab19ae..5cb382bf 100644 --- a/src/test/check_getdns_convert_ulabel_to_alabel.h +++ b/src/test/check_getdns_convert_ulabel_to_alabel.h @@ -17,8 +17,9 @@ */ char *ulabel = NULL; - ck_assert_msg(strcmp( getdns_convert_ulabel_to_alabel( ulabel ), "nil" ) == 0, - "Was not expecting %s from getdns_convert_ulabel_to_alabel()", getdns_convert_ulabel_to_alabel( ulabel ) ); + + ck_assert_msg(( getdns_convert_ulabel_to_alabel( ulabel ) == 0 ), + "Was not expecting %d from getdns_convert_ulabel_to_alabel()", getdns_convert_ulabel_to_alabel( ulabel ) ); } END_TEST From 4c0e13ac76d737e823fc4cee7a4e1b853e407ffb Mon Sep 17 00:00:00 2001 From: Neel Goyal Date: Mon, 27 Jan 2014 19:32:54 -0500 Subject: [PATCH 5/5] Attempt to address issue #46 --- Makefile.in | 3 +++ src/general.c | 5 +++++ src/sync.c | 5 +++++ src/util-internal.c | 55 ++++++++++++++++++++++++++++++++++++++++----- src/util-internal.h | 7 +++++- 5 files changed, 69 insertions(+), 6 deletions(-) diff --git a/Makefile.in b/Makefile.in index e052d610..472ab2e9 100644 --- a/Makefile.in +++ b/Makefile.in @@ -31,6 +31,9 @@ uninstall: doc: cd doc && $(MAKE) $@ +example: + cd src && $(MAKE) $@ + test: cd src && $(MAKE) $@ diff --git a/src/general.c b/src/general.c index c5c80b31..35dfef13 100644 --- a/src/general.c +++ b/src/general.c @@ -341,6 +341,11 @@ getdns_general(struct getdns_context *context, return GETDNS_RETURN_INVALID_PARAMETER; } + extcheck = validate_dname(name); + if (extcheck != GETDNS_RETURN_GOOD) { + return extcheck; + } + extcheck = validate_extensions(extensions); if (extcheck != GETDNS_RETURN_GOOD) return extcheck; diff --git a/src/sync.c b/src/sync.c index 5519c757..48b0ad84 100644 --- a/src/sync.c +++ b/src/sync.c @@ -71,6 +71,11 @@ getdns_general_sync(struct getdns_context *context, getdns_return_t response_status; RETURN_IF_NULL(context, GETDNS_RETURN_BAD_CONTEXT); RETURN_IF_NULL(response, GETDNS_RETURN_INVALID_PARAMETER); + RETURN_IF_NULL(name, GETDNS_RETURN_INVALID_PARAMETER); + response_status = validate_dname(name); + if (response_status != GETDNS_RETURN_GOOD) { + return response_status; + } response_status = validate_extensions(extensions); if (response_status == GETDNS_RETURN_GOOD) { response_status = getdns_general_ub(context->unbound_sync, diff --git a/src/util-internal.c b/src/util-internal.c index e71e559c..9769653e 100644 --- a/src/util-internal.c +++ b/src/util-internal.c @@ -11,7 +11,7 @@ /* * Copyright (c) 2013, Versign, Inc. * All rights reserved. - * + * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: * * Redistributions of source code must retain the above copyright @@ -374,7 +374,7 @@ add_only_addresses(struct getdns_list * addrs, ldns_rr_list * rr_list) int r = GETDNS_RETURN_GOOD; size_t i = 0; size_t item_idx = 0; - + r = getdns_list_get_length(addrs, &item_idx); for (i = 0; r == GETDNS_RETURN_GOOD && i < ldns_rr_list_rr_count(rr_list); ++i) { @@ -405,7 +405,7 @@ add_only_addresses(struct getdns_list * addrs, ldns_rr_list * rr_list) ? &IPv4_str_bindata : &IPv6_str_bindata)); r |= getdns_dict_set_bindata(this_address, GETDNS_STR_ADDRESS_DATA, &rbin); - r |= getdns_list_set_dict(addrs, item_idx++, + r |= getdns_list_set_dict(addrs, item_idx++, this_address); getdns_dict_destroy(this_address); } @@ -418,7 +418,7 @@ create_reply_dict(struct getdns_context *context, getdns_network_req * req, struct getdns_list * just_addrs) { /* turn a packet into this glorious structure - * + * * { # This is the first reply * "header": { "id": 23456, "qr": 1, "opcode": 0, ... }, * "question": { "qname": , "qtype": 1, "qclass": 1 }, @@ -454,7 +454,7 @@ create_reply_dict(struct getdns_context *context, getdns_network_req * req, * "canonical_name": , * "answer_type": GETDNS_NAMETYPE_DNS * } - * + * */ int r = 0; ldns_pkt *reply = req->result; @@ -677,4 +677,49 @@ validate_extensions(struct getdns_dict * extensions) return GETDNS_RETURN_GOOD; } /* validate_extensions */ +getdns_return_t +validate_dname(const char* dname) { + int len; + int label_len; + const char* s; + if (dname == NULL) { + return GETDNS_RETURN_INVALID_PARAMETER; + } + len = strlen(dname); + if (len >= GETDNS_MAX_DNAME_LEN || len == 0) { + return GETDNS_RETURN_BAD_DOMAIN_NAME; + } + if (len == 1 && dname[0] == '.') { + /* root is ok */ + return GETDNS_RETURN_GOOD; + } + label_len = 0; + for (s = dname; *s; ++s) { + switch (*s) { + case '.': + if (label_len > GETDNS_MAX_LABEL_LEN || + label_len == 0) { + return GETDNS_RETURN_BAD_DOMAIN_NAME; + } + label_len = 0; + break; + default: + if ((*s >= 'a' && *s <= 'z') || + (*s >= 'A' && *s <= 'Z') || + (*s >= '0' && *s <= '9')) { + label_len++; + } else if (*s == '-' && label_len != 0) { + label_len++; + } else { + return GETDNS_RETURN_BAD_DOMAIN_NAME; + } + break; + } + } + if (label_len > GETDNS_MAX_LABEL_LEN) { + return GETDNS_RETURN_BAD_DOMAIN_NAME; + } + return GETDNS_RETURN_GOOD; +} + /* util-internal.c */ diff --git a/src/util-internal.h b/src/util-internal.h index 04398957..d39b2c5d 100644 --- a/src/util-internal.h +++ b/src/util-internal.h @@ -10,7 +10,7 @@ /* * Copyright (c) 2013, Versign, Inc. * All rights reserved. - * + * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: * * Redistributions of source code must retain the above copyright @@ -41,6 +41,9 @@ #include #include "context.h" +#define GETDNS_MAX_DNAME_LEN 255 +#define GETDNS_MAX_LABEL_LEN 63 + /** * add an item to the tail of a list - note that this was not in the getdns API * description but the list_set functions seem to be designed to modify an existing @@ -96,6 +99,8 @@ getdns_return_t getdns_dict_util_get_string(struct getdns_dict * dict, char *nam char **result); char *reverse_address(struct getdns_bindata *address_data); +getdns_return_t validate_dname(const char* dname); + /** * detect unrecognized extension strings or invalid extension formats * TODO: this could be optimized by searching a sorted list