wownero-python/tests/test_address.py

245 lines
12 KiB
Python
Raw Normal View History

import pytest
2017-11-24 02:05:16 +00:00
import unittest
from monero import const
2017-11-30 02:43:34 +00:00
from monero.address import Address, SubAddress, IntegratedAddress, address
from tests.utils import classproperty
2017-11-24 02:05:16 +00:00
class Tests(object):
@classproperty
def __test__(cls):
return issubclass(cls, unittest.TestCase)
2017-11-24 02:05:16 +00:00
def test_from_and_to_string(self):
a = Address(self.addr)
self.assertEqual(str(a), self.addr)
2019-10-04 12:38:17 +00:00
self.assertEqual("{:s}".format(a), self.addr)
self.assertEqual(a.spend_key(), self.psk)
self.assertEqual(a.view_key(), self.pvk)
2019-09-09 11:57:03 +00:00
self.assertEqual(hash(a), hash(self.addr))
2019-10-07 13:10:54 +00:00
ba = Address(self.addr.encode())
self.assertEqual(ba, a)
ba = address(self.addr.encode())
self.assertEqual(ba, a)
2017-11-24 02:05:16 +00:00
ia = IntegratedAddress(self.iaddr)
self.assertEqual(ia.payment_id(), self.pid)
2017-11-24 02:05:16 +00:00
self.assertEqual(str(ia), self.iaddr)
2019-10-04 12:38:17 +00:00
self.assertEqual("{:s}".format(ia), self.iaddr)
self.assertEqual(ia.spend_key(), self.psk)
self.assertEqual(ia.view_key(), self.pvk)
self.assertEqual(ia.base_address(), a)
2019-10-07 13:10:54 +00:00
ba = IntegratedAddress(self.iaddr.encode())
self.assertEqual(ba, ia)
ba = address(self.iaddr.encode())
self.assertEqual(ba, ia)
2017-11-24 02:05:16 +00:00
2017-11-30 02:43:34 +00:00
sa = SubAddress(self.subaddr)
self.assertEqual(str(sa), self.subaddr)
2019-10-04 12:38:17 +00:00
self.assertEqual("{:s}".format(sa), self.subaddr)
2019-10-07 13:10:54 +00:00
ba = SubAddress(self.subaddr.encode())
self.assertEqual(ba, sa)
ba = address(self.subaddr.encode())
self.assertEqual(ba, sa)
2017-11-30 02:43:34 +00:00
2017-11-24 02:05:16 +00:00
def test_payment_id(self):
a = Address(self.addr)
ia = a.with_payment_id(self.pid)
self.assertIsInstance(ia, IntegratedAddress)
self.assertEqual(ia.payment_id(), self.pid)
2017-11-24 02:05:16 +00:00
self.assertEqual(str(ia), self.iaddr)
@pytest.mark.filterwarnings("ignore::DeprecationWarning")
2017-11-24 02:05:16 +00:00
def test_recognition_and_comparisons(self):
a = Address(self.addr)
a2 = address(self.addr)
self.assertIsInstance(a2, Address)
self.assertEqual(a, a2)
self.assertEqual(a, self.addr)
self.assertEqual(self.addr, a)
2019-09-09 11:57:03 +00:00
self.assertEqual(hash(a), hash(self.addr))
self.assertEqual(a.net, self.net)
with pytest.deprecated_call():
self.assertEqual(a.is_mainnet(), self.net == const.NET_MAIN)
with pytest.deprecated_call():
self.assertEqual(a.is_testnet(), self.net == const.NET_TEST)
with pytest.deprecated_call():
self.assertEqual(a.is_stagenet(), self.net == const.NET_STAGE)
self.assertEqual(a2.net, self.net)
with pytest.deprecated_call():
self.assertEqual(a2.is_mainnet(), self.net == const.NET_MAIN)
with pytest.deprecated_call():
self.assertEqual(a2.is_testnet(), self.net == const.NET_TEST)
with pytest.deprecated_call():
self.assertEqual(a2.is_stagenet(), self.net == const.NET_STAGE)
2017-11-24 02:05:16 +00:00
ia = IntegratedAddress(self.iaddr)
ia2 = address(self.iaddr)
self.assertIsInstance(ia, IntegratedAddress)
self.assertEqual(ia, ia2)
self.assertEqual(ia, self.iaddr)
self.assertEqual(self.iaddr, ia)
2019-09-09 11:57:03 +00:00
self.assertEqual(hash(ia), hash(self.iaddr))
self.assertEqual(ia.net, self.net)
with pytest.deprecated_call():
self.assertEqual(ia.is_mainnet(), self.net == const.NET_MAIN)
with pytest.deprecated_call():
self.assertEqual(ia.is_testnet(), self.net == const.NET_TEST)
with pytest.deprecated_call():
self.assertEqual(ia.is_stagenet(), self.net == const.NET_STAGE)
self.assertEqual(ia2.net, self.net)
with pytest.deprecated_call():
self.assertEqual(ia2.is_mainnet(), self.net == const.NET_MAIN)
with pytest.deprecated_call():
self.assertEqual(ia2.is_testnet(), self.net == const.NET_TEST)
with pytest.deprecated_call():
self.assertEqual(ia2.is_stagenet(), self.net == const.NET_STAGE)
self.assertEqual(ia2.base_address(), a)
2017-11-24 02:05:16 +00:00
self.assertEqual(ia.view_key(), a.view_key())
self.assertEqual(ia.spend_key(), a.spend_key())
2018-01-14 04:56:37 +00:00
2017-11-30 02:43:34 +00:00
sa = SubAddress(self.subaddr)
sa2 = address(self.subaddr)
self.assertIsInstance(sa2, SubAddress)
self.assertEqual(sa, sa2)
self.assertEqual(sa, self.subaddr)
self.assertEqual(self.subaddr, sa)
2019-09-09 11:57:03 +00:00
self.assertEqual(hash(sa), hash(self.subaddr))
self.assertEqual(sa.net, self.net)
with pytest.deprecated_call():
self.assertEqual(sa.is_mainnet(), self.net == const.NET_MAIN)
with pytest.deprecated_call():
self.assertEqual(sa.is_testnet(), self.net == const.NET_TEST)
with pytest.deprecated_call():
self.assertEqual(sa.is_stagenet(), self.net == const.NET_STAGE)
self.assertEqual(sa2.net, self.net)
with pytest.deprecated_call():
self.assertEqual(sa2.is_mainnet(), self.net == const.NET_MAIN)
with pytest.deprecated_call():
self.assertEqual(sa2.is_testnet(), self.net == const.NET_TEST)
with pytest.deprecated_call():
self.assertEqual(sa2.is_stagenet(), self.net == const.NET_STAGE)
2017-11-30 02:43:34 +00:00
2018-01-30 08:43:08 +00:00
self.assertNotEqual(a, 0)
2018-11-30 00:23:49 +00:00
def test_check_private_view_key(self):
a = Address(self.addr)
self.assertFalse(a.check_private_view_key(self.ssk))
self.assertTrue(a.check_private_view_key(self.svk))
self.assertFalse(a.check_private_view_key(self.psk))
self.assertFalse(a.check_private_view_key(self.pvk))
self.assertFalse(a.check_private_view_key('0000000000000000000000000000000000000000000000000000000000000000'))
def test_check_private_spend_key(self):
a = Address(self.addr)
self.assertTrue(a.check_private_spend_key(self.ssk))
self.assertFalse(a.check_private_spend_key(self.svk))
self.assertFalse(a.check_private_spend_key(self.psk))
self.assertFalse(a.check_private_spend_key(self.pvk))
self.assertFalse(a.check_private_spend_key('0000000000000000000000000000000000000000000000000000000000000000'))
2017-11-29 03:37:01 +00:00
def test_idempotence(self):
a = Address(self.addr)
a_idem = Address(a)
self.assertEqual(a, a_idem)
a_idem = Address(str(a))
self.assertEqual(a, a_idem)
a_idem = address(a)
self.assertEqual(a, a_idem)
ia = IntegratedAddress(self.iaddr)
ia_idem = IntegratedAddress(ia)
self.assertEqual(ia, ia_idem)
ia_idem = IntegratedAddress(str(ia))
self.assertEqual(ia, ia_idem)
ia_idem = address(ia)
self.assertEqual(ia, ia_idem)
2017-11-24 02:05:16 +00:00
2017-11-30 02:43:34 +00:00
def test_invalid(self):
self.assertRaises(ValueError, Address, self.addr_invalid)
self.assertRaises(ValueError, Address, self.iaddr_invalid)
2017-12-10 01:54:09 +00:00
a = Address(self.addr)
self.assertRaises(TypeError, a.with_payment_id, 2**64+1)
self.assertRaises(TypeError, a.with_payment_id, "%x" % (2**64+1))
s = SubAddress(self.subaddr)
self.assertRaises(TypeError, s.with_payment_id, 0)
2018-01-30 08:43:08 +00:00
self.assertRaises(ValueError, address, 'whatever')
self.assertRaises(ValueError, Address, 'whatever')
self.assertRaises(ValueError, SubAddress, 'whatever')
self.assertRaises(ValueError, IntegratedAddress, 'whatever')
# Aeon
self.assertRaises(
ValueError,
address,
'Wmtj8UAJhdrhbKvwyBJmLEUZKHcffv2VHNBaq6oTxJFwJjUj3QwMUSS32mddSX7vchbxXdmb4QuZA9TsN47441f61yAYLQYTo')
# invalid netbyte
self.assertRaises(
ValueError,
address,
'Cf6RinMUztY5otm6NEFjg3UWBBkXK6Lh23wKrLFMEcCY7i3A6aPLH9i4QMCkf6CdWk8Q9N7yoJf7ANKgtQMuPM6JANXgCWs')
2017-11-30 02:43:34 +00:00
def test_type_mismatch(self):
self.assertRaises(ValueError, Address, self.iaddr)
self.assertRaises(ValueError, Address, self.subaddr)
self.assertRaises(ValueError, IntegratedAddress, self.addr)
self.assertRaises(ValueError, IntegratedAddress, self.subaddr)
self.assertRaises(ValueError, SubAddress, self.addr)
self.assertRaises(ValueError, SubAddress, self.iaddr)
def test_subaddress_cannot_into_integrated(self):
sa = SubAddress(self.subaddr)
self.assertRaises(TypeError, sa.with_payment_id, self.pid)
2018-11-30 00:23:49 +00:00
2019-01-03 01:32:56 +00:00
class AddressTestCase(Tests, unittest.TestCase):
2018-11-30 00:23:49 +00:00
addr = '47ewoP19TN7JEEnFKUJHAYhGxkeTRH82sf36giEp9AcNfDBfkAtRLX7A6rZz18bbNHPNV7ex6WYbMN3aKisFRJZ8Ebsmgef'
ssk = 'e0fe01d5794e240a26609250c0d7e01673219eececa3f499d5cfa20a75739b0a'
svk = '6d9056aa2c096bfcd2f272759555e5764ba204dd362604a983fa3e0aafd35901'
psk = '9f2a76d879aaf0670039dc8dbdca01f0ca26a2f6d93268e3674666bfdc5957e4'
pvk = '716cfc7da7e6ce366935c55747839a85be798037ab189c7dd0f10b7f1690cb78'
2017-11-24 02:05:16 +00:00
pid = '4a6f686e47616c74'
2018-11-30 00:23:49 +00:00
iaddr = '4HMcpBpe4ddJEEnFKUJHAYhGxkeTRH82sf36giEp9AcNfDBfkAtRLX7A6rZz18bbNHPNV7ex6WYbMN3aKisFRJZ8M7yKhzQhKW3ECCLWQw'
subaddr = '84LooD7i35SFppgf4tQ453Vi3q5WexSUXaVgut69ro8MFnmHwuezAArEZTZyLr9fS6QotjqkSAxSF6d1aDgsPoX849izJ7m'
net = const.NET_MAIN
2018-11-30 00:23:49 +00:00
addr_invalid = '47ewoP19TN7JCEnFKUJHAYhGxkeTRH82sf36giEp9AcNfDBfkAtRLX7A6rZz18bbNHPNV7ex6WYbMN3aKisFRJZ8Ebsmgef'
iaddr_invalid = '4HMcpBpe4ddJEEnFKUJHAYhGxkyTRH82sf36giEp9AcNfDBfkAtRLX7A6rZz18bbNHPNV7ex6WYbMN3aKisFRJZ8M7yKhzQhKW3ECCLWQw'
2017-11-24 02:05:16 +00:00
2019-01-03 01:32:56 +00:00
class TestnetAddressTestCase(Tests, unittest.TestCase):
2018-11-30 00:23:49 +00:00
addr = '9wuKTHsxGiwEsMp2fYzJiVahyhU2aZi1oZ6R6fK5U64uRa1Pxi8diZh2S1GJFqYXRRhcbfzfWiPD819zKEZkXTMwP7hMs5N'
ssk = '4f5b7af2c1942067ba33d34318b9735cb46ab5d50b75294844c82a9dd872c201'
svk = '60cf228f2bf7f6a70643afe9468fde254145dbd3aab4072ede14bf8bae914103'
psk = '7cf743dcfd23d452e9b2936caeb622c9849f1ff1ddfd62bfdfac64113c1a4e92'
pvk = 'e3924b14d99a9c088e5a45278d5218f2d053b1c03c480f00ed2ee3dce80806c4'
2017-11-24 02:05:16 +00:00
pid = '4a6f686e47616c74'
2018-11-30 00:23:49 +00:00
subaddr = 'BaU3yLuDqdcETYzeF7vFSVEKNR4sSGxBV1Evrw5yNBf2VMiuAwfDmiF3RHqLHkaA5A6RGiNNRUqvtaqhMtdjA1SQ1tnQV8D'
iaddr = 'A7bzU6hSszTEsMp2fYzJiVahyhU2aZi1oZ6R6fK5U64uRa1Pxi8diZh2S1GJFqYXRRhcbfzfWiPD819zKEZkXTMwZqGSmLeBXqMEBnZVkh'
net = const.NET_TEST
2018-11-30 00:23:49 +00:00
addr_invalid = '9wuKTHsxGiwEsMp3fYzJiVahyhU2aZi1oZ6R6fK5U64uRa1Pxi8diZh2S1GJFqYXRRhcbfzfWiPD819zKEZkXTMwP7hMs5N'
iaddr_invalid = 'A7bzU6hSszTEsMp2fYzJiVahyhU2aZi2oZ6R6fK5U64uRa1Pxi8diZh2S1GJFqYXRRhcbfzfWiPD819zKEZkXTMwZqGSmLeBXqMEBnZVkh'
2018-05-21 15:25:28 +00:00
2019-01-03 01:32:56 +00:00
class StagenetAddressTestCase(Tests, unittest.TestCase):
2018-11-30 00:23:49 +00:00
addr = '52jzuBBUMty3xPL3JsQxGP74LDuV6E1LS8Zda1PbdqQjGzFmH6N9ep9McbFKMALujVT9S5mKpbEgC5VPhfoAiVj8LdAqbp6'
ssk = 'a8733c61797115db4ec8a5ce39fb811f81dd4ec163b880526683e059c7e62503'
svk = 'fd5c0d25f8f994268079a4f7844274dc870a7c2b88fbfc24ba318375e1d9430f'
psk = '180c1d7bbf7f2e11aa90d0f61bf49024370e01cd54f33f2d36bba0357c9c205f'
pvk = '94b66a81e646927b3da74392306f789c5024734b4ce6351ad74c4c7d7351b3ad'
2018-05-21 15:25:28 +00:00
pid = '4a6f686e47616c74'
2018-11-30 00:23:49 +00:00
subaddr = '7AeQwvrLtPeYoXVPRkEu8oEL7N9wnqHjYKwSvTf6YKbHgYmw6AJMsjggzVLo21egMK9qcoV1mxCTfP4FbaGb7JEMDfpLetk'
iaddr = '5CSfuyzxyAV3xPL3JsQxGP74LDuV6E1LS8Zda1PbdqQjGzFmH6N9ep9McbFKMALujVT9S5mKpbEgC5VPhfoAiVj8Vz8ySmoqYgTE8dR1yS'
net = const.NET_STAGE
2018-11-30 00:23:49 +00:00
addr_invalid = '52jzuBBUMty3xPL3JsQxGP74LDuV6H1LS8Zda1PbdqQjGzFmH6N9ep9McbFKMALujVT9S5mKpbEgC5VPhfoAiVj8LdAqbp6'
iaddr_invalid = '5CSfuyzxyAV3xPL3JsQxGP74LDuV6E1LS8Zda1PbdqQjGzFmH6N9ep9McbFKMALujVT9S5mKppEgC5VPhfoAiVj8Vz8ySmoqYgTE8dR1yS'
class KnownBugsTest(unittest.TestCase):
def test_issue27(self):
addr = '41tjz19p4qc2gudqnwsdrhgcgxud8bgxy84ufe869nyw7ywbxw9s9gqbix7piu9d7qjvbjtrdnbubhcf663ydq3bsxj1brL'
self.assertRaises(ValueError, Address, addr)
self.assertRaises(ValueError, SubAddress, addr)
self.assertRaises(ValueError, IntegratedAddress, addr)
self.assertRaises(ValueError, address, addr)