Skip to content

Commit 62a861f

Browse files
unit tests: update and add more data model tests
1 parent a233e58 commit 62a861f

5 files changed

Lines changed: 147 additions & 100 deletions

File tree

‎tests/test_monero_common.py‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,6 @@ def test_serializable_struct(self) -> None:
3737
SerializableStruct()
3838

3939
# test ssl options serialization integrity
40-
@pytest.mark.xfail(reason="TODO monero-cpp implement ssl_options::from_property_tree()", strict=True)
4140
def test_ssl_options(self) -> None:
4241
# create ssl_options objects and populate properties
4342
ssl_options: SslOptions = SslOptions()

‎tests/test_monero_daemon_model.py‎

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,38 @@ def test_block_template_deserialize(self) -> None:
135135
template.seed_height = 0
136136
AssertUtils.assert_serialization_integrity(template)
137137

138+
def test_block_header_deserialize(self) -> None:
139+
header: MoneroBlockHeader = MoneroBlockHeader()
140+
header.hash = "a" * 64
141+
header.height = 12345
142+
header.timestamp = 1600000000
143+
header.size = 2048
144+
header.weight = 2048
145+
header.long_term_weight = 2048
146+
header.depth = 10
147+
header.difficulty_low = 5000
148+
header.difficulty_high = 0
149+
header.cumulative_difficulty_low = 900000
150+
header.cumulative_difficulty_high = 0
151+
header.major_version = 16
152+
header.minor_version = 16
153+
header.nonce = 42
154+
header.miner_tx_hash = "b" * 64
155+
header.num_txs = 3
156+
header.orphan_status = False
157+
header.prev_hash = "c" * 64
158+
header.reward = 600000000000
159+
header.pow_hash = "d" * 64
160+
AssertUtils.assert_serialization_integrity(header)
161+
162+
def test_block_deserialize(self) -> None:
163+
block: MoneroBlock = MoneroBlock()
164+
block.hash = "a" * 64
165+
block.height = 12345
166+
block.hex = "deadbeef"
167+
block.tx_hashes = ["b" * 64, "c" * 64]
168+
AssertUtils.assert_serialization_integrity(block)
169+
138170
def test_connection_span_deserialize(self) -> None:
139171
span: MoneroConnectionSpan = MoneroConnectionSpan()
140172
span.connection_id = "deadbeef"

‎tests/test_monero_utils.py‎

Lines changed: 31 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -566,27 +566,15 @@ def test_get_blocks_from_transfers_unconfirmed_placeholder(self) -> None:
566566
assert t1.tx.block is placeholder
567567
assert t2.tx.block is placeholder
568568

569-
@pytest.mark.xfail(reason="get_blocks_from_transfers() dereferences transfer.tx without a null check and segfaults the interpreter when it's unset", strict=True)
570-
def test_get_blocks_from_transfers_missing_tx_does_not_crash(self) -> None:
569+
def test_get_blocks_from_transfers_missing_tx_raises(self) -> None:
571570
# a transfer with no tx set is a legitimate, reachable state (it's just
572-
# never assigned), but get_blocks_from_transfers() used to dereference
573-
# transfer.tx unconditionally, causing a native segfault (SIGSEGV)
574-
# instead of raising a catchable Python exception. Run in an isolated
575-
# subprocess so a regression here only kills a throwaway process
576-
# instead of the whole test run; fixed upstream in the local
577-
# everoddandeven/monero-cpp checkout, pending a submodule bump.
578-
script: str = (
579-
"import monero\n"
580-
"t = monero.MoneroIncomingTransfer()\n"
581-
"t.amount = 500000\n"
582-
"monero.MoneroUtils.get_blocks_from_transfers([t])\n"
583-
)
584-
result: subprocess.CompletedProcess[str] = subprocess.run([sys.executable, "-c", script], capture_output=True, text=True, timeout=30)
585-
logger.debug(f"subprocess exit code: {result.returncode}, stderr: {result.stderr.strip()}")
586-
assert result.returncode == 0, (
587-
f"get_blocks_from_transfers() crashed the interpreter (exit code {result.returncode}) "
588-
"instead of raising a Python exception for a transfer with no tx set"
589-
)
571+
# never assigned); get_blocks_from_transfers() must raise a catchable
572+
# exception, not dereference transfer.tx and segfault
573+
transfer: MoneroIncomingTransfer = MoneroIncomingTransfer()
574+
transfer.amount = 500000
575+
with pytest.raises(RuntimeError) as exc_info:
576+
MoneroUtils.get_blocks_from_transfers([transfer])
577+
assert str(exc_info.value) == "Transfer has no tx"
590578

591579
def test_get_blocks_from_outputs_dedup_and_order(self) -> None:
592580
block1: MoneroBlock = MoneroBlock()
@@ -625,22 +613,14 @@ def test_get_blocks_from_outputs_unconfirmed_raises(self) -> None:
625613
with pytest.raises(RuntimeError, match="Need to handle unconfirmed output"):
626614
MoneroUtils.get_blocks_from_outputs([output])
627615

628-
@pytest.mark.xfail(reason="get_blocks_from_outputs() bug", strict=True)
629-
def test_get_blocks_from_outputs_missing_tx_does_not_crash(self) -> None:
630-
# same crash as get_blocks_from_transfers(), for the same reason:
631-
# output.tx is a legitimate but unchecked null before the cast/dereference.
632-
script: str = (
633-
"import monero\n"
634-
"o = monero.MoneroOutputWallet()\n"
635-
"o.amount = 1000000\n"
636-
"monero.MoneroUtils.get_blocks_from_outputs([o])\n"
637-
)
638-
result: subprocess.CompletedProcess[str] = subprocess.run([sys.executable, "-c", script], capture_output=True, text=True, timeout=30)
639-
logger.debug(f"subprocess exit code: {result.returncode}, stderr: {result.stderr.strip()}")
640-
assert result.returncode == 0, (
641-
f"get_blocks_from_outputs() crashed the interpreter (exit code {result.returncode}) "
642-
"instead of raising a Python exception for an output with no tx set"
643-
)
616+
def test_get_blocks_from_outputs_missing_tx_raises(self) -> None:
617+
# same as get_blocks_from_transfers(): output.tx is a legitimate but
618+
# unchecked null; must raise rather than dereference and segfault
619+
output: MoneroOutputWallet = MoneroOutputWallet()
620+
output.amount = 1000000
621+
with pytest.raises(RuntimeError) as exc_info:
622+
MoneroUtils.get_blocks_from_outputs([output])
623+
assert str(exc_info.value) == "Output has no tx"
644624

645625
#endregion
646626

@@ -791,34 +771,21 @@ def test_free_none_does_not_crash(self) -> None:
791771
"instead of raising a Python exception (or being a documented no-op)"
792772
)
793773

794-
@pytest.mark.xfail(reason="free(transfers) delegates to get_blocks_from_transfers(), which segfaults on a transfer with no tx set", strict=True)
795-
def test_free_transfers_missing_tx_does_not_crash(self) -> None:
796-
script: str = (
797-
"import monero\n"
798-
"t = monero.MoneroIncomingTransfer()\n"
799-
"t.amount = 500000\n"
800-
"monero.MoneroUtils.free([t])\n"
801-
)
802-
result: subprocess.CompletedProcess[str] = subprocess.run([sys.executable, "-c", script], capture_output=True, text=True, timeout=30)
803-
logger.debug(f"subprocess exit code: {result.returncode}, stderr: {result.stderr.strip()}")
804-
assert result.returncode == 0, (
805-
f"free([transfer]) crashed the interpreter (exit code {result.returncode}) "
806-
"instead of raising a Python exception for a transfer with no tx set"
807-
)
774+
def test_free_transfers_missing_tx_raises(self) -> None:
775+
# free([transfers]) delegates to get_blocks_from_transfers(): a transfer
776+
# with no tx must surface a catchable exception, not segfault
777+
transfer: MoneroIncomingTransfer = MoneroIncomingTransfer()
778+
transfer.amount = 500000
779+
with pytest.raises(RuntimeError) as exc_info:
780+
MoneroUtils.free([transfer])
781+
assert str(exc_info.value) == "Transfer has no tx"
808782

809-
@pytest.mark.xfail(reason="free(outputs) delegates to get_blocks_from_outputs(), which segfaults on an output with no tx set", strict=True)
810-
def test_free_outputs_missing_tx_does_not_crash(self) -> None:
811-
script: str = (
812-
"import monero\n"
813-
"o = monero.MoneroOutputWallet()\n"
814-
"o.amount = 1000000\n"
815-
"monero.MoneroUtils.free([o])\n"
816-
)
817-
result: subprocess.CompletedProcess[str] = subprocess.run([sys.executable, "-c", script], capture_output=True, text=True, timeout=30)
818-
logger.debug(f"subprocess exit code: {result.returncode}, stderr: {result.stderr.strip()}")
819-
assert result.returncode == 0, (
820-
f"free([output]) crashed the interpreter (exit code {result.returncode}) "
821-
"instead of raising a Python exception for an output with no tx set"
822-
)
783+
def test_free_outputs_missing_tx_raises(self) -> None:
784+
# free([outputs]) delegates to get_blocks_from_outputs(): same guarantee
785+
output: MoneroOutputWallet = MoneroOutputWallet()
786+
output.amount = 1000000
787+
with pytest.raises(RuntimeError) as exc_info:
788+
MoneroUtils.free([output])
789+
assert str(exc_info.value) == "Output has no tx"
823790

824791
#endregion

‎tests/test_monero_wallet_full.py‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -753,7 +753,6 @@ def test_move_to(self) -> None:
753753
wallet.close()
754754

755755
# Can export and import wallet files
756-
@pytest.mark.not_implemented
757756
def test_export_and_import_wallet_files(self) -> None:
758757
config: MoneroWalletConfig = MoneroWalletConfig()
759758
config.path = ""

‎tests/test_monero_wallet_model.py‎

Lines changed: 84 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,13 @@
88
MoneroWalletConfig, MoneroDestination, MoneroUtils,
99
MoneroTxConfig, MoneroSubaddress, MoneroAccount, MoneroTxWallet,
1010
MoneroOutputWallet, MoneroKeyImage, MoneroIntegratedAddress,
11-
MoneroKeyImageImportResult, MoneroMessageSignatureResult,
12-
MoneroMessageSignatureType, MoneroCheckTx, MoneroCheckReserve,
11+
MoneroKeyImageImportResult, MoneroKeyImageExportResult, MoneroMessageSignatureResult,
12+
MoneroMessageSignatureType, MoneroCheck, MoneroCheckTx, MoneroCheckReserve,
1313
MoneroMultisigInfo, MoneroMultisigInitResult, MoneroMultisigSignResult,
1414
MoneroAddressBookEntry, MoneroAccountTag, MoneroIncomingTransfer,
1515
MoneroOutgoingTransfer, IncomingTransferComparator, OutputComparator, MoneroTx,
16-
MoneroTxSet
16+
MoneroTxSet, MoneroSyncResult, MoneroDecodedAddress,
17+
MoneroAddressType, MoneroNetworkType
1718
)
1819
from utils import BaseTestClass, TestUtils, AssertUtils
1920

@@ -189,6 +190,11 @@ def test_wallet_config(self) -> None:
189190
logger.debug(f"Deserialized config: {deserialized_config.serialize()}")
190191
AssertUtils.assert_equals(config, deserialized_config)
191192

193+
@pytest.mark.parametrize("json_fragment", ['{"networkType":-1}', '{"networkType":3}'])
194+
def test_wallet_config_invalid_network_type(self, json_fragment: str) -> None:
195+
with pytest.raises(Exception, match="Invalid network type"):
196+
MoneroWalletConfig.deserialize(json_fragment)
197+
192198
def test_tx_config(self) -> None:
193199
config: MoneroTxConfig = MoneroTxConfig()
194200
config.set_address(TestUtils.ADDRESS)
@@ -507,6 +513,60 @@ def test_tx_set_signed_tx_hex_deserialize(self) -> None:
507513
tx_set.signed_tx_hex = "deadbeef"
508514
AssertUtils.assert_serialization_integrity(tx_set)
509515

516+
def test_sync_result_deserialize(self) -> None:
517+
result: MoneroSyncResult = MoneroSyncResult()
518+
result.num_blocks_fetched = 42
519+
result.received_money = True
520+
AssertUtils.assert_serialization_integrity(result)
521+
522+
def test_check_deserialize(self) -> None:
523+
check: MoneroCheck = MoneroCheck()
524+
check.is_good = True
525+
AssertUtils.assert_serialization_integrity(check)
526+
527+
def test_incoming_transfer_deserialize(self) -> None:
528+
transfer: MoneroIncomingTransfer = MoneroIncomingTransfer()
529+
transfer.amount = 500000
530+
transfer.account_index = 0
531+
transfer.subaddress_index = 1
532+
transfer.address = TestUtils.ADDRESS
533+
transfer.num_suggested_confirmations = 10
534+
AssertUtils.assert_serialization_integrity(transfer)
535+
536+
def test_outgoing_transfer_deserialize(self) -> None:
537+
transfer: MoneroOutgoingTransfer = MoneroOutgoingTransfer()
538+
transfer.amount = 500000
539+
transfer.account_index = 0
540+
transfer.subaddress_indices = [0, 1]
541+
transfer.addresses = [TestUtils.ADDRESS]
542+
transfer.destinations = [MoneroDestination(TestUtils.ADDRESS, 500000)]
543+
AssertUtils.assert_serialization_integrity(transfer)
544+
545+
def test_decoded_address_deserialize(self) -> None:
546+
address: MoneroDecodedAddress = MoneroDecodedAddress(
547+
TestUtils.ADDRESS, MoneroAddressType.PRIMARY_ADDRESS, MoneroNetworkType.MAINNET
548+
)
549+
AssertUtils.assert_serialization_integrity(address)
550+
551+
@pytest.mark.parametrize("json_fragment", [
552+
'{"networkType":-1}',
553+
'{"networkType":3}',
554+
'{"addressType":-1}',
555+
'{"addressType":3}',
556+
])
557+
def test_decoded_address_invalid_enum_range(self, json_fragment: str) -> None:
558+
with pytest.raises(Exception, match="Invalid (network|address) type"):
559+
MoneroDecodedAddress.deserialize(json_fragment)
560+
561+
def test_key_image_export_result_deserialize(self) -> None:
562+
result: MoneroKeyImageExportResult = MoneroKeyImageExportResult()
563+
result.offset = 3
564+
key_image: MoneroKeyImage = MoneroKeyImage()
565+
key_image.hex = "a" * 64
566+
key_image.signature = "b" * 128
567+
result.key_images = [key_image]
568+
AssertUtils.assert_serialization_integrity(result)
569+
510570
#endregion
511571

512572
#region Copy / merge / comparators
@@ -621,38 +681,28 @@ def test_outgoing_transfer_merge(self) -> None:
621681
assert a.subaddress_indices == [0]
622682
assert len(a.destinations) == 1
623683

624-
@pytest.mark.xfail(reason="monero_outgoing_transfer::merge() dereferences destination address/amount unconditionally", strict=True)
625684
def test_outgoing_transfer_merge_destinations_with_unset_fields(self) -> None:
626-
script: str = (
627-
"import monero, sys\n"
628-
# dirty the heap first: a clean freshly-started interpreter doesn't reliably
629-
# reproduce the crash, but a heap with realistic allocation churn (much closer to
630-
# a real test run or application) does, consistently
631-
"garbage = []\n"
632-
"for i in range(500):\n"
633-
" tx = monero.MoneroTxWallet()\n"
634-
" tx.hash = 'b' * 64 + str(i)\n"
635-
" tx.note = 'x' * (i % 200)\n"
636-
" garbage.append(tx)\n"
637-
"del garbage\n"
638-
"a = monero.MoneroOutgoingTransfer()\n"
639-
"a.amount = 500000\n"
640-
"a.account_index = 0\n"
641-
"a.destinations = [monero.MoneroDestination()]\n"
642-
"b = a.copy()\n"
643-
"b.destinations = [monero.MoneroDestination()]\n"
644-
"try:\n"
645-
" a.merge(b)\n"
646-
" sys.exit('merge() did not raise')\n"
647-
"except RuntimeError as e:\n"
648-
" sys.exit(0 if str(e) == 'Destination vectors are different' else f'wrong message: {e}')\n"
649-
)
650-
result: subprocess.CompletedProcess[str] = subprocess.run([sys.executable, "-c", script], capture_output=True, text=True, timeout=30)
651-
logger.debug(f"subprocess exit code: {result.returncode}, stderr: {result.stderr.strip()}")
652-
assert result.returncode == 0, (
653-
f"outgoing_transfer.merge() did not cleanly raise 'Destination vectors are different' "
654-
f"(exit code {result.returncode}): {result.stderr.strip()[-300:]}"
655-
)
685+
# destinations with unset address/amount are a reachable state; merge()
686+
# must compare them without dereferencing the empty optionals
687+
a: MoneroOutgoingTransfer = MoneroOutgoingTransfer()
688+
a.amount = 500000
689+
a.account_index = 0
690+
a.destinations = [MoneroDestination()]
691+
b: MoneroOutgoingTransfer = a.copy()
692+
b.destinations = [MoneroDestination()]
693+
a.merge(b) # equal (empty) destinations -> no error
694+
assert len(a.destinations) == 1
695+
696+
def test_outgoing_transfer_merge_conflicting_destinations_raises(self) -> None:
697+
a: MoneroOutgoingTransfer = MoneroOutgoingTransfer()
698+
a.amount = 500000
699+
a.account_index = 0
700+
a.destinations = [MoneroDestination(TestUtils.ADDRESS, 1)]
701+
b: MoneroOutgoingTransfer = a.copy()
702+
b.destinations = [MoneroDestination(TestUtils.ADDRESS, 2)]
703+
with pytest.raises(RuntimeError) as exc_info:
704+
a.merge(b)
705+
assert str(exc_info.value) == "Destination vectors are different"
656706

657707
def test_output_wallet_copy(self) -> None:
658708
output: MoneroOutputWallet = MoneroOutputWallet()

0 commit comments

Comments
 (0)