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