townforge/tests/functional_tests/uri.py
2020-05-29 23:20:49 +00:00

222 lines
12 KiB
Python
Executable File

#!/usr/bin/env python3
#encoding=utf-8
# Copyright (c) 2019 The Monero Project
#
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without modification, are
# permitted provided that the following conditions are met:
#
# 1. Redistributions of source code must retain the above copyright notice, this list of
# conditions and the following disclaimer.
#
# 2. Redistributions in binary form must reproduce the above copyright notice, this list
# of conditions and the following disclaimer in the documentation and/or other
# materials provided with the distribution.
#
# 3. Neither the name of the copyright holder nor the names of its contributors may be
# used to endorse or promote products derived from this software without specific
# prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
# EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
# MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
# THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
# PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
# STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
# THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
"""Test URI RPC
"""
from __future__ import print_function
try:
from urllib import quote as urllib_quote
except:
from urllib.parse import quote as urllib_quote
from framework.wallet import Wallet
class URITest():
def run_test(self):
self.create()
self.test_townforge_uri()
def create(self):
print('Creating wallet')
wallet = Wallet()
# close the wallet if any, will throw if none is loaded
try: wallet.close_wallet()
except: pass
seed = 'teardrop owls later width skater gadget different pegs yard ahead onslaught dynamite thorn espionage dwelt rural eels aimless shipped toaster shocking rounded maverick mystery thorn'
res = wallet.restore_deterministic_wallet(seed = seed)
assert res.address == 'TF1MMEY5v2dN49XKNCKCdBqmTMM6GdZZA5UBbDgTewaUd3c2jDazN5yKrG1BBHX3UyPqKD9hrh3DpPTDmWiCmsuRpePT1MTaPxm'
assert res.seed == seed
def test_townforge_uri(self):
print('Testing townforge: URI')
wallet = Wallet()
utf8string = [u'えんしゅう', u'あまやかす']
quoted_utf8string = [urllib_quote(x.encode('utf8')) for x in utf8string]
ok = False
try: res = wallet.make_uri()
except: ok = True
assert ok
ok = False
try: res = wallet.make_uri(address = '')
except: ok = True
assert ok
ok = False
try: res = wallet.make_uri(address = 'kjshdkj')
except: ok = True
assert ok
for address in [
'TF1MMEY5v2dN49XKNCKCdBqmTMM6GdZZA5UBbDgTewaUd3c2jDazN5yKrG1BBHX3UyPqKD9hrh3DpPTDmWiCmsuRpePT1MTaPxm',
'TF2MMFPZA4h7z4MFGWsXXS6YSt67ZAjQxFWhQecUjAXWGKtT3z4yh12HxLMBinKw2Ng99DDvMncWF7xjS8qM43mdWPiWDaDkZ2m'
]:
res = wallet.make_uri(address = address)
assert res.uri == 'townforge:' + address
res = wallet.parse_uri(res.uri)
assert res.uri.address == address
assert res.uri.amount == 0
assert res.uri.tx_description == ''
assert res.uri.recipient_name == ''
assert not 'unknown_parameters' in res or len(res.unknown_parameters) == 0
res = wallet.make_uri(address = address, amount = 1100000)
assert res.uri == 'townforge:' + address + '?tx_amount=0.011' or res.uri == 'townforge:' + address + '?tx_amount=0.01100000'
res = wallet.parse_uri(res.uri)
assert res.uri.address == address
assert res.uri.amount == 1100000
assert res.uri.tx_description == ''
assert res.uri.recipient_name == ''
assert not 'unknown_parameters' in res or len(res.unknown_parameters) == 0
address = 'TF1MMEY5v2dN49XKNCKCdBqmTMM6GdZZA5UBbDgTewaUd3c2jDazN5yKrG1BBHX3UyPqKD9hrh3DpPTDmWiCmsuRpePT1MTaPxm'
res = wallet.make_uri(address = address, tx_description = utf8string[0])
assert res.uri == 'townforge:' + address + '?tx_description=' + quoted_utf8string[0]
res = wallet.parse_uri(res.uri)
assert res.uri.address == address
assert res.uri.amount == 0
assert res.uri.tx_description == utf8string[0]
assert res.uri.recipient_name == ''
assert not 'unknown_parameters' in res or len(res.unknown_parameters) == 0
res = wallet.make_uri(address = address, recipient_name = utf8string[0])
assert res.uri == 'townforge:' + address + '?recipient_name=' + quoted_utf8string[0]
res = wallet.parse_uri(res.uri)
assert res.uri.address == address
assert res.uri.amount == 0
assert res.uri.tx_description == ''
assert res.uri.recipient_name == utf8string[0]
assert not 'unknown_parameters' in res or len(res.unknown_parameters) == 0
res = wallet.make_uri(address = address, recipient_name = utf8string[0], tx_description = utf8string[1])
assert res.uri == 'townforge:' + address + '?recipient_name=' + quoted_utf8string[0] + '&tx_description=' + quoted_utf8string[1]
res = wallet.parse_uri(res.uri)
assert res.uri.address == address
assert res.uri.amount == 0
assert res.uri.tx_description == utf8string[1]
assert res.uri.recipient_name == utf8string[0]
assert not 'unknown_parameters' in res or len(res.unknown_parameters) == 0
res = wallet.make_uri(address = address, recipient_name = utf8string[0], tx_description = utf8string[1], amount = 100000000)
assert res.uri == 'townforge:' + address + '?tx_amount=1.00000000&recipient_name=' + quoted_utf8string[0] + '&tx_description=' + quoted_utf8string[1]
res = wallet.parse_uri(res.uri)
assert res.uri.address == address
assert res.uri.amount == 100000000
assert res.uri.tx_description == utf8string[1]
assert res.uri.recipient_name == utf8string[0]
assert not 'unknown_parameters' in res or len(res.unknown_parameters) == 0
res = wallet.make_uri(address = address, recipient_name = utf8string[0], tx_description = utf8string[1], amount = 100000000)
assert res.uri == 'townforge:' + address + '?tx_amount=1.00000000&recipient_name=' + quoted_utf8string[0] + '&tx_description=' + quoted_utf8string[1]
res = wallet.parse_uri(res.uri)
assert res.uri.address == address
assert res.uri.amount == 100000000
assert res.uri.tx_description == utf8string[1]
assert res.uri.recipient_name == utf8string[0]
assert not 'unknown_parameters' in res or len(res.unknown_parameters) == 0
# spaces must be encoded as %20
res = wallet.make_uri(address = address, tx_description = ' ' + utf8string[1] + ' ' + utf8string[0] + ' ', amount = 100000000)
assert res.uri == 'townforge:' + address + '?tx_amount=1.00000000&tx_description=%20' + quoted_utf8string[1] + '%20' + quoted_utf8string[0] + '%20'
res = wallet.parse_uri(res.uri)
assert res.uri.address == address
assert res.uri.amount == 100000000
assert res.uri.tx_description == ' ' + utf8string[1] + ' ' + utf8string[0] + ' '
assert res.uri.recipient_name == ''
assert not 'unknown_parameters' in res or len(res.unknown_parameters) == 0
# the example from the docs
res = wallet.parse_uri('townforge:TF1MMBg4zx18SnZC6oswCRB7DzdVeUKce5NdCMSWUHNY4wNvNhzmFj4WqZY2bFj8R1REAWR3qAH5zD7sjXyHz3tVayzHSswqymx?tx_amount=239.39014&tx_description=donation')
assert res.uri.address == 'TF1MMBg4zx18SnZC6oswCRB7DzdVeUKce5NdCMSWUHNY4wNvNhzmFj4WqZY2bFj8R1REAWR3qAH5zD7sjXyHz3tVayzHSswqymx'
assert res.uri.amount == 23939014000
assert res.uri.tx_description == 'donation'
assert res.uri.recipient_name == ''
assert not 'unknown_parameters' in res or len(res.unknown_parameters) == 0
# malformed/invalid
for uri in [
'monero:TF1MMBg4zx18SnZC6oswCRB7DzdVeUKce5NdCMSWUHNY4wNvNhzmFj4WqZY2bFj8R1REAWR3qAH5zD7sjXyHz3tVayzHSswqymx?tx_amount=239.39014&tx_description=donation',
'',
':',
'monero',
'townforge',
'nottownforge:TF1MMEY5v2dN49XKNCKCdBqmTMM6GdZZA5UBbDgTewaUd3c2jDazN5yKrG1BBHX3UyPqKD9hrh3DpPTDmWiCmsuRpePT1MTaPxm',
'MONERO:TF1MMEY5v2dN49XKNCKCdBqmTMM6GdZZA5UBbDgTewaUd3c2jDazN5yKrG1BBHX3UyPqKD9hrh3DpPTDmWiCmsuRpePT1MTaPxm',
'MONERO::TF1MMEY5v2dN49XKNCKCdBqmTMM6GdZZA5UBbDgTewaUd3c2jDazN5yKrG1BBHX3UyPqKD9hrh3DpPTDmWiCmsuRpePT1MTaPxm',
'townforge:',
'townforge:badaddress',
'townforge:tx_amount=10',
'townforge:?tx_amount=10',
'townforge:TF1MMEY5v2dN49XKNCKCdBqmTMM6GdZZA5UBbDgTewaUd3c2jDazN5yKrG1BBHX3UyPqKD9hrh3DpPTDmWiCmsuRpePT1MTaPxm?tx_amount=-1',
'townforge:TF1MMEY5v2dN49XKNCKCdBqmTMM6GdZZA5UBbDgTewaUd3c2jDazN5yKrG1BBHX3UyPqKD9hrh3DpPTDmWiCmsuRpePT1MTaPxm?tx_amount=1e12',
'townforge:TF1MMEY5v2dN49XKNCKCdBqmTMM6GdZZA5UBbDgTewaUd3c2jDazN5yKrG1BBHX3UyPqKD9hrh3DpPTDmWiCmsuRpePT1MTaPxm?tx_amount=+12',
'townforge:TF1MMEY5v2dN49XKNCKCdBqmTMM6GdZZA5UBbDgTewaUd3c2jDazN5yKrG1BBHX3UyPqKD9hrh3DpPTDmWiCmsuRpePT1MTaPxm?tx_amount=1+2',
'townforge:TF1MMEY5v2dN49XKNCKCdBqmTMM6GdZZA5UBbDgTewaUd3c2jDazN5yKrG1BBHX3UyPqKD9hrh3DpPTDmWiCmsuRpePT1MTaPxm?tx_amount=A',
'townforge:TF1MMEY5v2dN49XKNCKCdBqmTMM6GdZZA5UBbDgTewaUd3c2jDazN5yKrG1BBHX3UyPqKD9hrh3DpPTDmWiCmsuRpePT1MTaPxm?tx_amount=0x2',
'townforge:TF1MMEY5v2dN49XKNCKCdBqmTMM6GdZZA5UBbDgTewaUd3c2jDazN5yKrG1BBHX3UyPqKD9hrh3DpPTDmWiCmsuRpePT1MTaPxm?tx_amount=222222222222222222222',
'townforge:TF1MMEY5v2dN49XKNCKCdBqmTMM6GdZZA5UBbDgTewaUd3c2jDazN5yKrG1BBHX3UyPqKD9hrh3DpPTDmWiCmsuRpePT1MTaPxn?tx_amount=10',
'townforge:TF1MMEY5v2dN49XKNCKCdBqmTMM6GdZZA5UBbDgTewaUd3c2jDazN5yKrG1BBHX3UyPqKD9hrh3DpPTDmWiCmsuRpePT1MTaPxm&',
'townforge:TF1MMEY5v2dN49XKNCKCdBqmTMM6GdZZA5UBbDgTewaUd3c2jDazN5yKrG1BBHX3UyPqKD9hrh3DpPTDmWiCmsuRpePT1MTaPxm&tx_amount',
'townforge:TF1MMEY5v2dN49XKNCKCdBqmTMM6GdZZA5UBbDgTewaUd3c2jDazN5yKrG1BBHX3UyPqKD9hrh3DpPTDmWiCmsuRpePT1MTaPxm&tx_amount=',
'townforge:TF1MMEY5v2dN49XKNCKCdBqmTMM6GdZZA5UBbDgTewaUd3c2jDazN5yKrG1BBHX3UyPqKD9hrh3DpPTDmWiCmsuRpePT1MTaPxm&tx_amount=10=',
'townforge:TF1MMEY5v2dN49XKNCKCdBqmTMM6GdZZA5UBbDgTewaUd3c2jDazN5yKrG1BBHX3UyPqKD9hrh3DpPTDmWiCmsuRpePT1MTaPxm&tx_amount=10=&',
'townforge:TF1MMEY5v2dN49XKNCKCdBqmTMM6GdZZA5UBbDgTewaUd3c2jDazN5yKrG1BBHX3UyPqKD9hrh3DpPTDmWiCmsuRpePT1MTaPxm&tx_amount=10=&foo=bar',
'townforge:TF1MMEY5v2dN49XKNCKCdBqmTMM6GdZZA5UBbDgTewaUd3c2jDazN5yKrG1BBHX3UyPqKD9hrh3DpPTDmWiCmsuRpePT1MTaPxm?tx_amount=10&tx_amount=20',
]:
ok = False
try: res = wallet.parse_uri(uri)
except: ok = True
assert ok, res
# unknown parameters but otherwise valid
res = wallet.parse_uri('townforge:' + address + '?tx_amount=239.39014&foo=bar')
assert res.uri.address == address
assert res.uri.amount == 23939014000
assert res.unknown_parameters == ['foo=bar'], res
res = wallet.parse_uri('townforge:' + address + '?tx_amount=239.39014&foo=bar&baz=quux')
assert res.uri.address == address
assert res.uri.amount == 23939014000
assert res.unknown_parameters == ['foo=bar', 'baz=quux'], res
res = wallet.parse_uri('townforge:' + address + '?tx_amount=239.39014&%20=%20')
assert res.uri.address == address
assert res.uri.amount == 23939014000
assert res.unknown_parameters == ['%20=%20'], res
res = wallet.parse_uri('townforge:' + address + '?tx_amount=239.39014&unknown=' + quoted_utf8string[0])
assert res.uri.address == address
assert res.uri.amount == 23939014000
assert res.unknown_parameters == [u'unknown=' + quoted_utf8string[0]], res
if __name__ == '__main__':
URITest().run_test()