Debian Codegraph @

tests/trezor/trezor_tests.h

raw symbols SCIP
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 #pragma once
32
33 #include <device_trezor/device_trezor.hpp>
34 #include <wallet/api/wallet2_api.h>
35 #include "daemon.h"
36 #include "../core_tests/chaingen.h"
37 #include "../core_tests/wallet_tools.h"
38
39 #define TREZOR_TEST_FEE 90000000000
40 #define TREZOR_TEST_CLSAG_MIXIN 11
41 #define TREZOR_TEST_HF15_MIXIN 16
42 #define TREZOR_TEST_MIXIN TREZOR_TEST_CLSAG_MIXIN
43
44 /************************************************************************/
45 /* */
46 /************************************************************************/
47 class tsx_builder;
48 class gen_trezor_base : public test_chain_unit_base
49 {
50 public:
51 friend class tsx_builder;
52
53 gen_trezor_base();
54 gen_trezor_base(const gen_trezor_base &other);
55 virtual ~gen_trezor_base() {};
56
57 virtual void setup_args(const std::string & trezor_path, bool heavy_tests=false);
58 virtual bool generate(std::vector<test_event_entry>& events);
59 virtual void load(std::vector<test_event_entry>& events); // load events, init test obj
60 virtual void fix_hf(std::vector<test_event_entry>& events);
61 virtual void update_trackers(std::vector<test_event_entry>& events);
62
63 virtual void fork(gen_trezor_base & other); // fork generated chain to another test
64 virtual void clear(); // clears m_events, bt, generator, hforks
65 virtual void add_shared_events(std::vector<test_event_entry>& events); // m_events -> events
66 virtual void test_setup(std::vector<test_event_entry>& events); // init setup env, wallets
67
68 virtual void add_transactions_to_events(
69 std::vector<test_event_entry>& events,
70 test_generator &generator,
71 const std::vector<cryptonote::transaction> &txs);
72
73 virtual void test_trezor_tx(
74 std::vector<test_event_entry>& events,
75 std::vector<tools::wallet2::pending_tx>& ptxs,
76 std::vector<cryptonote::address_parse_info>& dsts_info,
77 test_generator &generator,
78 std::vector<tools::wallet2*> wallets,
79 bool is_sweep=false);
80
81 virtual void test_get_tx(
82 std::vector<test_event_entry>& events,
83 std::vector<tools::wallet2*> wallets,
84 const std::vector<tools::wallet2::pending_tx> &ptxs,
85 const std::vector<std::string> &aux_tx_info);
86
87 virtual void mine_and_test(std::vector<test_event_entry>& events);
88
89 virtual void rewind_blocks(std::vector<test_event_entry>& events, size_t rewind_n, uint8_t hf);
90
91 virtual void set_hard_fork(uint8_t hf);
92
93 crypto::hash head_hash() const { return get_block_hash(m_head); }
94 cryptonote::block head_block() const { return m_head; }
95 bool heavy_tests() const { return m_heavy_tests; }
96 void rct_config(rct::RCTConfig rct_config) { m_rct_config = rct_config; }
97 uint8_t cur_hf() const { return m_hard_forks.size() > 0 ? m_hard_forks.back().first : 0; }
98 size_t num_mixin() const { return m_top_hard_fork >= HF_VERSION_BULLETPROOF_PLUS ? TREZOR_TEST_HF15_MIXIN : TREZOR_TEST_CLSAG_MIXIN; }
99 cryptonote::network_type nettype() const { return m_network_type; }
100 std::shared_ptr<mock_daemon> daemon() const { return m_daemon; }
101 void daemon(std::shared_ptr<mock_daemon> daemon){ m_daemon = std::move(daemon); }
102
103 // Static configuration
104 static const uint64_t m_ts_start;
105 static const uint64_t m_wallet_ts;
106 static const std::string m_device_name;
107 static const std::string m_master_seed_str;
108 static const std::string m_device_seed;
109 static const std::string m_alice_spend_private;
110 static const std::string m_alice_view_private;
111
112 protected:
113 virtual void setup_trezor();
114 virtual void init_fields();
115 virtual void update_client_settings();
116 virtual bool verify_tx_key(const ::crypto::secret_key & tx_priv, const ::crypto::public_key & tx_pub, const subaddresses_t & subs);
117
118 test_generator m_generator;
119 block_tracker m_bt;
120 cryptonote::network_type m_network_type;
121 std::shared_ptr<mock_daemon> m_daemon;
122
123 uint8_t m_top_hard_fork;
124 v_hardforks_t m_hard_forks;
125 cryptonote::block m_head;
126 std::vector<test_event_entry> m_events;
127
128 std::string m_trezor_path;
129 bool m_heavy_tests;
130 bool m_test_get_tx_key;
131 rct::RCTConfig m_rct_config;
132 bool m_live_refresh_enabled;
133
134 cryptonote::account_base m_miner_account;
135 cryptonote::account_base m_bob_account;
136 cryptonote::account_base m_alice_account;
137 cryptonote::account_base m_eve_account;
138 hw::trezor::device_trezor * m_trezor;
139 std::unique_ptr<tools::wallet2> m_wl_alice;
140 std::unique_ptr<tools::wallet2> m_wl_bob;
141 std::unique_ptr<tools::wallet2> m_wl_eve;
142
143 friend class boost::serialization::access;
144
145 template<class Archive>
146 void serialize(Archive & ar, const unsigned int /*version*/)
147 {
148 ar & m_generator;
149 ar & m_network_type;
150 }
151 };
152
153 class tsx_builder {
154 public:
155 tsx_builder(): m_tester(nullptr), m_from(nullptr), m_account(0), m_mixin(TREZOR_TEST_MIXIN), m_fee(TREZOR_TEST_FEE),
156 m_rct_config({rct::RangeProofPaddedBulletproof, 1 }){}
157
158 tsx_builder(gen_trezor_base * tester): m_tester(tester), m_from(nullptr), m_account(0),
159 m_mixin(TREZOR_TEST_MIXIN), m_fee(TREZOR_TEST_FEE),
160 m_rct_config({rct::RangeProofPaddedBulletproof, 1 }){}
161
162 tsx_builder * cur_height(uint64_t cur_height) { m_cur_height = cur_height; return this; }
163 tsx_builder * mixin(size_t mixin=TREZOR_TEST_MIXIN) { m_mixin = mixin; return this; }
164 tsx_builder * fee(uint64_t fee=TREZOR_TEST_FEE) { m_fee = fee; return this; }
165 tsx_builder * payment_id(const std::string & payment_id) { m_payment_id = payment_id; return this; }
166 tsx_builder * from(tools::wallet2 *from, uint32_t account=0) { m_from = from; m_account=account; return this; }
167 tsx_builder * sources(std::vector<cryptonote::tx_source_entry> & sources, std::vector<size_t> & selected_transfers);
168 tsx_builder * compute_sources(boost::optional<size_t> num_utxo=boost::none, boost::optional<uint64_t> min_amount=boost::none, ssize_t offset=-1, int step=1, boost::optional<fnc_accept_tx_source_t> fnc_accept=boost::none);
169 tsx_builder * compute_sources_to_sub(boost::optional<size_t> num_utxo=boost::none, boost::optional<uint64_t> min_amount=boost::none, ssize_t offset=-1, int step=1, boost::optional<fnc_accept_tx_source_t> fnc_accept=boost::none);
170 tsx_builder * compute_sources_to_sub_acc(boost::optional<size_t> num_utxo=boost::none, boost::optional<uint64_t> min_amount=boost::none, ssize_t offset=-1, int step=1, boost::optional<fnc_accept_tx_source_t> fnc_accept=boost::none);
171
172 tsx_builder * destinations(std::vector<cryptonote::tx_destination_entry> &dsts);
173 tsx_builder * add_destination(const cryptonote::tx_destination_entry &dst);
174 tsx_builder * add_destination(const tools::wallet2 * wallet, bool is_subaddr=false, uint64_t amount=1000);
175 tsx_builder * add_destination(const var_addr_t addr, bool is_subaddr=false, uint64_t amount=1000);
176 tsx_builder * set_integrated(size_t idx);
177 tsx_builder * rct_config(const rct::RCTConfig & rct_config) {m_rct_config = rct_config; return this; };
178
179 tsx_builder * build_tx();
180 tsx_builder * construct_pending_tx(tools::wallet2::pending_tx &ptx, boost::optional<std::vector<uint8_t>> extra = boost::none);
181 tsx_builder * clear_current();
182 std::vector<tools::wallet2::pending_tx> build();
183 std::vector<cryptonote::address_parse_info> dest_info(){ return m_dsts_info; }
184
185 protected:
186 gen_trezor_base * m_tester;
187 uint64_t m_cur_height;
188 std::vector<tools::wallet2::pending_tx> m_ptxs; // all transactions
189
190 // current transaction
191 size_t m_mixin;
192 uint64_t m_fee;
193 tools::wallet2 * m_from;
194 uint32_t m_account;
195 cryptonote::transaction m_tx;
196 std::vector<size_t> m_selected_transfers;
197 std::vector<cryptonote::tx_source_entry> m_sources;
198 std::vector<cryptonote::tx_destination_entry> m_destinations;
199 std::vector<cryptonote::tx_destination_entry> m_destinations_orig;
200 std::vector<cryptonote::address_parse_info> m_dsts_info;
201 std::unordered_set<size_t> m_integrated;
202 std::string m_payment_id;
203 rct::RCTConfig m_rct_config;
204 };
205
206 // Trezor device ship to track actual method calls.
207 class device_trezor_test : public hw::trezor::device_trezor {
208 public:
209 size_t m_tx_sign_ctr;
210 size_t m_compute_key_image_ctr;
211
212 device_trezor_test();
213
214 void clear_test_counters();
215 void setup_for_tests(const std::string & trezor_path, const std::string & seed, cryptonote::network_type network_type);
216
217 bool compute_key_image(const ::cryptonote::account_keys &ack, const ::crypto::public_key &out_key,
218 const ::crypto::key_derivation &recv_derivation, size_t real_output_index,
219 const ::cryptonote::subaddress_index &received_index, ::cryptonote::keypair &in_ephemeral,
220 ::crypto::key_image &ki) override;
221
222 protected:
223 void tx_sign(hw::wallet_shim *wallet, const ::tools::wallet2::unsigned_tx_set &unsigned_tx, size_t idx,
224 hw::tx_aux_data &aux_data, std::shared_ptr<hw::trezor::protocol::tx::Signer> &signer) override;
225 };
226
227 // Tests
228 class gen_trezor_ki_sync : public gen_trezor_base
229 {
230 public:
231 bool generate(std::vector<test_event_entry>& events) override;
232 };
233
234 class gen_trezor_ki_sync_with_refresh : public gen_trezor_ki_sync
235 {
236 public:
237 bool generate(std::vector<test_event_entry>& events) override;
238 };
239
240 class gen_trezor_ki_sync_without_refresh : public gen_trezor_ki_sync
241 {
242 public:
243 bool generate(std::vector<test_event_entry>& events) override;
244 };
245
246 class gen_trezor_live_refresh : public gen_trezor_base
247 {
248 public:
249 bool generate(std::vector<test_event_entry>& events) override;
250 };
251
252 class gen_trezor_1utxo : public gen_trezor_base
253 {
254 public:
255 bool generate(std::vector<test_event_entry>& events) override;
256 };
257
258 class gen_trezor_1utxo_paymentid_short : public gen_trezor_base
259 {
260 public:
261 bool generate(std::vector<test_event_entry>& events) override;
262 };
263
264 class gen_trezor_1utxo_paymentid_short_integrated : public gen_trezor_base
265 {
266 public:
267 bool generate(std::vector<test_event_entry>& events) override;
268 };
269
270 class gen_trezor_4utxo : public gen_trezor_base
271 {
272 public:
273 bool generate(std::vector<test_event_entry>& events) override;
274 };
275
276 class gen_trezor_4utxo_acc1 : public gen_trezor_base
277 {
278 public:
279 bool generate(std::vector<test_event_entry>& events) override;
280 };
281
282 class gen_trezor_4utxo_to_sub : public gen_trezor_base
283 {
284 public:
285 bool generate(std::vector<test_event_entry>& events) override;
286 };
287
288 class gen_trezor_4utxo_to_2sub : public gen_trezor_base
289 {
290 public:
291 bool generate(std::vector<test_event_entry>& events) override;
292 };
293
294 class gen_trezor_4utxo_to_1norm_2sub : public gen_trezor_base
295 {
296 public:
297 bool generate(std::vector<test_event_entry>& events) override;
298 };
299
300 class gen_trezor_2utxo_sub_acc_to_1norm_2sub : public gen_trezor_base
301 {
302 public:
303 bool generate(std::vector<test_event_entry>& events) override;
304 };
305
306 class gen_trezor_4utxo_to_7outs : public gen_trezor_base
307 {
308 public:
309 bool generate(std::vector<test_event_entry>& events) override;
310 };
311
312 class gen_trezor_4utxo_to_15outs : public gen_trezor_base
313 {
314 public:
315 bool generate(std::vector<test_event_entry>& events) override;
316 };
317
318 class gen_trezor_many_utxo : public gen_trezor_base
319 {
320 public:
321 bool generate(std::vector<test_event_entry>& events) override;
322 };
323
324 class gen_trezor_many_utxo_many_txo : public gen_trezor_base
325 {
326 public:
327 bool generate(std::vector<test_event_entry>& events) override;
328 };
329
330 // Wallet::API tests
331 class wallet_api_tests : public gen_trezor_base
332 {
333 public:
334 virtual ~wallet_api_tests();
335 void init();
336 bool generate(std::vector<test_event_entry>& events) override;
337
338 protected:
339 boost::filesystem::path m_wallet_dir;
340 };