import Test.QuickCheck
import qualified Bit as B
+import Maskable
import Maskbits
import Octet
+instance Maskable IPv4Address where
+ apply_mask _ Maskbits.None = IPv4Address.None
+ apply_mask addr mask
+ | mask == ThirtyTwo = ipv4address_from_octets oct1 oct2 oct3 oct4
+ | mask == ThirtyOne = ipv4address_from_octets oct1 oct2 oct3 (apply_mask oct4 Seven)
+ | mask == Thirty = ipv4address_from_octets oct1 oct2 oct3 (apply_mask oct4 Six)
+ | mask == TwentyNine = ipv4address_from_octets oct1 oct2 oct3 (apply_mask oct4 Five)
+ | mask == TwentyEight = ipv4address_from_octets oct1 oct2 oct3 (apply_mask oct4 Four)
+ | mask == TwentySeven = ipv4address_from_octets oct1 oct2 oct3 (apply_mask oct4 Three)
+ | mask == TwentySix = ipv4address_from_octets oct1 oct2 oct3 (apply_mask oct4 Two)
+ | mask == TwentyFive = ipv4address_from_octets oct1 oct2 oct3 (apply_mask oct4 One)
+ | mask == TwentyFour = ipv4address_from_octets oct1 oct2 oct3 (min_octet)
+ | mask == TwentyThree = ipv4address_from_octets oct1 oct2 (apply_mask oct3 Seven) (min_octet)
+ | mask == TwentyTwo = ipv4address_from_octets oct1 oct2 (apply_mask oct3 Six) (min_octet)
+ | mask == TwentyOne = ipv4address_from_octets oct1 oct2 (apply_mask oct3 Five) (min_octet)
+ | mask == Twenty = ipv4address_from_octets oct1 oct2 (apply_mask oct3 Four) (min_octet)
+ | mask == Nineteen = ipv4address_from_octets oct1 oct2 (apply_mask oct3 Three) (min_octet)
+ | mask == Eighteen = ipv4address_from_octets oct1 oct2 (apply_mask oct3 Two) (min_octet)
+ | mask == Seventeen = ipv4address_from_octets oct1 oct2 (apply_mask oct3 One) (min_octet)
+ | mask == Sixteen = ipv4address_from_octets oct1 oct2 (min_octet) (min_octet)
+ | mask == Fifteen = ipv4address_from_octets oct1 (apply_mask oct2 Seven) (min_octet) (min_octet)
+ | mask == Fourteen = ipv4address_from_octets oct1 (apply_mask oct2 Six) (min_octet) (min_octet)
+ | mask == Thirteen = ipv4address_from_octets oct1 (apply_mask oct2 Five) (min_octet) (min_octet)
+ | mask == Twelve = ipv4address_from_octets oct1 (apply_mask oct2 Four) (min_octet) (min_octet)
+ | mask == Eleven = ipv4address_from_octets oct1 (apply_mask oct2 Three) (min_octet) (min_octet)
+ | mask == Ten = ipv4address_from_octets oct1 (apply_mask oct2 Two) (min_octet) (min_octet)
+ | mask == Nine = ipv4address_from_octets oct1 (apply_mask oct2 One) (min_octet) (min_octet)
+ | mask == Eight = ipv4address_from_octets oct1 (min_octet) (min_octet) (min_octet)
+ | mask == Seven = ipv4address_from_octets (apply_mask oct1 Seven) (min_octet) (min_octet) (min_octet)
+ | mask == Six = ipv4address_from_octets (apply_mask oct1 Six) (min_octet) (min_octet) (min_octet)
+ | mask == Five = ipv4address_from_octets (apply_mask oct1 Five) (min_octet) (min_octet) (min_octet)
+ | mask == Four = ipv4address_from_octets (apply_mask oct1 Four) (min_octet) (min_octet) (min_octet)
+ | mask == Three = ipv4address_from_octets (apply_mask oct1 Three) (min_octet) (min_octet) (min_octet)
+ | mask == Two = ipv4address_from_octets (apply_mask oct1 Two) (min_octet) (min_octet) (min_octet)
+ | mask == One = ipv4address_from_octets (apply_mask oct1 One) (min_octet) (min_octet) (min_octet)
+ | mask == Zero = ipv4address_from_octets (min_octet) (min_octet) (min_octet) (min_octet)
+ | otherwise = IPv4Address.None
+ where
+ oct1 = (octet1 addr)
+ oct2 = (octet2 addr)
+ oct3 = (octet3 addr)
+ oct4 = (octet4 addr)
+
+
-- We don't export our constructor so this function is the only
-- way to construct an address from octets. As a result, we can
-- return IPv4Address.None in response to being passed one of more
module Maskable where
-import Test.HUnit
-
-import qualified Bit as B
-import IPv4Address
import Maskbits
-import Octet
-- Any string of bits should be maskable by some number of netmask
-- bits. The convention of the Maskable typeclass follows CIDR
class Maskable a where
apply_mask :: a -> Maskbits -> a
-
-instance Maskable Octet where
- apply_mask _ Maskbits.None = Octet.None
- apply_mask Octet.None _ = Octet.None
- apply_mask oct mask
- | mask == Eight = oct
- | mask == Seven = oct { b8 = B.Zero }
- | mask == Six = oct { b8 = B.Zero, b7 = B.Zero }
- | mask == Five = oct { b8 = B.Zero, b7 = B.Zero, b6 = B.Zero }
- | mask == Four = oct { b8 = B.Zero, b7 = B.Zero, b6 = B.Zero, b5 = B.Zero }
- | mask == Three = oct { b8 = B.Zero, b7 = B.Zero, b6 = B.Zero, b5 = B.Zero, b4 = B.Zero }
- | mask == Two = oct { b8 = B.Zero, b7 = B.Zero, b6 = B.Zero, b5 = B.Zero, b4 = B.Zero, b3 = B.Zero }
- | mask == One = oct { b8 = B.Zero, b7 = B.Zero, b6 = B.Zero, b5 = B.Zero, b4 = B.Zero, b3 = B.Zero, b2 = B.Zero }
- | mask == Zero = min_octet
- | otherwise = Octet.None
-
-
-
-instance Maskable IPv4Address where
- apply_mask _ Maskbits.None = IPv4Address.None
- apply_mask addr mask
- | mask == ThirtyTwo = ipv4address_from_octets oct1 oct2 oct3 oct4
- | mask == ThirtyOne = ipv4address_from_octets oct1 oct2 oct3 (apply_mask oct4 Seven)
- | mask == Thirty = ipv4address_from_octets oct1 oct2 oct3 (apply_mask oct4 Six)
- | mask == TwentyNine = ipv4address_from_octets oct1 oct2 oct3 (apply_mask oct4 Five)
- | mask == TwentyEight = ipv4address_from_octets oct1 oct2 oct3 (apply_mask oct4 Four)
- | mask == TwentySeven = ipv4address_from_octets oct1 oct2 oct3 (apply_mask oct4 Three)
- | mask == TwentySix = ipv4address_from_octets oct1 oct2 oct3 (apply_mask oct4 Two)
- | mask == TwentyFive = ipv4address_from_octets oct1 oct2 oct3 (apply_mask oct4 One)
- | mask == TwentyFour = ipv4address_from_octets oct1 oct2 oct3 (min_octet)
- | mask == TwentyThree = ipv4address_from_octets oct1 oct2 (apply_mask oct3 Seven) (min_octet)
- | mask == TwentyTwo = ipv4address_from_octets oct1 oct2 (apply_mask oct3 Six) (min_octet)
- | mask == TwentyOne = ipv4address_from_octets oct1 oct2 (apply_mask oct3 Five) (min_octet)
- | mask == Twenty = ipv4address_from_octets oct1 oct2 (apply_mask oct3 Four) (min_octet)
- | mask == Nineteen = ipv4address_from_octets oct1 oct2 (apply_mask oct3 Three) (min_octet)
- | mask == Eighteen = ipv4address_from_octets oct1 oct2 (apply_mask oct3 Two) (min_octet)
- | mask == Seventeen = ipv4address_from_octets oct1 oct2 (apply_mask oct3 One) (min_octet)
- | mask == Sixteen = ipv4address_from_octets oct1 oct2 (min_octet) (min_octet)
- | mask == Fifteen = ipv4address_from_octets oct1 (apply_mask oct2 Seven) (min_octet) (min_octet)
- | mask == Fourteen = ipv4address_from_octets oct1 (apply_mask oct2 Six) (min_octet) (min_octet)
- | mask == Thirteen = ipv4address_from_octets oct1 (apply_mask oct2 Five) (min_octet) (min_octet)
- | mask == Twelve = ipv4address_from_octets oct1 (apply_mask oct2 Four) (min_octet) (min_octet)
- | mask == Eleven = ipv4address_from_octets oct1 (apply_mask oct2 Three) (min_octet) (min_octet)
- | mask == Ten = ipv4address_from_octets oct1 (apply_mask oct2 Two) (min_octet) (min_octet)
- | mask == Nine = ipv4address_from_octets oct1 (apply_mask oct2 One) (min_octet) (min_octet)
- | mask == Eight = ipv4address_from_octets oct1 (min_octet) (min_octet) (min_octet)
- | mask == Seven = ipv4address_from_octets (apply_mask oct1 Seven) (min_octet) (min_octet) (min_octet)
- | mask == Six = ipv4address_from_octets (apply_mask oct1 Six) (min_octet) (min_octet) (min_octet)
- | mask == Five = ipv4address_from_octets (apply_mask oct1 Five) (min_octet) (min_octet) (min_octet)
- | mask == Four = ipv4address_from_octets (apply_mask oct1 Four) (min_octet) (min_octet) (min_octet)
- | mask == Three = ipv4address_from_octets (apply_mask oct1 Three) (min_octet) (min_octet) (min_octet)
- | mask == Two = ipv4address_from_octets (apply_mask oct1 Two) (min_octet) (min_octet) (min_octet)
- | mask == One = ipv4address_from_octets (apply_mask oct1 One) (min_octet) (min_octet) (min_octet)
- | mask == Zero = ipv4address_from_octets (min_octet) (min_octet) (min_octet) (min_octet)
- | otherwise = IPv4Address.None
- where
- oct1 = (octet1 addr)
- oct2 = (octet2 addr)
- oct3 = (octet3 addr)
- oct4 = (octet4 addr)
-
-
-
--- HUnit Tests
-
-test_octet_mask1 :: Test
-test_octet_mask1 =
- TestCase $ assertEqual "The network bits of 255/4 should equal 240" oct2 (apply_mask oct1 Four)
- where
- oct1 = octet_from_int 255
- oct2 = octet_from_int 240
-
-
-test_octet_mask2 :: Test
-test_octet_mask2 =
- TestCase $ assertEqual "The network bits of 255/1 should equal 128" oct2 (apply_mask oct1 One)
- where
- oct1 = octet_from_int 255
- oct2 = octet_from_int 128
-
-
-maskable_tests :: [Test]
-maskable_tests = [ test_octet_mask1,
- test_octet_mask2 ]
import Test.HUnit
import Test.QuickCheck
-import Bit
+import Bit as B
+import Maskable
+import Maskbits
-- An Octet consists of eight bits. For our purposes, the most
-- significant bit will come "first." That is, b1 is in the 2^7
coarbitrary _ = variant 0
+instance Maskable Octet where
+ apply_mask _ Maskbits.None = Octet.None
+ apply_mask Octet.None _ = Octet.None
+ apply_mask oct mask
+ | mask == Eight = oct
+ | mask == Seven = oct { b8 = B.Zero }
+ | mask == Six = oct { b8 = B.Zero, b7 = B.Zero }
+ | mask == Five = oct { b8 = B.Zero, b7 = B.Zero, b6 = B.Zero }
+ | mask == Four = oct { b8 = B.Zero, b7 = B.Zero, b6 = B.Zero, b5 = B.Zero }
+ | mask == Three = oct { b8 = B.Zero, b7 = B.Zero, b6 = B.Zero, b5 = B.Zero, b4 = B.Zero }
+ | mask == Two = oct { b8 = B.Zero, b7 = B.Zero, b6 = B.Zero, b5 = B.Zero, b4 = B.Zero, b3 = B.Zero }
+ | mask == Maskbits.One = oct { b8 = B.Zero, b7 = B.Zero, b6 = B.Zero, b5 = B.Zero, b4 = B.Zero, b3 = B.Zero, b2 = B.Zero }
+ | mask == Maskbits.Zero = min_octet
+ | otherwise = Octet.None
+
+
-- Convert each bit to its integer value, and multiply by the
-- appropriate power of two. Sum them up, and we should get an integer
-- between 0 and 255.
| (x < 0) || (x > 255) = Octet.None
| otherwise = (Octet a1 a2 a3 a4 a5 a6 a7 a8)
where
- a1 = if (x >= 128) then One else Zero
- a2 = if ((x `mod` 128) >= 64) then One else Zero
- a3 = if ((x `mod` 64) >= 32) then One else Zero
- a4 = if ((x `mod` 32) >= 16) then One else Zero
- a5 = if ((x `mod` 16) >= 8) then One else Zero
- a6 = if ((x `mod` 8) >= 4) then One else Zero
- a7 = if ((x `mod` 4) >= 2) then One else Zero
- a8 = if ((x `mod` 2) == 1) then One else Zero
+ a1 = if (x >= 128) then B.One else B.Zero
+ a2 = if ((x `mod` 128) >= 64) then B.One else B.Zero
+ a3 = if ((x `mod` 64) >= 32) then B.One else B.Zero
+ a4 = if ((x `mod` 32) >= 16) then B.One else B.Zero
+ a5 = if ((x `mod` 16) >= 8) then B.One else B.Zero
+ a6 = if ((x `mod` 8) >= 4) then B.One else B.Zero
+ a7 = if ((x `mod` 4) >= 2) then B.One else B.Zero
+ a8 = if ((x `mod` 2) == 1) then B.One else B.Zero
octet_from_string :: String -> Octet
-- The octet with the least possible value.
min_octet :: Octet
-min_octet = Octet Zero Zero Zero Zero Zero Zero Zero Zero
+min_octet = Octet B.Zero B.Zero B.Zero B.Zero B.Zero B.Zero B.Zero B.Zero
-- The octet with the greatest possible value.
max_octet :: Octet
-max_octet = Octet One One One One One One One One
+max_octet = Octet B.One B.One B.One B.One B.One B.One B.One B.One
test_octet_from_int1 =
TestCase $ assertEqual "octet_from_int 128 should parse as 10000000" oct1 (octet_from_int 128)
where
- oct1 = Octet One Zero Zero Zero Zero Zero Zero Zero
+ oct1 = Octet B.One B.Zero B.Zero B.Zero B.Zero B.Zero B.Zero B.Zero
+
+
+test_octet_mask1 :: Test
+test_octet_mask1 =
+ TestCase $ assertEqual "The network bits of 255/4 should equal 240" oct2 (apply_mask oct1 Four)
+ where
+ oct1 = octet_from_int 255
+ oct2 = octet_from_int 240
+
+
+test_octet_mask2 :: Test
+test_octet_mask2 =
+ TestCase $ assertEqual "The network bits of 255/1 should equal 128" oct2 (apply_mask oct1 Maskbits.One)
+ where
+ oct1 = octet_from_int 255
+ oct2 = octet_from_int 128
octet_tests :: [Test]
-octet_tests = [ test_octet_from_int1 ]
+octet_tests = [ test_octet_from_int1,
+ test_octet_mask1,
+ test_octet_mask2 ]
prop_contains_proper_intransitive)
import IPv4Address (ipv4address_tests)
-import Maskable (maskable_tests)
import Octet (octet_tests)
-- The list of HUnit tests.
test_suite = TestList (concat [cidr_tests,
ipv4address_tests,
- maskable_tests,
octet_tests])
-- QuickCheck options