@@ -23,19 +23,20 @@ import Bilge.Assert
2323import Data.Aeson (object , toJSON , (.=) )
2424import Data.ByteString.Conversion
2525import Data.Default
26- import Data.Domain
2726import Data.Id
2827import Data.Json.Util
2928import qualified Data.Map as Map
3029import Data.Qualified
31- import qualified Data.Text as T
30+ import qualified Data.Text as Text
3231import Data.Timeout
3332import Imports
3433import System.FilePath
3534import System.Process
35+ import Test.Tasty.HUnit
3636import Util
3737import Wire.API.MLS.Credential
3838import Wire.API.MLS.KeyPackage
39+ import Wire.API.MLS.Serialisation
3940import Wire.API.User.Client
4041
4142data SetKey = SetKey | DontSetKey
@@ -49,6 +50,39 @@ data KeyingInfo = KeyingInfo
4950instance Default KeyingInfo where
5051 def = KeyingInfo SetKey Nothing
5152
53+ cliCmd :: FilePath -> ClientIdentity -> [String ]
54+ cliCmd tmp qcid =
55+ [" mls-test-cli" , " --store" , tmp </> (show qcid <> " .db" )]
56+
57+ initStore ::
58+ HasCallStack =>
59+ MonadIO m =>
60+ FilePath ->
61+ ClientIdentity ->
62+ m ()
63+ initStore tmp qcid = do
64+ let cmd0 = cliCmd tmp qcid
65+ void . liftIO . flip spawn Nothing . shell . unwords $
66+ cmd0 <> [" init" , show qcid]
67+
68+ generateKeyPackage ::
69+ HasCallStack =>
70+ MonadIO m =>
71+ FilePath ->
72+ ClientIdentity ->
73+ Maybe Timeout ->
74+ m (RawMLS KeyPackage , KeyPackageRef )
75+ generateKeyPackage tmp qcid lifetime = do
76+ let cmd0 = cliCmd tmp qcid
77+ kp <-
78+ liftIO $
79+ decodeMLSError <=< (flip spawn Nothing . shell . unwords ) $
80+ cmd0
81+ <> [" key-package" , " create" ]
82+ <> ((" --lifetime " <> ) . show . (#> Second ) <$> maybeToList lifetime)
83+ let ref = fromJust (kpRef' kp)
84+ pure (kp, ref)
85+
5286uploadKeyPackages ::
5387 HasCallStack =>
5488 Brig ->
@@ -59,20 +93,10 @@ uploadKeyPackages ::
5993 Int ->
6094 Http ()
6195uploadKeyPackages brig tmp KeyingInfo {.. } u c n = do
62- let cmd0 = [" mls-test-cli" , " --store" , tmp </> (clientId <> " .db" )]
63- clientId =
64- show (qUnqualified u)
65- <> " :"
66- <> T. unpack (client c)
67- <> " @"
68- <> T. unpack (domainText (qDomain u))
69- void . liftIO . flip spawn Nothing . shell . unwords $
70- cmd0 <> [" init" , clientId]
71- kps <-
72- replicateM n . liftIO . flip spawn Nothing . shell . unwords $
73- cmd0
74- <> [" key-package" , " create" ]
75- <> ((" --lifetime " <> ) . show . (#> Second ) <$> maybeToList kiLifetime)
96+ let cmd0 = cliCmd tmp cid
97+ cid = mkClientIdentity u c
98+ initStore tmp cid
99+ kps <- replicateM n (fst <$> generateKeyPackage tmp cid kiLifetime)
76100 when (kiSetKey == SetKey ) $
77101 do
78102 pk <-
@@ -85,7 +109,7 @@ uploadKeyPackages brig tmp KeyingInfo {..} u c n = do
85109 . json defUpdateClient {updateClientMLSPublicKeys = Map. fromList [(Ed25519 , pk)]}
86110 )
87111 !!! const 200 === statusCode
88- let upload = object [" key_packages" .= toJSON (map Base64ByteString kps)]
112+ let upload = object [" key_packages" .= toJSON (map ( Base64ByteString . rmRaw) kps)]
89113 post
90114 ( brig
91115 . paths [" mls" , " key-packages" , " self" , toByteString' c]
@@ -102,3 +126,8 @@ getKeyPackageCount brig u c =
102126 . zUser (qUnqualified u)
103127 )
104128 <!! const 200 === statusCode
129+
130+ decodeMLSError :: ParseMLS a => ByteString -> IO a
131+ decodeMLSError s = case decodeMLS' s of
132+ Left e -> assertFailure (" Could not parse MLS object: " <> Text. unpack e)
133+ Right x -> pure x
0 commit comments