Browse Source

Add MacAddress

master
Denis Tereshkin 2 years ago
parent
commit
9684383a98
  1. 5
      src/Main.hs
  2. 10
      src/Packets/L2.hs
  3. 20
      src/Packets/MacAddress.hs
  4. 2
      src/Packets/Serializable.hs
  5. 14
      test/Test.hs
  6. 48
      test/Test/Packets/MacAddress.hs
  7. 18
      theta.cabal

5
src/Main.hs

@ -1,5 +0,0 @@ @@ -1,5 +0,0 @@
module Main (main) where
main :: IO ()
main = do
putStrLn "hello world"

10
src/Packets/L2.hs

@ -0,0 +1,10 @@ @@ -0,0 +1,10 @@
module Packets.L2
(
) where
import Packets.Packet (Serializable (..))
data L2Header

20
src/Packets/MacAddress.hs

@ -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

2
src/Packets/Packet.hs → src/Packets/Serializable.hs

@ -1,5 +1,5 @@ @@ -1,5 +1,5 @@
module Packets.Packet
module Packets.Serializable
(
Serializable(..)
) where

14
test/Test.hs

@ -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 ]

48
test/Test/Packets/MacAddress.hs

@ -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

18
theta.cabal

@ -16,9 +16,12 @@ extra-source-files: README.md @@ -16,9 +16,12 @@ extra-source-files: README.md
library theta-lib
hs-source-dirs: src
modules: Packets.Packet
modules: Packets.Serializable
, Packets.L2
, Packets.MacAddress
default-language: Haskell2010
build-depends: base >= 4.7 && < 5
, bytestring
ghc-options: -Wall
-Wcompat
-Widentities
@ -28,3 +31,16 @@ library theta-lib @@ -28,3 +31,16 @@ library theta-lib
-Wmissing-home-modules
-Wpartial-fields
-Wredundant-constraints
Test-Suite test-theta
type: exitcode-stdio-1.0
hs-source-dirs: src test
main-is: Test.hs
other-modules: Test.Packets.MacAddress
build-depends: base
, theta-lib
, tasty
, tasty-hunit
, tasty-hedgehog
, hedgehog
, bytestring

Loading…
Cancel
Save