2 * copyright (c) 2011 Emil Mikulic.
4 * test_addr.c: tests for addr module
6 * You may use, modify and redistribute this file under the terms of the
7 * GNU General Public License version 2. (see COPYING.GPL)
16 void test(const char *in
, const char *expect_out
, int expect_result
)
23 ret
= str_to_addr(in
, &a
);
25 if (ret
!= expect_result
)
29 out
= addr_to_str(&a
);
33 if (expect_out
&& (strcmp(out
, expect_out
) != 0))
36 printf("%s:", success
? "PASS" : "FAIL");
38 printf(" \"%s\" -> \"%s\"", in
, out
);
39 if (expect_out
&& (strcmp(out
, expect_out
) != 0))
40 printf(" (expected \"%s\")", expect_out
);
42 if (ret
!= expect_result
)
43 printf(" [ret %d, expected %d]", ret
, expect_result
);
46 printf(" [err: %s]", gai_strerror(ret
));
51 void test_inside(const char *a
, const char *net
, const char *mask
, int expect
)
53 struct addr aa
, anet
, amask
;
56 str_to_addr(net
, &anet
);
57 str_to_addr(mask
, &amask
);
59 printf("%s: %s in %s/%s\n",
60 addr_inside(&aa
, &anet
, &amask
) ? "PASS" : "FAIL",
66 test("0.0.0.0", "0.0.0.0", 0);
67 test("192.168.1.2", "192.168.1.2", 0);
71 test("::00", "::", 0);
72 test("::000", "::", 0);
73 test("::0000", "::", 0);
75 test("::1", "::1", 0);
76 test("::01", "::1", 0);
77 test("::001", "::1", 0);
78 test("::0001", "::1", 0);
80 test("2404:6800:8004::68", "2404:6800:8004::68", 0);
81 test("2404:6800:8004:0000:0000:0000:0000:0068", "2404:6800:8004::68", 0);
83 test(".", NULL
, EAI_NONAME
);
84 test(":", NULL
, EAI_NONAME
);
85 test("23.75.345.200", NULL
, EAI_NONAME
);
87 test_inside("192.168.1.2", "192.168.0.0", "255.255.0.0", 1);
88 test_inside("2001:0200::3eff:feb1:44d7",
95 /* vim:set ts=3 sw=3 tw=78 et: */