X-Git-Url: http://gitweb.michael.orlitzky.com/?a=blobdiff_plain;f=src%2FCidr.hs;h=422a0adf076216f16b128791a610eee54fc9ca42;hb=9200fe5fcab505e5a331514a6ee687c6f78011b1;hp=8fc014f6cf5b062f8ae019d3b990c22a9604e9b8;hpb=a3c4fbd855c182390f39cdf88de36d7cb0ea69d5;p=hath.git diff --git a/src/Cidr.hs b/src/Cidr.hs index 8fc014f..422a0ad 100644 --- a/src/Cidr.hs +++ b/src/Cidr.hs @@ -31,7 +31,7 @@ import Octet data Cidr = None | Cidr { ipv4address :: IPv4Address, maskbits :: Maskbits } - deriving (Eq) + instance Show Cidr where @@ -45,7 +45,9 @@ instance Arbitrary Cidr where mask <- arbitrary :: Gen Maskbits return (Cidr ipv4 mask) - coarbitrary _ = variant 0 + +instance Eq Cidr where + cidr1 == cidr2 = (cidr1 `equivalent` cidr2) -- Two CIDR ranges are equivalent if they have the same network bits @@ -60,8 +62,11 @@ equivalent (Cidr addr1 mbits1) (Cidr addr2 mbits2) = -- Returns the mask portion of a CIDR address. That is, everything -- after the trailing slash. maskbits_from_cidr_string :: String -> Maskbits -maskbits_from_cidr_string s = - maskbits_from_string ((splitWith (`elem` "/") s) !! 1) +maskbits_from_cidr_string s + | length partlist == 2 = maskbits_from_string (partlist !! 1) + | otherwise = Maskbits.None + where + partlist = (splitWith (`elem` "/") s) -- Takes an IP address String in CIDR notation, and returns a list of @@ -191,9 +196,15 @@ redundant cidrlist cidr = any ((flip contains_proper) cidr) cidrlist -- adjacent Cidrs are combined into a larger one, they will be removed -- in the second step since the larger Cidr must contain the smaller -- two. +-- +-- Once this is done, we see whether or not the result is different +-- than the argument that was passed in. If nothing changed, we're +-- done and return the list that was passed to us. However, if +-- something changed, we recurse and try to combine the list again. combine_all :: [Cidr] -> [Cidr] -combine_all cidrs = - combine_contained unique_cidrs +combine_all cidrs + | cidrs == (combine_contained unique_cidrs) = cidrs + | otherwise = combine_all (combine_contained unique_cidrs) where unique_cidrs = nubBy equivalent valid_cidr_combinations valid_cidr_combinations = filter (/= Cidr.None) cidr_combinations @@ -242,6 +253,22 @@ adjacent cidr1 cidr2 -- HUnit Tests +test_min_host1 :: Test +test_min_host1 = + TestCase $ assertEqual "The minimum host in 10.0.0.0/24 is 10.0.0.0" expected actual + where + actual = show $ min_host (cidr_from_string "10.0.0.0/24") + expected = "10.0.0.0" + + +test_max_host1 :: Test +test_max_host1 = + TestCase $ assertEqual "The maximum host in 10.0.0.0/24 is 10.0.0.255" expected actual + where + actual = show $ max_host (cidr_from_string "10.0.0.0/24") + expected = "10.0.0.255" + + test_equality1 :: Test test_equality1 = TestCase $ assertEqual "10.1.1.0/23 equals itself" True (cidr1 == cidr1) @@ -339,8 +366,8 @@ test_combine_all1 = cidr3 = cidr_from_string "10.0.2.0/24" cidr4 = cidr_from_string "10.0.3.0/23" cidr5 = cidr_from_string "10.0.0.0/23" - expected_cidrs = [cidr4, cidr5] - test_cidrs = [cidr1, cidr2, cidr3, cidr4] + expected_cidrs = [cidr_from_string "10.0.0.0/22"] + test_cidrs = [cidr1, cidr2, cidr3, cidr4, cidr5] test_combine_all2 :: Test @@ -352,8 +379,22 @@ test_combine_all2 = test_cidrs = [cidr1, cidr1, cidr1, cidr1, cidr1] +test_combine_all3 :: Test +test_combine_all3 = + TestCase $ assertEqual "10.0.0.16, 10.0.0.17, 10.0.0.18, and 10.0.0.19 get combined into 10.0.0.16/30" expected_cidrs (combine_all test_cidrs) + where + cidr1 = cidr_from_string "10.0.0.16/32" + cidr2 = cidr_from_string "10.0.0.17/32" + cidr3 = cidr_from_string "10.0.0.18/32" + cidr4 = cidr_from_string "10.0.0.19/32" + expected_cidrs = [cidr_from_string "10.0.0.16/30"] + test_cidrs = [cidr1, cidr2, cidr3, cidr4] + + cidr_tests :: [Test] -cidr_tests = [ test_equality1, +cidr_tests = [ test_min_host1, + test_max_host1, + test_equality1, test_contains1, test_contains2, test_contains_proper1, @@ -365,7 +406,8 @@ cidr_tests = [ test_equality1, test_combine_contained1, test_combine_contained2, test_combine_all1, - test_combine_all2 + test_combine_all2, + test_combine_all3 ]