tobogganSe connecter

packages

The Louise packages, each released on its own

Clone avec HTTPS, un jeton personnel en mot de passe, ou avec SSH, une clé ajoutée dans vos paramètres.

280 lignes · 6124 octetsBrut · Historique
1// The ranges are those of the IANA special-purpose registries (RFC 6890)
2// that ADR 0079 refuses, checked at both edges; the reference below is
3// written with 32-bit numbers, not the octet patterns `address` uses.
4
5import address.{type Reach, Private, Public}
6import gen
7import gen/check
8import std/list
9import std/random.{type random}
10import std/result
11import std/string
12import std/test.{assert, assert_equal}
13
14fn public :: ip: String -> (String, Bool)
15{
16 (ip, address.classify ip == Ok(Public))
17}
18
19fn is_private :: reach: Result<Reach, String> -> Bool
20{
21 match reach { Ok(Private(_)) -> True, _ -> False }
22}
23
24@test
25fn ipv4_edges ::
26{
27 let table =
28 [ ("0.0.0.0", False)
29 , ("0.255.255.255", False)
30 , ("1.0.0.0", True)
31 , ("8.8.8.8", True)
32 , ("9.255.255.255", True)
33 , ("10.0.0.0", False)
34 , ("10.255.255.255", False)
35 , ("11.0.0.0", True)
36 , ("100.63.255.255", True)
37 , ("100.64.0.0", False)
38 , ("100.127.255.255", False)
39 , ("100.128.0.0", True)
40 , ("126.255.255.255", True)
41 , ("127.0.0.1", False)
42 , ("127.255.255.255", False)
43 , ("128.0.0.0", True)
44 , ("169.253.255.255", True)
45 , ("169.254.0.0", False)
46 , ("169.254.169.254", False)
47 , ("169.255.0.0", True)
48 , ("172.15.255.255", True)
49 , ("172.16.0.0", False)
50 , ("172.31.255.255", False)
51 , ("172.32.0.0", True)
52 , ("192.167.255.255", True)
53 , ("192.168.0.0", False)
54 , ("192.168.255.255", False)
55 , ("192.169.0.0", True)
56 , ("223.255.255.255", True)
57 , ("224.0.0.0", False)
58 , ("239.255.255.250", False)
59 , ("240.0.0.1", False)
60 , ("255.255.255.255", False)
61 ]
62 list.each table fn row { assert_equal (public row.0) row }
63}
64
65@test
66fn ipv6_ranges ::
67{
68 let table =
69 [ ("::", False)
70 , ("::1", False)
71 , ("0:0:0:0:0:0:0:1", False)
72 , ("::2", False)
73 , ("::8.8.8.8", False)
74 , ("fe80::1", False)
75 , ("FEBF:ffff::1", False)
76 , ("fec0::1", False)
77 , ("fc00::1", False)
78 , ("fdff:ffff::1", False)
79 , ("ff02::1", False)
80 , ("ff00::", False)
81 , ("2001:4860:4860::8888", True)
82 , ("2606:4700:4700::1111", True)
83 , ("fbff::1", True)
84 , ("1:2:3:4:5:6:7:8", True)
85 ]
86 list.each table fn row { assert_equal (public row.0) row }
87}
88
89@test
90fn an_embedded_ipv4_address_is_judged_as_ipv4 ::
91{
92 assert_equal (public "::ffff:8.8.8.8") ("::ffff:8.8.8.8", True)
93 assert_equal (public "::ffff:808:808") ("::ffff:808:808", True)
94 assert_equal
95 (address.classify "::ffff:127.0.0.1")
96 Ok(Private("IPv4-mapped: loopback"))
97 assert_equal
98 (address.classify "::FFFF:7f00:1")
99 Ok(Private("IPv4-mapped: loopback"))
100 assert_equal
101 (address.classify "0:0:0:0:0:ffff:a00:1")
102 Ok(Private("IPv4-mapped: private"))
103 assert_equal (public "64:ff9b::8.8.8.8") ("64:ff9b::8.8.8.8", True)
104 assert_equal
105 (address.classify "64:ff9b::a9fe:a9fe")
106 Ok(Private("NAT64: link-local"))
107 assert_equal
108 (address.classify "64:ff9b::127.0.0.1")
109 Ok(Private("NAT64: loopback"))
110 assert
111 (is_private (address.classify "::ffff:172.16.0.1"))
112 "is_private (address.classify ::ffff:172.16.0.1)"
113 assert
114 (address.is_public "::ffff:172.15.255.255")
115 "address.is_public ::ffff:172.15.255.255"
116}
117
118@test
119fn garbage_is_not_an_address ::
120{
121 let garbage =
122 [ ""
123 , "example.com"
124 , "1.2.3"
125 , "1.2.3.4.5"
126 , "256.1.1.1"
127 , "1.2.3.256"
128 , "01.2.3.4"
129 , "1.2.3.04"
130 , "+1.2.3.4"
131 , "1.2.3.-4"
132 , "1..3.4"
133 , " 1.2.3.4"
134 , "1.2.3.4 "
135 , "1.2.3.4/8"
136 , "0x7f.0.0.1"
137 , "2130706433"
138 , ":"
139 , ":::"
140 , "::1::"
141 , "1::2::3"
142 , "1:2:3:4:5:6:7:8:9"
143 , "1:2:3:4:5:6:7"
144 , "1:2:3:4:5:6:7::8"
145 , "12345::"
146 , "g::1"
147 , "fe80::1%eth0"
148 , "[::1]"
149 , "::ffff:1.2.3"
150 , "1.2.3.4::"
151 , "1:"
152 ]
153 list.each
154 garbage
155 fn ip
156 {
157 assert_equal (ip, result.is_error (address.classify ip)) (ip, True)
158 }
159}
160
161// ---- properties ----------------------------------------------------------------
162
163/// The refused IPv4 ranges as 32-bit numbers: (first, last).
164fn refused_ranges :: -> List<(Int, Int)>
165{
166 let range = fn a b bits
167 {
168 let first = a * 16777216 + b * 65536
169 (first, first + pow2 (32 - bits) - 1)
170 }
171 [ range 0 0 8
172 , range 10 0 8
173 , range 100 64 10
174 , range 127 0 8
175 , range 169 254 16
176 , range 172 16 12
177 , range 192 168 16
178 , range 224 0 4
179 , range 240 0 4
180 ]
181}
182
183fn pow2 :: n: Int -> Int
184{
185 if n == 0
186 {
187 1
188 }
189 else
190 {
191 2 * pow2 (n - 1)
192 }
193}
194
195fn edge_byte :: -> Int / <random>
196{
197 match gen.int ~low: 0 ~high: 1
198 { 0 ->
199 gen.pick
200 [ 0
201 , 1
202 , 9
203 , 10
204 , 11
205 , 63
206 , 64
207 , 99
208 , 100
209 , 126
210 , 127
211 , 128
212 , 168
213 , 169
214 , 172
215 , 192
216 , 223
217 , 224
218 , 239
219 , 240
220 , 254
221 , 255
222 ]
223 , _ -> gen.int ~low: 0 ~high: 255
224 }
225}
226
227fn any_ipv4 :: -> List<Int> / <random>
228{
229 [edge_byte (), edge_byte (), edge_byte (), edge_byte ()]
230}
231
232fn dotted :: b: List<Int> -> String
233{
234 string.join (list.map b show) "."
235}
236
237fn expected_public :: b: List<Int> -> Bool
238{
239 let n = list.fold b 0 fn acc x { acc * 256 + x }
240 !list.any (refused_ranges ()) fn r { n >= r.0 && n <= r.1 }
241}
242
243@test
244fn ipv4_matches_the_reference_ranges :: -> () / <random>
245{
246 check.property
247 ~runs: 2000
248 any_ipv4
249 fn b { address.is_public (dotted b) == expected_public b }
250}
251
252@test
253fn a_mapped_address_is_its_ipv4_address :: -> () / <random>
254{
255 check.property
256 ~runs: 1000
257 any_ipv4
258 fn b
259 {
260 let v4 = address.is_public (dotted b)
261 address.is_public ("::ffff:" <> dotted b) == v4
262 && address.is_public ("64:ff9b::" <> dotted b) == v4
263 && address.is_public ("::" <> dotted b) == False
264 }
265}
266
267@test
268fn random_text_never_crashes :: -> () / <random>
269{
270 check.property
271 ~runs: 1000
272 fn { gen.string ~alphabet: "0123456789abcdefABCDEFg.:%" ~min: 0 ~max: 20 }
273 fn text
274 {
275 match address.classify text
276 { Ok(_) -> string.contains text "." || string.contains text ":"
277 , Error(_) -> True
278 }
279 }
280}