aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--src/Network/WireGuard/Internal/Data/RpcTypes.hs2
-rw-r--r--src/Network/WireGuard/Internal/RpcParsers.hs8
-rw-r--r--tests/spec/Network/WireGuard/RPCSpec.hs37
3 files changed, 42 insertions, 5 deletions
diff --git a/src/Network/WireGuard/Internal/Data/RpcTypes.hs b/src/Network/WireGuard/Internal/Data/RpcTypes.hs
index d45d38e..a78d9df 100644
--- a/src/Network/WireGuard/Internal/Data/RpcTypes.hs
+++ b/src/Network/WireGuard/Internal/Data/RpcTypes.hs
@@ -31,7 +31,7 @@ data RpcRequest = RpcRequest {
data RpcSetPayload = RpcSetPayload {
devicePayload :: !RpcDevicePayload,
peersPayload :: [RpcPeerPayload]
-}
+} deriving (Eq, Show)
-- | Device related payload sent together with a set RPC operation.
data RpcDevicePayload = RpcDevicePayload {
diff --git a/src/Network/WireGuard/Internal/RpcParsers.hs b/src/Network/WireGuard/Internal/RpcParsers.hs
index c5179c8..e5e38d1 100644
--- a/src/Network/WireGuard/Internal/RpcParsers.hs
+++ b/src/Network/WireGuard/Internal/RpcParsers.hs
@@ -3,7 +3,8 @@
module Network.WireGuard.Internal.RpcParsers(
requestParser,
deviceParser,
- peerParser
+ peerParser,
+ setPayloadParser
) where
import Control.Applicative ((*>), (<|>))
import Control.Monad (join)
@@ -48,7 +49,10 @@ requestTypeParser = "get=1" *> return Get
<|> "set=1" *> return Set
setPayloadParser :: Parser RpcSetPayload
-setPayloadParser = undefined
+setPayloadParser = do
+ dev <- deviceParser
+ peers <- many' peerParser
+ return $ RpcSetPayload dev peers
deviceParser :: Parser RpcDevicePayload
deviceParser = do
diff --git a/tests/spec/Network/WireGuard/RPCSpec.hs b/tests/spec/Network/WireGuard/RPCSpec.hs
index 18ba13d..ec77662 100644
--- a/tests/spec/Network/WireGuard/RPCSpec.hs
+++ b/tests/spec/Network/WireGuard/RPCSpec.hs
@@ -24,11 +24,13 @@ import Test.Hspec (Spec, describe,
shouldSatisfy)
import Network.WireGuard.RPC (serveConduit, showPeer)
-import Network.WireGuard.Internal.RpcParsers (deviceParser, peerParser)
+import Network.WireGuard.Internal.RpcParsers (deviceParser, peerParser,
+ setPayloadParser)
import Network.WireGuard.Internal.State (Device(..), Peer(..),
createDevice, createPeer)
import Network.WireGuard.Internal.Data.Types (PresharedKey, PeerId)
-import qualified Network.WireGuard.Internal.Data.RpcTypes as RPC (RpcDevicePayload(..), RpcPeerPayload(..))
+import qualified Network.WireGuard.Internal.Data.RpcTypes as RPC (RpcDevicePayload(..), RpcPeerPayload(..),
+ RpcSetPayload(..))
spec :: Spec
spec = do
@@ -141,6 +143,37 @@ spec = do
it "must not parse a peer having an incorrect allowed ip" $ do
let result = feed (parse peerParser $ BC.pack "public_key=2e14fd594556f522604703340351258903b64f35553763f19426ab2a515c58\nendpoint=192.168.1.1:1337\nallowed_ip=192.168.1.0.2/24\n") BC.empty
eitherResult result `shouldSatisfy` isLeft
+ describe "setPayloadParser" $ do
+ it "must parse a standard set payload" $ do
+ pkHex <- unhex $ BC.pack "e84b5a6d2717c1003a13b431570353dbaca9146cf150c5f8575680feba52027a"
+ let pk = DH.dhBytesToPair $ BA.convert pkHex
+ pubHex <- unhex $ BC.pack "662e14fd594556f522604703340351258903b64f35553763f19426ab2a515c58"
+ let pubK = fromJust . DH.dhBytesToPub $ BA.convert pubHex
+ let expectedDevice = RPC.RpcDevicePayload pk 777 (Just 0) False
+ let expectedPeer = RPC.RpcPeerPayload pubK False Nothing (SockAddrInet 1337 $ tupleToHostAddress (192,168,1,1)) 0 False [IPv4Range (read "192.168.1.0/24" :: AddrRange IPv4)]
+ let expectedPayload = RPC.RpcSetPayload expectedDevice [expectedPeer]
+ let result = feed (parse setPayloadParser $ BC.pack "private_key=e84b5a6d2717c1003a13b431570353dbaca9146cf150c5f8575680feba52027a\nlisten_port=777\nfwmark=0\npublic_key=662e14fd594556f522604703340351258903b64f35553763f19426ab2a515c58\nendpoint=192.168.1.1:1337\nallowed_ip=192.168.1.0/24\n") BC.empty
+ eitherResult result `shouldBe` Right expectedPayload
+ it "must parse a set payload containing several peers" $ do
+ pkHex <- unhex $ BC.pack "e84b5a6d2717c1003a13b431570353dbaca9146cf150c5f8575680feba52027a"
+ let pk = DH.dhBytesToPair $ BA.convert pkHex
+ pubHex1 <- unhex $ BC.pack "662e14fd594556f522604703340351258903b64f35553763f19426ab2a515c58"
+ let pubK1 = fromJust . DH.dhBytesToPub $ BA.convert pubHex1
+ pubHex2 <- unhex $ BC.pack "b85996fecc9c7f1fc6d2572a76eda11d59bcd20be8e543b15ce4bd85a8e75a33"
+ let pubK2 = fromJust . DH.dhBytesToPub $ BA.convert pubHex2
+ let expectedDevice = RPC.RpcDevicePayload pk 777 (Just 0) False
+ let expectedPeer1 = RPC.RpcPeerPayload pubK1 False Nothing (SockAddrInet 1337 $ tupleToHostAddress (192,168,1,1)) 0 False [IPv4Range (read "192.168.1.0/24" :: AddrRange IPv4)]
+ let expectedPeer2 = RPC.RpcPeerPayload pubK2 False Nothing (SockAddrInet 1337 $ tupleToHostAddress (192,168,1,1)) 0 False [IPv4Range (read "192.168.1.0/24" :: AddrRange IPv4)]
+ let expectedPayload = RPC.RpcSetPayload expectedDevice [expectedPeer1, expectedPeer2]
+ let result = feed (parse setPayloadParser $ BC.pack "private_key=e84b5a6d2717c1003a13b431570353dbaca9146cf150c5f8575680feba52027a\nlisten_port=777\nfwmark=0\npublic_key=662e14fd594556f522604703340351258903b64f35553763f19426ab2a515c58\nendpoint=192.168.1.1:1337\nallowed_ip=192.168.1.0/24\npublic_key=b85996fecc9c7f1fc6d2572a76eda11d59bcd20be8e543b15ce4bd85a8e75a33\nendpoint=192.168.1.1:1337\nallowed_ip=192.168.1.0/24\n") BC.empty
+ eitherResult result `shouldBe` Right expectedPayload
+ it "must parse a set payload containing no peers" $ do
+ pkHex <- unhex $ BC.pack "e84b5a6d2717c1003a13b431570353dbaca9146cf150c5f8575680feba52027a"
+ let pk = DH.dhBytesToPair $ BA.convert pkHex
+ let expectedDevice = RPC.RpcDevicePayload pk 777 (Just 0) False
+ let expectedPayload = RPC.RpcSetPayload expectedDevice []
+ let result = feed (parse setPayloadParser $ BC.pack "private_key=e84b5a6d2717c1003a13b431570353dbaca9146cf150c5f8575680feba52027a\nlisten_port=777\nfwmark=0\n") BC.empty
+ eitherResult result `shouldBe` Right expectedPayload
where
testDevice = do
pkH <- unhex $ BC.pack "e84b5a6d2717c1003a13b431570353dbaca9146cf150c5f8575680feba52027a"