7 changed files with 111 additions and 8 deletions
@ -1,5 +0,0 @@
@@ -1,5 +0,0 @@
|
||||
module Main (main) where |
||||
|
||||
main :: IO () |
||||
main = do |
||||
putStrLn "hello world" |
||||
@ -0,0 +1,10 @@
@@ -0,0 +1,10 @@
|
||||
|
||||
module Packets.L2 |
||||
( |
||||
) where |
||||
|
||||
import Packets.Packet (Serializable (..)) |
||||
|
||||
|
||||
data L2Header |
||||
|
||||
@ -0,0 +1,20 @@
@@ -0,0 +1,20 @@
|
||||
|
||||
module Packets.MacAddress |
||||
( |
||||
MacAddress(..) |
||||
, broadcastMac |
||||
) where |
||||
|
||||
import qualified Data.ByteString as B |
||||
import Data.Word (Word8) |
||||
import Packets.Serializable (Serializable (..)) |
||||
|
||||
data MacAddress = |
||||
MacAddress Word8 Word8 Word8 Word8 Word8 Word8 |
||||
deriving (Show, Eq) |
||||
|
||||
instance Serializable MacAddress where |
||||
serialize (MacAddress b1 b2 b3 b4 b5 b6) = B.pack [b1, b2, b3, b4, b5, b6] |
||||
|
||||
broadcastMac :: MacAddress |
||||
broadcastMac = MacAddress 0xff 0xff 0xff 0xff 0xff 0xff |
||||
@ -1,8 +1,8 @@
@@ -1,8 +1,8 @@
|
||||
|
||||
module Packets.Packet |
||||
module Packets.Serializable |
||||
( |
||||
Serializable(..) |
||||
) where |
||||
) where |
||||
|
||||
import qualified Data.ByteString as B |
||||
|
||||
@ -0,0 +1,14 @@
@@ -0,0 +1,14 @@
|
||||
|
||||
module Main |
||||
( |
||||
main |
||||
) where |
||||
|
||||
import qualified Test.Packets.MacAddress |
||||
import Test.Tasty |
||||
|
||||
main = defaultMain tests |
||||
|
||||
tests :: TestTree |
||||
tests = testGroup "Tests" [ Test.Packets.MacAddress.tests ] |
||||
|
||||
@ -0,0 +1,48 @@
@@ -0,0 +1,48 @@
|
||||
|
||||
module Test.Packets.MacAddress |
||||
( |
||||
tests |
||||
) where |
||||
|
||||
import qualified Data.ByteString as B |
||||
import Data.Word (Word8 (..)) |
||||
import Hedgehog (MonadGen (..), forAll, property, (===)) |
||||
import Hedgehog.Gen |
||||
import qualified Hedgehog.Range as Range |
||||
import Packets.MacAddress |
||||
import Packets.Serializable |
||||
import Test.Tasty |
||||
import Test.Tasty.Hedgehog |
||||
import Test.Tasty.HUnit |
||||
|
||||
tests :: TestTree |
||||
tests = testGroup "MacAddress" [ unitTests, properties ] |
||||
|
||||
unitTests :: TestTree |
||||
unitTests = testGroup "Unit tests" [ testSerialization ] |
||||
|
||||
testSerialization :: TestTree |
||||
testSerialization = testCase "serialization" $ do |
||||
let mac = MacAddress 0x01 0x00 0x5e 0x01 0x02 0x03 |
||||
let bs = serialize mac |
||||
bs @?= B.pack [0x01, 0x00, 0x5e, 0x01, 0x02, 0x03] |
||||
|
||||
|
||||
genMac :: (MonadGen m) => Range.Range Word8 -> m MacAddress |
||||
genMac range = do |
||||
b1 <- word8 range |
||||
b2 <- word8 range |
||||
b3 <- word8 range |
||||
b4 <- word8 range |
||||
b5 <- word8 range |
||||
b6 <- word8 range |
||||
return $ MacAddress b1 b2 b3 b4 b5 b6 |
||||
|
||||
properties :: TestTree |
||||
properties = testGroup "Properties" [ propMacAddressLength ] |
||||
|
||||
propMacAddressLength :: TestTree |
||||
propMacAddressLength = testProperty "Serialized MAC address length always equal to 6" $ |
||||
property $ do |
||||
mac <- forAll $ genMac (Range.constantBounded) |
||||
(B.length . serialize) mac === 6 |
||||
Loading…
Reference in new issue