feat(base58): unfixed bug with double \0 and incorrect real str_size

This commit is contained in:
2024-09-19 01:50:32 +03:00
parent f2119850ee
commit 817f6c4fb1
8 changed files with 172 additions and 61 deletions

35
test/test-base58.cpp Normal file
View File

@ -0,0 +1,35 @@
#include <utility>
#include <base/base58.hpp>
#include <base/hex.hpp>
#include <gtest/gtest.h>
using namespace base58;
std::pair<std::string, std::string> test = {
"003812e515df45235c6bcc2233ac9d0c4ebbd781de",
"167VUagc755PbQoB7cCTfTPjbQ5Nk6fEuD",
};
TEST(base58, encodeCheck)
{
EXPECT_EQ(encodeCheck(hex::decode(test.first)), test.second);
}
TEST(base58, decodeCheck)
{
EXPECT_EQ(test.first, hex::encode(decodeCheck(test.second)));
try
{
decodeCheck("incorrect");
}
catch (const std::exception &e)
{
EXPECT_STREQ(e.what(), "base58::decodeCheck: checksum incorrect");
}
}
int main(int argc, char **argv)
{
testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
}

View File

@ -18,14 +18,14 @@ TEST(base64, isValid)
for (auto it : tests)
EXPECT_EQ(it.first, isValid(it.second));
}
std::vector<std::pair<std::string, std::string>> tests = {
{"", ""},
{"BKUEpQ==", "04a504a5"},
{"BKUEpQA=", "04a504a500"},
{"BKUEpQAA", "04a504a50000"},
};
TEST(base64, encode)
{
std::vector<std::pair<std::string, std::string>> tests = {
{"", ""},
{"BKUEpQ==", "04a504a5"},
{"BKUEpQA=", "04a504a500"},
{"BKUEpQAA", "04a504a50000"},
};
for (auto it : tests)
EXPECT_EQ(it.first, encode(hex::decode(it.second)));
}
@ -36,12 +36,6 @@ TEST(base64, encode_1e7)
}
TEST(base64, decode)
{
std::vector<std::pair<std::string, std::string>> tests = {
{"", ""},
{"BKUEpQ==", "04a504a5"},
{"BKUEpQA=", "04a504a500"},
{"BKUEpQAA", "04a504a50000"},
};
for (auto it : tests)
EXPECT_EQ(hex::encode(decode(it.first)), it.second);
}

View File

@ -1,34 +1,12 @@
#include <utility>
#include <base/base58.hpp>
#include <base/baseN.hpp>
#include <base/hex.hpp>
#include <gtest/gtest.h>
using namespace baseN;
static const char b58digits[] =
"123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz";
static const int8_t b58map[] = {
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
-1, 0, 1, 2, 3, 4, 5, 6, 7, 8, -1, -1, -1, -1, -1, -1,
-1, 9, 10, 11, 12, 13, 14, 15, 16, -1, 17, 18, 19, 20, 21, -1,
22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, -1, -1, -1, -1, -1,
-1, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, -1, 44, 45, 46,
47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
//
};
TEST(baseN, isValid)
{
std::vector<std::pair<bool, std::string>> tests = {
@ -36,44 +14,38 @@ TEST(baseN, isValid)
{false, "@#$"},
};
for (auto it : tests)
EXPECT_EQ(it.first, isValid(it.second, b58map));
EXPECT_EQ(it.first, isValid(it.second, base58::map));
}
std::vector<std::pair<std::string, std::string>> tests = {
{"", ""},
{"Ky", "044c"},
{"KyK", "f94a"},
{"KyKX", "387ae2"},
{"KyKXa", "0ccbd755"},
{"KyKXaa", "02e62ec963"},
{"4uqWDRyJZUpS6KKwLAiitndmv7TPFt2bfxVVfhJhgTn3Rh6aQtGHQY6PhhNDpCwSNU8a",
"057902f9cebebb68879911002aae743280140a78c4a077405b057902f9cebebb68879911002aae743280140a78c4a077405b"},
};
TEST(baseN, encode)
{
std::vector<std::pair<std::string, std::string>> tests = {
{"", ""},
{"Ky", "044c"},
{"KyK", "f94a"},
{"KyKX", "387ae2"},
{"KyKXa", "0ccbd755"},
{"KyKXaa", "02e62ec963"},
};
for (auto it : tests)
EXPECT_EQ(it.first, encode(hex::decode(it.second), 58, b58digits));
EXPECT_EQ(it.first, encode(hex::decode(it.second), 58, base58::digits));
}
TEST(baseN, encode_1e3)
{
std::vector<uint8_t> data(1e3);
std::fill(data.begin(), data.end(), 1);
encode(data, 58, b58digits);
encode(data, 58, base58::digits);
}
TEST(baseN, decode)
{
std::vector<std::pair<std::string, std::string>> tests = {
{"", ""},
{"Ky", "044c"},
{"KyK", "f94a"},
{"KyKX", "387ae2"},
{"KyKXa", "0ccbd755"},
{"KyKXaa", "02e62ec963"},
};
for (auto it : tests)
EXPECT_EQ(hex::encode(decode(it.first, 58, b58digits, b58map)), it.second);
EXPECT_EQ(hex::encode(decode(it.first, 58, base58::digits, base58::map)), it.second);
}
TEST(baseN, decode_1e3)
{
std::string str(1e3, '2');
decode(str, 58, b58digits, b58map);
decode(str, 58, base58::digits, base58::map);
}
int main(int argc, char **argv)