From f8302e203052a4922aa8de873dbf2b71235cb717 Mon Sep 17 00:00:00 2001 From: Evgeny Poberezkin <2769109+epoberezkin@users.noreply.github.com> Date: Tue, 15 Nov 2022 18:31:29 +0000 Subject: [PATCH] core: SMP server connection test (#1367) * core: SMP server connection test * fix test * update simplexmq --- cabal.project | 2 +- scripts/nix/sha256map.nix | 2 +- src/Simplex/Chat.hs | 5 +++++ src/Simplex/Chat/Controller.hs | 4 +++- src/Simplex/Chat/Help.hs | 2 +- src/Simplex/Chat/View.hs | 19 ++++++++++++++++--- stack.yaml | 2 +- tests/ChatTests.hs | 15 ++++++++++++++- 8 files changed, 42 insertions(+), 9 deletions(-) diff --git a/cabal.project b/cabal.project index 6afa53f021..973338d69e 100644 --- a/cabal.project +++ b/cabal.project @@ -7,7 +7,7 @@ constraints: zip +disable-bzip2 +disable-zstd source-repository-package type: git location: https://github.com/simplex-chat/simplexmq.git - tag: 95db734b2d89bdf35e413f0abd4eac4ed3c64fc3 + tag: f3b6ed4db024c946e6e5d119a07386d3746ec225 source-repository-package type: git diff --git a/scripts/nix/sha256map.nix b/scripts/nix/sha256map.nix index bede7681b5..ba6dd1f7ab 100644 --- a/scripts/nix/sha256map.nix +++ b/scripts/nix/sha256map.nix @@ -1,5 +1,5 @@ { - "https://github.com/simplex-chat/simplexmq.git"."95db734b2d89bdf35e413f0abd4eac4ed3c64fc3" = "139ixj08y2x4vyf9qj1d9sz35hqxpaiyy9wi5skkncxgzgcm1ksa"; + "https://github.com/simplex-chat/simplexmq.git"."f3b6ed4db024c946e6e5d119a07386d3746ec225" = "0hsdrk80vy65dwf7gzmlkx48dy592qfm8d8sl7fpy9nr9y0rl8q1"; "https://github.com/simplex-chat/direct-sqlcipher.git"."34309410eb2069b029b8fc1872deb1e0db123294" = "0kwkmhyfsn2lixdlgl15smgr1h5gjk7fky6abzh8rng2h5ymnffd"; "https://github.com/simplex-chat/sqlcipher-simple.git"."5e154a2aeccc33ead6c243ec07195ab673137221" = "1d1gc5wax4vqg0801ajsmx1sbwvd9y7p7b8mmskvqsmpbwgbh0m0"; "https://github.com/simplex-chat/aeson.git"."3eb66f9a68f103b5f1489382aad89f5712a64db7" = "0kilkx59fl6c3qy3kjczqvm8c3f4n3p0bdk9biyflf51ljnzp4yp"; diff --git a/src/Simplex/Chat.hs b/src/Simplex/Chat.hs index 01949c8e7f..20a316a377 100644 --- a/src/Simplex/Chat.hs +++ b/src/Simplex/Chat.hs @@ -675,6 +675,7 @@ processChatCommand = \case ChatConfig {defaultServers = InitialAgentServers {smp = defaultSMPServers}} <- asks config withAgent $ \a -> setSMPServers a (fromMaybe defaultSMPServers (nonEmpty smpServers)) pure CRCmdOk + TestSMPServer smpServer -> CRSMPTestResult <$> withAgent (`testSMPServerConnection` smpServer) APISetChatItemTTL newTTL_ -> withUser' $ \user -> checkStoreNotChanged $ withChatLock "setChatItemTTL" $ do @@ -3203,6 +3204,10 @@ chatCommandP = "/smp_servers default" $> SetUserSMPServers [], "/smp_servers " *> (SetUserSMPServers <$> smpServersP), "/smp_servers" $> GetUserSMPServers, + "/smp default" $> SetUserSMPServers [], + "/smp test " *> (TestSMPServer <$> strP), + "/smp " *> (SetUserSMPServers <$> smpServersP), + "/smp" $> GetUserSMPServers, "/_ttl " *> (APISetChatItemTTL <$> ciTTLDecimal), "/ttl " *> (APISetChatItemTTL <$> ciTTL), "/ttl" $> APIGetChatItemTTL, diff --git a/src/Simplex/Chat/Controller.hs b/src/Simplex/Chat/Controller.hs index c5d3a090d7..9ca6e297d7 100644 --- a/src/Simplex/Chat/Controller.hs +++ b/src/Simplex/Chat/Controller.hs @@ -39,7 +39,7 @@ import Simplex.Chat.Protocol import Simplex.Chat.Store (AutoAccept, StoreError, UserContactLink) import Simplex.Chat.Types import Simplex.Messaging.Agent (AgentClient) -import Simplex.Messaging.Agent.Client (AgentLocks) +import Simplex.Messaging.Agent.Client (AgentLocks, SMPTestFailure) import Simplex.Messaging.Agent.Env.SQLite (AgentConfig, InitialAgentServers, NetworkConfig) import Simplex.Messaging.Agent.Lock import Simplex.Messaging.Agent.Protocol @@ -187,6 +187,7 @@ data ChatCommand | APIGetGroupLink GroupId | GetUserSMPServers | SetUserSMPServers [SMPServerWithAuth] + | TestSMPServer SMPServerWithAuth | APISetChatItemTTL (Maybe Int64) | APIGetChatItemTTL | APISetNetworkConfig NetworkConfig @@ -262,6 +263,7 @@ data ChatResponse | CRLastMessages {chatItems :: [AChatItem]} | CRApiParsedMarkdown {formattedText :: Maybe MarkdownList} | CRUserSMPServers {smpServers :: [SMPServerWithAuth]} + | CRSMPTestResult {smpTestFailure :: Maybe SMPTestFailure} | CRChatItemTTL {chatItemTTL :: Maybe Int64} | CRNetworkConfig {networkConfig :: NetworkConfig} | CRContactInfo {contact :: Contact, connectionStats :: ConnectionStats, customUserProfile :: Maybe Profile} diff --git a/src/Simplex/Chat/Help.hs b/src/Simplex/Chat/Help.hs index d157f0dfea..f60b3135bd 100644 --- a/src/Simplex/Chat/Help.hs +++ b/src/Simplex/Chat/Help.hs @@ -199,7 +199,7 @@ settingsInfo = styleMarkdown [ green "Chat settings:", indent <> highlight "/network " <> " - show / set network access options", - indent <> highlight "/smp_servers " <> " - show / set custom SMP servers", + indent <> highlight "/smp " <> " - show / set custom SMP servers", indent <> highlight "/info " <> " - information about contact connection", indent <> highlight "/info # " <> " - information about member connection", indent <> highlight "/(un)mute " <> " - (un)mute contact, the last messages can be printed with /tail command", diff --git a/src/Simplex/Chat/View.hs b/src/Simplex/Chat/View.hs index 8791a75af9..eb2ad3a122 100644 --- a/src/Simplex/Chat/View.hs +++ b/src/Simplex/Chat/View.hs @@ -37,6 +37,7 @@ import Simplex.Chat.Protocol import Simplex.Chat.Store (AutoAccept (..), StoreError (..), UserContactLink (..)) import Simplex.Chat.Styled import Simplex.Chat.Types +import Simplex.Messaging.Agent.Client (SMPTestFailure (..), SMPTestStep (..)) import Simplex.Messaging.Agent.Env.SQLite (NetworkConfig (..)) import Simplex.Messaging.Agent.Protocol import qualified Simplex.Messaging.Crypto as C @@ -65,6 +66,7 @@ responseToView user_ testView ts = \case CRApiChat chat -> if testView then testViewChat chat else [plain . bshow $ J.encode chat] CRApiParsedMarkdown ft -> [plain . bshow $ J.encode ft] CRUserSMPServers smpServers -> viewSMPServers smpServers testView + CRSMPTestResult testFailure -> viewSMPTestResult testFailure CRChatItemTTL ttl -> viewChatItemTTL ttl CRNetworkConfig cfg -> viewNetworkConfig cfg CRContactInfo ct cStats customUserProfile -> viewContactInfo ct cStats customUserProfile @@ -627,8 +629,9 @@ viewSMPServers smpServers testView = else [ customSMPServers, "", - "use " <> highlight' "/smp_servers " <> " to switch to custom SMP servers", - "use " <> highlight' "/smp_servers default" <> " to remove custom SMP servers and use default", + "use " <> highlight' "/smp test " <> " to test SMP server connection", + "use " <> highlight' "/smp set " <> " to switch to custom SMP servers", + "use " <> highlight' "/smp default" <> " to remove custom SMP servers and use default", "(chat option " <> highlight' "-s" <> " (" <> highlight' "--server" <> ") has precedence over saved SMP servers for chat session)" ] where @@ -637,6 +640,16 @@ viewSMPServers smpServers testView = then "no custom SMP servers saved" else viewServers smpServers +viewSMPTestResult :: Maybe SMPTestFailure -> [StyledString] +viewSMPTestResult = \case + Just SMPTestFailure {testStep, testError} -> + result + <> ["Server requires authentication to create queues, check password" | testStep == TSCreateQueue && testError == SMP SMP.AUTH] + <> ["Possibly, certificate fingerprint in server address is incorrect" | testStep == TSConnect && testError == BROKER NETWORK] + where + result = ["SMP server test failed at " <> plain (drop 2 $ show testStep) <> ", error: " <> plain (strEncode testError)] + _ -> ["SMP server test passed"] + viewChatItemTTL :: Maybe Int64 -> [StyledString] viewChatItemTTL = \case Nothing -> ["old messages are not being deleted"] @@ -652,7 +665,7 @@ viewNetworkConfig :: NetworkConfig -> [StyledString] viewNetworkConfig NetworkConfig {socksProxy, tcpTimeout} = [ plain $ maybe "direct network connection" (("using SOCKS5 proxy " <>) . show) socksProxy, "TCP timeout: " <> sShow tcpTimeout, - "use `/network socks=[ timeout=]` to change settings" + "use " <> highlight' "/network socks=[ timeout=]" <> " to change settings" ] viewContactInfo :: Contact -> ConnectionStats -> Maybe Profile -> [StyledString] diff --git a/stack.yaml b/stack.yaml index 2d363c7420..7f8d1cad6c 100644 --- a/stack.yaml +++ b/stack.yaml @@ -49,7 +49,7 @@ extra-deps: # - simplexmq-1.0.0@sha256:34b2004728ae396e3ae449cd090ba7410781e2b3cefc59259915f4ca5daa9ea8,8561 # - ../simplexmq - github: simplex-chat/simplexmq - commit: 95db734b2d89bdf35e413f0abd4eac4ed3c64fc3 + commit: f3b6ed4db024c946e6e5d119a07386d3746ec225 # - ../direct-sqlcipher - github: simplex-chat/direct-sqlcipher commit: 34309410eb2069b029b8fc1872deb1e0db123294 diff --git a/tests/ChatTests.hs b/tests/ChatTests.hs index a41fd21f76..1391e103f4 100644 --- a/tests/ChatTests.hs +++ b/tests/ChatTests.hs @@ -116,8 +116,9 @@ chatTests = do it "set contact alias" testSetAlias it "set connection alias" testSetConnectionAlias it "set contact prefs" testSetContactPrefs - describe "SMP servers" $ + describe "SMP servers" $ do it "get and set SMP servers" testGetSetSMPServers + it "test SMP server connection" testTestSMPServerConnection describe "async connection handshake" $ do it "connect when initiating client goes offline" testAsyncInitiatingOffline it "connect when accepting client goes offline" testAsyncAcceptingOffline @@ -2876,6 +2877,18 @@ testGetSetSMPServers = alice #$> ("/smp_servers default", id, "ok") alice #$> ("/smp_servers", id, "no custom SMP servers saved") +testTestSMPServerConnection :: IO () +testTestSMPServerConnection = + testChat2 aliceProfile bobProfile $ + \alice _ -> do + alice ##> "/smp test smp://LcJUMfVhwD8yxjAiSaDzzGF3-kLG4Uh0Fl_ZIjrRwjI=@localhost:5001" + alice <## "SMP server test passed" + alice ##> "/smp test smp://LcJUMfVhwD8yxjAiSaDzzGF3-kLG4Uh0Fl_ZIjrRwjI=:server_password@localhost:5001" + alice <## "SMP server test passed" + alice ##> "/smp test smp://LcJUMfVhwD8yxjAiSaDzzGF3-kLG4Uh0Fl_ZwjI=@localhost:5001" + alice <## "SMP server test failed at Connect, error: BROKER NETWORK" + alice <## "Possibly, certificate fingerprint in server address is incorrect" + testAsyncInitiatingOffline :: IO () testAsyncInitiatingOffline = withTmpFiles $ do putStrLn "testAsyncInitiatingOffline"