add tests for establishing connection asynchronously; retrieve correct key for activation on restart (#169)

This commit is contained in:
Efim Poberezkin
2021-07-10 20:02:48 +10:00
committed by GitHub
parent 8ba3e3e45a
commit 5e380e1d47
4 changed files with 166 additions and 60 deletions
+11 -9
View File
@@ -38,6 +38,7 @@ module Simplex.Messaging.Agent
AgentMonad,
AgentErrorMonad,
getSMPAgentClient,
disconnectAgentClient, -- used in tests
createConnection,
joinConnection,
allowConnection,
@@ -359,27 +360,28 @@ subscribeConnection' :: forall m. AgentMonad m => AgentClient -> ConnId -> m ()
subscribeConnection' c connId =
withStore (`getConn` connId) >>= \case
SomeConn _ (DuplexConnection _ rq sq) -> case status (sq :: SndQueue) of
Confirmed -> withVerifyKey sq $ \sndKey -> do
secureQueue c rq sndKey
Confirmed -> withVerifyKey sq $ \verifyKey -> do
conf <- withStore (`getAcceptedConfirmation` connId)
secureQueue c rq $ senderKey (conf :: AcceptedConfirmation)
withStore $ \st -> setRcvQueueStatus st rq Secured
activateSecuredQueue rq sq sndKey
activateSecuredQueue rq sq verifyKey
Secured -> withVerifyKey sq $ activateSecuredQueue rq sq
Active -> subscribeQueue c rq connId
_ -> throwError $ INTERNAL "unexpected queue status"
SomeConn _ (SndConnection _ sq) -> case status (sq :: SndQueue) of
Confirmed -> withVerifyKey sq $ \sndKey ->
activateQueueJoining c connId sq sndKey resumeInterval
Confirmed -> withVerifyKey sq $ \verifyKey ->
activateQueueJoining c connId sq verifyKey resumeInterval
Active -> throwError $ CONN SIMPLEX
_ -> throwError $ INTERNAL "unexpected queue status"
SomeConn _ (RcvConnection _ rq) -> subscribeQueue c rq connId
where
withVerifyKey :: SndQueue -> (C.PublicKey -> m ()) -> m ()
withVerifyKey sq action =
let err = throwError $ INTERNAL "missing send queue public key"
in maybe err action . C.publicKey $ sndPrivateKey sq
let err = throwError $ INTERNAL "missing signing key public counterpart"
in maybe err action . C.publicKey $ signKey sq
activateSecuredQueue :: RcvQueue -> SndQueue -> C.PublicKey -> m ()
activateSecuredQueue rq sq sndKey = do
activateQueueInitiating c connId sq sndKey resumeInterval
activateSecuredQueue rq sq verifyKey = do
activateQueueInitiating c connId sq verifyKey resumeInterval
subscribeQueue c rq connId
-- | Send message to the connection (SEND command) in Reader monad