Bitcoin Core  24.99.0
P2P Digital Currency
addressbooktests.cpp
Go to the documentation of this file.
1 // Copyright (c) 2017-2021 The Bitcoin Core developers
2 // Distributed under the MIT software license, see the accompanying
3 // file COPYING or http://www.opensource.org/licenses/mit-license.php.
4 
6 #include <qt/test/util.h>
8 
9 #include <interfaces/chain.h>
10 #include <interfaces/node.h>
11 #include <qt/addressbookpage.h>
12 #include <qt/clientmodel.h>
13 #include <qt/editaddressdialog.h>
14 #include <qt/optionsmodel.h>
15 #include <qt/platformstyle.h>
16 #include <qt/qvalidatedlineedit.h>
17 #include <qt/walletmodel.h>
18 
19 #include <key.h>
20 #include <key_io.h>
21 #include <wallet/wallet.h>
22 #include <walletinitinterface.h>
23 
24 #include <chrono>
25 
26 #include <QApplication>
27 #include <QLineEdit>
28 #include <QMessageBox>
29 #include <QTableView>
30 #include <QTimer>
31 
32 using wallet::AddWallet;
33 using wallet::CWallet;
38 
39 namespace
40 {
41 
46 void EditAddressAndSubmit(
47  EditAddressDialog* dialog,
48  const QString& label, const QString& address, QString expected_msg)
49 {
50  QString warning_text;
51 
52  dialog->findChild<QLineEdit*>("labelEdit")->setText(label);
53  dialog->findChild<QValidatedLineEdit*>("addressEdit")->setText(address);
54 
55  ConfirmMessage(&warning_text, 5ms);
56  dialog->accept();
57  QCOMPARE(warning_text, expected_msg);
58 }
59 
72 void TestAddAddressesToSendBook(interfaces::Node& node)
73 {
74  TestChain100Setup test;
75  auto wallet_loader = interfaces::MakeWalletLoader(*test.m_node.chain, *Assert(test.m_node.args));
76  test.m_node.wallet_loader = wallet_loader.get();
77  node.setContext(&test.m_node);
78  const std::shared_ptr<CWallet> wallet = std::make_shared<CWallet>(node.context()->chain.get(), "", gArgs, CreateMockWalletDatabase());
79  wallet->LoadWallet();
80  wallet->SetWalletFlag(WALLET_FLAG_DESCRIPTORS);
81  {
82  LOCK(wallet->cs_wallet);
83  wallet->SetupDescriptorScriptPubKeyMans();
84  }
85 
86  auto build_address = [&wallet]() {
87  CKey key;
88  key.MakeNewKey(true);
90  key.GetPubKey(), wallet->m_default_address_type));
91 
92  return std::make_pair(dest, QString::fromStdString(EncodeDestination(dest)));
93  };
94 
95  CTxDestination r_key_dest, s_key_dest;
96 
97  // Add a preexisting "receive" entry in the address book.
98  QString preexisting_r_address;
99  QString r_label("already here (r)");
100 
101  // Add a preexisting "send" entry in the address book.
102  QString preexisting_s_address;
103  QString s_label("already here (s)");
104 
105  // Define a new address (which should add to the address book successfully).
106  QString new_address_a;
107  QString new_address_b;
108 
109  std::tie(r_key_dest, preexisting_r_address) = build_address();
110  std::tie(s_key_dest, preexisting_s_address) = build_address();
111  std::tie(std::ignore, new_address_a) = build_address();
112  std::tie(std::ignore, new_address_b) = build_address();
113 
114  {
115  LOCK(wallet->cs_wallet);
116  wallet->SetAddressBook(r_key_dest, r_label.toStdString(), "receive");
117  wallet->SetAddressBook(s_key_dest, s_label.toStdString(), "send");
118  }
119 
120  auto check_addbook_size = [&wallet](int expected_size) {
121  LOCK(wallet->cs_wallet);
122  QCOMPARE(static_cast<int>(wallet->m_address_book.size()), expected_size);
123  };
124 
125  // We should start with the two addresses we added earlier and nothing else.
126  check_addbook_size(2);
127 
128  // Initialize relevant QT models.
129  std::unique_ptr<const PlatformStyle> platformStyle(PlatformStyle::instantiate("other"));
130  OptionsModel optionsModel(node);
132  QVERIFY(optionsModel.Init(error));
133  ClientModel clientModel(node, &optionsModel);
134  WalletContext& context = *node.walletLoader().context();
136  WalletModel walletModel(interfaces::MakeWallet(context, wallet), clientModel, platformStyle.get());
137  RemoveWallet(context, wallet, /* load_on_start= */ std::nullopt);
139  editAddressDialog.setModel(walletModel.getAddressTableModel());
140 
141  AddressBookPage address_book{platformStyle.get(), AddressBookPage::ForEditing, AddressBookPage::SendingTab};
142  address_book.setModel(walletModel.getAddressTableModel());
143  auto table_view = address_book.findChild<QTableView*>("tableView");
144  QCOMPARE(table_view->model()->rowCount(), 1);
145 
146  EditAddressAndSubmit(
147  &editAddressDialog, QString("uhoh"), preexisting_r_address,
148  QString(
149  "Address \"%1\" already exists as a receiving address with label "
150  "\"%2\" and so cannot be added as a sending address."
151  ).arg(preexisting_r_address).arg(r_label));
152  check_addbook_size(2);
153  QCOMPARE(table_view->model()->rowCount(), 1);
154 
155  EditAddressAndSubmit(
156  &editAddressDialog, QString("uhoh, different"), preexisting_s_address,
157  QString(
158  "The entered address \"%1\" is already in the address book with "
159  "label \"%2\"."
160  ).arg(preexisting_s_address).arg(s_label));
161  check_addbook_size(2);
162  QCOMPARE(table_view->model()->rowCount(), 1);
163 
164  // Submit a new address which should add successfully - we expect the
165  // warning message to be blank.
166  EditAddressAndSubmit(
167  &editAddressDialog, QString("io - new A"), new_address_a, QString(""));
168  check_addbook_size(3);
169  QCOMPARE(table_view->model()->rowCount(), 2);
170 
171  EditAddressAndSubmit(
172  &editAddressDialog, QString("io - new B"), new_address_b, QString(""));
173  check_addbook_size(4);
174  QCOMPARE(table_view->model()->rowCount(), 3);
175 
176  auto search_line = address_book.findChild<QLineEdit*>("searchLineEdit");
177 
178  search_line->setText(r_label);
179  QCOMPARE(table_view->model()->rowCount(), 0);
180 
181  search_line->setText(s_label);
182  QCOMPARE(table_view->model()->rowCount(), 1);
183 
184  search_line->setText("io");
185  QCOMPARE(table_view->model()->rowCount(), 2);
186 
187  // Check wildcard "?".
188  search_line->setText("io?new");
189  QCOMPARE(table_view->model()->rowCount(), 0);
190  search_line->setText("io???new");
191  QCOMPARE(table_view->model()->rowCount(), 2);
192 
193  // Check wildcard "*".
194  search_line->setText("io*new");
195  QCOMPARE(table_view->model()->rowCount(), 2);
196  search_line->setText("*");
197  QCOMPARE(table_view->model()->rowCount(), 3);
198 
199  search_line->setText(preexisting_r_address);
200  QCOMPARE(table_view->model()->rowCount(), 0);
201 
202  search_line->setText(preexisting_s_address);
203  QCOMPARE(table_view->model()->rowCount(), 1);
204 
205  search_line->setText(new_address_a);
206  QCOMPARE(table_view->model()->rowCount(), 1);
207 
208  search_line->setText(new_address_b);
209  QCOMPARE(table_view->model()->rowCount(), 1);
210 
211  search_line->setText("");
212  QCOMPARE(table_view->model()->rowCount(), 3);
213 }
214 
215 } // namespace
216 
218 {
219 #ifdef Q_OS_MACOS
220  if (QApplication::platformName() == "minimal") {
221  // Disable for mac on "minimal" platform to avoid crashes inside the Qt
222  // framework when it tries to look up unimplemented cocoa functions,
223  // and fails to handle returned nulls
224  // (https://bugreports.qt.io/browse/QTBUG-49686).
225  QWARN("Skipping AddressBookTests on mac build with 'minimal' platform set due to Qt bugs. To run AppTests, invoke "
226  "with 'QT_QPA_PLATFORM=cocoa test_bitcoin-qt' on mac, or else use a linux or windows build.");
227  return;
228  }
229 #endif
230  TestAddAddressesToSendBook(m_node);
231 }
#define Assert(val)
Identity function.
Definition: check.h:74
Widget that shows a list of sending or receiving addresses.
@ ForEditing
Open address book for editing.
interfaces::Node & m_node
An encapsulated private key.
Definition: key.h:27
void MakeNewKey(bool fCompressed)
Generate a new private key using a cryptographic PRNG.
Definition: key.cpp:160
CPubKey GetPubKey() const
Compute the public key from a private key.
Definition: key.cpp:187
Model for Bitcoin network client.
Definition: clientmodel.h:55
Dialog for editing an address and associated information.
void accept() override
Interface from Qt to configuration data structure for Bitcoin client.
Definition: optionsmodel.h:41
static const PlatformStyle * instantiate(const QString &platformId)
Get style associated with provided platform name, or 0 if not known.
Line edit that can be marked as "invalid" to show input validation feedback.
Interface to Bitcoin wallet from Qt view code.
Definition: walletmodel.h:53
Top-level interface for a bitcoin node (bitcoind process).
Definition: node.h:70
A CWallet maintains a set of transactions and balances, and provides the ability to create new transa...
Definition: wallet.h:236
std::string EncodeDestination(const CTxDestination &dest)
Definition: key_io.cpp:276
std::unique_ptr< WalletLoader > MakeWalletLoader(Chain &chain, ArgsManager &args)
Return implementation of ChainClient interface for a wallet loader.
Definition: dummywallet.cpp:62
std::unique_ptr< Wallet > MakeWallet(wallet::WalletContext &context, const std::shared_ptr< wallet::CWallet > &wallet)
Return implementation of Wallet interface.
Definition: interfaces.cpp:630
Definition: init.h:25
Definition: node.h:39
std::unique_ptr< WalletDatabase > CreateMockWalletDatabase(DatabaseOptions &options)
Return object for accessing temporary in-memory database.
Definition: walletdb.cpp:1242
bool AddWallet(WalletContext &context, const std::shared_ptr< CWallet > &wallet)
Definition: wallet.cpp:108
@ WALLET_FLAG_DESCRIPTORS
Indicate that this wallet supports DescriptorScriptPubKeyMan.
Definition: walletutil.h:66
bool RemoveWallet(WalletContext &context, const std::shared_ptr< CWallet > &wallet, std::optional< bool > load_on_start, std::vector< bilingual_str > &warnings)
Definition: wallet.cpp:120
std::shared_ptr< CWallet > wallet
WalletContext context
CTxDestination GetDestinationForKey(const CPubKey &key, OutputType type)
Get a destination of the requested type (if possible) to the specified key.
Definition: outputtype.cpp:53
void ConfirmMessage(QString *text, std::chrono::milliseconds msec)
Press "Ok" button in message box dialog.
Definition: util.cpp:14
std::variant< CNoDestination, PKHash, ScriptHash, WitnessV0ScriptHash, WitnessV0KeyHash, WitnessV1Taproot, WitnessUnknown > CTxDestination
A txout script template with a specific destination.
Definition: standard.h:149
node::NodeContext m_node
Definition: setup_common.h:84
Testing fixture that pre-creates a 100-block REGTEST-mode block chain.
Definition: setup_common.h:126
Bilingual messages:
Definition: translation.h:18
ArgsManager * args
Definition: context.h:56
interfaces::WalletLoader * wallet_loader
Reference to chain client that should used to load or create wallets opened by the gui.
Definition: context.h:62
std::unique_ptr< interfaces::Chain > chain
Definition: context.h:57
WalletContext struct containing references to state shared between CWallet instances,...
Definition: context.h:35
#define LOCK(cs)
Definition: sync.h:261
bool error(const char *fmt, const Args &... args)
Definition: system.h:48
ArgsManager gArgs
Definition: system.cpp:86