/** * \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 */ /* The MIT License (MIT) * Copyright (c) 2013 Verisign, Inc. * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal * in the Software without restriction, including without limitation the rights * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell * copies of the Software, and to permit persons to whom the Software is * furnished to do so, subject to the following conditions: * * The above copyright notice and this permission notice shall be included in * all copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN * THE SOFTWARE. */ #include #include #include #include "testmessages.h" #include #define TSTMSGBUF 80 /*---------------------------------------- tst_bindatasetget */ /** * test the bindata get and set routines */ void tst_bindatasetget(void) { char msg[TSTMSGBUF]; char key[20]; getdns_return_t retval; struct getdns_dict *dict = NULL; struct getdns_bindata *ans_bdata; struct getdns_bindata *bindata; tstmsg_case_begin("tst_bindatasetget"); dict = getdns_dict_create(); /* test int get function against empty dict and with bogus params */ strcpy(key, "foo"); tstmsg_case_msg("getdns_dict_get_bindata() empty dict"); retval = getdns_dict_get_bindata(NULL, key, &ans_bdata); sprintf(msg, "line %d: getdns_dict_get_bindata(NULL, key, &ans_bdata),retval = %d", __LINE__, retval); tstmsg_case_msg(msg); retval = getdns_dict_get_bindata(dict, key, NULL); sprintf(msg, "line %d: getdns_dict_get_bindata(dict, key, NULL),retval = %d", __LINE__, retval); tstmsg_case_msg(msg); tstmsg_case_msg("getdns_dict_get_bindata(dict, NULL, &ans_bindata)"); retval = getdns_dict_get_bindata(dict, NULL, &ans_bdata); sprintf(msg, "line %d: getdns_dict_get_bindata,retval = %d", __LINE__, retval); tstmsg_case_msg(msg); tstmsg_case_msg("getdns_dict_get_bindata(dict, key, &ans_bdata)"); retval = getdns_dict_get_bindata(dict, key, &ans_bdata); sprintf(msg, "line %d: getdns_list_get_bindata,retval = %d", __LINE__, retval); tstmsg_case_msg(msg); getdns_dict_destroy(dict); /* TODO: test getdns_dict_set functions with bogus params */ /* test set and get legitimate use case */ dict = getdns_dict_create(); strcpy(key, "foo"); bindata = (struct getdns_bindata *) malloc(sizeof(struct getdns_bindata)); bindata->size = strlen("foobar") + 1; bindata->data = (void *) strdup("foobar"); tstmsg_case_msg("getdns_dict_set_bindata(dict, key, bindata)"); retval = getdns_dict_set_bindata(dict, key, bindata); sprintf(msg, "line %d: getdns_dict_set_bindata,retval=%d,key=%s", __LINE__, retval, key); tstmsg_case_msg(msg); tstmsg_case_msg("getdns_dict_get_bindata(dict, key, &ans_bdata)"); retval = getdns_dict_get_bindata(dict, key, &ans_bdata); sprintf(msg, "line %d: getdns_dict_get_bindata,retval=%d,key=%s,data=%s", __LINE__, retval, key, ans_bdata->data); tstmsg_case_msg(msg); getdns_dict_destroy(dict); free(bindata->data); free(bindata); tstmsg_case_end(); return; } /* tst_bindatasetget */ /*---------------------------------------- tst_dictsetget */ /** * test the dict get and set routines */ void tst_dictsetget(void) { char msg[TSTMSGBUF]; char key[20]; uint32_t int1; uint32_t int2; getdns_return_t retval; struct getdns_dict *newdict; struct getdns_dict *ansdict; struct getdns_dict *dict = NULL; tstmsg_case_begin("tst_dictsetget"); dict = getdns_dict_create(); /* test get function against empty list and with bogus params */ strcpy(key, "foo"); tstmsg_case_msg("getdns_dict_get_dict() empty dict"); retval = getdns_dict_get_dict(NULL, key, &ansdict); sprintf(msg, "line %d: getdns_dict_get_dict(NULL, key, &ansdict),retval = %d", __LINE__, retval); tstmsg_case_msg(msg); retval = getdns_dict_get_dict(dict, key, NULL); sprintf(msg, "line %d: getdns_dict_get_dict(dict, key, NULL),retval = %d", __LINE__, retval); tstmsg_case_msg(msg); tstmsg_case_msg("getdns_dict_get_dict(dict, NULL, &ansdict)"); retval = getdns_dict_get_dict(dict, NULL, &ansdict); sprintf(msg, "line %d: getdns_dict_get_dict,retval = %d", __LINE__, retval); tstmsg_case_msg(msg); tstmsg_case_msg("getdns_dict_get_dict(dict, key, &ansdict)"); retval = getdns_dict_get_dict(dict, key, &ansdict); sprintf(msg, "line %d: getdns_list_get_dict,retval = %d", __LINE__, retval); tstmsg_case_msg(msg); getdns_dict_destroy(dict); /* TODO: test getdns_dict_set functions with bogus params */ /* test set and get legitimate use case */ dict = getdns_dict_create(); strcpy(key, "foo"); newdict = getdns_dict_create(); getdns_dict_set_int(newdict, "foo", 42); getdns_dict_set_int(newdict, "bar", 52); tstmsg_case_msg("getdns_dict_set_dict(dict, key, newdict)"); retval = getdns_dict_set_dict(dict, key, newdict); sprintf(msg, "line %d: getdns_dict_set_dict,retval=%d,key=%s", __LINE__, retval, key); tstmsg_case_msg(msg); getdns_dict_destroy(newdict); tstmsg_case_msg("getdns_dict_get_dict(dict, key, &ansdict)"); retval = getdns_dict_get_dict(dict, key, &ansdict); getdns_dict_get_int(ansdict, "foo", &int1); getdns_dict_get_int(ansdict, "bar", &int2); sprintf(msg, "line %d: getdns_dict_get_dict,retval=%d,key=%s,int1=%d,int2=%d" , __LINE__, retval, key, int1, int2); tstmsg_case_msg(msg); getdns_dict_destroy(dict); tstmsg_case_end(); return; } /* tst_dictsetget */ /*---------------------------------------- tst_getnames */ /** * exercise the getdns_dict_get_names function */ void tst_getnames(void) { size_t index; size_t llen; uint32_t ansint; int i; getdns_return_t result; getdns_data_type dtype; struct getdns_dict *dict = NULL; struct getdns_list *list = NULL; tstmsg_case_begin("tst_getnames"); dict = getdns_dict_create(); /* degenerative use cases */ tstmsg_case_msg("getdns_dict_get_names(NULL, &list)"); getdns_dict_get_names(NULL, &list); getdns_list_destroy(list); tstmsg_case_msg("getdns_dict_get_names(dict, NULL)"); getdns_dict_get_names(dict, NULL); tstmsg_case_msg("getdns_dict_get_names(dict, &list), empty dictionary"); getdns_dict_get_names(dict, &list); getdns_list_destroy(list); /* legit use case, add items out of order to exercise tree */ /* TODO: add elements of type dict, bindata, list to the dict */ i = 0; getdns_dict_set_int(dict, "foo", i++); getdns_dict_set_int(dict, "bar", i++); getdns_dict_set_int(dict, "quz", i++); getdns_dict_set_int(dict, "alpha", i++); getdns_dict_get_names(dict, &list); result = getdns_list_get_length(list, &llen); if(llen != i) { tstmsg_case_msg("getdns_list_get_length returned unreasonable length, exiting"); return; } for(index=0; index