Bugfix in converting IPs in str2dict

+ tpkg test for server capabilities
This commit is contained in:
Hoda Rohani 2017-04-19 12:07:55 +02:00
parent 6c4af3af93
commit 6f3db561f3
7 changed files with 229 additions and 1 deletions

View File

@ -1682,8 +1682,18 @@ getdns_str2dict(const char *str, getdns_dict **dict)
str++;
if (*str != '{') {
char value_buf[3072], *value_str = value_buf;
if (strlen(str) > sizeof(value_str) - 1)
value_str = STRDUP(str);
else
(void)strncpy(value_buf, str, sizeof(value_buf));
getdns_dict *dict_r = _getdns_ipaddr_dict_mf(
&_getdns_plain_mem_funcs, str);
&_getdns_plain_mem_funcs, value_str);
if (value_str != value_buf)
free(value_str);
if (dict_r) {
*dict = dict_r;

View File

@ -0,0 +1,17 @@
builddir = @BUILDDIR@
testname = @TPKG_NAME@
LIBTOOL = $(builddir)/libtool
CFLAGS=-Wall -Wextra -I$(builddir)/src -g
LDLIBS=$(builddir)/src/libgetdns.la
.SUFFIXES: .c .o .a .lo .h
.c.lo:
$(LIBTOOL) --quiet --tag=CC --mode=compile $(CC) $(CFLAGS) -c $< -o $@
$(testname): $(testname).lo
$(LIBTOOL) --tag=CC --mode=link $(CC) $(LDLIBS) $(LDFLAGS) -o $(testname) $(testname).lo
clean:
rm -f $(testname).lo $(testname).o $(testname)

View File

@ -0,0 +1,130 @@
/*
* capabilities.c - A DNS server for testing server capabilities
*
* Copyright (c) 2016, NLnet Labs. All rights reserved.
*
* This software is open source.
*
* 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.
*
* Neither the name of the NLNET LABS nor the names of its contributors may
* be used to endorse or promote products derived from this software without
* specific prior written permission.
*
* 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 THE COPYRIGHT
* HOLDER OR CONTRIBUTORS 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.
*/
#include <getdns/getdns_extra.h>
#include <stdio.h>
#include <string.h>
void handler(getdns_context *context, getdns_callback_type_t callback_type,
getdns_dict *request, void *userarg, getdns_transaction_t request_id)
{
getdns_bindata *qname;
char ans_str[] = "Some answer";
getdns_bindata ans_bd = { sizeof(ans_str) - 1, (void *)ans_str };
(void) userarg; (void)callback_type;
if (getdns_dict_get_bindata(request, "/question/qname", &qname) ||
getdns_dict_set_bindata(request, "/answer/0/name", qname) ||
getdns_dict_set_int(request, "/answer/0/type", GETDNS_RRTYPE_TXT) ||
getdns_dict_set_int(request, "/header/tc", 1) ||
getdns_dict_set_bindata(request, "/answer/0/rdata/txt_strings/-", &ans_bd))
fprintf(stderr, "Request init error\n");
else if (qname->size >= 8 && qname->data[0] == 6 &&
qname->data[1] == 'c' && qname->data[2] == 'a' &&
qname->data[3] == 'n' && qname->data[4] == 'c' &&
qname->data[5] == 'e' && qname->data[6] == 'l') {
(void) getdns_reply(context, NULL, request_id);
getdns_dict_destroy(request);
return;
} else if (qname->size >= 6 && qname->data[0] == 4 &&
qname->data[1] == 'q' && qname->data[2] == 'u' &&
qname->data[3] == 'i' && qname->data[4] == 't') {
(void) getdns_dict_set_int(request, "/header/tc", 0);
(void) getdns_reply(context, request, request_id);
(void) getdns_context_set_listen_addresses(context, NULL, NULL, NULL);
getdns_dict_destroy(request);
return;
} else {
if (getdns_reply(context, request, request_id))
getdns_reply(context, NULL, request_id);
return;
}
getdns_dict_destroy(request);
exit(EXIT_FAILURE);
}
int main()
{
getdns_context *context = NULL;
getdns_list *listeners = NULL;
getdns_dict *address = NULL;
getdns_dict *address2 = NULL;
uint32_t port1 = 18000;
uint32_t port2 = 18000;
getdns_return_t r;
if ((r = getdns_str2list("[ 127.0.0.1:18000 ]", &listeners)) ||
(r = getdns_str2dict("127.0.0.1:18000", &address2)) ||
(r = getdns_list_get_dict(listeners, 0, &address)) ||
(r = getdns_context_create(&context, 0)))
fprintf(stderr, "Error initializing: ");
else while (++port1 < 18200 &&
!(r = getdns_dict_set_int(address, "port", port1)) &&
(r = getdns_context_set_listen_addresses(
context, listeners, NULL, handler)))
; /* pass */
if (!r &&
((r = getdns_list_set_dict(listeners, 1, address2)) ||
(r = getdns_list_get_dict(listeners, 1, &address))))
fprintf(stderr, "Error initializing 2nd address: ");
if (r) fprintf(stderr, "%s\n", getdns_get_errorstr_by_id(r));
else {
port2 = port1;
while (++port2 < 18200 &&
!(r = getdns_dict_set_int(address, "port", port2)) &&
(r = getdns_context_set_listen_addresses(
context, listeners, NULL, handler)))
; /* pass */
fprintf(stdout, "%d\n", (int)port2);
fprintf(stdout, "%d\n", (int)port1);
fflush(stdout);
getdns_context_run(context);
}
getdns_list_destroy(listeners);
getdns_dict_destroy(address2);
getdns_context_destroy(context);
return r;
}

View File

@ -0,0 +1,4 @@
#!/bin/sh
make clean || true
rm -fr .libs Makefile *_out

View File

@ -0,0 +1,16 @@
BaseName: 275-server-capabilities
Version: 1.0
Description: Test server capabilities (TCP, canceling requests etc.)
CreationDate: wo 19 apr 2017 10:01:58 CEST
Maintainer: Hoda Rohani
Category:
Component:
CmdDepends:
Depends: 210-stub-only-link.tpkg
Help:
Pre: 275-server-capabilities.pre
Post:
Test: 275-server-capabilities.test
AuxFiles:
Passed:
Failure:

View File

@ -0,0 +1,14 @@
# #-- 275-server-capabilities.test --#
# source the master var file when it's there
[ -f ../.tpkg.var.master ] && source ../.tpkg.var.master
# use .tpkg.var.test for in test variable passing
[ -f .tpkg.var.test ] && source .tpkg.var.test
(
grep '^CC=' "${BUILDDIR}/build-stub-only/src/Makefile"
grep '^LDFLAGS=' "${BUILDDIR}/build-stub-only/src/Makefile"
BUILDDIR4SED=`echo "${BUILDDIR}/build-stub-only" | sed 's/\//\\\\\//g'`
sed -e "s/@BUILDDIR@/${BUILDDIR4SED}/g" \
-e "s/@TPKG_NAME@/${TPKG_NAME}/g" "${TPKG_NAME}.Makefile"
) > Makefile

View File

@ -0,0 +1,37 @@
# #-- 275-server-capabilities.test --#
# source the master var file when it's there
[ -f ../.tpkg.var.master ] && source ../.tpkg.var.master
# use .tpkg.var.test for in test variable passing
[ -f .tpkg.var.test ] && source .tpkg.var.test
make && "./${TPKG_NAME}" | (
read PORT
read PORT2
${GETDNS_STUB_QUERY} -s -t 1000 @127.0.0.1:$PORT TXT cancel. +return_call_reporting 2>&1 > time_out
${GETDNS_STUB_QUERY} -s @127.0.0.1:$PORT TXT test +return_call_reporting 2>&1 > tcp_out
${GETDNS_STUB_QUERY} -s -q @127.0.0.1:$PORT TXT quit.
)
if ! grep -q '"status": GETDNS_RESPSTATUS_ALL_TIMEOUT' time_out
then
cat time_out
echo 'error: Query was answered (i think)!'
exit 1
elif ! grep -q '"transport": GETDNS_TRANSPORT_TCP' tcp_out
then
cat tcp_out
echo 'error: Query was not over TCP!'
exit 1
elif ! grep -q '"Some answer"' tcp_out
then
cat tcp_out
echo 'error: Query was not answered!'
exit 1
else
exit 0
fi