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