Skip to content

Commit d4802c6

Browse files
committed
Add tests for address
1 parent d00442e commit d4802c6

File tree

3 files changed

+270
-20
lines changed

3 files changed

+270
-20
lines changed

pycardano/key.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -318,6 +318,13 @@ def from_signing_key(
318318
) -> StakeKeyPair:
319319
return cls(signing_key, StakeVerificationKey.from_signing_key(signing_key))
320320

321+
def __eq__(self, other):
322+
if isinstance(other, StakeKeyPair):
323+
return (
324+
other.signing_key == self.signing_key
325+
and other.verification_key == self.verification_key
326+
)
327+
321328

322329
class StakePoolSigningKey(SigningKey):
323330
KEY_TYPE = "StakePoolSigningKey_ed25519"

test/pycardano/test_address.py

Lines changed: 188 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,17 @@
1-
from unittest import TestCase
1+
import pytest
22

3-
from pycardano.address import Address, PointerAddress
4-
from pycardano.exception import DeserializeException
3+
from pycardano.address import Address, AddressType, PointerAddress
4+
from pycardano.exception import (
5+
DecodingException,
6+
DeserializeException,
7+
InvalidAddressInputException,
8+
)
9+
from pycardano.hash import (
10+
SCRIPT_HASH_SIZE,
11+
VERIFICATION_KEY_HASH_SIZE,
12+
ScriptHash,
13+
VerificationKeyHash,
14+
)
515
from pycardano.key import PaymentVerificationKey
616
from pycardano.network import Network
717

@@ -20,25 +30,184 @@ def test_payment_addr():
2030
)
2131

2232

23-
class PointerAddressTest(TestCase):
24-
def test_from_primitive_invalid_value(self):
25-
with self.assertRaises(DeserializeException):
26-
PointerAddress.from_primitive(1)
33+
def test_to_primitive_pointer_addr():
34+
assert PointerAddress(1, 2, 3).to_primitive() == b"\x01\x02\x03"
2735

28-
with self.assertRaises(DeserializeException):
29-
PointerAddress.from_primitive([])
3036

31-
with self.assertRaises(DeserializeException):
32-
PointerAddress.from_primitive({})
37+
def test_from_primitive_pointer_addr():
38+
assert PointerAddress.from_primitive(
39+
b"\x01\x02\x03"
40+
) == PointerAddress.from_primitive(b"\x01\x02\x03")
3341

3442

35-
class AddressTest(TestCase):
36-
def test_from_primitive_invalid_value(self):
37-
with self.assertRaises(DeserializeException):
38-
Address.from_primitive(1)
43+
def test_from_primitive_invalid_value_pointer_addr():
44+
with pytest.raises(DecodingException):
45+
PointerAddress.decode(data=b"\x01\x02")
3946

40-
with self.assertRaises(DeserializeException):
41-
Address.from_primitive([])
47+
with pytest.raises(DeserializeException):
48+
PointerAddress.from_primitive(1)
4249

43-
with self.assertRaises(DeserializeException):
44-
Address.from_primitive({})
50+
with pytest.raises(DeserializeException):
51+
PointerAddress.from_primitive([])
52+
53+
with pytest.raises(DeserializeException):
54+
PointerAddress.from_primitive({})
55+
56+
57+
def test_equality_pointer_addr():
58+
assert PointerAddress(1, 2, 3) == PointerAddress(1, 2, 3)
59+
60+
61+
def test_inequality_different_values_pointer_addr():
62+
assert PointerAddress(1, 2, 3) != PointerAddress(4, 5, 6)
63+
64+
65+
def test_inequality_not_pointer_addr():
66+
assert PointerAddress(1, 2, 3) != (1, 2, 3)
67+
68+
69+
def test_inequality_null_pointer_addr():
70+
assert PointerAddress(1, 2, 3) != None
71+
72+
73+
def test_self_equality_pointer_addr():
74+
assert PointerAddress(1, 2, 3) == PointerAddress(1, 2, 3)
75+
76+
77+
def test_from_primitive_invalid_value_addr():
78+
with pytest.raises(DeserializeException):
79+
Address.from_primitive(1)
80+
81+
with pytest.raises(DeserializeException):
82+
Address.from_primitive([])
83+
84+
with pytest.raises(DeserializeException):
85+
Address.from_primitive({})
86+
87+
88+
def test_key_script_addr():
89+
address = Address(
90+
VerificationKeyHash(b"1" * VERIFICATION_KEY_HASH_SIZE),
91+
ScriptHash(b"1" * SCRIPT_HASH_SIZE),
92+
)
93+
assert address.address_type == AddressType.KEY_SCRIPT
94+
95+
96+
def test_script_key_addr():
97+
address = Address(
98+
ScriptHash(b"1" * SCRIPT_HASH_SIZE),
99+
VerificationKeyHash(b"1" * VERIFICATION_KEY_HASH_SIZE),
100+
)
101+
assert address.address_type == AddressType.SCRIPT_KEY
102+
103+
104+
def test_script_point_addr():
105+
address = Address(ScriptHash(b"1" * SCRIPT_HASH_SIZE), PointerAddress(1, 2, 3))
106+
assert address.address_type == AddressType.SCRIPT_POINTER
107+
108+
109+
def test_none_script_hash_addr():
110+
address = Address(None, ScriptHash(b"1" * SCRIPT_HASH_SIZE))
111+
assert address.address_type == AddressType.NONE_SCRIPT
112+
113+
114+
def test_invalid_combination_unhandled_types_addr():
115+
class UnknownType:
116+
pass
117+
118+
with pytest.raises(InvalidAddressInputException):
119+
Address(UnknownType(), UnknownType())
120+
121+
122+
def test_equality_same_values_addr():
123+
a1 = Address(
124+
VerificationKeyHash(b"1" * VERIFICATION_KEY_HASH_SIZE),
125+
ScriptHash(b"1" * SCRIPT_HASH_SIZE),
126+
)
127+
a2 = Address(
128+
VerificationKeyHash(b"1" * VERIFICATION_KEY_HASH_SIZE),
129+
ScriptHash(b"1" * SCRIPT_HASH_SIZE),
130+
)
131+
assert a1 == a2
132+
133+
134+
def test_inequality_not_address_addr():
135+
a1 = Address(
136+
VerificationKeyHash(b"1" * VERIFICATION_KEY_HASH_SIZE),
137+
ScriptHash(b"1" * SCRIPT_HASH_SIZE),
138+
)
139+
not_address = (1, 2, 3)
140+
assert a1 != not_address
141+
142+
143+
def test_from_primitive_address_type_key_script_addr():
144+
header = AddressType.KEY_SCRIPT.value << 4
145+
payment = b"\x01" * VERIFICATION_KEY_HASH_SIZE
146+
staking = b"\x02" * SCRIPT_HASH_SIZE
147+
value = bytes([header]) + payment + staking
148+
149+
address = Address.from_primitive(value)
150+
151+
assert isinstance(address.payment_part, VerificationKeyHash)
152+
153+
assert isinstance(address.staking_part, ScriptHash)
154+
155+
156+
def test_from_primitive_type_verification_key_hash_addr():
157+
header = AddressType.KEY_POINTER.value << 4
158+
payment = b"\x01" * VERIFICATION_KEY_HASH_SIZE
159+
staking = b"\x01\x02\x03"
160+
value = bytes([header]) + payment + staking
161+
162+
address = Address.from_primitive(value)
163+
164+
assert isinstance(address.payment_part, VerificationKeyHash)
165+
166+
assert isinstance(address.staking_part, PointerAddress)
167+
168+
169+
def test_from_primitive_staking_script_hash_addr():
170+
header = AddressType.SCRIPT_KEY.value << 4
171+
payment = b"\x01" * SCRIPT_HASH_SIZE
172+
staking = b"\x02" * VERIFICATION_KEY_HASH_SIZE
173+
value = bytes([header]) + payment + staking
174+
175+
address = Address.from_primitive(value)
176+
177+
assert isinstance(address.payment_part, ScriptHash)
178+
179+
assert isinstance(address.staking_part, VerificationKeyHash)
180+
181+
182+
def test_from_primitive_payment_script_hash_addr():
183+
header = AddressType.SCRIPT_POINTER.value << 4
184+
payment = b"\x01" * SCRIPT_HASH_SIZE
185+
staking = b"\x01\x02\x03"
186+
value = bytes([header]) + payment + staking
187+
188+
address = Address.from_primitive(value)
189+
190+
assert isinstance(address.payment_part, ScriptHash)
191+
192+
193+
def test_from_primitive_type_none_addr():
194+
header = AddressType.NONE_SCRIPT.value << 4
195+
payment = b"\x01" * 14
196+
staking = b"\x02" * 14
197+
value = bytes([header]) + payment + staking
198+
199+
address = Address.from_primitive(value)
200+
201+
assert address.payment_part is None
202+
203+
assert isinstance(address.staking_part, ScriptHash)
204+
205+
206+
def test_from_primitive_invalid_type_addr():
207+
header = AddressType.BYRON.value << 4
208+
payment = b"\x01" * 14
209+
staking = b"\x02" * 14
210+
value = bytes([header]) + payment + staking
211+
212+
with pytest.raises(DeserializeException):
213+
Address.from_primitive(value)

test/pycardano/test_key.py

Lines changed: 75 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,16 @@
1+
import json
12
import pathlib
23
import tempfile
34

5+
import pytest
46
from mnemonic import Mnemonic
57

6-
from pycardano import HDWallet
8+
from pycardano import HDWallet, StakeKeyPair, StakeSigningKey, StakeVerificationKey
9+
from pycardano.exception import InvalidKeyTypeException
710
from pycardano.key import (
811
ExtendedSigningKey,
912
ExtendedVerificationKey,
13+
Key,
1014
PaymentExtendedSigningKey,
1115
PaymentKeyPair,
1216
PaymentSigningKey,
@@ -66,6 +70,56 @@
6670
)
6771

6872

73+
def test_invalid_key_type():
74+
data = json.dumps(
75+
{
76+
"type": "invalid_type",
77+
"payload": "example_payload",
78+
"description": "example_description",
79+
}
80+
)
81+
82+
with pytest.raises(InvalidKeyTypeException):
83+
Key.from_json(data, validate_type=True)
84+
85+
86+
def test_bytes_conversion():
87+
obj = Key(b"1234")
88+
assert (bytes)(Key(b"1234")) == b"1234"
89+
90+
91+
def test_eq_not_instance():
92+
assert Key(b"hello") != "1234"
93+
94+
95+
def test_from_hdwallet_missing_xprivate_key():
96+
obj = ExtendedSigningKey(b"1234")
97+
98+
with pytest.raises(InvalidKeyTypeException):
99+
obj.from_hdwallet(
100+
HDWallet(
101+
b"root_xprivate_key",
102+
b"root_public_key",
103+
b"root_chain_code",
104+
None,
105+
b"valid_public_key",
106+
chain_code=b"valid_chain_code",
107+
)
108+
)
109+
110+
with pytest.raises(InvalidKeyTypeException):
111+
obj.from_hdwallet(
112+
HDWallet(
113+
b"root_xprivate_key",
114+
b"root_public_key",
115+
b"root_chain_code",
116+
b"valid_xprivate_key",
117+
b"valid_public_key",
118+
None,
119+
)
120+
)
121+
122+
69123
def test_payment_key():
70124
assert (
71125
SK.payload
@@ -123,12 +177,25 @@ def test_extended_payment_key_sign():
123177

124178

125179
def test_key_pair():
180+
PaymentSigningKey.generate()
181+
182+
183+
def test_payment_key_pair():
184+
PaymentKeyPair.generate()
126185
sk = PaymentSigningKey.generate()
127186
vk = PaymentVerificationKey.from_signing_key(sk)
128187
assert PaymentKeyPair(sk, vk) == PaymentKeyPair.from_signing_key(sk)
129188

130189

190+
def test_stake_key_pair():
191+
StakeKeyPair.generate()
192+
sk = StakeSigningKey.generate()
193+
vk = StakeVerificationKey.from_signing_key(sk)
194+
assert StakeKeyPair(sk, vk) == StakeKeyPair.from_signing_key(sk)
195+
196+
131197
def test_stake_pool_key_pair():
198+
StakePoolKeyPair.generate()
132199
sk = StakePoolSigningKey.generate()
133200
vk = StakePoolVerificationKey.from_signing_key(sk)
134201
assert StakePoolKeyPair(sk, vk) == StakePoolKeyPair.from_signing_key(sk)
@@ -158,6 +225,13 @@ def test_key_save():
158225
assert SK == sk
159226

160227

228+
def test_key_save_invalid_address():
229+
with tempfile.NamedTemporaryFile() as f:
230+
SK.save(f.name)
231+
with pytest.raises(IOError):
232+
VK.save(f.name)
233+
234+
161235
def test_stake_pool_key_save():
162236
with tempfile.NamedTemporaryFile() as skf, tempfile.NamedTemporaryFile() as vkf:
163237
SPSK.save(skf.name)

0 commit comments

Comments
 (0)