| 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 "wallet/api/wallet2_api.h" |
| 34 | #include "wallet/wallet2.h" |
| 35 | #include "include_base_utils.h" |
| 36 | #include "common/util.h" |
| 37 | |
| 38 | #include <boost/chrono/chrono.hpp> |
| 39 | #include <boost/filesystem.hpp> |
| 40 | #include <boost/algorithm/string.hpp> |
| 41 | #include <boost/asio.hpp> |
| 42 | #include <boost/date_time/posix_time/posix_time.hpp> |
| 43 | #include <boost/thread/condition_variable.hpp> |
| 44 | #include <boost/thread/mutex.hpp> |
| 45 | #include <boost/thread/thread.hpp> |
| 46 | |
| 47 | #include <iostream> |
| 48 | #include <vector> |
| 49 | #include <atomic> |
| 50 | #include <functional> |
| 51 | |
| 52 | |
| 53 | using namespace std; |
| 54 | //unsigned int epee::g_test_dbg_lock_sleep = 0; |
| 55 | |
| 56 | namespace Consts |
| 57 | { |
| 58 | |
| 59 | |
| 60 | // TODO: get rid of hardcoded paths |
| 61 | |
| 62 | const char * WALLET_NAME = "testwallet"; |
| 63 | const char * WALLET_NAME_MAINNET = "testwallet_mainnet"; |
| 64 | const char * WALLET_NAME_COPY = "testwallet_copy"; |
| 65 | const char * WALLET_NAME_WITH_DIR = "walletdir/testwallet_test"; |
| 66 | const char * WALLET_NAME_WITH_DIR_NON_WRITABLE = "/var/walletdir/testwallet_test"; |
| 67 | const char * WALLET_PASS = "password"; |
| 68 | const char * WALLET_PASS2 = "password22"; |
| 69 | const char * WALLET_LANG = "English"; |
| 70 | |
| 71 | std::string WALLETS_ROOT_DIR = "/var/monero/testnet_pvt"; |
| 72 | std::string TESTNET_WALLET1_NAME; |
| 73 | std::string TESTNET_WALLET2_NAME; |
| 74 | std::string TESTNET_WALLET3_NAME; |
| 75 | std::string TESTNET_WALLET4_NAME; |
| 76 | std::string TESTNET_WALLET5_NAME; |
| 77 | std::string TESTNET_WALLET6_NAME; |
| 78 | |
| 79 | const char * TESTNET_WALLET_PASS = ""; |
| 80 | |
| 81 | std::string CURRENT_SRC_WALLET; |
| 82 | std::string CURRENT_DST_WALLET; |
| 83 | |
| 84 | const uint64_t AMOUNT_10XMR = 10000000000000L; |
| 85 | const uint64_t AMOUNT_5XMR = 5000000000000L; |
| 86 | const uint64_t AMOUNT_1XMR = 1000000000000L; |
| 87 | |
| 88 | const std::string PAYMENT_ID_EMPTY = ""; |
| 89 | |
| 90 | std::string TESTNET_DAEMON_ADDRESS = "localhost:38081"; |
| 91 | std::string MAINNET_DAEMON_ADDRESS = "localhost:18081"; |
| 92 | |
| 93 | |
| 94 | } |
| 95 | |
| 96 | |
| 97 | |
| 98 | using namespace Consts; |
| 99 | |
| 100 | struct Utils |
| 101 | { |
| 102 | static void deleteWallet(const std::string & walletname) |
| 103 | { |
| 104 | std::cout << "** deleting wallet: " << walletname << std::endl; |
| 105 | boost::filesystem::remove(walletname); |
| 106 | boost::filesystem::remove(walletname + ".address.txt"); |
| 107 | boost::filesystem::remove(walletname + ".keys"); |
| 108 | } |
| 109 | |
| 110 | static void deleteDir(const std::string &path) |
| 111 | { |
| 112 | std::cout << "** removing dir recursively: " << path << std::endl; |
| 113 | boost::filesystem::remove_all(path); |
| 114 | } |
| 115 | |
| 116 | static void print_transaction(Monero::TransactionInfo * t) |
| 117 | { |
| 118 | |
| 119 | std::cout << "d: " |
| 120 | << (t->direction() == Monero::TransactionInfo::Direction_In ? "in" : "out") |
| 121 | << ", pe: " << (t->isPending() ? "true" : "false") |
| 122 | << ", bh: " << t->blockHeight() |
| 123 | << ", a: " << Monero::Wallet::displayAmount(t->amount()) |
| 124 | << ", f: " << Monero::Wallet::displayAmount(t->fee()) |
| 125 | << ", h: " << t->hash() |
| 126 | << ", pid: " << t->paymentId() |
| 127 | << std::endl; |
| 128 | } |
| 129 | |
| 130 | static std::string get_wallet_address(const std::string &filename, const std::string &password) |
| 131 | { |
| 132 | Monero::WalletManager *wmgr = Monero::WalletManagerFactory::getWalletManager(); |
| 133 | Monero::Wallet * w = wmgr->openWallet(filename, password, Monero::NetworkType::TESTNET); |
| 134 | std::string result = w->mainAddress(); |
| 135 | wmgr->closeWallet(w); |
| 136 | return result; |
| 137 | } |
| 138 | }; |
| 139 | |
| 140 | |
| 141 | struct WalletManagerTest : public testing::Test |
| 142 | { |
| 143 | Monero::WalletManager * wmgr; |
| 144 | |
| 145 | |
| 146 | WalletManagerTest() |
| 147 | { |
| 148 | std::cout << __FUNCTION__ << std::endl; |
| 149 | wmgr = Monero::WalletManagerFactory::getWalletManager(); |
| 150 | // Monero::WalletManagerFactory::setLogLevel(Monero::WalletManagerFactory::LogLevel_4); |
| 151 | Utils::deleteWallet(WALLET_NAME); |
| 152 | Utils::deleteDir(boost::filesystem::path(WALLET_NAME_WITH_DIR).parent_path().string()); |
| 153 | } |
| 154 | |
| 155 | |
| 156 | ~WalletManagerTest() |
| 157 | { |
| 158 | std::cout << __FUNCTION__ << std::endl; |
| 159 | //deleteWallet(WALLET_NAME); |
| 160 | } |
| 161 | |
| 162 | }; |
| 163 | |
| 164 | struct WalletManagerMainnetTest : public testing::Test |
| 165 | { |
| 166 | Monero::WalletManager * wmgr; |
| 167 | |
| 168 | |
| 169 | WalletManagerMainnetTest() |
| 170 | { |
| 171 | std::cout << __FUNCTION__ << std::endl; |
| 172 | wmgr = Monero::WalletManagerFactory::getWalletManager(); |
| 173 | Utils::deleteWallet(WALLET_NAME_MAINNET); |
| 174 | } |
| 175 | |
| 176 | |
| 177 | ~WalletManagerMainnetTest() |
| 178 | { |
| 179 | std::cout << __FUNCTION__ << std::endl; |
| 180 | } |
| 181 | |
| 182 | }; |
| 183 | |
| 184 | struct WalletTest1 : public testing::Test |
| 185 | { |
| 186 | Monero::WalletManager * wmgr; |
| 187 | |
| 188 | WalletTest1() |
| 189 | { |
| 190 | wmgr = Monero::WalletManagerFactory::getWalletManager(); |
| 191 | } |
| 192 | |
| 193 | |
| 194 | }; |
| 195 | |
| 196 | |
| 197 | struct WalletTest2 : public testing::Test |
| 198 | { |
| 199 | Monero::WalletManager * wmgr; |
| 200 | |
| 201 | WalletTest2() |
| 202 | { |
| 203 | wmgr = Monero::WalletManagerFactory::getWalletManager(); |
| 204 | } |
| 205 | |
| 206 | }; |
| 207 | |
| 208 | TEST_F(WalletManagerTest, WalletManagerCreatesWallet) |
| 209 | { |
| 210 | |
| 211 | Monero::Wallet * wallet = wmgr->createWallet(WALLET_NAME, WALLET_PASS, WALLET_LANG, Monero::NetworkType::MAINNET); |
| 212 | ASSERT_TRUE(wallet->status() == Monero::Wallet::Status_Ok); |
| 213 | ASSERT_TRUE(!wallet->seed().empty()); |
| 214 | std::vector<std::string> words; |
| 215 | std::string seed = wallet->seed(); |
| 216 | boost::split(words, seed, boost::is_any_of(" "), boost::token_compress_on); |
| 217 | ASSERT_TRUE(words.size() == 25); |
| 218 | std::cout << "** seed: " << wallet->seed() << std::endl; |
| 219 | ASSERT_FALSE(wallet->mainAddress().empty()); |
| 220 | std::cout << "** address: " << wallet->mainAddress() << std::endl; |
| 221 | ASSERT_TRUE(wmgr->closeWallet(wallet)); |
| 222 | |
| 223 | } |
| 224 | |
| 225 | TEST_F(WalletManagerTest, WalletManagerOpensWallet) |
| 226 | { |
| 227 | |
| 228 | Monero::Wallet * wallet1 = wmgr->createWallet(WALLET_NAME, WALLET_PASS, WALLET_LANG, Monero::NetworkType::MAINNET); |
| 229 | std::string seed1 = wallet1->seed(); |
| 230 | ASSERT_TRUE(wmgr->closeWallet(wallet1)); |
| 231 | Monero::Wallet * wallet2 = wmgr->openWallet(WALLET_NAME, WALLET_PASS, Monero::NetworkType::MAINNET); |
| 232 | ASSERT_TRUE(wallet2->status() == Monero::Wallet::Status_Ok); |
| 233 | ASSERT_TRUE(wallet2->seed() == seed1); |
| 234 | std::cout << "** seed: " << wallet2->seed() << std::endl; |
| 235 | } |
| 236 | |
| 237 | |
| 238 | TEST_F(WalletManagerTest, WalletMaxAmountAsString) |
| 239 | { |
| 240 | LOG_PRINT_L3("max amount: " << Monero::Wallet::displayAmount( |
| 241 | Monero::Wallet::maximumAllowedAmount())); |
| 242 | |
| 243 | } |
| 244 | |
| 245 | |
| 246 | TEST_F(WalletManagerTest, WalletAmountFromString) |
| 247 | { |
| 248 | uint64_t amount = Monero::Wallet::amountFromString("18446740"); |
| 249 | ASSERT_TRUE(amount > 0); |
| 250 | amount = Monero::Wallet::amountFromString("11000000000000"); |
| 251 | ASSERT_FALSE(amount > 0); |
| 252 | amount = Monero::Wallet::amountFromString("0.0"); |
| 253 | ASSERT_FALSE(amount > 0); |
| 254 | amount = Monero::Wallet::amountFromString("10.1"); |
| 255 | ASSERT_TRUE(amount > 0); |
| 256 | |
| 257 | } |
| 258 | |
| 259 | void open_wallet_helper(Monero::WalletManager *wmgr, Monero::Wallet **wallet, const std::string &pass, boost::mutex *mutex) |
| 260 | { |
| 261 | if (mutex) |
| 262 | mutex->lock(); |
| 263 | LOG_PRINT_L3("opening wallet in thread: " << boost::this_thread::get_id()); |
| 264 | *wallet = wmgr->openWallet(WALLET_NAME, pass, Monero::NetworkType::TESTNET); |
| 265 | LOG_PRINT_L3("wallet address: " << (*wallet)->mainAddress()); |
| 266 | LOG_PRINT_L3("wallet status: " << (*wallet)->status()); |
| 267 | LOG_PRINT_L3("closing wallet in thread: " << boost::this_thread::get_id()); |
| 268 | if (mutex) |
| 269 | mutex->unlock(); |
| 270 | } |
| 271 | |
| 272 | |
| 273 | |
| 274 | |
| 275 | //TEST_F(WalletManagerTest, WalletManagerOpensWalletWithPasswordAndReopenMultiThreaded) |
| 276 | //{ |
| 277 | // // create password protected wallet |
| 278 | // std::string wallet_pass = "password"; |
| 279 | // std::string wrong_wallet_pass = "1111"; |
| 280 | // Monero::Wallet * wallet1 = wmgr->createWallet(WALLET_NAME, wallet_pass, WALLET_LANG, Monero::NetworkType::TESTNET); |
| 281 | // std::string seed1 = wallet1->seed(); |
| 282 | // ASSERT_TRUE(wmgr->closeWallet(wallet1)); |
| 283 | |
| 284 | // Monero::Wallet *wallet2 = nullptr; |
| 285 | // Monero::Wallet *wallet3 = nullptr; |
| 286 | |
| 287 | // std::mutex mutex; |
| 288 | // std::thread thread1(open_wallet, wmgr, &wallet2, wrong_wallet_pass, &mutex); |
| 289 | // thread1.join(); |
| 290 | // ASSERT_TRUE(wallet2->status() != Monero::Wallet::Status_Ok); |
| 291 | // ASSERT_TRUE(wmgr->closeWallet(wallet2)); |
| 292 | |
| 293 | // std::thread thread2(open_wallet, wmgr, &wallet3, wallet_pass, &mutex); |
| 294 | // thread2.join(); |
| 295 | |
| 296 | // ASSERT_TRUE(wallet3->status() == Monero::Wallet::Status_Ok); |
| 297 | // ASSERT_TRUE(wmgr->closeWallet(wallet3)); |
| 298 | //} |
| 299 | |
| 300 | |
| 301 | TEST_F(WalletManagerTest, WalletManagerOpensWalletWithPasswordAndReopen) |
| 302 | { |
| 303 | // create password protected wallet |
| 304 | std::string wallet_pass = "password"; |
| 305 | std::string wrong_wallet_pass = "1111"; |
| 306 | Monero::Wallet * wallet1 = wmgr->createWallet(WALLET_NAME, wallet_pass, WALLET_LANG, Monero::NetworkType::TESTNET); |
| 307 | std::string seed1 = wallet1->seed(); |
| 308 | ASSERT_TRUE(wmgr->closeWallet(wallet1)); |
| 309 | |
| 310 | Monero::Wallet *wallet2 = nullptr; |
| 311 | Monero::Wallet *wallet3 = nullptr; |
| 312 | boost::mutex mutex; |
| 313 | |
| 314 | open_wallet_helper(wmgr, &wallet2, wrong_wallet_pass, nullptr); |
| 315 | ASSERT_TRUE(wallet2 != nullptr); |
| 316 | ASSERT_TRUE(wallet2->status() != Monero::Wallet::Status_Ok); |
| 317 | ASSERT_TRUE(wmgr->closeWallet(wallet2)); |
| 318 | |
| 319 | open_wallet_helper(wmgr, &wallet3, wallet_pass, nullptr); |
| 320 | ASSERT_TRUE(wallet3 != nullptr); |
| 321 | ASSERT_TRUE(wallet3->status() == Monero::Wallet::Status_Ok); |
| 322 | ASSERT_TRUE(wmgr->closeWallet(wallet3)); |
| 323 | } |
| 324 | |
| 325 | |
| 326 | TEST_F(WalletManagerTest, WalletManagerStoresWallet) |
| 327 | { |
| 328 | |
| 329 | Monero::Wallet * wallet1 = wmgr->createWallet(WALLET_NAME, WALLET_PASS, WALLET_LANG, Monero::NetworkType::MAINNET); |
| 330 | std::string seed1 = wallet1->seed(); |
| 331 | wallet1->store(""); |
| 332 | ASSERT_TRUE(wmgr->closeWallet(wallet1)); |
| 333 | Monero::Wallet * wallet2 = wmgr->openWallet(WALLET_NAME, WALLET_PASS, Monero::NetworkType::MAINNET); |
| 334 | ASSERT_TRUE(wallet2->status() == Monero::Wallet::Status_Ok); |
| 335 | ASSERT_TRUE(wallet2->seed() == seed1); |
| 336 | } |
| 337 | |
| 338 | |
| 339 | TEST_F(WalletManagerTest, WalletManagerMovesWallet) |
| 340 | { |
| 341 | |
| 342 | Monero::Wallet * wallet1 = wmgr->createWallet(WALLET_NAME, WALLET_PASS, WALLET_LANG, Monero::NetworkType::MAINNET); |
| 343 | std::string WALLET_NAME_MOVED = std::string("/tmp/") + WALLET_NAME + ".moved"; |
| 344 | std::string seed1 = wallet1->seed(); |
| 345 | ASSERT_TRUE(wallet1->store(WALLET_NAME_MOVED)); |
| 346 | |
| 347 | Monero::Wallet * wallet2 = wmgr->openWallet(WALLET_NAME_MOVED, WALLET_PASS, Monero::NetworkType::MAINNET); |
| 348 | ASSERT_TRUE(wallet2->filename() == WALLET_NAME_MOVED); |
| 349 | ASSERT_TRUE(wallet2->keysFilename() == WALLET_NAME_MOVED + ".keys"); |
| 350 | ASSERT_TRUE(wallet2->status() == Monero::Wallet::Status_Ok); |
| 351 | ASSERT_TRUE(wallet2->seed() == seed1); |
| 352 | } |
| 353 | |
| 354 | |
| 355 | TEST_F(WalletManagerTest, WalletManagerChangesPassword) |
| 356 | { |
| 357 | Monero::Wallet * wallet1 = wmgr->createWallet(WALLET_NAME, WALLET_PASS, WALLET_LANG, Monero::NetworkType::MAINNET); |
| 358 | std::string seed1 = wallet1->seed(); |
| 359 | ASSERT_TRUE(wallet1->setPassword(WALLET_PASS2)); |
| 360 | ASSERT_TRUE(wmgr->closeWallet(wallet1)); |
| 361 | Monero::Wallet * wallet2 = wmgr->openWallet(WALLET_NAME, WALLET_PASS2, Monero::NetworkType::MAINNET); |
| 362 | ASSERT_TRUE(wallet2->status() == Monero::Wallet::Status_Ok); |
| 363 | ASSERT_TRUE(wallet2->seed() == seed1); |
| 364 | ASSERT_TRUE(wmgr->closeWallet(wallet2)); |
| 365 | Monero::Wallet * wallet3 = wmgr->openWallet(WALLET_NAME, WALLET_PASS, Monero::NetworkType::MAINNET); |
| 366 | ASSERT_FALSE(wallet3->status() == Monero::Wallet::Status_Ok); |
| 367 | } |
| 368 | |
| 369 | |
| 370 | |
| 371 | TEST_F(WalletManagerTest, WalletManagerRecoversWallet) |
| 372 | { |
| 373 | Monero::Wallet * wallet1 = wmgr->createWallet(WALLET_NAME, WALLET_PASS, WALLET_LANG, Monero::NetworkType::MAINNET); |
| 374 | std::string seed1 = wallet1->seed(); |
| 375 | std::string address1 = wallet1->mainAddress(); |
| 376 | ASSERT_FALSE(address1.empty()); |
| 377 | ASSERT_TRUE(wmgr->closeWallet(wallet1)); |
| 378 | Utils::deleteWallet(WALLET_NAME); |
| 379 | Monero::Wallet * wallet2 = wmgr->recoveryWallet(WALLET_NAME, seed1, Monero::NetworkType::MAINNET); |
| 380 | ASSERT_TRUE(wallet2->status() == Monero::Wallet::Status_Ok); |
| 381 | ASSERT_TRUE(wallet2->seed() == seed1); |
| 382 | ASSERT_TRUE(wallet2->mainAddress() == address1); |
| 383 | ASSERT_TRUE(wmgr->closeWallet(wallet2)); |
| 384 | } |
| 385 | |
| 386 | |
| 387 | TEST_F(WalletManagerTest, WalletManagerStoresWallet1) |
| 388 | { |
| 389 | Monero::Wallet * wallet1 = wmgr->createWallet(WALLET_NAME, WALLET_PASS, WALLET_LANG, Monero::NetworkType::MAINNET); |
| 390 | std::string seed1 = wallet1->seed(); |
| 391 | std::string address1 = wallet1->mainAddress(); |
| 392 | |
| 393 | ASSERT_TRUE(wallet1->store("")); |
| 394 | ASSERT_TRUE(wallet1->store(WALLET_NAME_COPY)); |
| 395 | ASSERT_TRUE(wmgr->closeWallet(wallet1)); |
| 396 | Monero::Wallet * wallet2 = wmgr->openWallet(WALLET_NAME_COPY, WALLET_PASS, Monero::NetworkType::MAINNET); |
| 397 | ASSERT_TRUE(wallet2->status() == Monero::Wallet::Status_Ok); |
| 398 | ASSERT_TRUE(wallet2->seed() == seed1); |
| 399 | ASSERT_TRUE(wallet2->mainAddress() == address1); |
| 400 | ASSERT_TRUE(wmgr->closeWallet(wallet2)); |
| 401 | } |
| 402 | |
| 403 | |
| 404 | TEST_F(WalletManagerTest, WalletManagerStoresWallet2) |
| 405 | { |
| 406 | Monero::Wallet * wallet1 = wmgr->createWallet(WALLET_NAME, WALLET_PASS, WALLET_LANG, Monero::NetworkType::MAINNET); |
| 407 | std::string seed1 = wallet1->seed(); |
| 408 | std::string address1 = wallet1->mainAddress(); |
| 409 | |
| 410 | ASSERT_TRUE(wallet1->store(WALLET_NAME_WITH_DIR)); |
| 411 | ASSERT_TRUE(wmgr->closeWallet(wallet1)); |
| 412 | |
| 413 | wallet1 = wmgr->openWallet(WALLET_NAME_WITH_DIR, WALLET_PASS, Monero::NetworkType::MAINNET); |
| 414 | ASSERT_TRUE(wallet1->status() == Monero::Wallet::Status_Ok); |
| 415 | ASSERT_TRUE(wallet1->seed() == seed1); |
| 416 | ASSERT_TRUE(wallet1->mainAddress() == address1); |
| 417 | ASSERT_TRUE(wmgr->closeWallet(wallet1)); |
| 418 | } |
| 419 | |
| 420 | |
| 421 | TEST_F(WalletManagerTest, WalletManagerStoresWallet3) |
| 422 | { |
| 423 | Monero::Wallet * wallet1 = wmgr->createWallet(WALLET_NAME, WALLET_PASS, WALLET_LANG, Monero::NetworkType::MAINNET); |
| 424 | std::string seed1 = wallet1->seed(); |
| 425 | std::string address1 = wallet1->mainAddress(); |
| 426 | |
| 427 | ASSERT_FALSE(wallet1->store(WALLET_NAME_WITH_DIR_NON_WRITABLE)); |
| 428 | ASSERT_TRUE(wmgr->closeWallet(wallet1)); |
| 429 | |
| 430 | wallet1 = wmgr->openWallet(WALLET_NAME_WITH_DIR_NON_WRITABLE, WALLET_PASS, Monero::NetworkType::MAINNET); |
| 431 | ASSERT_FALSE(wallet1->status() == Monero::Wallet::Status_Ok); |
| 432 | |
| 433 | // "close" always returns true; |
| 434 | ASSERT_TRUE(wmgr->closeWallet(wallet1)); |
| 435 | |
| 436 | wallet1 = wmgr->openWallet(WALLET_NAME, WALLET_PASS, Monero::NetworkType::MAINNET); |
| 437 | ASSERT_TRUE(wallet1->status() == Monero::Wallet::Status_Ok); |
| 438 | ASSERT_TRUE(wallet1->seed() == seed1); |
| 439 | ASSERT_TRUE(wallet1->mainAddress() == address1); |
| 440 | ASSERT_TRUE(wmgr->closeWallet(wallet1)); |
| 441 | |
| 442 | } |
| 443 | |
| 444 | |
| 445 | TEST_F(WalletManagerTest, WalletManagerStoresWallet4) |
| 446 | { |
| 447 | Monero::Wallet * wallet1 = wmgr->createWallet(WALLET_NAME, WALLET_PASS, WALLET_LANG, Monero::NetworkType::MAINNET); |
| 448 | std::string seed1 = wallet1->seed(); |
| 449 | std::string address1 = wallet1->mainAddress(); |
| 450 | |
| 451 | ASSERT_TRUE(wallet1->store("")); |
| 452 | ASSERT_TRUE(wallet1->status() == Monero::Wallet::Status_Ok); |
| 453 | |
| 454 | ASSERT_TRUE(wallet1->store("")); |
| 455 | ASSERT_TRUE(wallet1->status() == Monero::Wallet::Status_Ok); |
| 456 | |
| 457 | ASSERT_TRUE(wmgr->closeWallet(wallet1)); |
| 458 | |
| 459 | wallet1 = wmgr->openWallet(WALLET_NAME, WALLET_PASS, Monero::NetworkType::MAINNET); |
| 460 | ASSERT_TRUE(wallet1->status() == Monero::Wallet::Status_Ok); |
| 461 | ASSERT_TRUE(wallet1->seed() == seed1); |
| 462 | ASSERT_TRUE(wallet1->mainAddress() == address1); |
| 463 | ASSERT_TRUE(wmgr->closeWallet(wallet1)); |
| 464 | } |
| 465 | |
| 466 | |
| 467 | |
| 468 | |
| 469 | TEST_F(WalletManagerTest, WalletManagerFindsWallet) |
| 470 | { |
| 471 | std::vector<std::string> wallets = wmgr->findWallets(WALLETS_ROOT_DIR); |
| 472 | ASSERT_FALSE(wallets.empty()); |
| 473 | std::cout << "Found wallets: " << std::endl; |
| 474 | for (auto wallet_path: wallets) { |
| 475 | std::cout << wallet_path << std::endl; |
| 476 | } |
| 477 | } |
| 478 | |
| 479 | |
| 480 | TEST_F(WalletTest1, WalletGeneratesPaymentId) |
| 481 | { |
| 482 | std::string payment_id = Monero::Wallet::genPaymentId(); |
| 483 | ASSERT_TRUE(payment_id.length() == 16); |
| 484 | } |
| 485 | |
| 486 | |
| 487 | TEST_F(WalletTest1, WalletGeneratesIntegratedAddress) |
| 488 | { |
| 489 | std::string payment_id = Monero::Wallet::genPaymentId(); |
| 490 | |
| 491 | Monero::Wallet * wallet1 = wmgr->openWallet(CURRENT_SRC_WALLET, TESTNET_WALLET_PASS, Monero::NetworkType::TESTNET); |
| 492 | std::string integrated_address = wallet1->integratedAddress(payment_id); |
| 493 | ASSERT_TRUE(integrated_address.length() == 106); |
| 494 | } |
| 495 | |
| 496 | |
| 497 | TEST_F(WalletTest1, WalletShowsBalance) |
| 498 | { |
| 499 | Monero::Wallet * wallet1 = wmgr->openWallet(CURRENT_SRC_WALLET, TESTNET_WALLET_PASS, Monero::NetworkType::TESTNET); |
| 500 | ASSERT_TRUE(wallet1->balance(0) > 0); |
| 501 | ASSERT_TRUE(wallet1->unlockedBalance(0) > 0); |
| 502 | |
| 503 | uint64_t balance1 = wallet1->balance(0); |
| 504 | uint64_t unlockedBalance1 = wallet1->unlockedBalance(0); |
| 505 | ASSERT_TRUE(wmgr->closeWallet(wallet1)); |
| 506 | Monero::Wallet * wallet2 = wmgr->openWallet(CURRENT_SRC_WALLET, TESTNET_WALLET_PASS, Monero::NetworkType::TESTNET); |
| 507 | |
| 508 | ASSERT_TRUE(balance1 == wallet2->balance(0)); |
| 509 | std::cout << "wallet balance: " << wallet2->balance(0) << std::endl; |
| 510 | ASSERT_TRUE(unlockedBalance1 == wallet2->unlockedBalance(0)); |
| 511 | std::cout << "wallet unlocked balance: " << wallet2->unlockedBalance(0) << std::endl; |
| 512 | ASSERT_TRUE(wmgr->closeWallet(wallet2)); |
| 513 | } |
| 514 | |
| 515 | TEST_F(WalletTest1, WalletReturnsCurrentBlockHeight) |
| 516 | { |
| 517 | Monero::Wallet * wallet1 = wmgr->openWallet(CURRENT_SRC_WALLET, TESTNET_WALLET_PASS, Monero::NetworkType::TESTNET); |
| 518 | ASSERT_TRUE(wallet1->blockChainHeight() > 0); |
| 519 | wmgr->closeWallet(wallet1); |
| 520 | } |
| 521 | |
| 522 | |
| 523 | TEST_F(WalletTest1, WalletReturnsDaemonBlockHeight) |
| 524 | { |
| 525 | Monero::Wallet * wallet1 = wmgr->openWallet(CURRENT_SRC_WALLET, TESTNET_WALLET_PASS, Monero::NetworkType::TESTNET); |
| 526 | // wallet not connected to daemon |
| 527 | ASSERT_TRUE(wallet1->daemonBlockChainHeight() == 0); |
| 528 | ASSERT_TRUE(wallet1->status() != Monero::Wallet::Status_Ok); |
| 529 | ASSERT_FALSE(wallet1->errorString().empty()); |
| 530 | wmgr->closeWallet(wallet1); |
| 531 | |
| 532 | wallet1 = wmgr->openWallet(CURRENT_SRC_WALLET, TESTNET_WALLET_PASS, Monero::NetworkType::TESTNET); |
| 533 | // wallet connected to daemon |
| 534 | wallet1->init(TESTNET_DAEMON_ADDRESS, 0); |
| 535 | ASSERT_TRUE(wallet1->daemonBlockChainHeight() > 0); |
| 536 | std::cout << "daemonBlockChainHeight: " << wallet1->daemonBlockChainHeight() << std::endl; |
| 537 | wmgr->closeWallet(wallet1); |
| 538 | } |
| 539 | |
| 540 | |
| 541 | TEST_F(WalletTest1, WalletRefresh) |
| 542 | { |
| 543 | |
| 544 | std::cout << "Opening wallet: " << CURRENT_SRC_WALLET << std::endl; |
| 545 | Monero::Wallet * wallet1 = wmgr->openWallet(CURRENT_SRC_WALLET, TESTNET_WALLET_PASS, Monero::NetworkType::TESTNET); |
| 546 | // make sure testnet daemon is running |
| 547 | std::cout << "connecting to daemon: " << TESTNET_DAEMON_ADDRESS << std::endl; |
| 548 | ASSERT_TRUE(wallet1->init(TESTNET_DAEMON_ADDRESS, 0)); |
| 549 | ASSERT_TRUE(wallet1->refresh()); |
| 550 | ASSERT_TRUE(wmgr->closeWallet(wallet1)); |
| 551 | } |
| 552 | |
| 553 | TEST_F(WalletTest1, WalletConvertsToString) |
| 554 | { |
| 555 | std::string strAmount = Monero::Wallet::displayAmount(AMOUNT_5XMR); |
| 556 | ASSERT_TRUE(AMOUNT_5XMR == Monero::Wallet::amountFromString(strAmount)); |
| 557 | |
| 558 | ASSERT_TRUE(AMOUNT_5XMR == Monero::Wallet::amountFromDouble(5.0)); |
| 559 | ASSERT_TRUE(AMOUNT_10XMR == Monero::Wallet::amountFromDouble(10.0)); |
| 560 | ASSERT_TRUE(AMOUNT_1XMR == Monero::Wallet::amountFromDouble(1.0)); |
| 561 | |
| 562 | } |
| 563 | |
| 564 | |
| 565 | |
| 566 | TEST_F(WalletTest1, WalletTransaction) |
| 567 | |
| 568 | { |
| 569 | Monero::Wallet * wallet1 = wmgr->openWallet(CURRENT_SRC_WALLET, TESTNET_WALLET_PASS, Monero::NetworkType::TESTNET); |
| 570 | // make sure testnet daemon is running |
| 571 | ASSERT_TRUE(wallet1->init(TESTNET_DAEMON_ADDRESS, 0)); |
| 572 | ASSERT_TRUE(wallet1->refresh()); |
| 573 | uint64_t balance = wallet1->balance(0); |
| 574 | ASSERT_TRUE(wallet1->status() == Monero::PendingTransaction::Status_Ok); |
| 575 | |
| 576 | std::string recepient_address = Utils::get_wallet_address(CURRENT_DST_WALLET, TESTNET_WALLET_PASS); |
| 577 | const int MIXIN_COUNT = 4; |
| 578 | |
| 579 | |
| 580 | Monero::PendingTransaction * transaction = wallet1->createTransaction(recepient_address, |
| 581 | PAYMENT_ID_EMPTY, |
| 582 | AMOUNT_10XMR, |
| 583 | MIXIN_COUNT, |
| 584 | Monero::PendingTransaction::Priority_Medium, |
| 585 | 0, |
| 586 | std::set<uint32_t>{}); |
| 587 | ASSERT_TRUE(transaction->status() == Monero::PendingTransaction::Status_Ok); |
| 588 | wallet1->refresh(); |
| 589 | |
| 590 | ASSERT_TRUE(wallet1->balance(0) == balance); |
| 591 | ASSERT_TRUE(transaction->amount() == AMOUNT_10XMR); |
| 592 | ASSERT_TRUE(transaction->commit()); |
| 593 | ASSERT_FALSE(wallet1->balance(0) == balance); |
| 594 | ASSERT_TRUE(wmgr->closeWallet(wallet1)); |
| 595 | } |
| 596 | |
| 597 | |
| 598 | |
| 599 | TEST_F(WalletTest1, WalletTransactionWithMixin) |
| 600 | { |
| 601 | |
| 602 | std::vector<int> mixins; |
| 603 | // 2,3,4,5,6,7,8,9,10,15,20,25 can we do it like that? |
| 604 | mixins.push_back(2); mixins.push_back(3); mixins.push_back(4); mixins.push_back(5); mixins.push_back(6); |
| 605 | mixins.push_back(7); mixins.push_back(8); mixins.push_back(9); mixins.push_back(10); mixins.push_back(15); |
| 606 | mixins.push_back(20); mixins.push_back(25); |
| 607 | |
| 608 | |
| 609 | std::string payment_id = ""; |
| 610 | |
| 611 | Monero::Wallet * wallet1 = wmgr->openWallet(CURRENT_SRC_WALLET, TESTNET_WALLET_PASS, Monero::NetworkType::TESTNET); |
| 612 | |
| 613 | |
| 614 | // make sure testnet daemon is running |
| 615 | ASSERT_TRUE(wallet1->init(TESTNET_DAEMON_ADDRESS, 0)); |
| 616 | ASSERT_TRUE(wallet1->refresh()); |
| 617 | uint64_t balance = wallet1->balance(0); |
| 618 | ASSERT_TRUE(wallet1->status() == Monero::PendingTransaction::Status_Ok); |
| 619 | |
| 620 | std::string recepient_address = Utils::get_wallet_address(CURRENT_DST_WALLET, TESTNET_WALLET_PASS); |
| 621 | for (auto mixin : mixins) { |
| 622 | std::cerr << "Transaction mixin count: " << mixin << std::endl; |
| 623 | |
| 624 | Monero::PendingTransaction * transaction = wallet1->createTransaction( |
| 625 | recepient_address, payment_id, AMOUNT_5XMR, mixin, Monero::PendingTransaction::Priority_Medium, 0, std::set<uint32_t>{}); |
| 626 | |
| 627 | std::cerr << "Transaction status: " << transaction->status() << std::endl; |
| 628 | std::cerr << "Transaction fee: " << Monero::Wallet::displayAmount(transaction->fee()) << std::endl; |
| 629 | std::cerr << "Transaction error: " << transaction->errorString() << std::endl; |
| 630 | ASSERT_TRUE(transaction->status() == Monero::PendingTransaction::Status_Ok); |
| 631 | wallet1->disposeTransaction(transaction); |
| 632 | } |
| 633 | |
| 634 | wallet1->refresh(); |
| 635 | |
| 636 | ASSERT_TRUE(wallet1->balance(0) == balance); |
| 637 | ASSERT_TRUE(wmgr->closeWallet(wallet1)); |
| 638 | } |
| 639 | |
| 640 | TEST_F(WalletTest1, WalletTransactionWithPriority) |
| 641 | { |
| 642 | |
| 643 | std::string payment_id = ""; |
| 644 | |
| 645 | Monero::Wallet * wallet1 = wmgr->openWallet(CURRENT_SRC_WALLET, TESTNET_WALLET_PASS, Monero::NetworkType::TESTNET); |
| 646 | |
| 647 | // make sure testnet daemon is running |
| 648 | ASSERT_TRUE(wallet1->init(TESTNET_DAEMON_ADDRESS, 0)); |
| 649 | ASSERT_TRUE(wallet1->refresh()); |
| 650 | uint64_t balance = wallet1->balance(0); |
| 651 | ASSERT_TRUE(wallet1->status() == Monero::PendingTransaction::Status_Ok); |
| 652 | |
| 653 | std::string recepient_address = Utils::get_wallet_address(CURRENT_DST_WALLET, TESTNET_WALLET_PASS); |
| 654 | uint32_t mixin = 2; |
| 655 | uint64_t fee = 0; |
| 656 | |
| 657 | std::vector<Monero::PendingTransaction::Priority> priorities = { |
| 658 | Monero::PendingTransaction::Priority_Low, |
| 659 | Monero::PendingTransaction::Priority_Medium, |
| 660 | Monero::PendingTransaction::Priority_High |
| 661 | }; |
| 662 | |
| 663 | for (auto it = priorities.begin(); it != priorities.end(); ++it) { |
| 664 | std::cerr << "Transaction priority: " << *it << std::endl; |
| 665 | |
| 666 | Monero::PendingTransaction * transaction = wallet1->createTransaction( |
| 667 | recepient_address, payment_id, AMOUNT_5XMR, mixin, *it, 0, std::set<uint32_t>{}); |
| 668 | std::cerr << "Transaction status: " << transaction->status() << std::endl; |
| 669 | std::cerr << "Transaction fee: " << Monero::Wallet::displayAmount(transaction->fee()) << std::endl; |
| 670 | std::cerr << "Transaction error: " << transaction->errorString() << std::endl; |
| 671 | ASSERT_TRUE(transaction->fee() > fee); |
| 672 | ASSERT_TRUE(transaction->status() == Monero::PendingTransaction::Status_Ok); |
| 673 | fee = transaction->fee(); |
| 674 | wallet1->disposeTransaction(transaction); |
| 675 | } |
| 676 | wallet1->refresh(); |
| 677 | ASSERT_TRUE(wallet1->balance(0) == balance); |
| 678 | ASSERT_TRUE(wmgr->closeWallet(wallet1)); |
| 679 | } |
| 680 | |
| 681 | |
| 682 | |
| 683 | TEST_F(WalletTest1, WalletHistory) |
| 684 | { |
| 685 | Monero::Wallet * wallet1 = wmgr->openWallet(CURRENT_SRC_WALLET, TESTNET_WALLET_PASS, Monero::NetworkType::TESTNET); |
| 686 | // make sure testnet daemon is running |
| 687 | ASSERT_TRUE(wallet1->init(TESTNET_DAEMON_ADDRESS, 0)); |
| 688 | ASSERT_TRUE(wallet1->refresh()); |
| 689 | Monero::TransactionHistory * history = wallet1->history(); |
| 690 | history->refresh(); |
| 691 | ASSERT_TRUE(history->count() > 0); |
| 692 | |
| 693 | |
| 694 | for (auto t: history->getAll()) { |
| 695 | ASSERT_TRUE(t != nullptr); |
| 696 | Utils::print_transaction(t); |
| 697 | } |
| 698 | } |
| 699 | |
| 700 | TEST_F(WalletTest1, WalletTransactionAndHistory) |
| 701 | { |
| 702 | return; |
| 703 | Monero::Wallet * wallet_src = wmgr->openWallet(CURRENT_SRC_WALLET, TESTNET_WALLET_PASS, Monero::NetworkType::TESTNET); |
| 704 | // make sure testnet daemon is running |
| 705 | ASSERT_TRUE(wallet_src->init(TESTNET_DAEMON_ADDRESS, 0)); |
| 706 | ASSERT_TRUE(wallet_src->refresh()); |
| 707 | Monero::TransactionHistory * history = wallet_src->history(); |
| 708 | history->refresh(); |
| 709 | ASSERT_TRUE(history->count() > 0); |
| 710 | size_t count1 = history->count(); |
| 711 | |
| 712 | std::cout << "**** Transactions before transfer (" << count1 << ")" << std::endl; |
| 713 | for (auto t: history->getAll()) { |
| 714 | ASSERT_TRUE(t != nullptr); |
| 715 | Utils::print_transaction(t); |
| 716 | } |
| 717 | |
| 718 | std::string wallet4_addr = Utils::get_wallet_address(CURRENT_DST_WALLET, TESTNET_WALLET_PASS); |
| 719 | |
| 720 | |
| 721 | Monero::PendingTransaction * tx = wallet_src->createTransaction(wallet4_addr, |
| 722 | PAYMENT_ID_EMPTY, |
| 723 | AMOUNT_10XMR * 5, 1, Monero::PendingTransaction::Priority_Medium, 0, std::set<uint32_t>{}); |
| 724 | |
| 725 | ASSERT_TRUE(tx->status() == Monero::PendingTransaction::Status_Ok); |
| 726 | ASSERT_TRUE(tx->commit()); |
| 727 | history = wallet_src->history(); |
| 728 | history->refresh(); |
| 729 | ASSERT_TRUE(count1 != history->count()); |
| 730 | |
| 731 | std::cout << "**** Transactions after transfer (" << history->count() << ")" << std::endl; |
| 732 | for (auto t: history->getAll()) { |
| 733 | ASSERT_TRUE(t != nullptr); |
| 734 | Utils::print_transaction(t); |
| 735 | } |
| 736 | } |
| 737 | |
| 738 | |
| 739 | TEST_F(WalletTest1, WalletTransactionWithPaymentId) |
| 740 | { |
| 741 | |
| 742 | Monero::Wallet * wallet_src = wmgr->openWallet(CURRENT_SRC_WALLET, TESTNET_WALLET_PASS, Monero::NetworkType::TESTNET); |
| 743 | // make sure testnet daemon is running |
| 744 | ASSERT_TRUE(wallet_src->init(TESTNET_DAEMON_ADDRESS, 0)); |
| 745 | ASSERT_TRUE(wallet_src->refresh()); |
| 746 | Monero::TransactionHistory * history = wallet_src->history(); |
| 747 | history->refresh(); |
| 748 | ASSERT_TRUE(history->count() > 0); |
| 749 | size_t count1 = history->count(); |
| 750 | |
| 751 | std::cout << "**** Transactions before transfer (" << count1 << ")" << std::endl; |
| 752 | for (auto t: history->getAll()) { |
| 753 | ASSERT_TRUE(t != nullptr); |
| 754 | Utils::print_transaction(t); |
| 755 | } |
| 756 | |
| 757 | std::string wallet4_addr = Utils::get_wallet_address(CURRENT_DST_WALLET, TESTNET_WALLET_PASS); |
| 758 | |
| 759 | std::string payment_id = Monero::Wallet::genPaymentId(); |
| 760 | ASSERT_TRUE(payment_id.length() == 16); |
| 761 | |
| 762 | |
| 763 | Monero::PendingTransaction * tx = wallet_src->createTransaction(wallet4_addr, |
| 764 | payment_id, |
| 765 | AMOUNT_1XMR, 1, Monero::PendingTransaction::Priority_Medium, 0, std::set<uint32_t>{}); |
| 766 | |
| 767 | ASSERT_TRUE(tx->status() == Monero::PendingTransaction::Status_Ok); |
| 768 | ASSERT_TRUE(tx->commit()); |
| 769 | history = wallet_src->history(); |
| 770 | history->refresh(); |
| 771 | ASSERT_TRUE(count1 != history->count()); |
| 772 | |
| 773 | bool payment_id_in_history = false; |
| 774 | |
| 775 | std::cout << "**** Transactions after transfer (" << history->count() << ")" << std::endl; |
| 776 | for (auto t: history->getAll()) { |
| 777 | ASSERT_TRUE(t != nullptr); |
| 778 | Utils::print_transaction(t); |
| 779 | if (t->paymentId() == payment_id) { |
| 780 | payment_id_in_history = true; |
| 781 | } |
| 782 | } |
| 783 | |
| 784 | ASSERT_TRUE(payment_id_in_history); |
| 785 | } |
| 786 | |
| 787 | |
| 788 | struct MyWalletListener : public Monero::WalletListener |
| 789 | { |
| 790 | |
| 791 | Monero::Wallet * wallet; |
| 792 | uint64_t total_tx; |
| 793 | uint64_t total_rx; |
| 794 | boost::mutex mutex; |
| 795 | boost::condition_variable cv_send; |
| 796 | boost::condition_variable cv_receive; |
| 797 | boost::condition_variable cv_update; |
| 798 | boost::condition_variable cv_refresh; |
| 799 | boost::condition_variable cv_newblock; |
| 800 | bool send_triggered; |
| 801 | bool receive_triggered; |
| 802 | bool newblock_triggered; |
| 803 | bool update_triggered; |
| 804 | bool refresh_triggered; |
| 805 | |
| 806 | |
| 807 | |
| 808 | MyWalletListener(Monero::Wallet * wallet) |
| 809 | : total_tx(0), total_rx(0) |
| 810 | { |
| 811 | reset(); |
| 812 | |
| 813 | this->wallet = wallet; |
| 814 | this->wallet->setListener(this); |
| 815 | } |
| 816 | |
| 817 | void reset() |
| 818 | { |
| 819 | send_triggered = receive_triggered = newblock_triggered = update_triggered = refresh_triggered = false; |
| 820 | } |
| 821 | |
| 822 | virtual void moneySpent(const string &txId, uint64_t amount) |
| 823 | { |
| 824 | std::cerr << "wallet: " << wallet->mainAddress() << "**** just spent money (" |
| 825 | << txId << ", " << wallet->displayAmount(amount) << ")" << std::endl; |
| 826 | total_tx += amount; |
| 827 | send_triggered = true; |
| 828 | cv_send.notify_one(); |
| 829 | } |
| 830 | |
| 831 | virtual void moneyReceived(const string &txId, uint64_t amount) |
| 832 | { |
| 833 | std::cout << "wallet: " << wallet->mainAddress() << "**** just received money (" |
| 834 | << txId << ", " << wallet->displayAmount(amount) << ")" << std::endl; |
| 835 | total_rx += amount; |
| 836 | receive_triggered = true; |
| 837 | cv_receive.notify_one(); |
| 838 | } |
| 839 | |
| 840 | virtual void unconfirmedMoneyReceived(const string &txId, uint64_t amount) |
| 841 | { |
| 842 | std::cout << "wallet: " << wallet->mainAddress() << "**** just received unconfirmed money (" |
| 843 | << txId << ", " << wallet->displayAmount(amount) << ")" << std::endl; |
| 844 | // Don't trigger receive until tx is mined |
| 845 | // total_rx += amount; |
| 846 | // receive_triggered = true; |
| 847 | // cv_receive.notify_one(); |
| 848 | } |
| 849 | |
| 850 | virtual void newBlock(uint64_t height) |
| 851 | { |
| 852 | // std::cout << "wallet: " << wallet->mainAddress() |
| 853 | // <<", new block received, blockHeight: " << height << std::endl; |
| 854 | static int bc_height = wallet->daemonBlockChainHeight(); |
| 855 | std::cout << height |
| 856 | << " / " << bc_height/* 0*/ |
| 857 | << std::endl; |
| 858 | newblock_triggered = true; |
| 859 | cv_newblock.notify_one(); |
| 860 | } |
| 861 | |
| 862 | virtual void updated() |
| 863 | { |
| 864 | std::cout << __FUNCTION__ << "Wallet updated"; |
| 865 | update_triggered = true; |
| 866 | cv_update.notify_one(); |
| 867 | } |
| 868 | |
| 869 | virtual void refreshed() |
| 870 | { |
| 871 | std::cout << __FUNCTION__ << "Wallet refreshed"; |
| 872 | refresh_triggered = true; |
| 873 | cv_refresh.notify_one(); |
| 874 | } |
| 875 | |
| 876 | }; |
| 877 | |
| 878 | |
| 879 | |
| 880 | |
| 881 | TEST_F(WalletTest2, WalletCallBackRefreshedSync) |
| 882 | { |
| 883 | |
| 884 | Monero::Wallet * wallet_src = wmgr->openWallet(CURRENT_SRC_WALLET, TESTNET_WALLET_PASS, Monero::NetworkType::TESTNET); |
| 885 | MyWalletListener * wallet_src_listener = new MyWalletListener(wallet_src); |
| 886 | ASSERT_TRUE(wallet_src->init(TESTNET_DAEMON_ADDRESS, 0)); |
| 887 | ASSERT_TRUE(wallet_src_listener->refresh_triggered); |
| 888 | ASSERT_TRUE(wallet_src->connected()); |
| 889 | boost::chrono::seconds wait_for = boost::chrono::seconds(60*3); |
| 890 | boost::unique_lock<boost::mutex> lock (wallet_src_listener->mutex); |
| 891 | wallet_src_listener->cv_refresh.wait_for(lock, wait_for); |
| 892 | wmgr->closeWallet(wallet_src); |
| 893 | } |
| 894 | |
| 895 | |
| 896 | |
| 897 | |
| 898 | TEST_F(WalletTest2, WalletCallBackRefreshedAsync) |
| 899 | { |
| 900 | |
| 901 | Monero::Wallet * wallet_src = wmgr->openWallet(CURRENT_SRC_WALLET, TESTNET_WALLET_PASS, Monero::NetworkType::TESTNET); |
| 902 | MyWalletListener * wallet_src_listener = new MyWalletListener(wallet_src); |
| 903 | |
| 904 | boost::chrono::seconds wait_for = boost::chrono::seconds(20); |
| 905 | boost::unique_lock<boost::mutex> lock (wallet_src_listener->mutex); |
| 906 | wallet_src->init(MAINNET_DAEMON_ADDRESS, 0); |
| 907 | wallet_src->startRefresh(); |
| 908 | std::cerr << "TEST: waiting on refresh lock...\n"; |
| 909 | wallet_src_listener->cv_refresh.wait_for(lock, wait_for); |
| 910 | std::cerr << "TEST: refresh lock acquired...\n"; |
| 911 | ASSERT_TRUE(wallet_src_listener->refresh_triggered); |
| 912 | ASSERT_TRUE(wallet_src->connected()); |
| 913 | std::cerr << "TEST: closing wallet...\n"; |
| 914 | wmgr->closeWallet(wallet_src); |
| 915 | } |
| 916 | |
| 917 | |
| 918 | |
| 919 | |
| 920 | TEST_F(WalletTest2, WalletCallbackSent) |
| 921 | { |
| 922 | |
| 923 | Monero::Wallet * wallet_src = wmgr->openWallet(CURRENT_SRC_WALLET, TESTNET_WALLET_PASS, Monero::NetworkType::TESTNET); |
| 924 | // make sure testnet daemon is running |
| 925 | ASSERT_TRUE(wallet_src->init(TESTNET_DAEMON_ADDRESS, 0)); |
| 926 | ASSERT_TRUE(wallet_src->refresh()); |
| 927 | MyWalletListener * wallet_src_listener = new MyWalletListener(wallet_src); |
| 928 | uint64_t balance = wallet_src->balance(0); |
| 929 | std::cout << "** Balance: " << wallet_src->displayAmount(wallet_src->balance(0)) << std::endl; |
| 930 | Monero::Wallet * wallet_dst = wmgr->openWallet(CURRENT_DST_WALLET, TESTNET_WALLET_PASS, Monero::NetworkType::TESTNET); |
| 931 | |
| 932 | uint64_t amount = AMOUNT_1XMR * 5; |
| 933 | std::cout << "** Sending " << Monero::Wallet::displayAmount(amount) << " to " << wallet_dst->mainAddress(); |
| 934 | |
| 935 | |
| 936 | Monero::PendingTransaction * tx = wallet_src->createTransaction(wallet_dst->mainAddress(), |
| 937 | PAYMENT_ID_EMPTY, |
| 938 | amount, 1, Monero::PendingTransaction::Priority_Medium, 0, std::set<uint32_t>{}); |
| 939 | std::cout << "** Committing transaction: " << Monero::Wallet::displayAmount(tx->amount()) |
| 940 | << " with fee: " << Monero::Wallet::displayAmount(tx->fee()); |
| 941 | |
| 942 | ASSERT_TRUE(tx->status() == Monero::PendingTransaction::Status_Ok); |
| 943 | ASSERT_TRUE(tx->commit()); |
| 944 | |
| 945 | boost::chrono::seconds wait_for = boost::chrono::seconds(60*3); |
| 946 | boost::unique_lock<boost::mutex> lock (wallet_src_listener->mutex); |
| 947 | std::cerr << "TEST: waiting on send lock...\n"; |
| 948 | wallet_src_listener->cv_send.wait_for(lock, wait_for); |
| 949 | std::cerr << "TEST: send lock acquired...\n"; |
| 950 | ASSERT_TRUE(wallet_src_listener->send_triggered); |
| 951 | ASSERT_TRUE(wallet_src_listener->update_triggered); |
| 952 | std::cout << "** Balance: " << wallet_src->displayAmount(wallet_src->balance(0)) << std::endl; |
| 953 | ASSERT_TRUE(wallet_src->balance(0) < balance); |
| 954 | wmgr->closeWallet(wallet_src); |
| 955 | wmgr->closeWallet(wallet_dst); |
| 956 | } |
| 957 | |
| 958 | |
| 959 | TEST_F(WalletTest2, WalletCallbackReceived) |
| 960 | { |
| 961 | |
| 962 | Monero::Wallet * wallet_src = wmgr->openWallet(CURRENT_SRC_WALLET, TESTNET_WALLET_PASS, Monero::NetworkType::TESTNET); |
| 963 | // make sure testnet daemon is running |
| 964 | ASSERT_TRUE(wallet_src->init(TESTNET_DAEMON_ADDRESS, 0)); |
| 965 | ASSERT_TRUE(wallet_src->refresh()); |
| 966 | std::cout << "** Balance src1: " << wallet_src->displayAmount(wallet_src->balance(0)) << std::endl; |
| 967 | |
| 968 | Monero::Wallet * wallet_dst = wmgr->openWallet(CURRENT_DST_WALLET, TESTNET_WALLET_PASS, Monero::NetworkType::TESTNET); |
| 969 | ASSERT_TRUE(wallet_dst->init(TESTNET_DAEMON_ADDRESS, 0)); |
| 970 | ASSERT_TRUE(wallet_dst->refresh()); |
| 971 | uint64_t balance = wallet_dst->balance(0); |
| 972 | std::cout << "** Balance dst1: " << wallet_dst->displayAmount(wallet_dst->balance(0)) << std::endl; |
| 973 | std::unique_ptr<MyWalletListener> wallet_dst_listener (new MyWalletListener(wallet_dst)); |
| 974 | |
| 975 | uint64_t amount = AMOUNT_1XMR * 5; |
| 976 | std::cout << "** Sending " << Monero::Wallet::displayAmount(amount) << " to " << wallet_dst->mainAddress(); |
| 977 | Monero::PendingTransaction * tx = wallet_src->createTransaction(wallet_dst->mainAddress(), |
| 978 | PAYMENT_ID_EMPTY, |
| 979 | amount, 1, Monero::PendingTransaction::Priority_Medium, 0, std::set<uint32_t>{}); |
| 980 | |
| 981 | std::cout << "** Committing transaction: " << Monero::Wallet::displayAmount(tx->amount()) |
| 982 | << " with fee: " << Monero::Wallet::displayAmount(tx->fee()); |
| 983 | |
| 984 | ASSERT_TRUE(tx->status() == Monero::PendingTransaction::Status_Ok); |
| 985 | ASSERT_TRUE(tx->commit()); |
| 986 | |
| 987 | boost::chrono::seconds wait_for = boost::chrono::seconds(60*4); |
| 988 | boost::unique_lock<boost::mutex> lock (wallet_dst_listener->mutex); |
| 989 | std::cerr << "TEST: waiting on receive lock...\n"; |
| 990 | wallet_dst_listener->cv_receive.wait_for(lock, wait_for); |
| 991 | std::cerr << "TEST: receive lock acquired...\n"; |
| 992 | ASSERT_TRUE(wallet_dst_listener->receive_triggered); |
| 993 | ASSERT_TRUE(wallet_dst_listener->update_triggered); |
| 994 | |
| 995 | std::cout << "** Balance src2: " << wallet_dst->displayAmount(wallet_src->balance(0)) << std::endl; |
| 996 | std::cout << "** Balance dst2: " << wallet_dst->displayAmount(wallet_dst->balance(0)) << std::endl; |
| 997 | |
| 998 | ASSERT_TRUE(wallet_dst->balance(0) > balance); |
| 999 | |
| 1000 | wmgr->closeWallet(wallet_src); |
| 1001 | wmgr->closeWallet(wallet_dst); |
| 1002 | } |
| 1003 | |
| 1004 | |
| 1005 | |
| 1006 | TEST_F(WalletTest2, WalletCallbackNewBlock) |
| 1007 | { |
| 1008 | |
| 1009 | Monero::Wallet * wallet_src = wmgr->openWallet(TESTNET_WALLET5_NAME, TESTNET_WALLET_PASS, Monero::NetworkType::TESTNET); |
| 1010 | // make sure testnet daemon is running |
| 1011 | ASSERT_TRUE(wallet_src->init(TESTNET_DAEMON_ADDRESS, 0)); |
| 1012 | ASSERT_TRUE(wallet_src->refresh()); |
| 1013 | uint64_t bc1 = wallet_src->blockChainHeight(); |
| 1014 | std::cout << "** Block height: " << bc1 << std::endl; |
| 1015 | |
| 1016 | |
| 1017 | std::unique_ptr<MyWalletListener> wallet_listener (new MyWalletListener(wallet_src)); |
| 1018 | |
| 1019 | // wait max 4 min for new block |
| 1020 | boost::chrono::seconds wait_for = boost::chrono::seconds(60*4); |
| 1021 | boost::unique_lock<boost::mutex> lock (wallet_listener->mutex); |
| 1022 | std::cerr << "TEST: waiting on newblock lock...\n"; |
| 1023 | wallet_listener->cv_newblock.wait_for(lock, wait_for); |
| 1024 | std::cerr << "TEST: newblock lock acquired...\n"; |
| 1025 | ASSERT_TRUE(wallet_listener->newblock_triggered); |
| 1026 | uint64_t bc2 = wallet_src->blockChainHeight(); |
| 1027 | std::cout << "** Block height: " << bc2 << std::endl; |
| 1028 | ASSERT_TRUE(bc2 > bc1); |
| 1029 | wmgr->closeWallet(wallet_src); |
| 1030 | |
| 1031 | } |
| 1032 | |
| 1033 | TEST_F(WalletManagerMainnetTest, CreateOpenAndRefreshWalletMainNetSync) |
| 1034 | { |
| 1035 | |
| 1036 | Monero::Wallet * wallet = wmgr->createWallet(WALLET_NAME_MAINNET, "", WALLET_LANG, Monero::NetworkType::MAINNET); |
| 1037 | std::unique_ptr<MyWalletListener> wallet_listener (new MyWalletListener(wallet)); |
| 1038 | wallet->init(MAINNET_DAEMON_ADDRESS, 0); |
| 1039 | std::cerr << "TEST: waiting on refresh lock...\n"; |
| 1040 | //wallet_listener->cv_refresh.wait_for(lock, wait_for); |
| 1041 | std::cerr << "TEST: refresh lock acquired...\n"; |
| 1042 | ASSERT_TRUE(wallet_listener->refresh_triggered); |
| 1043 | ASSERT_TRUE(wallet->connected()); |
| 1044 | ASSERT_TRUE(wallet->blockChainHeight() == wallet->daemonBlockChainHeight()); |
| 1045 | std::cerr << "TEST: closing wallet...\n"; |
| 1046 | wmgr->closeWallet(wallet); |
| 1047 | } |
| 1048 | |
| 1049 | |
| 1050 | TEST_F(WalletManagerMainnetTest, CreateAndRefreshWalletMainNetAsync) |
| 1051 | { |
| 1052 | // supposing 120 seconds should be enough for fast refresh |
| 1053 | int SECONDS_TO_REFRESH = 120; |
| 1054 | |
| 1055 | Monero::Wallet * wallet = wmgr->createWallet(WALLET_NAME_MAINNET, "", WALLET_LANG, Monero::NetworkType::MAINNET); |
| 1056 | std::unique_ptr<MyWalletListener> wallet_listener (new MyWalletListener(wallet)); |
| 1057 | |
| 1058 | boost::chrono::seconds wait_for = boost::chrono::seconds(SECONDS_TO_REFRESH); |
| 1059 | boost::unique_lock<boost::mutex> lock (wallet_listener->mutex); |
| 1060 | wallet->init(MAINNET_DAEMON_ADDRESS, 0); |
| 1061 | wallet->startRefresh(); |
| 1062 | std::cerr << "TEST: waiting on refresh lock...\n"; |
| 1063 | wallet_listener->cv_refresh.wait_for(lock, wait_for); |
| 1064 | std::cerr << "TEST: refresh lock acquired...\n"; |
| 1065 | ASSERT_TRUE(wallet->status() == Monero::Wallet::Status_Ok); |
| 1066 | ASSERT_TRUE(wallet_listener->refresh_triggered); |
| 1067 | ASSERT_TRUE(wallet->connected()); |
| 1068 | ASSERT_TRUE(wallet->blockChainHeight() == wallet->daemonBlockChainHeight()); |
| 1069 | std::cerr << "TEST: closing wallet...\n"; |
| 1070 | wmgr->closeWallet(wallet); |
| 1071 | } |
| 1072 | |
| 1073 | TEST_F(WalletManagerMainnetTest, OpenAndRefreshWalletMainNetAsync) |
| 1074 | { |
| 1075 | |
| 1076 | // supposing 120 seconds should be enough for fast refresh |
| 1077 | int SECONDS_TO_REFRESH = 120; |
| 1078 | Monero::Wallet * wallet = wmgr->createWallet(WALLET_NAME_MAINNET, "", WALLET_LANG, Monero::NetworkType::MAINNET); |
| 1079 | wmgr->closeWallet(wallet); |
| 1080 | wallet = wmgr->openWallet(WALLET_NAME_MAINNET, "", Monero::NetworkType::MAINNET); |
| 1081 | |
| 1082 | std::unique_ptr<MyWalletListener> wallet_listener (new MyWalletListener(wallet)); |
| 1083 | |
| 1084 | boost::chrono::seconds wait_for = boost::chrono::seconds(SECONDS_TO_REFRESH); |
| 1085 | boost::unique_lock<boost::mutex> lock (wallet_listener->mutex); |
| 1086 | wallet->init(MAINNET_DAEMON_ADDRESS, 0); |
| 1087 | wallet->startRefresh(); |
| 1088 | std::cerr << "TEST: waiting on refresh lock...\n"; |
| 1089 | wallet_listener->cv_refresh.wait_for(lock, wait_for); |
| 1090 | std::cerr << "TEST: refresh lock acquired...\n"; |
| 1091 | ASSERT_TRUE(wallet->status() == Monero::Wallet::Status_Ok); |
| 1092 | ASSERT_TRUE(wallet_listener->refresh_triggered); |
| 1093 | ASSERT_TRUE(wallet->connected()); |
| 1094 | ASSERT_TRUE(wallet->blockChainHeight() == wallet->daemonBlockChainHeight()); |
| 1095 | std::cerr << "TEST: closing wallet...\n"; |
| 1096 | wmgr->closeWallet(wallet); |
| 1097 | |
| 1098 | } |
| 1099 | |
| 1100 | TEST_F(WalletManagerMainnetTest, RecoverAndRefreshWalletMainNetAsync) |
| 1101 | { |
| 1102 | |
| 1103 | // supposing 120 seconds should be enough for fast refresh |
| 1104 | int SECONDS_TO_REFRESH = 120; |
| 1105 | Monero::Wallet * wallet = wmgr->createWallet(WALLET_NAME_MAINNET, "", WALLET_LANG, Monero::NetworkType::MAINNET); |
| 1106 | std::string seed = wallet->seed(); |
| 1107 | std::string address = wallet->mainAddress(); |
| 1108 | wmgr->closeWallet(wallet); |
| 1109 | |
| 1110 | // deleting wallet files |
| 1111 | Utils::deleteWallet(WALLET_NAME_MAINNET); |
| 1112 | // ..and recovering wallet from seed |
| 1113 | |
| 1114 | wallet = wmgr->recoveryWallet(WALLET_NAME_MAINNET, seed, Monero::NetworkType::MAINNET); |
| 1115 | ASSERT_TRUE(wallet->status() == Monero::Wallet::Status_Ok); |
| 1116 | ASSERT_TRUE(wallet->mainAddress() == address); |
| 1117 | std::unique_ptr<MyWalletListener> wallet_listener (new MyWalletListener(wallet)); |
| 1118 | boost::chrono::seconds wait_for = boost::chrono::seconds(SECONDS_TO_REFRESH); |
| 1119 | boost::unique_lock<boost::mutex> lock (wallet_listener->mutex); |
| 1120 | wallet->init(MAINNET_DAEMON_ADDRESS, 0); |
| 1121 | wallet->startRefresh(); |
| 1122 | std::cerr << "TEST: waiting on refresh lock...\n"; |
| 1123 | |
| 1124 | // here we wait for 120 seconds and test if wallet doesn't syncrnonize blockchain completely, |
| 1125 | // as it needs much more than 120 seconds for mainnet |
| 1126 | |
| 1127 | wallet_listener->cv_refresh.wait_for(lock, wait_for); |
| 1128 | ASSERT_TRUE(wallet->status() == Monero::Wallet::Status_Ok); |
| 1129 | ASSERT_FALSE(wallet_listener->refresh_triggered); |
| 1130 | ASSERT_TRUE(wallet->connected()); |
| 1131 | ASSERT_FALSE(wallet->blockChainHeight() == wallet->daemonBlockChainHeight()); |
| 1132 | std::cerr << "TEST: closing wallet...\n"; |
| 1133 | wmgr->closeWallet(wallet); |
| 1134 | std::cerr << "TEST: wallet closed\n"; |
| 1135 | |
| 1136 | } |
| 1137 | |
| 1138 | |
| 1139 | |
| 1140 | int main(int argc, char** argv) |
| 1141 | { |
| 1142 | TRY_ENTRY(); |
| 1143 | |
| 1144 | tools::on_startup(); |
| 1145 | // we can override default values for "TESTNET_DAEMON_ADDRESS" and "WALLETS_ROOT_DIR" |
| 1146 | |
| 1147 | const char * testnet_daemon_addr = std::getenv("TESTNET_DAEMON_ADDRESS"); |
| 1148 | if (testnet_daemon_addr) { |
| 1149 | TESTNET_DAEMON_ADDRESS = testnet_daemon_addr; |
| 1150 | } |
| 1151 | |
| 1152 | const char * mainnet_daemon_addr = std::getenv("MAINNET_DAEMON_ADDRESS"); |
| 1153 | if (mainnet_daemon_addr) { |
| 1154 | MAINNET_DAEMON_ADDRESS = mainnet_daemon_addr; |
| 1155 | } |
| 1156 | |
| 1157 | |
| 1158 | |
| 1159 | const char * wallets_root_dir = std::getenv("WALLETS_ROOT_DIR"); |
| 1160 | if (wallets_root_dir) { |
| 1161 | WALLETS_ROOT_DIR = wallets_root_dir; |
| 1162 | } |
| 1163 | |
| 1164 | |
| 1165 | TESTNET_WALLET1_NAME = WALLETS_ROOT_DIR + "/wallet_01.bin"; |
| 1166 | TESTNET_WALLET2_NAME = WALLETS_ROOT_DIR + "/wallet_02.bin"; |
| 1167 | TESTNET_WALLET3_NAME = WALLETS_ROOT_DIR + "/wallet_03.bin"; |
| 1168 | TESTNET_WALLET4_NAME = WALLETS_ROOT_DIR + "/wallet_04.bin"; |
| 1169 | TESTNET_WALLET5_NAME = WALLETS_ROOT_DIR + "/wallet_05.bin"; |
| 1170 | TESTNET_WALLET6_NAME = WALLETS_ROOT_DIR + "/wallet_06.bin"; |
| 1171 | |
| 1172 | CURRENT_SRC_WALLET = TESTNET_WALLET5_NAME; |
| 1173 | CURRENT_DST_WALLET = TESTNET_WALLET1_NAME; |
| 1174 | |
| 1175 | ::testing::InitGoogleTest(&argc, argv); |
| 1176 | Monero::WalletManagerFactory::setLogLevel(Monero::WalletManagerFactory::LogLevel_Max); |
| 1177 | return RUN_ALL_TESTS(); |
| 1178 | CATCH_ENTRY_L0("main", 1); |
| 1179 | } |