2 This file is part of catta.
4 catta is free software; you can redistribute it and/or modify it
5 under the terms of the GNU Lesser General Public License as
6 published by the Free Software Foundation; either version 2.1 of the
7 License, or (at your option) any later version.
9 catta is distributed in the hope that it will be useful, but WITHOUT
10 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
11 or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General
12 Public License for more details.
14 You should have received a copy of the GNU Lesser General Public
15 License along with catta; if not, write to the Free Software
16 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
20 /* Regression test for Catta bug #84.
21 * This program tests whether the catta_dns_packet_consume_name function
22 * returns (rather than spinning forever). For a function as simple as
23 * catta_dns_packet_consume_name, we assume that 1 second of CPU time ≈ forever
37 #include "../src/dns.h"
39 #define MAX_CPU_SECONDS 1
41 #define TEST_NAME "dns-spin-test"
43 static void fail(const char *fmt, ...) __attribute__((format(printf, 1, 2), noreturn));
44 static void unresolved(const char *fmt, ...) __attribute__((format(printf, 1, 2), noreturn));
45 static void stdlib_fail(const char *msg) __attribute__((noreturn));
46 static void handle(int sig) __attribute__((noreturn));
48 void stdlib_fail(const char *msg) {
51 printf("UNRESOLVED: " TEST_NAME " (stdlib failure)\n");
56 void unresolved(const char *fmt, ...) {
59 printf("UNRESOLVED: " TEST_NAME ": ");
68 void fail(const char *fmt, ...) {
79 void handle(CATTA_GCC_UNUSED int sig) {
80 fail("Interrupted after %d second of CPU time", MAX_CPU_SECONDS);
83 #define TRY_EXCEPT(cmd, badresult) \
85 if ((cmd) == (badresult)) \
86 unresolved("%s returned %s", #cmd, #badresult); \
89 int main(CATTA_GCC_UNUSED int argc, CATTA_GCC_UNUSED char *argv[]) {
90 struct itimerval itval;
91 CattaDnsPacket *packet;
95 0xC0, CATTA_DNS_PACKET_HEADER_SIZE, /* self-referential QNAME pointer */
96 0, 1, /* QTYPE A (host addr) */
97 0, 1, /* QCLASS IN (internet/ipv4) */
100 if (signal(SIGVTALRM, handle) == SIG_ERR)
101 stdlib_fail("signal(SIGVTALRM)");
103 memset(&itval, 0, sizeof(itval));
104 itval.it_value.tv_sec = MAX_CPU_SECONDS;
106 if (setitimer(ITIMER_VIRTUAL, &itval, NULL) == -1)
107 stdlib_fail("setitimer()");
109 TRY_EXCEPT(packet = catta_dns_packet_new_query(512), NULL);
110 TRY_EXCEPT(catta_dns_packet_append_bytes(packet, badrr, sizeof(badrr)), NULL);
112 /* This is expected to fail (if it returns) */
113 ret = catta_dns_packet_consume_name(packet, name, sizeof(name));
116 fail("catta_dns_packet_consume_name() returned %d; -1 was expected", ret);