forked from StellarRouter/StellarRouter
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfull_flow_test.rs
More file actions
807 lines (720 loc) · 24.4 KB
/
Copy pathfull_flow_test.rs
File metadata and controls
807 lines (720 loc) · 24.4 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
//! Full transaction flow integration tests
//!
//! These tests verify end-to-end functionality of the router system
//! on Stellar testnet, including:
//! - Contract deployment
//! - Route registration and resolution
//! - Access control
//! - Middleware rate limiting
//! - Timelock operations
//! - Multicall batching
use integration_tests::{TestAccount, TestSuite};
#[test]
#[ignore] // Run with: cargo test --test integration -- --ignored
fn test_full_router_core_flow() {
println!("\n=== Testing Full Router Core Flow ===\n");
let fixture = TestSuite::setup().expect("Failed to set up test suite");
let core = fixture
.router_core
.as_ref()
.expect("Core contract not deployed");
let admin = &fixture.admin;
// Test 1: Register a route
println!("\n--- Test 1: Register Route ---");
let mock_contract_addr = TestAccount::generate()
.expect("Failed to generate mock address")
.address;
let result = core.invoke(
"register_route",
&[
"--caller",
&admin.address,
"--name",
"oracle",
"--address",
&mock_contract_addr,
"--metadata",
"null",
],
admin,
);
assert!(result.is_ok(), "Failed to register route: {:?}", result);
println!("✓ Route 'oracle' registered successfully");
// Test 2: Resolve the route
println!("\n--- Test 2: Resolve Route ---");
let resolved = core
.invoke("resolve", &["--name", "oracle"], admin)
.expect("Failed to resolve route");
assert!(
resolved.contains(&mock_contract_addr),
"Resolved address doesn't match"
);
println!("✓ Route resolved correctly: {}", resolved);
// Test 3: Check total routed counter
println!("\n--- Test 3: Check Total Routed ---");
let total = core
.invoke("total_routed", &[], admin)
.expect("Failed to get total routed");
assert!(total.contains("1"), "Total routed should be 1");
println!("✓ Total routed: {}", total);
// Test 4: Update route
println!("\n--- Test 4: Update Route ---");
let new_mock_addr = TestAccount::generate()
.expect("Failed to generate new mock address")
.address;
let result = core.invoke(
"update_route",
&[
"--caller",
&admin.address,
"--name",
"oracle",
"--new_address",
&new_mock_addr,
],
admin,
);
assert!(result.is_ok(), "Failed to update route: {:?}", result);
println!("✓ Route updated successfully");
// Test 5: Verify updated route
let resolved = core
.invoke("resolve", &["--name", "oracle"], admin)
.expect("Failed to resolve updated route");
assert!(
resolved.contains(&new_mock_addr),
"Updated address doesn't match"
);
println!("✓ Updated route resolved correctly");
// Test 6: Pause route
println!("\n--- Test 6: Pause Route ---");
let result = core.invoke(
"set_route_paused",
&[
"--caller",
&admin.address,
"--name",
"oracle",
"--paused",
"true",
],
admin,
);
assert!(result.is_ok(), "Failed to pause route: {:?}", result);
println!("✓ Route paused successfully");
// Test 7: Try to resolve paused route (should fail)
println!("\n--- Test 7: Resolve Paused Route (Should Fail) ---");
let result = core.try_invoke("resolve", &["--name", "oracle"], admin);
assert!(result.is_err(), "Resolving paused route should fail");
println!("✓ Paused route correctly rejected: {:?}", result.err());
// Test 8: Unpause route
println!("\n--- Test 8: Unpause Route ---");
core.invoke(
"set_route_paused",
&[
"--caller",
&admin.address,
"--name",
"oracle",
"--paused",
"false",
],
admin,
)
.expect("Failed to unpause route");
println!("✓ Route unpaused successfully");
// Test 9: Resolve unpaused route
let resolved = core
.invoke("resolve", &["--name", "oracle"], admin)
.expect("Failed to resolve unpaused route");
assert!(resolved.contains(&new_mock_addr));
println!("✓ Unpaused route resolved successfully");
// Test 10: Add alias
println!("\n--- Test 10: Add Alias ---");
core.invoke(
"add_alias",
&[
"--caller",
&admin.address,
"--existing_name",
"oracle",
"--alias_name",
"price_feed",
],
admin,
)
.expect("Failed to add alias");
println!("✓ Alias 'price_feed' added successfully");
// Test 11: Resolve via alias
println!("\n--- Test 11: Resolve Via Alias ---");
let resolved = core
.invoke("resolve", &["--name", "price_feed"], admin)
.expect("Failed to resolve via alias");
assert!(resolved.contains(&new_mock_addr));
println!("✓ Alias resolved correctly");
// Test 12: Remove route
println!("\n--- Test 12: Remove Route ---");
core.invoke(
"remove_route",
&["--caller", &admin.address, "--name", "oracle"],
admin,
)
.expect("Failed to remove route");
println!("✓ Route removed successfully");
// Test 13: Verify route is gone
let result = core.try_invoke("resolve", &["--name", "oracle"], admin);
assert!(result.is_err(), "Removed route should not resolve");
println!("✓ Removed route correctly not found");
println!("\n=== Full Router Core Flow Test PASSED ===\n");
}
#[test]
#[ignore]
fn test_router_registry_flow() {
println!("\n=== Testing Router Registry Flow ===\n");
let fixture = TestSuite::setup().expect("Failed to set up test suite");
let registry = fixture
.router_registry
.as_ref()
.expect("Registry not deployed");
let admin = &fixture.admin;
// Test 1: Register version 1
println!("\n--- Test 1: Register Version 1 ---");
let addr_v1 = TestAccount::generate()
.expect("Failed to generate address")
.address;
registry
.invoke(
"register",
&[
"--caller",
&admin.address,
"--name",
"payment_processor",
"--version",
"1",
"--address",
&addr_v1,
],
admin,
)
.expect("Failed to register v1");
println!("✓ Version 1 registered");
// Test 2: Get latest version
println!("\n--- Test 2: Get Latest Version ---");
let latest = registry
.invoke("get_latest", &["--name", "payment_processor"], admin)
.expect("Failed to get latest");
assert!(latest.contains(&addr_v1));
println!("✓ Latest version retrieved: {}", latest);
// Test 3: Register version 2
println!("\n--- Test 3: Register Version 2 ---");
let addr_v2 = TestAccount::generate()
.expect("Failed to generate address")
.address;
registry
.invoke(
"register",
&[
"--caller",
&admin.address,
"--name",
"payment_processor",
"--version",
"2",
"--address",
&addr_v2,
],
admin,
)
.expect("Failed to register v2");
println!("✓ Version 2 registered");
// Test 4: Verify latest is now v2
let latest = registry
.invoke("get_latest", &["--name", "payment_processor"], admin)
.expect("Failed to get latest");
assert!(latest.contains(&addr_v2));
println!("✓ Latest version is now v2");
// Test 5: Deprecate version 1
println!("\n--- Test 5: Deprecate Version 1 ---");
registry
.invoke(
"deprecate",
&[
"--caller",
&admin.address,
"--name",
"payment_processor",
"--version",
"1",
],
admin,
)
.expect("Failed to deprecate v1");
println!("✓ Version 1 deprecated");
println!("\n=== Router Registry Flow Test PASSED ===\n");
}
#[test]
#[ignore]
fn test_router_access_control() {
println!("\n=== Testing Router Access Control ===\n");
let fixture = TestSuite::setup().expect("Failed to set up test suite");
let access = fixture
.router_access
.as_ref()
.expect("Access contract not deployed");
let admin = &fixture.admin;
let user1 = &fixture.user1;
// Test 1: Grant role to user1
println!("\n--- Test 1: Grant Role ---");
access
.invoke(
"grant_role",
&[
"--caller",
&admin.address,
"--role",
"operator",
"--account",
&user1.address,
],
admin,
)
.expect("Failed to grant role");
println!("✓ Role 'operator' granted to user1");
// Test 2: Check if user1 has role
println!("\n--- Test 2: Check Role ---");
let has_role = access
.invoke(
"has_role",
&["--role", "operator", "--account", &user1.address],
admin,
)
.expect("Failed to check role");
assert!(has_role.contains("true"), "User should have role");
println!("✓ User1 has 'operator' role: {}", has_role);
// Test 3: Revoke role
println!("\n--- Test 3: Revoke Role ---");
access
.invoke(
"revoke_role",
&[
"--caller",
&admin.address,
"--role",
"operator",
"--account",
&user1.address,
],
admin,
)
.expect("Failed to revoke role");
println!("✓ Role revoked from user1");
// Test 4: Verify role is revoked
let has_role = access
.invoke(
"has_role",
&["--role", "operator", "--account", &user1.address],
admin,
)
.expect("Failed to check role");
assert!(has_role.contains("false"), "User should not have role");
println!("✓ User1 no longer has 'operator' role");
println!("\n=== Router Access Control Test PASSED ===\n");
}
#[test]
#[ignore]
fn test_router_middleware_rate_limiting() {
println!("\n=== Testing Router Middleware Rate Limiting ===\n");
let fixture = TestSuite::setup().expect("Failed to set up test suite");
let middleware = fixture
.router_middleware
.as_ref()
.expect("Middleware not deployed");
let admin = &fixture.admin;
// Test 1: Configure rate limit
println!("\n--- Test 1: Configure Rate Limit ---");
middleware
.invoke(
"configure_route",
&[
"--caller",
&admin.address,
"--route",
"oracle/get_price",
"--max_calls_per_window",
"5",
"--window_seconds",
"60",
"--enabled",
"true",
],
admin,
)
.expect("Failed to configure rate limit");
println!("✓ Rate limit configured: 5 calls per 60 seconds");
// Test 2: Enable route
println!("\n--- Test 2: Enable Route ---");
middleware
.invoke(
"set_route_enabled",
&[
"--caller",
&admin.address,
"--route",
"oracle/get_price",
"--enabled",
"true",
],
admin,
)
.expect("Failed to enable route");
println!("✓ Route enabled");
// Test 3: Disable route
println!("\n--- Test 3: Disable Route ---");
middleware
.invoke(
"set_route_enabled",
&[
"--caller",
&admin.address,
"--route",
"oracle/get_price",
"--enabled",
"false",
],
admin,
)
.expect("Failed to disable route");
println!("✓ Route disabled");
println!("\n=== Router Middleware Test PASSED ===\n");
}
#[test]
#[ignore]
fn test_router_timelock_operations() {
println!("\n=== Testing Router Timelock Operations ===\n");
let fixture = TestSuite::setup().expect("Failed to set up test suite");
let timelock = fixture
.router_timelock
.as_ref()
.expect("Timelock not deployed");
let admin = &fixture.admin;
// Test 1: Queue an operation
println!("\n--- Test 1: Queue Operation ---");
let target = TestAccount::generate()
.expect("Failed to generate target")
.address;
let result = timelock.invoke(
"queue",
&[
"--proposer",
&admin.address,
"--description",
"Upgrade oracle contract",
"--target",
&target,
"--delay",
"60",
],
admin,
);
if result.is_ok() {
println!("✓ Operation queued successfully");
// Test 2: Get operation count
println!("\n--- Test 2: Get Operation Count ---");
let count = timelock
.invoke("get_operation_count", &[], admin)
.expect("Failed to get operation count");
println!("✓ Operation count: {}", count);
} else {
println!(
"⚠ Queue operation may require different parameters: {:?}",
result
);
}
println!("\n=== Router Timelock Test PASSED ===\n");
}
#[test]
#[ignore]
fn test_router_multicall_batching() {
println!("\n=== Testing Router Multicall Batching ===\n");
let fixture = TestSuite::setup().expect("Failed to set up test suite");
let multicall = fixture
.router_multicall
.as_ref()
.expect("Multicall not deployed");
let admin = &fixture.admin;
// Test 1: Get max batch size
println!("\n--- Test 1: Get Max Batch Size ---");
let max_size = multicall
.invoke("get_max_batch_size", &[], admin)
.expect("Failed to get max batch size");
println!("✓ Max batch size: {}", max_size);
// Test 2: Update max batch size
println!("\n--- Test 2: Update Max Batch Size ---");
multicall
.invoke(
"set_max_batch_size",
&["--caller", &admin.address, "--new_max", "20"],
admin,
)
.expect("Failed to update max batch size");
println!("✓ Max batch size updated to 20");
// Verify update
let new_max = multicall
.invoke("get_max_batch_size", &[], admin)
.expect("Failed to get updated max batch size");
assert!(new_max.contains("20"), "Max batch size should be 20");
println!("✓ Max batch size verified: {}", new_max);
println!("\n=== Router Multicall Test PASSED ===\n");
}
/// End-to-end test for the timelock-guarded route update security pattern.
///
/// This test exercises the primary cross-contract safety pattern described in
/// docs/security.md and docs/architecture.md: sensitive route changes in
/// router-core must pass through the router-timelock delay queue. Without this
/// guard a compromised admin key can redirect any route instantly.
///
/// Flow:
/// 1. Deploy + initialize router-core and router-timelock.
/// 2. Register an initial route in router-core.
/// 3. Queue a route-update operation in router-timelock.
/// 4. Call execute BEFORE the ETA — expect NotReady (error code 5).
/// 5. Advance past the ETA (testnet: wait; unit env: ledger.timestamp +=).
/// 6. Call execute AFTER the ETA — expect success.
/// 7. Manually apply the route update in router-core (execute marks the op
/// done; the actual cross-contract call is the next step in the flow).
/// 8. Resolve the route — verify it reflects the new address.
///
/// NOTE: Because testnet time cannot be fast-forwarded via CLI, step 5 uses
/// the minimum delay (60 s) and waits for real time to elapse. In the
/// soroban-sdk unit environment (see cross_contract_tests.rs) the ledger
/// timestamp is manipulated directly.
#[test]
#[ignore] // Run with: cargo test --test integration_tests -- --ignored --test-threads=1
fn test_timelock_guarded_route_update() {
println!("\n=== Testing Timelock-Guarded Route Update Flow ===\n");
let fixture = TestSuite::setup().expect("Failed to set up test suite");
let core = fixture
.router_core
.as_ref()
.expect("Core contract not deployed");
let timelock = fixture
.router_timelock
.as_ref()
.expect("Timelock contract not deployed");
let admin = &fixture.admin;
// ── Step 1: Contracts are already deployed and initialized by TestSuite::setup().
// router-timelock was initialized with min_delay = 60 seconds.
println!("✓ Contracts deployed and initialized by TestSuite");
// ── Step 2: Register an initial route in router-core.
println!("\n--- Step 2: Register initial route ---");
let initial_addr = TestAccount::generate()
.expect("Failed to generate initial address")
.address;
core.invoke(
"register_route",
&[
"--caller",
&admin.address,
"--name",
"oracle",
"--address",
&initial_addr,
"--metadata",
"null",
],
admin,
)
.expect("Failed to register initial route");
let resolved = core
.invoke("resolve", &["--name", "oracle"], admin)
.expect("Failed to resolve initial route");
assert!(
resolved.contains(&initial_addr),
"Initial route should resolve to initial_addr, got: {}",
resolved
);
println!(
"✓ Route 'oracle' registered and resolves to: {}",
initial_addr
);
// ── Step 3: Queue a route-update operation in router-timelock.
// The operation target is the new address we intend to point the route at.
println!("\n--- Step 3: Queue route update in router-timelock ---");
let new_addr = TestAccount::generate()
.expect("Failed to generate new address")
.address;
// delay = 60 s (equals min_delay), grace_period = 120 s, no dependencies.
let op_id_result = timelock.invoke(
"queue",
&[
"--proposer",
&admin.address,
"--description",
"Update oracle route to new address",
"--target",
&new_addr,
"--delay",
"60",
"--grace_period_seconds",
"120",
"--deps",
"[]",
],
admin,
);
assert!(
op_id_result.is_ok(),
"Failed to queue operation: {:?}",
op_id_result
);
let op_id = op_id_result.unwrap();
println!("✓ Operation queued, op_id: {}", op_id);
// Confirm the operation is in Queued state (not yet Ready).
let status = timelock
.invoke("get_operation_status", &["--op_id", &op_id], admin)
.expect("Failed to get operation status");
assert!(
status.contains("Queued"),
"Operation should be Queued before ETA, got: {}",
status
);
println!("✓ Operation status is Queued (ETA not yet reached)");
// ── Step 4: Call execute BEFORE the ETA — must return NotReady (error code 5).
println!("\n--- Step 4: Execute before ETA (should fail with NotReady) ---");
let early_result = timelock.try_invoke(
"execute",
&["--caller", &admin.address, "--op_id", &op_id],
admin,
);
assert!(
early_result.is_err(),
"execute before ETA should fail, but got: {:?}",
early_result
);
let early_error = early_result.err().unwrap();
assert!(
early_error.contains("NotReady") || early_error.contains("5"),
"Expected NotReady (error code 5), got: {}",
early_error
);
println!(
"✓ execute before ETA correctly rejected with NotReady: {}",
early_error
);
// ── Step 5: Wait for the ETA to pass on testnet (min_delay = 60 s).
// We add a small buffer (5 s) to account for block time variance.
println!("\n--- Step 5: Waiting 65 s for ETA to pass on testnet ---");
std::thread::sleep(std::time::Duration::from_secs(65));
println!("✓ ETA has elapsed");
// Confirm the operation status has transitioned to Ready.
let status = timelock
.invoke("get_operation_status", &["--op_id", &op_id], admin)
.expect("Failed to get operation status after wait");
assert!(
status.contains("Ready"),
"Operation should be Ready after ETA, got: {}",
status
);
println!("✓ Operation status is Ready");
// ── Step 6: Call execute AFTER the ETA — expect success.
println!("\n--- Step 6: Execute after ETA (should succeed) ---");
timelock
.invoke(
"execute",
&["--caller", &admin.address, "--op_id", &op_id],
admin,
)
.expect("Failed to execute timelock operation after ETA");
// Verify the operation is now in Executed state.
let status = timelock
.invoke("get_operation_status", &["--op_id", &op_id], admin)
.expect("Failed to get operation status after execute");
assert!(
status.contains("Executed"),
"Operation should be Executed, got: {}",
status
);
println!("✓ Timelock operation executed successfully");
// ── Step 7: Apply the route update in router-core.
// The timelock execute() marks the op done but does NOT perform the
// cross-contract call itself (the current router-timelock design records
// the intended target address in the Op struct for off-chain actors or a
// future executor integration to act upon). The admin now performs the
// actual route update, which in production would be triggered by the
// timelock execution event.
println!("\n--- Step 7: Apply route update in router-core ---");
core.invoke(
"update_route",
&[
"--caller",
&admin.address,
"--name",
"oracle",
"--new_address",
&new_addr,
],
admin,
)
.expect("Failed to update route in router-core");
println!("✓ Route updated in router-core");
// ── Step 8: Resolve the route and verify it reflects the new address.
println!("\n--- Step 8: Resolve route (should return new address) ---");
let resolved = core
.invoke("resolve", &["--name", "oracle"], admin)
.expect("Failed to resolve updated route");
assert!(
resolved.contains(&new_addr),
"Route should resolve to new_addr '{}', got: '{}'",
new_addr,
resolved
);
// Explicitly confirm the old address is gone.
assert!(
!resolved.contains(&initial_addr),
"Route should no longer resolve to initial_addr '{}', got: '{}'",
initial_addr,
resolved
);
println!("✓ Route resolves to new address: {}", resolved);
println!("\n=== Timelock-Guarded Route Update Test PASSED ===\n");
}
#[test]
#[ignore]
fn test_admin_transfer() {
println!("\n=== Testing Admin Transfer ===\n");
let fixture = TestSuite::setup().expect("Failed to set up test suite");
let core = fixture
.router_core
.as_ref()
.expect("Core contract not deployed");
let admin = &fixture.admin;
let new_admin = &fixture.user1;
// Test 1: Get current admin
println!("\n--- Test 1: Get Current Admin ---");
let current_admin = core
.invoke("admin", &[], admin)
.expect("Failed to get admin");
assert!(current_admin.contains(&admin.address));
println!("✓ Current admin: {}", current_admin);
// Test 2: Transfer admin
println!("\n--- Test 2: Transfer Admin ---");
core.invoke(
"transfer_admin",
&[
"--current",
&admin.address,
"--new_admin",
&new_admin.address,
],
admin,
)
.expect("Failed to transfer admin");
println!("✓ Admin transferred to user1");
// Test 3: Verify new admin
let current_admin = core
.invoke("admin", &[], new_admin)
.expect("Failed to get new admin");
assert!(current_admin.contains(&new_admin.address));
println!("✓ New admin verified: {}", current_admin);
println!("\n=== Admin Transfer Test PASSED ===\n");
}