1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* This Source Code Form is subject to the terms of the Mozilla Public
3  * License, v. 2.0. If a copy of the MPL was not distributed with this
4  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
5 
6 #include "prnetdb.h"
7 
8 #include <stdio.h>
9 #include <string.h>
10 #include <stdlib.h>
11 
12 const char *testaddrs[] = {
13     "::", "::",
14     "::1", "::1",
15     "::ffff", "::ffff",
16     "::1:0", "::0.1.0.0",
17     "::127.0.0.1", "::127.0.0.1",
18     "::FFFF:127.0.0.1", "::ffff:127.0.0.1",
19     "::FFFE:9504:3501", "::fffe:9504:3501",
20     "0:0:1:0:35c:0:0:0", "0:0:1:0:35c::",
21     "0:0:3f4c:0:0:4552:0:0", "::3f4c:0:0:4552:0:0",
22     "0:0:1245:0:0:0:0567:0", "0:0:1245::567:0",
23     "0:1:2:3:4:5:6:7", "0:1:2:3:4:5:6:7",
24     "1:2:3:0:4:5:6:7", "1:2:3:0:4:5:6:7",
25     "1:2:3:4:5:6:7:0", "1:2:3:4:5:6:7:0",
26     "1:2:3:4:5:6:7:8", "1:2:3:4:5:6:7:8",
27     "1:2:3:4:5:6::7", "1:2:3:4:5:6:0:7",
28     0
29 };
30 
31 const char *badaddrs[] = {
32     "::.1.2.3",
33     "ffff::.1.2.3",
34     "1:2:3:4:5:6:7::8",
35     "1:2:3:4:5:6::7:8",
36     "::ff99.2.3.4",
37     0
38 };
39 
40 int failed_already = 0;
41 
main(int argc,char ** argv)42 int main(int argc, char **argv)
43 {
44     const char **nexttestaddr = testaddrs;
45     const char **nextbadaddr = badaddrs;
46     const char *in, *expected_out;
47     PRNetAddr addr;
48     char buf[256];
49     PRStatus rv;
50 
51     while ((in = *nexttestaddr++) != 0) {
52         expected_out = *nexttestaddr++;
53         rv = PR_StringToNetAddr(in, &addr);
54         if (rv) {
55             printf("cannot convert %s to addr: %d\n", in, rv);
56             failed_already = 1;
57             continue;
58         }
59         rv = PR_NetAddrToString(&addr, buf, sizeof(buf));
60         if (rv) {
61             printf("cannot convert %s back to string: %d\n", in, rv);
62             failed_already = 1;
63             continue;
64         }
65         if (strcmp(buf, expected_out)) {
66             /* This is not necessarily an error */
67             printf("%s expected %s got %s\n", in, expected_out, buf);
68         }
69     }
70     while ((in = *nextbadaddr++) != 0) {
71         if (PR_StringToNetAddr(in, &addr) == PR_SUCCESS) {
72             printf("converted bad addr %s\n", in);
73             failed_already = 1;
74         }
75     }
76     if (failed_already) {
77         printf("FAIL\n");
78         return 1;
79     }
80     printf("PASS\n");
81     return 0;
82 }
83