Debian Codegraph @

tests/functional_tests/uri.py

raw symbols SCIP
1 #!/usr/bin/env python3
2 #encoding=utf-8
3
4 # Copyright (c) 2019-2022, The Monero Project
5 #
6 # All rights reserved.
7 #
8 # Redistribution and use in source and binary forms, with or without modification, are
9 # permitted provided that the following conditions are met:
10 #
11 # 1. Redistributions of source code must retain the above copyright notice, this list of
12 # conditions and the following disclaimer.
13 #
14 # 2. Redistributions in binary form must reproduce the above copyright notice, this list
15 # of conditions and the following disclaimer in the documentation and/or other
16 # materials provided with the distribution.
17 #
18 # 3. Neither the name of the copyright holder nor the names of its contributors may be
19 # used to endorse or promote products derived from this software without specific
20 # prior written permission.
21 #
22 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
23 # EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
24 # MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
25 # THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26 # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
27 # PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28 # INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
29 # STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
30 # THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31
32 """Test URI RPC
33 """
34
35 from __future__ import print_function
36 try:
37 from urllib import quote as urllib_quote
38 except:
39 from urllib.parse import quote as urllib_quote
40
41 from framework.wallet import Wallet
42
43 class URITest():
44 def run_test(self):
45 self.create()
46 self.test_monero_uri()
47
48 def create(self):
49 print('Creating wallet')
50 wallet = Wallet()
51 # close the wallet if any, will throw if none is loaded
52 try: wallet.close_wallet()
53 except: pass
54 seed = 'velvet lymph giddy number token physics poetry unquoted nibs useful sabotage limits benches lifestyle eden nitrogen anvil fewest avoid batch vials washing fences goat unquoted'
55 res = wallet.restore_deterministic_wallet(seed = seed)
56 assert res.address == '42ey1afDFnn4886T7196doS9GPMzexD9gXpsZJDwVjeRVdFCSoHnv7KPbBeGpzJBzHRCAs9UxqeoyFQMYbqSWYTfJJQAWDm'
57 assert res.seed == seed
58
59 def test_monero_uri(self):
60 print('Testing monero: URI')
61 wallet = Wallet()
62
63 utf8string = [u'えんしゅう', u'あまやかす']
64 quoted_utf8string = [urllib_quote(x.encode('utf8')) for x in utf8string]
65
66 ok = False
67 try: res = wallet.make_uri()
68 except: ok = True
69 assert ok
70 ok = False
71 try: res = wallet.make_uri(address = '')
72 except: ok = True
73 assert ok
74 ok = False
75 try: res = wallet.make_uri(address = 'kjshdkj')
76 except: ok = True
77 assert ok
78
79 for address in [
80 '42ey1afDFnn4886T7196doS9GPMzexD9gXpsZJDwVjeRVdFCSoHnv7KPbBeGpzJBzHRCAs9UxqeoyFQMYbqSWYTfJJQAWDm',
81 '4BxSHvcgTwu25WooY4BVmgdcKwZu5EksVZSZkDd6ooxSVVqQ4ubxXkhLF6hEqtw96i9cf3cVfLw8UWe95bdDKfRQeYtPwLm1Jiw7AKt2LY',
82 '8AsN91rznfkBGTY8psSNkJBg9SZgxxGGRUhGwRptBhgr5XSQ1XzmA9m8QAnoxydecSh5aLJXdrgXwTDMMZ1AuXsN1EX5Mtm'
83 ]:
84 res = wallet.make_uri(address = address)
85 assert res.uri == 'monero:' + address
86 res = wallet.parse_uri(res.uri)
87 assert res.uri.address == address
88 assert res.uri.payment_id == ''
89 assert res.uri.amount == 0
90 assert res.uri.tx_description == ''
91 assert res.uri.recipient_name == ''
92 assert not 'unknown_parameters' in res or len(res.unknown_parameters) == 0
93 res = wallet.make_uri(address = address, amount = 11000000000)
94 assert res.uri == 'monero:' + address + '?tx_amount=0.011' or res.uri == 'monero:' + address + '?tx_amount=0.011000000000'
95 res = wallet.parse_uri(res.uri)
96 assert res.uri.address == address
97 assert res.uri.payment_id == ''
98 assert res.uri.amount == 11000000000
99 assert res.uri.tx_description == ''
100 assert res.uri.recipient_name == ''
101 assert not 'unknown_parameters' in res or len(res.unknown_parameters) == 0
102
103 address = '42ey1afDFnn4886T7196doS9GPMzexD9gXpsZJDwVjeRVdFCSoHnv7KPbBeGpzJBzHRCAs9UxqeoyFQMYbqSWYTfJJQAWDm'
104
105 res = wallet.make_uri(address = address, tx_description = utf8string[0])
106 assert res.uri == 'monero:' + address + '?tx_description=' + quoted_utf8string[0]
107 res = wallet.parse_uri(res.uri)
108 assert res.uri.address == address
109 assert res.uri.payment_id == ''
110 assert res.uri.amount == 0
111 assert res.uri.tx_description == utf8string[0]
112 assert res.uri.recipient_name == ''
113 assert not 'unknown_parameters' in res or len(res.unknown_parameters) == 0
114
115 res = wallet.make_uri(address = address, recipient_name = utf8string[0])
116 assert res.uri == 'monero:' + address + '?recipient_name=' + quoted_utf8string[0]
117 res = wallet.parse_uri(res.uri)
118 assert res.uri.address == address
119 assert res.uri.payment_id == ''
120 assert res.uri.amount == 0
121 assert res.uri.tx_description == ''
122 assert res.uri.recipient_name == utf8string[0]
123 assert not 'unknown_parameters' in res or len(res.unknown_parameters) == 0
124
125 res = wallet.make_uri(address = address, recipient_name = utf8string[0], tx_description = utf8string[1])
126 assert res.uri == 'monero:' + address + '?recipient_name=' + quoted_utf8string[0] + '&tx_description=' + quoted_utf8string[1]
127 res = wallet.parse_uri(res.uri)
128 assert res.uri.address == address
129 assert res.uri.payment_id == ''
130 assert res.uri.amount == 0
131 assert res.uri.tx_description == utf8string[1]
132 assert res.uri.recipient_name == utf8string[0]
133 assert not 'unknown_parameters' in res or len(res.unknown_parameters) == 0
134
135 res = wallet.make_uri(address = address, recipient_name = utf8string[0], tx_description = utf8string[1], amount = 1000000000000)
136 assert res.uri == 'monero:' + address + '?tx_amount=1.000000000000&recipient_name=' + quoted_utf8string[0] + '&tx_description=' + quoted_utf8string[1]
137 res = wallet.parse_uri(res.uri)
138 assert res.uri.address == address
139 assert res.uri.payment_id == ''
140 assert res.uri.amount == 1000000000000
141 assert res.uri.tx_description == utf8string[1]
142 assert res.uri.recipient_name == utf8string[0]
143 assert not 'unknown_parameters' in res or len(res.unknown_parameters) == 0
144
145 # external payment ids are not supported anymore
146 ok = False
147 try: res = wallet.make_uri(address = address, recipient_name = utf8string[0], tx_description = utf8string[1], amount = 1000000000000, payment_id = '1' * 64)
148 except: ok = True
149 assert ok
150
151 # spaces must be encoded as %20
152 res = wallet.make_uri(address = address, tx_description = ' ' + utf8string[1] + ' ' + utf8string[0] + ' ', amount = 1000000000000)
153 assert res.uri == 'monero:' + address + '?tx_amount=1.000000000000&tx_description=%20' + quoted_utf8string[1] + '%20' + quoted_utf8string[0] + '%20'
154 res = wallet.parse_uri(res.uri)
155 assert res.uri.address == address
156 assert res.uri.payment_id == ''
157 assert res.uri.amount == 1000000000000
158 assert res.uri.tx_description == ' ' + utf8string[1] + ' ' + utf8string[0] + ' '
159 assert res.uri.recipient_name == ''
160 assert not 'unknown_parameters' in res or len(res.unknown_parameters) == 0
161
162 # the example from the docs
163 res = wallet.parse_uri('monero:46BeWrHpwXmHDpDEUmZBWZfoQpdc6HaERCNmx1pEYL2rAcuwufPN9rXHHtyUA4QVy66qeFQkn6sfK8aHYjA3jk3o1Bv16em?tx_amount=239.39014&tx_description=donation')
164 assert res.uri.address == '46BeWrHpwXmHDpDEUmZBWZfoQpdc6HaERCNmx1pEYL2rAcuwufPN9rXHHtyUA4QVy66qeFQkn6sfK8aHYjA3jk3o1Bv16em'
165 assert res.uri.amount == 239390140000000
166 assert res.uri.tx_description == 'donation'
167 assert res.uri.recipient_name == ''
168 assert res.uri.payment_id == ''
169 assert not 'unknown_parameters' in res or len(res.unknown_parameters) == 0
170
171 # malformed/invalid
172 for uri in [
173 '',
174 ':',
175 'monero',
176 'notmonero:42ey1afDFnn4886T7196doS9GPMzexD9gXpsZJDwVjeRVdFCSoHnv7KPbBeGpzJBzHRCAs9UxqeoyFQMYbqSWYTfJJQAWDm',
177 'MONERO:42ey1afDFnn4886T7196doS9GPMzexD9gXpsZJDwVjeRVdFCSoHnv7KPbBeGpzJBzHRCAs9UxqeoyFQMYbqSWYTfJJQAWDm',
178 'MONERO::42ey1afDFnn4886T7196doS9GPMzexD9gXpsZJDwVjeRVdFCSoHnv7KPbBeGpzJBzHRCAs9UxqeoyFQMYbqSWYTfJJQAWDm',
179 'monero:',
180 'monero:badaddress',
181 'monero:tx_amount=10',
182 'monero:?tx_amount=10',
183 'monero:42ey1afDFnn4886T7196doS9GPMzexD9gXpsZJDwVjeRVdFCSoHnv7KPbBeGpzJBzHRCAs9UxqeoyFQMYbqSWYTfJJQAWDm?tx_amount=-1',
184 'monero:42ey1afDFnn4886T7196doS9GPMzexD9gXpsZJDwVjeRVdFCSoHnv7KPbBeGpzJBzHRCAs9UxqeoyFQMYbqSWYTfJJQAWDm?tx_amount=1e12',
185 'monero:42ey1afDFnn4886T7196doS9GPMzexD9gXpsZJDwVjeRVdFCSoHnv7KPbBeGpzJBzHRCAs9UxqeoyFQMYbqSWYTfJJQAWDm?tx_amount=+12',
186 'monero:42ey1afDFnn4886T7196doS9GPMzexD9gXpsZJDwVjeRVdFCSoHnv7KPbBeGpzJBzHRCAs9UxqeoyFQMYbqSWYTfJJQAWDm?tx_amount=1+2',
187 'monero:42ey1afDFnn4886T7196doS9GPMzexD9gXpsZJDwVjeRVdFCSoHnv7KPbBeGpzJBzHRCAs9UxqeoyFQMYbqSWYTfJJQAWDm?tx_amount=A',
188 'monero:42ey1afDFnn4886T7196doS9GPMzexD9gXpsZJDwVjeRVdFCSoHnv7KPbBeGpzJBzHRCAs9UxqeoyFQMYbqSWYTfJJQAWDm?tx_amount=0x2',
189 'monero:42ey1afDFnn4886T7196doS9GPMzexD9gXpsZJDwVjeRVdFCSoHnv7KPbBeGpzJBzHRCAs9UxqeoyFQMYbqSWYTfJJQAWDm?tx_amount=222222222222222222222',
190 'monero:42ey1afDFnn4886T7196doS9GPMzexD9gXpsZJDwVjeRVdFCSoHnv7KPbBeGpzJBzHRCAs9UxqeoyFQMYbqSWYTfJJQAWDn?tx_amount=10',
191 'monero:42ey1afDFnn4886T7196doS9GPMzexD9gXpsZJDwVjeRVdFCSoHnv7KPbBeGpzJBzHRCAs9UxqeoyFQMYbqSWYTfJJQAWDm&',
192 'monero:42ey1afDFnn4886T7196doS9GPMzexD9gXpsZJDwVjeRVdFCSoHnv7KPbBeGpzJBzHRCAs9UxqeoyFQMYbqSWYTfJJQAWDm&tx_amount',
193 'monero:42ey1afDFnn4886T7196doS9GPMzexD9gXpsZJDwVjeRVdFCSoHnv7KPbBeGpzJBzHRCAs9UxqeoyFQMYbqSWYTfJJQAWDm&tx_amount=',
194 'monero:42ey1afDFnn4886T7196doS9GPMzexD9gXpsZJDwVjeRVdFCSoHnv7KPbBeGpzJBzHRCAs9UxqeoyFQMYbqSWYTfJJQAWDm&tx_amount=10=',
195 'monero:42ey1afDFnn4886T7196doS9GPMzexD9gXpsZJDwVjeRVdFCSoHnv7KPbBeGpzJBzHRCAs9UxqeoyFQMYbqSWYTfJJQAWDm&tx_amount=10=&',
196 'monero:42ey1afDFnn4886T7196doS9GPMzexD9gXpsZJDwVjeRVdFCSoHnv7KPbBeGpzJBzHRCAs9UxqeoyFQMYbqSWYTfJJQAWDm&tx_amount=10=&foo=bar',
197 'monero:42ey1afDFnn4886T7196doS9GPMzexD9gXpsZJDwVjeRVdFCSoHnv7KPbBeGpzJBzHRCAs9UxqeoyFQMYbqSWYTfJJQAWDm?tx_amount=10&tx_amount=20',
198 'monero:42ey1afDFnn4886T7196doS9GPMzexD9gXpsZJDwVjeRVdFCSoHnv7KPbBeGpzJBzHRCAs9UxqeoyFQMYbqSWYTfJJQAWDm?tx_payment_id=1111111111111111',
199 'monero:4BxSHvcgTwu25WooY4BVmgdcKwZu5EksVZSZkDd6ooxSVVqQ4ubxXkhLF6hEqtw96i9cf3cVfLw8UWe95bdDKfRQeYtPwLm1Jiw7AKt2LY?tx_payment_id=' + '1' * 64,
200 'monero:9ujeXrjzf7bfeK3KZdCqnYaMwZVFuXemPU8Ubw335rj2FN1CdMiWNyFV3ksEfMFvRp9L9qum5UxkP5rN9aLcPxbH1au4WAB',
201 'monero:5K8mwfjumVseCcQEjNbf59Um6R9NfVUNkHTLhhPCmNvgDLVS88YW5tScnm83rw9mfgYtchtDDTW5jEfMhygi27j1QYphX38hg6m4VMtN29',
202 'monero:7A1Hr63MfgUa8pkWxueD5xBqhQczkusYiCMYMnJGcGmuQxa7aDBxN1G7iCuLCNB3VPeb2TW7U9FdxB27xKkWKfJ8VhUZthF',
203 ]:
204 ok = False
205 try: res = wallet.parse_uri(uri)
206 except: ok = True
207 assert ok, res
208
209 # unknown parameters but otherwise valid
210 res = wallet.parse_uri('monero:' + address + '?tx_amount=239.39014&foo=bar')
211 assert res.uri.address == address
212 assert res.uri.amount == 239390140000000
213 assert res.unknown_parameters == ['foo=bar'], res
214 res = wallet.parse_uri('monero:' + address + '?tx_amount=239.39014&foo=bar&baz=quux')
215 assert res.uri.address == address
216 assert res.uri.amount == 239390140000000
217 assert res.unknown_parameters == ['foo=bar', 'baz=quux'], res
218 res = wallet.parse_uri('monero:' + address + '?tx_amount=239.39014&%20=%20')
219 assert res.uri.address == address
220 assert res.uri.amount == 239390140000000
221 assert res.unknown_parameters == ['%20=%20'], res
222 res = wallet.parse_uri('monero:' + address + '?tx_amount=239.39014&unknown=' + quoted_utf8string[0])
223 assert res.uri.address == address
224 assert res.uri.amount == 239390140000000
225 assert res.unknown_parameters == [u'unknown=' + quoted_utf8string[0]], res
226
227
228
229 if __name__ == '__main__':
230 URITest().run_test()