Debian Codegraph @

tests/unit_tests/test_tx_utils.cpp

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 #include "gtest/gtest.h"
32
33 #include <vector>
34
35 #include "common/util.h"
36 #include "cryptonote_basic/cryptonote_basic.h"
37 #include "cryptonote_basic/tx_extra.h"
38 #include "cryptonote_core/cryptonote_tx_utils.h"
39
40 namespace
41 {
42 uint64_t const TEST_FEE = 5000000000; // 5 * 10^9
43 }
44
45 TEST(parse_tx_extra, handles_empty_extra)
46 {
47 std::vector<uint8_t> extra;
48 std::vector<cryptonote::tx_extra_field> tx_extra_fields;
49 ASSERT_TRUE(cryptonote::parse_tx_extra(extra, tx_extra_fields));
50 ASSERT_TRUE(tx_extra_fields.empty());
51 }
52
53 TEST(parse_tx_extra, handles_padding_only_size_1)
54 {
55 const uint8_t extra_arr[] = {0};
56 std::vector<uint8_t> extra(&extra_arr[0], &extra_arr[0] + sizeof(extra_arr));
57 std::vector<cryptonote::tx_extra_field> tx_extra_fields;
58 ASSERT_TRUE(cryptonote::parse_tx_extra(extra, tx_extra_fields));
59 ASSERT_EQ(1, tx_extra_fields.size());
60 ASSERT_EQ(typeid(cryptonote::tx_extra_padding), tx_extra_fields[0].type());
61 ASSERT_EQ(1, boost::get<cryptonote::tx_extra_padding>(tx_extra_fields[0]).size);
62 }
63
64 TEST(parse_tx_extra, handles_padding_only_size_2)
65 {
66 const uint8_t extra_arr[] = {0, 0};
67 std::vector<uint8_t> extra(&extra_arr[0], &extra_arr[0] + sizeof(extra_arr));
68 std::vector<cryptonote::tx_extra_field> tx_extra_fields;
69 ASSERT_TRUE(cryptonote::parse_tx_extra(extra, tx_extra_fields));
70 ASSERT_EQ(1, tx_extra_fields.size());
71 ASSERT_EQ(typeid(cryptonote::tx_extra_padding), tx_extra_fields[0].type());
72 ASSERT_EQ(2, boost::get<cryptonote::tx_extra_padding>(tx_extra_fields[0]).size);
73 }
74
75 TEST(parse_tx_extra, handles_padding_only_max_size)
76 {
77 std::vector<uint8_t> extra(TX_EXTRA_NONCE_MAX_COUNT, 0);
78 std::vector<cryptonote::tx_extra_field> tx_extra_fields;
79 ASSERT_TRUE(cryptonote::parse_tx_extra(extra, tx_extra_fields));
80 ASSERT_EQ(1, tx_extra_fields.size());
81 ASSERT_EQ(typeid(cryptonote::tx_extra_padding), tx_extra_fields[0].type());
82 ASSERT_EQ(TX_EXTRA_NONCE_MAX_COUNT, boost::get<cryptonote::tx_extra_padding>(tx_extra_fields[0]).size);
83 }
84
85 TEST(parse_tx_extra, handles_padding_only_exceed_max_size)
86 {
87 std::vector<uint8_t> extra(TX_EXTRA_NONCE_MAX_COUNT + 1, 0);
88 std::vector<cryptonote::tx_extra_field> tx_extra_fields;
89 ASSERT_FALSE(cryptonote::parse_tx_extra(extra, tx_extra_fields));
90 }
91
92 TEST(parse_tx_extra, handles_invalid_padding_only)
93 {
94 std::vector<uint8_t> extra(2, 0);
95 extra[1] = 42;
96 std::vector<cryptonote::tx_extra_field> tx_extra_fields;
97 ASSERT_FALSE(cryptonote::parse_tx_extra(extra, tx_extra_fields));
98 }
99
100 TEST(parse_tx_extra, handles_pub_key_only)
101 {
102 const uint8_t extra_arr[] = {1, 30, 208, 98, 162, 133, 64, 85, 83, 112, 91, 188, 89, 211, 24, 131, 39, 154, 22, 228,
103 80, 63, 198, 141, 173, 111, 244, 183, 4, 149, 186, 140, 230};
104 std::vector<uint8_t> extra(&extra_arr[0], &extra_arr[0] + sizeof(extra_arr));
105 std::vector<cryptonote::tx_extra_field> tx_extra_fields;
106 ASSERT_TRUE(cryptonote::parse_tx_extra(extra, tx_extra_fields));
107 ASSERT_EQ(1, tx_extra_fields.size());
108 ASSERT_EQ(typeid(cryptonote::tx_extra_pub_key), tx_extra_fields[0].type());
109 }
110
111 TEST(parse_tx_extra, handles_extra_nonce_only)
112 {
113 const uint8_t extra_arr[] = {2, 1, 42};
114 std::vector<uint8_t> extra(&extra_arr[0], &extra_arr[0] + sizeof(extra_arr));
115 std::vector<cryptonote::tx_extra_field> tx_extra_fields;
116 ASSERT_TRUE(cryptonote::parse_tx_extra(extra, tx_extra_fields));
117 ASSERT_EQ(1, tx_extra_fields.size());
118 ASSERT_EQ(typeid(cryptonote::tx_extra_nonce), tx_extra_fields[0].type());
119 cryptonote::tx_extra_nonce extra_nonce = boost::get<cryptonote::tx_extra_nonce>(tx_extra_fields[0]);
120 ASSERT_EQ(1, extra_nonce.nonce.size());
121 ASSERT_EQ(42, extra_nonce.nonce[0]);
122 }
123
124 TEST(parse_tx_extra, handles_pub_key_and_padding)
125 {
126 const uint8_t extra_arr[] = {1, 30, 208, 98, 162, 133, 64, 85, 83, 112, 91, 188, 89, 211, 24, 131, 39, 154, 22, 228,
127 80, 63, 198, 141, 173, 111, 244, 183, 4, 149, 186, 140, 230, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
128 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
129 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
130 std::vector<uint8_t> extra(&extra_arr[0], &extra_arr[0] + sizeof(extra_arr));
131 std::vector<cryptonote::tx_extra_field> tx_extra_fields;
132 ASSERT_TRUE(cryptonote::parse_tx_extra(extra, tx_extra_fields));
133 ASSERT_EQ(2, tx_extra_fields.size());
134 ASSERT_EQ(typeid(cryptonote::tx_extra_pub_key), tx_extra_fields[0].type());
135 ASSERT_EQ(typeid(cryptonote::tx_extra_padding), tx_extra_fields[1].type());
136 }
137
138 TEST(parse_and_validate_tx_extra, is_valid_tx_extra_parsed)
139 {
140 cryptonote::transaction tx = AUTO_VAL_INIT(tx);
141 cryptonote::account_base acc;
142 acc.generate();
143 cryptonote::blobdata b = "dsdsdfsdfsf";
144 ASSERT_TRUE(cryptonote::construct_miner_tx(0, 0, 10000000000000, 1000, TEST_FEE, acc.get_keys().m_account_address, tx, b, 1));
145 crypto::public_key tx_pub_key = cryptonote::get_tx_pub_key_from_extra(tx);
146 ASSERT_NE(tx_pub_key, crypto::null_pkey);
147 }
148 TEST(parse_and_validate_tx_extra, fails_on_big_extra_nonce)
149 {
150 cryptonote::transaction tx = AUTO_VAL_INIT(tx);
151 cryptonote::account_base acc;
152 acc.generate();
153 cryptonote::blobdata b(TX_EXTRA_NONCE_MAX_COUNT + 1, 0);
154 ASSERT_FALSE(cryptonote::construct_miner_tx(0, 0, 10000000000000, 1000, TEST_FEE, acc.get_keys().m_account_address, tx, b, 1));
155 }
156 TEST(parse_and_validate_tx_extra, fails_on_wrong_size_in_extra_nonce)
157 {
158 cryptonote::transaction tx = AUTO_VAL_INIT(tx);
159 tx.extra.resize(20, 0);
160 tx.extra[0] = TX_EXTRA_NONCE;
161 tx.extra[1] = 255;
162 std::vector<cryptonote::tx_extra_field> tx_extra_fields;
163 ASSERT_FALSE(cryptonote::parse_tx_extra(tx.extra, tx_extra_fields));
164 }
165 TEST(validate_parse_amount_case, validate_parse_amount)
166 {
167 uint64_t res = 0;
168 bool r = cryptonote::parse_amount(res, "0.0001");
169 ASSERT_TRUE(r);
170 ASSERT_EQ(res, 100000000);
171
172 r = cryptonote::parse_amount(res, "100.0001");
173 ASSERT_TRUE(r);
174 ASSERT_EQ(res, 100000100000000);
175
176 r = cryptonote::parse_amount(res, "000.0000");
177 ASSERT_TRUE(r);
178 ASSERT_EQ(res, 0);
179
180 r = cryptonote::parse_amount(res, "0");
181 ASSERT_TRUE(r);
182 ASSERT_EQ(res, 0);
183
184
185 r = cryptonote::parse_amount(res, " 100.0001 ");
186 ASSERT_TRUE(r);
187 ASSERT_EQ(res, 100000100000000);
188
189 r = cryptonote::parse_amount(res, " 100.0000 ");
190 ASSERT_TRUE(r);
191 ASSERT_EQ(res, 100000000000000);
192
193 r = cryptonote::parse_amount(res, " 100. 0000 ");
194 ASSERT_FALSE(r);
195
196 r = cryptonote::parse_amount(res, "100. 0000");
197 ASSERT_FALSE(r);
198
199 r = cryptonote::parse_amount(res, "100 . 0000");
200 ASSERT_FALSE(r);
201
202 r = cryptonote::parse_amount(res, "100.00 00");
203 ASSERT_FALSE(r);
204
205 r = cryptonote::parse_amount(res, "1 00.00 00");
206 ASSERT_FALSE(r);
207 }
208
209 TEST(sort_tx_extra, empty)
210 {
211 std::vector<uint8_t> extra, sorted;
212 ASSERT_TRUE(cryptonote::sort_tx_extra(extra, sorted));
213 ASSERT_EQ(extra, sorted);
214 }
215
216 TEST(sort_tx_extra, pubkey)
217 {
218 std::vector<uint8_t> sorted;
219 const uint8_t extra_arr[] = {1, 30, 208, 98, 162, 133, 64, 85, 83, 112, 91, 188, 89, 211, 24, 131, 39, 154, 22, 228,
220 80, 63, 198, 141, 173, 111, 244, 183, 4, 149, 186, 140, 230};
221 std::vector<uint8_t> extra(&extra_arr[0], &extra_arr[0] + sizeof(extra_arr));
222 ASSERT_TRUE(cryptonote::sort_tx_extra(extra, sorted));
223 ASSERT_EQ(extra, sorted);
224 }
225
226 TEST(sort_tx_extra, two_pubkeys)
227 {
228 std::vector<uint8_t> sorted;
229 const uint8_t extra_arr[] = {1, 30, 208, 98, 162, 133, 64, 85, 83, 112, 91, 188, 89, 211, 24, 131, 39, 154, 22, 228,
230 80, 63, 198, 141, 173, 111, 244, 183, 4, 149, 186, 140, 230,
231 1, 30, 208, 98, 162, 133, 64, 85, 83, 112, 91, 188, 89, 211, 24, 131, 39, 154, 22, 228,
232 80, 63, 198, 141, 173, 111, 244, 183, 4, 149, 186, 140, 230};
233 std::vector<uint8_t> extra(&extra_arr[0], &extra_arr[0] + sizeof(extra_arr));
234 ASSERT_TRUE(cryptonote::sort_tx_extra(extra, sorted));
235 ASSERT_EQ(extra, sorted);
236 }
237
238 TEST(sort_tx_extra, keep_order)
239 {
240 std::vector<uint8_t> sorted;
241 const uint8_t extra_arr[] = {1, 30, 208, 98, 162, 133, 64, 85, 83, 112, 91, 188, 89, 211, 24, 131, 39, 154, 22, 228,
242 80, 63, 198, 141, 173, 111, 244, 183, 4, 149, 186, 140, 230,
243 2, 9, 1, 0, 0, 0, 0, 0, 0, 0, 0};
244 std::vector<uint8_t> extra(&extra_arr[0], &extra_arr[0] + sizeof(extra_arr));
245 ASSERT_TRUE(cryptonote::sort_tx_extra(extra, sorted));
246 ASSERT_EQ(extra, sorted);
247 }
248
249 TEST(sort_tx_extra, switch_order)
250 {
251 std::vector<uint8_t> sorted;
252 const uint8_t extra_arr[] = {2, 9, 1, 0, 0, 0, 0, 0, 0, 0, 0,
253 1, 30, 208, 98, 162, 133, 64, 85, 83, 112, 91, 188, 89, 211, 24, 131, 39, 154, 22, 228,
254 80, 63, 198, 141, 173, 111, 244, 183, 4, 149, 186, 140, 230};
255 const uint8_t expected_arr[] = {1, 30, 208, 98, 162, 133, 64, 85, 83, 112, 91, 188, 89, 211, 24, 131, 39, 154, 22, 228,
256 80, 63, 198, 141, 173, 111, 244, 183, 4, 149, 186, 140, 230,
257 2, 9, 1, 0, 0, 0, 0, 0, 0, 0, 0};
258 std::vector<uint8_t> extra(&extra_arr[0], &extra_arr[0] + sizeof(extra_arr));
259 ASSERT_TRUE(cryptonote::sort_tx_extra(extra, sorted));
260 std::vector<uint8_t> expected(&expected_arr[0], &expected_arr[0] + sizeof(expected_arr));
261 ASSERT_EQ(expected, sorted);
262 }
263
264 TEST(sort_tx_extra, invalid)
265 {
266 std::vector<uint8_t> sorted;
267 const uint8_t extra_arr[] = {1};
268 std::vector<uint8_t> extra(&extra_arr[0], &extra_arr[0] + sizeof(extra_arr));
269 ASSERT_FALSE(cryptonote::sort_tx_extra(extra, sorted));
270 }
271
272 TEST(sort_tx_extra, invalid_suffix_strict)
273 {
274 std::vector<uint8_t> sorted;
275 const uint8_t extra_arr[] = {2, 9, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1};
276 std::vector<uint8_t> extra(&extra_arr[0], &extra_arr[0] + sizeof(extra_arr));
277 ASSERT_FALSE(cryptonote::sort_tx_extra(extra, sorted));
278 }
279
280 TEST(sort_tx_extra, invalid_suffix_partial)
281 {
282 std::vector<uint8_t> sorted;
283 const uint8_t extra_arr[] = {2, 9, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1};
284 const uint8_t expected_arr[] = {2, 9, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1};
285 std::vector<uint8_t> extra(&extra_arr[0], &extra_arr[0] + sizeof(extra_arr));
286 ASSERT_TRUE(cryptonote::sort_tx_extra(extra, sorted, true));
287 std::vector<uint8_t> expected(&expected_arr[0], &expected_arr[0] + sizeof(expected_arr));
288 ASSERT_EQ(sorted, expected);
289 }
290
291 TEST(remove_field_from_tx_extra, remove_first)
292 {
293 const uint8_t extra_arr[] = {1, 30, 208, 98, 162, 133, 64, 85, 83, 112, 91, 188, 89, 211, 24, 131, 39, 154, 22, 228,
294 80, 63, 198, 141, 173, 111, 244, 183, 4, 149, 186, 140, 230, 2, 1, 42};
295 std::vector<uint8_t> extra(&extra_arr[0], &extra_arr[0] + sizeof(extra_arr));
296
297 std::vector<cryptonote::tx_extra_field> tx_extra_fields;
298 ASSERT_TRUE(cryptonote::parse_tx_extra(extra, tx_extra_fields));
299 ASSERT_EQ(2, tx_extra_fields.size());
300 ASSERT_EQ(typeid(cryptonote::tx_extra_pub_key), tx_extra_fields[0].type());
301 ASSERT_EQ(typeid(cryptonote::tx_extra_nonce), tx_extra_fields[1].type());
302
303 tx_extra_fields.clear();
304 ASSERT_TRUE(cryptonote::remove_field_from_tx_extra(extra, typeid(cryptonote::tx_extra_pub_key)));
305 ASSERT_TRUE(cryptonote::parse_tx_extra(extra, tx_extra_fields));
306 ASSERT_EQ(1, tx_extra_fields.size());
307 ASSERT_EQ(typeid(cryptonote::tx_extra_nonce), tx_extra_fields[0].type());
308 }
309
310 TEST(remove_field_from_tx_extra, remove_last)
311 {
312 const uint8_t extra_arr[] = {1, 30, 208, 98, 162, 133, 64, 85, 83, 112, 91, 188, 89, 211, 24, 131, 39, 154, 22, 228,
313 80, 63, 198, 141, 173, 111, 244, 183, 4, 149, 186, 140, 230, 2, 1, 42};
314 std::vector<uint8_t> extra(&extra_arr[0], &extra_arr[0] + sizeof(extra_arr));
315
316 std::vector<cryptonote::tx_extra_field> tx_extra_fields;
317 ASSERT_TRUE(cryptonote::parse_tx_extra(extra, tx_extra_fields));
318 ASSERT_EQ(2, tx_extra_fields.size());
319 ASSERT_EQ(typeid(cryptonote::tx_extra_pub_key), tx_extra_fields[0].type());
320 ASSERT_EQ(typeid(cryptonote::tx_extra_nonce), tx_extra_fields[1].type());
321
322 tx_extra_fields.clear();
323 ASSERT_TRUE(cryptonote::remove_field_from_tx_extra(extra, typeid(cryptonote::tx_extra_nonce)));
324 ASSERT_TRUE(cryptonote::parse_tx_extra(extra, tx_extra_fields));
325 ASSERT_EQ(1, tx_extra_fields.size());
326 ASSERT_EQ(typeid(cryptonote::tx_extra_pub_key), tx_extra_fields[0].type());
327 }
328
329 TEST(remove_field_from_tx_extra, remove_middle)
330 {
331 const uint8_t extra_arr[] = {1, 30, 208, 98, 162, 133, 64, 85, 83, 112, 91, 188, 89, 211, 24, 131, 39, 154, 22, 228,
332 80, 63, 198, 141, 173, 111, 244, 183, 4, 149, 186, 140, 230, 2, 1, 42, 1, 30, 208, 98, 162, 133, 64, 85, 83, 112,
333 91, 188, 89, 211, 24, 131, 39, 154, 22, 228, 80, 63, 198, 141, 173, 111, 244, 183, 4, 149, 186, 140, 230};
334 std::vector<uint8_t> extra(&extra_arr[0], &extra_arr[0] + sizeof(extra_arr));
335
336 std::vector<cryptonote::tx_extra_field> tx_extra_fields;
337 ASSERT_TRUE(cryptonote::parse_tx_extra(extra, tx_extra_fields));
338 ASSERT_EQ(3, tx_extra_fields.size());
339 ASSERT_EQ(typeid(cryptonote::tx_extra_pub_key), tx_extra_fields[0].type());
340 ASSERT_EQ(typeid(cryptonote::tx_extra_nonce), tx_extra_fields[1].type());
341 ASSERT_EQ(typeid(cryptonote::tx_extra_pub_key), tx_extra_fields[2].type());
342
343 tx_extra_fields.clear();
344 ASSERT_TRUE(cryptonote::remove_field_from_tx_extra(extra, typeid(cryptonote::tx_extra_nonce)));
345 ASSERT_TRUE(cryptonote::parse_tx_extra(extra, tx_extra_fields));
346 ASSERT_EQ(2, tx_extra_fields.size());
347 ASSERT_EQ(typeid(cryptonote::tx_extra_pub_key), tx_extra_fields[0].type());
348 ASSERT_EQ(typeid(cryptonote::tx_extra_pub_key), tx_extra_fields[0].type());
349 }
350
351 TEST(remove_field_from_tx_extra, invalid_varint)
352 {
353 const uint8_t extra_arr[] = {1, 30, 208, 98, 162, 133, 64, 85, 83, 112, 91, 188, 89, 211, 24, 131, 39, 154, 22, 228,
354 80, 63, 198, 141, 173, 111, 244, 183, 4, 149, 186, 140, 230, 2, 0x80, 0};
355 std::vector<uint8_t> extra(&extra_arr[0], &extra_arr[0] + sizeof(extra_arr));
356
357 ASSERT_FALSE(cryptonote::remove_field_from_tx_extra(extra, typeid(cryptonote::tx_extra_nonce)));
358 ASSERT_EQ(sizeof(extra_arr), extra.size());
359 }