1 /* This Source Code Form is subject to the terms of the Mozilla Public
2  * License, v. 2.0. If a copy of the MPL was not distributed with this
3  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
4 module tanya.net.tests.ip;
5 
6 import tanya.net.ip;
7 import tanya.range;
8 
9 // Rejects malformed addresses
10 @nogc nothrow pure @safe unittest
11 {
12     assert(address4("256.0.0.1").isNull);
13     assert(address4(".0.0.1").isNull);
14     assert(address4("0..0.1").isNull);
15     assert(address4("0.0.0.").isNull);
16     assert(address4("0.0.").isNull);
17     assert(address4("").isNull);
18 }
19 
20 @nogc nothrow pure @safe unittest
21 {
22     assert(address4(cast(ubyte[]) []).isNull);
23 }
24 
25 // Assignment and comparison works
26 @nogc nothrow pure @safe unittest
27 {
28     auto address1 = Address4.loopback();
29     auto address2 = Address4.any();
30     address1 = address2;
31     assert(address1 == address2);
32 }
33 
34 @nogc nothrow @safe unittest
35 {
36     char[18] actual;
37 
38     address6("ff00:2:3:4:5:6:7:8").get.toString(arrayInserter(actual));
39     assert(actual[] == "ff00:2:3:4:5:6:7:8");
40 }
41 
42 // Skips zero group in the middle
43 @nogc nothrow @safe unittest
44 {
45     char[12] actual;
46 
47     address6("1::4:5:6:7:8").get.toString(arrayInserter(actual));
48     assert(actual[] == "1::4:5:6:7:8");
49 }
50 
51 // Doesn't replace lonely zeroes
52 @nogc nothrow @safe unittest
53 {
54     char[15] actual;
55 
56     address6("0:1:0:2:3:0:4:0").get.toString(arrayInserter(actual));
57     assert(actual[] == "0:1:0:2:3:0:4:0");
58 }
59 
60 // Skips zero group at the beginning
61 @nogc nothrow @safe unittest
62 {
63     char[13] actual;
64 
65     address6("::3:4:5:6:7:8").get.toString(arrayInserter(actual));
66     assert(actual[] == "::3:4:5:6:7:8");
67 }
68 
69 // Skips zero group at the end
70 @nogc nothrow @safe unittest
71 {
72     char[13] actual;
73 
74     address6("1:2:3:4:5:6::").get.toString(arrayInserter(actual));
75     assert(actual[] == "1:2:3:4:5:6::");
76 }
77 
78 @nogc nothrow @safe unittest
79 {
80     ubyte[16] expected = [0, 1, 0, 2, 0, 3, 0, 4, 0, 5, 0, 6, 0, 7, 0, 8];
81     auto actual = address6("1:2:3:4:5:6:7:8");
82     assert(actual.get.toBytes() == expected);
83 }
84 
85 @nogc nothrow @safe unittest
86 {
87     ubyte[16] expected;
88     auto actual = address6("::");
89     assert(actual.get.toBytes() == expected);
90 }
91 
92 @nogc nothrow @safe unittest
93 {
94     ubyte[16] expected = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1];
95     auto actual = address6("::1");
96     assert(actual.get.toBytes() == expected);
97 }
98 
99 @nogc nothrow @safe unittest
100 {
101     ubyte[16] expected = [0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0];
102     auto actual = address6("1::");
103     assert(actual.get.toBytes() == expected);
104 }
105 
106 // Rejects malformed addresses
107 @nogc nothrow @safe unittest
108 {
109     assert(address6("").isNull);
110     assert(address6(":").isNull);
111     assert(address6(":a").isNull);
112     assert(address6("a:").isNull);
113     assert(address6("1:2:3:4::6:").isNull);
114     assert(address6("fe80:2:3:4::6:7:8%").isNull);
115 }
116 
117 // Parses embedded IPv4 address
118 @nogc nothrow @safe unittest
119 {
120     ubyte[16] expected = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 2, 3, 4];
121     auto actual = address6("0:0:0:0:0:0:1.2.3.4");
122     assert(actual.get.toBytes() == expected);
123 }
124 
125 @nogc nothrow @safe unittest
126 {
127     ubyte[16] expected = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 2, 3, 4];
128     auto actual = address6("::1.2.3.4");
129     assert(actual.get.toBytes() == expected);
130 }
131 
132 @nogc nothrow @safe unittest
133 {
134     ubyte[16] expected = [0, 0, 0, 0, 0, 0, 0, 0, 0, 5, 0, 6, 1, 2, 3, 4];
135     auto actual = address6("::5:6:1.2.3.4");
136     assert(actual.get.toBytes() == expected);
137 }
138 
139 @nogc nothrow @safe unittest
140 {
141     assert(address6("0:0:0:0:0:0:1.2.3.").isNull);
142     assert(address6("0:0:0:0:0:0:1.2:3.4").isNull);
143     assert(address6("0:0:0:0:0:0:1.2.3.4.").isNull);
144     assert(address6("fe80:0:0:0:0:0:1.2.3.4%1").get.scopeID == 1);
145 }
146 
147 // Can assign another address
148 @nogc nothrow pure @safe unittest
149 {
150     Address actual = Address4.loopback;
151     Address expected = Address6.loopback;
152     actual = expected;
153     assert(actual == expected);
154 }