| 1 | // Copyright (c) 2023, 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 "unit_tests_utils.h" |
| 30 | #include "gtest/gtest.h" |
| 31 | |
| 32 | #include "file_io_utils.h" |
| 33 | #include "wallet/wallet2.h" |
| 34 | #include "common/util.h" |
| 35 | |
| 36 | using namespace boost::filesystem; |
| 37 | using namespace epee::file_io_utils; |
| 38 | |
| 39 | static constexpr const char WALLET_00fd416a_PRIMARY_ADDRESS[] = |
| 40 | "45p2SngJAPSJbqSiUvYfS3BfhEdxZmv8pDt25oW1LzxrZv9Uq6ARagiFViMGUE3gJk5VPWingCXVf1p2tyAy6SUeSHPhbve"; |
| 41 | |
| 42 | TEST(wallet_storage, store_to_file2file) |
| 43 | { |
| 44 | const path source_wallet_file = unit_test::data_dir / "wallet_00fd416a"; |
| 45 | const path interm_wallet_file = unit_test::data_dir / "wallet_00fd416a_copy_file2file"; |
| 46 | const path target_wallet_file = unit_test::data_dir / "wallet_00fd416a_new_file2file"; |
| 47 | |
| 48 | ASSERT_TRUE(is_file_exist(source_wallet_file.string())); |
| 49 | ASSERT_TRUE(is_file_exist(source_wallet_file.string() + ".keys")); |
| 50 | |
| 51 | tools::copy_file(source_wallet_file.string(), interm_wallet_file.string()); |
| 52 | tools::copy_file(source_wallet_file.string() + ".keys", interm_wallet_file.string() + ".keys"); |
| 53 | |
| 54 | ASSERT_TRUE(is_file_exist(interm_wallet_file.string())); |
| 55 | ASSERT_TRUE(is_file_exist(interm_wallet_file.string() + ".keys")); |
| 56 | |
| 57 | if (is_file_exist(target_wallet_file.string())) |
| 58 | remove(target_wallet_file); |
| 59 | if (is_file_exist(target_wallet_file.string() + ".keys")) |
| 60 | remove(target_wallet_file.string() + ".keys"); |
| 61 | ASSERT_FALSE(is_file_exist(target_wallet_file.string())); |
| 62 | ASSERT_FALSE(is_file_exist(target_wallet_file.string() + ".keys")); |
| 63 | |
| 64 | epee::wipeable_string password("beepbeep"); |
| 65 | |
| 66 | const auto files_are_expected = [&]() |
| 67 | { |
| 68 | EXPECT_FALSE(is_file_exist(interm_wallet_file.string())); |
| 69 | EXPECT_FALSE(is_file_exist(interm_wallet_file.string() + ".keys")); |
| 70 | EXPECT_TRUE(is_file_exist(target_wallet_file.string())); |
| 71 | EXPECT_TRUE(is_file_exist(target_wallet_file.string() + ".keys")); |
| 72 | }; |
| 73 | |
| 74 | { |
| 75 | tools::wallet2 w; |
| 76 | w.load(interm_wallet_file.string(), password); |
| 77 | const std::string primary_address = w.get_address_as_str(); |
| 78 | EXPECT_EQ(WALLET_00fd416a_PRIMARY_ADDRESS, primary_address); |
| 79 | w.store_to(target_wallet_file.string(), password); |
| 80 | files_are_expected(); |
| 81 | } |
| 82 | |
| 83 | files_are_expected(); |
| 84 | |
| 85 | { |
| 86 | tools::wallet2 w; |
| 87 | w.load(target_wallet_file.string(), password); |
| 88 | const std::string primary_address = w.get_address_as_str(); |
| 89 | EXPECT_EQ(WALLET_00fd416a_PRIMARY_ADDRESS, primary_address); |
| 90 | w.store_to("", ""); |
| 91 | files_are_expected(); |
| 92 | } |
| 93 | |
| 94 | files_are_expected(); |
| 95 | } |
| 96 | |
| 97 | TEST(wallet_storage, store_to_mem2file) |
| 98 | { |
| 99 | const path target_wallet_file = unit_test::data_dir / "wallet_mem2file"; |
| 100 | |
| 101 | if (is_file_exist(target_wallet_file.string())) |
| 102 | remove(target_wallet_file); |
| 103 | if (is_file_exist(target_wallet_file.string() + ".keys")) |
| 104 | remove(target_wallet_file.string() + ".keys"); |
| 105 | ASSERT_FALSE(is_file_exist(target_wallet_file.string())); |
| 106 | ASSERT_FALSE(is_file_exist(target_wallet_file.string() + ".keys")); |
| 107 | |
| 108 | epee::wipeable_string password("beepbeep2"); |
| 109 | |
| 110 | { |
| 111 | tools::wallet2 w; |
| 112 | w.generate("", password); |
| 113 | w.store_to(target_wallet_file.string(), password); |
| 114 | |
| 115 | EXPECT_TRUE(is_file_exist(target_wallet_file.string())); |
| 116 | EXPECT_TRUE(is_file_exist(target_wallet_file.string() + ".keys")); |
| 117 | } |
| 118 | |
| 119 | EXPECT_TRUE(is_file_exist(target_wallet_file.string())); |
| 120 | EXPECT_TRUE(is_file_exist(target_wallet_file.string() + ".keys")); |
| 121 | |
| 122 | { |
| 123 | tools::wallet2 w; |
| 124 | w.load(target_wallet_file.string(), password); |
| 125 | |
| 126 | EXPECT_TRUE(is_file_exist(target_wallet_file.string())); |
| 127 | EXPECT_TRUE(is_file_exist(target_wallet_file.string() + ".keys")); |
| 128 | } |
| 129 | |
| 130 | EXPECT_TRUE(is_file_exist(target_wallet_file.string())); |
| 131 | EXPECT_TRUE(is_file_exist(target_wallet_file.string() + ".keys")); |
| 132 | } |
| 133 | |
| 134 | TEST(wallet_storage, change_password_same_file) |
| 135 | { |
| 136 | const path source_wallet_file = unit_test::data_dir / "wallet_00fd416a"; |
| 137 | const path interm_wallet_file = unit_test::data_dir / "wallet_00fd416a_copy_change_password_same"; |
| 138 | |
| 139 | ASSERT_TRUE(is_file_exist(source_wallet_file.string())); |
| 140 | ASSERT_TRUE(is_file_exist(source_wallet_file.string() + ".keys")); |
| 141 | |
| 142 | tools::copy_file(source_wallet_file.string(), interm_wallet_file.string()); |
| 143 | tools::copy_file(source_wallet_file.string() + ".keys", interm_wallet_file.string() + ".keys"); |
| 144 | |
| 145 | ASSERT_TRUE(is_file_exist(interm_wallet_file.string())); |
| 146 | ASSERT_TRUE(is_file_exist(interm_wallet_file.string() + ".keys")); |
| 147 | |
| 148 | epee::wipeable_string old_password("beepbeep"); |
| 149 | epee::wipeable_string new_password("meepmeep"); |
| 150 | |
| 151 | { |
| 152 | tools::wallet2 w; |
| 153 | w.load(interm_wallet_file.string(), old_password); |
| 154 | const std::string primary_address = w.get_address_as_str(); |
| 155 | EXPECT_EQ(WALLET_00fd416a_PRIMARY_ADDRESS, primary_address); |
| 156 | w.change_password(w.get_wallet_file(), old_password, new_password); |
| 157 | } |
| 158 | |
| 159 | { |
| 160 | tools::wallet2 w; |
| 161 | w.load(interm_wallet_file.string(), new_password); |
| 162 | const std::string primary_address = w.get_address_as_str(); |
| 163 | EXPECT_EQ(WALLET_00fd416a_PRIMARY_ADDRESS, primary_address); |
| 164 | } |
| 165 | |
| 166 | { |
| 167 | tools::wallet2 w; |
| 168 | EXPECT_THROW(w.load(interm_wallet_file.string(), old_password), tools::error::invalid_password); |
| 169 | } |
| 170 | } |
| 171 | |
| 172 | TEST(wallet_storage, change_password_different_file) |
| 173 | { |
| 174 | const path source_wallet_file = unit_test::data_dir / "wallet_00fd416a"; |
| 175 | const path interm_wallet_file = unit_test::data_dir / "wallet_00fd416a_copy_change_password_diff"; |
| 176 | const path target_wallet_file = unit_test::data_dir / "wallet_00fd416a_new_change_password_diff"; |
| 177 | |
| 178 | ASSERT_TRUE(is_file_exist(source_wallet_file.string())); |
| 179 | ASSERT_TRUE(is_file_exist(source_wallet_file.string() + ".keys")); |
| 180 | |
| 181 | tools::copy_file(source_wallet_file.string(), interm_wallet_file.string()); |
| 182 | tools::copy_file(source_wallet_file.string() + ".keys", interm_wallet_file.string() + ".keys"); |
| 183 | |
| 184 | ASSERT_TRUE(is_file_exist(interm_wallet_file.string())); |
| 185 | ASSERT_TRUE(is_file_exist(interm_wallet_file.string() + ".keys")); |
| 186 | |
| 187 | if (is_file_exist(target_wallet_file.string())) |
| 188 | remove(target_wallet_file); |
| 189 | if (is_file_exist(target_wallet_file.string() + ".keys")) |
| 190 | remove(target_wallet_file.string() + ".keys"); |
| 191 | ASSERT_FALSE(is_file_exist(target_wallet_file.string())); |
| 192 | ASSERT_FALSE(is_file_exist(target_wallet_file.string() + ".keys")); |
| 193 | |
| 194 | epee::wipeable_string old_password("beepbeep"); |
| 195 | epee::wipeable_string new_password("meepmeep"); |
| 196 | |
| 197 | { |
| 198 | tools::wallet2 w; |
| 199 | w.load(interm_wallet_file.string(), old_password); |
| 200 | const std::string primary_address = w.get_address_as_str(); |
| 201 | EXPECT_EQ(WALLET_00fd416a_PRIMARY_ADDRESS, primary_address); |
| 202 | w.change_password(target_wallet_file.string(), old_password, new_password); |
| 203 | } |
| 204 | |
| 205 | EXPECT_FALSE(is_file_exist(interm_wallet_file.string())); |
| 206 | EXPECT_FALSE(is_file_exist(interm_wallet_file.string() + ".keys")); |
| 207 | EXPECT_TRUE(is_file_exist(target_wallet_file.string())); |
| 208 | EXPECT_TRUE(is_file_exist(target_wallet_file.string() + ".keys")); |
| 209 | |
| 210 | { |
| 211 | tools::wallet2 w; |
| 212 | w.load(target_wallet_file.string(), new_password); |
| 213 | const std::string primary_address = w.get_address_as_str(); |
| 214 | EXPECT_EQ(WALLET_00fd416a_PRIMARY_ADDRESS, primary_address); |
| 215 | } |
| 216 | } |
| 217 | |
| 218 | TEST(wallet_storage, change_password_in_memory) |
| 219 | { |
| 220 | const epee::wipeable_string password1("monero"); |
| 221 | const epee::wipeable_string password2("means money"); |
| 222 | const epee::wipeable_string password_wrong("is traceable"); |
| 223 | |
| 224 | tools::wallet2 w; |
| 225 | w.generate("", password1); |
| 226 | const std::string primary_address_1 = w.get_address_as_str(); |
| 227 | w.change_password("", password1, password2); |
| 228 | const std::string primary_address_2 = w.get_address_as_str(); |
| 229 | EXPECT_EQ(primary_address_1, primary_address_2); |
| 230 | |
| 231 | EXPECT_THROW(w.change_password("", password_wrong, password1), tools::error::invalid_password); |
| 232 | } |
| 233 | |
| 234 | TEST(wallet_storage, change_password_mem2file) |
| 235 | { |
| 236 | const path target_wallet_file = unit_test::data_dir / "wallet_change_password_mem2file"; |
| 237 | |
| 238 | if (is_file_exist(target_wallet_file.string())) |
| 239 | remove(target_wallet_file); |
| 240 | if (is_file_exist(target_wallet_file.string() + ".keys")) |
| 241 | remove(target_wallet_file.string() + ".keys"); |
| 242 | ASSERT_FALSE(is_file_exist(target_wallet_file.string())); |
| 243 | ASSERT_FALSE(is_file_exist(target_wallet_file.string() + ".keys")); |
| 244 | |
| 245 | const epee::wipeable_string password1("https://safecurves.cr.yp.to/rigid.html"); |
| 246 | const epee::wipeable_string password2( |
| 247 | "https://csrc.nist.gov/csrc/media/projects/crypto-standards-development-process/documents/dualec_in_x982_and_sp800-90.pdf"); |
| 248 | |
| 249 | std::string primary_address_1, primary_address_2; |
| 250 | { |
| 251 | tools::wallet2 w; |
| 252 | w.generate("", password1); |
| 253 | primary_address_1 = w.get_address_as_str(); |
| 254 | w.change_password(target_wallet_file.string(), password1, password2); |
| 255 | } |
| 256 | |
| 257 | EXPECT_TRUE(is_file_exist(target_wallet_file.string())); |
| 258 | EXPECT_TRUE(is_file_exist(target_wallet_file.string() + ".keys")); |
| 259 | |
| 260 | { |
| 261 | tools::wallet2 w; |
| 262 | w.load(target_wallet_file.string(), password2); |
| 263 | primary_address_2 = w.get_address_as_str(); |
| 264 | } |
| 265 | |
| 266 | EXPECT_EQ(primary_address_1, primary_address_2); |
| 267 | } |