From: ThomasV Date: Mon, 30 Jun 2014 15:51:02 +0000 (+0200) Subject: Merge pull request #746 from slush0/bits X-Git-Url: https://git.novaco.in/?p=electrum-nvc.git;a=commitdiff_plain;h=42742d98ba7853d4bdc9ac2275d68433c2383ce8;hp=a97a0629dd3d22afe98cb3d68a6ed0fa0eca7170 Merge pull request #746 from slush0/bits Base unit 'bits' --- diff --git a/electrum b/electrum index e63803d..8b004b3 100755 --- a/electrum +++ b/electrum @@ -64,7 +64,7 @@ def prompt_password(prompt, confirm=True): def arg_parser(): usage = "%prog [options] command" - parser = optparse.OptionParser(prog=usage, add_help_option=False) + parser = optparse.OptionParser(usage=usage, add_help_option=False) parser.add_option("-h", "--help", action="callback", callback=print_help_cb, help="show this help text") parser.add_option("-g", "--gui", dest="gui", help="User interface: qt, lite, gtk, text or stdio") parser.add_option("-w", "--wallet", dest="wallet_path", help="wallet path (default: electrum.dat)") diff --git a/lib/tests/test_util.py b/lib/tests/test_util.py index f0da88b..1fab17b 100644 --- a/lib/tests/test_util.py +++ b/lib/tests/test_util.py @@ -1,5 +1,5 @@ import unittest -from lib.util import format_satoshis +from lib.util import format_satoshis, parse_URI class TestUtil(unittest.TestCase): @@ -17,3 +17,49 @@ class TestUtil(unittest.TestCase): result = format_satoshis(-1234, is_diff=True) expected = "-0.00001234" self.assertEqual(expected, result) + + def _do_test_parse_URI(self, uri, expected_address, expected_amount, expected_label, expected_message, expected_request_url): + address, amount, label, message, request_url = parse_URI(uri) + self.assertEqual(expected_address, address) + self.assertEqual(expected_amount, amount) + self.assertEqual(expected_label, label) + self.assertEqual(expected_message, message) + self.assertEqual(expected_request_url, request_url) + + def test_parse_URI_address(self): + self._do_test_parse_URI('bitcoin:15mKKb2eos1hWa6tisdPwwDC1a5J1y9nma', '15mKKb2eos1hWa6tisdPwwDC1a5J1y9nma', '', '', '', '') + + def test_parse_URI_only_address(self): + self._do_test_parse_URI('15mKKb2eos1hWa6tisdPwwDC1a5J1y9nma', '15mKKb2eos1hWa6tisdPwwDC1a5J1y9nma', None, None, None, None) + + + def test_parse_URI_address_label(self): + self._do_test_parse_URI('bitcoin:15mKKb2eos1hWa6tisdPwwDC1a5J1y9nma?label=electrum%20test', '15mKKb2eos1hWa6tisdPwwDC1a5J1y9nma', '', 'electrum test', '', '') + + def test_parse_URI_address_message(self): + self._do_test_parse_URI('bitcoin:15mKKb2eos1hWa6tisdPwwDC1a5J1y9nma?message=electrum%20test', '15mKKb2eos1hWa6tisdPwwDC1a5J1y9nma', '', '', 'electrum test', '') + + def test_parse_URI_address_amount(self): + self._do_test_parse_URI('bitcoin:15mKKb2eos1hWa6tisdPwwDC1a5J1y9nma?amount=0.0003', '15mKKb2eos1hWa6tisdPwwDC1a5J1y9nma', 30000, '', '', '') + + def test_parse_URI_address_request_url(self): + self._do_test_parse_URI('bitcoin:15mKKb2eos1hWa6tisdPwwDC1a5J1y9nma?r=http://domain.tld/page?h%3D2a8628fc2fbe', '15mKKb2eos1hWa6tisdPwwDC1a5J1y9nma', '', '', '', 'http://domain.tld/page?h=2a8628fc2fbe') + + def test_parse_URI_ignore_args(self): + self._do_test_parse_URI('bitcoin:15mKKb2eos1hWa6tisdPwwDC1a5J1y9nma?test=test', '15mKKb2eos1hWa6tisdPwwDC1a5J1y9nma', '', '', '', '') + + def test_parse_URI_multiple_args(self): + self._do_test_parse_URI('bitcoin:15mKKb2eos1hWa6tisdPwwDC1a5J1y9nma?amount=0.00004&label=electrum-test&message=electrum%20test&test=none&r=http://domain.tld/page', '15mKKb2eos1hWa6tisdPwwDC1a5J1y9nma', 4000, 'electrum-test', 'electrum test', 'http://domain.tld/page') + + def test_parse_URI_no_address_request_url(self): + self._do_test_parse_URI('bitcoin:?r=http://domain.tld/page?h%3D2a8628fc2fbe', '', '', '', '', 'http://domain.tld/page?h=2a8628fc2fbe') + + def test_parse_URI_invalid_address(self): + self.assertRaises(AssertionError, parse_URI, 'bitcoin:invalidaddress') + + def test_parse_URI_invalid(self): + self.assertRaises(AssertionError, parse_URI, 'notbitcoin:15mKKb2eos1hWa6tisdPwwDC1a5J1y9nma') + + def test_parse_URI_parameter_polution(self): + self.assertRaises(Exception, parse_URI, 'bitcoin:15mKKb2eos1hWa6tisdPwwDC1a5J1y9nma?amount=0.0003&label=test&amount=30.0') + diff --git a/lib/util.py b/lib/util.py index 97906eb..d796ae4 100644 --- a/lib/util.py +++ b/lib/util.py @@ -170,7 +170,7 @@ def parse_URI(uri): assert u.scheme == 'bitcoin' address = u.path - assert bitcoin.is_address(address) + valid_address = bitcoin.is_address(address) pq = urlparse.parse_qs(u.query) @@ -194,6 +194,11 @@ def parse_URI(uri): if 'r' in pq: request_url = pq['r'][0] + if request_url != '': + return address, amount, label, message, request_url + + assert valid_address + return address, amount, label, message, request_url