From 1a456c494a982b5b7eb2b6e5f6120ca745a5df10 Mon Sep 17 00:00:00 2001 From: Cheng Date: Mon, 2 Sep 2024 10:44:42 +0000 Subject: [PATCH] const where it should be const, and unit test of secret store --- src/display_wallet.cpp | 2 +- src/display_wallet.h | 2 +- src/frame.cpp | 36 +++++++++++++++++++++++------------- src/frame.h | 4 +++- src/localization.h | 1 + src/unit_test.cpp | 42 ++++++++++++++++++++++++++++++++++++------ 6 files changed, 65 insertions(+), 22 deletions(-) diff --git a/src/display_wallet.cpp b/src/display_wallet.cpp index a2b6ff1..7ceebbb 100644 --- a/src/display_wallet.cpp +++ b/src/display_wallet.cpp @@ -2,7 +2,7 @@ using ro::base58; static constexpr char SrcFilename[]{ "src/display_wallet.cpp" }; -display_wallet::display_wallet(wxWindow* parent, wxFileName& walletfile) : +display_wallet::display_wallet(wxWindow* parent, const wxFileName& walletfile) : wxPanel(parent, myID_WALLET_UI, wxDefaultPosition, wxDefaultSize, wxTAB_TRAVERSAL, wxT("Wallet")), m_db(walletfile), m_DisplayWalletEditMenu(1), m_read_names_and_keys(m_db, R"|(SELECT * FROM "UserZookoIDs"; )|"), diff --git a/src/display_wallet.h b/src/display_wallet.h index 7290518..1e2f872 100644 --- a/src/display_wallet.h +++ b/src/display_wallet.h @@ -2,7 +2,7 @@ class display_wallet : public wxPanel { public: - display_wallet(wxWindow*, wxFileName&); + display_wallet(wxWindow*, const wxFileName&); ~display_wallet(); private: struct cleanup; diff --git a/src/frame.cpp b/src/frame.cpp index e155d65..9380b1a 100644 --- a/src/frame.cpp +++ b/src/frame.cpp @@ -114,7 +114,7 @@ void Frame::StorePositionToConfig() { pConfig->SetPath(wxT("/")); } } -void RecursiveCreateDirectory(wxFileName& fn){ +void RecursiveCreateDirectory(const wxFileName& fn){ //Bug workaround, because wxPATH_MKDIR_FULL simply does not work if (!fn.DirExists()) { wxFileName parent(fn); @@ -341,7 +341,7 @@ void Frame::OnDeleteConfiguration(wxCommandEvent&) } using ro::bin2hex, ro::to_base64_string; -void Frame::NewWallet(wxFileName& filename, ristretto255::hash<256>& secret) { +void Frame::NewWallet(const wxFileName& filename, ristretto255::hash<256>& secret) { RecursiveCreateDirectory(filename); /*If creation fails, abort with exception. If it succeeds, set LastUsed to default filename. The exception in unit test should simply generate an error message, but if run during initialization, @@ -439,8 +439,7 @@ COMMIT; } } -void Frame::OnSaveNew(wxCommandEvent& WXUNUSED(event)) -{ +wxFileName Frame::GetPathForNewWallet() { wxFileName wxFileWallet; if (m_DefaultWalletLocation.FileExists()) { // OK, the default wallet exists, so we need a new @@ -486,16 +485,27 @@ void Frame::OnSaveNew(wxCommandEvent& WXUNUSED(event)) RecursiveCreateDirectory(m_DefaultWalletLocation); wxFileWallet = m_DefaultWalletLocation; } - if (wxFileWallet.IsOk()) { - ristretto255::hash<256> WalletSecret(wxFileWallet.GetFullPath().ToUTF8()); - NewWallet(wxFileWallet, WalletSecret); - wxLogMessage("new wallet created: %s", wxFileWallet.GetFullPath()); - if (m_panel)m_panel->Close(true); - m_panel = nullptr; - auto panel = new display_wallet(this, wxFileWallet); - m_panel = panel; - m_LastUsedWallet = wxFileWallet; //We do this last, so that if an exception occurs the filename is forgotten. + if (wxFileWallet.IsOk() == false) { + throw MyException(sz_unexpected_error, __LINE__, __func__, SrcFilename); } + return wxFileWallet; +} + +void Frame::NewWalletDisplay(const wxFileName& filename, const char* human_legible_secret) { + ristretto255::hash<256> WalletSecret(human_legible_secret); + NewWallet(filename, WalletSecret); + wxLogMessage("new wallet created: %s", filename.GetFullPath()); + if (m_panel)m_panel->Close(true); + m_panel = nullptr; + auto panel = new display_wallet(this, filename); + m_panel = panel; + m_LastUsedWallet = filename; //We do this last, so that if an exception occurs the filename is forgotten. +} + +void Frame::OnSaveNew(wxCommandEvent& WXUNUSED(event)) +{ + wxFileName wxFileWallet = GetPathForNewWallet(); + NewWalletDisplay(wxFileWallet, wxFileWallet.GetFullPath().ToUTF8()); } void Frame::OnFileOpen(wxCommandEvent&) { diff --git a/src/frame.h b/src/frame.h index d882faf..d593988 100644 --- a/src/frame.h +++ b/src/frame.h @@ -114,8 +114,10 @@ private: void OnMyCloseMPanel(wxCommandEvent&); public: + wxFileName GetPathForNewWallet(); + void NewWalletDisplay(const wxFileName&, const char*); void OnSaveNew(wxCommandEvent&); - void NewWallet(wxFileName&, ristretto255::hash<256>&); + void NewWallet(const wxFileName&, ristretto255::hash<256>&); void RecreateWalletFromExistingSecret(wxCommandEvent&); void OnFileOpen(wxCommandEvent&); void OnClose(wxCloseEvent& event); diff --git a/src/localization.h b/src/localization.h index c29c4aa..5cf7af8 100644 --- a/src/localization.h +++ b/src/localization.h @@ -73,6 +73,7 @@ inline constexpr auto sz_name_does_not_correspond{ R"|(Public key of name fails inline constexpr auto sz_public_key_of{ R"|(Public key of ")|" }; inline constexpr auto sz_fails_to_correspond{ R"|(" fails to correspond to master secret. This wallet cannot communicate as this identity.)|" }; +constexpr auto sz_unexpected_error{ R"|(unexpected error)|" }; //Command line parameters extern const wchar_t wsz_commandLineLogo[]; diff --git a/src/unit_test.cpp b/src/unit_test.cpp index 00ba51e..211c58a 100644 --- a/src/unit_test.cpp +++ b/src/unit_test.cpp @@ -569,12 +569,42 @@ COMMIT; } static bool StandardPaths(void) { - wxStandardPaths& StandardPaths{ wxStandardPaths::Get() }; - StandardPaths.UseAppInfo(3); - ILogMessage("\tStandard paths"); - wxLogMessage("\t\twxStandardPaths::GetUserLocalDataDir()\t %s", StandardPaths.GetUserLocalDataDir()); - wxLogMessage("\t\twxStandardPaths::GetUserDataDir() \t %s", StandardPaths.GetUserDataDir()); - wxLogMessage("\t\twxStandardPaths::GetLocalDataDir() \t %s", StandardPaths.GetLocalDataDir()); + try{ + wxStandardPaths& StandardPaths{ wxStandardPaths::Get() }; + StandardPaths.UseAppInfo(3); + ILogMessage("\tStandard paths"); + wxLogMessage("\t\twxStandardPaths::GetUserLocalDataDir()\t%s", StandardPaths.GetUserLocalDataDir()); + wxLogMessage("\t\twxStandardPaths::GetUserDataDir() \t%s", StandardPaths.GetUserDataDir()); + wxLogMessage("\t\twxStandardPaths::GetLocalDataDir() \t%s", StandardPaths.GetLocalDataDir()); + wxLogMessage("\t\twxGetUserId()\t\t\t%s", wxGetUserId()); + wxSecretStore store = wxSecretStore::GetDefault(); + wxString errmsg; + if (!store.IsOk(&errmsg))throw MyException(std::string("Secret Store not working. ") + errmsg, __LINE__, __func__, SrcFilename); + wxSecretValue test_secret(9, "12345678"); + wxSecretValue test_secret_ret; + if (!store.Save("MyApp/MyService", wxGetUserId(), test_secret))throw MyException("Failed to save credentials to the system secret store.", __LINE__, __func__, SrcFilename); + // And to load it back : + wxString username; + if (!store.Load("MyApp/MyService", username, test_secret_ret))throw MyException("Unable to reload secret.", __LINE__, __func__, SrcFilename); + if (test_secret != test_secret_ret)throw MyException(wxString(R"|(Reloaded unexpected secret: ")|") + (const char *)(test_secret_ret.GetData()) + R"|(")|", __LINE__, __func__, SrcFilename); + if (username != wxGetUserId())throw MyException(std::string(R"|(Reloaded unexpected username: ")|") + username + R"|(")|", __LINE__, __func__, SrcFilename); + wxLogMessage("\t\tSecret Store working as expected."); + } + catch (const MyException& e) { + errorCode = e.what_num(); + szError = e.what(); + ILogError(szError.c_str()); + } + catch (const std::exception& e) { + errorCode = 19; + szError = e.what(); + ILogError(szError.c_str()); + } + catch (...) { + szError = sz_unknown_error; + errorCode = 19; + ILogError(szError.c_str()); + } unit_test_action = &OpenWallet; return true; }