]> gitweb.michael.orlitzky.com - hath.git/blob - src/Cidr.hs
Add an antisymmetry property check for the Cidr Ord instance.
[hath.git] / src / Cidr.hs
1 -- | The CIDR modules contains most of the functions used for working
2 -- with the CIDR type.
3 module Cidr
4 ( Cidr(..),
5 cidr_properties,
6 cidr_tests,
7 combine_all,
8 contains,
9 contains_proper,
10 enumerate,
11 max_octet1,
12 max_octet2,
13 max_octet3,
14 max_octet4,
15 min_octet1,
16 min_octet2,
17 min_octet3,
18 min_octet4,
19 normalize
20 ) where
21
22 import Data.List (nub)
23 import Data.List.Split (splitOneOf)
24 import Data.Maybe (catMaybes, mapMaybe)
25
26 import Test.Tasty ( TestTree, localOption, testGroup )
27 import Test.Tasty.HUnit ( (@?=), testCase )
28 import Test.Tasty.QuickCheck (
29 Arbitrary( arbitrary ),
30 Gen,
31 Property,
32 QuickCheckTests( QuickCheckTests ),
33 (==>),
34 testProperty )
35 import Text.Read (readMaybe)
36
37 import qualified Bit as B (Bit(..))
38 import IPv4Address (
39 IPv4Address( IPv4Address, octet1, octet2, octet3, octet4 ),
40 most_sig_bit_different )
41 import Maskable (Maskable(apply_mask))
42 import Maskbits ( Maskbits(Zero) )
43 import Octet (Octet())
44
45
46 data Cidr = Cidr { ipv4address :: IPv4Address,
47 maskbits :: Maskbits }
48
49
50 instance Show Cidr where
51 show cidr = (show (ipv4address cidr)) ++ "/" ++ (show (maskbits cidr))
52
53
54 instance Arbitrary Cidr where
55 arbitrary = do
56 ipv4 <- arbitrary :: Gen IPv4Address
57 mask <- arbitrary :: Gen Maskbits
58 return (Cidr ipv4 mask)
59
60
61 instance Eq Cidr where
62 -- | Two CIDRs are equal if they have the same network bits and if
63 -- their masks are the same. In other words, if they are the same
64 -- after normalization.
65 cidr1 == cidr2 = (cidr1 <= cidr2) && (cidr2 <= cidr1)
66
67 instance Ord Cidr where
68 cidr1 <= cidr2 = if addr1 == addr2 then mask1 <= mask2 else addr1 <= addr2
69 where
70 Cidr addr1 mask1 = normalize cidr1
71 Cidr addr2 mask2 = normalize cidr2
72
73 -- | Returns the mask portion of a CIDR address. That is, everything
74 -- after the trailing slash.
75 maskbits_from_cidr_string :: String -> Maybe Maskbits
76 maskbits_from_cidr_string s
77 | length partlist == 2 = readMaybe (partlist !! 1)
78 | otherwise = Nothing
79 where
80 partlist = splitOneOf "/" s
81
82
83 -- | Takes an IP address String in CIDR notation, and returns a list
84 -- of its octets (as Ints).
85 octets_from_cidr_string :: String -> [Octet]
86 octets_from_cidr_string s =
87 case parts of
88 (p1:p2:p3:p4:_) -> mapMaybe readMaybe [p1,p2,p3,p4]
89 _ -> []
90 where
91 parts = splitOneOf "./" s
92
93 instance Read Cidr where
94 -- | Parse everything or nothing.
95 readsPrec _ s =
96 case (octets_from_cidr_string s) of
97 [oct1, oct2, oct3, oct4] ->
98 case (maskbits_from_cidr_string s) of
99 Just mbits ->
100 [(Cidr (IPv4Address oct1 oct2 oct3 oct4) mbits, "")]
101 _ -> []
102 _ -> []
103
104
105 -- | Given a CIDR, return the minimum valid IPv4 address contained
106 -- within it.
107 min_host :: Cidr -> IPv4Address
108 min_host (Cidr addr mask) = apply_mask addr mask B.Zero
109
110 -- | Given a CIDR, return the maximum valid IPv4 address contained
111 -- within it.
112 max_host :: Cidr -> IPv4Address
113 max_host (Cidr addr mask) = apply_mask addr mask B.One
114
115 -- | Given a CIDR, return the first octet of the minimum valid IPv4
116 -- address contained within it.
117 min_octet1 :: Cidr -> Octet
118 min_octet1 cidr = octet1 (min_host cidr)
119
120 -- | Given a CIDR, return the second octet of the minimum valid IPv4
121 -- address contained within it.
122 min_octet2 :: Cidr -> Octet
123 min_octet2 cidr = octet2 (min_host cidr)
124
125 -- | Given a CIDR, return the third octet of the minimum valid IPv4
126 -- address contained within it.
127 min_octet3 :: Cidr -> Octet
128 min_octet3 cidr = octet3 (min_host cidr)
129
130 -- | Given a CIDR, return the fourth octet of the minimum valid IPv4
131 -- address contained within it.
132 min_octet4 :: Cidr -> Octet
133 min_octet4 cidr = octet4 (min_host cidr)
134
135 -- | Given a CIDR, return the first octet of the maximum valid IPv4
136 -- address contained within it.
137 max_octet1 :: Cidr -> Octet
138 max_octet1 cidr = octet1 (max_host cidr)
139
140 -- | Given a CIDR, return the second octet of the maximum valid IPv4
141 -- address contained within it.
142 max_octet2 :: Cidr -> Octet
143 max_octet2 cidr = octet2 (max_host cidr)
144
145 -- | Given a CIDR, return the third octet of the maximum valid IPv4
146 -- address contained within it.
147 max_octet3 :: Cidr -> Octet
148 max_octet3 cidr = octet3 (max_host cidr)
149
150 -- | Given a CIDR, return the fourth octet of the maximum valid IPv4
151 -- address contained within it.
152 max_octet4 :: Cidr -> Octet
153 max_octet4 cidr = octet4 (max_host cidr)
154
155
156
157 -- | Return true if the first argument (a CIDR range) contains the
158 -- second (another CIDR range). There are a lot of ways we can be
159 -- fed junk here. For lack of a better alternative, just return
160 -- False when we are given nonsense.
161 --
162 -- If the number of bits in the network part of the first address is
163 -- larger than the number of bits in the second, there is no way
164 -- that the first range can contain the second. For, if the number
165 -- of network bits is larger, then the number of host bits must be
166 -- smaller, and if cidr1 has fewer hosts than cidr2, cidr1 most
167 -- certainly does not contain cidr2.
168 --
169 -- On the other hand, if the first argument (cidr1) has fewer (or
170 -- the same number of) network bits as the second, it can contain
171 -- the second. In this case, we need to check that every host in
172 -- cidr2 is contained in cidr1. If a host in cidr2 is contained in
173 -- cidr1, then at least mbits1 of an address in cidr2 will match
174 -- cidr1. For example,
175 --
176 -- cidr1 = 192.168.1.0\/23, cidr2 = 192.168.1.100\/24
177 --
178 -- Here, cidr2 contains all of 192.168.1.0 through
179 -- 192.168.1.255. However, cidr1 contains BOTH 192.168.0.0 through
180 -- 192.168.0.255 and 192.168.1.0 through 192.168.1.255. In essence,
181 -- what we want to check is that cidr2 "begins with" something that
182 -- cidr1 CAN begin with. Since cidr1 can begin with 192.168.1, and
183 -- cidr2 DOES, cidr1 contains cidr2..
184 --
185 -- The way that we check this is to apply cidr1's mask to cidr2's
186 -- address and see if the result is the same as cidr1's mask applied
187 -- to cidr1's address.
188 --
189 contains :: Cidr -> Cidr -> Bool
190 contains (Cidr addr1 mbits1) (Cidr addr2 mbits2)
191 | mbits1 > mbits2 = False
192 | otherwise = addr1masked == addr2masked
193 where
194 addr1masked = apply_mask addr1 mbits1 B.Zero
195 addr2masked = apply_mask addr2 mbits1 B.Zero
196
197
198 -- | Contains but is not equal to.
199 contains_proper :: Cidr -> Cidr -> Bool
200 contains_proper cidr1 cidr2 =
201 (cidr1 `contains` cidr2) && (not (cidr2 `contains` cidr1))
202
203
204 -- | A CIDR range is redundant (with respect to the given list) if
205 -- another CIDR range in that list properly contains it.
206 redundant :: [Cidr] -> Cidr -> Bool
207 redundant cidrlist cidr = any ((flip contains_proper) cidr) cidrlist
208
209
210 -- | First, we look at all possible pairs of cidrs, and combine the
211 -- adjacent ones in to a new list. Then, we concatenate that list
212 -- with the original one, and filter out all of the redundancies. If
213 -- two adjacent Cidrs are combined into a larger one, they will be
214 -- removed in the second step since the larger Cidr must contain the
215 -- smaller two.
216 --
217 -- Once this is done, we see whether or not the result is different
218 -- than the argument that was passed in. If nothing changed, we're
219 -- done and return the list that was passed to us. However, if
220 -- something changed, we recurse and try to combine the list again.
221 combine_all :: [Cidr] -> [Cidr]
222 combine_all cidrs
223 | cidrs == (combine_contained unique_cidrs) = cidrs
224 | otherwise = combine_all (combine_contained unique_cidrs)
225 where
226 unique_cidrs = nub cidr_combinations
227 cidr_combinations =
228 cidrs ++ (catMaybes [ (combine_adjacent x y) | x <- cidrs, y <- cidrs ])
229
230
231 -- | Take a list of CIDR ranges and filter out all of the ones that
232 -- are contained entirelt within some other range in the list.
233 combine_contained :: [Cidr] -> [Cidr]
234 combine_contained cidrs =
235 filter (not . (redundant cidrs)) cidrs
236
237
238 -- | If the two Cidrs are not adjacent, return Cidr.None. Otherwise,
239 -- decrement the maskbits of cidr1 and return that; it will contain
240 -- both cidr1 and cidr2.
241 combine_adjacent :: Cidr -> Cidr -> Maybe Cidr
242 combine_adjacent cidr1 cidr2
243 | not (adjacent cidr1 cidr2) = Nothing
244 | (maskbits cidr1 == Zero) = Nothing
245 | otherwise = Just $ cidr1 { maskbits = pred (maskbits cidr1) }
246
247
248
249 -- | Determine whether or not two CIDR ranges are adjacent. If two
250 -- ranges lie consecutively within the IP space, they can be
251 -- combined. For example, 10.1.0.0/24 and 10.0.1.0/24 are adjacent,
252 -- and can be combined in to 10.1.0.0/23.
253 adjacent :: Cidr -> Cidr -> Bool
254 adjacent cidr1 cidr2
255 | mbits1 /= mbits2 = False
256 | mbits1 == Maskbits.Zero = False -- They're equal.
257 | otherwise = (mbits1 == (most_sig_bit_different addr1 addr2))
258 where
259 addr1 = ipv4address cidr1
260 addr2 = ipv4address cidr2
261 mbits1 = maskbits cidr1
262 mbits2 = maskbits cidr2
263
264
265 enumerate :: Cidr -> [IPv4Address]
266 enumerate cidr = [(min_host cidr)..(max_host cidr)]
267
268
269 -- | Replace any masked bits in this CIDR's IPv4Address with zeros.
270 normalize :: Cidr -> Cidr
271 normalize (Cidr addr mask) =
272 Cidr nrml_addr mask
273 where
274 nrml_addr = apply_mask addr mask B.Zero
275
276 -- Test lists.
277 cidr_tests :: TestTree
278 cidr_tests =
279 testGroup "CIDR Tests" [
280 test_enumerate,
281 test_min_host1,
282 test_max_host1,
283 test_equality1,
284 test_contains1,
285 test_contains2,
286 test_contains_proper1,
287 test_contains_proper2,
288 test_adjacent1,
289 test_adjacent2,
290 test_adjacent3,
291 test_adjacent4,
292 test_combine_contained1,
293 test_combine_contained2,
294 test_combine_all1,
295 test_combine_all2,
296 test_combine_all3,
297 test_normalize1,
298 test_normalize2,
299 test_normalize3 ]
300
301 cidr_properties :: TestTree
302 cidr_properties =
303 testGroup "CIDR Properties" [
304 prop_all_cidrs_contain_themselves,
305 prop_contains_proper_antisymmetric,
306 prop_normalize_idempotent,
307 prop_normalize_preserves_equality,
308 prop_ord_instance_antisymmetric,
309 prop_ord_instance_reflexive,
310 prop_ord_instance_transitive ]
311
312
313 -- HUnit Tests
314 test_enumerate :: TestTree
315 test_enumerate =
316 testCase desc $ actual @?= expected
317 where
318 desc = "192.168.0.240/30 is enumerated correctly"
319 oct1 = toEnum 192 :: Octet
320 oct2 = toEnum 168 :: Octet
321 oct3 = minBound :: Octet
322 mk_ip = IPv4Address oct1 oct2 oct3
323 addr1 = mk_ip $ toEnum 240
324 addr2 = mk_ip $ toEnum 241
325 addr3 = mk_ip $ toEnum 242
326 addr4 = mk_ip $ toEnum 243
327 expected = [addr1, addr2, addr3, addr4]
328 actual = enumerate (read "192.168.0.240/30" :: Cidr)
329
330 test_min_host1 :: TestTree
331 test_min_host1 =
332 testCase desc $ actual @?= expected
333 where
334 desc = "The minimum host in 10.0.0.0/24 is 10.0.0.0"
335 actual = show $ min_host (read "10.0.0.0/24" :: Cidr)
336 expected = "10.0.0.0"
337
338
339 test_max_host1 :: TestTree
340 test_max_host1 =
341 testCase desc $ actual @?= expected
342 where
343 desc = "The maximum host in 10.0.0.0/24 is 10.0.0.255"
344 actual = show $ max_host (read "10.0.0.0/24" :: Cidr)
345 expected = "10.0.0.255"
346
347
348 test_equality1 :: TestTree
349 test_equality1 =
350 testCase desc $ actual @?= expected
351 where
352 desc = "10.1.1.0/23 equals itself"
353 actual = read "10.1.1.0/23" :: Cidr
354 expected = read "10.1.1.0/23" :: Cidr
355
356
357 test_contains1 :: TestTree
358 test_contains1 =
359 testCase desc $ actual @?= expected
360 where
361 desc = "10.1.1.0/23 contains 10.1.1.0/24"
362 cidr1 = read "10.1.1.0/23" :: Cidr
363 cidr2 = read "10.1.1.0/24" :: Cidr
364 expected = True
365 actual = cidr1 `contains` cidr2
366
367
368 test_contains2 :: TestTree
369 test_contains2 =
370 testCase desc $ actual @?= expected
371 where
372 desc = "10.1.1.0/23 contains itself"
373 cidr1 = read "10.1.1.0/23" :: Cidr
374 expected = True
375 actual = cidr1 `contains` cidr1
376
377
378 test_contains_proper1 :: TestTree
379 test_contains_proper1 =
380 testCase desc $ actual @?= expected
381 where
382 desc = "10.1.1.0/23 contains 10.1.1.0/24 properly"
383 cidr1 = read "10.1.1.0/23" :: Cidr
384 cidr2 = read "10.1.1.0/24" :: Cidr
385 expected = True
386 actual = cidr1 `contains_proper` cidr2
387
388
389 test_contains_proper2 :: TestTree
390 test_contains_proper2 =
391 testCase desc $ actual @?= expected
392 where
393 desc = "10.1.1.0/23 does not contain itself properly"
394 cidr1 = read "10.1.1.0/23" :: Cidr
395 expected = False
396 actual = cidr1 `contains_proper` cidr1
397
398
399 test_adjacent1 :: TestTree
400 test_adjacent1 =
401 testCase desc $ actual @?= expected
402 where
403 desc = "10.1.0.0/24 is adjacent to 10.1.1.0/24"
404 cidr1 = read "10.1.0.0/24" :: Cidr
405 cidr2 = read "10.1.1.0/24" :: Cidr
406 expected = True
407 actual = cidr1 `adjacent` cidr2
408
409
410 test_adjacent2 :: TestTree
411 test_adjacent2 =
412 testCase desc $ actual @?= expected
413 where
414 desc = "10.1.0.0/23 is not adjacent to 10.1.0.0/24"
415 cidr1 = read "10.1.0.0/23" :: Cidr
416 cidr2 = read "10.1.0.0/24" :: Cidr
417 expected = False
418 actual = cidr1 `adjacent` cidr2
419
420
421 test_adjacent3 :: TestTree
422 test_adjacent3 =
423 testCase desc $ actual @?= expected
424 where
425 desc = "10.1.0.0/24 is not adjacent to 10.2.5.0/24"
426 cidr1 = read "10.1.0.0/24" :: Cidr
427 cidr2 = read "10.2.5.0/24" :: Cidr
428 expected = False
429 actual = cidr1 `adjacent` cidr2
430
431
432 test_adjacent4 :: TestTree
433 test_adjacent4 =
434 testCase desc $ actual @?= expected
435 where
436 desc = "10.1.1.0/24 is not adjacent to 10.1.2.0/24"
437 cidr1 = read "10.1.1.0/24" :: Cidr
438 cidr2 = read "10.1.2.0/24" :: Cidr
439 expected = False
440 actual = cidr1 `adjacent` cidr2
441
442 test_combine_contained1 :: TestTree
443 test_combine_contained1 =
444 testCase desc $ actual @?= expected
445 where
446 desc = "10.0.0.0/8, 10.1.0.0/16, and 10.1.1.0/24 combine to 10.0.0.0/8"
447 cidr1 = read "10.0.0.0/8" :: Cidr
448 cidr2 = read "10.1.0.0/16" :: Cidr
449 cidr3 = read "10.1.1.0/24" :: Cidr
450 test_cidrs = [cidr1, cidr2, cidr3]
451 expected = [cidr1]
452 actual = combine_contained test_cidrs
453
454 test_combine_contained2 :: TestTree
455 test_combine_contained2 =
456 testCase desc $ actual @?= expected
457 where
458 desc = "192.168.3.0/23 does not contain 192.168.1.0/24"
459 cidr1 = read "192.168.3.0/23" :: Cidr
460 cidr2 = read "192.168.1.0/24" :: Cidr
461 expected = [cidr1, cidr2]
462 actual = combine_contained [cidr1, cidr2]
463
464
465 test_combine_all1 :: TestTree
466 test_combine_all1 =
467 testCase desc $ actual @?= expected
468 where
469 desc = "10.0.0.0/24 is adjacent to 10.0.1.0/24 "
470 ++ "and 10.0.3.0/23 contains 10.0.2.0/24"
471 cidr1 = read "10.0.0.0/24" :: Cidr
472 cidr2 = read "10.0.1.0/24" :: Cidr
473 cidr3 = read "10.0.2.0/24" :: Cidr
474 cidr4 = read "10.0.3.0/23" :: Cidr
475 cidr5 = read "10.0.0.0/23" :: Cidr
476 test_cidrs = [cidr1, cidr2, cidr3, cidr4, cidr5]
477 expected = [read "10.0.0.0/22" :: Cidr]
478 actual = combine_all test_cidrs
479
480
481 test_combine_all2 :: TestTree
482 test_combine_all2 =
483 testCase desc $ actual @?= expected
484 where
485 desc = "127.0.0.1/32 combines with itself recursively"
486 cidr1 = read "127.0.0.1/32" :: Cidr
487 test_cidrs = [cidr1, cidr1, cidr1, cidr1, cidr1]
488 expected = [cidr1]
489 actual = combine_all test_cidrs
490
491
492 test_combine_all3 :: TestTree
493 test_combine_all3 =
494 testCase desc $ actual @?= expected
495 where
496 desc = "10.0.0.16, 10.0.0.17, 10.0.0.18, and "
497 ++ "10.0.0.19 get combined into 10.0.0.16/30"
498 cidr1 = read "10.0.0.16/32" :: Cidr
499 cidr2 = read "10.0.0.17/32" :: Cidr
500 cidr3 = read "10.0.0.18/32" :: Cidr
501 cidr4 = read "10.0.0.19/32" :: Cidr
502 test_cidrs = [cidr1, cidr2, cidr3, cidr4]
503 expected = [read "10.0.0.16/30" :: Cidr]
504 actual = combine_all test_cidrs
505
506 test_normalize1 :: TestTree
507 test_normalize1 =
508 testCase desc $ actual @?= expected
509 where
510 desc = "127.0.0.1/8 normalized is 127.0.0.0/8"
511 expected = read "127.0.0.0/8" :: Cidr
512 actual = normalize (read "127.0.0.1/8" :: Cidr)
513
514
515 test_normalize2 :: TestTree
516 test_normalize2 =
517 testCase desc $ actual @?= expected
518 where
519 desc = "192.168.1.101/24 normalized is 192.168.1.0/24"
520 expected = read "192.168.1.0/24" :: Cidr
521 actual = normalize (read "192.168.1.101/24" :: Cidr)
522
523 test_normalize3 :: TestTree
524 test_normalize3 =
525 testCase desc $ actual @?= expected
526 where
527 desc = "10.10.10.10/22 normalized is 10.10.8.0/22"
528 expected = read "10.10.8.0/22" :: Cidr
529 actual = normalize (read "10.10.10.10/22" :: Cidr)
530
531 -- QuickCheck Tests
532 prop_all_cidrs_contain_themselves :: TestTree
533 prop_all_cidrs_contain_themselves =
534 testProperty "All CIDRs contain themselves" prop
535 where
536 prop :: Cidr -> Bool
537 prop cidr1 = cidr1 `contains` cidr1
538
539
540 -- If cidr1 properly contains cidr2, then by definition cidr2
541 -- does not properly contain cidr1.
542 prop_contains_proper_antisymmetric :: TestTree
543 prop_contains_proper_antisymmetric =
544 testProperty "CIDR proper containment is an antisymmetric relation" prop
545 where
546 prop :: Cidr -> Cidr -> Property
547 prop cidr1 cidr2 =
548 (cidr1 `contains_proper` cidr2) ==>
549 (not (cidr2 `contains_proper` cidr1))
550
551
552 -- Running "normalize" a second time shouldn't do anything.
553 prop_normalize_idempotent :: TestTree
554 prop_normalize_idempotent =
555 testProperty "The CIDR \"normalize\" function is idempotent" prop
556 where
557 prop :: Cidr -> Bool
558 prop cidr = (normalize cidr) == (normalize (normalize cidr))
559
560 -- Normalization should not affect equality of two CIDRs.
561 prop_normalize_preserves_equality :: TestTree
562 prop_normalize_preserves_equality =
563 testProperty "The CIDR \"normalize\" function preserves equality" prop
564 where
565 prop :: Cidr -> Cidr -> Bool
566 prop cidr1 cidr2 = (cidr1 == cidr2) == (normalize cidr1 == normalize cidr2)
567
568
569 prop_ord_instance_reflexive :: TestTree
570 prop_ord_instance_reflexive =
571 testProperty "The CIDR order is reflexive" prop
572 where
573 prop :: Cidr -> Bool
574 prop cidr = cidr <= cidr
575
576
577 prop_ord_instance_transitive :: TestTree
578 prop_ord_instance_transitive =
579 testProperty "The CIDR order is transitive" prop
580 where
581 prop :: Cidr -> Cidr -> Cidr -> Property
582 prop cidr1 cidr2 cidr3 =
583 (cidr1 <= cidr2 && cidr2 <= cidr3) ==> cidr1 <= cidr3
584
585 -- This is how Eq is currently implemented, but it is useful to have
586 -- around in case that changes. Try fewer instances of this than usual
587 -- because it's a rare condition.
588 prop_ord_instance_antisymmetric :: TestTree
589 prop_ord_instance_antisymmetric =
590 localOption (QuickCheckTests 500) $
591 testProperty "The CIDR order is antisymmetric" prop
592 where
593 prop :: Cidr -> Cidr -> Property
594 prop cidr1 cidr2 =
595 (cidr1 <= cidr2 && cidr2 <= cidr1) ==> cidr1 == cidr2