| 1 |
import asyncio |
| 2 |
from datetime import datetime |
| 3 |
from decimal import Decimal |
| 4 |
|
| 5 |
from electrum import util |
| 6 |
from electrum.util import (format_satoshis, format_fee_satoshis, is_hash256_str, chunks, is_ip_address, |
| 7 |
list_enabled_bits, format_satoshis_plain, is_private_netaddress, is_hex_str, |
| 8 |
is_integer, is_non_negative_integer, is_int_or_float, is_non_negative_int_or_float, |
| 9 |
ShortID) |
| 10 |
from electrum.bip21 import parse_bip21_URI, InvalidBitcoinURI |
| 11 |
from . import ElectrumTestCase, as_testnet |
| 12 |
|
| 13 |
|
| 14 |
class TestUtil(ElectrumTestCase): |
| 15 |
|
| 16 |
def test_format_satoshis(self): |
| 17 |
self.assertEqual("0.00001234", format_satoshis(1234)) |
| 18 |
|
| 19 |
def test_format_satoshis_negative(self): |
| 20 |
self.assertEqual("-0.00001234", format_satoshis(-1234)) |
| 21 |
|
| 22 |
def test_format_satoshis_to_mbtc(self): |
| 23 |
self.assertEqual("0.01234", format_satoshis(1234, decimal_point=5)) |
| 24 |
|
| 25 |
def test_format_satoshis_decimal(self): |
| 26 |
self.assertEqual("0.00001234", format_satoshis(Decimal(1234))) |
| 27 |
|
| 28 |
def test_format_satoshis_msat_resolution(self): |
| 29 |
self.assertEqual("45831276.", format_satoshis(Decimal("45831276"), decimal_point=0)) |
| 30 |
self.assertEqual("45831276.", format_satoshis(Decimal("45831275.748"), decimal_point=0)) |
| 31 |
self.assertEqual("45831275.75", format_satoshis(Decimal("45831275.748"), decimal_point=0, precision=2)) |
| 32 |
self.assertEqual("45831275.748", format_satoshis(Decimal("45831275.748"), decimal_point=0, precision=3)) |
| 33 |
|
| 34 |
self.assertEqual("458312.76", format_satoshis(Decimal("45831276"), decimal_point=2)) |
| 35 |
self.assertEqual("458312.76", format_satoshis(Decimal("45831275.748"), decimal_point=2)) |
| 36 |
self.assertEqual("458312.7575", format_satoshis(Decimal("45831275.748"), decimal_point=2, precision=2)) |
| 37 |
self.assertEqual("458312.75748", format_satoshis(Decimal("45831275.748"), decimal_point=2, precision=3)) |
| 38 |
|
| 39 |
self.assertEqual("458.31276", format_satoshis(Decimal("45831276"), decimal_point=5)) |
| 40 |
self.assertEqual("458.31276", format_satoshis(Decimal("45831275.748"), decimal_point=5)) |
| 41 |
self.assertEqual("458.3127575", format_satoshis(Decimal("45831275.748"), decimal_point=5, precision=2)) |
| 42 |
self.assertEqual("458.31275748", format_satoshis(Decimal("45831275.748"), decimal_point=5, precision=3)) |
| 43 |
|
| 44 |
def test_format_fee_float(self): |
| 45 |
self.assertEqual("1.7", format_fee_satoshis(1700/1000)) |
| 46 |
|
| 47 |
def test_format_fee_decimal(self): |
| 48 |
self.assertEqual("1.7", format_fee_satoshis(Decimal("1.7"))) |
| 49 |
|
| 50 |
def test_format_fee_precision(self): |
| 51 |
self.assertEqual("1.666", |
| 52 |
format_fee_satoshis(1666/1000, precision=6)) |
| 53 |
self.assertEqual("1.7", |
| 54 |
format_fee_satoshis(1666/1000, precision=1)) |
| 55 |
|
| 56 |
def test_format_satoshis_whitespaces(self): |
| 57 |
self.assertEqual(" 0.0001234 ", format_satoshis(12340, whitespaces=True)) |
| 58 |
self.assertEqual(" 0.00001234", format_satoshis(1234, whitespaces=True)) |
| 59 |
self.assertEqual(" 0.45831275", format_satoshis(Decimal("45831275."), whitespaces=True)) |
| 60 |
self.assertEqual(" 0.45831275 ", format_satoshis(Decimal("45831275."), whitespaces=True, precision=3)) |
| 61 |
self.assertEqual(" 0.458312757 ", format_satoshis(Decimal("45831275.7"), whitespaces=True, precision=3)) |
| 62 |
self.assertEqual(" 0.45831275748", format_satoshis(Decimal("45831275.748"), whitespaces=True, precision=3)) |
| 63 |
|
| 64 |
def test_format_satoshis_whitespaces_negative(self): |
| 65 |
self.assertEqual(" -0.0001234 ", format_satoshis(-12340, whitespaces=True)) |
| 66 |
self.assertEqual(" -0.00001234", format_satoshis(-1234, whitespaces=True)) |
| 67 |
|
| 68 |
def test_format_satoshis_diff_positive(self): |
| 69 |
self.assertEqual("+0.00001234", format_satoshis(1234, is_diff=True)) |
| 70 |
self.assertEqual("+456789.00001234", format_satoshis(45678900001234, is_diff=True)) |
| 71 |
|
| 72 |
def test_format_satoshis_diff_negative(self): |
| 73 |
self.assertEqual("-0.00001234", format_satoshis(-1234, is_diff=True)) |
| 74 |
self.assertEqual("-456789.00001234", format_satoshis(-45678900001234, is_diff=True)) |
| 75 |
|
| 76 |
def test_format_satoshis_add_thousands_sep(self): |
| 77 |
self.assertEqual("178 890 000.", format_satoshis(Decimal(178890000), decimal_point=0, add_thousands_sep=True)) |
| 78 |
self.assertEqual("458 312.757 48", format_satoshis(Decimal("45831275.748"), decimal_point=2, add_thousands_sep=True, precision=5)) |
| 79 |
|
| 80 |
self.assertEqual("+4 583 127.574 8", format_satoshis(Decimal("45831275.748"), decimal_point=1, is_diff=True, add_thousands_sep=True, precision=4)) |
| 81 |
self.assertEqual("+456 789 112.004 56", format_satoshis(Decimal("456789112.00456"), decimal_point=0, is_diff=True, add_thousands_sep=True, precision=5)) |
| 82 |
self.assertEqual("-0.000 012 34", format_satoshis(-1234, is_diff=True, add_thousands_sep=True)) |
| 83 |
self.assertEqual("-456 789.000 012 34", format_satoshis(-45678900001234, is_diff=True, add_thousands_sep=True)) |
| 84 |
|
| 85 |
self.assertEqual("-456 789.123 400", format_satoshis(-45678912340000, num_zeros=6, add_thousands_sep=True)) |
| 86 |
self.assertEqual("-456 789.123 4", format_satoshis(-45678912340000, num_zeros=2, add_thousands_sep=True)) |
| 87 |
|
| 88 |
self.assertEqual(" 1 432.731 11", format_satoshis(143273111, decimal_point=5, add_thousands_sep=True, whitespaces=True)) |
| 89 |
self.assertEqual(" 1 432.731 ", format_satoshis(143273100, decimal_point=5, add_thousands_sep=True, whitespaces=True)) |
| 90 |
self.assertEqual(" 67 891 432.731 ", format_satoshis(6789143273100, decimal_point=5, add_thousands_sep=True, whitespaces=True)) |
| 91 |
self.assertEqual(" 143 273 100.", format_satoshis(143273100, decimal_point=0, add_thousands_sep=True, whitespaces=True)) |
| 92 |
self.assertEqual(" 6 789 143 273 100.", format_satoshis(6789143273100, decimal_point=0, add_thousands_sep=True, whitespaces=True)) |
| 93 |
self.assertEqual("56 789 143 273 100.", format_satoshis(56789143273100, decimal_point=0, add_thousands_sep=True, whitespaces=True)) |
| 94 |
|
| 95 |
def test_format_satoshis_plain(self): |
| 96 |
self.assertEqual("0.00001234", format_satoshis_plain(1234)) |
| 97 |
|
| 98 |
def test_format_satoshis_plain_decimal(self): |
| 99 |
self.assertEqual("0.00001234", format_satoshis_plain(Decimal(1234))) |
| 100 |
|
| 101 |
def test_format_satoshis_plain_to_mbtc(self): |
| 102 |
self.assertEqual("0.01234", format_satoshis_plain(1234, decimal_point=5)) |
| 103 |
|
| 104 |
def _do_test_parse_URI(self, uri, expected): |
| 105 |
result = parse_bip21_URI(uri) |
| 106 |
self.assertEqual(expected, result) |
| 107 |
|
| 108 |
def test_parse_URI_address(self): |
| 109 |
self._do_test_parse_URI('bitcoin:15mKKb2eos1hWa6tisdPwwDC1a5J1y9nma', |
| 110 |
{'address': '15mKKb2eos1hWa6tisdPwwDC1a5J1y9nma'}) |
| 111 |
|
| 112 |
def test_parse_URI_only_address(self): |
| 113 |
self._do_test_parse_URI('15mKKb2eos1hWa6tisdPwwDC1a5J1y9nma', |
| 114 |
{'address': '15mKKb2eos1hWa6tisdPwwDC1a5J1y9nma'}) |
| 115 |
|
| 116 |
|
| 117 |
def test_parse_URI_address_label(self): |
| 118 |
self._do_test_parse_URI('bitcoin:15mKKb2eos1hWa6tisdPwwDC1a5J1y9nma?label=electrum%20test', |
| 119 |
{'address': '15mKKb2eos1hWa6tisdPwwDC1a5J1y9nma', 'label': 'electrum test'}) |
| 120 |
|
| 121 |
def test_parse_URI_address_message(self): |
| 122 |
self._do_test_parse_URI('bitcoin:15mKKb2eos1hWa6tisdPwwDC1a5J1y9nma?message=electrum%20test', |
| 123 |
{'address': '15mKKb2eos1hWa6tisdPwwDC1a5J1y9nma', 'message': 'electrum test', 'memo': 'electrum test'}) |
| 124 |
|
| 125 |
def test_parse_URI_address_amount(self): |
| 126 |
self._do_test_parse_URI('bitcoin:15mKKb2eos1hWa6tisdPwwDC1a5J1y9nma?amount=0.0003', |
| 127 |
{'address': '15mKKb2eos1hWa6tisdPwwDC1a5J1y9nma', 'amount': 30000}) |
| 128 |
|
| 129 |
def test_parse_URI_address_request_url(self): |
| 130 |
self._do_test_parse_URI('bitcoin:15mKKb2eos1hWa6tisdPwwDC1a5J1y9nma?r=http://domain.tld/page?h%3D2a8628fc2fbe', |
| 131 |
{'address': '15mKKb2eos1hWa6tisdPwwDC1a5J1y9nma', 'r': 'http://domain.tld/page?h=2a8628fc2fbe'}) |
| 132 |
|
| 133 |
def test_parse_URI_ignore_args(self): |
| 134 |
self._do_test_parse_URI('bitcoin:15mKKb2eos1hWa6tisdPwwDC1a5J1y9nma?test=test', |
| 135 |
{'address': '15mKKb2eos1hWa6tisdPwwDC1a5J1y9nma', 'test': 'test'}) |
| 136 |
|
| 137 |
def test_parse_URI_multiple_args(self): |
| 138 |
self._do_test_parse_URI('bitcoin:15mKKb2eos1hWa6tisdPwwDC1a5J1y9nma?amount=0.00004&label=electrum-test&message=electrum%20test&test=none&r=http://domain.tld/page', |
| 139 |
{'address': '15mKKb2eos1hWa6tisdPwwDC1a5J1y9nma', 'amount': 4000, 'label': 'electrum-test', 'message': u'electrum test', 'memo': u'electrum test', 'r': 'http://domain.tld/page', 'test': 'none'}) |
| 140 |
|
| 141 |
def test_parse_URI_no_address_request_url(self): |
| 142 |
self._do_test_parse_URI('bitcoin:?r=http://domain.tld/page?h%3D2a8628fc2fbe', |
| 143 |
{'r': 'http://domain.tld/page?h=2a8628fc2fbe'}) |
| 144 |
|
| 145 |
def test_parse_URI_invalid_address(self): |
| 146 |
self.assertRaises(InvalidBitcoinURI, parse_bip21_URI, 'bitcoin:invalidaddress') |
| 147 |
|
| 148 |
def test_parse_URI_invalid(self): |
| 149 |
self.assertRaises(InvalidBitcoinURI, parse_bip21_URI, 'notbitcoin:15mKKb2eos1hWa6tisdPwwDC1a5J1y9nma') |
| 150 |
|
| 151 |
def test_parse_URI_parameter_pollution(self): |
| 152 |
self.assertRaises(InvalidBitcoinURI, parse_bip21_URI, 'bitcoin:15mKKb2eos1hWa6tisdPwwDC1a5J1y9nma?amount=0.0003&label=test&amount=30.0') |
| 153 |
|
| 154 |
@as_testnet |
| 155 |
def test_parse_URI_unsupported_req_key(self): |
| 156 |
self._do_test_parse_URI('bitcoin:TB1QXJ6KVTE6URY2MX695METFTFT7LR5HYK4M3VT5F?amount=0.00100000&label=test&somethingyoudontunderstand=50', |
| 157 |
{'address': 'TB1QXJ6KVTE6URY2MX695METFTFT7LR5HYK4M3VT5F', 'amount': 100000, 'label': 'test', 'somethingyoudontunderstand': '50'}) |
| 158 |
|
| 159 |
self.assertRaises(InvalidBitcoinURI, parse_bip21_URI, 'bitcoin:TB1QXJ6KVTE6URY2MX695METFTFT7LR5HYK4M3VT5F?amount=0.00100000&label=test&req-test=1&somethingyoudontunderstand=50') |
| 160 |
|
| 161 |
@as_testnet |
| 162 |
def test_parse_URI_lightning_consistency(self): |
| 163 |
|
| 164 |
self._do_test_parse_URI('bitcoin:?lightning=lntb700u1p3kqy0cpp5azvqy3wez7hcz3ka7tpqqvw5mpsa7fknxl4ca7a7669kswhf0hgqsp5qxhxul9k88w2nsk643elzuu4nepwkq052ek79esmz47yj6lfrhuqdqvw3jhxapjxcmscqzynxq8zals8sq9q7sqqqqqqqqqqqqqqqqqqqqqqqqq9qsqyznyzw55q63yytup920n9qcsnh6qqht48maapzgadll2qy5vheeq26crapt0rcv9aqmpm93ljkapgtc05keud9jhlasns795fylfdjsphud9uh', |
| 165 |
{'lightning': 'lntb700u1p3kqy0cpp5azvqy3wez7hcz3ka7tpqqvw5mpsa7fknxl4ca7a7669kswhf0hgqsp5qxhxul9k88w2nsk643elzuu4nepwkq052ek79esmz47yj6lfrhuqdqvw3jhxapjxcmscqzynxq8zals8sq9q7sqqqqqqqqqqqqqqqqqqqqqqqqq9qsqyznyzw55q63yytup920n9qcsnh6qqht48maapzgadll2qy5vheeq26crapt0rcv9aqmpm93ljkapgtc05keud9jhlasns795fylfdjsphud9uh'}) |
| 166 |
|
| 167 |
self._do_test_parse_URI('bitcoin:?lightning=lntb700u1p3kqy26pp5l7rj7w0u5sdsj24umzdlhdhkk8a597sn865rhap4h4jenjefdk7ssp5d9zjr96ezp89gsyenfse5f4jn9ls29p0awvp0zxlt6tpzn2m3j5qdqvw3jhxapjxcmqcqzynxq8zals8sq9q7sqqqqqqqqqqqqqqqqqqqqqqqqq9qsqfppqu5ua3szskclyd48wlfdwfd32j65phxy9vu8dmmk3u20u0e0yqw484xzn4hc3cux6kk2wenhw7zy0mseu9ntpk9l4fws2d46svzszrc6mqy535740ks9j22w67fw0x4dt8w2hhzspcqakql', |
| 168 |
{'lightning': 'lntb700u1p3kqy26pp5l7rj7w0u5sdsj24umzdlhdhkk8a597sn865rhap4h4jenjefdk7ssp5d9zjr96ezp89gsyenfse5f4jn9ls29p0awvp0zxlt6tpzn2m3j5qdqvw3jhxapjxcmqcqzynxq8zals8sq9q7sqqqqqqqqqqqqqqqqqqqqqqqqq9qsqfppqu5ua3szskclyd48wlfdwfd32j65phxy9vu8dmmk3u20u0e0yqw484xzn4hc3cux6kk2wenhw7zy0mseu9ntpk9l4fws2d46svzszrc6mqy535740ks9j22w67fw0x4dt8w2hhzspcqakql'}) |
| 169 |
|
| 170 |
self._do_test_parse_URI('bitcoin:tb1qu5ua3szskclyd48wlfdwfd32j65phxy9yf7ytl?amount=0.0007&message=test266&lightning=lntb700u1p3kqy0cpp5azvqy3wez7hcz3ka7tpqqvw5mpsa7fknxl4ca7a7669kswhf0hgqsp5qxhxul9k88w2nsk643elzuu4nepwkq052ek79esmz47yj6lfrhuqdqvw3jhxapjxcmscqzynxq8zals8sq9q7sqqqqqqqqqqqqqqqqqqqqqqqqq9qsqyznyzw55q63yytup920n9qcsnh6qqht48maapzgadll2qy5vheeq26crapt0rcv9aqmpm93ljkapgtc05keud9jhlasns795fylfdjsphud9uh', |
| 171 |
{'address': 'tb1qu5ua3szskclyd48wlfdwfd32j65phxy9yf7ytl', |
| 172 |
'amount': 70000, |
| 173 |
'lightning': 'lntb700u1p3kqy0cpp5azvqy3wez7hcz3ka7tpqqvw5mpsa7fknxl4ca7a7669kswhf0hgqsp5qxhxul9k88w2nsk643elzuu4nepwkq052ek79esmz47yj6lfrhuqdqvw3jhxapjxcmscqzynxq8zals8sq9q7sqqqqqqqqqqqqqqqqqqqqqqqqq9qsqyznyzw55q63yytup920n9qcsnh6qqht48maapzgadll2qy5vheeq26crapt0rcv9aqmpm93ljkapgtc05keud9jhlasns795fylfdjsphud9uh', |
| 174 |
'memo': 'test266', |
| 175 |
'message': 'test266'}) |
| 176 |
|
| 177 |
self._do_test_parse_URI('bitcoin:tb1qu5ua3szskclyd48wlfdwfd32j65phxy9yf7ytl?amount=0.0007&message=test266&lightning=lntb700u1p3kqy26pp5l7rj7w0u5sdsj24umzdlhdhkk8a597sn865rhap4h4jenjefdk7ssp5d9zjr96ezp89gsyenfse5f4jn9ls29p0awvp0zxlt6tpzn2m3j5qdqvw3jhxapjxcmqcqzynxq8zals8sq9q7sqqqqqqqqqqqqqqqqqqqqqqqqq9qsqfppqu5ua3szskclyd48wlfdwfd32j65phxy9vu8dmmk3u20u0e0yqw484xzn4hc3cux6kk2wenhw7zy0mseu9ntpk9l4fws2d46svzszrc6mqy535740ks9j22w67fw0x4dt8w2hhzspcqakql', |
| 178 |
{'address': 'tb1qu5ua3szskclyd48wlfdwfd32j65phxy9yf7ytl', |
| 179 |
'amount': 70000, |
| 180 |
'lightning': 'lntb700u1p3kqy26pp5l7rj7w0u5sdsj24umzdlhdhkk8a597sn865rhap4h4jenjefdk7ssp5d9zjr96ezp89gsyenfse5f4jn9ls29p0awvp0zxlt6tpzn2m3j5qdqvw3jhxapjxcmqcqzynxq8zals8sq9q7sqqqqqqqqqqqqqqqqqqqqqqqqq9qsqfppqu5ua3szskclyd48wlfdwfd32j65phxy9vu8dmmk3u20u0e0yqw484xzn4hc3cux6kk2wenhw7zy0mseu9ntpk9l4fws2d46svzszrc6mqy535740ks9j22w67fw0x4dt8w2hhzspcqakql', |
| 181 |
'memo': 'test266', |
| 182 |
'message': 'test266'}) |
| 183 |
|
| 184 |
self.assertRaises(InvalidBitcoinURI, parse_bip21_URI, 'bitcoin:tb1qvu0c9xme0ul3gzx4nzqdgxsu25acuk9wvsj2j2?amount=0.0007&message=test266&lightning=lntb700u1p3kqy26pp5l7rj7w0u5sdsj24umzdlhdhkk8a597sn865rhap4h4jenjefdk7ssp5d9zjr96ezp89gsyenfse5f4jn9ls29p0awvp0zxlt6tpzn2m3j5qdqvw3jhxapjxcmqcqzynxq8zals8sq9q7sqqqqqqqqqqqqqqqqqqqqqqqqq9qsqfppqu5ua3szskclyd48wlfdwfd32j65phxy9vu8dmmk3u20u0e0yqw484xzn4hc3cux6kk2wenhw7zy0mseu9ntpk9l4fws2d46svzszrc6mqy535740ks9j22w67fw0x4dt8w2hhzspcqakql') |
| 185 |
|
| 186 |
self.assertRaises(InvalidBitcoinURI, parse_bip21_URI, 'bitcoin:tb1qu5ua3szskclyd48wlfdwfd32j65phxy9yf7ytl?amount=0.0008&message=test266&lightning=lntb700u1p3kqy26pp5l7rj7w0u5sdsj24umzdlhdhkk8a597sn865rhap4h4jenjefdk7ssp5d9zjr96ezp89gsyenfse5f4jn9ls29p0awvp0zxlt6tpzn2m3j5qdqvw3jhxapjxcmqcqzynxq8zals8sq9q7sqqqqqqqqqqqqqqqqqqqqqqqqq9qsqfppqu5ua3szskclyd48wlfdwfd32j65phxy9vu8dmmk3u20u0e0yqw484xzn4hc3cux6kk2wenhw7zy0mseu9ntpk9l4fws2d46svzszrc6mqy535740ks9j22w67fw0x4dt8w2hhzspcqakql') |
| 187 |
|
| 188 |
self.assertRaises(InvalidBitcoinURI, parse_bip21_URI, 'bitcoin:tb1qu5ua3szskclyd48wlfdwfd32j65phxy9yf7ytl?amount=0.0008&message=test266&lightning=lntb700u1p3kqy26pp5l7rj7w0u5sdsj24umzdlhdasdasdasdasd') |
| 189 |
|
| 190 |
def test_is_hash256_str(self): |
| 191 |
self.assertTrue(is_hash256_str('09a4c03e3bdf83bbe3955f907ee52da4fc12f4813d459bc75228b64ad08617c7')) |
| 192 |
self.assertTrue(is_hash256_str('2A5C3F4062E4F2FCCE7A1C7B4310CB647B327409F580F4ED72CB8FC0B1804DFA')) |
| 193 |
self.assertTrue(is_hash256_str('00' * 32)) |
| 194 |
|
| 195 |
self.assertFalse(is_hash256_str('00' * 33)) |
| 196 |
self.assertFalse(is_hash256_str('qweqwe')) |
| 197 |
self.assertFalse(is_hash256_str(None)) |
| 198 |
self.assertFalse(is_hash256_str(7)) |
| 199 |
|
| 200 |
def test_is_hex_str(self): |
| 201 |
self.assertTrue(is_hex_str('09a4')) |
| 202 |
self.assertTrue(is_hex_str('abCD')) |
| 203 |
self.assertTrue(is_hex_str('2A5C3F4062E4F2FCCE7A1C7B4310CB647B327409F580F4ED72CB8FC0B1804DFA')) |
| 204 |
self.assertTrue(is_hex_str('00' * 33)) |
| 205 |
|
| 206 |
self.assertFalse(is_hex_str('0x09a4')) |
| 207 |
self.assertFalse(is_hex_str('2A 5C3F')) |
| 208 |
self.assertFalse(is_hex_str(' 2A5C3F')) |
| 209 |
self.assertFalse(is_hex_str('2A5C3F ')) |
| 210 |
self.assertFalse(is_hex_str('000')) |
| 211 |
self.assertFalse(is_hex_str('123')) |
| 212 |
self.assertFalse(is_hex_str('0x123')) |
| 213 |
self.assertFalse(is_hex_str('qweqwe')) |
| 214 |
self.assertFalse(is_hex_str(b'09a4')) |
| 215 |
self.assertFalse(is_hex_str(b'\x09\xa4')) |
| 216 |
self.assertFalse(is_hex_str(None)) |
| 217 |
self.assertFalse(is_hex_str(7)) |
| 218 |
self.assertFalse(is_hex_str(7.2)) |
| 219 |
|
| 220 |
def test_is_integer(self): |
| 221 |
self.assertTrue(is_integer(7)) |
| 222 |
self.assertTrue(is_integer(0)) |
| 223 |
self.assertTrue(is_integer(-1)) |
| 224 |
self.assertTrue(is_integer(-7)) |
| 225 |
|
| 226 |
self.assertFalse(is_integer(Decimal("2.0"))) |
| 227 |
self.assertFalse(is_integer(Decimal(2.0))) |
| 228 |
self.assertFalse(is_integer(Decimal(2))) |
| 229 |
self.assertFalse(is_integer(0.72)) |
| 230 |
self.assertFalse(is_integer(2.0)) |
| 231 |
self.assertFalse(is_integer(-2.0)) |
| 232 |
self.assertFalse(is_integer('09a4')) |
| 233 |
self.assertFalse(is_integer('2A5C3F4062E4F2FCCE7A1C7B4310CB647B327409F580F4ED72CB8FC0B1804DFA')) |
| 234 |
self.assertFalse(is_integer('000')) |
| 235 |
self.assertFalse(is_integer('qweqwe')) |
| 236 |
self.assertFalse(is_integer(None)) |
| 237 |
|
| 238 |
def test_is_non_negative_integer(self): |
| 239 |
self.assertTrue(is_non_negative_integer(7)) |
| 240 |
self.assertTrue(is_non_negative_integer(0)) |
| 241 |
|
| 242 |
self.assertFalse(is_non_negative_integer(Decimal("2.0"))) |
| 243 |
self.assertFalse(is_non_negative_integer(Decimal(2.0))) |
| 244 |
self.assertFalse(is_non_negative_integer(Decimal(2))) |
| 245 |
self.assertFalse(is_non_negative_integer(0.72)) |
| 246 |
self.assertFalse(is_non_negative_integer(2.0)) |
| 247 |
self.assertFalse(is_non_negative_integer(-2.0)) |
| 248 |
self.assertFalse(is_non_negative_integer(-1)) |
| 249 |
self.assertFalse(is_non_negative_integer(-7)) |
| 250 |
self.assertFalse(is_non_negative_integer('09a4')) |
| 251 |
self.assertFalse(is_non_negative_integer('2A5C3F4062E4F2FCCE7A1C7B4310CB647B327409F580F4ED72CB8FC0B1804DFA')) |
| 252 |
self.assertFalse(is_non_negative_integer('000')) |
| 253 |
self.assertFalse(is_non_negative_integer('qweqwe')) |
| 254 |
self.assertFalse(is_non_negative_integer(None)) |
| 255 |
|
| 256 |
def test_is_int_or_float(self): |
| 257 |
self.assertTrue(is_int_or_float(7)) |
| 258 |
self.assertTrue(is_int_or_float(0)) |
| 259 |
self.assertTrue(is_int_or_float(-1)) |
| 260 |
self.assertTrue(is_int_or_float(-7)) |
| 261 |
self.assertTrue(is_int_or_float(0.72)) |
| 262 |
self.assertTrue(is_int_or_float(2.0)) |
| 263 |
self.assertTrue(is_int_or_float(-2.0)) |
| 264 |
|
| 265 |
self.assertFalse(is_int_or_float(Decimal("2.0"))) |
| 266 |
self.assertFalse(is_int_or_float(Decimal(2.0))) |
| 267 |
self.assertFalse(is_int_or_float(Decimal(2))) |
| 268 |
self.assertFalse(is_int_or_float('09a4')) |
| 269 |
self.assertFalse(is_int_or_float('2A5C3F4062E4F2FCCE7A1C7B4310CB647B327409F580F4ED72CB8FC0B1804DFA')) |
| 270 |
self.assertFalse(is_int_or_float('000')) |
| 271 |
self.assertFalse(is_int_or_float('qweqwe')) |
| 272 |
self.assertFalse(is_int_or_float(None)) |
| 273 |
|
| 274 |
def test_is_non_negative_int_or_float(self): |
| 275 |
self.assertTrue(is_non_negative_int_or_float(7)) |
| 276 |
self.assertTrue(is_non_negative_int_or_float(0)) |
| 277 |
self.assertTrue(is_non_negative_int_or_float(0.0)) |
| 278 |
self.assertTrue(is_non_negative_int_or_float(0.72)) |
| 279 |
self.assertTrue(is_non_negative_int_or_float(2.0)) |
| 280 |
|
| 281 |
self.assertFalse(is_non_negative_int_or_float(-1)) |
| 282 |
self.assertFalse(is_non_negative_int_or_float(-7)) |
| 283 |
self.assertFalse(is_non_negative_int_or_float(-2.0)) |
| 284 |
self.assertFalse(is_non_negative_int_or_float(Decimal("2.0"))) |
| 285 |
self.assertFalse(is_non_negative_int_or_float(Decimal(2.0))) |
| 286 |
self.assertFalse(is_non_negative_int_or_float(Decimal(2))) |
| 287 |
self.assertFalse(is_non_negative_int_or_float('09a4')) |
| 288 |
self.assertFalse(is_non_negative_int_or_float('2A5C3F4062E4F2FCCE7A1C7B4310CB647B327409F580F4ED72CB8FC0B1804DFA')) |
| 289 |
self.assertFalse(is_non_negative_int_or_float('000')) |
| 290 |
self.assertFalse(is_non_negative_int_or_float('qweqwe')) |
| 291 |
self.assertFalse(is_non_negative_int_or_float(None)) |
| 292 |
|
| 293 |
def test_chunks(self): |
| 294 |
self.assertEqual([[1, 2], [3, 4], [5]], |
| 295 |
list(chunks([1, 2, 3, 4, 5], 2))) |
| 296 |
self.assertEqual([], list(chunks(b'', 64))) |
| 297 |
self.assertEqual([b'12', b'34', b'56'], |
| 298 |
list(chunks(b'123456', 2))) |
| 299 |
with self.assertRaises(ValueError): |
| 300 |
list(chunks([1, 2, 3], 0)) |
| 301 |
|
| 302 |
def test_list_enabled_bits(self): |
| 303 |
self.assertEqual((0, 2, 3, 6), list_enabled_bits(77)) |
| 304 |
self.assertEqual((), list_enabled_bits(0)) |
| 305 |
|
| 306 |
def test_is_ip_address(self): |
| 307 |
self.assertTrue(is_ip_address("127.0.0.1")) |
| 308 |
|
| 309 |
self.assertTrue(is_ip_address("255.255.255.255")) |
| 310 |
self.assertFalse(is_ip_address("255.255.256.255")) |
| 311 |
self.assertFalse(is_ip_address("123.456.789.000")) |
| 312 |
self.assertTrue(is_ip_address("2001:0db8:0000:0000:0000:ff00:0042:8329")) |
| 313 |
self.assertTrue(is_ip_address("2001:db8:0:0:0:ff00:42:8329")) |
| 314 |
self.assertTrue(is_ip_address("2001:db8::ff00:42:8329")) |
| 315 |
self.assertFalse(is_ip_address("2001:::db8::ff00:42:8329")) |
| 316 |
self.assertTrue(is_ip_address("::1")) |
| 317 |
self.assertFalse(is_ip_address("2001:db8:0:0:g:ff00:42:8329")) |
| 318 |
self.assertFalse(is_ip_address("lol")) |
| 319 |
self.assertFalse(is_ip_address(":@ASD:@AS\x77\x22\xff¬!")) |
| 320 |
|
| 321 |
def test_is_private_netaddress(self): |
| 322 |
self.assertTrue(is_private_netaddress("127.0.0.1")) |
| 323 |
self.assertTrue(is_private_netaddress("127.5.6.7")) |
| 324 |
self.assertTrue(is_private_netaddress("::1")) |
| 325 |
self.assertTrue(is_private_netaddress("[::1]")) |
| 326 |
self.assertTrue(is_private_netaddress("localhost")) |
| 327 |
self.assertTrue(is_private_netaddress("localhost.")) |
| 328 |
self.assertTrue(is_private_netaddress("192.168.1.1")) |
| 329 |
self.assertTrue(is_private_netaddress("10.10.10.10")) |
| 330 |
self.assertTrue(is_private_netaddress("172.16.0.1")) |
| 331 |
self.assertTrue(is_private_netaddress("172.31.255.254")) |
| 332 |
self.assertTrue(is_private_netaddress("::ffff:ac10:0001")) |
| 333 |
self.assertTrue(is_private_netaddress("[::ffff:c0a8:0001]")) |
| 334 |
self.assertTrue(is_private_netaddress("fe80::0001")) |
| 335 |
self.assertFalse(is_private_netaddress("[::2]")) |
| 336 |
self.assertFalse(is_private_netaddress("2a00:1450:400e:80d::200e")) |
| 337 |
self.assertFalse(is_private_netaddress("[2a00:1450:400e:80d::200e]")) |
| 338 |
self.assertFalse(is_private_netaddress("8.8.8.8")) |
| 339 |
self.assertFalse(is_private_netaddress("example.com")) |
| 340 |
|
| 341 |
def test_is_subpath(self): |
| 342 |
self.assertTrue(util.is_subpath("/a/b/c/d/e", "/")) |
| 343 |
self.assertTrue(util.is_subpath("/a/b/c/d/e", "/a")) |
| 344 |
self.assertTrue(util.is_subpath("/a/b/c/d/e", "/a/")) |
| 345 |
self.assertTrue(util.is_subpath("/a/b/c/d/e", "/a/b/c/")) |
| 346 |
self.assertTrue(util.is_subpath("/a/b/c/d/e/", "/a/b/c/")) |
| 347 |
self.assertTrue(util.is_subpath("/a/b/c/d/e/", "/a/b/c")) |
| 348 |
self.assertTrue(util.is_subpath("/a/b/c/d/e/", "/a/b/c/d/e/")) |
| 349 |
self.assertTrue(util.is_subpath("/", "/")) |
| 350 |
self.assertTrue(util.is_subpath("a/b/c", "a")) |
| 351 |
self.assertTrue(util.is_subpath("a/b/c", "a/")) |
| 352 |
self.assertTrue(util.is_subpath("a/b/c", "a/b")) |
| 353 |
self.assertTrue(util.is_subpath("a/b/c", "a/b/c")) |
| 354 |
|
| 355 |
self.assertFalse(util.is_subpath("/a/b/c/d/e/", "/b")) |
| 356 |
self.assertFalse(util.is_subpath("/a/b/c/d/e/", "/b/c/")) |
| 357 |
self.assertFalse(util.is_subpath("/a/b/c", "/a/b/c/d/e/")) |
| 358 |
self.assertFalse(util.is_subpath("/a/b/c", "a")) |
| 359 |
self.assertFalse(util.is_subpath("/a/b/c", "c")) |
| 360 |
self.assertFalse(util.is_subpath("a", "/a/b/c")) |
| 361 |
self.assertFalse(util.is_subpath("c", "/a/b/c")) |
| 362 |
|
| 363 |
def test_error_text_bytes_to_safe_str(self): |
| 364 |
|
| 365 |
self.assertEqual("'test'", util.error_text_bytes_to_safe_str(b"test")) |
| 366 |
self.assertEqual('"test123 \'QWE"', util.error_text_bytes_to_safe_str(b"test123 'QWE")) |
| 367 |
self.assertEqual("'prefix: \\x08\\x08\\x08\\x08\\x08\\x08\\x08\\x08malicious_stuff'", |
| 368 |
util.error_text_bytes_to_safe_str(b"prefix: " + 8 * b"\x08" + b"malicious_stuff")) |
| 369 |
|
| 370 |
self.assertEqual("'here is some unicode: \\\\xe2\\\\x82\\\\xbf \\\\xf0\\\\x9f\\\\x98\\\\x80 \\\\xf0\\\\x9f\\\\x98\\\\x88'", |
| 371 |
util.error_text_bytes_to_safe_str(b'here is some unicode: \xe2\x82\xbf \xf0\x9f\x98\x80 \xf0\x9f\x98\x88')) |
| 372 |
|
| 373 |
self.assertEqual("""\'\\x00\\x01\\x02\\x03\\x04\\x05\\x06\\x07\\x08\\t\\n\\x0b\\x0c\\r\\x0e\\x0f\\x10\\x11\\x12\\x13\\x14\\x15\\x16\\x17\\x18\\x19\\x1a\\x1b\\x1c\\x1d\\x1e\\x1f !"#$%&\\\'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\\\]^_`abcdefghijklmnopqrstuvwxyz{|}~\\x7f\\\\x80\\\\x81\\\\x82\\\\x83\\\\x84\\\\x85\\\\x86\\\\x87\\\\x88\\\\x89\\\\x8a\\\\x8b\\\\x8c\\\\x8d\\\\x8e\\\\x8f\\\\x90\\\\x91\\\\x92\\\\x93\\\\x94\\\\x95\\\\x96\\\\x97\\\\x98\\\\x99\\\\x9a\\\\x9b\\\\x9c\\\\x9d\\\\x9e\\\\x9f\\\\xa0\\\\xa1\\\\xa2\\\\xa3\\\\xa4\\\\xa5\\\\xa6\\\\xa7\\\\xa8\\\\xa9\\\\xaa\\\\xab\\\\xac\\\\xad\\\\xae\\\\xaf\\\\xb0\\\\xb1\\\\xb2\\\\xb3\\\\xb4\\\\xb5\\\\xb6\\\\xb7\\\\xb8\\\\xb9\\\\xba\\\\xbb\\\\xbc\\\\xbd\\\\xbe\\\\xbf\\\\xc0\\\\xc1\\\\xc2\\\\xc3\\\\xc4\\\\xc5\\\\xc6\\\\xc7\\\\xc8\\\\xc9\\\\xca\\\\xcb\\\\xcc\\\\xcd\\\\xce\\\\xcf\\\\xd0\\\\xd1\\\\xd2\\\\xd3\\\\xd4\\\\xd5\\\\xd6\\\\xd7\\\\xd8\\\\xd9\\\\xda\\\\xdb\\\\xdc\\\\xdd\\\\xde\\\\xdf\\\\xe0\\\\xe1\\\\xe2\\\\xe3\\\\xe4\\\\xe5\\\\xe6\\\\xe7\\\\xe8\\\\xe9\\\\xea\\\\xeb\\\\xec\\\\xed\\\\xee\\\\xef\\\\xf0\\\\xf1\\\\xf2\\\\xf3\\\\xf4\\\\xf5\\\\xf6\\\\xf7\\\\xf8\\\\xf9\\\\xfa\\\\xfb\\\\xfc\\\\xfd\\\\xfe\\\\xff\'""", |
| 374 |
util.error_text_bytes_to_safe_str(bytes(range(256)), max_len=1000)) |
| 375 |
|
| 376 |
t1 = util.error_text_bytes_to_safe_str(b"test" * 10000) |
| 377 |
self.assertTrue(t1.endswith("... (truncated. orig_len=40002)")) |
| 378 |
self.assertTrue(len(t1) < 550) |
| 379 |
|
| 380 |
def test_error_text_str_to_safe_str(self): |
| 381 |
|
| 382 |
self.assertEqual("'test'", util.error_text_str_to_safe_str("test")) |
| 383 |
self.assertEqual('"test123 \'QWE"', util.error_text_str_to_safe_str("test123 'QWE")) |
| 384 |
self.assertEqual("'prefix: \\x08\\x08\\x08\\x08\\x08\\x08\\x08\\x08malicious_stuff'", |
| 385 |
util.error_text_str_to_safe_str("prefix: " + 8 * "\x08" + "malicious_stuff")) |
| 386 |
|
| 387 |
self.assertEqual("'here is some unicode: \\\\u20bf \\\\U0001f600 \\\\U0001f608'", |
| 388 |
util.error_text_str_to_safe_str("here is some unicode: ₿ 😀 😈")) |
| 389 |
|
| 390 |
t1 = util.error_text_str_to_safe_str("test"*10000) |
| 391 |
self.assertTrue(t1.endswith("... (truncated. orig_len=40002)")) |
| 392 |
self.assertTrue(len(t1) < 550) |
| 393 |
|
| 394 |
def test_age(self): |
| 395 |
now = datetime(2023, 4, 16, 22, 30, 00) |
| 396 |
self.assertEqual("Unknown", |
| 397 |
util.age(from_date=None, since_date=now)) |
| 398 |
|
| 399 |
self.assertEqual("less than a minute ago", |
| 400 |
util.age(from_date=now.timestamp()-1, since_date=now)) |
| 401 |
self.assertEqual("1 seconds ago", |
| 402 |
util.age(from_date=now.timestamp()-1, since_date=now, include_seconds=True)) |
| 403 |
self.assertEqual("25 seconds ago", |
| 404 |
util.age(from_date=now.timestamp()-25, since_date=now, include_seconds=True)) |
| 405 |
self.assertEqual("about 30 minutes ago", |
| 406 |
util.age(from_date=now.timestamp()-1800, since_date=now)) |
| 407 |
self.assertEqual("about 30 minutes ago", |
| 408 |
util.age(from_date=now.timestamp()-1800, since_date=now, include_seconds=True)) |
| 409 |
self.assertEqual("about 1 hour ago", |
| 410 |
util.age(from_date=now.timestamp()-3300, since_date=now)) |
| 411 |
self.assertEqual("about 2 hours ago", |
| 412 |
util.age(from_date=now.timestamp()-8700, since_date=now)) |
| 413 |
self.assertEqual("about 7 hours ago", |
| 414 |
util.age(from_date=now.timestamp()-26700, since_date=now)) |
| 415 |
self.assertEqual("about 1 day ago", |
| 416 |
util.age(from_date=now.timestamp()-109800, since_date=now)) |
| 417 |
self.assertEqual("about 3 days ago", |
| 418 |
util.age(from_date=now.timestamp()-282600, since_date=now)) |
| 419 |
self.assertEqual("about 15 days ago", |
| 420 |
util.age(from_date=now.timestamp()-1319400, since_date=now)) |
| 421 |
self.assertEqual("about 1 month ago", |
| 422 |
util.age(from_date=now.timestamp()-3220200, since_date=now)) |
| 423 |
self.assertEqual("about 3 months ago", |
| 424 |
util.age(from_date=now.timestamp()-8317800, since_date=now)) |
| 425 |
self.assertEqual("about 1 year ago", |
| 426 |
util.age(from_date=now.timestamp()-39853800, since_date=now)) |
| 427 |
self.assertEqual("over 3 years ago", |
| 428 |
util.age(from_date=now.timestamp()-103012200, since_date=now)) |
| 429 |
|
| 430 |
self.assertEqual("in less than a minute", |
| 431 |
util.age(from_date=now.timestamp()+1, since_date=now)) |
| 432 |
self.assertEqual("in 1 seconds", |
| 433 |
util.age(from_date=now.timestamp()+1, since_date=now, include_seconds=True)) |
| 434 |
self.assertEqual("in 25 seconds", |
| 435 |
util.age(from_date=now.timestamp()+25, since_date=now, include_seconds=True)) |
| 436 |
self.assertEqual("in about 30 minutes", |
| 437 |
util.age(from_date=now.timestamp()+1800, since_date=now)) |
| 438 |
self.assertEqual("in about 30 minutes", |
| 439 |
util.age(from_date=now.timestamp()+1800, since_date=now, include_seconds=True)) |
| 440 |
self.assertEqual("in about 1 hour", |
| 441 |
util.age(from_date=now.timestamp()+3300, since_date=now)) |
| 442 |
self.assertEqual("in about 2 hours", |
| 443 |
util.age(from_date=now.timestamp()+8700, since_date=now)) |
| 444 |
self.assertEqual("in about 7 hours", |
| 445 |
util.age(from_date=now.timestamp()+26700, since_date=now)) |
| 446 |
self.assertEqual("in about 1 day", |
| 447 |
util.age(from_date=now.timestamp()+109800, since_date=now)) |
| 448 |
self.assertEqual("in about 3 days", |
| 449 |
util.age(from_date=now.timestamp()+282600, since_date=now)) |
| 450 |
self.assertEqual("in about 15 days", |
| 451 |
util.age(from_date=now.timestamp()+1319400, since_date=now)) |
| 452 |
self.assertEqual("in about 1 month", |
| 453 |
util.age(from_date=now.timestamp()+3220200, since_date=now)) |
| 454 |
self.assertEqual("in about 3 months", |
| 455 |
util.age(from_date=now.timestamp()+8317800, since_date=now)) |
| 456 |
self.assertEqual("in about 1 year", |
| 457 |
util.age(from_date=now.timestamp()+39853800, since_date=now)) |
| 458 |
self.assertEqual("in over 3 years", |
| 459 |
util.age(from_date=now.timestamp()+103012200, since_date=now)) |
| 460 |
|
| 461 |
def test_shortchannelid(self): |
| 462 |
scid1 = ShortID.from_components(2, 45, 789) |
| 463 |
self.assertEqual("2x45x789", str(scid1)) |
| 464 |
self.assertEqual(2, scid1.block_height) |
| 465 |
self.assertEqual(45, scid1.txpos) |
| 466 |
self.assertEqual(789, scid1.output_index) |
| 467 |
|
| 468 |
scid2_raw = bytes([0, 0, 2, 0, 0, 45, 789 // 256, 789 % 256]) |
| 469 |
scid2 = ShortID(scid2_raw) |
| 470 |
self.assertEqual(scid1, scid2_raw) |
| 471 |
self.assertEqual(scid1, scid2) |
| 472 |
self.assertEqual(scid1, ShortID.from_str(str(scid1))) |
| 473 |
self.assertEqual(scid1, ShortID.normalize(scid1.hex())) |
| 474 |
self.assertEqual(scid1, ShortID.normalize(bytes(scid1))) |
| 475 |
|
| 476 |
self.assertTrue(ShortID.from_components(3, 30, 300) == ShortID.from_components(3, 30, 300)) |
| 477 |
self.assertTrue(ShortID.from_components(3, 30, 300) > ShortID.from_components(2, 999, 999)) |
| 478 |
self.assertTrue(ShortID.from_components(3, 30, 300) < ShortID.from_components(3, 999, 999)) |
| 479 |
self.assertTrue(ShortID.from_components(3, 30, 300) > ShortID.from_components(3, 1, 1)) |
| 480 |
self.assertTrue(ShortID.from_components(3, 30, 300) > ShortID.from_components(3, 1, 999)) |
| 481 |
self.assertTrue(ShortID.from_components(3, 30, 300) < ShortID.from_components(3, 999, 1)) |
| 482 |
|
| 483 |
async def test_custom_task_factory(self): |
| 484 |
loop = util.get_running_loop() |
| 485 |
|
| 486 |
util._set_custom_task_factory(loop) |
| 487 |
|
| 488 |
evt = asyncio.Event() |
| 489 |
async def foo(): |
| 490 |
await evt.wait() |
| 491 |
|
| 492 |
|
| 493 |
fut = asyncio.ensure_future(foo()) |
| 494 |
self.assertTrue(fut in util._running_asyncio_tasks) |
| 495 |
fut = asyncio.create_task(foo()) |
| 496 |
self.assertTrue(fut in util._running_asyncio_tasks) |
| 497 |
fut = loop.create_task(foo()) |
| 498 |
self.assertTrue(fut in util._running_asyncio_tasks) |
| 499 |
fut = asyncio.run_coroutine_threadsafe(foo(), loop=loop) |
| 500 |
|
| 501 |
|
| 502 |
|
| 503 |
|
| 504 |
|
| 505 |
for _ in range(10): |
| 506 |
await asyncio.sleep(0) |
| 507 |
|
| 508 |
|
| 509 |
self.assertEqual(4, len(util._running_asyncio_tasks)) |
| 510 |
for task in util._running_asyncio_tasks: |
| 511 |
self.assertEqual(foo.__qualname__, task.get_coro().__qualname__) |
| 512 |
|
| 513 |
evt.set() |
| 514 |
|
| 515 |
for _ in range(10): |
| 516 |
await asyncio.sleep(0) |
| 517 |
|
| 518 |
self.assertEqual(0, len(util._running_asyncio_tasks)) |
| 519 |
|