-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_ward.py
More file actions
1174 lines (981 loc) · 42.1 KB
/
test_ward.py
File metadata and controls
1174 lines (981 loc) · 42.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
"""
Ward Protocol SDK — pytest test suite
======================================
Unit tests: No XRPL network required (all XRPL calls are mocked).
Integration tests: Marked @pytest.mark.integration — hit XRPL testnet.
Adversarial tests: Simulate real attack scenarios against the validator.
Run unit tests only:
pytest test_ward.py -v -m "not integration"
Run all tests (requires testnet access):
pytest test_ward.py -v
"""
from __future__ import annotations
import asyncio
import hashlib
import json
import os
import time
from dataclasses import dataclass
from typing import Any, Dict, List, Optional
from unittest.mock import AsyncMock, MagicMock, patch
import pytest
from ward_client import (
PREIMAGE_BYTES,
RATE_LIMIT_ATTEMPTS,
RATE_LIMIT_WINDOW_S,
TF_BURNABLE,
WARD_POLICY_TAXON,
ClaimValidator,
EscrowRecord,
EscrowSettlement,
LedgerError,
PoolHealth,
PoolHealthMonitor,
SecurityError,
ValidationError,
ValidationResult,
VaultMonitor,
WardClient,
WardError,
calculate_coverage_ratio,
extract_nft_id,
generate_claim_condition,
get_ledger_time,
make_preimage_condition,
validate_drops_amount,
validate_nft_id,
validate_xrpl_address,
)
# ---------------------------------------------------------------------------
# Fixtures
# ---------------------------------------------------------------------------
VALID_ADDRESS = "rHb9CJAWyB4rj91VRWn96DkukG4bwdtyTh"
VALID_ADDRESS2 = "rPT1Sjq2YGrBMTttX4GZHjKu9dyfzbpAYe"
VALID_NFT_ID = "A" * 64 # valid 64-hex-char string
VALID_LOAN_ID = "B" * 64
POLICY_TAXON = WARD_POLICY_TAXON
@dataclass
class FakeWallet:
classic_address: str = VALID_ADDRESS
seed: str = "sEdTM1uX8pu2do5XvTnutH6HsouMaM2"
def _make_mock_client():
mock = AsyncMock()
mock.request = AsyncMock()
return mock
def _make_success_response(result_data: dict):
resp = MagicMock()
resp.is_successful.return_value = True
resp.result = result_data
return resp
def _make_fail_response(engine_result: str = "tecFAILED"):
resp = MagicMock()
resp.is_successful.return_value = False
resp.result = {"meta": {"TransactionResult": engine_result}, "error": engine_result}
return resp
def _make_nft_entry(nft_token_id: str, uri_metadata: dict, taxon: int = WARD_POLICY_TAXON):
uri_hex = json.dumps(uri_metadata, separators=(",", ":")).encode().hex().upper()
return {
"NFTokenID": nft_token_id,
"NFTokenTaxon": taxon,
"URI": uri_hex,
}
def _make_policy_metadata(
vault_address: str = VALID_ADDRESS,
coverage_drops: int = 1_000_000,
expiry_ledger_time: int = 9_999_999_999,
) -> dict:
# Mirrors the compact URI format used by WardClient.purchase_coverage
return {
"protocol": "ward-v1",
"vault_address": vault_address,
"coverage_drops": str(coverage_drops),
"expiry_ledger_time": expiry_ledger_time,
"pool_address": VALID_ADDRESS2,
}
def _make_loan_node(
flags: int = 0x00010000, # lsfLoanDefault
principal: int = 500_000,
interest: int = 10_000,
loan_broker_id: str = "E" * 64,
) -> dict:
return {
"Flags": flags,
"PrincipalOutstanding": principal,
"InterestOutstanding": interest,
"TotalValueOutstanding": principal + interest,
"LoanBrokerID": loan_broker_id,
}
def _make_broker_node(
debt_total: int = 1_000_000,
cover_available: int = 500_000,
cover_rate_minimum: float = 0.10,
cover_rate_liquidation: float = 0.50,
) -> dict:
return {
"DebtTotal": debt_total,
"CoverAvailable": cover_available,
"CoverRateMinimum": cover_rate_minimum,
"CoverRateLiquidation": cover_rate_liquidation,
}
def _make_vault_node(
assets_total: int = 500_000,
assets_available: int = 200_000,
loss_unrealized: int = 0,
shares_total: int = 1_000,
) -> dict:
return {
"AssetsTotal": assets_total,
"AssetsAvailable": assets_available,
"LossUnrealized": loss_unrealized,
"SharesTotal": shares_total,
}
def _make_server_info_response(close_time: int = 100_000_000) -> dict:
return {
"info": {
"validated_ledger": {
"close_time": close_time,
"seq": 12_345_678,
}
}
}
# ===========================================================================
# Tests: Security utilities
# ===========================================================================
class TestValidateXrplAddress:
def test_valid_address(self):
validate_xrpl_address(VALID_ADDRESS) # no exception
def test_valid_address2(self):
validate_xrpl_address(VALID_ADDRESS2)
def test_empty_string_raises(self):
with pytest.raises(ValidationError):
validate_xrpl_address("")
def test_none_raises(self):
with pytest.raises(ValidationError):
validate_xrpl_address(None) # type: ignore
def test_too_short_raises(self):
with pytest.raises(ValidationError):
validate_xrpl_address("rShort")
def test_wrong_prefix_raises(self):
with pytest.raises(ValidationError):
validate_xrpl_address("xrpL" + "A" * 30)
def test_invalid_checksum_raises(self):
# Valid format but wrong checksum
with pytest.raises(ValidationError):
validate_xrpl_address("rHb9CJAWyB4rj91VRWn96DkukG4bwdtyXX")
def test_example_vault_fails(self):
"""Original prototype passed 'rExampleVaultXXX' — must be rejected."""
with pytest.raises(ValidationError):
validate_xrpl_address("rExampleVaultXXX")
class TestValidateDropsAmount:
def test_valid_drops(self):
validate_drops_amount(1_000_000) # 1 XRP
def test_one_drop(self):
validate_drops_amount(1)
def test_zero_raises(self):
with pytest.raises(ValidationError):
validate_drops_amount(0)
def test_negative_raises(self):
with pytest.raises(ValidationError):
validate_drops_amount(-1)
def test_exceeds_max_raises(self):
with pytest.raises(ValidationError):
validate_drops_amount(100_000_000_000_000_001)
def test_float_raises(self):
with pytest.raises(ValidationError):
validate_drops_amount(1.5) # type: ignore
class TestValidateNftId:
def test_valid_64_hex(self):
validate_nft_id("A" * 64)
def test_mixed_case_valid(self):
validate_nft_id("aAbBcCdD" * 8)
def test_63_chars_raises(self):
with pytest.raises(ValidationError):
validate_nft_id("A" * 63)
def test_65_chars_raises(self):
with pytest.raises(ValidationError):
validate_nft_id("A" * 65)
def test_non_hex_raises(self):
with pytest.raises(ValidationError):
validate_nft_id("G" * 64)
def test_empty_raises(self):
with pytest.raises(ValidationError):
validate_nft_id("")
class TestCoverageRatio:
def test_basic_ratio(self):
ratio = calculate_coverage_ratio(2_000_000, 1_000_000)
assert ratio == pytest.approx(2.0)
def test_zero_loans_is_inf(self):
assert calculate_coverage_ratio(1_000_000, 0) == float("inf")
def test_ratio_below_min_raises(self):
with pytest.raises(ValidationError, match="below minimum"):
calculate_coverage_ratio(1_500_000, 1_000_000) # 1.5x < 2.0x
def test_ratio_exactly_at_min(self):
ratio = calculate_coverage_ratio(2_000_000, 1_000_000)
assert ratio == pytest.approx(2.0)
def test_high_ratio(self):
ratio = calculate_coverage_ratio(10_000_000, 1_000_000)
assert ratio == pytest.approx(10.0)
class TestMakePreimageCondition:
def test_output_lengths(self):
preimage = os.urandom(PREIMAGE_BYTES)
cond, fulf = make_preimage_condition(preimage)
# Condition = A0 25 + 37 bytes → 39 bytes → 78 hex chars
assert len(cond) == 78
# Fulfillment = A0 22 + 34 bytes → 36 bytes → 72 hex chars
assert len(fulf) == 72
def test_condition_starts_with_a025(self):
preimage = os.urandom(PREIMAGE_BYTES)
cond, _ = make_preimage_condition(preimage)
assert cond.upper().startswith("A025")
def test_fulfillment_starts_with_a022(self):
preimage = os.urandom(PREIMAGE_BYTES)
_, fulf = make_preimage_condition(preimage)
assert fulf.upper().startswith("A022")
def test_condition_contains_sha256_of_preimage(self):
preimage = bytes(range(32))
sha256_hex = hashlib.sha256(preimage).hexdigest().upper()
cond, _ = make_preimage_condition(preimage)
# Condition inner: 8020 + sha256_hash
assert sha256_hex in cond.upper()
def test_different_preimages_give_different_conditions(self):
p1 = os.urandom(PREIMAGE_BYTES)
p2 = os.urandom(PREIMAGE_BYTES)
c1, _ = make_preimage_condition(p1)
c2, _ = make_preimage_condition(p2)
assert c1 != c2
def test_wrong_length_raises(self):
with pytest.raises(ValueError):
make_preimage_condition(b"\x00" * 16)
def test_generate_claim_condition_returns_three_items(self):
preimage, cond, fulf = generate_claim_condition()
assert len(preimage) == PREIMAGE_BYTES
assert len(cond) == 78
assert len(fulf) == 72
class TestExtractNftId:
def test_meta_nftoken_id_shortcut(self):
"""Newer rippled exposes nftoken_id directly in meta."""
response = MagicMock()
response.result = {"meta": {"nftoken_id": "ab" * 32}}
nft_id = extract_nft_id(response)
assert nft_id == ("AB" * 32).upper()
def test_created_node_fallback(self):
"""Older nodes: parse from CreatedNode NFTokenPage."""
nft_id_hex = "CC" * 32
response = MagicMock()
response.result = {
"meta": {
"AffectedNodes": [
{
"CreatedNode": {
"LedgerEntryType": "NFTokenPage",
"NewFields": {
"NFTokens": [
{"NFToken": {"NFTokenID": nft_id_hex}}
]
},
}
}
]
}
}
nft_id = extract_nft_id(response)
assert nft_id == nft_id_hex.upper()
def test_modified_node_fallback(self):
"""Older nodes: parse from ModifiedNode NFTokenPage diff."""
old_id = "AA" * 32
new_id = "BB" * 32
response = MagicMock()
response.result = {
"meta": {
"AffectedNodes": [
{
"ModifiedNode": {
"LedgerEntryType": "NFTokenPage",
"PreviousFields": {
"NFTokens": [{"NFToken": {"NFTokenID": old_id}}]
},
"FinalFields": {
"NFTokens": [
{"NFToken": {"NFTokenID": old_id}},
{"NFToken": {"NFTokenID": new_id}},
]
},
}
}
]
}
}
nft_id = extract_nft_id(response)
assert nft_id == new_id.upper()
def test_no_nft_id_raises(self):
response = MagicMock()
response.result = {"meta": {"AffectedNodes": []}}
with pytest.raises(LedgerError):
extract_nft_id(response)
def test_original_bug_phantom_nftoken_entry_raises(self):
"""
Bug fix [3]: original code looked for LedgerEntryType == 'NFToken'
which doesn't exist — ensure we raise instead of silently returning None.
"""
response = MagicMock()
response.result = {
"meta": {
"AffectedNodes": [
{
"CreatedNode": {
"LedgerEntryType": "NFToken", # phantom type
"NewFields": {"NFTokenID": "AA" * 32},
}
}
]
}
}
with pytest.raises(LedgerError):
extract_nft_id(response)
# ===========================================================================
# Tests: WardClient — purchase_coverage
# ===========================================================================
class TestWardClientInputValidation:
"""Module 1 — input validation before any network call."""
def setup_method(self):
self.client = WardClient()
self.wallet = FakeWallet()
@pytest.mark.asyncio
async def test_invalid_vault_address_raises(self):
with pytest.raises(ValidationError, match="vault_address"):
await self.client.purchase_coverage(
wallet=self.wallet,
vault_address="rInvalid",
coverage_drops=1_000_000,
period_days=30,
pool_address=VALID_ADDRESS2,
)
@pytest.mark.asyncio
async def test_invalid_pool_address_raises(self):
with pytest.raises(ValidationError, match="pool_address"):
await self.client.purchase_coverage(
wallet=self.wallet,
vault_address=VALID_ADDRESS,
coverage_drops=1_000_000,
period_days=30,
pool_address="not-an-address",
)
@pytest.mark.asyncio
async def test_zero_coverage_raises(self):
with pytest.raises(ValidationError, match="coverage_drops"):
await self.client.purchase_coverage(
wallet=self.wallet,
vault_address=VALID_ADDRESS,
coverage_drops=0,
period_days=30,
pool_address=VALID_ADDRESS2,
)
@pytest.mark.asyncio
async def test_negative_period_raises(self):
with pytest.raises(ValidationError):
await self.client.purchase_coverage(
wallet=self.wallet,
vault_address=VALID_ADDRESS,
coverage_drops=1_000_000,
period_days=-1,
pool_address=VALID_ADDRESS2,
)
@pytest.mark.asyncio
async def test_premium_rate_above_1_raises(self):
with pytest.raises(ValidationError, match="premium_rate"):
await self.client.purchase_coverage(
wallet=self.wallet,
vault_address=VALID_ADDRESS,
coverage_drops=1_000_000,
period_days=30,
pool_address=VALID_ADDRESS2,
premium_rate=1.5,
)
@pytest.mark.asyncio
async def test_example_vault_address_from_prototype_rejected(self):
"""Bug fix [6]: 'rExampleVaultXXX' must be rejected before any network call."""
with pytest.raises(ValidationError):
await self.client.purchase_coverage(
wallet=self.wallet,
vault_address="rExampleVaultXXX",
coverage_drops=1_000_000,
period_days=90,
pool_address="rPoolAddressXXX",
)
def test_nft_flag_constant_is_burnable_not_transferable(self):
"""
Security: TF_BURNABLE must be 0x1 (tfBurnable), NOT 0x8 (tfTransferable).
Original prototype bug: flags=8 (tfTransferable) allowed policies to be sold.
Correct value: flags=1 (tfBurnable only) — policy is non-transferable.
"""
TF_TRANSFERABLE = 0x00000008
assert TF_BURNABLE == 0x00000001, f"TF_BURNABLE must be 0x1, got {TF_BURNABLE:#x}"
assert TF_BURNABLE & TF_TRANSFERABLE == 0, (
"TF_BURNABLE must NOT have the tfTransferable bit set"
)
@pytest.mark.asyncio
async def test_nft_mint_uses_burnable_flag(self):
"""Verify NFTokenMint is constructed with flags=TF_BURNABLE (0x1) not 8."""
from xrpl.models import NFTokenMint as _NM
captured_txs: list = []
async def mock_submit(tx, client, wallet, **kwargs):
captured_txs.append(tx)
resp = MagicMock()
resp.is_successful.return_value = True
resp.result = {
"hash": "D" * 64,
"meta": {"TransactionResult": "tesSUCCESS", "nftoken_id": "F" * 64},
"Sequence": 1,
}
return resp
async def mock_autofill(tx, client):
return tx
with (
patch("ward_client.submit_and_wait", side_effect=mock_submit),
patch("ward_client.autofill", side_effect=mock_autofill),
patch("ward_client.get_ledger_time", new=AsyncMock(return_value=100_000_000)),
):
ward = WardClient()
result = await ward.purchase_coverage(
wallet=FakeWallet(),
vault_address=VALID_ADDRESS,
coverage_drops=1_000_000,
period_days=30,
pool_address=VALID_ADDRESS2,
)
assert result["status"] == "active"
mint_txs = [t for t in captured_txs if isinstance(t, _NM)]
assert mint_txs, "No NFTokenMint transaction was built"
assert mint_txs[0].flags == TF_BURNABLE, (
f"NFT flags={mint_txs[0].flags:#x}, expected {TF_BURNABLE:#x} (tfBurnable). "
"tfTransferable (0x8) must NOT be set."
)
# ===========================================================================
# Tests: ClaimValidator — 9-step adversarial validation
# ===========================================================================
class TestClaimValidatorInputSanitation:
def setup_method(self):
self.validator = ClaimValidator()
@pytest.mark.asyncio
async def test_invalid_claimant_address(self):
result = await self.validator.validate_claim(
claimant_address="bad-addr",
nft_token_id=VALID_NFT_ID,
defaulted_vault=VALID_ADDRESS,
loan_id=VALID_LOAN_ID,
pool_address=VALID_ADDRESS2,
)
assert not result.approved
assert result.steps_passed == 0
@pytest.mark.asyncio
async def test_invalid_nft_id(self):
result = await self.validator.validate_claim(
claimant_address=VALID_ADDRESS,
nft_token_id="not-64-hex",
defaulted_vault=VALID_ADDRESS,
loan_id=VALID_LOAN_ID,
pool_address=VALID_ADDRESS2,
)
assert not result.approved
assert result.steps_passed == 0
class TestClaimValidatorAdversarial:
"""Adversarial test cases against the 9-step validator."""
def _make_validator_with_mocks(
self,
*,
nft_exists: bool = True,
nft_taxon: int = WARD_POLICY_TAXON,
ledger_time: int = 100_000_000,
expiry_time: int = 999_999_999,
policy_vault: str = VALID_ADDRESS,
defaulted_vault: str = VALID_ADDRESS,
default_flag_set: bool = True,
vault_loss_drops: int = 100_000,
pool_balance_drops: int = 10_000_000,
loan_broker_available: bool = True,
coverage_drops: int = 500_000,
) -> ClaimValidator:
validator = ClaimValidator()
metadata = _make_policy_metadata(
vault_address=policy_vault,
coverage_drops=coverage_drops,
expiry_ledger_time=expiry_time,
)
uri_hex = json.dumps(metadata, separators=(",", ":")).encode().hex().upper()
nft_entry = {
"NFTokenID": VALID_NFT_ID,
"NFTokenTaxon": nft_taxon,
"URI": uri_hex,
}
loan_flags = 0x00010000 if default_flag_set else 0x0
loan_node = _make_loan_node(
flags=loan_flags,
principal=500_000,
interest=10_000,
)
broker_node = _make_broker_node(
debt_total=1_000_000,
cover_available=100_000,
)
# Vault with TVL < 2x outstanding loans → coverage breach
vault_node = _make_vault_node(
assets_total=400_000,
loss_unrealized=0,
)
pool_info = {"account_data": {"Balance": str(pool_balance_drops)}}
async def mock_request(req):
from xrpl.models import AccountNFTs as _ANFTs, AccountInfo as _AI
from xrpl.models import ServerInfo as _SI, LedgerEntry as _LE
if isinstance(req, _ANFTs):
nfts = [nft_entry] if nft_exists else []
return _make_success_response({"account_nfts": nfts})
elif isinstance(req, _SI):
return _make_success_response(_make_server_info_response(ledger_time))
elif isinstance(req, _LE):
# ward_client uses LedgerEntry(index=...) for loan/loan_broker objects
# and LedgerEntry(vault=...) for vault objects
index_val = getattr(req, "index", None)
vault_val = getattr(req, "vault", None)
if index_val == VALID_LOAN_ID:
# Loan lookup
if not default_flag_set:
return _make_fail_response()
return _make_success_response({"node": loan_node})
elif index_val is not None and index_val != VALID_LOAN_ID:
# LoanBroker lookup (any other index)
if not loan_broker_available:
return _make_fail_response()
return _make_success_response({"node": broker_node})
elif vault_val is not None:
return _make_success_response({"node": vault_node})
return _make_fail_response()
elif isinstance(req, _AI):
return _make_success_response(pool_info)
return _make_fail_response()
validator._client = MagicMock()
validator._client.request = AsyncMock(side_effect=mock_request)
return validator
# ── Adversarial Test 1: Fake claim — NFT does not exist ──────────
@pytest.mark.asyncio
async def test_fake_claim_nft_not_found(self):
"""
Attack: Attacker tries to claim without owning a valid policy NFT.
Mitigation: Step 1 verifies NFT existence on-chain.
"""
validator = self._make_validator_with_mocks(nft_exists=False)
result = await validator.validate_claim(
claimant_address=VALID_ADDRESS,
nft_token_id=VALID_NFT_ID,
defaulted_vault=VALID_ADDRESS,
loan_id=VALID_LOAN_ID,
pool_address=VALID_ADDRESS2,
)
assert not result.approved
assert "not found" in result.rejection_reason.lower()
# ── Adversarial Test 2: Expired policy ───────────────────────────
@pytest.mark.asyncio
async def test_expired_policy_rejected(self):
"""
Attack: Claimant uses an expired policy.
Mitigation: Step 2 checks XRPL ledger time, not local clock.
"""
validator = self._make_validator_with_mocks(
ledger_time=200_000_000,
expiry_time=100_000_000, # expired
)
result = await validator.validate_claim(
claimant_address=VALID_ADDRESS,
nft_token_id=VALID_NFT_ID,
defaulted_vault=VALID_ADDRESS,
loan_id=VALID_LOAN_ID,
pool_address=VALID_ADDRESS2,
)
assert not result.approved
assert "expired" in result.rejection_reason.lower()
# ── Adversarial Test 3: Wrong vault in claim ──────────────────────
@pytest.mark.asyncio
async def test_wrong_vault_rejected(self):
"""
Attack: Claimant uses a policy for Vault A to claim against Vault B.
Mitigation: Step 3 checks vault_address in NFT metadata.
"""
validator = self._make_validator_with_mocks(
policy_vault=VALID_ADDRESS,
defaulted_vault=VALID_ADDRESS2, # different vault
)
result = await validator.validate_claim(
claimant_address=VALID_ADDRESS,
nft_token_id=VALID_NFT_ID,
defaulted_vault=VALID_ADDRESS2, # trying to claim against wrong vault
loan_id=VALID_LOAN_ID,
pool_address=VALID_ADDRESS2,
)
assert not result.approved
assert "mismatch" in result.rejection_reason.lower()
# ── Adversarial Test 4: No default flag on loan ───────────────────
@pytest.mark.asyncio
async def test_no_default_flag_rejected(self):
"""
Attack: Claimant tries to claim when no actual default occurred.
Mitigation: Step 4 reads lsfLoanDefault flag directly from ledger object.
"""
validator = self._make_validator_with_mocks(default_flag_set=False)
result = await validator.validate_claim(
claimant_address=VALID_ADDRESS,
nft_token_id=VALID_NFT_ID,
defaulted_vault=VALID_ADDRESS,
loan_id=VALID_LOAN_ID,
pool_address=VALID_ADDRESS2,
)
assert not result.approved
assert "lsfLoanDefault" in result.rejection_reason or "not found" in result.rejection_reason.lower()
# ── Adversarial Test 5: Pool insolvent ────────────────────────────
@pytest.mark.asyncio
async def test_pool_insolvent_rejected(self):
"""
Attack: Claim submitted when pool is drained / undercollateralized.
Mitigation: Step 9 checks pool balance on-chain.
"""
validator = self._make_validator_with_mocks(
pool_balance_drops=100, # near-zero balance
coverage_drops=10_000_000,
)
result = await validator.validate_claim(
claimant_address=VALID_ADDRESS,
nft_token_id=VALID_NFT_ID,
defaulted_vault=VALID_ADDRESS,
loan_id=VALID_LOAN_ID,
pool_address=VALID_ADDRESS2,
)
assert not result.approved
assert "insolvent" in result.rejection_reason.lower()
# ── Adversarial Test 6: Wrong NFT taxon ──────────────────────────
@pytest.mark.asyncio
async def test_wrong_taxon_rejected(self):
"""
Attack: Attacker mints an NFT with fake policy data but wrong taxon.
Mitigation: Step 1 verifies NFTokenTaxon == WARD_POLICY_TAXON.
"""
validator = self._make_validator_with_mocks(nft_taxon=9999)
result = await validator.validate_claim(
claimant_address=VALID_ADDRESS,
nft_token_id=VALID_NFT_ID,
defaulted_vault=VALID_ADDRESS,
loan_id=VALID_LOAN_ID,
pool_address=VALID_ADDRESS2,
)
assert not result.approved
# ── Adversarial Test 7: Replay attack (NFT already burned) ────────
@pytest.mark.asyncio
async def test_replay_attack_burned_nft_rejected(self):
"""
Attack: Claimant tries to file a second claim after NFT was burned.
Mitigation: Step 1 — burned NFT is no longer in account_nfts → rejected.
This test simulates the post-burn state (nft_exists=False).
"""
validator = self._make_validator_with_mocks(nft_exists=False)
result = await validator.validate_claim(
claimant_address=VALID_ADDRESS,
nft_token_id=VALID_NFT_ID,
defaulted_vault=VALID_ADDRESS,
loan_id=VALID_LOAN_ID,
pool_address=VALID_ADDRESS2,
)
assert not result.approved
# Must explicitly call out the replay attempt
assert "burned" in result.rejection_reason.lower() or "not found" in result.rejection_reason.lower()
# ── Adversarial Test 8: Rate limit / rapid claim attempts ─────────
@pytest.mark.asyncio
async def test_rate_limit_exceeded(self):
"""
Attack: Spamming claim submissions for the same NFT.
Mitigation: In-memory rate limiter caps RATE_LIMIT_ATTEMPTS attempts.
"""
validator = self._make_validator_with_mocks(nft_exists=False)
# Exhaust the rate limit
for _ in range(RATE_LIMIT_ATTEMPTS):
await validator.validate_claim(
claimant_address=VALID_ADDRESS,
nft_token_id=VALID_NFT_ID,
defaulted_vault=VALID_ADDRESS,
loan_id=VALID_LOAN_ID,
pool_address=VALID_ADDRESS2,
)
# Next attempt should be rate-limited
result = await validator.validate_claim(
claimant_address=VALID_ADDRESS,
nft_token_id=VALID_NFT_ID,
defaulted_vault=VALID_ADDRESS,
loan_id=VALID_LOAN_ID,
pool_address=VALID_ADDRESS2,
)
assert not result.approved
assert "rate limit" in result.rejection_reason.lower()
# ── Happy path ─────────────────────────────────────────────────────
@pytest.mark.asyncio
async def test_valid_claim_approved(self):
"""Happy path: all 9 steps pass → claim approved."""
validator = self._make_validator_with_mocks(
nft_exists=True,
pool_balance_drops=50_000_000,
coverage_drops=100_000,
)
result = await validator.validate_claim(
claimant_address=VALID_ADDRESS,
nft_token_id=VALID_NFT_ID,
defaulted_vault=VALID_ADDRESS,
loan_id=VALID_LOAN_ID,
pool_address=VALID_ADDRESS2,
)
assert result.approved
assert result.claim_payout_drops > 0
assert result.steps_passed == 9
# ===========================================================================
# Tests: make_preimage_condition — cryptographic correctness
# ===========================================================================
class TestPreimageConditionCryptography:
def test_roundtrip_condition_matches_fulfillment(self):
"""
Verify the condition encodes SHA-256(preimage) as its fingerprint.
This ensures the XRPL escrow can validate the fulfillment.
"""
preimage = bytes(range(32))
sha256 = hashlib.sha256(preimage).digest()
cond, fulf = make_preimage_condition(preimage)
cond_bytes = bytes.fromhex(cond)
fulf_bytes = bytes.fromhex(fulf)
# Condition structure: A0 25 80 20 <sha256> 81 01 20
assert cond_bytes[0] == 0xA0 # PREIMAGE-SHA-256 tag
assert cond_bytes[1] == 0x25 # inner length = 37
assert cond_bytes[2] == 0x80 # fingerprint field tag
assert cond_bytes[3] == 0x20 # fingerprint length = 32
assert cond_bytes[4:36] == sha256 # fingerprint = sha256(preimage)
assert cond_bytes[36] == 0x81 # cost field tag
assert cond_bytes[37] == 0x01 # cost length = 1
assert cond_bytes[38] == 0x20 # cost = 32
# Fulfillment structure: A0 22 80 20 <preimage>
assert fulf_bytes[0] == 0xA0 # PREIMAGE-SHA-256 tag
assert fulf_bytes[1] == 0x22 # inner length = 34
assert fulf_bytes[2] == 0x80 # preimage field tag
assert fulf_bytes[3] == 0x20 # preimage length = 32
assert fulf_bytes[4:36] == preimage # preimage itself
def test_unique_conditions_per_preimage(self):
"""Different preimages must produce different conditions (no collision)."""
conditions = set()
for _ in range(100):
p = os.urandom(PREIMAGE_BYTES)
c, _ = make_preimage_condition(p)
conditions.add(c)
assert len(conditions) == 100, "Hash collision detected — critical bug"
# ===========================================================================
# Tests: VaultMonitor — anomaly detection
# ===========================================================================
class TestVaultMonitorAnomalyDetection:
def test_anomaly_triggered_after_threshold(self):
monitor = VaultMonitor(vault_addresses=[VALID_ADDRESS])
vault = VALID_ADDRESS
# Inject ANOMALY_THRESHOLD - 1 signals — should not trigger
from ward_client import ANOMALY_THRESHOLD, ANOMALY_WINDOW_SECONDS
for i in range(ANOMALY_THRESHOLD - 1):
result = monitor._detect_anomaly(vault)
assert not result, "Anomaly should not trigger below threshold"
# One more — now at threshold — should trigger
result = monitor._detect_anomaly(vault)
assert result, "Anomaly should trigger at threshold"
def test_anomaly_window_expires(self):
from ward_client import ANOMALY_THRESHOLD
monitor = VaultMonitor(vault_addresses=[VALID_ADDRESS])
vault = VALID_ADDRESS
# Fill the window
now = time.time()
for _ in range(ANOMALY_THRESHOLD):
monitor._recent_signals[vault].append(now - 9999) # expired timestamps
# Fresh signal — window should have cleared old entries
result = monitor._detect_anomaly(vault)
assert not result, "Old signals should not count after window expires"
def test_invalid_vault_address_rejected(self):
with pytest.raises(ValidationError):
VaultMonitor(vault_addresses=["rInvalidXXXXXXXX"])
def test_add_vault_validates_address(self):
monitor = VaultMonitor(vault_addresses=[VALID_ADDRESS])
with pytest.raises(ValidationError):
monitor.add_vault("not-a-valid-xrpl-address")
# ===========================================================================
# Tests: PoolHealthMonitor — solvency and dynamic pricing
# ===========================================================================
class TestPoolHealthMonitor:
def _make_monitor(self, balance_drops: int = 10_000_000) -> PoolHealthMonitor:
monitor = PoolHealthMonitor(pool_address=VALID_ADDRESS)
async def mock_request(req):
return _make_success_response(
{"account_data": {"Balance": str(balance_drops)}}
)
monitor._client = MagicMock()
monitor._client.request = AsyncMock(side_effect=mock_request)
return monitor
@pytest.mark.asyncio
async def test_solvent_pool(self):
monitor = self._make_monitor(balance_drops=20_000_000 + 4_000_000)
# 4 XRP usable vs 1 XRP coverage = 4x ratio → solvent
health = await monitor.get_health(active_coverage_drops=1_000_000)
assert health.is_solvent
@pytest.mark.asyncio
async def test_undercollateralized_pool(self):
monitor = self._make_monitor(balance_drops=20_000_000 + 1_000_000)
# 1 XRP usable vs 10 XRP coverage = 0.1x ratio → insolvent
health = await monitor.get_health(active_coverage_drops=10_000_000)
assert not health.is_solvent
@pytest.mark.asyncio
async def test_minting_blocked_when_insolvent(self):
monitor = self._make_monitor(balance_drops=20_000_000 + 100)
health = await monitor.get_health(active_coverage_drops=10_000_000)
assert not monitor.is_minting_allowed(health)
@pytest.mark.asyncio
async def test_dynamic_premium_higher_when_undercollateralized(self):
monitor_safe = self._make_monitor(balance_drops=20_000_000 + 100_000_000)
monitor_stressed = self._make_monitor(balance_drops=20_000_000 + 3_000_000)
health_safe = await monitor_safe.get_health(active_coverage_drops=1_000_000)