chiark / gitweb /
Quieten compiler
[disorder] / libtests / t-addr.c
CommitLineData
b90f122b
RK
1/*
2 * This file is part of DisOrder.
3 * Copyright (C) 2005, 2007, 2008 Richard Kettlewell
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
18 * USA
19 */
20#include "test.h"
21
c68d8eba 22static void test_addr(void) {
b90f122b 23 struct stringlist a;
136ef3a3 24 const char *s[3];
b90f122b
RK
25 struct addrinfo *ai;
26 char *name;
27 const struct sockaddr_in *sin4;
28 struct sockaddr_in s4;
29 struct sockaddr_un su;
30
31 static const struct addrinfo pref = {
66613034
RK
32 .ai_flags = AI_PASSIVE,
33 .ai_family = PF_INET,
34 .ai_socktype = SOCK_STREAM,
b90f122b
RK
35 };
36
136ef3a3
RK
37 struct sockaddr_in a1 = {
38 .sin_port = ntohs(25),
39 .sin_addr = { .s_addr = 0}
40 };
41 struct addrinfo p1 = {
42 .ai_family = PF_INET,
43 .ai_socktype = SOCK_STREAM,
44 .ai_protocol = IPPROTO_TCP,
45 .ai_addrlen = sizeof a1,
46 .ai_addr = (struct sockaddr *)&a1,
47 };
48
49 struct sockaddr_in a2 = {
50 .sin_port = ntohs(119),
51 .sin_addr = { .s_addr = htonl(0x7F000001) }
52 };
53 struct addrinfo p2 = {
54 .ai_family = PF_INET,
55 .ai_socktype = SOCK_STREAM,
56 .ai_protocol = IPPROTO_TCP,
57 .ai_addrlen = sizeof a2,
58 .ai_addr = (struct sockaddr *)&a2,
59 };
60
136ef3a3
RK
61 insist(addrinfocmp(&p1, &p2) < 0);
62
b90f122b
RK
63 a.n = 1;
64 a.s = (char **)s;
65 s[0] = "smtp";
66 ai = get_address(&a, &pref, &name);
67 insist(ai != 0);
68 check_integer(ai->ai_family, PF_INET);
69 check_integer(ai->ai_socktype, SOCK_STREAM);
70 check_integer(ai->ai_protocol, IPPROTO_TCP);
71 check_integer(ai->ai_addrlen, sizeof(struct sockaddr_in));
72 sin4 = (const struct sockaddr_in *)ai->ai_addr;
73 check_integer(sin4->sin_family, AF_INET);
74 check_integer(sin4->sin_addr.s_addr, 0);
75 check_integer(ntohs(sin4->sin_port), 25);
76 check_string(name, "host * service smtp");
136ef3a3
RK
77 insist(addrinfocmp(ai, &p1) == 0);
78 insist(addrinfocmp(ai, &p2) < 0);
b90f122b
RK
79
80 a.n = 2;
81 s[0] = "localhost";
82 s[1] = "nntp";
83 ai = get_address(&a, &pref, &name);
84 insist(ai != 0);
85 check_integer(ai->ai_family, PF_INET);
86 check_integer(ai->ai_socktype, SOCK_STREAM);
87 check_integer(ai->ai_protocol, IPPROTO_TCP);
88 check_integer(ai->ai_addrlen, sizeof(struct sockaddr_in));
89 sin4 = (const struct sockaddr_in *)ai->ai_addr;
90 check_integer(sin4->sin_family, AF_INET);
91 check_integer(ntohl(sin4->sin_addr.s_addr), 0x7F000001);
92 check_integer(ntohs(sin4->sin_port), 119);
93 check_string(name, "host localhost service nntp");
136ef3a3
RK
94 insist(addrinfocmp(ai, &p2) == 0);
95 insist(addrinfocmp(ai, &p1) > 0);
96
97 a.n = 2;
98 s[0] = "no.such.domain.really.i.mean.it.greenend.org.uk";
99 s[1] = "nntp";
100 insist(get_address(&a, &pref, &name) == 0);
101
102 a.n = 3;
103 s[0] = 0;
104 s[1] = 0;
105 s[2] = 0;
106 insist(get_address(&a, &pref, &name) == 0);
b90f122b
RK
107
108 memset(&s4, 0, sizeof s4);
109 s4.sin_family = AF_INET;
110 s4.sin_addr.s_addr = 0;
111 s4.sin_port = 0;
112 check_string(format_sockaddr((struct sockaddr *)&s4),
113 "0.0.0.0");
114 check_integer(multicast((struct sockaddr *)&s4), 0);
115 s4.sin_addr.s_addr = htonl(0x7F000001);
116 s4.sin_port = htons(1000);
117 check_string(format_sockaddr((struct sockaddr *)&s4),
118 "127.0.0.1 port 1000");
119 check_integer(multicast((struct sockaddr *)&s4), 0);
120 s4.sin_addr.s_addr = htonl(0xE0000001);
121 check_string(format_sockaddr((struct sockaddr *)&s4),
122 "224.0.0.1 port 1000");
123 check_integer(multicast((struct sockaddr *)&s4), 1);
124
125 memset(&su, 0, sizeof su);
126 su.sun_family = AF_UNIX;
127 strcpy(su.sun_path, "/wibble/wobble");
128 check_string(format_sockaddr((struct sockaddr *)&su),
129 "/wibble/wobble");
130 check_integer(multicast((struct sockaddr *)&su), 0);
131}
132
c68d8eba
RK
133TEST(addr);
134
b90f122b
RK
135/*
136Local Variables:
137c-basic-offset:2
138comment-column:40
139fill-column:79
140indent-tabs-mode:nil
141End:
142*/