const where it should be const, and unit test of secret store

This commit is contained in:
Cheng 2024-09-02 10:44:42 +00:00
parent a6b700a45d
commit 1a456c494a
No known key found for this signature in database
6 changed files with 65 additions and 22 deletions

View File

@ -2,7 +2,7 @@
using ro::base58; using ro::base58;
static constexpr char SrcFilename[]{ "src/display_wallet.cpp" }; 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")), wxPanel(parent, myID_WALLET_UI, wxDefaultPosition, wxDefaultSize, wxTAB_TRAVERSAL, wxT("Wallet")),
m_db(walletfile), m_DisplayWalletEditMenu(1), m_db(walletfile), m_DisplayWalletEditMenu(1),
m_read_names_and_keys(m_db, R"|(SELECT * FROM "UserZookoIDs"; )|"), m_read_names_and_keys(m_db, R"|(SELECT * FROM "UserZookoIDs"; )|"),

View File

@ -2,7 +2,7 @@
class display_wallet : public wxPanel class display_wallet : public wxPanel
{ {
public: public:
display_wallet(wxWindow*, wxFileName&); display_wallet(wxWindow*, const wxFileName&);
~display_wallet(); ~display_wallet();
private: private:
struct cleanup; struct cleanup;

View File

@ -114,7 +114,7 @@ void Frame::StorePositionToConfig() {
pConfig->SetPath(wxT("/")); pConfig->SetPath(wxT("/"));
} }
} }
void RecursiveCreateDirectory(wxFileName& fn){ void RecursiveCreateDirectory(const wxFileName& fn){
//Bug workaround, because wxPATH_MKDIR_FULL simply does not work //Bug workaround, because wxPATH_MKDIR_FULL simply does not work
if (!fn.DirExists()) { if (!fn.DirExists()) {
wxFileName parent(fn); wxFileName parent(fn);
@ -341,7 +341,7 @@ void Frame::OnDeleteConfiguration(wxCommandEvent&)
} }
using ro::bin2hex, ro::to_base64_string; 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); RecursiveCreateDirectory(filename);
/*If creation fails, abort with exception. If it succeeds, set LastUsed to default 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, 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; wxFileName wxFileWallet;
if (m_DefaultWalletLocation.FileExists()) { if (m_DefaultWalletLocation.FileExists()) {
// OK, the default wallet exists, so we need a new // OK, the default wallet exists, so we need a new
@ -486,16 +485,27 @@ void Frame::OnSaveNew(wxCommandEvent& WXUNUSED(event))
RecursiveCreateDirectory(m_DefaultWalletLocation); RecursiveCreateDirectory(m_DefaultWalletLocation);
wxFileWallet = m_DefaultWalletLocation; wxFileWallet = m_DefaultWalletLocation;
} }
if (wxFileWallet.IsOk()) { if (wxFileWallet.IsOk() == false) {
ristretto255::hash<256> WalletSecret(wxFileWallet.GetFullPath().ToUTF8()); throw MyException(sz_unexpected_error, __LINE__, __func__, SrcFilename);
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.
} }
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&) { void Frame::OnFileOpen(wxCommandEvent&) {

View File

@ -114,8 +114,10 @@ private:
void OnMyCloseMPanel(wxCommandEvent&); void OnMyCloseMPanel(wxCommandEvent&);
public: public:
wxFileName GetPathForNewWallet();
void NewWalletDisplay(const wxFileName&, const char*);
void OnSaveNew(wxCommandEvent&); void OnSaveNew(wxCommandEvent&);
void NewWallet(wxFileName&, ristretto255::hash<256>&); void NewWallet(const wxFileName&, ristretto255::hash<256>&);
void RecreateWalletFromExistingSecret(wxCommandEvent&); void RecreateWalletFromExistingSecret(wxCommandEvent&);
void OnFileOpen(wxCommandEvent&); void OnFileOpen(wxCommandEvent&);
void OnClose(wxCloseEvent& event); void OnClose(wxCloseEvent& event);

View File

@ -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_public_key_of{ R"|(Public key of ")|" };
inline constexpr auto sz_fails_to_correspond{ R"|(" fails to correspond to master secret. inline constexpr auto sz_fails_to_correspond{ R"|(" fails to correspond to master secret.
This wallet cannot communicate as this identity.)|" }; This wallet cannot communicate as this identity.)|" };
constexpr auto sz_unexpected_error{ R"|(unexpected error)|" };
//Command line parameters //Command line parameters
extern const wchar_t wsz_commandLineLogo[]; extern const wchar_t wsz_commandLineLogo[];

View File

@ -569,12 +569,42 @@ COMMIT;
} }
static bool StandardPaths(void) { static bool StandardPaths(void) {
wxStandardPaths& StandardPaths{ wxStandardPaths::Get() }; try{
StandardPaths.UseAppInfo(3); wxStandardPaths& StandardPaths{ wxStandardPaths::Get() };
ILogMessage("\tStandard paths"); StandardPaths.UseAppInfo(3);
wxLogMessage("\t\twxStandardPaths::GetUserLocalDataDir()\t %s", StandardPaths.GetUserLocalDataDir()); ILogMessage("\tStandard paths");
wxLogMessage("\t\twxStandardPaths::GetUserDataDir() \t %s", StandardPaths.GetUserDataDir()); wxLogMessage("\t\twxStandardPaths::GetUserLocalDataDir()\t%s", StandardPaths.GetUserLocalDataDir());
wxLogMessage("\t\twxStandardPaths::GetLocalDataDir() \t %s", StandardPaths.GetLocalDataDir()); 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; unit_test_action = &OpenWallet;
return true; return true;
} }