X-Git-Url: http://gitweb.michael.orlitzky.com/?a=blobdiff_plain;f=src%2FIPv4Address.hs;h=d262a5c0537ea47092d0255ab417b6516d1cdab8;hb=eee156f562f9c1c1194a67cef12f146304d88ce9;hp=b46f18c453ea967d19291810cd824a546975f5c9;hpb=a6569af040d40db73dfd32677893916638c0d56a;p=hath.git diff --git a/src/IPv4Address.hs b/src/IPv4Address.hs index b46f18c..d262a5c 100644 --- a/src/IPv4Address.hs +++ b/src/IPv4Address.hs @@ -1,18 +1,23 @@ -module IPv4Address -( ipv4address_tests, +module IPv4Address( IPv4Address(..), - max_address, - min_address, - most_sig_bit_different, -) where + ipv4address_properties, + ipv4address_tests, + most_sig_bit_different ) +where -import Data.Maybe (fromJust) -import Test.HUnit -import Test.QuickCheck -import Maskable -import Maskbits -import Octet +import Test.QuickCheck ( Gen ) -- Not re-exported by tasty +import Test.Tasty ( TestTree, testGroup ) +import Test.Tasty.HUnit ( (@?=), testCase ) +import Test.Tasty.QuickCheck ( + Arbitrary(..), + Property, + (==>), + testProperty ) + +import Maskable (Maskable(..)) +import Maskbits (Maskbits(..)) +import Octet (Octet(..)) data IPv4Address = IPv4Address { octet1 :: Octet, @@ -163,18 +168,49 @@ instance Maskable IPv4Address where new_addr3 { octet1 = (apply_mask oct1 Zero bit) } - --- | The minimum possible IPv4 address, 0.0.0.0. -min_address :: IPv4Address -min_address = - IPv4Address min_octet min_octet min_octet min_octet - - --- | The maximum possible IPv4 address, 255.255.255.255. -max_address :: IPv4Address -max_address = - IPv4Address max_octet max_octet max_octet max_octet - +instance Bounded IPv4Address where + -- | The minimum possible IPv4 address, 0.0.0.0. + minBound = IPv4Address minBound minBound minBound minBound + + -- | The maximum possible IPv4 address, 255.255.255.255. + maxBound = IPv4Address maxBound maxBound maxBound maxBound + + + + +instance Enum IPv4Address where + -- | Convert an 'Int' @x@ to an 'IPv4Address'. Each octet of @x@ is + -- right-shifted by the appropriate number of bits, and the fractional + -- part is dropped. + toEnum x = + IPv4Address oct1 oct2 oct3 oct4 + where + -- Chop off the higher octets. x1 = x `mod` 2^32, would be + -- redundant. + x2 = x `mod` 2^(24 :: Integer) + x3 = x `mod` 2^(16 :: Integer) + x4 = x `mod` 2^(8 :: Integer) + -- Perform right-shifts. x4 doesn't need a shift. + shifted_x1 = x `quot` 2^(24 :: Integer) + shifted_x2 = x2 `quot` 2^(16 :: Integer) + shifted_x3 = x3 `quot` 2^(8 :: Integer) + oct1 = toEnum shifted_x1 + oct2 = toEnum shifted_x2 + oct3 = toEnum shifted_x3 + oct4 = toEnum x4 + + -- | Convert @addr@ to an 'Int' by converting each octet to an 'Int' + -- and shifting the result to the left by 0,8.16, or 24 bits. + fromEnum addr = + (shifted_oct1) + (shifted_oct2) + (shifted_oct3) + oct4 + where + oct1 = fromEnum (octet1 addr) + oct2 = fromEnum (octet2 addr) + oct3 = fromEnum (octet3 addr) + oct4 = fromEnum (octet4 addr) + shifted_oct1 = oct1 * 2^(24 :: Integer) + shifted_oct2 = oct2 * 2^(16 :: Integer) + shifted_oct3 = oct3 * 2^(8 :: Integer) -- | Given two addresses, find the number of the most significant bit -- where they differ. If the addresses are the same, return @@ -290,42 +326,100 @@ most_sig_bit_different addr1 addr2 oct4b = (octet4 addr2) +-- Test lists. +ipv4address_tests :: TestTree +ipv4address_tests = + testGroup "IPv4 Address Tests" [ + test_enum, + test_maxBound, + test_minBound, + test_most_sig_bit_different1, + test_most_sig_bit_different2, + test_to_enum ] + +ipv4address_properties :: TestTree +ipv4address_properties = + testGroup + "IPv4 Address Properties " + [ prop_from_enum_to_enum_inverses ] + +-- QuickCheck properties +prop_from_enum_to_enum_inverses :: TestTree +prop_from_enum_to_enum_inverses = + testProperty "fromEnum and toEnum are inverses" prop + where + prop :: Int -> Property + prop x = + (0 <= x) && (x <= 2^(32 :: Integer) - 1) ==> + fromEnum (toEnum x :: IPv4Address) == x -- HUnit Tests mk_testaddr :: Int -> Int -> Int -> Int -> IPv4Address mk_testaddr a b c d = IPv4Address oct1 oct2 oct3 oct4 where - oct1 = fromJust $ octet_from_int a - oct2 = fromJust $ octet_from_int b - oct3 = fromJust $ octet_from_int c - oct4 = fromJust $ octet_from_int d + oct1 = toEnum a + oct2 = toEnum b + oct3 = toEnum c + oct4 = toEnum d + + +test_minBound :: TestTree +test_minBound = + testCase desc $ actual @?= expected + where + desc = "minBound should be 0.0.0.0" + expected = mk_testaddr 0 0 0 0 + actual = minBound :: IPv4Address + +test_maxBound :: TestTree +test_maxBound = + testCase desc $ actual @?= expected + where + desc = "maxBound should be 255.255.255.255" + expected = mk_testaddr 255 255 255 255 + actual = maxBound :: IPv4Address -test_most_sig_bit_different1 :: Test + +test_enum :: TestTree +test_enum = + testCase desc $ actual @?= expected + where + desc = "enumerating a /24 gives the correct addresses" + expected = ["192.168.0." ++ (show x) | x <- [0..255::Int] ] + lb = mk_testaddr 192 168 0 0 + ub = mk_testaddr 192 168 0 255 + actual = map show [lb..ub] + + +test_most_sig_bit_different1 :: TestTree test_most_sig_bit_different1 = - TestCase $ assertEqual "10.1.1.0 and 10.1.0.0 differ in bit 24" - TwentyFour - bit + testCase desc $ actual @?= expected where + desc = "10.1.1.0 and 10.1.0.0 differ in bit 24" addr1 = mk_testaddr 10 1 1 0 addr2 = (mk_testaddr 10 1 0 0) - bit = most_sig_bit_different addr1 addr2 + expected = TwentyFour + actual = most_sig_bit_different addr1 addr2 -test_most_sig_bit_different2 :: Test +test_most_sig_bit_different2 :: TestTree test_most_sig_bit_different2 = - TestCase $ assertEqual "10.1.2.0 and 10.1.1.0 differ in bit 23" - TwentyThree - bit + testCase desc $ actual @?= expected where + desc = "10.1.2.0 and 10.1.1.0 differ in bit 23" addr1 = mk_testaddr 10 1 2 0 addr2 = mk_testaddr 10 1 1 0 - bit = most_sig_bit_different addr1 addr2 + expected = TwentyThree + actual = most_sig_bit_different addr1 addr2 -ipv4address_tests :: [Test] -ipv4address_tests = - [ test_most_sig_bit_different1, - test_most_sig_bit_different2 ] +test_to_enum :: TestTree +test_to_enum = + testCase desc $ actual @?= expected + where + desc = "192.168.0.0 in base-10 is 3232235520" + expected = mk_testaddr 192 168 0 0 + actual = toEnum 3232235520