|
| 1 | +// Copyright (c) 2025, 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 | +#include "gtest/gtest.h" |
| 30 | + |
| 31 | +#include "crypto/generators.h" |
| 32 | +#include "cryptonote_basic/cryptonote_format_utils.h" |
| 33 | + |
| 34 | +TEST(cn_format_utils, add_extra_nonce_to_tx_extra) |
| 35 | +{ |
| 36 | + static constexpr std::size_t max_nonce_size = TX_EXTRA_NONCE_MAX_COUNT + 1; // we *can* test higher if desired |
| 37 | + |
| 38 | + std::vector<std::uint8_t> extra_prefix; |
| 39 | + cryptonote::add_tx_pub_key_to_extra(extra_prefix, crypto::get_H()); |
| 40 | + |
| 41 | + std::vector<std::uint8_t> extra; |
| 42 | + std::string nonce; |
| 43 | + std::vector<cryptonote::tx_extra_field> tx_extra_fields; |
| 44 | + extra.reserve(extra_prefix.size() + max_nonce_size + 1 + 10); |
| 45 | + nonce.reserve(max_nonce_size); |
| 46 | + tx_extra_fields.reserve(2); |
| 47 | + for (std::size_t nonce_size = 0; nonce_size <= max_nonce_size; ++nonce_size) |
| 48 | + { |
| 49 | + extra = extra_prefix; |
| 50 | + nonce.resize(nonce_size); |
| 51 | + memset(&nonce[0], '%', nonce.size()); |
| 52 | + tx_extra_fields.clear(); |
| 53 | + |
| 54 | + const std::size_t expected_extra_size = extra_prefix.size() + 1 |
| 55 | + + tools::get_varint_byte_size(nonce_size) + nonce_size; |
| 56 | + const bool expected_success = nonce_size <= TX_EXTRA_NONCE_MAX_COUNT; |
| 57 | + |
| 58 | + // add nonce and do detailed test |
| 59 | + const bool add_success = cryptonote::add_extra_nonce_to_tx_extra(extra, nonce); |
| 60 | + ASSERT_EQ(expected_success, add_success); |
| 61 | + if (!expected_success) |
| 62 | + continue; |
| 63 | + ASSERT_EQ(expected_extra_size, extra.size()); |
| 64 | + ASSERT_EQ(0, memcmp(extra_prefix.data(), extra.data(), extra_prefix.size())); |
| 65 | + const std::uint8_t *p = extra.data() + extra_prefix.size(); |
| 66 | + ASSERT_EQ(TX_EXTRA_NONCE, *p); |
| 67 | + ++p; |
| 68 | + std::size_t read_nonce_size = 0; |
| 69 | + const int varint_size = tools::read_varint((const uint8_t*)(p), // copy p |
| 70 | + (const uint8_t*) extra.data() + extra.size(), |
| 71 | + read_nonce_size); |
| 72 | + ASSERT_EQ(tools::get_varint_byte_size(nonce_size), varint_size); |
| 73 | + p += varint_size; |
| 74 | + for (std::size_t i = 0; i < nonce_size; ++i) |
| 75 | + { |
| 76 | + ASSERT_EQ('%', *p); |
| 77 | + ++p; |
| 78 | + } |
| 79 | + ASSERT_EQ(extra.data() + extra.size(), p); |
| 80 | + |
| 81 | + // do integration test with higher-level tx_extra parsing code |
| 82 | + ASSERT_TRUE(cryptonote::parse_tx_extra(extra, tx_extra_fields)); |
| 83 | + ASSERT_EQ(2, tx_extra_fields.size()); |
| 84 | + const auto &pk_field = boost::get<cryptonote::tx_extra_pub_key>(tx_extra_fields.at(0)); |
| 85 | + ASSERT_EQ(crypto::get_H(), pk_field.pub_key); |
| 86 | + const auto &nonce_field = boost::get<cryptonote::tx_extra_nonce>(tx_extra_fields.at(1)); |
| 87 | + ASSERT_EQ(nonce, nonce_field.nonce); |
| 88 | + } |
| 89 | +} |
0 commit comments