Electroneum
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 
45 TEST(uri, empty_string)
46 {
47  PARSE_URI("", false);
48 }
49 
50 TEST(uri, no_scheme)
51 {
52  PARSE_URI("electroneum", false);
53 }
54 
55 TEST(uri, bad_scheme)
56 {
57  PARSE_URI("http://foo", false);
58 }
59 
60 TEST(uri, scheme_not_first)
61 {
62  PARSE_URI(" electroneum:", false);
63 }
64 
65 TEST(uri, no_body)
66 {
67  PARSE_URI("electroneum:", false);
68 }
69 
70 TEST(uri, no_address)
71 {
72  PARSE_URI("electroneum:?", false);
73 }
74 
75 TEST(uri, bad_address)
76 {
77  PARSE_URI("electroneum:44444", false);
78 }
79 
80 TEST(uri, good_address)
81 {
82  PARSE_URI("electroneum:" TEST_ADDRESS, true);
84 }
85 
86 TEST(uri, good_integrated_address)
87 {
88  PARSE_URI("electroneum:" TEST_INTEGRATED_ADDRESS, true);
89 }
90 
91 TEST(uri, parameter_without_inter)
92 {
93  PARSE_URI("electroneum:" TEST_ADDRESS"&amount=1", false);
94 }
95 
96 TEST(uri, parameter_without_equals)
97 {
98  PARSE_URI("electroneum:" TEST_ADDRESS"?amount", false);
99 }
100 
101 TEST(uri, parameter_without_value)
102 {
103  PARSE_URI("electroneum:" TEST_ADDRESS"?tx_amount=", false);
104 }
105 
106 TEST(uri, negative_amount)
107 {
108  PARSE_URI("electroneum:" TEST_ADDRESS"?tx_amount=-1", false);
109 }
110 
111 TEST(uri, bad_amount)
112 {
113  PARSE_URI("electroneum:" TEST_ADDRESS"?tx_amount=alphanumeric", false);
114 }
115 
116 TEST(uri, duplicate_parameter)
117 {
118  PARSE_URI("electroneum:" TEST_ADDRESS"?tx_amount=1&tx_amount=1", false);
119 }
120 
121 TEST(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 
128 TEST(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 
136 TEST(uri, empty_payment_id)
137 {
138  PARSE_URI("electroneum:" TEST_ADDRESS"?tx_payment_id=", false);
139 }
140 
141 TEST(uri, bad_payment_id)
142 {
143  PARSE_URI("electroneum:" TEST_ADDRESS"?tx_payment_id=1234567890", false);
144 }
145 
146 TEST(uri, short_payment_id)
147 {
148  PARSE_URI("electroneum:" TEST_ADDRESS"?tx_payment_id=1234567890123456", true);
149 }
150 
151 TEST(uri, long_payment_id)
152 {
153  PARSE_URI("electroneum:" TEST_ADDRESS"?tx_payment_id=1234567890123456789012345678901234567890123456789012345678901234", true);
155  ASSERT_EQ(payment_id, "1234567890123456789012345678901234567890123456789012345678901234");
156 }
157 
158 TEST(uri, payment_id_with_integrated_address)
159 {
160  PARSE_URI("electroneum:" TEST_INTEGRATED_ADDRESS"?tx_payment_id=1234567890123456", false);
161 }
162 
163 TEST(uri, empty_description)
164 {
165  PARSE_URI("electroneum:" TEST_ADDRESS"?tx_description=", true);
166  ASSERT_EQ(description, "");
167 }
168 
169 TEST(uri, empty_recipient_name)
170 {
171  PARSE_URI("electroneum:" TEST_ADDRESS"?recipient_name=", true);
172  ASSERT_EQ(recipient_name, "");
173 }
174 
175 TEST(uri, non_empty_description)
176 {
177  PARSE_URI("electroneum:" TEST_ADDRESS"?tx_description=foo", true);
178  ASSERT_EQ(description, "foo");
179 }
180 
181 TEST(uri, non_empty_recipient_name)
182 {
183  PARSE_URI("electroneum:" TEST_ADDRESS"?recipient_name=foo", true);
184  ASSERT_EQ(recipient_name, "foo");
185 }
186 
187 TEST(uri, url_encoding)
188 {
189  PARSE_URI("electroneum:" TEST_ADDRESS"?tx_description=foo%20bar", true);
190  ASSERT_EQ(description, "foo bar");
191 }
192 
193 TEST(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 
199 TEST(uri, truncated_url_encoding)
200 {
201  PARSE_URI("electroneum:" TEST_ADDRESS"?tx_description=foo%2", true);
202  ASSERT_EQ(description, "foo%2");
203 }
204 
205 TEST(uri, percent_without_url_encoding)
206 {
207  PARSE_URI("electroneum:" TEST_ADDRESS"?tx_description=foo%", true);
208  ASSERT_EQ(description, "foo%");
209 }
210 
211 TEST(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 TEST_INTEGRATED_ADDRESS
Definition: uri.cpp:34
#define ASSERT_EQ(val1, val2)
Definition: gtest.h:1956
#define TEST_ADDRESS
Definition: uri.cpp:33
TEST(uri, empty_string)
Definition: uri.cpp:45
#define PARSE_URI(uri, expected)
Definition: uri.cpp:37
const char * address
Definition: multisig.cpp:37