mirror of https://github.com/getdnsapi/getdns.git
Added tests for context create and destroy
This commit is contained in:
parent
6ae6ce3bbb
commit
c8a7180a3a
|
@ -3,12 +3,15 @@
|
|||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <inttypes.h>
|
||||
#include <unistd.h>
|
||||
#include <check.h>
|
||||
#include <getdns/getdns.h>
|
||||
#include "check_getdns_libevent.h"
|
||||
#include "check_getdns_common.h"
|
||||
#include "check_getdns_general.h"
|
||||
#include "check_getdns_general_sync.h"
|
||||
#include "check_getdns_context_create.h"
|
||||
#include "check_getdns_context_destroy.h"
|
||||
#include "check_getdns_address_sync.h"
|
||||
#include "check_getdns_list_get_length.h"
|
||||
#include "check_getdns_list_get_data_type.h"
|
||||
|
@ -19,15 +22,19 @@ main (void)
|
|||
int number_failed;
|
||||
SRunner *sr ;
|
||||
|
||||
Suite *getdns_address_sync_suite(void);
|
||||
Suite *getdns_general_sync_suite(void);
|
||||
Suite *getdns_general_suite(void);
|
||||
Suite *getdns_general_sync_suite(void);
|
||||
Suite *getdns_address_sync_suite(void);
|
||||
Suite *getdns_context_create_suite(void);
|
||||
Suite *getdns_context_destroy_suite(void);
|
||||
Suite *getdns_list_get_length_suite(void);
|
||||
Suite *getdns_list_get_data_type_suite(void);
|
||||
|
||||
sr = srunner_create(getdns_general_suite());
|
||||
srunner_add_suite(sr, getdns_general_sync_suite());
|
||||
srunner_add_suite(sr, getdns_address_sync_suite());
|
||||
srunner_add_suite(sr, getdns_context_create_suite());
|
||||
srunner_add_suite(sr, getdns_context_destroy_suite());
|
||||
srunner_add_suite(sr, getdns_list_get_length_suite());
|
||||
srunner_add_suite(sr, getdns_list_get_data_type_suite());
|
||||
|
||||
|
|
|
@ -7,6 +7,8 @@
|
|||
#include <getdns/getdns.h>
|
||||
#include "check_getdns_common.h"
|
||||
|
||||
int callback_called = 0;
|
||||
|
||||
/*
|
||||
* extract_response extracts all of the various information
|
||||
* a test may want to look at from the response.
|
||||
|
@ -231,9 +233,11 @@ void assert_ptr_in_answer(struct extracted_response *ex_response)
|
|||
void callbackfn(struct getdns_context *context,
|
||||
uint16_t callback_type,
|
||||
struct getdns_dict *response,
|
||||
void *(userarg)(struct extracted_response *ex_response),
|
||||
void *userarg,
|
||||
getdns_transaction_t transaction_id)
|
||||
{
|
||||
typedef void (*fn_ptr)(struct extracted_response *ex_response);
|
||||
fn_ptr fn = userarg;
|
||||
|
||||
/*
|
||||
* If userarg is NULL, either a negative test case
|
||||
|
@ -248,6 +252,10 @@ void callbackfn(struct getdns_context *context,
|
|||
*/
|
||||
ASSERT_RC(callback_type, GETDNS_CALLBACK_COMPLETE, "Callback type");
|
||||
|
||||
/*
|
||||
printf("DICT:\n%s\n", getdns_pretty_print_dict(response));
|
||||
*/
|
||||
|
||||
/*
|
||||
* Extract the response.
|
||||
*/
|
||||
|
@ -257,6 +265,6 @@ void callbackfn(struct getdns_context *context,
|
|||
* Call the response verification function that
|
||||
* was passed via userarg.
|
||||
*/
|
||||
userarg(&ex_response);
|
||||
fn(&ex_response);
|
||||
|
||||
}
|
||||
|
|
|
@ -4,6 +4,8 @@
|
|||
#define TRUE 1
|
||||
#define FALSE 0
|
||||
#define MAXLEN 200
|
||||
|
||||
extern int callback_called;
|
||||
|
||||
struct extracted_response {
|
||||
uint32_t top_answer_type;
|
||||
|
@ -163,12 +165,10 @@
|
|||
* be called for positive tests and will verify the
|
||||
* response that is returned.
|
||||
*/
|
||||
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
|
||||
);
|
||||
void callbackfn(struct getdns_context *context,
|
||||
uint16_t callback_type,
|
||||
struct getdns_dict *response,
|
||||
void *userarg,
|
||||
getdns_transaction_t transaction_id);
|
||||
|
||||
#endif
|
||||
|
|
|
@ -0,0 +1,73 @@
|
|||
#ifndef _check_getdns_context_create_h_
|
||||
#define _check_getdns_context_create_h_
|
||||
|
||||
/*
|
||||
**************************************************************************
|
||||
* *
|
||||
* T E S T S F O R G E T D N S _ C O N T E X T _ C R E A T E *
|
||||
* *
|
||||
**************************************************************************
|
||||
*/
|
||||
|
||||
START_TEST (getdns_context_create_1)
|
||||
{
|
||||
/*
|
||||
* context = NULL
|
||||
* expect: GETDNS_RETURN_GENERIC_ERROR
|
||||
*/
|
||||
|
||||
ASSERT_RC(getdns_context_create(NULL, TRUE),
|
||||
GETDNS_RETURN_GENERIC_ERROR, "Return code from getdns_context_create()");
|
||||
}
|
||||
END_TEST
|
||||
|
||||
START_TEST (getdns_context_create_2)
|
||||
{
|
||||
/*
|
||||
* set_from_os = TRUE
|
||||
* expect: context initialized with operating system info
|
||||
* GETDNS_RETURN_GOOD
|
||||
*/
|
||||
struct getdns_context *context = NULL;
|
||||
|
||||
CONTEXT_CREATE(TRUE);
|
||||
// TODO: Do something here to verify set_from_os = TRUE
|
||||
CONTEXT_DESTROY;
|
||||
}
|
||||
END_TEST
|
||||
|
||||
START_TEST (getdns_context_create_3)
|
||||
{
|
||||
/*
|
||||
* set_from_os = FALSE
|
||||
* expect: context is not initialized with operating system info
|
||||
* GETDNS_RETURN_GOOD
|
||||
*/
|
||||
struct getdns_context *context = NULL;
|
||||
|
||||
CONTEXT_CREATE(FALSE);
|
||||
// TODO: Do something here to verify set_from_os = TRUE
|
||||
CONTEXT_DESTROY;
|
||||
}
|
||||
END_TEST
|
||||
|
||||
Suite *
|
||||
getdns_context_create_suite (void)
|
||||
{
|
||||
Suite *s = suite_create ("getdns_context_create()");
|
||||
|
||||
/* Negative test caseis */
|
||||
TCase *tc_neg = tcase_create("Negative");
|
||||
tcase_add_test(tc_neg, getdns_context_create_1);
|
||||
suite_add_tcase(s, tc_neg);
|
||||
|
||||
/* Positive test cases */
|
||||
TCase *tc_pos = tcase_create("Positive");
|
||||
tcase_add_test(tc_pos, getdns_context_create_2);
|
||||
tcase_add_test(tc_pos, getdns_context_create_3);
|
||||
suite_add_tcase(s, tc_pos);
|
||||
|
||||
return s;
|
||||
}
|
||||
|
||||
#endif
|
|
@ -0,0 +1,181 @@
|
|||
#ifndef _check_getdns_context_destroy_h_
|
||||
#define _check_getdns_context_destroy_h_
|
||||
|
||||
/*
|
||||
**************************************************************************
|
||||
* *
|
||||
* T E S T S F O R G E T D N S _ C O N T E X T _ D E S T R O Y *
|
||||
* *
|
||||
**************************************************************************
|
||||
*/
|
||||
|
||||
START_TEST (getdns_context_destroy_1)
|
||||
{
|
||||
/*
|
||||
* context = NULL
|
||||
* expect: nothing, no segmentation fault
|
||||
*/
|
||||
|
||||
getdns_context_destroy(NULL);
|
||||
}
|
||||
END_TEST
|
||||
|
||||
START_TEST (getdns_context_destroy_2)
|
||||
{
|
||||
/*
|
||||
* destroy called with valid context and no outstanding transactions
|
||||
* expect: nothing, context is freed
|
||||
*/
|
||||
struct getdns_context *context = NULL;
|
||||
|
||||
CONTEXT_CREATE(TRUE);
|
||||
CONTEXT_DESTROY;
|
||||
}
|
||||
END_TEST
|
||||
|
||||
START_TEST (getdns_context_destroy_3)
|
||||
{
|
||||
/*
|
||||
* destroy called immediately following getdns_general
|
||||
* expect: callback should be called before getdns_context_destroy() returns
|
||||
*/
|
||||
void verify_getdns_context_destroy(struct extracted_response *ex_response);
|
||||
struct getdns_context *context = NULL;
|
||||
struct event_base *event_base = NULL;
|
||||
getdns_transaction_t transaction_id = 0;
|
||||
|
||||
CONTEXT_CREATE(TRUE);
|
||||
EVENT_BASE_CREATE;
|
||||
|
||||
ASSERT_RC(getdns_general(context, "google.com", GETDNS_RRTYPE_A, NULL,
|
||||
verify_getdns_context_destroy, &transaction_id, callbackfn),
|
||||
GETDNS_RETURN_GOOD, "Return code from getdns_general()");
|
||||
|
||||
RUN_EVENT_LOOP;
|
||||
CONTEXT_DESTROY;
|
||||
|
||||
ck_assert_msg(callback_called == 1, "callback_called should == 1, got %d", callback_called);
|
||||
}
|
||||
END_TEST
|
||||
|
||||
START_TEST (getdns_context_destroy_4)
|
||||
{
|
||||
/*
|
||||
* destroy called immediately following getdns_address
|
||||
* expect: callback should be called before getdns_context_destroy() returns
|
||||
*/
|
||||
void verify_getdns_context_destroy(struct extracted_response *ex_response);
|
||||
struct getdns_context *context = NULL;
|
||||
struct event_base *event_base = NULL;
|
||||
getdns_transaction_t transaction_id = 0;
|
||||
|
||||
CONTEXT_CREATE(TRUE);
|
||||
EVENT_BASE_CREATE;
|
||||
|
||||
ASSERT_RC(getdns_address(context, "8.8.8.8", NULL,
|
||||
verify_getdns_context_destroy, &transaction_id, callbackfn),
|
||||
GETDNS_RETURN_GOOD, "Return code from getdns_address()");
|
||||
|
||||
RUN_EVENT_LOOP;
|
||||
CONTEXT_DESTROY;
|
||||
|
||||
ck_assert_msg(callback_called == 1, "callback_called should == 1, got %d", callback_called);
|
||||
}
|
||||
END_TEST
|
||||
|
||||
START_TEST (getdns_context_destroy_5)
|
||||
{
|
||||
/*
|
||||
* destroy called immediately following getdns_address
|
||||
* expect: callback should be called before getdns_context_destroy() returns
|
||||
*/
|
||||
void verify_getdns_context_destroy(struct extracted_response *ex_response);
|
||||
struct getdns_context *context = NULL;
|
||||
struct event_base *event_base = NULL;
|
||||
struct getdns_bindata address_type = { 5, (void *)"IPv4" };
|
||||
struct getdns_bindata address_data = { 4, (void *)"\x08\x08\x08\x08" };
|
||||
struct getdns_dict *address = NULL;
|
||||
getdns_transaction_t transaction_id = 0;
|
||||
|
||||
CONTEXT_CREATE(TRUE);
|
||||
EVENT_BASE_CREATE;
|
||||
|
||||
DICT_CREATE(address);
|
||||
ASSERT_RC(getdns_dict_set_bindata(address, "address_type", &address_type),
|
||||
GETDNS_RETURN_GOOD, "Return code from getdns_dict_set_bindata");
|
||||
ASSERT_RC(getdns_dict_set_bindata(address, "address_data", &address_data),
|
||||
GETDNS_RETURN_GOOD, "Return code from getdns_dict_set_bindata");
|
||||
|
||||
ASSERT_RC(getdns_hostname(context, address, NULL,
|
||||
verify_getdns_context_destroy, &transaction_id, callbackfn),
|
||||
GETDNS_RETURN_GOOD, "Return code from getdns_address()");
|
||||
|
||||
RUN_EVENT_LOOP;
|
||||
CONTEXT_DESTROY;
|
||||
|
||||
ck_assert_msg(callback_called == 1, "callback_called should == 1, got %d", callback_called);
|
||||
}
|
||||
END_TEST
|
||||
|
||||
START_TEST (getdns_context_destroy_6)
|
||||
{
|
||||
/*
|
||||
* destroy called immediately following getdns_address
|
||||
* expect: callback should be called before getdns_context_destroy() returns
|
||||
*/
|
||||
void verify_getdns_context_destroy(struct extracted_response *ex_response);
|
||||
struct getdns_context *context = NULL;
|
||||
struct event_base *event_base = NULL;
|
||||
getdns_transaction_t transaction_id = 0;
|
||||
|
||||
CONTEXT_CREATE(TRUE);
|
||||
EVENT_BASE_CREATE;
|
||||
|
||||
ASSERT_RC(getdns_service(context, "google.com", NULL,
|
||||
verify_getdns_context_destroy, &transaction_id, callbackfn),
|
||||
GETDNS_RETURN_GOOD, "Return code from getdns_service()");
|
||||
|
||||
RUN_EVENT_LOOP;
|
||||
CONTEXT_DESTROY;
|
||||
|
||||
ck_assert_msg(callback_called == 1, "callback_called should == 1, got %d", callback_called);
|
||||
}
|
||||
END_TEST
|
||||
|
||||
void verify_getdns_context_destroy(struct extracted_response *ex_response)
|
||||
{
|
||||
/*
|
||||
* Sleep for a second to make getdns_context_destroy() wait.
|
||||
*/
|
||||
sleep(1);
|
||||
|
||||
/*
|
||||
* callback_called is a global and we increment it
|
||||
* here to show that the callback was called.
|
||||
*/
|
||||
callback_called++;
|
||||
}
|
||||
|
||||
Suite *
|
||||
getdns_context_destroy_suite (void)
|
||||
{
|
||||
Suite *s = suite_create ("getdns_context_destroy()");
|
||||
|
||||
/* Negative test caseis */
|
||||
TCase *tc_neg = tcase_create("Negative");
|
||||
tcase_add_test(tc_neg, getdns_context_destroy_1);
|
||||
suite_add_tcase(s, tc_neg);
|
||||
|
||||
/* Positive test cases */
|
||||
TCase *tc_pos = tcase_create("Positive");
|
||||
tcase_add_test(tc_pos, getdns_context_destroy_2);
|
||||
tcase_add_test(tc_pos, getdns_context_destroy_3);
|
||||
tcase_add_test(tc_pos, getdns_context_destroy_4);
|
||||
tcase_add_test(tc_pos, getdns_context_destroy_5);
|
||||
tcase_add_test(tc_pos, getdns_context_destroy_6);
|
||||
suite_add_tcase(s, tc_pos);
|
||||
|
||||
return s;
|
||||
}
|
||||
|
||||
#endif
|
Loading…
Reference in New Issue