aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorBaylac-Jacqué Félix <felix@alternativebit.fr>2017-09-13 17:25:27 +0200
committerBaylac-Jacqué Félix <felix@alternativebit.fr>2017-09-16 17:11:27 +0200
commit544fb266e00229f9960b13dda864408a3500dd27 (patch)
tree544aa70c64ae6ad20d3509fa857f195324a629c2
parentImplemented RPC set's payload parser. (diff)
downloadwireguard-hs-544fb266e00229f9960b13dda864408a3500dd27.tar.xz
wireguard-hs-544fb266e00229f9960b13dda864408a3500dd27.zip
Implemented rpc request parser.
-rw-r--r--src/Network/WireGuard/Internal/Data/RpcTypes.hs4
-rw-r--r--src/Network/WireGuard/Internal/RpcParsers.hs12
-rw-r--r--tests/spec/Network/WireGuard/RPCSpec.hs20
3 files changed, 26 insertions, 10 deletions
diff --git a/src/Network/WireGuard/Internal/Data/RpcTypes.hs b/src/Network/WireGuard/Internal/Data/RpcTypes.hs
index a78d9df..088bbb5 100644
--- a/src/Network/WireGuard/Internal/Data/RpcTypes.hs
+++ b/src/Network/WireGuard/Internal/Data/RpcTypes.hs
@@ -17,7 +17,7 @@ import Network.WireGuard.Internal.Data.Types (PublicKey, KeyPair,
-- | Kind of client operation.
--
-- See <https://www.wireguard.com/xplatform/#configuration-protocol> for more informations.
-data OpType = Get | Set
+data OpType = Get | Set deriving (Eq, Show)
-- | Request wrapper. The payload is set only for Set operations.
--
@@ -25,7 +25,7 @@ data OpType = Get | Set
data RpcRequest = RpcRequest {
opType :: !OpType,
payload :: !(Maybe RpcSetPayload)
-}
+} deriving (Eq, Show)
-- | Payload sent together with a set RPC operation.
data RpcSetPayload = RpcSetPayload {
diff --git a/src/Network/WireGuard/Internal/RpcParsers.hs b/src/Network/WireGuard/Internal/RpcParsers.hs
index e5e38d1..d2eac92 100644
--- a/src/Network/WireGuard/Internal/RpcParsers.hs
+++ b/src/Network/WireGuard/Internal/RpcParsers.hs
@@ -38,15 +38,15 @@ import Network.WireGuard.Internal.Data.RpcTypes (OpType(..),
requestParser :: Parser RpcRequest
requestParser = do
op <- requestTypeParser
- let p = case op of
- Set -> undefined
- Get -> Nothing
- _ <- string $ BC.pack "\n\n"
+ p <- case op of
+ Set -> Just <$> setPayloadParser
+ Get -> return Nothing
+ _ <- string $ BC.pack "\n"
return $ RpcRequest op p
requestTypeParser :: Parser OpType
-requestTypeParser = "get=1" *> return Get
- <|> "set=1" *> return Set
+requestTypeParser = "get=1\n" *> return Get
+ <|> "set=1\n" *> return Set
setPayloadParser :: Parser RpcSetPayload
setPayloadParser = do
diff --git a/tests/spec/Network/WireGuard/RPCSpec.hs b/tests/spec/Network/WireGuard/RPCSpec.hs
index ec77662..e0c3e87 100644
--- a/tests/spec/Network/WireGuard/RPCSpec.hs
+++ b/tests/spec/Network/WireGuard/RPCSpec.hs
@@ -25,12 +25,13 @@ import Test.Hspec (Spec, describe,
import Network.WireGuard.RPC (serveConduit, showPeer)
import Network.WireGuard.Internal.RpcParsers (deviceParser, peerParser,
- setPayloadParser)
+ setPayloadParser, requestParser)
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(..),
- RpcSetPayload(..))
+ RpcSetPayload(..), RpcRequest(..),
+ OpType(..))
spec :: Spec
spec = do
@@ -174,6 +175,21 @@ spec = do
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
+ describe "requestParser" $ do
+ it "must correctly parse a set operation" $ 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 expectedResult = RPC.RpcRequest RPC.Set . Just $ RPC.RpcSetPayload expectedDevice [expectedPeer]
+ let result = feed (parse requestParser $ BC.pack "set=1\nprivate_key=e84b5a6d2717c1003a13b431570353dbaca9146cf150c5f8575680feba52027a\nlisten_port=777\nfwmark=0\npublic_key=662e14fd594556f522604703340351258903b64f35553763f19426ab2a515c58\nendpoint=192.168.1.1:1337\nallowed_ip=192.168.1.0/24\n\n") BC.empty
+ eitherResult result `shouldBe` Right expectedResult
+ it "must correctly parse a get operation" $ do
+ let expectedResult = RPC.RpcRequest RPC.Get Nothing
+ let result = feed (parse requestParser $ BC.pack "get=1\n\n") BC.empty
+ eitherResult result `shouldBe` Right expectedResult
where
testDevice = do
pkH <- unhex $ BC.pack "e84b5a6d2717c1003a13b431570353dbaca9146cf150c5f8575680feba52027a"