]> gitweb.michael.orlitzky.com - hath.git/blob - src/Cidr.hs
Started updating the max_host function to use record syntax.
[hath.git] / src / Cidr.hs
1 module Cidr
2 ( Cidr(..),
3 cidr_from_string,
4 cidr_tests,
5 combine_all,
6 contains,
7 contains_proper,
8 max_octet1,
9 max_octet2,
10 max_octet3,
11 max_octet4,
12 min_octet1,
13 min_octet2,
14 min_octet3,
15 min_octet4,
16 prop_all_cidrs_contain_themselves,
17 prop_contains_proper_intransitive
18 ) where
19
20 import Data.List (nubBy)
21 import Test.HUnit
22 import Test.QuickCheck
23
24 import qualified Bit as B
25 import IPv4Address
26 import ListUtils
27 import Maskable
28 import Maskbits
29 import Octet
30
31
32 data Cidr = None | Cidr { ipv4address :: IPv4Address,
33 maskbits :: Maskbits }
34 deriving (Eq)
35
36
37 instance Show Cidr where
38 show Cidr.None = "None"
39 show cidr = (show (ipv4address cidr)) ++ "/" ++ (show (maskbits cidr))
40
41
42 instance Arbitrary Cidr where
43 arbitrary = do
44 ipv4 <- arbitrary :: Gen IPv4Address
45 mask <- arbitrary :: Gen Maskbits
46 return (Cidr ipv4 mask)
47
48 coarbitrary _ = variant 0
49
50
51 -- Two CIDR ranges are equivalent if they have the same network bits
52 -- and the masks are the same.
53 equivalent :: Cidr -> Cidr -> Bool
54 equivalent Cidr.None Cidr.None = True
55 equivalent Cidr.None _ = False
56 equivalent _ Cidr.None = False
57 equivalent (Cidr addr1 mbits1) (Cidr addr2 mbits2) =
58 (mbits1 == mbits2) && ((apply_mask addr1 mbits1) == (apply_mask addr2 mbits2))
59
60 -- Returns the mask portion of a CIDR address. That is, everything
61 -- after the trailing slash.
62 maskbits_from_cidr_string :: String -> Maskbits
63 maskbits_from_cidr_string s =
64 maskbits_from_string ((splitWith (`elem` "/") s) !! 1)
65
66
67 -- Takes an IP address String in CIDR notation, and returns a list of
68 -- its octets (as Ints).
69 octets_from_cidr_string :: String -> [Octet]
70 octets_from_cidr_string s =
71 map octet_from_string (take 4 (splitWith (`elem` "./") s))
72
73
74 cidr_from_string :: String -> Cidr
75 cidr_from_string s
76 | addr == IPv4Address.None = Cidr.None
77 | mbits == Maskbits.None = Cidr.None
78 | otherwise = Cidr addr mbits
79 where
80 addr = ipv4address_from_octets (oct1) (oct2) (oct3) (oct4)
81 oct1 = (octs !! 0)
82 oct2 = (octs !! 1)
83 oct3 = (octs !! 2)
84 oct4 = (octs !! 3)
85 octs = octets_from_cidr_string s
86 mbits = maskbits_from_cidr_string s
87
88
89
90 min_host :: Cidr -> IPv4Address
91 min_host Cidr.None = IPv4Address.None
92 min_host (Cidr IPv4Address.None _) = IPv4Address.None
93 min_host (Cidr _ Maskbits.None) = IPv4Address.None
94 min_host (Cidr addr mask)
95 | mask == ThirtyTwo = addr
96 | mask == ThirtyOne = addr { octet4 = apply_mask oct4 Seven }
97 | mask == Thirty = addr { octet4 = apply_mask oct4 Six }
98 | mask == TwentyNine = addr { octet4 = apply_mask oct4 Five }
99 | mask == TwentyEight = addr { octet4 = apply_mask oct4 Four }
100 | mask == TwentySeven = addr { octet4 = apply_mask oct4 Three }
101 | mask == TwentySix = addr { octet4 = apply_mask oct4 Two }
102 | mask == TwentyFive = addr { octet4 = apply_mask oct4 One }
103 | mask == TwentyFour = addr { octet4 = min_octet }
104 | mask == TwentyThree = addr { octet3 = apply_mask oct3 Seven,
105 octet4 = min_octet }
106 | mask == TwentyTwo = addr { octet3 = apply_mask oct3 Six,
107 octet4 = min_octet }
108 | mask == TwentyOne = addr { octet3 = apply_mask oct3 Five,
109 octet4 = min_octet }
110 | mask == Twenty = addr { octet3 = apply_mask oct3 Four,
111 octet4 = min_octet }
112 | mask == Nineteen = addr { octet3 = apply_mask oct3 Three,
113 octet4 = min_octet }
114 | mask == Eighteen = addr { octet3 = apply_mask oct3 Two,
115 octet4 = min_octet }
116 | mask == Seventeen = addr { octet3 = apply_mask oct3 One,
117 octet4 = min_octet }
118 | mask == Sixteen = addr { octet3 = min_octet,
119 octet4 = min_octet }
120 | mask == Fifteen = addr { octet2 = apply_mask oct2 Seven,
121 octet3 = min_octet,
122 octet4 = min_octet }
123 | mask == Fourteen = addr { octet2 = apply_mask oct2 Six,
124 octet3 = min_octet,
125 octet4 = min_octet }
126 | mask == Thirteen = addr { octet2 = apply_mask oct2 Five,
127 octet3 = min_octet,
128 octet4 = min_octet }
129 | mask == Twelve = addr { octet2 = apply_mask oct2 Four,
130 octet3 = min_octet,
131 octet4 = min_octet }
132 | mask == Eleven = addr { octet2 = apply_mask oct2 Three,
133 octet3 = min_octet,
134 octet4 = min_octet }
135 | mask == Ten = addr { octet2 = apply_mask oct2 Two,
136 octet3 = min_octet,
137 octet4 = min_octet }
138 | mask == Nine = addr { octet2 = apply_mask oct2 One,
139 octet3 = min_octet,
140 octet4 = min_octet }
141 | mask == Eight = addr { octet2 = min_octet,
142 octet3 = min_octet,
143 octet4 = min_octet }
144 | mask == Seven = addr { octet1 = apply_mask oct1 Seven,
145 octet2 = min_octet,
146 octet3 = min_octet,
147 octet4 = min_octet }
148 | mask == Six = addr { octet1 = apply_mask oct1 Six,
149 octet2 = min_octet,
150 octet3 = min_octet,
151 octet4 = min_octet }
152 | mask == Five = addr { octet1 = apply_mask oct1 Five,
153 octet2 = min_octet,
154 octet3 = min_octet,
155 octet4 = min_octet }
156 | mask == Four = addr { octet1 = apply_mask oct1 Four,
157 octet2 = min_octet,
158 octet3 = min_octet,
159 octet4 = min_octet }
160 | mask == Three = addr { octet1 = apply_mask oct1 Three,
161 octet2 = min_octet,
162 octet3 = min_octet,
163 octet4 = min_octet }
164 | mask == Two = addr { octet1 = apply_mask oct1 Two,
165 octet2 = min_octet,
166 octet3 = min_octet,
167 octet4 = min_octet }
168 | mask == One = addr { octet1 = apply_mask oct1 One,
169 octet2 = min_octet,
170 octet3 = min_octet,
171 octet4 = min_octet }
172 | mask == Zero = addr { octet1 = min_octet,
173 octet2 = min_octet,
174 octet3 = min_octet,
175 octet4 = min_octet }
176 | otherwise = addr
177 where
178 oct1 = (octet1 addr)
179 oct2 = (octet2 addr)
180 oct3 = (octet3 addr)
181 oct4 = (octet4 addr)
182
183
184
185 max_host :: Cidr -> IPv4Address
186 max_host Cidr.None = IPv4Address.None
187 max_host (Cidr IPv4Address.None _) = IPv4Address.None
188 max_host (Cidr _ Maskbits.None) = IPv4Address.None
189 max_host (Cidr addr mask)
190 | mask == ThirtyTwo = addr
191 | mask == ThirtyOne = addr { octet4 = oct4 { b8 = B.One } }
192 | mask == Thirty = addr { octet4 = oct4 { b7 = B.One, b8 = B.One } }
193 | mask == TwentyNine = addr { octet4 = oct4 { b6 = B.One,
194 b7 = B.One,
195 b8 = B.One } }
196 | mask == TwentyEight = addr { octet4 = oct4 { b5 = B.One,
197 b6 = B.One,
198 b7 = B.One,
199 b8 = B.One } }
200 | mask == TwentySeven = addr { octet4 = oct4 { b4 = B.One,
201 b5 = B.One,
202 b6 = B.One,
203 b7 = B.One,
204 b8 = B.One } }
205 | mask == TwentySix = addr { octet4 = oct4 { b3 = B.One,
206 b4 = B.One,
207 b5 = B.One,
208 b6 = B.One,
209 b7 = B.One,
210 b8 = B.One } }
211 | mask == TwentyFive = addr { octet4 = oct4 { b2 = B.One,
212 b3 = B.One,
213 b4 = B.One,
214 b5 = B.One,
215 b6 = B.One,
216 b7 = B.One,
217 b8 = B.One } }
218 | mask == TwentyFour = addr { octet4 = max_octet }
219 | mask == TwentyThree = ipv4address_from_octets oct1 oct2 (Octet a17 a18 a19 a20 a21 a22 a23 B.One) (max_octet)
220 | mask == TwentyTwo = ipv4address_from_octets oct1 oct2 (Octet a17 a18 a19 a20 a21 a22 B.One B.One) (max_octet)
221 | mask == TwentyOne = ipv4address_from_octets oct1 oct2 (Octet a17 a18 a19 a20 a21 B.One B.One B.One) (max_octet)
222 | mask == Twenty = ipv4address_from_octets oct1 oct2 (Octet a17 a18 a19 a20 B.One B.One B.One B.One) (max_octet)
223 | mask == Nineteen = ipv4address_from_octets oct1 oct2 (Octet a17 a18 a19 B.One B.One B.One B.One B.One) (max_octet)
224 | mask == Eighteen = ipv4address_from_octets oct1 oct2 (Octet a17 a18 B.One B.One B.One B.One B.One B.One) (max_octet)
225 | mask == Seventeen = ipv4address_from_octets oct1 oct2 (Octet a17 B.One B.One B.One B.One B.One B.One B.One) (max_octet)
226 | mask == Sixteen = ipv4address_from_octets oct1 oct2 (max_octet) (max_octet)
227 | mask == Fifteen = ipv4address_from_octets oct1 (Octet a9 a10 a11 a12 a13 a14 a15 B.One) (max_octet) (max_octet)
228 | mask == Fourteen = ipv4address_from_octets oct1 (Octet a9 a10 a11 a12 a13 a14 B.One B.One) (max_octet) (max_octet)
229 | mask == Thirteen = ipv4address_from_octets oct1 (Octet a9 a10 a11 a12 a13 B.One B.One B.One) (max_octet) (max_octet)
230 | mask == Twelve = ipv4address_from_octets oct1 (Octet a9 a10 a11 a12 B.One B.One B.One B.One) (max_octet) (max_octet)
231 | mask == Eleven = ipv4address_from_octets oct1 (Octet a9 a10 a11 B.One B.One B.One B.One B.One) (max_octet) (max_octet)
232 | mask == Ten = ipv4address_from_octets oct1 (Octet a9 a10 B.One B.One B.One B.One B.One B.One) (max_octet) (max_octet)
233 | mask == Nine = ipv4address_from_octets oct1 (Octet a9 B.One B.One B.One B.One B.One B.One B.One) (max_octet) (max_octet)
234 | mask == Eight = ipv4address_from_octets oct1 (max_octet) (max_octet) (max_octet)
235 | mask == Seven = ipv4address_from_octets (Octet a1 a2 a3 a4 a5 a6 a7 B.One) (max_octet) (max_octet) (max_octet)
236 | mask == Six = ipv4address_from_octets (Octet a1 a2 a3 a4 a5 a6 B.One B.One) (max_octet) (max_octet) (max_octet)
237 | mask == Five = ipv4address_from_octets (Octet a1 a2 a3 a4 a5 B.One B.One B.One) (max_octet) (max_octet) (max_octet)
238 | mask == Four = ipv4address_from_octets (Octet a1 a2 a3 a4 B.One B.One B.One B.One) (max_octet) (max_octet) (max_octet)
239 | mask == Three = ipv4address_from_octets (Octet a1 a2 a3 B.One B.One B.One B.One B.One) (max_octet) (max_octet) (max_octet)
240 | mask == Two = ipv4address_from_octets (Octet a1 a2 B.One B.One B.One B.One B.One B.One) (max_octet) (max_octet) (max_octet)
241 | mask == One = ipv4address_from_octets (Octet a1 B.One B.One B.One B.One B.One B.One B.One) (max_octet) (max_octet) (max_octet)
242 | mask == Zero = ipv4address_from_octets (max_octet) (max_octet) (max_octet) (max_octet)
243 | otherwise = addr
244 where
245 a1 = (b1 oct1)
246 a2 = (b2 oct1)
247 a3 = (b3 oct1)
248 a4 = (b4 oct1)
249 a5 = (b5 oct1)
250 a6 = (b6 oct1)
251 a7 = (b7 oct1)
252 a9 = (b1 oct2)
253 a10 = (b2 oct2)
254 a11 = (b3 oct2)
255 a12 = (b4 oct2)
256 a13 = (b5 oct2)
257 a14 = (b6 oct2)
258 a15 = (b7 oct2)
259 a17 = (b1 oct3)
260 a18 = (b2 oct3)
261 a19 = (b3 oct3)
262 a20 = (b4 oct3)
263 a21 = (b5 oct3)
264 a22 = (b6 oct3)
265 a23 = (b7 oct3)
266 oct1 = (octet1 addr)
267 oct2 = (octet2 addr)
268 oct3 = (octet3 addr)
269 oct4 = (octet4 addr)
270
271
272
273 min_octet1 :: Cidr -> Octet
274 min_octet1 cidr = octet1 (min_host cidr)
275
276 min_octet2 :: Cidr -> Octet
277 min_octet2 cidr = octet2 (min_host cidr)
278
279 min_octet3 :: Cidr -> Octet
280 min_octet3 cidr = octet3 (min_host cidr)
281
282 min_octet4 :: Cidr -> Octet
283 min_octet4 cidr = octet4 (min_host cidr)
284
285 max_octet1 :: Cidr -> Octet
286 max_octet1 cidr = octet1 (max_host cidr)
287
288 max_octet2 :: Cidr -> Octet
289 max_octet2 cidr = octet2 (max_host cidr)
290
291 max_octet3 :: Cidr -> Octet
292 max_octet3 cidr = octet3 (max_host cidr)
293
294 max_octet4 :: Cidr -> Octet
295 max_octet4 cidr = octet4 (max_host cidr)
296
297
298
299 -- Return true if the first argument (a CIDR range) contains the
300 -- second (another CIDR range). There are a lot of ways we can be fed
301 -- junk here. For lack of a better alternative, just return False when
302 -- we are given nonsense.
303 contains :: Cidr -> Cidr -> Bool
304 contains Cidr.None _ = False
305 contains _ Cidr.None = False
306 contains (Cidr _ Maskbits.None) _ = False
307 contains (Cidr IPv4Address.None _) _ = False
308 contains _ (Cidr _ Maskbits.None) = False
309 contains _ (Cidr IPv4Address.None _) = False
310
311 -- If the number of bits in the network part of the first address is
312 -- larger than the number of bits in the second, there is no way that
313 -- the first range can contain the second. For, if the number of
314 -- network bits is larger, then the number of host bits must be
315 -- smaller, and if cidr1 has fewer hosts than cidr2, cidr1 most
316 -- certainly does not contain cidr2.
317 --
318 -- On the other hand, if the first argument (cidr1) has fewer (or the
319 -- same number of) network bits as the second, it can contain the
320 -- second. In this case, we need to check that every host in cidr2 is
321 -- contained in cidr1. If a host in cidr2 is contained in cidr1, then
322 -- at least mbits1 of an address in cidr2 will match cidr1. For
323 -- example,
324 --
325 -- cidr1 = 192.168.1.0/23, cidr2 = 192.168.1.100/24
326 --
327 -- Here, cidr2 contains all of 192.168.1.0 through
328 -- 192.168.1.255. However, cidr1 contains BOTH 192.168.0.0 through
329 -- 192.168.0.255 and 192.168.1.0 through 192.168.1.255. In essence,
330 -- what we want to check is that cidr2 "begins with" something that
331 -- cidr1 CAN begin with. Since cidr1 can begin with 192.168.1, and
332 -- cidr2 DOES, cidr1 contains cidr2..
333 --
334 -- The way that we check this is to apply cidr1's mask to cidr2's
335 -- address and see if the result is the same as cidr1's mask applied
336 -- to cidr1's address.
337 --
338 contains (Cidr addr1 mbits1) (Cidr addr2 mbits2)
339 | mbits1 > mbits2 = False
340 | otherwise = addr1masked == addr2masked
341 where
342 addr1masked = apply_mask addr1 mbits1
343 addr2masked = apply_mask addr2 mbits1
344
345
346 contains_proper :: Cidr -> Cidr -> Bool
347 contains_proper cidr1 cidr2 =
348 (cidr1 `contains` cidr2) && (not (cidr2 `contains` cidr1))
349
350
351 -- A CIDR range is redundant (with respect to the given list) if
352 -- another CIDR range in that list properly contains it.
353 redundant :: [Cidr] -> Cidr -> Bool
354 redundant cidrlist cidr = any ((flip contains_proper) cidr) cidrlist
355
356
357 -- First, we look at all possible pairs of cidrs, and combine the
358 -- adjacent ones in to a new list. Then, we concatenate that list with
359 -- the original one, and filter out all of the redundancies. If two
360 -- adjacent Cidrs are combined into a larger one, they will be removed
361 -- in the second step since the larger Cidr must contain the smaller
362 -- two.
363 combine_all :: [Cidr] -> [Cidr]
364 combine_all cidrs =
365 combine_contained unique_cidrs
366 where
367 unique_cidrs = nubBy equivalent valid_cidr_combinations
368 valid_cidr_combinations = filter (/= Cidr.None) cidr_combinations
369 cidr_combinations =
370 cidrs ++ [ (combine_adjacent x y) | x <- cidrs, y <- cidrs ]
371
372
373 -- Take a list of CIDR ranges and filter out all of the ones that are
374 -- contained entirelt within some other range in the list.
375 combine_contained :: [Cidr] -> [Cidr]
376 combine_contained cidrs =
377 filter (not . (redundant cidrs)) cidrs
378
379
380 -- If the two Cidrs are not adjacent, return Cidr.None. Otherwise,
381 -- decrement the maskbits of cidr1 and return that; it will contain
382 -- both cidr1 and cidr2.
383 combine_adjacent :: Cidr -> Cidr -> Cidr
384 combine_adjacent cidr1 cidr2
385 | not (adjacent cidr1 cidr2) = Cidr.None
386 | (maskbits cidr1 == Zero) = Cidr.None
387 | otherwise = cidr1 { maskbits = decrement (maskbits cidr1) }
388
389
390
391 -- Determine whether or not two CIDR ranges are adjacent. If two
392 -- ranges lie consecutively within the IP space, they can be
393 -- combined. For example, 10.1.0.0/24 and 10.0.1.0/24 are adjacent,
394 -- and can be combined in to 10.1.0.0/23.
395 adjacent :: Cidr -> Cidr -> Bool
396 adjacent Cidr.None _ = False
397 adjacent _ Cidr.None = False
398 adjacent cidr1 cidr2
399 | mbits1 /= mbits2 = False
400 | mbits1 == Maskbits.Zero = False -- They're equal.
401 | otherwise = (mbits1 == (most_sig_bit_different addr1 addr2))
402 where
403 addr1 = ipv4address cidr1
404 addr2 = ipv4address cidr2
405 mbits1 = maskbits cidr1
406 mbits2 = maskbits cidr2
407
408
409
410
411
412 -- HUnit Tests
413
414 test_equality1 :: Test
415 test_equality1 =
416 TestCase $ assertEqual "10.1.1.0/23 equals itself" True (cidr1 == cidr1)
417 where
418 cidr1 = cidr_from_string "10.1.1.0/23"
419
420
421 test_contains1 :: Test
422 test_contains1 =
423 TestCase $ assertEqual "10.1.1.0/23 contains 10.1.1.0/24" True (cidr1 `contains` cidr2)
424 where
425 cidr1 = cidr_from_string "10.1.1.0/23"
426 cidr2 = cidr_from_string "10.1.1.0/24"
427
428
429 test_contains2 :: Test
430 test_contains2 =
431 TestCase $ assertEqual "10.1.1.0/23 contains itself" True (cidr1 `contains` cidr1)
432 where
433 cidr1 = cidr_from_string "10.1.1.0/23"
434
435
436 test_contains_proper1 :: Test
437 test_contains_proper1 =
438 TestCase $ assertEqual "10.1.1.0/23 contains 10.1.1.0/24 properly" True (cidr1 `contains_proper` cidr2)
439 where
440 cidr1 = cidr_from_string "10.1.1.0/23"
441 cidr2 = cidr_from_string "10.1.1.0/24"
442
443
444 test_contains_proper2 :: Test
445 test_contains_proper2 =
446 TestCase $ assertEqual "10.1.1.0/23 does not contain itself properly" False (cidr1 `contains_proper` cidr1)
447 where
448 cidr1 = cidr_from_string "10.1.1.0/23"
449
450
451 test_adjacent1 :: Test
452 test_adjacent1 =
453 TestCase $ assertEqual "10.1.0.0/24 is adjacent to 10.1.1.0/24" True (cidr1 `adjacent` cidr2)
454 where
455 cidr1 = cidr_from_string "10.1.0.0/24"
456 cidr2 = cidr_from_string "10.1.1.0/24"
457
458
459 test_adjacent2 :: Test
460 test_adjacent2 =
461 TestCase $ assertEqual "10.1.0.0/23 is not adjacent to 10.1.0.0/24" False (cidr1 `adjacent` cidr2)
462 where
463 cidr1 = cidr_from_string "10.1.0.0/23"
464 cidr2 = cidr_from_string "10.1.0.0/24"
465
466
467 test_adjacent3 :: Test
468 test_adjacent3 =
469 TestCase $ assertEqual "10.1.0.0/24 is not adjacent to 10.2.5.0/24" False (cidr1 `adjacent` cidr2)
470 where
471 cidr1 = cidr_from_string "10.1.0.0/24"
472 cidr2 = cidr_from_string "10.2.5.0/24"
473
474
475 test_adjacent4 :: Test
476 test_adjacent4 =
477 TestCase $ assertEqual "10.1.1.0/24 is not adjacent to 10.1.2.0/24" False (cidr1 `adjacent` cidr2)
478 where
479 cidr1 = cidr_from_string "10.1.1.0/24"
480 cidr2 = cidr_from_string "10.1.2.0/24"
481
482
483 test_combine_contained1 :: Test
484 test_combine_contained1 =
485 TestCase $ assertEqual "10.0.0.0/8, 10.1.0.0/16, and 10.1.1.0/24 combine to 10.0.0.0/8" expected_cidrs (combine_contained test_cidrs)
486 where
487 cidr1 = cidr_from_string "10.0.0.0/8"
488 cidr2 = cidr_from_string "10.1.0.0/16"
489 cidr3 = cidr_from_string "10.1.1.0/24"
490 expected_cidrs = [cidr1]
491 test_cidrs = [cidr1, cidr2, cidr3]
492
493
494 test_combine_contained2 :: Test
495 test_combine_contained2 =
496 TestCase $ assertEqual "192.168.3.0/23 does not contain 192.168.1.0/24" [cidr1, cidr2] (combine_contained [cidr1, cidr2])
497 where
498 cidr1 = cidr_from_string "192.168.3.0/23"
499 cidr2 = cidr_from_string "192.168.1.0/24"
500
501
502 test_combine_all1 :: Test
503 test_combine_all1 =
504 TestCase $ assertEqual "10.0.0.0/24 is adjacent to 10.0.1.0/24 and 10.0.3.0/23 contains 10.0.2.0/24" expected_cidrs (combine_all test_cidrs)
505 where
506 cidr1 = cidr_from_string "10.0.0.0/24"
507 cidr2 = cidr_from_string "10.0.1.0/24"
508 cidr3 = cidr_from_string "10.0.2.0/24"
509 cidr4 = cidr_from_string "10.0.3.0/23"
510 cidr5 = cidr_from_string "10.0.0.0/23"
511 expected_cidrs = [cidr4, cidr5]
512 test_cidrs = [cidr1, cidr2, cidr3, cidr4]
513
514
515 test_combine_all2 :: Test
516 test_combine_all2 =
517 TestCase $ assertEqual "127.0.0.1/32 combines with itself recursively" expected_cidrs (combine_all test_cidrs)
518 where
519 cidr1 = cidr_from_string "127.0.0.1/32"
520 expected_cidrs = [cidr1]
521 test_cidrs = [cidr1, cidr1, cidr1, cidr1, cidr1]
522
523
524 cidr_tests :: [Test]
525 cidr_tests = [ test_equality1,
526 test_contains1,
527 test_contains2,
528 test_contains_proper1,
529 test_contains_proper2,
530 test_adjacent1,
531 test_adjacent2,
532 test_adjacent3,
533 test_adjacent4,
534 test_combine_contained1,
535 test_combine_contained2,
536 test_combine_all1,
537 test_combine_all2
538 ]
539
540
541 -- QuickCheck Tests
542 prop_all_cidrs_contain_themselves :: Cidr -> Bool
543 prop_all_cidrs_contain_themselves cidr1 = cidr1 `contains` cidr1
544
545
546 -- If cidr1 properly contains cidr2, then by definition cidr2
547 -- does not properly contain cidr1.
548 prop_contains_proper_intransitive :: Cidr -> Cidr -> Property
549 prop_contains_proper_intransitive cidr1 cidr2 =
550 (cidr1 `contains_proper` cidr2) ==>
551 (not (cidr2 `contains_proper` cidr1))