| 1 | // Copyright (c) 2014-2022, The Monero Project |
| 2 | // |
| 3 | // All rights reserved. |
| 4 | // |
| 5 | // Redistribution and use in source and binary forms, with or without modification, are |
| 6 | // permitted provided that the following conditions are met: |
| 7 | // |
| 8 | // 1. Redistributions of source code must retain the above copyright notice, this list of |
| 9 | // conditions and the following disclaimer. |
| 10 | // |
| 11 | // 2. Redistributions in binary form must reproduce the above copyright notice, this list |
| 12 | // of conditions and the following disclaimer in the documentation and/or other |
| 13 | // materials provided with the distribution. |
| 14 | // |
| 15 | // 3. Neither the name of the copyright holder nor the names of its contributors may be |
| 16 | // used to endorse or promote products derived from this software without specific |
| 17 | // prior written permission. |
| 18 | // |
| 19 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY |
| 20 | // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF |
| 21 | // MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL |
| 22 | // THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 23 | // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
| 24 | // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| 25 | // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, |
| 26 | // STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF |
| 27 | // THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 28 | // |
| 29 | // Parts of this file are originally copyright (c) 2012-2013 The Cryptonote developers |
| 30 | |
| 31 | #include "gtest/gtest.h" |
| 32 | |
| 33 | #include <cstdint> |
| 34 | |
| 35 | #include "common/base58.cpp" |
| 36 | #include "cryptonote_basic/cryptonote_basic_impl.h" |
| 37 | #include "serialization/binary_utils.h" |
| 38 | |
| 39 | using namespace tools; |
| 40 | |
| 41 | #define MAKE_STR(arr) std::string(arr, sizeof(arr) - 1) |
| 42 | |
| 43 | namespace |
| 44 | { |
| 45 | void do_test_uint_8be_to_64(uint64_t expected, const uint8_t* data, size_t size) |
| 46 | { |
| 47 | uint64_t val = base58::uint_8be_to_64(data, size); |
| 48 | ASSERT_EQ(val, expected); |
| 49 | } |
| 50 | |
| 51 | void do_test_uint_64_to_8be(uint64_t num, const std::string& expected_str) |
| 52 | { |
| 53 | std::string data(expected_str.size(), '\x0'); |
| 54 | base58::uint_64_to_8be(num, data.size(), reinterpret_cast<uint8_t*>(&data[0])); |
| 55 | ASSERT_EQ(data, expected_str); |
| 56 | } |
| 57 | |
| 58 | void do_test_encode_block(const std::string& block, const std::string& expected) |
| 59 | { |
| 60 | ASSERT_TRUE(1 <= block.size() && block.size() <= base58::full_block_size); |
| 61 | std::string enc(base58::encoded_block_sizes[block.size()], base58::alphabet[0]); |
| 62 | base58::encode_block(block.data(), block.size(), &enc[0]); |
| 63 | ASSERT_EQ(enc, expected); |
| 64 | |
| 65 | std::string dec(block.size(), '\0'); |
| 66 | ASSERT_TRUE(base58::decode_block(enc.data(), enc.size(), &dec[0])); |
| 67 | ASSERT_EQ(block, dec); |
| 68 | } |
| 69 | |
| 70 | void do_test_decode_block_pos(const std::string& enc, const std::string& expected) |
| 71 | { |
| 72 | std::string data(base58::decoded_block_sizes::instance(enc.size()), '\0'); |
| 73 | ASSERT_TRUE(base58::decode_block(enc.data(), enc.size(), &data[0])); |
| 74 | ASSERT_EQ(data, expected); |
| 75 | } |
| 76 | |
| 77 | void do_test_decode_block_neg(const std::string& enc) |
| 78 | { |
| 79 | std::string data(base58::full_block_size, '\0'); |
| 80 | ASSERT_FALSE(base58::decode_block(enc.data(), enc.size(), &data[0])); |
| 81 | } |
| 82 | |
| 83 | void do_test_encode(const std::string& data, const std::string& expected) |
| 84 | { |
| 85 | std::string enc = base58::encode(data); |
| 86 | ASSERT_EQ(enc, expected); |
| 87 | |
| 88 | std::string dec; |
| 89 | ASSERT_TRUE(base58::decode(enc, dec)); |
| 90 | ASSERT_EQ(dec, data); |
| 91 | } |
| 92 | |
| 93 | void do_test_decode_pos(const std::string& enc, const std::string& expected) |
| 94 | { |
| 95 | std::string dec; |
| 96 | ASSERT_TRUE(base58::decode(enc, dec)); |
| 97 | ASSERT_EQ(dec, expected); |
| 98 | } |
| 99 | |
| 100 | void do_test_decode_neg(const std::string& enc) |
| 101 | { |
| 102 | std::string dec; |
| 103 | ASSERT_FALSE(base58::decode(enc, dec)); |
| 104 | } |
| 105 | |
| 106 | void do_test_encode_decode_addr(uint64_t tag, const std::string& data, const std::string& expected) |
| 107 | { |
| 108 | std::string addr = base58::encode_addr(tag, data); |
| 109 | ASSERT_EQ(addr, expected); |
| 110 | |
| 111 | uint64_t dec_tag; |
| 112 | std::string dec_data; |
| 113 | ASSERT_TRUE(base58::decode_addr(addr, dec_tag, dec_data)); |
| 114 | ASSERT_EQ(tag, dec_tag); |
| 115 | ASSERT_EQ(data, dec_data); |
| 116 | } |
| 117 | } |
| 118 | |
| 119 | #define TEST_uint_8be_to_64(expected, str) \ |
| 120 | TEST(base58_uint_8be_to_64, handles_bytes_##expected) \ |
| 121 | { \ |
| 122 | std::string data = str; \ |
| 123 | do_test_uint_8be_to_64(expected, reinterpret_cast<const uint8_t*>(data.data()), data.size()); \ |
| 124 | } |
| 125 | |
| 126 | TEST_uint_8be_to_64(0x0000000000000001, "\x1"); |
| 127 | TEST_uint_8be_to_64(0x0000000000000102, "\x1\x2"); |
| 128 | TEST_uint_8be_to_64(0x0000000000010203, "\x1\x2\x3"); |
| 129 | TEST_uint_8be_to_64(0x0000000001020304, "\x1\x2\x3\x4"); |
| 130 | TEST_uint_8be_to_64(0x0000000102030405, "\x1\x2\x3\x4\x5"); |
| 131 | TEST_uint_8be_to_64(0x0000010203040506, "\x1\x2\x3\x4\x5\x6"); |
| 132 | TEST_uint_8be_to_64(0x0001020304050607, "\x1\x2\x3\x4\x5\x6\x7"); |
| 133 | TEST_uint_8be_to_64(0x0102030405060708, "\x1\x2\x3\x4\x5\x6\x7\x8"); |
| 134 | |
| 135 | |
| 136 | #define TEST_uint_64_to_8be(num, expected_str) \ |
| 137 | TEST(base58_uint_64_to_8be, handles_bytes_##num) \ |
| 138 | { \ |
| 139 | do_test_uint_64_to_8be(num, expected_str); \ |
| 140 | } |
| 141 | |
| 142 | TEST_uint_64_to_8be(0x0000000000000001, "\x1"); |
| 143 | TEST_uint_64_to_8be(0x0000000000000102, "\x1\x2"); |
| 144 | TEST_uint_64_to_8be(0x0000000000010203, "\x1\x2\x3"); |
| 145 | TEST_uint_64_to_8be(0x0000000001020304, "\x1\x2\x3\x4"); |
| 146 | TEST_uint_64_to_8be(0x0000000102030405, "\x1\x2\x3\x4\x5"); |
| 147 | TEST_uint_64_to_8be(0x0000010203040506, "\x1\x2\x3\x4\x5\x6"); |
| 148 | TEST_uint_64_to_8be(0x0001020304050607, "\x1\x2\x3\x4\x5\x6\x7"); |
| 149 | TEST_uint_64_to_8be(0x0102030405060708, "\x1\x2\x3\x4\x5\x6\x7\x8"); |
| 150 | |
| 151 | TEST(reverse_alphabet, is_correct) |
| 152 | { |
| 153 | ASSERT_EQ(-1, base58::reverse_alphabet::instance(0)); |
| 154 | ASSERT_EQ(-1, base58::reverse_alphabet::instance(std::numeric_limits<char>::min())); |
| 155 | ASSERT_EQ(-1, base58::reverse_alphabet::instance(std::numeric_limits<char>::max())); |
| 156 | ASSERT_EQ(-1, base58::reverse_alphabet::instance('1' - 1)); |
| 157 | ASSERT_EQ(-1, base58::reverse_alphabet::instance('z' + 1)); |
| 158 | ASSERT_EQ(-1, base58::reverse_alphabet::instance('0')); |
| 159 | ASSERT_EQ(-1, base58::reverse_alphabet::instance('I')); |
| 160 | ASSERT_EQ(-1, base58::reverse_alphabet::instance('O')); |
| 161 | ASSERT_EQ(-1, base58::reverse_alphabet::instance('l')); |
| 162 | ASSERT_EQ(0, base58::reverse_alphabet::instance('1')); |
| 163 | ASSERT_EQ(8, base58::reverse_alphabet::instance('9')); |
| 164 | ASSERT_EQ(base58::alphabet_size - 1, base58::reverse_alphabet::instance('z')); |
| 165 | } |
| 166 | |
| 167 | |
| 168 | #define TEST_encode_block(block, expected) \ |
| 169 | TEST(base58_encode_block, handles_##expected) \ |
| 170 | { \ |
| 171 | do_test_encode_block(MAKE_STR(block), #expected); \ |
| 172 | } |
| 173 | |
| 174 | TEST_encode_block("\x00", 11); |
| 175 | TEST_encode_block("\x39", 1z); |
| 176 | TEST_encode_block("\xFF", 5Q); |
| 177 | |
| 178 | TEST_encode_block("\x00\x00", 111); |
| 179 | TEST_encode_block("\x00\x39", 11z); |
| 180 | TEST_encode_block("\x01\x00", 15R); |
| 181 | TEST_encode_block("\xFF\xFF", LUv); |
| 182 | |
| 183 | TEST_encode_block("\x00\x00\x00", 11111); |
| 184 | TEST_encode_block("\x00\x00\x39", 1111z); |
| 185 | TEST_encode_block("\x01\x00\x00", 11LUw); |
| 186 | TEST_encode_block("\xFF\xFF\xFF", 2UzHL); |
| 187 | |
| 188 | TEST_encode_block("\x00\x00\x00\x39", 11111z); |
| 189 | TEST_encode_block("\xFF\xFF\xFF\xFF", 7YXq9G); |
| 190 | TEST_encode_block("\x00\x00\x00\x00\x39", 111111z); |
| 191 | TEST_encode_block("\xFF\xFF\xFF\xFF\xFF", VtB5VXc); |
| 192 | TEST_encode_block("\x00\x00\x00\x00\x00\x39", 11111111z); |
| 193 | TEST_encode_block("\xFF\xFF\xFF\xFF\xFF\xFF", 3CUsUpv9t); |
| 194 | TEST_encode_block("\x00\x00\x00\x00\x00\x00\x39", 111111111z); |
| 195 | TEST_encode_block("\xFF\xFF\xFF\xFF\xFF\xFF\xFF", Ahg1opVcGW); |
| 196 | TEST_encode_block("\x00\x00\x00\x00\x00\x00\x00\x39", 1111111111z); |
| 197 | TEST_encode_block("\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF", jpXCZedGfVQ); |
| 198 | |
| 199 | TEST_encode_block("\x00\x00\x00\x00\x00\x00\x00\x00", 11111111111); |
| 200 | TEST_encode_block("\x00\x00\x00\x00\x00\x00\x00\x01", 11111111112); |
| 201 | TEST_encode_block("\x00\x00\x00\x00\x00\x00\x00\x08", 11111111119); |
| 202 | TEST_encode_block("\x00\x00\x00\x00\x00\x00\x00\x09", 1111111111A); |
| 203 | TEST_encode_block("\x00\x00\x00\x00\x00\x00\x00\x3A", 11111111121); |
| 204 | TEST_encode_block("\x00\xFF\xFF\xFF\xFF\xFF\xFF\xFF", 1Ahg1opVcGW); |
| 205 | TEST_encode_block("\x06\x15\x60\x13\x76\x28\x79\xF7", 22222222222); |
| 206 | TEST_encode_block("\x05\xE0\x22\xBA\x37\x4B\x2A\x00", 1z111111111); |
| 207 | |
| 208 | |
| 209 | #define TEST_decode_block_pos(enc, expected) \ |
| 210 | TEST(base58_decode_block, handles_pos_##enc) \ |
| 211 | { \ |
| 212 | do_test_decode_block_pos(#enc, MAKE_STR(expected)); \ |
| 213 | } |
| 214 | |
| 215 | #define TEST_decode_block_neg(enc) \ |
| 216 | TEST(base58_decode_block, handles_neg_##enc) \ |
| 217 | { \ |
| 218 | do_test_decode_block_neg(#enc); \ |
| 219 | } |
| 220 | |
| 221 | // 1-byte block |
| 222 | TEST_decode_block_neg(1); |
| 223 | TEST_decode_block_neg(z); |
| 224 | // 2-bytes block |
| 225 | TEST_decode_block_pos(11, "\x00"); |
| 226 | TEST_decode_block_pos(5Q, "\xFF"); |
| 227 | TEST_decode_block_neg(5R); |
| 228 | TEST_decode_block_neg(zz); |
| 229 | // 3-bytes block |
| 230 | TEST_decode_block_pos(111, "\x00\x00"); |
| 231 | TEST_decode_block_pos(LUv, "\xFF\xFF"); |
| 232 | TEST_decode_block_neg(LUw); |
| 233 | TEST_decode_block_neg(zzz); |
| 234 | // 4-bytes block |
| 235 | TEST_decode_block_neg(1111); |
| 236 | TEST_decode_block_neg(zzzz); |
| 237 | // 5-bytes block |
| 238 | TEST_decode_block_pos(11111, "\x00\x00\x00"); |
| 239 | TEST_decode_block_pos(2UzHL, "\xFF\xFF\xFF"); |
| 240 | TEST_decode_block_neg(2UzHM); |
| 241 | TEST_decode_block_neg(zzzzz); |
| 242 | // 6-bytes block |
| 243 | TEST_decode_block_pos(111111, "\x00\x00\x00\x00"); |
| 244 | TEST_decode_block_pos(7YXq9G, "\xFF\xFF\xFF\xFF"); |
| 245 | TEST_decode_block_neg(7YXq9H); |
| 246 | TEST_decode_block_neg(zzzzzz); |
| 247 | // 7-bytes block |
| 248 | TEST_decode_block_pos(1111111, "\x00\x00\x00\x00\x00"); |
| 249 | TEST_decode_block_pos(VtB5VXc, "\xFF\xFF\xFF\xFF\xFF"); |
| 250 | TEST_decode_block_neg(VtB5VXd); |
| 251 | TEST_decode_block_neg(zzzzzzz); |
| 252 | // 8-bytes block |
| 253 | TEST_decode_block_neg(11111111); |
| 254 | TEST_decode_block_neg(zzzzzzzz); |
| 255 | // 9-bytes block |
| 256 | TEST_decode_block_pos(111111111, "\x00\x00\x00\x00\x00\x00"); |
| 257 | TEST_decode_block_pos(3CUsUpv9t, "\xFF\xFF\xFF\xFF\xFF\xFF"); |
| 258 | TEST_decode_block_neg(3CUsUpv9u); |
| 259 | TEST_decode_block_neg(zzzzzzzzz); |
| 260 | // 10-bytes block |
| 261 | TEST_decode_block_pos(1111111111, "\x00\x00\x00\x00\x00\x00\x00"); |
| 262 | TEST_decode_block_pos(Ahg1opVcGW, "\xFF\xFF\xFF\xFF\xFF\xFF\xFF"); |
| 263 | TEST_decode_block_neg(Ahg1opVcGX); |
| 264 | TEST_decode_block_neg(zzzzzzzzzz); |
| 265 | // 11-bytes block |
| 266 | TEST_decode_block_pos(11111111111, "\x00\x00\x00\x00\x00\x00\x00\x00"); |
| 267 | TEST_decode_block_pos(jpXCZedGfVQ, "\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF"); |
| 268 | TEST_decode_block_neg(jpXCZedGfVR); |
| 269 | TEST_decode_block_neg(zzzzzzzzzzz); |
| 270 | // Invalid symbols |
| 271 | TEST_decode_block_neg(01111111111); |
| 272 | TEST_decode_block_neg(11111111110); |
| 273 | TEST_decode_block_neg(11111011111); |
| 274 | TEST_decode_block_neg(I1111111111); |
| 275 | TEST_decode_block_neg(O1111111111); |
| 276 | TEST_decode_block_neg(l1111111111); |
| 277 | TEST_decode_block_neg(_1111111111); |
| 278 | |
| 279 | |
| 280 | #define TEST_encode(expected, data) \ |
| 281 | TEST(base58_encode, handles_##expected) \ |
| 282 | { \ |
| 283 | do_test_encode(MAKE_STR(data), #expected); \ |
| 284 | } |
| 285 | |
| 286 | TEST_encode(11, "\x00"); |
| 287 | TEST_encode(111, "\x00\x00"); |
| 288 | TEST_encode(11111, "\x00\x00\x00"); |
| 289 | TEST_encode(111111, "\x00\x00\x00\x00"); |
| 290 | TEST_encode(1111111, "\x00\x00\x00\x00\x00"); |
| 291 | TEST_encode(111111111, "\x00\x00\x00\x00\x00\x00"); |
| 292 | TEST_encode(1111111111, "\x00\x00\x00\x00\x00\x00\x00"); |
| 293 | TEST_encode(11111111111, "\x00\x00\x00\x00\x00\x00\x00\x00"); |
| 294 | TEST_encode(1111111111111, "\x00\x00\x00\x00\x00\x00\x00\x00\x00"); |
| 295 | TEST_encode(11111111111111, "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"); |
| 296 | TEST_encode(1111111111111111, "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"); |
| 297 | TEST_encode(11111111111111111, "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"); |
| 298 | TEST_encode(111111111111111111, "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"); |
| 299 | TEST_encode(11111111111111111111, "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"); |
| 300 | TEST_encode(111111111111111111111, "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"); |
| 301 | TEST_encode(1111111111111111111111, "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"); |
| 302 | TEST_encode(22222222222VtB5VXc, "\x06\x15\x60\x13\x76\x28\x79\xF7\xFF\xFF\xFF\xFF\xFF"); |
| 303 | |
| 304 | |
| 305 | #define TEST_decode_pos(enc, expected) \ |
| 306 | TEST(base58_decode_pos, handles_pos_##enc) \ |
| 307 | { \ |
| 308 | do_test_decode_pos(#enc, MAKE_STR(expected)); \ |
| 309 | } |
| 310 | |
| 311 | #define TEST_decode_neg(enc) \ |
| 312 | TEST(base58_decode_neg, handles_neg_##enc) \ |
| 313 | { \ |
| 314 | do_test_decode_neg(#enc); \ |
| 315 | } |
| 316 | |
| 317 | TEST_decode_pos(, ""); |
| 318 | TEST_decode_pos(5Q, "\xFF"); |
| 319 | TEST_decode_pos(LUv, "\xFF\xFF"); |
| 320 | TEST_decode_pos(2UzHL, "\xFF\xFF\xFF"); |
| 321 | TEST_decode_pos(7YXq9G, "\xFF\xFF\xFF\xFF"); |
| 322 | TEST_decode_pos(VtB5VXc, "\xFF\xFF\xFF\xFF\xFF"); |
| 323 | TEST_decode_pos(3CUsUpv9t, "\xFF\xFF\xFF\xFF\xFF\xFF"); |
| 324 | TEST_decode_pos(Ahg1opVcGW, "\xFF\xFF\xFF\xFF\xFF\xFF\xFF"); |
| 325 | TEST_decode_pos(jpXCZedGfVQ, "\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF"); |
| 326 | TEST_decode_pos(jpXCZedGfVQ5Q, "\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF"); |
| 327 | TEST_decode_pos(jpXCZedGfVQLUv, "\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF"); |
| 328 | TEST_decode_pos(jpXCZedGfVQ2UzHL, "\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF"); |
| 329 | TEST_decode_pos(jpXCZedGfVQ7YXq9G, "\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF"); |
| 330 | TEST_decode_pos(jpXCZedGfVQVtB5VXc, "\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF"); |
| 331 | TEST_decode_pos(jpXCZedGfVQ3CUsUpv9t, "\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF"); |
| 332 | TEST_decode_pos(jpXCZedGfVQAhg1opVcGW, "\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF"); |
| 333 | TEST_decode_pos(jpXCZedGfVQjpXCZedGfVQ, "\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF"); |
| 334 | // Invalid length |
| 335 | TEST_decode_neg(1); |
| 336 | TEST_decode_neg(z); |
| 337 | TEST_decode_neg(1111); |
| 338 | TEST_decode_neg(zzzz); |
| 339 | TEST_decode_neg(11111111); |
| 340 | TEST_decode_neg(zzzzzzzz); |
| 341 | TEST_decode_neg(123456789AB1); |
| 342 | TEST_decode_neg(123456789ABz); |
| 343 | TEST_decode_neg(123456789AB1111); |
| 344 | TEST_decode_neg(123456789ABzzzz); |
| 345 | TEST_decode_neg(123456789AB11111111); |
| 346 | TEST_decode_neg(123456789ABzzzzzzzz); |
| 347 | // Overflow |
| 348 | TEST_decode_neg(5R); |
| 349 | TEST_decode_neg(zz); |
| 350 | TEST_decode_neg(LUw); |
| 351 | TEST_decode_neg(zzz); |
| 352 | TEST_decode_neg(2UzHM); |
| 353 | TEST_decode_neg(zzzzz); |
| 354 | TEST_decode_neg(7YXq9H); |
| 355 | TEST_decode_neg(zzzzzz); |
| 356 | TEST_decode_neg(VtB5VXd); |
| 357 | TEST_decode_neg(zzzzzzz); |
| 358 | TEST_decode_neg(3CUsUpv9u); |
| 359 | TEST_decode_neg(zzzzzzzzz); |
| 360 | TEST_decode_neg(Ahg1opVcGX); |
| 361 | TEST_decode_neg(zzzzzzzzzz); |
| 362 | TEST_decode_neg(jpXCZedGfVR); |
| 363 | TEST_decode_neg(zzzzzzzzzzz); |
| 364 | TEST_decode_neg(123456789AB5R); |
| 365 | TEST_decode_neg(123456789ABzz); |
| 366 | TEST_decode_neg(123456789ABLUw); |
| 367 | TEST_decode_neg(123456789ABzzz); |
| 368 | TEST_decode_neg(123456789AB2UzHM); |
| 369 | TEST_decode_neg(123456789ABzzzzz); |
| 370 | TEST_decode_neg(123456789AB7YXq9H); |
| 371 | TEST_decode_neg(123456789ABzzzzzz); |
| 372 | TEST_decode_neg(123456789ABVtB5VXd); |
| 373 | TEST_decode_neg(123456789ABzzzzzzz); |
| 374 | TEST_decode_neg(123456789AB3CUsUpv9u); |
| 375 | TEST_decode_neg(123456789ABzzzzzzzzz); |
| 376 | TEST_decode_neg(123456789ABAhg1opVcGX); |
| 377 | TEST_decode_neg(123456789ABzzzzzzzzzz); |
| 378 | TEST_decode_neg(123456789ABjpXCZedGfVR); |
| 379 | TEST_decode_neg(123456789ABzzzzzzzzzzz); |
| 380 | TEST_decode_neg(zzzzzzzzzzz11); |
| 381 | // Invalid symbols |
| 382 | TEST_decode_neg(10); |
| 383 | TEST_decode_neg(11I); |
| 384 | TEST_decode_neg(11O11); |
| 385 | TEST_decode_neg(11l111); |
| 386 | TEST_decode_neg(11_11111111); |
| 387 | TEST_decode_neg(1101111111111); |
| 388 | TEST_decode_neg(11I11111111111111); |
| 389 | TEST_decode_neg(11O1111111111111111111); |
| 390 | TEST_decode_neg(1111111111110); |
| 391 | TEST_decode_neg(111111111111l1111); |
| 392 | TEST_decode_neg(111111111111_111111111); |
| 393 | |
| 394 | |
| 395 | #define TEST_encode_decode_addr(addr, tag, data) \ |
| 396 | TEST(base58_encode_decode_addr, handles_##addr) \ |
| 397 | { \ |
| 398 | do_test_encode_decode_addr(UINT64_C(tag), MAKE_STR(data), #addr); \ |
| 399 | } |
| 400 | |
| 401 | TEST_encode_decode_addr(21D35quxec71111111111111111111111111111111111111111111111111111111111111111111111111111116Q5tCH, 6, |
| 402 | "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00" |
| 403 | "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"); |
| 404 | TEST_encode_decode_addr(2Aui6ejTFscjpXCZedGfVQjpXCZedGfVQjpXCZedGfVQjpXCZedGfVQjpXCZedGfVQjpXCZedGfVQjpXCZedGfVQVqegMoV, 6, |
| 405 | "\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF" |
| 406 | "\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF"); |
| 407 | TEST_encode_decode_addr(1119XrkPuSmLzdHXgVgrZKjepg5hZAxffLzdHXgVgrZKjepg5hZAxffLzdHXgVgrZKjepg5hZAxffLzdHXgVgrZKVphZRvn, 0, |
| 408 | "\x00\x11\x22\x33\x44\x55\x66\x77\x88\x99\xAA\xBB\xCC\xDD\xEE\xFF\x00\x11\x22\x33\x44\x55\x66\x77\x88\x99\xAA\xBB\xCC\xDD\xEE\xFF" |
| 409 | "\x00\x11\x22\x33\x44\x55\x66\x77\x88\x99\xAA\xBB\xCC\xDD\xEE\xFF\x00\x11\x22\x33\x44\x55\x66\x77\x88\x99\xAA\xBB\xCC\xDD\xEE\xFF"); |
| 410 | TEST_encode_decode_addr(111111111111111111111111111111111111111111111111111111111111111111111111111111111111111115TXfiA, 0, |
| 411 | "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00" |
| 412 | "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"); |
| 413 | TEST_encode_decode_addr(PuT7GAdgbA83qvSEivPLYo11111111111111111111111111111111111111111111111111111111111111111111111111111169tWrH, 0x1122334455667788, |
| 414 | "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00" |
| 415 | "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"); |
| 416 | TEST_encode_decode_addr(PuT7GAdgbA841d7FXjswpJjpXCZedGfVQjpXCZedGfVQjpXCZedGfVQjpXCZedGfVQjpXCZedGfVQjpXCZedGfVQjpXCZedGfVQVq4LL1v, 0x1122334455667788, |
| 417 | "\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF" |
| 418 | "\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF"); |
| 419 | TEST_encode_decode_addr(PuT7GAdgbA819VwdWVDP, 0x1122334455667788, "\x11"); |
| 420 | TEST_encode_decode_addr(PuT7GAdgbA81efAfdCjPg, 0x1122334455667788, "\x22\x22"); |
| 421 | TEST_encode_decode_addr(PuT7GAdgbA83sryEt3YC8Q, 0x1122334455667788, "\x33\x33\x33"); |
| 422 | TEST_encode_decode_addr(PuT7GAdgbA83tWUuc54PFP3b, 0x1122334455667788, "\x44\x44\x44\x44"); |
| 423 | TEST_encode_decode_addr(PuT7GAdgbA83u9zaKrtRKZ1J6, 0x1122334455667788, "\x55\x55\x55\x55\x55"); |
| 424 | TEST_encode_decode_addr(PuT7GAdgbA83uoWF3eanGG1aRoG, 0x1122334455667788, "\x66\x66\x66\x66\x66\x66"); |
| 425 | TEST_encode_decode_addr(PuT7GAdgbA83vT1umSHMYJ4oNVdu, 0x1122334455667788, "\x77\x77\x77\x77\x77\x77\x77"); |
| 426 | TEST_encode_decode_addr(PuT7GAdgbA83w6XaVDyvpoGQBEWbB, 0x1122334455667788, "\x88\x88\x88\x88\x88\x88\x88\x88"); |
| 427 | TEST_encode_decode_addr(PuT7GAdgbA83wk3FD1gW7J2KVGofA1r, 0x1122334455667788, "\x99\x99\x99\x99\x99\x99\x99\x99\x99"); |
| 428 | TEST_encode_decode_addr(15p2yAV, 0, ""); |
| 429 | TEST_encode_decode_addr(FNQ3D6A, 0x7F, ""); |
| 430 | TEST_encode_decode_addr(26k9QWweu, 0x80, ""); |
| 431 | TEST_encode_decode_addr(3BzAD7n3y, 0xFF, ""); |
| 432 | TEST_encode_decode_addr(11efCaY6UjG7JrxuB, 0, "\x11\x22\x33\x44\x55\x66\x77"); |
| 433 | TEST_encode_decode_addr(21rhHRT48LN4PriP9, 6, "\x11\x22\x33\x44\x55\x66\x77"); |
| 434 | |
| 435 | |
| 436 | #define TEST_decode_addr_neg(addr, test_name) \ |
| 437 | TEST(base58_decode_addr_neg, test_name) \ |
| 438 | { \ |
| 439 | uint64_t tag; \ |
| 440 | std::string data; \ |
| 441 | ASSERT_FALSE(base58::decode_addr(MAKE_STR(addr), tag, data)); \ |
| 442 | } |
| 443 | |
| 444 | TEST_decode_addr_neg("zuT7GAdgbA819VwdWVDP", decode_fails_due_overflow); |
| 445 | TEST_decode_addr_neg("0uT7GAdgbA819VwdWVDP", decode_fails_due_invalid_char_0); |
| 446 | TEST_decode_addr_neg("IuT7GAdgbA819VwdWVDP", decode_fails_due_invalid_char_I); |
| 447 | TEST_decode_addr_neg("OuT7GAdgbA819VwdWVDP", decode_fails_due_invalid_char_O); |
| 448 | TEST_decode_addr_neg("luT7GAdgbA819VwdWVDP", decode_fails_due_invalid_char_l); |
| 449 | TEST_decode_addr_neg("\0uT7GAdgbA819VwdWVDP", decode_fails_due_invalid_char_00); |
| 450 | TEST_decode_addr_neg("PuT7GAdgbA819VwdWVD", decode_fails_due_invalid_lenght); |
| 451 | TEST_decode_addr_neg("11efCaY6UjG7JrxuC", handles_invalid_checksum); |
| 452 | TEST_decode_addr_neg("jerj2e4mESo", handles_non_correct_tag); // "jerj2e4mESo" == "\xFF\x00\xFF\xFF\x5A\xD9\xF1\x1C" |
| 453 | TEST_decode_addr_neg("1", decode_fails_due_invalid_block_len_0); |
| 454 | TEST_decode_addr_neg("1111", decode_fails_due_invalid_block_len_1); |
| 455 | TEST_decode_addr_neg("11", decode_fails_due_address_too_short_0); |
| 456 | TEST_decode_addr_neg("111", decode_fails_due_address_too_short_1); |
| 457 | TEST_decode_addr_neg("11111", decode_fails_due_address_too_short_2); |
| 458 | TEST_decode_addr_neg("111111", decode_fails_due_address_too_short_3); |
| 459 | TEST_decode_addr_neg("999999", decode_fails_due_address_too_short_4); |
| 460 | TEST_decode_addr_neg("ZZZZZZ", decode_fails_due_address_too_short_5); |
| 461 | |
| 462 | namespace |
| 463 | { |
| 464 | std::string test_serialized_keys = MAKE_STR( |
| 465 | "\xf7\x24\xbc\x5c\x6c\xfb\xb9\xd9\x76\x02\xc3\x00\x42\x3a\x2f\x28" |
| 466 | "\x64\x18\x74\x51\x3a\x03\x57\x78\xa0\xc1\x77\x8d\x83\x32\x01\xe9" |
| 467 | "\x22\x09\x39\x68\x9e\xdf\x1a\xbd\x5b\xc1\xd0\x31\xf7\x3e\xcd\x6c" |
| 468 | "\x99\x3a\xdd\x66\xd6\x80\x88\x70\x45\x6a\xfe\xb8\xe7\xee\xb6\x8d"); |
| 469 | // DON'T ever use this as a destination for funds, as the keys are right above this comment... |
| 470 | std::string test_keys_addr_str = "4AzKEX4gXdJdNeM6dfiBFL7kqund3HYGvMBF3ttsNd9SfzgYB6L7ep1Yg1osYJzLdaKAYSLVh6e6jKnAuzj3bw1oGy9kXCb"; |
| 471 | } |
| 472 | |
| 473 | TEST(get_account_address_as_str, works_correctly) |
| 474 | { |
| 475 | cryptonote::account_public_address addr; |
| 476 | ASSERT_TRUE(serialization::parse_binary(test_serialized_keys, addr)); |
| 477 | std::string addr_str = cryptonote::get_account_address_as_str(cryptonote::MAINNET, false, addr); |
| 478 | ASSERT_EQ(addr_str, test_keys_addr_str); |
| 479 | } |
| 480 | |
| 481 | TEST(get_account_address_from_str, handles_valid_address) |
| 482 | { |
| 483 | cryptonote::address_parse_info info; |
| 484 | ASSERT_TRUE(cryptonote::get_account_address_from_str(info, cryptonote::MAINNET, test_keys_addr_str)); |
| 485 | |
| 486 | std::string blob; |
| 487 | ASSERT_TRUE(serialization::dump_binary(info.address, blob)); |
| 488 | ASSERT_EQ(blob, test_serialized_keys); |
| 489 | } |
| 490 | |
| 491 | TEST(get_account_address_from_str, fails_on_invalid_address_format) |
| 492 | { |
| 493 | cryptonote::address_parse_info info; |
| 494 | std::string addr_str = test_keys_addr_str; |
| 495 | addr_str[0] = '0'; |
| 496 | |
| 497 | ASSERT_FALSE(cryptonote::get_account_address_from_str(info, cryptonote::MAINNET, addr_str)); |
| 498 | } |
| 499 | |
| 500 | TEST(get_account_address_from_str, fails_on_invalid_address_prefix) |
| 501 | { |
| 502 | std::string addr_str = base58::encode_addr(0, test_serialized_keys); |
| 503 | |
| 504 | cryptonote::address_parse_info info; |
| 505 | ASSERT_FALSE(cryptonote::get_account_address_from_str(info, cryptonote::MAINNET, addr_str)); |
| 506 | } |
| 507 | |
| 508 | TEST(get_account_address_from_str, fails_on_invalid_address_content) |
| 509 | { |
| 510 | std::string addr_str = base58::encode_addr(config::CRYPTONOTE_PUBLIC_ADDRESS_BASE58_PREFIX, test_serialized_keys.substr(1)); |
| 511 | |
| 512 | cryptonote::address_parse_info info; |
| 513 | ASSERT_FALSE(cryptonote::get_account_address_from_str(info, cryptonote::MAINNET, addr_str)); |
| 514 | } |
| 515 | |
| 516 | TEST(get_account_address_from_str, fails_on_invalid_address_spend_key) |
| 517 | { |
| 518 | std::string serialized_keys_copy = test_serialized_keys; |
| 519 | serialized_keys_copy[0] = '\0'; |
| 520 | std::string addr_str = base58::encode_addr(config::CRYPTONOTE_PUBLIC_ADDRESS_BASE58_PREFIX, serialized_keys_copy); |
| 521 | |
| 522 | cryptonote::address_parse_info info; |
| 523 | ASSERT_FALSE(cryptonote::get_account_address_from_str(info, cryptonote::MAINNET, addr_str)); |
| 524 | } |
| 525 | |
| 526 | TEST(get_account_address_from_str, fails_on_invalid_address_view_key) |
| 527 | { |
| 528 | std::string serialized_keys_copy = test_serialized_keys; |
| 529 | serialized_keys_copy.back() = '\x01'; |
| 530 | std::string addr_str = base58::encode_addr(config::CRYPTONOTE_PUBLIC_ADDRESS_BASE58_PREFIX, serialized_keys_copy); |
| 531 | |
| 532 | cryptonote::address_parse_info info; |
| 533 | ASSERT_FALSE(cryptonote::get_account_address_from_str(info, cryptonote::MAINNET, addr_str)); |
| 534 | } |
| 535 | |
| 536 | TEST(get_account_address_from_str, parses_old_address_format) |
| 537 | { |
| 538 | cryptonote::address_parse_info info; |
| 539 | ASSERT_TRUE(cryptonote::get_account_address_from_str(info, cryptonote::MAINNET, "002391bbbb24dea6fd95232e97594a27769d0153d053d2102b789c498f57a2b00b69cd6f2f5c529c1660f2f4a2b50178d6640c20ce71fe26373041af97c5b10236fc")); |
| 540 | } |