etc/services - sync with NetBSD-8
[minix.git] / external / bsd / bind / dist / bin / tests / sockaddr / t_sockaddr.c
blob127b8f25641b2bb1ef3c1e69fd0dd0efc2a18c78
1 /* $NetBSD: t_sockaddr.c,v 1.5 2014/12/10 04:37:54 christos Exp $ */
3 /*
4 * Copyright (C) 2004, 2007, 2013 Internet Systems Consortium, Inc. ("ISC")
5 * Copyright (C) 1999-2001 Internet Software Consortium.
7 * Permission to use, copy, modify, and/or distribute this software for any
8 * purpose with or without fee is hereby granted, provided that the above
9 * copyright notice and this permission notice appear in all copies.
11 * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
12 * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
13 * AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
14 * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
15 * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
16 * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
17 * PERFORMANCE OF THIS SOFTWARE.
20 /* Id: t_sockaddr.c,v 1.14 2007/06/19 23:47:00 tbox Exp */
22 #include <config.h>
24 #include <isc/netaddr.h>
25 #include <isc/result.h>
26 #include <isc/sockaddr.h>
28 #include <tests/t_api.h>
30 static int
31 test_isc_sockaddr_eqaddrprefix(void) {
32 struct in_addr ina_a;
33 struct in_addr ina_b;
34 struct in_addr ina_c;
35 isc_sockaddr_t isa_a;
36 isc_sockaddr_t isa_b;
37 isc_sockaddr_t isa_c;
39 if (inet_pton(AF_INET, "194.100.32.87", &ina_a) < 0)
40 return T_FAIL;
41 if (inet_pton(AF_INET, "194.100.32.80", &ina_b) < 0)
42 return T_FAIL;
43 if (inet_pton(AF_INET, "194.101.32.87", &ina_c) < 0)
44 return T_FAIL;
45 isc_sockaddr_fromin(&isa_a, &ina_a, 0);
46 isc_sockaddr_fromin(&isa_b, &ina_b, 42);
47 isc_sockaddr_fromin(&isa_c, &ina_c, 0);
49 if (isc_sockaddr_eqaddrprefix(&isa_a, &isa_b, 0) != ISC_TRUE)
50 return T_FAIL;
51 if (isc_sockaddr_eqaddrprefix(&isa_a, &isa_b, 29) != ISC_TRUE)
52 return T_FAIL;
53 if (isc_sockaddr_eqaddrprefix(&isa_a, &isa_b, 30) != ISC_FALSE)
54 return T_FAIL;
55 if (isc_sockaddr_eqaddrprefix(&isa_a, &isa_b, 32) != ISC_FALSE)
56 return T_FAIL;
57 if (isc_sockaddr_eqaddrprefix(&isa_a, &isa_c, 8) != ISC_TRUE)
58 return T_FAIL;
59 if (isc_sockaddr_eqaddrprefix(&isa_a, &isa_c, 16) != ISC_FALSE)
60 return T_FAIL;
62 return T_PASS;
65 static void
66 t1(void) {
67 int result;
68 t_assert("isc_sockaddr_eqaddrprefix", 1, T_REQUIRED,
69 "isc_sockaddr_eqaddrprefix() returns ISC_TRUE when "
70 "prefixes of a and b are equal, and ISC_FALSE when "
71 "they are not equal");
72 result = test_isc_sockaddr_eqaddrprefix();
73 t_result(result);
76 static int
77 test_isc_netaddr_masktoprefixlen(void) {
78 struct in_addr na_a;
79 struct in_addr na_b;
80 struct in_addr na_c;
81 struct in_addr na_d;
82 isc_netaddr_t ina_a;
83 isc_netaddr_t ina_b;
84 isc_netaddr_t ina_c;
85 isc_netaddr_t ina_d;
86 unsigned int plen;
88 if (inet_pton(AF_INET, "0.0.0.0", &na_a) < 0)
89 return T_FAIL;
90 if (inet_pton(AF_INET, "255.255.255.254", &na_b) < 0)
91 return T_FAIL;
92 if (inet_pton(AF_INET, "255.255.255.255", &na_c) < 0)
93 return T_FAIL;
94 if (inet_pton(AF_INET, "255.255.255.0", &na_d) < 0)
95 return T_FAIL;
96 isc_netaddr_fromin(&ina_a, &na_a);
97 isc_netaddr_fromin(&ina_b, &na_b);
98 isc_netaddr_fromin(&ina_c, &na_c);
99 isc_netaddr_fromin(&ina_d, &na_d);
101 if (isc_netaddr_masktoprefixlen(&ina_a, &plen) != ISC_R_SUCCESS)
102 return T_FAIL;
103 if (plen != 0)
104 return T_FAIL;
106 if (isc_netaddr_masktoprefixlen(&ina_b, &plen) != ISC_R_SUCCESS)
107 return T_FAIL;
108 if (plen != 31)
109 return T_FAIL;
111 if (isc_netaddr_masktoprefixlen(&ina_c, &plen) != ISC_R_SUCCESS)
112 return T_FAIL;
113 if (plen != 32)
114 return T_FAIL;
116 if (isc_netaddr_masktoprefixlen(&ina_d, &plen) != ISC_R_SUCCESS)
117 return T_FAIL;
118 if (plen != 24)
119 return T_FAIL;
121 return T_PASS;
124 static void
125 t2(void) {
126 int result;
127 t_assert("isc_netaddr_masktoprefixlen", 1, T_REQUIRED,
128 "isc_netaddr_masktoprefixlen() calculates "
129 "correct prefix lengths ");
130 result = test_isc_netaddr_masktoprefixlen();
131 t_result(result);
134 testspec_t T_testlist[] = {
135 { (PFV) t1, "isc_sockaddr_eqaddrprefix" },
136 { (PFV) t2, "isc_netaddr_masktoprefixlen" },
137 { (PFV) 0, NULL }
140 #ifdef WIN32
142 main(int argc, char **argv) {
143 t_settests(T_testlist);
144 return (t_main(argc, argv));
146 #endif