From: Lennart Poettering Date: Thu, 12 Apr 2007 23:40:57 +0000 (+0000) Subject: add a regression test for #84 contributed by tedp. (closes #84) X-Git-Url: https://git.meshlink.io/?a=commitdiff_plain;h=76827819636d9a2d66a9e2ad042aaed1fbf952db;p=catta add a regression test for #84 contributed by tedp. (closes #84) git-svn-id: file:///home/lennart/svn/public/avahi/trunk@1420 941a03a8-eaeb-0310-b9a0-b1bbd8fe43fe --- diff --git a/avahi-core/Makefile.am b/avahi-core/Makefile.am index cb2d8f9..6e1d992 100644 --- a/avahi-core/Makefile.am +++ b/avahi-core/Makefile.am @@ -41,10 +41,16 @@ noinst_PROGRAMS = \ conformance-test \ avahi-reflector \ dns-test \ + dns-spin-test \ timeeventq-test \ hashmap-test \ querier-test \ update-test + +TESTS = \ + dns-spin-test \ + dns-test \ + hashmap-test endif libavahi_core_la_SOURCES = \ @@ -138,6 +144,11 @@ dns_test_SOURCES = \ dns_test_CFLAGS = $(AM_CFLAGS) dns_test_LDADD = $(AM_LDADD) ../avahi-common/libavahi-common.la +dns_spin_test_SOURCES = \ + dns-spin-test.c +dns_spin_test_CFLAGS = $(AM_CFLAGS) +dns_spin_test_LDADD = $(AM_LDADD) libavahi-core.la + timeeventq_test_SOURCES = \ timeeventq-test.c \ timeeventq.h timeeventq.c \ diff --git a/avahi-core/dns-spin-test.c b/avahi-core/dns-spin-test.c new file mode 100644 index 0000000..d93e508 --- /dev/null +++ b/avahi-core/dns-spin-test.c @@ -0,0 +1,124 @@ +/* $Id$ */ + +/*** + This file is part of avahi. + + avahi is free software; you can redistribute it and/or modify it + under the terms of the GNU Lesser General Public License as + published by the Free Software Foundation; either version 2.1 of the + License, or (at your option) any later version. + + avahi is distributed in the hope that it will be useful, but WITHOUT + ANY WARRANTY; without even the implied warranty of MERCHANTABILITY + or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General + Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with avahi; if not, write to the Free Software + Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 + USA. +***/ + +/* Regression test for Avahi bug #84. + * This program tests whether the avahi_dns_packet_consume_name function + * returns (rather than spinning forever). For a function as simple as + * avahi_dns_packet_consume_name, we assume that 1 second of CPU time ≈ forever + */ + +#ifdef HAVE_CONFIG_H +# include +#endif + +#include +#include +#include +#include +#include +#include + +#include "dns.h" + +#define MAX_CPU_SECONDS 1 + +#define TEST_NAME "dns-spin-test" + +static void fail(const char *fmt, ...) __attribute__((format(printf, 1, 2), noreturn)); +static void unresolved(const char *fmt, ...) __attribute__((format(printf, 1, 2), noreturn)); +static void stdlib_fail(const char *msg) __attribute__((noreturn)); +static void handle(int sig) __attribute__((noreturn)); + +void stdlib_fail(const char *msg) { + perror(msg); + + printf("UNRESOLVED: " TEST_NAME " (stdlib failure)\n"); + + exit(77); +} + +void unresolved(const char *fmt, ...) { + va_list ap; + + printf("UNRESOLVED: " TEST_NAME ": "); + va_start(ap, fmt); + vprintf(fmt, ap); + va_end(ap); + printf("\n"); + + exit(77); +} + +void fail(const char *fmt, ...) { + va_list ap; + + va_start(ap, fmt); + vprintf(fmt, ap); + va_end(ap); + printf("\n"); + + exit(EXIT_FAILURE); +} + +void handle(AVAHI_GCC_UNUSED int sig) { + fail("Interrupted after %d second of CPU time", MAX_CPU_SECONDS); +} + +#define TRY_EXCEPT(cmd, badresult) \ + do { \ + if ((cmd) == (badresult)) \ + unresolved("%s returned %s", #cmd, #badresult); \ + } while (0) + +int main(AVAHI_GCC_UNUSED int argc, AVAHI_GCC_UNUSED char *argv[]) { + struct itimerval itval; + AvahiDnsPacket *packet; + char name[512]; + int ret; + uint8_t badrr[] = { + 0xC0, AVAHI_DNS_PACKET_HEADER_SIZE, /* self-referential QNAME pointer */ + 0, 1, /* QTYPE A (host addr) */ + 0, 1, /* QCLASS IN (internet/ipv4) */ + }; + + if (signal(SIGVTALRM, handle) == SIG_ERR) + stdlib_fail("signal(SIGVTALRM)"); + + memset(&itval, 0, sizeof(itval)); + itval.it_value.tv_sec = MAX_CPU_SECONDS; + + if (setitimer(ITIMER_VIRTUAL, &itval, NULL) == -1) + stdlib_fail("setitimer()"); + + TRY_EXCEPT(packet = avahi_dns_packet_new_query(512), NULL); + TRY_EXCEPT(avahi_dns_packet_append_bytes(packet, badrr, sizeof(badrr)), NULL); + + /* This is expected to fail (if it returns) */ + ret = avahi_dns_packet_consume_name(packet, name, sizeof(name)); + + if (ret != -1) + fail("avahi_dns_packet_consume_name() returned %d; -1 was expected", ret); + + return EXIT_SUCCESS; +} + +/* vim:ts=4:sw=4:et + */