| 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 | #include <boost/filesystem.hpp> |
| 31 | #include <boost/optional/optional_io.hpp> |
| 32 | #include "gtest/gtest.h" |
| 33 | |
| 34 | #include "include_base_utils.h" |
| 35 | #include "wallet/wallet2.h" |
| 36 | #include "crypto/crypto.h" |
| 37 | #include "cryptonote_basic/account.h" |
| 38 | #include "cryptonote_basic/cryptonote_basic_impl.h" |
| 39 | #include "wallet/api/subaddress.h" |
| 40 | |
| 41 | class WalletSubaddress : public ::testing::Test |
| 42 | { |
| 43 | protected: |
| 44 | virtual void SetUp() |
| 45 | { |
| 46 | try |
| 47 | { |
| 48 | w1.generate("", password, recovery_key, true, false); |
| 49 | } |
| 50 | catch (const std::exception& e) |
| 51 | { |
| 52 | LOG_ERROR("failed to generate wallet: " << e.what()); |
| 53 | throw; |
| 54 | } |
| 55 | |
| 56 | w1.add_subaddress_account(test_label); |
| 57 | w1.set_subaddress_label(subaddress_index, test_label); |
| 58 | } |
| 59 | |
| 60 | virtual void TearDown() |
| 61 | { |
| 62 | } |
| 63 | |
| 64 | tools::wallet2 w1; |
| 65 | const std::string password = "testpass"; |
| 66 | crypto::secret_key recovery_key = crypto::secret_key(); |
| 67 | const std::string test_label = "subaddress test label"; |
| 68 | |
| 69 | uint32_t major_index = 0; |
| 70 | uint32_t minor_index = 0; |
| 71 | const cryptonote::subaddress_index subaddress_index = {major_index, minor_index}; |
| 72 | }; |
| 73 | |
| 74 | TEST_F(WalletSubaddress, GetSubaddressLabel) |
| 75 | { |
| 76 | EXPECT_EQ(test_label, w1.get_subaddress_label(subaddress_index)); |
| 77 | } |
| 78 | |
| 79 | TEST_F(WalletSubaddress, AddSubaddress) |
| 80 | { |
| 81 | std::string label = "test adding subaddress"; |
| 82 | w1.add_subaddress(0, label); |
| 83 | EXPECT_EQ(label, w1.get_subaddress_label({0, 1})); |
| 84 | } |
| 85 | |
| 86 | TEST_F(WalletSubaddress, OutOfBoundsIndexes) |
| 87 | { |
| 88 | try |
| 89 | { |
| 90 | w1.get_subaddress_label({1,0}); |
| 91 | } |
| 92 | catch(const std::exception& e) |
| 93 | { |
| 94 | EXPECT_STREQ("index_major is out of bound", e.what()); |
| 95 | } |
| 96 | try |
| 97 | { |
| 98 | w1.get_subaddress_label({0,2}); |
| 99 | } |
| 100 | catch(const std::exception& e) |
| 101 | { |
| 102 | EXPECT_STREQ("index.minor is out of bound", e.what()); |
| 103 | } |
| 104 | } |
| 105 | |
| 106 | // Helper function to check max subaddrs allocated |
| 107 | static void check_expected_max(const tools::wallet2 &w1, const cryptonote::subaddress_index exp_max) |
| 108 | { |
| 109 | for (uint32_t i = 0; i <= exp_max.minor; ++i) |
| 110 | { |
| 111 | auto subaddr = w1.get_subaddress({exp_max.major, i}); |
| 112 | EXPECT_NE(boost::none, w1.get_subaddress_index(subaddr)); |
| 113 | } |
| 114 | auto subaddr = w1.get_subaddress({exp_max.major, exp_max.minor + 1}); |
| 115 | EXPECT_EQ(boost::none, w1.get_subaddress_index(subaddr)); |
| 116 | }; |
| 117 | |
| 118 | static void expect_default_wallet_state(const tools::wallet2 &w1) |
| 119 | { |
| 120 | // these tests assume we are starting with the default setup state |
| 121 | EXPECT_EQ(2, w1.get_num_subaddress_accounts()); |
| 122 | EXPECT_EQ(50, w1.get_subaddress_lookahead().first); |
| 123 | EXPECT_EQ(200, w1.get_subaddress_lookahead().second); |
| 124 | |
| 125 | // We assume we start with subaddrs for minor indexes 0 to 199 |
| 126 | check_expected_max(w1, {0,199}); |
| 127 | check_expected_max(w1, {1,199}); |
| 128 | check_expected_max(w1, {49,199}); |
| 129 | check_expected_max(w1, {50,199}); // 50 because the test starts with accounts 0 and 1 already allocated |
| 130 | EXPECT_EQ(boost::none, w1.get_subaddress_index(w1.get_subaddress({51,0}))); |
| 131 | } |
| 132 | |
| 133 | TEST_F(WalletSubaddress, SetLookahead) |
| 134 | { |
| 135 | expect_default_wallet_state(w1); |
| 136 | // get_subaddress_index looks up keys in the private m_subaddresses dictionary so we will use it to test if a key is properly being scanned for |
| 137 | cryptonote::subaddress_index test_idx = {50, 199}; |
| 138 | auto subaddr = w1.get_subaddress(test_idx); |
| 139 | EXPECT_NE(boost::none, w1.get_subaddress_index(subaddr)); |
| 140 | // fist test expanding the major lookahead |
| 141 | w1.set_subaddress_lookahead(100, 200); |
| 142 | EXPECT_EQ(100, w1.get_subaddress_lookahead().first); |
| 143 | EXPECT_EQ(200, w1.get_subaddress_lookahead().second); |
| 144 | check_expected_max(w1, {100, 199}); |
| 145 | // next test expanding the minor lookahead |
| 146 | w1.set_subaddress_lookahead(100, 300); |
| 147 | EXPECT_EQ(100, w1.get_subaddress_lookahead().first); |
| 148 | EXPECT_EQ(300, w1.get_subaddress_lookahead().second); |
| 149 | check_expected_max(w1, {100, 299}); |
| 150 | } |
| 151 | |
| 152 | TEST_F(WalletSubaddress, ExpandThenSetMinorIncreaseOnly) |
| 153 | { |
| 154 | expect_default_wallet_state(w1); |
| 155 | |
| 156 | // Mock receive to {0,150}, so expand from there |
| 157 | w1.expand_subaddresses({0,150}); |
| 158 | // We should now have subaddresses for minor indexes 0 to 349 |
| 159 | check_expected_max(w1, {0,349}); |
| 160 | check_expected_max(w1, {1,199}); |
| 161 | check_expected_max(w1, {49,199}); |
| 162 | check_expected_max(w1, {50,199}); |
| 163 | EXPECT_EQ(boost::none, w1.get_subaddress_index(w1.get_subaddress({51,0}))); |
| 164 | |
| 165 | // Now set the minor lookahead 100 higher |
| 166 | w1.set_subaddress_lookahead(50, 200+100); |
| 167 | // We should have subaddresses for minor indexes 0 to 449 |
| 168 | check_expected_max(w1, {0,449}); |
| 169 | check_expected_max(w1, {1,299}); |
| 170 | check_expected_max(w1, {49,299}); |
| 171 | check_expected_max(w1, {50,299}); |
| 172 | EXPECT_EQ(boost::none, w1.get_subaddress_index(w1.get_subaddress({51,0}))); |
| 173 | } |
| 174 | |
| 175 | TEST_F(WalletSubaddress, ExpandThenSetMajorIncreaseOnly) |
| 176 | { |
| 177 | expect_default_wallet_state(w1); |
| 178 | |
| 179 | // Mock receive to {40,0}, so expand from there |
| 180 | w1.expand_subaddresses({40,0}); |
| 181 | check_expected_max(w1, {0,199}); |
| 182 | check_expected_max(w1, {1,199}); |
| 183 | check_expected_max(w1, {40,199}); |
| 184 | check_expected_max(w1, {89,199}); |
| 185 | EXPECT_EQ(boost::none, w1.get_subaddress_index(w1.get_subaddress({90,0}))); |
| 186 | |
| 187 | // Now set the major lookahead 10 higher |
| 188 | w1.set_subaddress_lookahead(50+10, 200); |
| 189 | check_expected_max(w1, {0,199}); |
| 190 | check_expected_max(w1, {1,199}); |
| 191 | check_expected_max(w1, {40,199}); |
| 192 | check_expected_max(w1, {99,199}); |
| 193 | EXPECT_EQ(boost::none, w1.get_subaddress_index(w1.get_subaddress({100,0}))); |
| 194 | } |
| 195 | |
| 196 | TEST_F(WalletSubaddress, ExpandThenSetIncreaseBoth) |
| 197 | { |
| 198 | expect_default_wallet_state(w1); |
| 199 | |
| 200 | // Mock receive to {40,150}, so expand from there |
| 201 | w1.expand_subaddresses({40,150}); |
| 202 | check_expected_max(w1, {0,199}); |
| 203 | check_expected_max(w1, {1,199}); |
| 204 | check_expected_max(w1, {40,349}); |
| 205 | check_expected_max(w1, {89,199}); |
| 206 | EXPECT_EQ(boost::none, w1.get_subaddress_index(w1.get_subaddress({90,0}))); |
| 207 | |
| 208 | // Now set the major lookahead 10 higher and minor 100 higher |
| 209 | w1.set_subaddress_lookahead(50+10, 200+100); |
| 210 | check_expected_max(w1, {0,299}); |
| 211 | check_expected_max(w1, {1,299}); |
| 212 | check_expected_max(w1, {40,449}); |
| 213 | check_expected_max(w1, {99,299}); |
| 214 | EXPECT_EQ(boost::none, w1.get_subaddress_index(w1.get_subaddress({100,0}))); |
| 215 | } |