1 | /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
---|
2 | /* ***** BEGIN LICENSE BLOCK *****
|
---|
3 | * Version: MPL 1.1/GPL 2.0/LGPL 2.1
|
---|
4 | *
|
---|
5 | * The contents of this file are subject to the Mozilla Public License Version
|
---|
6 | * 1.1 (the "License"); you may not use this file except in compliance with
|
---|
7 | * the License. You may obtain a copy of the License at
|
---|
8 | * http://www.mozilla.org/MPL/
|
---|
9 | *
|
---|
10 | * Software distributed under the License is distributed on an "AS IS" basis,
|
---|
11 | * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
|
---|
12 | * for the specific language governing rights and limitations under the
|
---|
13 | * License.
|
---|
14 | *
|
---|
15 | * The Original Code is the Netscape Portable Runtime (NSPR).
|
---|
16 | *
|
---|
17 | * The Initial Developer of the Original Code is
|
---|
18 | * Netscape Communications Corporation.
|
---|
19 | * Portions created by the Initial Developer are Copyright (C) 2000
|
---|
20 | * the Initial Developer. All Rights Reserved.
|
---|
21 | *
|
---|
22 | * Contributor(s):
|
---|
23 | *
|
---|
24 | * Alternatively, the contents of this file may be used under the terms of
|
---|
25 | * either the GNU General Public License Version 2 or later (the "GPL"), or
|
---|
26 | * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
|
---|
27 | * in which case the provisions of the GPL or the LGPL are applicable instead
|
---|
28 | * of those above. If you wish to allow use of your version of this file only
|
---|
29 | * under the terms of either the GPL or the LGPL, and not to allow others to
|
---|
30 | * use your version of this file under the terms of the MPL, indicate your
|
---|
31 | * decision by deleting the provisions above and replace them with the notice
|
---|
32 | * and other provisions required by the GPL or the LGPL. If you do not delete
|
---|
33 | * the provisions above, a recipient may use your version of this file under
|
---|
34 | * the terms of any one of the MPL, the GPL or the LGPL.
|
---|
35 | *
|
---|
36 | * ***** END LICENSE BLOCK ***** */
|
---|
37 |
|
---|
38 | #include "prnetdb.h"
|
---|
39 |
|
---|
40 | #include <stdio.h>
|
---|
41 | #include <string.h>
|
---|
42 | #include <stdlib.h>
|
---|
43 |
|
---|
44 | const char *testaddrs[] = {
|
---|
45 | "::", "::",
|
---|
46 | "::1", "::1",
|
---|
47 | "::ffff", "::ffff",
|
---|
48 | "::1:0", "::0.1.0.0",
|
---|
49 | "::127.0.0.1", "::127.0.0.1",
|
---|
50 | "::FFFF:127.0.0.1", "::ffff:127.0.0.1",
|
---|
51 | "::FFFE:9504:3501", "::fffe:9504:3501",
|
---|
52 | "0:0:1:0:35c:0:0:0", "0:0:1:0:35c::",
|
---|
53 | "0:0:3f4c:0:0:4552:0:0", "::3f4c:0:0:4552:0:0",
|
---|
54 | "0:0:1245:0:0:0:0567:0", "0:0:1245::567:0",
|
---|
55 | "0:1:2:3:4:5:6:7", "0:1:2:3:4:5:6:7",
|
---|
56 | "1:2:3:0:4:5:6:7", "1:2:3:0:4:5:6:7",
|
---|
57 | "1:2:3:4:5:6:7:0", "1:2:3:4:5:6:7:0",
|
---|
58 | "1:2:3:4:5:6:7:8", "1:2:3:4:5:6:7:8",
|
---|
59 | "1:2:3:4:5:6::7", "1:2:3:4:5:6:0:7",
|
---|
60 | 0
|
---|
61 | };
|
---|
62 |
|
---|
63 | const char *badaddrs[] = {
|
---|
64 | "::.1.2.3",
|
---|
65 | "ffff::.1.2.3",
|
---|
66 | "1:2:3:4:5:6:7::8",
|
---|
67 | "1:2:3:4:5:6::7:8",
|
---|
68 | "::ff99.2.3.4",
|
---|
69 | 0
|
---|
70 | };
|
---|
71 |
|
---|
72 | int failed_already = 0;
|
---|
73 |
|
---|
74 | int main()
|
---|
75 | {
|
---|
76 | const char **nexttestaddr = testaddrs;
|
---|
77 | const char **nextbadaddr = badaddrs;
|
---|
78 | const char *in, *expected_out;
|
---|
79 | PRNetAddr addr;
|
---|
80 | char buf[256];
|
---|
81 | PRStatus rv;
|
---|
82 |
|
---|
83 | while ((in = *nexttestaddr++) != 0) {
|
---|
84 | expected_out = *nexttestaddr++;
|
---|
85 | rv = PR_StringToNetAddr(in, &addr);
|
---|
86 | if (rv) {
|
---|
87 | printf("cannot convert %s to addr: %d\n", in, rv);
|
---|
88 | failed_already = 1;
|
---|
89 | continue;
|
---|
90 | }
|
---|
91 | rv = PR_NetAddrToString(&addr, buf, sizeof(buf));
|
---|
92 | if (rv) {
|
---|
93 | printf("cannot convert %s back to string: %d\n", in, rv);
|
---|
94 | failed_already = 1;
|
---|
95 | continue;
|
---|
96 | }
|
---|
97 | if (strcmp(buf, expected_out)) {
|
---|
98 | /* This is not necessarily an error */
|
---|
99 | printf("%s expected %s got %s\n", in, expected_out, buf);
|
---|
100 | }
|
---|
101 | }
|
---|
102 | while ((in = *nextbadaddr++) != 0) {
|
---|
103 | if (PR_StringToNetAddr(in, &addr) == PR_SUCCESS) {
|
---|
104 | printf("converted bad addr %s\n", in);
|
---|
105 | failed_already = 1;
|
---|
106 | }
|
---|
107 | }
|
---|
108 | if (failed_already) {
|
---|
109 | printf("FAIL\n");
|
---|
110 | return 1;
|
---|
111 | }
|
---|
112 | printf("PASS\n");
|
---|
113 | return 0;
|
---|
114 | }
|
---|