Exemplo n.º 1
0
Local<Value> GNUtil::convertToJSArray(struct getdns_list* list) {
    if (!list) {
        return Nan::Null();
    }
    size_t len;
    getdns_list_get_length(list, &len);
    Local<Array> array = Nan::New<Array>();
    for (size_t i = 0; i < len; ++i) {
        getdns_data_type type;
        getdns_list_get_data_type(list, i, &type);
        switch (type) {
            case t_bindata:
            {
                getdns_bindata* data = NULL;
                getdns_list_get_bindata(list, i, &data);
                array->Set(i, convertBinData(data, NULL));
                break;
            }
            case t_int:
            {
                uint32_t res = 0;
                getdns_list_get_int(list, i, &res);
                array->Set(i, Nan::New<Integer>(res));
                break;
            }
            case t_dict:
            {
                getdns_dict* dict = NULL;
                getdns_list_get_dict(list, i, &dict);
                array->Set(i, GNUtil::convertToJSObj(dict));
                break;
            }
            case t_list:
            {
                getdns_list* sublist = NULL;
                getdns_list_get_list(list, i, &sublist);
                array->Set(i, GNUtil::convertToJSArray(sublist));
                break;
            }
            default:
                break;
        }
    }
    return array;
}
Exemplo n.º 2
0
/**
 * test the list get and set routines 
 */
void
tst_listsetget(void)
{
	char msg[TSTMSGBUF];
	size_t index = 0;
	getdns_return_t retval;
	uint32_t ans_int;
	struct getdns_list *list = NULL;
	struct getdns_list *new_list = NULL;
	struct getdns_list *ans_list = NULL;

	tstmsg_case_begin("tst_listsetget");

	list = getdns_list_create();

	/* test get function against empty list and with bogus params */

	tstmsg_case_msg("getdns_list_get_list() empty list");
	retval = getdns_list_get_list(NULL, index, &ans_list);
	snprintf(msg, sizeof(msg),
	    "getdns_list_get_list(NULL, index, &ans_list),retval = %d",
	    retval);
	tstmsg_case_msg(msg);

	retval = getdns_list_get_list(list, index, NULL);
	snprintf(msg, sizeof(msg), "getdns_list_get_list(list, index, NULL),retval = %d",
	    retval);
	tstmsg_case_msg(msg);

	tstmsg_case_msg("getdns_list_get_list(list, 0, &ans_list)");
	retval = getdns_list_get_list(list, 0, &ans_list);
	snprintf(msg, sizeof(msg), "getdns_list_get_list,retval = %d", retval);
	tstmsg_case_msg(msg);

	tstmsg_case_msg("getdns_list_get_list(list, 1, &ans_list)");
	retval = getdns_list_get_list(list, 1, &ans_list);
	snprintf(msg, sizeof(msg), "getdns_list_get_list,retval = %d", retval);
	tstmsg_case_msg(msg);

	/* test set function against empty list with bogus params */

	tstmsg_case_msg("getdns_list_set_list(list, -1, ans_list)");
	retval = getdns_list_set_list(list, -1, NULL);
	snprintf(msg, sizeof(msg), "getdns_list_set_list,retval = %d", retval);
	tstmsg_case_msg(msg);

	tstmsg_case_msg("getdns_list_set_list(list, 1, ans_list)");
	retval = getdns_list_set_list(list, 1, NULL);
	snprintf(msg, sizeof(msg), "getdns_list_set_list,retval = %d", retval);
	tstmsg_case_msg(msg);

	/* test set and get legitimate use case */

	new_list = getdns_list_create();
	getdns_list_set_int(new_list, index, 42);

	getdns_list_set_list(list, index, new_list);
	retval = getdns_list_get_list(list, index, &ans_list);
	getdns_list_get_int(ans_list, 0, &ans_int);
	snprintf(msg, sizeof(msg), "getdns_list_set/get_list,retval = %d, ans[0] = %d",
	    retval, ans_int);
	tstmsg_case_msg(msg);

	getdns_list_destroy(new_list);
	getdns_list_destroy(list);

	tstmsg_case_end();

	return;
}				/* tst_listsetget */