Electroneum
Loading...
Searching...
No Matches
uri.cpp
Go to the documentation of this file.
1// Copyright (c) 2017-Present, Electroneum
2// Copyright (c) 2016-2019, The Monero Project
3//
4// All rights reserved.
5//
6// Redistribution and use in source and binary forms, with or without modification, are
7// permitted provided that the following conditions are met:
8//
9// 1. Redistributions of source code must retain the above copyright notice, this list of
10// conditions and the following disclaimer.
11//
12// 2. Redistributions in binary form must reproduce the above copyright notice, this list
13// of conditions and the following disclaimer in the documentation and/or other
14// materials provided with the distribution.
15//
16// 3. Neither the name of the copyright holder nor the names of its contributors may be
17// used to endorse or promote products derived from this software without specific
18// prior written permission.
19//
20// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
21// EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
22// MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
23// THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
25// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
27// STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
28// THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29
30#include "gtest/gtest.h"
31#include "wallet/wallet2.h"
32
33#define TEST_ADDRESS "etnjzhAXzXYYXSVmejcTDohsU2cyxksnLYr7Pap4mFx3UCArZoubGYyCsojTmLjXFHYUQAegfNiv9ZjG5a9o8zfy4a35zqbfpw"
34#define TEST_INTEGRATED_ADDRESS "f4VR1Vz2bo4YXSVmejcTDohsU2cyxksnLYr7Pap4mFx3UCArZoubGYyCsojTmLjXFHYUQAegfNiv9ZjG5a9o8zfyGkpDJ6jzUgm8hRv1hAVS2"
35// included payment id: <f612cac0b6cb1cda>
36
37#define PARSE_URI(uri, expected) \
38 std::string address, payment_id, recipient_name, description, error; \
39 uint64_t amount; \
40 std::vector<std::string> unknown_parameters; \
41 tools::wallet2 w(cryptonote::TESTNET); \
42 bool ret = w.parse_uri(uri, address, payment_id, amount, description, recipient_name, unknown_parameters, error); \
43 ASSERT_EQ(ret, expected);
44
45TEST(uri, empty_string)
46{
47 PARSE_URI("", false);
48}
49
50TEST(uri, no_scheme)
51{
52 PARSE_URI("electroneum", false);
53}
54
55TEST(uri, bad_scheme)
56{
57 PARSE_URI("http://foo", false);
58}
59
60TEST(uri, scheme_not_first)
61{
62 PARSE_URI(" electroneum:", false);
63}
64
65TEST(uri, no_body)
66{
67 PARSE_URI("electroneum:", false);
68}
69
70TEST(uri, no_address)
71{
72 PARSE_URI("electroneum:?", false);
73}
74
75TEST(uri, bad_address)
76{
77 PARSE_URI("electroneum:44444", false);
78}
79
80TEST(uri, good_address)
81{
82 PARSE_URI("electroneum:" TEST_ADDRESS, true);
84}
85
86TEST(uri, good_integrated_address)
87{
88 PARSE_URI("electroneum:" TEST_INTEGRATED_ADDRESS, true);
89}
90
91TEST(uri, parameter_without_inter)
92{
93 PARSE_URI("electroneum:" TEST_ADDRESS"&amount=1", false);
94}
95
96TEST(uri, parameter_without_equals)
97{
98 PARSE_URI("electroneum:" TEST_ADDRESS"?amount", false);
99}
100
101TEST(uri, parameter_without_value)
102{
103 PARSE_URI("electroneum:" TEST_ADDRESS"?tx_amount=", false);
104}
105
106TEST(uri, negative_amount)
107{
108 PARSE_URI("electroneum:" TEST_ADDRESS"?tx_amount=-1", false);
109}
110
111TEST(uri, bad_amount)
112{
113 PARSE_URI("electroneum:" TEST_ADDRESS"?tx_amount=alphanumeric", false);
114}
115
116TEST(uri, duplicate_parameter)
117{
118 PARSE_URI("electroneum:" TEST_ADDRESS"?tx_amount=1&tx_amount=1", false);
119}
120
121TEST(uri, unknown_parameter)
122{
123 PARSE_URI("electroneum:" TEST_ADDRESS"?unknown=1", true);
124 ASSERT_EQ(unknown_parameters.size(), 1);
125 ASSERT_EQ(unknown_parameters[0], "unknown=1");
126}
127
128TEST(uri, unknown_parameters)
129{
130 PARSE_URI("electroneum:" TEST_ADDRESS"?tx_amount=1&unknown=1&tx_description=desc&foo=bar", true);
131 ASSERT_EQ(unknown_parameters.size(), 2);
132 ASSERT_EQ(unknown_parameters[0], "unknown=1");
133 ASSERT_EQ(unknown_parameters[1], "foo=bar");
134}
135
136TEST(uri, empty_payment_id)
137{
138 PARSE_URI("electroneum:" TEST_ADDRESS"?tx_payment_id=", false);
139}
140
141TEST(uri, bad_payment_id)
142{
143 PARSE_URI("electroneum:" TEST_ADDRESS"?tx_payment_id=1234567890", false);
144}
145
146TEST(uri, short_payment_id)
147{
148 PARSE_URI("electroneum:" TEST_ADDRESS"?tx_payment_id=1234567890123456", true);
149}
150
151TEST(uri, long_payment_id)
152{
153 PARSE_URI("electroneum:" TEST_ADDRESS"?tx_payment_id=1234567890123456789012345678901234567890123456789012345678901234", true);
155 ASSERT_EQ(payment_id, "1234567890123456789012345678901234567890123456789012345678901234");
156}
157
158TEST(uri, payment_id_with_integrated_address)
159{
160 PARSE_URI("electroneum:" TEST_INTEGRATED_ADDRESS"?tx_payment_id=1234567890123456", false);
161}
162
163TEST(uri, empty_description)
164{
165 PARSE_URI("electroneum:" TEST_ADDRESS"?tx_description=", true);
166 ASSERT_EQ(description, "");
167}
168
169TEST(uri, empty_recipient_name)
170{
171 PARSE_URI("electroneum:" TEST_ADDRESS"?recipient_name=", true);
172 ASSERT_EQ(recipient_name, "");
173}
174
175TEST(uri, non_empty_description)
176{
177 PARSE_URI("electroneum:" TEST_ADDRESS"?tx_description=foo", true);
178 ASSERT_EQ(description, "foo");
179}
180
181TEST(uri, non_empty_recipient_name)
182{
183 PARSE_URI("electroneum:" TEST_ADDRESS"?recipient_name=foo", true);
184 ASSERT_EQ(recipient_name, "foo");
185}
186
187TEST(uri, url_encoding)
188{
189 PARSE_URI("electroneum:" TEST_ADDRESS"?tx_description=foo%20bar", true);
190 ASSERT_EQ(description, "foo bar");
191}
192
193TEST(uri, non_alphanumeric_url_encoding)
194{
195 PARSE_URI("electroneum:" TEST_ADDRESS"?tx_description=foo%2x", true);
196 ASSERT_EQ(description, "foo%2x");
197}
198
199TEST(uri, truncated_url_encoding)
200{
201 PARSE_URI("electroneum:" TEST_ADDRESS"?tx_description=foo%2", true);
202 ASSERT_EQ(description, "foo%2");
203}
204
205TEST(uri, percent_without_url_encoding)
206{
207 PARSE_URI("electroneum:" TEST_ADDRESS"?tx_description=foo%", true);
208 ASSERT_EQ(description, "foo%");
209}
210
211TEST(uri, url_encoded_once)
212{
213 PARSE_URI("electroneum:" TEST_ADDRESS"?tx_description=foo%2020", true);
214 ASSERT_EQ(description, "foo 20");
215}
216
#define ASSERT_EQ(val1, val2)
Definition gtest.h:1956
#define TEST(test_case_name, test_name)
Definition gtest.h:2187
const char * address
Definition multisig.cpp:37
#define TEST_INTEGRATED_ADDRESS
Definition uri.cpp:34
#define TEST_ADDRESS
Definition uri.cpp:33
#define PARSE_URI(uri, expected)
Definition uri.cpp:37