|
| 1 | +use openzeppelin::account::extensions::src9::interface::ISRC9_V2_ID; |
| 2 | +use openzeppelin::account::interface::ISRC6_ID; |
| 3 | +use openzeppelin::introspection::interface::{ISRC5Dispatcher, ISRC5DispatcherTrait}; |
| 4 | +use snforge_std::{EventSpyTrait, spy_events, start_cheat_caller_address, stop_cheat_caller_address}; |
| 5 | +use crate::interface::{IAccount712AdminDispatcher, IAccount712AdminDispatcherTrait}; |
| 6 | +use crate::test_utils::{ |
| 7 | + TEST_ETH_ADDRESS, WRONG_ETH_ADDRESS, deploy_eth712_account, get_invalid_signature, |
| 8 | + get_ownership_signature, |
| 9 | +}; |
| 10 | + |
| 11 | +// ================================ |
| 12 | +// initialize tests |
| 13 | +// ================================ |
| 14 | + |
| 15 | +#[test] |
| 16 | +fn test_initialize_success() { |
| 17 | + let contract_address = deploy_eth712_account(); |
| 18 | + let dispatcher = IAccount712AdminDispatcher { contract_address }; |
| 19 | + |
| 20 | + // Initialize with valid signature |
| 21 | + dispatcher.initialize(TEST_ETH_ADDRESS(), get_ownership_signature()); |
| 22 | + |
| 23 | + // Verify interfaces are registered |
| 24 | + let src5 = ISRC5Dispatcher { contract_address }; |
| 25 | + assert!(src5.supports_interface(ISRC9_V2_ID), "ISRC9_V2_ID not registered"); |
| 26 | + assert!(src5.supports_interface(ISRC6_ID), "ISRC6_ID not registered"); |
| 27 | +} |
| 28 | + |
| 29 | +#[test] |
| 30 | +#[should_panic(expected: 'ALREADY_INITIALIZED')] |
| 31 | +fn test_initialize_already_initialized_reverts() { |
| 32 | + let contract_address = deploy_eth712_account(); |
| 33 | + let dispatcher = IAccount712AdminDispatcher { contract_address }; |
| 34 | + |
| 35 | + // First initialization should succeed |
| 36 | + dispatcher.initialize(TEST_ETH_ADDRESS(), get_ownership_signature()); |
| 37 | + |
| 38 | + // Second initialization should fail |
| 39 | + dispatcher.initialize(TEST_ETH_ADDRESS(), get_ownership_signature()); |
| 40 | +} |
| 41 | + |
| 42 | +#[test] |
| 43 | +#[should_panic(expected: 'INVALID_OWNERSHIP_SIGNATURE')] |
| 44 | +fn test_initialize_invalid_signature_reverts() { |
| 45 | + let contract_address = deploy_eth712_account(); |
| 46 | + let dispatcher = IAccount712AdminDispatcher { contract_address }; |
| 47 | + |
| 48 | + // Initialize with invalid signature |
| 49 | + dispatcher.initialize(TEST_ETH_ADDRESS(), get_invalid_signature()); |
| 50 | +} |
| 51 | + |
| 52 | +#[test] |
| 53 | +#[should_panic(expected: 'INVALID_OWNERSHIP_SIGNATURE')] |
| 54 | +fn test_initialize_wrong_address_reverts() { |
| 55 | + let contract_address = deploy_eth712_account(); |
| 56 | + let dispatcher = IAccount712AdminDispatcher { contract_address }; |
| 57 | + |
| 58 | + // Initialize with correct signature but wrong address |
| 59 | + dispatcher.initialize(WRONG_ETH_ADDRESS(), get_ownership_signature()); |
| 60 | +} |
| 61 | + |
| 62 | +// ================================ |
| 63 | +// upgrade tests |
| 64 | +// ================================ |
| 65 | + |
| 66 | +#[test] |
| 67 | +#[should_panic(expected: 'UNAUTHORIZED')] |
| 68 | +fn test_upgrade_unauthorized_reverts() { |
| 69 | + let contract_address = deploy_eth712_account(); |
| 70 | + let dispatcher = IAccount712AdminDispatcher { contract_address }; |
| 71 | + |
| 72 | + // Initialize first |
| 73 | + dispatcher.initialize(TEST_ETH_ADDRESS(), get_ownership_signature()); |
| 74 | + |
| 75 | + // Try to upgrade from external caller (not self) |
| 76 | + let new_class_hash = crate::test_utils::declare_eth712_account(); |
| 77 | + dispatcher.upgrade(new_class_hash, Option::None); |
| 78 | +} |
| 79 | + |
| 80 | +#[test] |
| 81 | +fn test_upgrade_from_self_succeeds() { |
| 82 | + let contract_address = deploy_eth712_account(); |
| 83 | + let dispatcher = IAccount712AdminDispatcher { contract_address }; |
| 84 | + |
| 85 | + // Initialize first |
| 86 | + dispatcher.initialize(TEST_ETH_ADDRESS(), get_ownership_signature()); |
| 87 | + |
| 88 | + // Spoof caller as self |
| 89 | + start_cheat_caller_address(contract_address, contract_address); |
| 90 | + |
| 91 | + let mut spy = spy_events(); |
| 92 | + let new_class_hash = crate::test_utils::declare_eth712_account(); |
| 93 | + dispatcher.upgrade(new_class_hash, Option::None); |
| 94 | + |
| 95 | + // Verify an event was emitted (Upgraded event) |
| 96 | + let events = spy.get_events(); |
| 97 | + assert!(events.events.len() > 0, "No events emitted"); |
| 98 | + |
| 99 | + // Verify the event data contains the new class hash |
| 100 | + let (from, event) = events.events.at(0); |
| 101 | + assert!(*from == contract_address, "Event from wrong address"); |
| 102 | + assert!(event.data.len() > 0, "Event has no data"); |
| 103 | + assert!(*event.data.at(0) == new_class_hash.into(), "Wrong class hash in event"); |
| 104 | + |
| 105 | + stop_cheat_caller_address(contract_address); |
| 106 | +} |
| 107 | + |
| 108 | +#[test] |
| 109 | +fn test_upgrade_with_eic() { |
| 110 | + let contract_address = deploy_eth712_account(); |
| 111 | + let dispatcher = IAccount712AdminDispatcher { contract_address }; |
| 112 | + |
| 113 | + // Initialize first |
| 114 | + dispatcher.initialize(TEST_ETH_ADDRESS(), get_ownership_signature()); |
| 115 | + |
| 116 | + // Spoof caller as self |
| 117 | + start_cheat_caller_address(contract_address, contract_address); |
| 118 | + |
| 119 | + let new_class_hash = crate::test_utils::declare_eth712_account(); |
| 120 | + let eic_class_hash = crate::test_utils::declare_register_interfaces_eic(); |
| 121 | + |
| 122 | + // Register a custom interface via EIC |
| 123 | + let custom_interface_id: felt252 = 0x12345678; |
| 124 | + let eic_data: Span<felt252> = array![custom_interface_id].span(); |
| 125 | + |
| 126 | + dispatcher.upgrade(new_class_hash, Option::Some((eic_class_hash, eic_data))); |
| 127 | + |
| 128 | + // Verify the custom interface was registered |
| 129 | + let src5 = ISRC5Dispatcher { contract_address }; |
| 130 | + assert!(src5.supports_interface(custom_interface_id), "Custom interface not registered"); |
| 131 | + |
| 132 | + stop_cheat_caller_address(contract_address); |
| 133 | +} |
0 commit comments