| 1 |
// Copyright (c) 2016-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 |
#include "gtest/gtest.h" |
| 30 |
#include "wallet/wallet2.h" |
| 31 |
|
| 32 |
#define TEST_ADDRESS "9tTLtauaEKSj7xoVXytVH32R1pLZBk4VV4mZFGEh4wkXhDWqw1soPyf3fGixf1kni31VznEZkWNEza9d5TvjWwq5PaohYHC" |
| 33 |
#define TEST_INTEGRATED_ADDRESS "A4A1uPj4qaxj7xoVXytVH32R1pLZBk4VV4mZFGEh4wkXhDWqw1soPyf3fGixf1kni31VznEZkWNEza9d5TvjWwq5acaPMJfMbn3ReTsBpp" |
| 34 |
// included payment id: <f612cac0b6cb1cda> |
| 35 |
|
| 36 |
#define PARSE_URI(uri, expected) \ |
| 37 |
std::string address, payment_id, recipient_name, description, error; \ |
| 38 |
uint64_t amount; \ |
| 39 |
std::vector<std::string> unknown_parameters; \ |
| 40 |
tools::wallet2 w(cryptonote::TESTNET); \ |
| 41 |
bool ret = w.parse_uri(uri, address, payment_id, amount, description, recipient_name, unknown_parameters, error); \ |
| 42 |
ASSERT_EQ(ret, expected); |
| 43 |
|
| 44 |
TEST(uri, empty_string) |
| 45 |
{ |
| 46 |
PARSE_URI("", false); |
| 47 |
} |
| 48 |
|
| 49 |
TEST(uri, no_scheme) |
| 50 |
{ |
| 51 |
PARSE_URI("monero", false); |
| 52 |
} |
| 53 |
|
| 54 |
TEST(uri, bad_scheme) |
| 55 |
{ |
| 56 |
PARSE_URI("http://foo", false); |
| 57 |
} |
| 58 |
|
| 59 |
TEST(uri, scheme_not_first) |
| 60 |
{ |
| 61 |
PARSE_URI(" monero:", false); |
| 62 |
} |
| 63 |
|
| 64 |
TEST(uri, no_body) |
| 65 |
{ |
| 66 |
PARSE_URI("monero:", false); |
| 67 |
} |
| 68 |
|
| 69 |
TEST(uri, no_address) |
| 70 |
{ |
| 71 |
PARSE_URI("monero:?", false); |
| 72 |
} |
| 73 |
|
| 74 |
TEST(uri, bad_address) |
| 75 |
{ |
| 76 |
PARSE_URI("monero:44444", false); |
| 77 |
} |
| 78 |
|
| 79 |
TEST(uri, good_address) |
| 80 |
{ |
| 81 |
PARSE_URI("monero:" TEST_ADDRESS, true); |
| 82 |
ASSERT_EQ(address, TEST_ADDRESS); |
| 83 |
} |
| 84 |
|
| 85 |
TEST(uri, good_integrated_address) |
| 86 |
{ |
| 87 |
PARSE_URI("monero:" TEST_INTEGRATED_ADDRESS, true); |
| 88 |
} |
| 89 |
|
| 90 |
TEST(uri, parameter_without_inter) |
| 91 |
{ |
| 92 |
PARSE_URI("monero:" TEST_ADDRESS"&amount=1", false); |
| 93 |
} |
| 94 |
|
| 95 |
TEST(uri, parameter_without_equals) |
| 96 |
{ |
| 97 |
PARSE_URI("monero:" TEST_ADDRESS"?amount", false); |
| 98 |
} |
| 99 |
|
| 100 |
TEST(uri, parameter_without_value) |
| 101 |
{ |
| 102 |
PARSE_URI("monero:" TEST_ADDRESS"?tx_amount=", false); |
| 103 |
} |
| 104 |
|
| 105 |
TEST(uri, negative_amount) |
| 106 |
{ |
| 107 |
PARSE_URI("monero:" TEST_ADDRESS"?tx_amount=-1", false); |
| 108 |
} |
| 109 |
|
| 110 |
TEST(uri, bad_amount) |
| 111 |
{ |
| 112 |
PARSE_URI("monero:" TEST_ADDRESS"?tx_amount=alphanumeric", false); |
| 113 |
} |
| 114 |
|
| 115 |
TEST(uri, duplicate_parameter) |
| 116 |
{ |
| 117 |
PARSE_URI("monero:" TEST_ADDRESS"?tx_amount=1&tx_amount=1", false); |
| 118 |
} |
| 119 |
|
| 120 |
TEST(uri, unknown_parameter) |
| 121 |
{ |
| 122 |
PARSE_URI("monero:" TEST_ADDRESS"?unknown=1", true); |
| 123 |
ASSERT_EQ(unknown_parameters.size(), 1); |
| 124 |
ASSERT_EQ(unknown_parameters[0], "unknown=1"); |
| 125 |
} |
| 126 |
|
| 127 |
TEST(uri, unknown_parameters) |
| 128 |
{ |
| 129 |
PARSE_URI("monero:" TEST_ADDRESS"?tx_amount=1&unknown=1&tx_description=desc&foo=bar", true); |
| 130 |
ASSERT_EQ(unknown_parameters.size(), 2); |
| 131 |
ASSERT_EQ(unknown_parameters[0], "unknown=1"); |
| 132 |
ASSERT_EQ(unknown_parameters[1], "foo=bar"); |
| 133 |
} |
| 134 |
|
| 135 |
TEST(uri, empty_payment_id) |
| 136 |
{ |
| 137 |
PARSE_URI("monero:" TEST_ADDRESS"?tx_payment_id=", false); |
| 138 |
} |
| 139 |
|
| 140 |
TEST(uri, bad_payment_id) |
| 141 |
{ |
| 142 |
PARSE_URI("monero:" TEST_ADDRESS"?tx_payment_id=1234567890", false); |
| 143 |
} |
| 144 |
|
| 145 |
TEST(uri, short_payment_id) |
| 146 |
{ |
| 147 |
PARSE_URI("monero:" TEST_ADDRESS"?tx_payment_id=1234567890123456", false); |
| 148 |
} |
| 149 |
|
| 150 |
TEST(uri, long_payment_id) |
| 151 |
{ |
| 152 |
PARSE_URI("monero:" TEST_ADDRESS"?tx_payment_id=1234567890123456789012345678901234567890123456789012345678901234", true); |
| 153 |
ASSERT_EQ(address, TEST_ADDRESS); |
| 154 |
ASSERT_EQ(payment_id, "1234567890123456789012345678901234567890123456789012345678901234"); |
| 155 |
} |
| 156 |
|
| 157 |
TEST(uri, payment_id_with_integrated_address) |
| 158 |
{ |
| 159 |
PARSE_URI("monero:" TEST_INTEGRATED_ADDRESS"?tx_payment_id=1234567890123456", false); |
| 160 |
} |
| 161 |
|
| 162 |
TEST(uri, empty_description) |
| 163 |
{ |
| 164 |
PARSE_URI("monero:" TEST_ADDRESS"?tx_description=", true); |
| 165 |
ASSERT_EQ(description, ""); |
| 166 |
} |
| 167 |
|
| 168 |
TEST(uri, empty_recipient_name) |
| 169 |
{ |
| 170 |
PARSE_URI("monero:" TEST_ADDRESS"?recipient_name=", true); |
| 171 |
ASSERT_EQ(recipient_name, ""); |
| 172 |
} |
| 173 |
|
| 174 |
TEST(uri, non_empty_description) |
| 175 |
{ |
| 176 |
PARSE_URI("monero:" TEST_ADDRESS"?tx_description=foo", true); |
| 177 |
ASSERT_EQ(description, "foo"); |
| 178 |
} |
| 179 |
|
| 180 |
TEST(uri, non_empty_recipient_name) |
| 181 |
{ |
| 182 |
PARSE_URI("monero:" TEST_ADDRESS"?recipient_name=foo", true); |
| 183 |
ASSERT_EQ(recipient_name, "foo"); |
| 184 |
} |
| 185 |
|
| 186 |
TEST(uri, url_encoding) |
| 187 |
{ |
| 188 |
PARSE_URI("monero:" TEST_ADDRESS"?tx_description=foo%20bar", true); |
| 189 |
ASSERT_EQ(description, "foo bar"); |
| 190 |
} |
| 191 |
|
| 192 |
TEST(uri, non_alphanumeric_url_encoding) |
| 193 |
{ |
| 194 |
PARSE_URI("monero:" TEST_ADDRESS"?tx_description=foo%2x", true); |
| 195 |
ASSERT_EQ(description, "foo%2x"); |
| 196 |
} |
| 197 |
|
| 198 |
TEST(uri, truncated_url_encoding) |
| 199 |
{ |
| 200 |
PARSE_URI("monero:" TEST_ADDRESS"?tx_description=foo%2", true); |
| 201 |
ASSERT_EQ(description, "foo%2"); |
| 202 |
} |
| 203 |
|
| 204 |
TEST(uri, percent_without_url_encoding) |
| 205 |
{ |
| 206 |
PARSE_URI("monero:" TEST_ADDRESS"?tx_description=foo%", true); |
| 207 |
ASSERT_EQ(description, "foo%"); |
| 208 |
} |
| 209 |
|
| 210 |
TEST(uri, url_encoded_once) |
| 211 |
{ |
| 212 |
PARSE_URI("monero:" TEST_ADDRESS"?tx_description=foo%2020", true); |
| 213 |
ASSERT_EQ(description, "foo 20"); |
| 214 |
} |
| 215 |
|