Debian Codegraph @

tests/functional_tests/address_book.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 wallet address book RPC
33 """
34
35 from __future__ import print_function
36 from framework.wallet import Wallet
37
38 class AddressBookTest():
39 def run_test(self):
40 self.create()
41 self.test_address_book()
42
43 def create(self):
44 print('Creating wallet')
45 wallet = Wallet()
46 # close the wallet if any, will throw if none is loaded
47 try: wallet.close_wallet()
48 except: pass
49 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'
50 res = wallet.restore_deterministic_wallet(seed = seed)
51 assert res.address == '42ey1afDFnn4886T7196doS9GPMzexD9gXpsZJDwVjeRVdFCSoHnv7KPbBeGpzJBzHRCAs9UxqeoyFQMYbqSWYTfJJQAWDm'
52 assert res.seed == seed
53
54 def test_address_book(self):
55 print('Testing address book')
56 wallet = Wallet()
57
58 # empty at start
59 res = wallet.get_address_book()
60 assert not 'entries' in res or (res.entries) == 0
61 ok = False
62 try: wallet.get_address_book([0])
63 except: ok = True
64 assert ok
65 ok = False
66 try: wallet.delete_address_book(0)
67 except: ok = True
68 assert ok
69 ok = False
70 try: wallet.edit_address_book(0, description = '')
71 except: ok = True
72 assert ok
73
74 # add one
75 res = wallet.add_address_book('42ey1afDFnn4886T7196doS9GPMzexD9gXpsZJDwVjeRVdFCSoHnv7KPbBeGpzJBzHRCAs9UxqeoyFQMYbqSWYTfJJQAWDm', description = 'self')
76 assert res.index == 0, res
77 for get_all in [True, False]:
78 res = wallet.get_address_book() if get_all else wallet.get_address_book([0])
79 assert len(res.entries) == 1
80 e = res.entries[0]
81 assert e.index == 0
82 assert e.address == '42ey1afDFnn4886T7196doS9GPMzexD9gXpsZJDwVjeRVdFCSoHnv7KPbBeGpzJBzHRCAs9UxqeoyFQMYbqSWYTfJJQAWDm', e
83 assert e.description == 'self'
84
85 # add a duplicate
86 res = wallet.add_address_book('42ey1afDFnn4886T7196doS9GPMzexD9gXpsZJDwVjeRVdFCSoHnv7KPbBeGpzJBzHRCAs9UxqeoyFQMYbqSWYTfJJQAWDm', description = 'self')
87 assert res.index == 1
88 res = wallet.get_address_book()
89 assert len(res.entries) == 2
90 assert res.entries[0].index == 0
91 assert res.entries[1].index == 1
92 assert res.entries[0].address == res.entries[1].address
93 assert res.entries[0].description == res.entries[1].description
94 e = res.entries[1]
95 res = wallet.get_address_book([1])
96 assert len(res.entries) == 1
97 assert e == res.entries[0]
98
99 # request (partially) out of range
100 ok = False
101 try: res = wallet.get_address_book[4, 2]
102 except: ok = True
103 assert ok
104 ok = False
105 try: res = wallet.get_address_book[0, 2]
106 except: ok = True
107 assert ok
108 ok = False
109 try: res = wallet.get_address_book[2, 0]
110 except: ok = True
111 assert ok
112
113 # delete first
114 res = wallet.delete_address_book(0)
115 res = wallet.get_address_book()
116 assert len(res.entries) == 1
117 assert res.entries[0].index == 0
118 assert res.entries[0].address == e.address
119 assert res.entries[0].description == e.description
120
121 # delete (new) first
122 res = wallet.delete_address_book(0)
123 res = wallet.get_address_book()
124 assert not 'entries' in res or (res.entries) == 0
125
126 # add non-addresses
127 errors = 0
128 try: wallet.add_address_book('', description = 'bad')
129 except: errors += 1
130 try: wallet.add_address_book('42ey1afDFnn4886T7196doS9GPMzexD9gXpsZJDwVjeRVdFCSoHnv7KPbBeGpzJBzHRCAs9UxqeoyFQMYbqSWYTfJJQAWDm ', description = 'bad')
131 except: errors += 1
132 try: wallet.add_address_book('42ey1afDFnn4886T7196doS9GPMzexD9gXpsZJDwVjeRVdFCSoHnv7KPbBeGpzJBzHRCAs9UxqeoyFQMYbqSWYTfJJQAWDn', description = 'bad')
133 except: errors += 1
134 try: wallet.add_address_book('9ujeXrjzf7bfeK3KZdCqnYaMwZVFuXemPU8Ubw335rj2FN1CdMiWNyFV3ksEfMFvRp9L9qum5UxkP5rN9aLcPxbH1au4WAB', description = 'bad')
135 except: errors += 1
136 try: wallet.add_address_book('[email protected]', description = 'bad')
137 except: errors += 1
138 assert errors == 5
139 res = wallet.get_address_book()
140 assert not 'entries' in res or len(res.entries) == 0
141
142 # openalias
143 res = wallet.add_address_book('[email protected]', description = 'dev fund')
144 assert res.index == 0
145 res = wallet.get_address_book()
146 assert len(res.entries) == 1
147 e = res.entries[0]
148 assert e.address == '888tNkZrPN6JsEgekjMnABU4TBzc2Dt29EPAvkRxbANsAnjyPbb3iQ1YBRk1UXcdRsiKc9dhwMVgN5S9cQUiyoogDavup3H'
149 assert e.description == 'dev fund'
150
151 # UTF-8
152 res = wallet.add_address_book('42ey1afDFnn4886T7196doS9GPMzexD9gXpsZJDwVjeRVdFCSoHnv7KPbBeGpzJBzHRCAs9UxqeoyFQMYbqSWYTfJJQAWDm', description = u'あまやかす')
153 assert res.index == 1
154 res = wallet.get_address_book([1])
155 assert len(res.entries) == 1
156 assert res.entries[0].description == u'あまやかす'
157 e = res.entries[0]
158
159 # duplicate request
160 res = wallet.get_address_book([1, 1])
161 assert len(res.entries) == 2
162 assert res.entries[0] == e
163 assert res.entries[1] == e
164
165 # various address types
166 res = wallet.make_integrated_address()
167 integrated_address = res.integrated_address
168 res = wallet.add_address_book(integrated_address)
169 assert res.index == 2
170 res = wallet.add_address_book('87KfgTZ8ER5D3Frefqnrqif11TjVsTPaTcp37kqqKMrdDRUhpJRczeR7KiBmSHF32UJLP3HHhKUDmEQyJrv2mV8yFDCq8eB')
171 assert res.index == 3
172
173 # get them back
174 res = wallet.get_address_book([0])
175 assert len(res.entries) == 1
176 assert res.entries[0].address == '888tNkZrPN6JsEgekjMnABU4TBzc2Dt29EPAvkRxbANsAnjyPbb3iQ1YBRk1UXcdRsiKc9dhwMVgN5S9cQUiyoogDavup3H'
177 assert res.entries[0].description == 'dev fund'
178 res = wallet.get_address_book([1])
179 assert len(res.entries) == 1
180 assert res.entries[0].address == '42ey1afDFnn4886T7196doS9GPMzexD9gXpsZJDwVjeRVdFCSoHnv7KPbBeGpzJBzHRCAs9UxqeoyFQMYbqSWYTfJJQAWDm'
181 assert res.entries[0].description == u'あまやかす'
182 res = wallet.get_address_book([2])
183 assert len(res.entries) == 1
184 assert res.entries[0].address == integrated_address
185 res = wallet.get_address_book([3])
186 assert len(res.entries) == 1
187 assert res.entries[0].address == '87KfgTZ8ER5D3Frefqnrqif11TjVsTPaTcp37kqqKMrdDRUhpJRczeR7KiBmSHF32UJLP3HHhKUDmEQyJrv2mV8yFDCq8eB'
188
189 # edit
190 res = wallet.get_address_book([1])
191 assert len(res.entries) == 1
192 e = res.entries[0]
193 assert e.index == 1
194 assert e.address == '42ey1afDFnn4886T7196doS9GPMzexD9gXpsZJDwVjeRVdFCSoHnv7KPbBeGpzJBzHRCAs9UxqeoyFQMYbqSWYTfJJQAWDm'
195 assert e.description == u'あまやかす'
196 res = wallet.get_address_book([1])
197 assert len(res.entries) == 1
198 e = res.entries[0]
199 assert e.index == 1
200 assert e.address == '42ey1afDFnn4886T7196doS9GPMzexD9gXpsZJDwVjeRVdFCSoHnv7KPbBeGpzJBzHRCAs9UxqeoyFQMYbqSWYTfJJQAWDm'
201 assert e.description == u'あまやかす'
202 res = wallet.edit_address_book(1, description = '')
203 res = wallet.get_address_book([1])
204 assert len(res.entries) == 1
205 e = res.entries[0]
206 assert e.index == 1
207 assert e.address == '42ey1afDFnn4886T7196doS9GPMzexD9gXpsZJDwVjeRVdFCSoHnv7KPbBeGpzJBzHRCAs9UxqeoyFQMYbqSWYTfJJQAWDm'
208 assert e.description == ''
209 res = wallet.edit_address_book(1, description = 'えんしゅう')
210 res = wallet.get_address_book([1])
211 assert len(res.entries) == 1
212 e = res.entries[0]
213 assert e.index == 1
214 assert e.address == '42ey1afDFnn4886T7196doS9GPMzexD9gXpsZJDwVjeRVdFCSoHnv7KPbBeGpzJBzHRCAs9UxqeoyFQMYbqSWYTfJJQAWDm'
215 assert e.description == u'えんしゅう'
216 res = wallet.edit_address_book(1, address = '888tNkZrPN6JsEgekjMnABU4TBzc2Dt29EPAvkRxbANsAnjyPbb3iQ1YBRk1UXcdRsiKc9dhwMVgN5S9cQUiyoogDavup3H')
217 res = wallet.get_address_book([1])
218 assert len(res.entries) == 1
219 e = res.entries[0]
220 assert e.index == 1
221 assert e.address == '888tNkZrPN6JsEgekjMnABU4TBzc2Dt29EPAvkRxbANsAnjyPbb3iQ1YBRk1UXcdRsiKc9dhwMVgN5S9cQUiyoogDavup3H'
222 assert e.description == u'えんしゅう'
223 ok = False
224 try: res = wallet.edit_address_book(1, address = '')
225 except: ok = True
226 assert ok
227 ok = False
228 try: res = wallet.edit_address_book(1, address = 'address')
229 except: ok = True
230 assert ok
231 res = wallet.edit_address_book(1)
232 res = wallet.get_address_book([1])
233 assert len(res.entries) == 1
234 assert e == res.entries[0]
235
236 # empty
237 wallet.delete_address_book(0)
238 res = wallet.get_address_book([0]) # entries above the deleted one collapse one slot up
239 assert len(res.entries) == 1
240 assert res.entries[0].address == '888tNkZrPN6JsEgekjMnABU4TBzc2Dt29EPAvkRxbANsAnjyPbb3iQ1YBRk1UXcdRsiKc9dhwMVgN5S9cQUiyoogDavup3H'
241 assert res.entries[0].description == u'えんしゅう'
242 wallet.delete_address_book(2)
243 wallet.delete_address_book(0)
244 wallet.delete_address_book(0)
245 res = wallet.get_address_book()
246 assert not 'entries' in res or len(res.entries) == 0
247
248
249 if __name__ == '__main__':
250 AddressBookTest().run_test()