]> git.meshlink.io Git - catta/blob - tests/dns-spin-test.c
Fix compilation error caused by ACX_THREAD
[catta] / tests / dns-spin-test.c
1 /***
2   This file is part of catta.
3
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.
8
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.
13
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
17   USA.
18 ***/
19
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
24  */
25
26 #ifdef HAVE_CONFIG_H
27 # include <config.h>
28 #endif
29
30 #include <string.h>
31 #include <stdint.h>
32 #include <stdio.h>
33 #include <stdlib.h>
34 #include <signal.h>
35 #include <sys/time.h>
36
37 #include "../src/dns.h"
38
39 #define MAX_CPU_SECONDS 1
40
41 #define TEST_NAME "dns-spin-test"
42
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));
47
48 void stdlib_fail(const char *msg) {
49     perror(msg);
50
51     printf("UNRESOLVED: " TEST_NAME " (stdlib failure)\n");
52
53     exit(77);
54 }
55
56 void unresolved(const char *fmt, ...) {
57     va_list ap;
58
59     printf("UNRESOLVED: " TEST_NAME ": ");
60     va_start(ap, fmt);
61     vprintf(fmt, ap);
62     va_end(ap);
63     printf("\n");
64
65     exit(77);
66 }
67
68 void fail(const char *fmt, ...) {
69     va_list ap;
70
71     va_start(ap, fmt);
72     vprintf(fmt, ap);
73     va_end(ap);
74     printf("\n");
75
76     exit(EXIT_FAILURE);
77 }
78
79 void handle(CATTA_GCC_UNUSED int sig) {
80     fail("Interrupted after %d second of CPU time", MAX_CPU_SECONDS);
81 }
82
83 #define TRY_EXCEPT(cmd, badresult) \
84     do { \
85         if ((cmd) == (badresult)) \
86             unresolved("%s returned %s", #cmd, #badresult); \
87     } while (0)
88
89 int main(CATTA_GCC_UNUSED int argc, CATTA_GCC_UNUSED char *argv[]) {
90     struct itimerval itval;
91     CattaDnsPacket *packet;
92     char name[512];
93     int ret;
94     uint8_t badrr[] = {
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) */
98     };
99
100     if (signal(SIGVTALRM, handle) == SIG_ERR)
101         stdlib_fail("signal(SIGVTALRM)");
102
103     memset(&itval, 0, sizeof(itval));
104     itval.it_value.tv_sec = MAX_CPU_SECONDS;
105
106     if (setitimer(ITIMER_VIRTUAL, &itval, NULL) == -1)
107         stdlib_fail("setitimer()");
108
109     TRY_EXCEPT(packet = catta_dns_packet_new_query(512), NULL);
110     TRY_EXCEPT(catta_dns_packet_append_bytes(packet, badrr, sizeof(badrr)), NULL);
111
112     /* This is expected to fail (if it returns) */
113     ret = catta_dns_packet_consume_name(packet, name, sizeof(name));
114
115     if (ret != -1)
116         fail("catta_dns_packet_consume_name() returned %d; -1 was expected", ret);
117
118     return EXIT_SUCCESS;
119 }
120
121 /* vim:ts=4:sw=4:et
122  */