From 8e144d564843b90fcecafb55ffaaa4829696d72c Mon Sep 17 00:00:00 2001 From: Neel Goyal Date: Fri, 7 Mar 2014 14:32:35 -0500 Subject: [PATCH] Convert test programs to not use libevent. --- src/test/Makefile.in | 6 +-- src/test/tests_dnssec.c | 94 ++++++++++++++++--------------------- src/test/tests_stub_async.c | 72 ++++++++++------------------ 3 files changed, 69 insertions(+), 103 deletions(-) diff --git a/src/test/Makefile.in b/src/test/Makefile.in index b238f93a..d730d714 100644 --- a/src/test/Makefile.in +++ b/src/test/Makefile.in @@ -56,7 +56,7 @@ CHECK_EV_PROG=@CHECK_EV_PROG@ CC=@CC@ CFLAGS=@CFLAGS@ -Wall -I$(srcdir)/ -I$(srcdir)/../ -I/usr/local/include -std=c99 $(cflags) -LDFLAGS=@LDFLAGS@ -L. -L.. -L$(srcdir)/../ -L/usr/local/lib +LDFLAGS=@LDFLAGS@ -L. -L.. -L$(srcdir)/../ -L/usr/local/lib LDLIBS=-lgetdns @LIBS@ -lcheck PROGRAMS=tests_dict tests_list tests_stub_async tests_stub_sync check_getdns tests_dnssec $(CHECK_EV_PROG) $(CHECK_EVENT_PROG) $(CHECK_UV_PROG) @@ -76,7 +76,7 @@ tests_list: tests_list.o testmessages.o $(LIBTOOL) --tag=CC --mode=link $(CC) $(CFLAGS) $(LDFLAGS) $(LDLIBS) -o $@ tests_list.o testmessages.o tests_stub_async: tests_stub_async.o testmessages.o - $(LIBTOOL) --tag=CC --mode=link $(CC) $(CFLAGS) $(LDFLAGS) -lgetdns_ext_event $(LDLIBS) -o $@ tests_stub_async.o testmessages.o + $(LIBTOOL) --tag=CC --mode=link $(CC) $(CFLAGS) $(LDFLAGS) $(LDLIBS) -o $@ tests_stub_async.o testmessages.o tests_stub_sync: tests_stub_sync.o $(LIBTOOL) --tag=CC --mode=link $(CC) $(CFLAGS) $(LDFLAGS) $(LDLIBS) -o $@ tests_stub_sync.o @@ -97,7 +97,7 @@ check_getdns_ev: check_getdns.o check_getdns_common.o check_getdns_context_set_t $(LIBTOOL) --tag=CC --mode=link $(CC) $(CFLAGS) $(LDFLAGS) -lpthread -lgetdns_ext_ev $(EXTENSION_LIBEV_LDFLAGS) $(EXTENSION_LIBEV_EXT_LIBS) $(LDLIBS) -o $@ check_getdns.o check_getdns_common.o check_getdns_context_set_timeout.o check_getdns_libev.o tests_dnssec: tests_dnssec.o testmessages.o - $(LIBTOOL) --tag=CC --mode=link $(CC) $(CFLAGS) $(LDFLAGS) -lgetdns_ext_event $(LDLIBS) -o $@ tests_dnssec.o testmessages.o + $(LIBTOOL) --tag=CC --mode=link $(CC) $(CFLAGS) $(LDFLAGS) $(LDLIBS) -o $@ tests_dnssec.o testmessages.o test: all diff --git a/src/test/tests_dnssec.c b/src/test/tests_dnssec.c index 986f41e1..3c42636b 100644 --- a/src/test/tests_dnssec.c +++ b/src/test/tests_dnssec.c @@ -34,24 +34,19 @@ #include "config.h" -#ifdef HAVE_EVENT2_EVENT_H -# include -#else -# include -#endif #include #include #include #include "testmessages.h" #include -#include +#include /* Set up the callback function, which will also do the processing of the results */ void callbackfn(struct getdns_context *context, - getdns_callback_type_t callback_type, - struct getdns_dict *response, void *userarg, - getdns_transaction_t transaction_id) + getdns_callback_type_t callback_type, + struct getdns_dict *response, void *userarg, + getdns_transaction_t transaction_id) { struct getdns_list *validation_chain; struct getdns_list *trust_anchors; @@ -65,77 +60,76 @@ callbackfn(struct getdns_context *context, do { if (callback_type == GETDNS_CALLBACK_CANCEL) { fprintf(stderr, - "The callback with ID %llu was cancelled.\n", - (long long unsigned int)transaction_id); + "The callback with ID %llu was cancelled.\n", + (long long unsigned int)transaction_id); break; } else if (callback_type != GETDNS_CALLBACK_COMPLETE) { fprintf(stderr, - "The callback got a callback_type of %d.\n", - callback_type); + "The callback got a callback_type of %d.\n", + callback_type); break; } r = getdns_dict_get_list(response, - "validation_chain", &validation_chain); + "validation_chain", &validation_chain); if (r != GETDNS_RETURN_GOOD) { fprintf(stderr, - "Could not get \"validation_chain\" from response:" - " %d\n", r); + "Could not get \"validation_chain\" from response:" + " %d\n", r); break; } r = getdns_dict_get_list(response, "replies_tree", &replies_tree); if (r != GETDNS_RETURN_GOOD) { fprintf(stderr, - "Could not get \"replies_tree\" from response:" - " %d\n", r); + "Could not get \"replies_tree\" from response:" + " %d\n", r); break; } r = getdns_list_get_length(replies_tree, &replies_tree_length); if (r != GETDNS_RETURN_GOOD) { fprintf(stderr, - "Could not get length of the replies_tree:" - " %d\n", r); + "Could not get length of the replies_tree:" + " %d\n", r); break; } trust_anchors = getdns_root_trust_anchor(NULL); if (! trust_anchors) { fprintf(stderr, - "No root trust anchor present:" - " %d\n", r); + "No root trust anchor present:" + " %d\n", r); break; } for (i = 0; i < replies_tree_length; i++) { r = getdns_list_get_dict(replies_tree, i, &reply); if (r != GETDNS_RETURN_GOOD) { fprintf(stderr, - "Could not get \"reply\" from replies_tree:" - " %d\n", r); + "Could not get \"reply\" from replies_tree:" + " %d\n", r); break; } r = getdns_dict_get_list(reply, "answer", &answer); if (r != GETDNS_RETURN_GOOD) { fprintf(stderr, - "Could not get \"answer\" from reply:" - " %d\n", r); + "Could not get \"answer\" from reply:" + " %d\n", r); break; } r = getdns_list_get_length(answer, &answer_length); if (r != GETDNS_RETURN_GOOD) { fprintf(stderr, - "Could not get length of answer list:" - " %d\n", r); + "Could not get length of answer list:" + " %d\n", r); break; } if (answer_length == 0) continue; r = getdns_validate_dnssec(answer, - validation_chain, trust_anchors); + validation_chain, trust_anchors); printf("getdns_validate_dnssec returned: %d\n", r); } getdns_list_destroy(trust_anchors); } while (0); getdns_dict_destroy(response); - (void) event_base_loopexit((struct event_base *)userarg, NULL); } int @@ -144,10 +138,10 @@ main(int argc, char** argv) const char *name = argc > 1 ? argv[1] : "www.example.com"; struct getdns_context *context; struct getdns_dict *extensions; - struct event_base *event_base = NULL; getdns_transaction_t transaction_id = 0; getdns_return_t r; - + struct timeval tv; + r = getdns_context_create(&context, 1); if (r != GETDNS_RETURN_GOOD) { fprintf(stderr, "Create context failed: %d", r); @@ -168,28 +162,29 @@ main(int argc, char** argv) GETDNS_EXTENSION_TRUE); if (r != GETDNS_RETURN_GOOD) { fprintf(stderr, "Could not set extension " - "\"dnssec_return_validation_chain\": %d\n", r); + "\"dnssec_return_validation_chain\": %d\n", r); goto done_destroy_extensions; - } - - /* Create an event base and put it in the context */ - event_base = event_base_new(); - if (event_base == NULL) { - fprintf(stderr, "Trying to create the event base failed."); - r = GETDNS_RETURN_GENERIC_ERROR; - goto done_destroy_extensions; - } - (void) getdns_extension_set_libevent_base(context, event_base); + } /* Make the call */ - r = getdns_address(context, name, extensions, event_base, - &transaction_id, callbackfn); + r = getdns_address(context, name, extensions, NULL, + &transaction_id, callbackfn); if (r == GETDNS_RETURN_BAD_DOMAIN_NAME) { fprintf(stderr, "Bad domain name: %s.", name); goto done_destroy_extensions; } /* Call the event loop */ - event_base_dispatch(event_base); + while (getdns_context_get_num_pending_requests(context, &tv) > 0) { + int fd = getdns_context_fd(context); + fd_set read_fds; + FD_ZERO(&read_fds); + FD_SET(fd, &read_fds); + select(fd + 1, &read_fds, NULL, NULL, &tv); + if (getdns_context_process_async(context) != GETDNS_RETURN_GOOD) { + // context destroyed + break; + } + } /* Clean up */ done_destroy_extensions: @@ -197,12 +192,5 @@ done_destroy_extensions: done_destroy_context: getdns_context_destroy(context); - /* Event base must be destroyed after the context, because - * the context has to re-register its sockets from the eventbase, - * who has to communicate this to the system event-mechanism. - */ - if (event_base) - event_base_free(event_base); - return r; } diff --git a/src/test/tests_stub_async.c b/src/test/tests_stub_async.c index 54015f74..94345338 100644 --- a/src/test/tests_stub_async.c +++ b/src/test/tests_stub_async.c @@ -33,25 +33,20 @@ */ #include "config.h" -#ifdef HAVE_EVENT2_EVENT_H -# include -#else -# include -#endif #include #include #include #include "testmessages.h" #include -#include +#include #include /* Set up the callback function, which will also do the processing of the results */ void this_callbackfn(struct getdns_context *this_context, - getdns_callback_type_t this_callback_type, - struct getdns_dict *this_response, - void *this_userarg, getdns_transaction_t this_transaction_id) + getdns_callback_type_t this_callback_type, + struct getdns_dict *this_response, + void *this_userarg, getdns_transaction_t this_transaction_id) { if (this_callback_type == GETDNS_CALLBACK_COMPLETE) { /* This is a callback with data */ char *res = getdns_pretty_print_dict(this_response); @@ -60,12 +55,12 @@ this_callbackfn(struct getdns_context *this_context, } else if (this_callback_type == GETDNS_CALLBACK_CANCEL) fprintf(stderr, - "The callback with ID %llu was cancelled. Exiting.", - (unsigned long long)this_transaction_id); + "The callback with ID %llu was cancelled. Exiting.", + (unsigned long long)this_transaction_id); else fprintf(stderr, - "The callback got a callback_type of %d. Exiting.", - this_callback_type); + "The callback got a callback_type of %d. Exiting.", + this_callback_type); getdns_dict_destroy(this_response); } @@ -75,29 +70,16 @@ main(int argc, char** argv) /* Create the DNS context for this call */ struct getdns_context *this_context = NULL; getdns_return_t context_create_return = - getdns_context_create(&this_context, 1); + getdns_context_create(&this_context, 1); if (context_create_return != GETDNS_RETURN_GOOD) { fprintf(stderr, "Trying to create the context failed: %d", - context_create_return); + context_create_return); return (GETDNS_RETURN_GENERIC_ERROR); } getdns_context_set_resolution_type(this_context, GETDNS_RESOLUTION_STUB); getdns_context_set_timeout(this_context, 5000); /* Create an event base and put it in the context using the unknown function name */ - struct event_base *this_event_base; - this_event_base = event_base_new(); - if (this_event_base == NULL) { - fprintf(stderr, "Trying to create the event base failed."); - getdns_context_destroy(this_context); - return (GETDNS_RETURN_GENERIC_ERROR); - } - if (getdns_extension_set_libevent_base(this_context, - this_event_base) != GETDNS_RETURN_GOOD) { - fprintf(stderr, "Setting event base failed."); - getdns_context_destroy(this_context); - return (GETDNS_RETURN_GENERIC_ERROR); - } /* Set up the getdns call */ const char *this_name = argc > 1 ? argv[1] : "getdnsapi.net"; char *this_userarg = "somestring"; // Could add things here to help identify this call @@ -105,35 +87,31 @@ main(int argc, char** argv) /* Make the call */ getdns_return_t dns_request_return = - getdns_general(this_context, this_name, GETDNS_RRTYPE_A, - NULL, this_userarg, &this_transaction_id, this_callbackfn); + getdns_general(this_context, this_name, GETDNS_RRTYPE_A, + NULL, this_userarg, &this_transaction_id, this_callbackfn); if (dns_request_return == GETDNS_RETURN_BAD_DOMAIN_NAME) { fprintf(stderr, "A bad domain name was used: %s. Exiting.", - this_name); + this_name); getdns_context_destroy(this_context); - event_base_free(this_event_base); return (GETDNS_RETURN_GENERIC_ERROR); } -// dns_request_return = getdns_service(this_context, this_name, NULL, this_userarg, &this_transaction_id, -// this_callbackfn); -// if (dns_request_return == GETDNS_RETURN_BAD_DOMAIN_NAME) -// { -// fprintf(stderr, "A bad domain name was used: %s. Exiting.", this_name); -// return(GETDNS_RETURN_GENERIC_ERROR); -// } else { /* Call the event loop */ - event_base_loop(this_event_base, EVLOOP_ONCE); - while (getdns_context_get_num_pending_requests(this_context, NULL) > 0) { - event_base_loop(this_event_base, EVLOOP_ONCE); - } - // TODO: check the return value above + struct timeval tv; + while (getdns_context_get_num_pending_requests(this_context, &tv) > 0) { + int fd = getdns_context_fd(this_context); + fd_set read_fds; + FD_ZERO(&read_fds); + FD_SET(fd, &read_fds); + select(fd + 1, &read_fds, NULL, NULL, &tv); + if (getdns_context_process_async(this_context) != GETDNS_RETURN_GOOD) { + // context destroyed + break; + } + } } /* Clean up */ getdns_context_destroy(this_context); - /* the event base can only be free'd after the context has removed - * all of its events from it */ - event_base_free(this_event_base); /* Assuming we get here, leave gracefully */ exit(EXIT_SUCCESS); } /* main */