diff --git a/migrations/20210101_initial.sql b/migrations/20210101_initial.sql index 361765d2a..50464f79d 100644 --- a/migrations/20210101_initial.sql +++ b/migrations/20210101_initial.sql @@ -8,7 +8,7 @@ CREATE TABLE servers ( CREATE TABLE connections ( conn_alias BLOB NOT NULL PRIMARY KEY, conn_mode TEXT NOT NULL, - last_internal_msg_id INTEGER NOT NULL DEFAULT -3, + last_internal_msg_id INTEGER NOT NULL DEFAULT 0, last_internal_rcv_msg_id INTEGER NOT NULL DEFAULT 0, last_internal_snd_msg_id INTEGER NOT NULL DEFAULT 0, last_external_snd_msg_id INTEGER NOT NULL DEFAULT 0, diff --git a/tests/AgentTests.hs b/tests/AgentTests.hs index e30ea7c1f..2b0547c7d 100644 --- a/tests/AgentTests.hs +++ b/tests/AgentTests.hs @@ -125,25 +125,26 @@ testDuplexConnection _ alice bob = do bob <# ("", "alice", INFO "alice's connInfo") bob <# ("", "alice", CON) alice <# ("", "bob", CON) - alice #: ("3", "bob", "SEND :hello") #> ("3", "bob", MID 1) - alice <# ("", "bob", SENT 1) + -- message IDs 1 to 3 get assigned to control messages, so first MSG is assigned ID 4 + alice #: ("3", "bob", "SEND :hello") #> ("3", "bob", MID 4) + alice <# ("", "bob", SENT 4) bob <#= \case ("", "alice", Msg "hello") -> True; _ -> False - bob #: ("12", "alice", "ACK 1") #> ("12", "alice", OK) - alice #: ("4", "bob", "SEND :how are you?") #> ("4", "bob", MID 2) - alice <# ("", "bob", SENT 2) + bob #: ("12", "alice", "ACK 4") #> ("12", "alice", OK) + alice #: ("4", "bob", "SEND :how are you?") #> ("4", "bob", MID 5) + alice <# ("", "bob", SENT 5) bob <#= \case ("", "alice", Msg "how are you?") -> True; _ -> False - bob #: ("13", "alice", "ACK 2") #> ("13", "alice", OK) - bob #: ("14", "alice", "SEND 9\nhello too") #> ("14", "alice", MID 3) - bob <# ("", "alice", SENT 3) + bob #: ("13", "alice", "ACK 5") #> ("13", "alice", OK) + bob #: ("14", "alice", "SEND 9\nhello too") #> ("14", "alice", MID 6) + bob <# ("", "alice", SENT 6) alice <#= \case ("", "bob", Msg "hello too") -> True; _ -> False - alice #: ("3a", "bob", "ACK 3") #> ("3a", "bob", OK) - bob #: ("15", "alice", "SEND 9\nmessage 1") #> ("15", "alice", MID 4) - bob <# ("", "alice", SENT 4) + alice #: ("3a", "bob", "ACK 6") #> ("3a", "bob", OK) + bob #: ("15", "alice", "SEND 9\nmessage 1") #> ("15", "alice", MID 7) + bob <# ("", "alice", SENT 7) alice <#= \case ("", "bob", Msg "message 1") -> True; _ -> False - alice #: ("4a", "bob", "ACK 4") #> ("4a", "bob", OK) + alice #: ("4a", "bob", "ACK 7") #> ("4a", "bob", OK) alice #: ("5", "bob", "OFF") #> ("5", "bob", OK) - bob #: ("17", "alice", "SEND 9\nmessage 3") #> ("17", "alice", MID 5) - bob <# ("", "alice", MERR 5 (SMP AUTH)) + bob #: ("17", "alice", "SEND 9\nmessage 3") #> ("17", "alice", MID 8) + bob <# ("", "alice", MERR 8 (SMP AUTH)) alice #: ("6", "bob", "DEL") #> ("6", "bob", OK) alice #:# "nothing else should be delivered to alice" @@ -158,25 +159,25 @@ testDuplexConnRandomIds _ alice bob = do bob <# ("", aliceConn, INFO "alice's connInfo") bob <# ("", aliceConn, CON) alice <# ("", bobConn, CON) - alice #: ("2", bobConn, "SEND :hello") #> ("2", bobConn, MID 1) - alice <# ("", bobConn, SENT 1) + alice #: ("2", bobConn, "SEND :hello") #> ("2", bobConn, MID 4) + alice <# ("", bobConn, SENT 4) bob <#= \case ("", c, Msg "hello") -> c == aliceConn; _ -> False - bob #: ("12", aliceConn, "ACK 1") #> ("12", aliceConn, OK) - alice #: ("3", bobConn, "SEND :how are you?") #> ("3", bobConn, MID 2) - alice <# ("", bobConn, SENT 2) + bob #: ("12", aliceConn, "ACK 4") #> ("12", aliceConn, OK) + alice #: ("3", bobConn, "SEND :how are you?") #> ("3", bobConn, MID 5) + alice <# ("", bobConn, SENT 5) bob <#= \case ("", c, Msg "how are you?") -> c == aliceConn; _ -> False - bob #: ("13", aliceConn, "ACK 2") #> ("13", aliceConn, OK) - bob #: ("14", aliceConn, "SEND 9\nhello too") #> ("14", aliceConn, MID 3) - bob <# ("", aliceConn, SENT 3) + bob #: ("13", aliceConn, "ACK 5") #> ("13", aliceConn, OK) + bob #: ("14", aliceConn, "SEND 9\nhello too") #> ("14", aliceConn, MID 6) + bob <# ("", aliceConn, SENT 6) alice <#= \case ("", c, Msg "hello too") -> c == bobConn; _ -> False - alice #: ("3a", bobConn, "ACK 3") #> ("3a", bobConn, OK) - bob #: ("15", aliceConn, "SEND 9\nmessage 1") #> ("15", aliceConn, MID 4) - bob <# ("", aliceConn, SENT 4) + alice #: ("3a", bobConn, "ACK 6") #> ("3a", bobConn, OK) + bob #: ("15", aliceConn, "SEND 9\nmessage 1") #> ("15", aliceConn, MID 7) + bob <# ("", aliceConn, SENT 7) alice <#= \case ("", c, Msg "message 1") -> c == bobConn; _ -> False - alice #: ("4a", bobConn, "ACK 4") #> ("4a", bobConn, OK) + alice #: ("4a", bobConn, "ACK 7") #> ("4a", bobConn, OK) alice #: ("5", bobConn, "OFF") #> ("5", bobConn, OK) - bob #: ("17", aliceConn, "SEND 9\nmessage 3") #> ("17", aliceConn, MID 5) - bob <# ("", aliceConn, MERR 5 (SMP AUTH)) + bob #: ("17", aliceConn, "SEND 9\nmessage 3") #> ("17", aliceConn, MID 8) + bob <# ("", aliceConn, MERR 8 (SMP AUTH)) alice #: ("6", bobConn, "DEL") #> ("6", bobConn, OK) alice #:# "nothing else should be delivered to alice" @@ -193,10 +194,10 @@ testContactConnection _ alice bob tom = do alice <# ("", "bob", INFO "bob's connInfo 2") alice <# ("", "bob", CON) bob <# ("", "alice", CON) - alice #: ("3", "bob", "SEND :hi") #> ("3", "bob", MID 1) - alice <# ("", "bob", SENT 1) + alice #: ("3", "bob", "SEND :hi") #> ("3", "bob", MID 4) + alice <# ("", "bob", SENT 4) bob <#= \case ("", "alice", Msg "hi") -> True; _ -> False - bob #: ("13", "alice", "ACK 1") #> ("13", "alice", OK) + bob #: ("13", "alice", "ACK 4") #> ("13", "alice", OK) tom #: ("21", "alice", "JOIN " <> cReq' <> " 14\ntom's connInfo") #> ("21", "alice", OK) ("", "alice_contact", Right (REQ aInvId' "tom's connInfo")) <- (alice <#:) @@ -206,10 +207,10 @@ testContactConnection _ alice bob tom = do alice <# ("", "tom", INFO "tom's connInfo 2") alice <# ("", "tom", CON) tom <# ("", "alice", CON) - alice #: ("5", "tom", "SEND :hi there") #> ("5", "tom", MID 1) - alice <# ("", "tom", SENT 1) + alice #: ("5", "tom", "SEND :hi there") #> ("5", "tom", MID 4) + alice <# ("", "tom", SENT 4) tom <#= \case ("", "alice", Msg "hi there") -> True; _ -> False - tom #: ("23", "alice", "ACK 1") #> ("23", "alice", OK) + tom #: ("23", "alice", "ACK 4") #> ("23", "alice", OK) testContactConnRandomIds :: Transport c => TProxy c -> c -> c -> IO () testContactConnRandomIds _ alice bob = do @@ -229,10 +230,10 @@ testContactConnRandomIds _ alice bob = do alice <# ("", bobConn, CON) bob <# ("", aliceConn, CON) - alice #: ("3", bobConn, "SEND :hi") #> ("3", bobConn, MID 1) - alice <# ("", bobConn, SENT 1) + alice #: ("3", bobConn, "SEND :hi") #> ("3", bobConn, MID 4) + alice <# ("", bobConn, SENT 4) bob <#= \case ("", c, Msg "hi") -> c == aliceConn; _ -> False - bob #: ("13", aliceConn, "ACK 1") #> ("13", aliceConn, OK) + bob #: ("13", aliceConn, "ACK 4") #> ("13", aliceConn, OK) testRejectContactRequest :: Transport c => TProxy c -> c -> c -> IO () testRejectContactRequest _ alice bob = do @@ -249,20 +250,20 @@ testRejectContactRequest _ alice bob = do testSubscription :: Transport c => TProxy c -> c -> c -> c -> IO () testSubscription _ alice1 alice2 bob = do (alice1, "alice") `connect` (bob, "bob") - bob #: ("12", "alice", "SEND 5\nhello") #> ("12", "alice", MID 1) - bob <# ("", "alice", SENT 1) + bob #: ("12", "alice", "SEND 5\nhello") #> ("12", "alice", MID 4) + bob <# ("", "alice", SENT 4) alice1 <#= \case ("", "bob", Msg "hello") -> True; _ -> False - alice1 #: ("1", "bob", "ACK 1") #> ("1", "bob", OK) - bob #: ("13", "alice", "SEND 11\nhello again") #> ("13", "alice", MID 2) - bob <# ("", "alice", SENT 2) + alice1 #: ("1", "bob", "ACK 4") #> ("1", "bob", OK) + bob #: ("13", "alice", "SEND 11\nhello again") #> ("13", "alice", MID 5) + bob <# ("", "alice", SENT 5) alice1 <#= \case ("", "bob", Msg "hello again") -> True; _ -> False - alice1 #: ("2", "bob", "ACK 2") #> ("2", "bob", OK) + alice1 #: ("2", "bob", "ACK 5") #> ("2", "bob", OK) alice2 #: ("21", "bob", "SUB") #> ("21", "bob", OK) alice1 <# ("", "bob", END) - bob #: ("14", "alice", "SEND 2\nhi") #> ("14", "alice", MID 3) - bob <# ("", "alice", SENT 3) + bob #: ("14", "alice", "SEND 2\nhi") #> ("14", "alice", MID 6) + bob <# ("", "alice", SENT 6) alice2 <#= \case ("", "bob", Msg "hi") -> True; _ -> False - alice2 #: ("22", "bob", "ACK 3") #> ("22", "bob", OK) + alice2 #: ("22", "bob", "ACK 6") #> ("22", "bob", OK) alice1 #:# "nothing else should be delivered to alice1" testSubscrNotification :: Transport c => TProxy c -> (ThreadId, ThreadId) -> c -> IO () @@ -278,22 +279,22 @@ testMsgDeliveryServerRestart :: Transport c => TProxy c -> c -> c -> IO () testMsgDeliveryServerRestart t alice bob = do withServer $ do connect (alice, "alice") (bob, "bob") - bob #: ("1", "alice", "SEND 2\nhi") #> ("1", "alice", MID 1) - bob <# ("", "alice", SENT 1) + bob #: ("1", "alice", "SEND 2\nhi") #> ("1", "alice", MID 4) + bob <# ("", "alice", SENT 4) alice <#= \case ("", "bob", Msg "hi") -> True; _ -> False - alice #: ("11", "bob", "ACK 1") #> ("11", "bob", OK) + alice #: ("11", "bob", "ACK 4") #> ("11", "bob", OK) alice #:# "nothing else delivered before the server is killed" alice <# ("", "bob", DOWN) - bob #: ("2", "alice", "SEND 11\nhello again") #> ("2", "alice", MID 2) + bob #: ("2", "alice", "SEND 11\nhello again") #> ("2", "alice", MID 5) bob #:# "nothing else delivered before the server is restarted" alice #:# "nothing else delivered before the server is restarted" withServer $ do - bob <# ("", "alice", SENT 2) + bob <# ("", "alice", SENT 5) alice <# ("", "bob", UP) alice <#= \case ("", "bob", Msg "hello again") -> True; _ -> False - alice #: ("12", "bob", "ACK 2") #> ("12", "bob", OK) + alice #: ("12", "bob", "ACK 5") #> ("12", "bob", OK) removeFile testStoreLogFile where @@ -304,14 +305,14 @@ testMsgDeliveryAgentRestart t bob = do withAgent $ \alice -> do withServer $ do connect (bob, "bob") (alice, "alice") - alice #: ("1", "bob", "SEND 5\nhello") #> ("1", "bob", MID 1) - alice <# ("", "bob", SENT 1) + alice #: ("1", "bob", "SEND 5\nhello") #> ("1", "bob", MID 4) + alice <# ("", "bob", SENT 4) bob <#= \case ("", "alice", Msg "hello") -> True; _ -> False - bob #: ("11", "alice", "ACK 1") #> ("11", "alice", OK) + bob #: ("11", "alice", "ACK 4") #> ("11", "alice", OK) bob #:# "nothing else delivered before the server is down" bob <# ("", "alice", DOWN) - alice #: ("2", "bob", "SEND 11\nhello again") #> ("2", "bob", MID 2) + alice #: ("2", "bob", "SEND 11\nhello again") #> ("2", "bob", MID 5) alice #:# "nothing else delivered before the server is restarted" bob #:# "nothing else delivered before the server is restarted" @@ -321,11 +322,11 @@ testMsgDeliveryAgentRestart t bob = do alice <#= \case (corrId, "bob", cmd) -> (corrId == "3" && cmd == OK) - || (corrId == "" && cmd == SENT 2) + || (corrId == "" && cmd == SENT 5) _ -> False bob <# ("", "alice", UP) bob <#= \case ("", "alice", Msg "hello again") -> True; _ -> False - bob #: ("12", "alice", "ACK 2") #> ("12", "alice", OK) + bob #: ("12", "alice", "ACK 5") #> ("12", "alice", OK) removeFile testStoreLogFile removeFile testDB @@ -353,11 +354,11 @@ testConcurrentMsgDelivery _ alice bob = do -- alice <# ("", "bob", SENT 1) -- bob <#= \case ("", "alice", Msg "hello") -> True; _ -> False -- bob #: ("12", "alice", "ACK 1") #> ("12", "alice", OK) - bob #: ("14", "alice", "SEND 9\nhello too") #> ("14", "alice", MID 2) - bob <# ("", "alice", SENT 2) + bob #: ("14", "alice", "SEND 9\nhello too") #> ("14", "alice", MID 5) + bob <# ("", "alice", SENT 5) -- if delivery is blocked it won't go further alice <#= \case ("", "bob", Msg "hello too") -> True; _ -> False - alice #: ("3", "bob", "ACK 2") #> ("3", "bob", OK) + alice #: ("3", "bob", "ACK 5") #> ("3", "bob", OK) testMsgDeliveryQuotaExceeded :: Transport c => TProxy c -> c -> c -> IO () testMsgDeliveryQuotaExceeded _ alice bob = do @@ -370,9 +371,9 @@ testMsgDeliveryQuotaExceeded _ alice bob = do alice <#= \case ("", "bob", SENT m) -> m == mId; _ -> False (_, "bob", Right (MID _)) <- alice #: ("5", "bob", "SEND :over quota") - alice #: ("1", "bob2", "SEND :hello") #> ("1", "bob2", MID 1) + alice #: ("1", "bob2", "SEND :hello") #> ("1", "bob2", MID 4) -- if delivery is blocked it won't go further - alice <# ("", "bob2", SENT 1) + alice <# ("", "bob2", SENT 4) connect :: forall c. Transport c => (c, ByteString) -> (c, ByteString) -> IO () connect (h1, name1) (h2, name2) = do diff --git a/tests/AgentTests/FunctionalAPITests.hs b/tests/AgentTests/FunctionalAPITests.hs index 8053dd90b..32bb6056c 100644 --- a/tests/AgentTests/FunctionalAPITests.hs +++ b/tests/AgentTests/FunctionalAPITests.hs @@ -57,25 +57,26 @@ testAgentClient = do get alice ##> ("", bobId, CON) get bob ##> ("", aliceId, INFO "alice's connInfo") get bob ##> ("", aliceId, CON) - 1 <- sendMessage alice bobId "hello" - get alice ##> ("", bobId, SENT 1) - 2 <- sendMessage alice bobId "how are you?" - get alice ##> ("", bobId, SENT 2) + -- message IDs 1 to 3 get assigned to control messages, so first MSG is assigned ID 4 + 4 <- sendMessage alice bobId "hello" + get alice ##> ("", bobId, SENT 4) + 5 <- sendMessage alice bobId "how are you?" + get alice ##> ("", bobId, SENT 5) get bob =##> \case ("", c, Msg "hello") -> c == aliceId; _ -> False - ackMessage bob aliceId 1 + ackMessage bob aliceId 4 get bob =##> \case ("", c, Msg "how are you?") -> c == aliceId; _ -> False - ackMessage bob aliceId 2 - 3 <- sendMessage bob aliceId "hello too" - get bob ##> ("", aliceId, SENT 3) - 4 <- sendMessage bob aliceId "message 1" - get bob ##> ("", aliceId, SENT 4) + ackMessage bob aliceId 5 + 6 <- sendMessage bob aliceId "hello too" + get bob ##> ("", aliceId, SENT 6) + 7 <- sendMessage bob aliceId "message 1" + get bob ##> ("", aliceId, SENT 7) get alice =##> \case ("", c, Msg "hello too") -> c == bobId; _ -> False - ackMessage alice bobId 3 + ackMessage alice bobId 6 get alice =##> \case ("", c, Msg "message 1") -> c == bobId; _ -> False - ackMessage alice bobId 4 + ackMessage alice bobId 7 suspendConnection alice bobId - 5 <- sendMessage bob aliceId "message 2" - get bob ##> ("", aliceId, MERR 5 (SMP AUTH)) + 8 <- sendMessage bob aliceId "message 2" + get bob ##> ("", aliceId, MERR 8 (SMP AUTH)) deleteConnection alice bobId liftIO $ noMessages alice "nothing else should be delivered to alice" pure () @@ -147,11 +148,11 @@ testAsyncBothOffline = do exchangeGreetings :: AgentClient -> ConnId -> AgentClient -> ConnId -> ExceptT AgentErrorType IO () exchangeGreetings alice bobId bob aliceId = do - 1 <- sendMessage alice bobId "hello" - get alice ##> ("", bobId, SENT 1) + 4 <- sendMessage alice bobId "hello" + get alice ##> ("", bobId, SENT 4) get bob =##> \case ("", c, Msg "hello") -> c == aliceId; _ -> False - ackMessage bob aliceId 1 - 2 <- sendMessage bob aliceId "hello too" - get bob ##> ("", aliceId, SENT 2) + ackMessage bob aliceId 4 + 5 <- sendMessage bob aliceId "hello too" + get bob ##> ("", aliceId, SENT 5) get alice =##> \case ("", c, Msg "hello too") -> c == bobId; _ -> False - ackMessage alice bobId 2 + ackMessage alice bobId 5 diff --git a/tests/AgentTests/SQLiteTests.hs b/tests/AgentTests/SQLiteTests.hs index 77a5f724b..a98d4c804 100644 --- a/tests/AgentTests/SQLiteTests.hs +++ b/tests/AgentTests/SQLiteTests.hs @@ -412,9 +412,8 @@ testCreateRcvMsg = g <- newTVarIO =<< drgNew let ConnData {connId} = cData1 _ <- runExceptT $ createRcvConn st g cData1 rcvQueue1 SCMInvitation - -- TODO getMsg to check message - testCreateRcvMsg_ st 0 "" connId $ mkRcvMsgData (InternalId $ -2) (InternalRcvId 1) 1 "1" "hash_dummy" - testCreateRcvMsg_ st 1 "hash_dummy" connId $ mkRcvMsgData (InternalId $ -1) (InternalRcvId 2) 2 "2" "new_hash_dummy" + testCreateRcvMsg_ st 0 "" connId $ mkRcvMsgData (InternalId 1) (InternalRcvId 1) 1 "1" "hash_dummy" + testCreateRcvMsg_ st 1 "hash_dummy" connId $ mkRcvMsgData (InternalId 2) (InternalRcvId 2) 2 "2" "new_hash_dummy" mkSndMsgData :: InternalId -> InternalSndId -> MsgHash -> SndMsgData mkSndMsgData internalId internalSndId internalHash = @@ -441,9 +440,8 @@ testCreateSndMsg = g <- newTVarIO =<< drgNew let ConnData {connId} = cData1 _ <- runExceptT $ createSndConn store g cData1 sndQueue1 - -- TODO getMsg to check message - testCreateSndMsg_ store "" connId $ mkSndMsgData (InternalId $ -2) (InternalSndId 1) "hash_dummy" - testCreateSndMsg_ store "hash_dummy" connId $ mkSndMsgData (InternalId $ -1) (InternalSndId 2) "new_hash_dummy" + testCreateSndMsg_ store "" connId $ mkSndMsgData (InternalId 1) (InternalSndId 1) "hash_dummy" + testCreateSndMsg_ store "hash_dummy" connId $ mkSndMsgData (InternalId 2) (InternalSndId 2) "new_hash_dummy" testCreateRcvAndSndMsgs :: SpecWith SQLiteStore testCreateRcvAndSndMsgs = @@ -452,9 +450,9 @@ testCreateRcvAndSndMsgs = let ConnData {connId} = cData1 _ <- runExceptT $ createRcvConn store g cData1 rcvQueue1 SCMInvitation _ <- runExceptT $ upgradeRcvConnToDuplex store "conn1" sndQueue1 - testCreateRcvMsg_ store 0 "" connId $ mkRcvMsgData (InternalId $ -2) (InternalRcvId 1) 1 "1" "rcv_hash_1" - testCreateRcvMsg_ store 1 "rcv_hash_1" connId $ mkRcvMsgData (InternalId $ -1) (InternalRcvId 2) 2 "2" "rcv_hash_2" - testCreateSndMsg_ store "" connId $ mkSndMsgData (InternalId 0) (InternalSndId 1) "snd_hash_1" - testCreateRcvMsg_ store 2 "rcv_hash_2" connId $ mkRcvMsgData (InternalId 1) (InternalRcvId 3) 3 "3" "rcv_hash_3" - testCreateSndMsg_ store "snd_hash_1" connId $ mkSndMsgData (InternalId 2) (InternalSndId 2) "snd_hash_2" - testCreateSndMsg_ store "snd_hash_2" connId $ mkSndMsgData (InternalId 3) (InternalSndId 3) "snd_hash_3" + testCreateRcvMsg_ store 0 "" connId $ mkRcvMsgData (InternalId 1) (InternalRcvId 1) 1 "1" "rcv_hash_1" + testCreateRcvMsg_ store 1 "rcv_hash_1" connId $ mkRcvMsgData (InternalId 2) (InternalRcvId 2) 2 "2" "rcv_hash_2" + testCreateSndMsg_ store "" connId $ mkSndMsgData (InternalId 3) (InternalSndId 1) "snd_hash_1" + testCreateRcvMsg_ store 2 "rcv_hash_2" connId $ mkRcvMsgData (InternalId 4) (InternalRcvId 3) 3 "3" "rcv_hash_3" + testCreateSndMsg_ store "snd_hash_1" connId $ mkSndMsgData (InternalId 5) (InternalSndId 2) "snd_hash_2" + testCreateSndMsg_ store "snd_hash_2" connId $ mkSndMsgData (InternalId 6) (InternalSndId 3) "snd_hash_3"