From c18d406389e7aa281b929df6f98a5f08947db9c3 Mon Sep 17 00:00:00 2001 From: "Craig E. Despeaux" Date: Fri, 20 Dec 2013 16:47:59 -0500 Subject: [PATCH] Implemented the rest of Neel's review feedback. --- src/test/check_getdns_common.c | 88 +++++++++++-------------------- src/test/check_getdns_common.h | 24 +++------ src/test/check_getdns_general.h | 92 ++++++++++++++++++++++++++++----- 3 files changed, 115 insertions(+), 89 deletions(-) diff --git a/src/test/check_getdns_common.c b/src/test/check_getdns_common.c index 23cfc5e7..17e2392e 100644 --- a/src/test/check_getdns_common.c +++ b/src/test/check_getdns_common.c @@ -223,66 +223,40 @@ void assert_ptr_in_answer(struct extracted_response *ex_response) } /* - * negative_callbackfn is the callback function given - * to negative asynchronous query tests when no response - * is expected. + * callbackfn is the callback function given to all + * asynchronous query tests. It is expected to only + * be called for positive tests and will verify the + * response that is returned. */ -void negative_callbackfn(struct getdns_context *context, - uint16_t callback_type, - struct getdns_dict *response, - void *userarg, - getdns_transaction_t transaction_id) -{ - ck_abort_msg("Callback should never occur for negative test cases"); -} - -/* - * positive_callbackfn is the callback function given - * to positive asynchronous query tests and will validate - * the response that is returned. - */ -void positive_callbackfn(struct getdns_context *context, - uint16_t callback_type, - struct getdns_dict *response, - void *userarg, - getdns_transaction_t transaction_id) +void callbackfn(struct getdns_context *context, + uint16_t callback_type, + struct getdns_dict *response, + void *(userarg)(struct extracted_response *ex_response), + getdns_transaction_t transaction_id) { + /* + * If userarg is NULL, either a negative test case + * erroneously reached the query state, or the value + * in userarg (verification function) was somehow + * lost in transit. + */ + ck_assert_msg(userarg != NULL, "Callback called with NULL userarg"); + + /* + * We expect the callback type to be COMPLETE. + */ ASSERT_RC(callback_type, GETDNS_CALLBACK_COMPLETE, "Callback type"); + + /* + * Extract the response. + */ EXTRACT_RESPONSE; - - if(strcmp(userarg, "getdns_general_6") == 0 || - strcmp(userarg, "getdns_general_7") == 0 || - strcmp(userarg, "getdns_general_11") == 0) - { - assert_noerror(&ex_response); - assert_nodata(&ex_response); - } - else if(strcmp(userarg, "getdns_general_8") == 0 || - strcmp(userarg, "getdns_general_12") == 0) - { - assert_noerror(&ex_response); - assert_address_in_answer(&ex_response, TRUE, FALSE); - } - else if(strcmp(userarg, "getdns_general_9") == 0) - { - assert_noerror(&ex_response); - assert_address_in_answer(&ex_response, FALSE, TRUE); - } - else if(strcmp(userarg, "getdns_general_10") == 0) - { - assert_nxdomain(&ex_response); - assert_nodata(&ex_response); - assert_soa_in_authority(&ex_response); - } - else if(strcmp(userarg, "getdns_general_13") == 0 || - strcmp(userarg, "getdns_general_14") == 0) - { - assert_noerror(&ex_response); - assert_ptr_in_answer(&ex_response); - } - else - { - ck_abort_msg("Unexpected value in userarg: %s", userarg); - } + + /* + * Call the response verification function that + * was passed via userarg. + */ + userarg(&ex_response); + } diff --git a/src/test/check_getdns_common.h b/src/test/check_getdns_common.h index 061f41ab..5ff3d866 100644 --- a/src/test/check_getdns_common.h +++ b/src/test/check_getdns_common.h @@ -158,28 +158,16 @@ void assert_ptr_in_answer(struct extracted_response *ex_response); /* - * negative_callbackfn is the callback function given - * to negative asynchronous query tests when no response - * is expected. + * callbackfn is the callback function given to all + * asynchronous query tests. It is expected to only + * be called for positive tests and will verify the + * response that is returned. */ - void negative_callbackfn( - struct getdns_context *context, - uint16_t callback_type, - struct getdns_dict *response, - void *userarg, - getdns_transaction_t transaction_id - ); - - /* - * positive_callbackfn is the callback function given - * to positive asynchronous query tests and will validate - * the response that is returned. - */ - void positive_callbackfn( + void callbackfn( struct getdns_context *context, uint16_t callback_type, struct getdns_dict *response, - void *userarg, + void *(userarg)(struct extracted_response *ex_response), getdns_transaction_t transaction_id ); diff --git a/src/test/check_getdns_general.h b/src/test/check_getdns_general.h index a1f32b42..5ccd7fd7 100644 --- a/src/test/check_getdns_general.h +++ b/src/test/check_getdns_general.h @@ -19,7 +19,7 @@ getdns_transaction_t transaction_id = 0; ASSERT_RC(getdns_general(context, "google.com", GETDNS_RRTYPE_A, NULL, - "getdns_general_1", &transaction_id, negative_callbackfn), + NULL, &transaction_id, callbackfn), GETDNS_RETURN_BAD_CONTEXT, "Return code from getdns_general()"); } END_TEST @@ -38,7 +38,7 @@ EVENT_BASE_CREATE; ASSERT_RC(getdns_general(context, NULL, GETDNS_RRTYPE_A, NULL, - "getdns_general_2", &transaction_id, negative_callbackfn), + NULL, &transaction_id, callbackfn), GETDNS_RETURN_GENERIC_ERROR, "Return code from getdns_general()"); RUN_EVENT_LOOP; @@ -61,7 +61,7 @@ EVENT_BASE_CREATE; ASSERT_RC(getdns_general(context, name, GETDNS_RRTYPE_A, NULL, - "getdns_general_3", &transaction_id, negative_callbackfn), + NULL, &transaction_id, callbackfn), GETDNS_RETURN_BAD_DOMAIN_NAME, "Return code from getdns_general()"); RUN_EVENT_LOOP; @@ -84,7 +84,7 @@ EVENT_BASE_CREATE; ASSERT_RC(getdns_general(context, name, GETDNS_RRTYPE_A, NULL, - "getdns_general_4", &transaction_id, negative_callbackfn), + NULL, &transaction_id, callbackfn), GETDNS_RETURN_BAD_DOMAIN_NAME, "Return code from getdns_general()"); RUN_EVENT_LOOP; @@ -106,7 +106,7 @@ EVENT_BASE_CREATE; ASSERT_RC(getdns_general(context, "google.com", GETDNS_RRTYPE_A, NULL, - "getdns_general_5", &transaction_id, NULL), + NULL, &transaction_id, NULL), GETDNS_RETURN_GENERIC_ERROR, "Return code from getdns_general()"); RUN_EVENT_LOOP; @@ -124,6 +124,7 @@ * rcode = 0 * ancount = 0 (number of records in ANSWER section) */ + void verify_getdns_general_6(struct extracted_response *ex_response); struct getdns_context *context = NULL; \ struct event_base *event_base = NULL; \ getdns_transaction_t transaction_id = 0; @@ -132,13 +133,19 @@ EVENT_BASE_CREATE; ASSERT_RC(getdns_general(context, "google.com", 0, NULL, - "getdns_general_6", &transaction_id, positive_callbackfn), + verify_getdns_general_6, &transaction_id, callbackfn), GETDNS_RETURN_GOOD, "Return code from getdns_general()"); RUN_EVENT_LOOP; CONTEXT_DESTROY; } END_TEST + + void verify_getdns_general_6(struct extracted_response *ex_response) + { + assert_noerror(ex_response); + assert_nodata(ex_response); + } START_TEST (getdns_general_7) { @@ -150,6 +157,7 @@ * rcode = 0 * ancount = 0 (number of records in ANSWER section) */ + void verify_getdns_general_7(struct extracted_response *ex_response); struct getdns_context *context = NULL; \ struct event_base *event_base = NULL; \ getdns_transaction_t transaction_id = 0; @@ -158,13 +166,19 @@ EVENT_BASE_CREATE; ASSERT_RC(getdns_general(context, "google.com", 65279, NULL, - "getdns_general_7", &transaction_id, positive_callbackfn), + verify_getdns_general_7, &transaction_id, callbackfn), GETDNS_RETURN_GOOD, "Return code from getdns_general()"); RUN_EVENT_LOOP; CONTEXT_DESTROY; } END_TEST + + void verify_getdns_general_7(struct extracted_response *ex_response) + { + assert_noerror(ex_response); + assert_nodata(ex_response); + } START_TEST (getdns_general_8) { @@ -177,6 +191,7 @@ * ancount >= 1 (number of records in ANSWER section) * and equals number of A records ("type": 1) in "answer" list */ + void verify_getdns_general_8(struct extracted_response *ex_response); struct getdns_context *context = NULL; \ struct event_base *event_base = NULL; \ getdns_transaction_t transaction_id = 0; @@ -185,13 +200,19 @@ EVENT_BASE_CREATE; ASSERT_RC(getdns_general(context, "google.com", GETDNS_RRTYPE_A, NULL, - "getdns_general_8", &transaction_id, positive_callbackfn), + verify_getdns_general_8, &transaction_id, callbackfn), GETDNS_RETURN_GOOD, "Return code from getdns_general()"); RUN_EVENT_LOOP; CONTEXT_DESTROY; } END_TEST + + void verify_getdns_general_8(struct extracted_response *ex_response) + { + assert_noerror(ex_response); + assert_address_in_answer(ex_response, TRUE, FALSE); + } START_TEST (getdns_general_9) { @@ -204,6 +225,7 @@ * ancount >= 1 (number of records in ANSWER section) * and equals number of AAAA records ("type": 28) in "answer" list */ + void verify_getdns_general_9(struct extracted_response *ex_response); struct getdns_context *context = NULL; \ struct event_base *event_base = NULL; \ getdns_transaction_t transaction_id = 0; @@ -212,13 +234,19 @@ EVENT_BASE_CREATE; ASSERT_RC(getdns_general(context, "google.com", GETDNS_RRTYPE_AAAA, NULL, - "getdns_general_9", &transaction_id, positive_callbackfn), + verify_getdns_general_9, &transaction_id, callbackfn), GETDNS_RETURN_GOOD, "Return code from getdns_general()"); RUN_EVENT_LOOP; CONTEXT_DESTROY; } END_TEST + + void verify_getdns_general_9(struct extracted_response *ex_response) + { + assert_noerror(ex_response); + assert_address_in_answer(ex_response, FALSE, TRUE); + } START_TEST (getdns_general_10) { @@ -232,6 +260,7 @@ * nscount = 1 (number of records in AUTHORITY section) * and SOA record ("type": 6) present in "authority" list */ + void verify_getdns_general_10(struct extracted_response *ex_response); struct getdns_context *context = NULL; \ struct event_base *event_base = NULL; \ getdns_transaction_t transaction_id = 0; @@ -241,13 +270,20 @@ EVENT_BASE_CREATE; ASSERT_RC(getdns_general(context, name, GETDNS_RRTYPE_TXT, NULL, - "getdns_general_10", &transaction_id, positive_callbackfn), + verify_getdns_general_10, &transaction_id, callbackfn), GETDNS_RETURN_GOOD, "Return code from getdns_general()"); RUN_EVENT_LOOP; CONTEXT_DESTROY; } END_TEST + + void verify_getdns_general_10(struct extracted_response *ex_response) + { + assert_nxdomain(ex_response); + assert_nodata(ex_response); + assert_soa_in_authority(ex_response); + } START_TEST (getdns_general_11) { @@ -259,6 +295,7 @@ * rcode = 0 * ancount = 0 (number of records in ANSWER section) */ + void verify_getdns_general_11(struct extracted_response *ex_response); struct getdns_context *context = NULL; \ struct event_base *event_base = NULL; \ getdns_transaction_t transaction_id = 0; @@ -267,13 +304,19 @@ EVENT_BASE_CREATE; ASSERT_RC(getdns_general(context, "hampster.com", GETDNS_RRTYPE_MX, NULL, - "getdns_general_11", &transaction_id, positive_callbackfn), + verify_getdns_general_11, &transaction_id, callbackfn), GETDNS_RETURN_GOOD, "Return code from getdns_general()"); RUN_EVENT_LOOP; CONTEXT_DESTROY; } END_TEST + + void verify_getdns_general_11(struct extracted_response *ex_response) + { + assert_noerror(ex_response); + assert_nodata(ex_response); + } START_TEST (getdns_general_12) { @@ -286,6 +329,7 @@ * ancount >= 1 (number of records in ANSWER section) * and equals number of A records ("type": 1) in "answer" list */ + void verify_getdns_general_12(struct extracted_response *ex_response); struct getdns_context *context = NULL; \ struct event_base *event_base = NULL; \ getdns_transaction_t transaction_id = 0; @@ -294,13 +338,19 @@ EVENT_BASE_CREATE; ASSERT_RC(getdns_general(context, "google.com", GETDNS_RRTYPE_A, NULL, - "getdns_general_12", &transaction_id, positive_callbackfn), + verify_getdns_general_12, &transaction_id, callbackfn), GETDNS_RETURN_GOOD, "Return code from getdns_general()"); RUN_EVENT_LOOP; CONTEXT_DESTROY; } END_TEST + + void verify_getdns_general_12(struct extracted_response *ex_response) + { + assert_noerror(ex_response); + assert_address_in_answer(ex_response, TRUE, FALSE); + } START_TEST (getdns_general_13) { @@ -313,6 +363,7 @@ * ancount == 1 (number of records in ANSWER section) * and PTR record found ("type": 12) in "answer" list */ + void verify_getdns_general_13(struct extracted_response *ex_response); struct getdns_context *context = NULL; \ struct event_base *event_base = NULL; \ getdns_transaction_t transaction_id = 0; @@ -321,13 +372,19 @@ EVENT_BASE_CREATE; ASSERT_RC(getdns_general(context, "75.101.146.66", GETDNS_RRTYPE_PTR, NULL, - "getdns_general_13", &transaction_id, positive_callbackfn), + verify_getdns_general_13, &transaction_id, callbackfn), GETDNS_RETURN_GOOD, "Return code from getdns_general()"); RUN_EVENT_LOOP; CONTEXT_DESTROY; } END_TEST + + void verify_getdns_general_13(struct extracted_response *ex_response) + { + assert_noerror(ex_response); + assert_ptr_in_answer(ex_response); + } START_TEST (getdns_general_14) { @@ -340,6 +397,7 @@ * ancount == 1 (number of records in ANSWER section) * and PTR record found ("type": 12) in "answer" list */ + void verify_getdns_general_14(struct extracted_response *ex_response); struct getdns_context *context = NULL; \ struct event_base *event_base = NULL; \ getdns_transaction_t transaction_id = 0; @@ -348,13 +406,19 @@ EVENT_BASE_CREATE; ASSERT_RC(getdns_general(context, "2607:f8b0:4006:802::1007", GETDNS_RRTYPE_PTR, NULL, - "getdns_general_14", &transaction_id, positive_callbackfn), + verify_getdns_general_14, &transaction_id, callbackfn), GETDNS_RETURN_GOOD, "Return code from getdns_general()"); RUN_EVENT_LOOP; CONTEXT_DESTROY; } END_TEST + + void verify_getdns_general_14(struct extracted_response *ex_response) + { + assert_noerror(ex_response); + assert_ptr_in_answer(ex_response); + } Suite * getdns_general_suite (void)