Skip to content

Commit 902e363

Browse files
committed
client service test WIP
1 parent e696510 commit 902e363

File tree

8 files changed

+42
-13
lines changed

8 files changed

+42
-13
lines changed

src/Simplex/Messaging/Agent/Store/AgentStore.hs

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -402,29 +402,31 @@ deleteUsersWithoutConns db = do
402402
pure userIds
403403

404404
createClientService :: DB.Connection -> UserId -> SMPServer -> (C.KeyHash, TLS.Credential) -> IO ()
405-
createClientService db userId srv (kh, (cert, pk)) =
405+
createClientService db userId srv (kh, (cert, pk)) = do
406+
serverKeyHash_ <- createServer_ db srv
406407
DB.execute
407408
db
408409
[sql|
409410
INSERT INTO client_services
410-
(user_id, host, port, service_cert_hash, service_cert, service_priv_key)
411-
VALUES (?,?,?,?,?,?)
412-
ON CONFLICT (user_id, host, port)
411+
(user_id, host, port, server_key_hash, service_cert_hash, service_cert, service_priv_key)
412+
VALUES (?,?,?,?,?,?,?)
413+
ON CONFLICT (user_id, host, port, server_key_hash)
413414
DO UPDATE SET
414415
service_cert_hash = EXCLUDED.service_cert_hash,
415416
service_cert = EXCLUDED.service_cert,
416417
service_priv_key = EXCLUDED.service_priv_key,
417-
rcv_service_id = NULL
418+
service_id = NULL
418419
|]
419-
(userId, host srv, port srv, kh, cert, pk)
420+
(userId, host srv, port srv, serverKeyHash_, kh, cert, pk)
420421

422+
-- TODO [certs rcv] get correct service based on key hash of the server
421423
getClientService :: DB.Connection -> UserId -> SMPServer -> IO (Maybe ((C.KeyHash, TLS.Credential), Maybe ServiceId))
422424
getClientService db userId srv =
423425
maybeFirstRow toService $
424426
DB.query
425427
db
426428
[sql|
427-
SELECT service_cert_hash, service_cert, service_priv_key, rcv_service_id
429+
SELECT service_cert_hash, service_cert, service_priv_key, service_id
428430
FROM client_services
429431
WHERE user_id = ? AND host = ? AND port = ?
430432
|]
@@ -438,7 +440,7 @@ getClientServiceServers db userId =
438440
<$> DB.query
439441
db
440442
[sql|
441-
SELECT c.host, c.port, s.key_hash, c.rcv_service_id, c.rcv_service_queue_count, c.rcv_service_queue_ids_hash
443+
SELECT c.host, c.port, s.key_hash, c.service_id, c.rcv_service_queue_count, c.rcv_service_queue_ids_hash
442444
FROM client_services c
443445
JOIN servers s ON s.host = c.host AND s.port = c.port
444446
|]
@@ -453,7 +455,7 @@ setClientServiceId db userId srv serviceId =
453455
db
454456
[sql|
455457
UPDATE client_services
456-
SET rcv_service_id = ?
458+
SET service_id = ?
457459
WHERE user_id = ? AND host = ? AND port = ?
458460
|]
459461
(serviceId, userId, host srv, port srv)

src/Simplex/Messaging/Agent/Store/Postgres/Migrations/M20251020_service_certs.hs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ CREATE TABLE client_services(
1414
user_id BIGINT NOT NULL REFERENCES users ON UPDATE RESTRICT ON DELETE CASCADE,
1515
host TEXT NOT NULL,
1616
port TEXT NOT NULL,
17+
server_key_hash BYTEA,
1718
service_cert BYTEA NOT NULL,
1819
service_cert_hash BYTEA NOT NULL,
1920
service_priv_key BYTEA NOT NULL,
@@ -23,7 +24,7 @@ CREATE TABLE client_services(
2324
FOREIGN KEY(host, port) REFERENCES servers ON UPDATE CASCADE ON DELETE RESTRICT
2425
);
2526

26-
CREATE UNIQUE INDEX idx_server_certs_user_id_host_port ON client_services(user_id, host, port);
27+
CREATE UNIQUE INDEX idx_server_certs_user_id_host_port ON client_services(user_id, host, port, server_key_hash);
2728
CREATE INDEX idx_server_certs_host_port ON client_services(host, port);
2829

2930
ALTER TABLE rcv_queues ADD COLUMN rcv_service_assoc SMALLINT NOT NULL DEFAULT 0;

src/Simplex/Messaging/Agent/Store/SQLite/Common.hs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
{-# LANGUAGE DuplicateRecordFields #-}
22
{-# LANGUAGE LambdaCase #-}
33
{-# LANGUAGE NamedFieldPuns #-}
4+
{-# LANGUAGE OverloadedStrings #-}
45
{-# LANGUAGE ScopedTypeVariables #-}
56

67
module Simplex.Messaging.Agent.Store.SQLite.Common
@@ -53,6 +54,12 @@ withConnectionPriority DBStore {dbSem, dbConnection} priority action
5354
| priority = E.bracket_ signal release $ withMVar dbConnection action
5455
| otherwise = lowPriority
5556
where
57+
-- To debug FK errors, set foreign_keys = OFF in Simplex.Messaging.Agent.Store.SQLite and use action' instead of action
58+
-- action' conn = do
59+
-- r <- action conn
60+
-- violations <- DB.query_ conn "PRAGMA foreign_key_check" :: IO [ (String, Int, String, Int)]
61+
-- unless (null violations) $ print violations
62+
-- pure r
5663
lowPriority = wait >> withMVar dbConnection (\db -> ifM free (Just <$> action db) (pure Nothing)) >>= maybe lowPriority pure
5764
signal = atomically $ modifyTVar' dbSem (+ 1)
5865
release = atomically $ modifyTVar' dbSem $ \sem -> if sem > 0 then sem - 1 else 0

src/Simplex/Messaging/Agent/Store/SQLite/Migrations/M20251020_service_certs.hs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ CREATE TABLE client_services(
1212
user_id INTEGER NOT NULL REFERENCES users ON DELETE CASCADE,
1313
host TEXT NOT NULL,
1414
port TEXT NOT NULL,
15+
server_key_hash BLOB,
1516
service_cert BLOB NOT NULL,
1617
service_cert_hash BLOB NOT NULL,
1718
service_priv_key BLOB NOT NULL,
@@ -21,7 +22,7 @@ CREATE TABLE client_services(
2122
FOREIGN KEY(host, port) REFERENCES servers ON UPDATE CASCADE ON DELETE RESTRICT
2223
);
2324

24-
CREATE UNIQUE INDEX idx_server_certs_user_id_host_port ON client_services(user_id, host, port);
25+
CREATE UNIQUE INDEX idx_server_certs_user_id_host_port ON client_services(user_id, host, port, server_key_hash);
2526
CREATE INDEX idx_server_certs_host_port ON client_services(host, port);
2627

2728
ALTER TABLE rcv_queues ADD COLUMN rcv_service_assoc INTEGER NOT NULL DEFAULT 0;

src/Simplex/Messaging/Server.hs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1807,7 +1807,7 @@ client
18071807
deliverServiceMessages expectedCnt = do
18081808
(qCnt, _msgCnt, _dupCnt, _errCnt) <- foldRcvServiceMessages ms serviceId deliverQueueMsg (0, 0, 0, 0)
18091809
atomically $ writeTBQueue msgQ [(NoCorrId, NoEntity, SALL)]
1810-
-- TODO [cert rcv] compare with expected
1810+
-- TODO [certs rcv] compare with expected
18111811
logNote $ "Service subscriptions for " <> tshow serviceId <> " (" <> tshow qCnt <> " queues)"
18121812
deliverQueueMsg :: (Int, Int, Int, Int) -> RecipientId -> Either ErrorType (Maybe (QueueRec, Message)) -> IO (Int, Int, Int, Int)
18131813
deliverQueueMsg (!qCnt, !msgCnt, !dupCnt, !errCnt) rId = \case

tests/AgentTests/FunctionalAPITests.hs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -476,6 +476,9 @@ functionalAPITests ps = do
476476
testUsersNoServer ps
477477
it "should connect two users and switch session mode" $
478478
withSmpServer ps testTwoUsers
479+
describe "Client service certificates" $ do
480+
fit "should connect, subscribe and reconnect as a service" $
481+
withSmpServer ps testClientServiceConnection
479482
describe "Connection switch" $ do
480483
describe "should switch delivery to the new queue" $
481484
testServerMatrix2 ps testSwitchConnection
@@ -2295,9 +2298,13 @@ makeConnectionForUsers = makeConnectionForUsers_ PQSupportOn True
22952298

22962299
makeConnectionForUsers_ :: HasCallStack => PQSupport -> SndQueueSecured -> AgentClient -> UserId -> AgentClient -> UserId -> ExceptT AgentErrorType IO (ConnId, ConnId)
22972300
makeConnectionForUsers_ pqSupport sqSecured alice aliceUserId bob bobUserId = do
2301+
liftIO $ print 1
22982302
(bobId, CCLink qInfo Nothing) <- A.createConnection alice NRMInteractive aliceUserId True True SCMInvitation Nothing Nothing (IKLinkPQ pqSupport) SMSubscribe
2303+
liftIO $ print 2
22992304
aliceId <- A.prepareConnectionToJoin bob bobUserId True qInfo pqSupport
2305+
liftIO $ print 3
23002306
sqSecured' <- A.joinConnection bob NRMInteractive bobUserId aliceId True qInfo "bob's connInfo" pqSupport SMSubscribe
2307+
liftIO $ print 4
23012308
liftIO $ sqSecured' `shouldBe` sqSecured
23022309
("", _, A.CONF confId pqSup' _ "bob's connInfo") <- get alice
23032310
liftIO $ pqSup' `shouldBe` pqSupport
@@ -3664,6 +3671,14 @@ testTwoUsers = withAgentClients2 $ \a b -> do
36643671
hasClients :: HasCallStack => AgentClient -> Int -> ExceptT AgentErrorType IO ()
36653672
hasClients c n = liftIO $ M.size <$> readTVarIO (smpClients c) `shouldReturn` n
36663673

3674+
testClientServiceConnection :: HasCallStack => IO ()
3675+
testClientServiceConnection =
3676+
withAgentClientsServers2 (agentCfg, initAgentServersClientService) (agentCfg, initAgentServers) $ \service user -> do
3677+
r <- runExceptT $ makeConnection service user
3678+
print r
3679+
Right (sId, uId) <- pure r
3680+
pure ()
3681+
36673682
getSMPAgentClient' :: Int -> AgentConfig -> InitialAgentServers -> String -> IO AgentClient
36683683
getSMPAgentClient' clientId cfg' initServers dbPath = do
36693684
Right st <- liftIO $ createStore dbPath

tests/SMPAgentClient.hs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,9 @@ initAgentServersProxy_ smpProxyMode smpProxyFallback =
8383
initAgentServersProxy2 :: InitialAgentServers
8484
initAgentServersProxy2 = initAgentServersProxy {smp = userServers [testSMPServer2]}
8585

86+
initAgentServersClientService :: InitialAgentServers
87+
initAgentServersClientService = initAgentServers {useServices = M.fromList [(1, True)]}
88+
8689
agentCfg :: AgentConfig
8790
agentCfg =
8891
defaultAgentConfig

tests/Test.hs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@ main = do
150150
before (pure $ ASType SQSPostgres SMSPostgres) smpProxyTests
151151
#endif
152152
-- xdescribe "SMP client agent, server jornal message store" $ agentTests (transport @TLS, ASType SQSMemory SMSJournal)
153-
describe "SMP client agent, server memory message store" $ agentTests (transport @TLS, ASType SQSMemory SMSMemory)
153+
xdescribe "SMP client agent, server memory message store" $ agentTests (transport @TLS, ASType SQSMemory SMSMemory)
154154
describe "SMP proxy, jornal message store" $
155155
before (pure $ ASType SQSMemory SMSJournal) smpProxyTests
156156
describe "XFTP" $ do

0 commit comments

Comments
 (0)