Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

unit tests: wallet_storage: generate in "Ascii" format test #9030

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
117 changes: 117 additions & 0 deletions tests/unit_tests/wallet_storage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@
#include "unit_tests_utils.h"
#include "gtest/gtest.h"

#include <cctype>

#include "file_io_utils.h"
#include "wallet/wallet2.h"

Expand All @@ -38,6 +40,9 @@ using namespace epee::file_io_utils;
static constexpr const char WALLET_00fd416a_PRIMARY_ADDRESS[] =
"45p2SngJAPSJbqSiUvYfS3BfhEdxZmv8pDt25oW1LzxrZv9Uq6ARagiFViMGUE3gJk5VPWingCXVf1p2tyAy6SUeSHPhbve";

// https://github.com/monero-project/monero/blob/67d190ce7c33602b6a3b804f633ee1ddb7fbb4a1/src/wallet/wallet2.cpp#L156
static constexpr const char WALLET2_ASCII_OUTPUT_MAGIC[] = "MoneroAsciiDataV1";

TEST(wallet_storage, store_to_file2file)
{
const path source_wallet_file = unit_test::data_dir / "wallet_00fd416a";
Expand Down Expand Up @@ -264,3 +269,115 @@ TEST(wallet_storage, change_password_mem2file)

EXPECT_EQ(primary_address_1, primary_address_2);
}

TEST(wallet_storage, gen_ascii_format)
{
const path target_wallet_file = unit_test::data_dir / "wallet_gen_ascii_format";

if (is_file_exist(target_wallet_file.string()))
remove(target_wallet_file);
if (is_file_exist(target_wallet_file.string() + ".keys"))
remove(target_wallet_file.string() + ".keys");
ASSERT_FALSE(is_file_exist(target_wallet_file.string()));
ASSERT_FALSE(is_file_exist(target_wallet_file.string() + ".keys"));

const epee::wipeable_string password("https://safecurves.cr.yp.to/rigid.html");

std::string primary_address_1, primary_address_2;
{
tools::wallet2 w;
w.set_export_format(tools::wallet2::Ascii);
ASSERT_EQ(tools::wallet2::Ascii, w.export_format());
w.generate(target_wallet_file.string(), password);
primary_address_1 = w.get_address_as_str();
}

ASSERT_TRUE(is_file_exist(target_wallet_file.string()));
ASSERT_TRUE(is_file_exist(target_wallet_file.string() + ".keys"));

// Assert that we store keys in ascii format
{
std::string key_file_contents;
ASSERT_TRUE(epee::file_io_utils::load_file_to_string(target_wallet_file.string() + ".keys", key_file_contents));
EXPECT_NE(std::string::npos, key_file_contents.find(WALLET2_ASCII_OUTPUT_MAGIC));
for (const char c : key_file_contents)
ASSERT_TRUE(std::isprint(c) || c == '\n' || c == '\r');
}

{
tools::wallet2 w;
w.set_export_format(tools::wallet2::Ascii);
ASSERT_EQ(tools::wallet2::Ascii, w.export_format());
w.load(target_wallet_file.string(), password);
primary_address_2 = w.get_address_as_str();
}

EXPECT_EQ(primary_address_1, primary_address_2);
}

TEST(wallet_storage, change_export_format)
{
const path target_wallet_file = unit_test::data_dir / "wallet_change_export_format";

if (is_file_exist(target_wallet_file.string()))
remove(target_wallet_file);
if (is_file_exist(target_wallet_file.string() + ".keys"))
remove(target_wallet_file.string() + ".keys");
ASSERT_FALSE(is_file_exist(target_wallet_file.string()));
ASSERT_FALSE(is_file_exist(target_wallet_file.string() + ".keys"));

const epee::wipeable_string password("https://safecurves.cr.yp.to/rigid.html");

std::string primary_address_1, primary_address_2;
{
tools::wallet2 w;
ASSERT_EQ(tools::wallet2::Binary, w.export_format());
w.generate(target_wallet_file.string(), password);
primary_address_1 = w.get_address_as_str();
w.store();

// Assert that we initially store keys in binary format
{
std::string key_file_contents;
ASSERT_TRUE(epee::file_io_utils::load_file_to_string(target_wallet_file.string() + ".keys", key_file_contents));
EXPECT_EQ(std::string::npos, key_file_contents.find(WALLET2_ASCII_OUTPUT_MAGIC));
bool only_printable = true;
for (const char c : key_file_contents)
{
if (!std::isprint(c) && c != '\n' && c != '\r')
{
only_printable = false;
break;
}
}
EXPECT_FALSE(only_printable);
}

// switch formats and store
w.set_export_format(tools::wallet2::Ascii);
ASSERT_EQ(tools::wallet2::Ascii, w.export_format());
w.store_to("", password, /*force_rewrite_keys=*/ true);
}

ASSERT_TRUE(is_file_exist(target_wallet_file.string()));
ASSERT_TRUE(is_file_exist(target_wallet_file.string() + ".keys"));

// Assert that we store keys in ascii format
{
std::string key_file_contents;
ASSERT_TRUE(epee::file_io_utils::load_file_to_string(target_wallet_file.string() + ".keys", key_file_contents));
EXPECT_NE(std::string::npos, key_file_contents.find(WALLET2_ASCII_OUTPUT_MAGIC));
for (const char c : key_file_contents)
ASSERT_TRUE(std::isprint(c) || c == '\n' || c == '\r');
}

{
tools::wallet2 w;
w.set_export_format(tools::wallet2::Ascii);
ASSERT_EQ(tools::wallet2::Ascii, w.export_format());
w.load(target_wallet_file.string(), password);
primary_address_2 = w.get_address_as_str();
}

EXPECT_EQ(primary_address_1, primary_address_2);
}