2013-08-16 14:11:46 -05:00
|
|
|
/**
|
|
|
|
* \file
|
|
|
|
* unit tests for getdns_dict helper routines, these should be used to
|
|
|
|
* perform regression tests, output must be unchanged from canonical output
|
|
|
|
* stored with the sources
|
|
|
|
*/
|
2014-01-28 08:37:35 -06:00
|
|
|
|
|
|
|
/*
|
2014-02-25 07:12:33 -06:00
|
|
|
* Copyright (c) 2013, NLNet Labs, Verisign, Inc.
|
2014-01-28 08:37:35 -06:00
|
|
|
* All rights reserved.
|
2013-08-16 14:11:46 -05:00
|
|
|
*
|
2014-01-28 08:37:35 -06:00
|
|
|
* 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
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* * Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
2014-02-25 07:23:19 -06:00
|
|
|
* * Neither the names of the copyright holders nor the
|
2014-01-28 08:37:35 -06:00
|
|
|
* names of its contributors may be used to endorse or promote products
|
|
|
|
* derived from this software without specific prior written permission.
|
2013-08-16 14:11:46 -05:00
|
|
|
*
|
2014-01-28 08:37:35 -06:00
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
|
|
|
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
|
|
|
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
|
|
|
* DISCLAIMED. IN NO EVENT SHALL Verisign, Inc. BE LIABLE FOR ANY
|
|
|
|
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
|
|
|
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
|
|
|
|
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
|
|
|
|
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
|
|
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
|
|
|
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
2013-08-16 14:11:46 -05:00
|
|
|
*/
|
|
|
|
|
2013-11-30 06:53:57 -06:00
|
|
|
#include "config.h"
|
2013-08-16 14:11:46 -05:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include "testmessages.h"
|
|
|
|
#include <getdns/getdns.h>
|
2014-03-07 13:32:35 -06:00
|
|
|
#include <getdns/getdns_extra.h>
|
2014-01-31 14:16:10 -06:00
|
|
|
#include <sys/time.h>
|
2013-08-16 14:11:46 -05:00
|
|
|
|
|
|
|
/* Set up the callback function, which will also do the processing of the results */
|
2013-11-05 14:03:44 -06:00
|
|
|
void
|
2013-12-06 08:54:06 -06:00
|
|
|
this_callbackfn(struct getdns_context *this_context,
|
2014-03-07 13:32:35 -06:00
|
|
|
getdns_callback_type_t this_callback_type,
|
|
|
|
struct getdns_dict *this_response,
|
|
|
|
void *this_userarg, getdns_transaction_t this_transaction_id)
|
2013-08-16 14:11:46 -05:00
|
|
|
{
|
2013-11-05 14:03:44 -06:00
|
|
|
if (this_callback_type == GETDNS_CALLBACK_COMPLETE) { /* This is a callback with data */
|
|
|
|
char *res = getdns_pretty_print_dict(this_response);
|
2013-11-29 09:24:39 -06:00
|
|
|
fprintf(stdout, "%s\n", res);
|
2013-11-08 17:13:49 -06:00
|
|
|
free(res);
|
2013-10-17 18:45:25 -05:00
|
|
|
|
2013-11-05 14:03:44 -06:00
|
|
|
} else if (this_callback_type == GETDNS_CALLBACK_CANCEL)
|
|
|
|
fprintf(stderr,
|
2014-03-07 13:32:35 -06:00
|
|
|
"The callback with ID %llu was cancelled. Exiting.",
|
|
|
|
(unsigned long long)this_transaction_id);
|
2013-08-16 14:11:46 -05:00
|
|
|
else
|
2013-11-05 14:03:44 -06:00
|
|
|
fprintf(stderr,
|
2014-03-07 13:32:35 -06:00
|
|
|
"The callback got a callback_type of %d. Exiting.",
|
|
|
|
this_callback_type);
|
2013-12-11 16:41:21 -06:00
|
|
|
getdns_dict_destroy(this_response);
|
2013-08-16 14:11:46 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2013-12-03 03:37:23 -06:00
|
|
|
main(int argc, char** argv)
|
2013-08-16 14:11:46 -05:00
|
|
|
{
|
|
|
|
/* Create the DNS context for this call */
|
2013-12-06 08:54:06 -06:00
|
|
|
struct getdns_context *this_context = NULL;
|
2013-11-05 14:03:44 -06:00
|
|
|
getdns_return_t context_create_return =
|
2014-03-07 13:32:35 -06:00
|
|
|
getdns_context_create(&this_context, 1);
|
2013-11-05 14:03:44 -06:00
|
|
|
if (context_create_return != GETDNS_RETURN_GOOD) {
|
|
|
|
fprintf(stderr, "Trying to create the context failed: %d",
|
2014-03-07 13:32:35 -06:00
|
|
|
context_create_return);
|
2013-11-05 14:03:44 -06:00
|
|
|
return (GETDNS_RETURN_GENERIC_ERROR);
|
2013-08-16 14:11:46 -05:00
|
|
|
}
|
2014-02-09 10:46:12 -06:00
|
|
|
getdns_context_set_resolution_type(this_context, GETDNS_RESOLUTION_STUB);
|
2013-11-05 14:03:44 -06:00
|
|
|
|
|
|
|
getdns_context_set_timeout(this_context, 5000);
|
|
|
|
/* Create an event base and put it in the context using the unknown function name */
|
2013-08-16 14:11:46 -05:00
|
|
|
/* Set up the getdns call */
|
2014-02-18 14:10:28 -06:00
|
|
|
const char *this_name = argc > 1 ? argv[1] : "getdnsapi.net";
|
2013-11-05 14:03:44 -06:00
|
|
|
char *this_userarg = "somestring"; // Could add things here to help identify this call
|
2013-08-16 14:11:46 -05:00
|
|
|
getdns_transaction_t this_transaction_id = 0;
|
2013-10-17 18:45:25 -05:00
|
|
|
|
2013-08-16 14:11:46 -05:00
|
|
|
/* Make the call */
|
2013-11-05 14:03:44 -06:00
|
|
|
getdns_return_t dns_request_return =
|
2014-03-07 13:32:35 -06:00
|
|
|
getdns_general(this_context, this_name, GETDNS_RRTYPE_A,
|
|
|
|
NULL, this_userarg, &this_transaction_id, this_callbackfn);
|
2013-11-05 14:03:44 -06:00
|
|
|
if (dns_request_return == GETDNS_RETURN_BAD_DOMAIN_NAME) {
|
|
|
|
fprintf(stderr, "A bad domain name was used: %s. Exiting.",
|
2014-03-07 13:32:35 -06:00
|
|
|
this_name);
|
2013-12-11 16:41:21 -06:00
|
|
|
getdns_context_destroy(this_context);
|
2013-11-05 14:03:44 -06:00
|
|
|
return (GETDNS_RETURN_GENERIC_ERROR);
|
2013-08-16 14:11:46 -05:00
|
|
|
}
|
2013-11-05 14:03:44 -06:00
|
|
|
else {
|
2013-08-16 14:11:46 -05:00
|
|
|
/* Call the event loop */
|
2014-03-07 13:32:35 -06:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
2013-08-16 14:11:46 -05:00
|
|
|
}
|
|
|
|
/* Clean up */
|
|
|
|
getdns_context_destroy(this_context);
|
|
|
|
/* Assuming we get here, leave gracefully */
|
|
|
|
exit(EXIT_SUCCESS);
|
2013-11-05 14:03:44 -06:00
|
|
|
} /* main */
|
2013-08-16 14:11:46 -05:00
|
|
|
|
2014-01-28 08:37:35 -06:00
|
|
|
/* tests_stub_async.c */
|