-
Notifications
You must be signed in to change notification settings - Fork 136
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test(zk): Migrate ownership zksync tests to cargo test (#523)
migrate ownership zksync tests to cargo test Co-authored-by: Jrigada <[email protected]>
- Loading branch information
Showing
3 changed files
with
83 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,4 +6,5 @@ mod factory; | |
mod fuzz; | ||
mod invariant; | ||
mod logs; | ||
mod ownership; | ||
mod repros; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
//! Forge tests for testing ownership in zksync. | ||
use crate::{config::*, test_helpers::TEST_DATA_DEFAULT}; | ||
use forge::revm::primitives::SpecId; | ||
use foundry_test_utils::Filter; | ||
|
||
#[tokio::test(flavor = "multi_thread")] | ||
async fn test_zk_ownership() { | ||
let runner = TEST_DATA_DEFAULT.runner_zksync(); | ||
let filter = Filter::new("testZkOwnership", "ZkOwnershipTest", ".*"); | ||
|
||
TestConfig::with_filter(runner, filter).evm_spec(SpecId::SHANGHAI).run().await; | ||
} | ||
|
||
#[tokio::test(flavor = "multi_thread")] | ||
async fn test_zk_ownership_delegate_call() { | ||
let runner = TEST_DATA_DEFAULT.runner_zksync(); | ||
let filter = Filter::new("testZkOwnershipDelegateCall", "ZkOwnershipTest", ".*"); | ||
|
||
TestConfig::with_filter(runner, filter).evm_spec(SpecId::SHANGHAI).run().await; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
// SPDX-License-Identifier: UNLICENSED | ||
pragma solidity ^0.8.13; | ||
|
||
import "ds-test/test.sol"; | ||
import "../cheats/Vm.sol"; | ||
|
||
contract MyOwnable { | ||
address public createOwner; | ||
address public txOwner; | ||
|
||
constructor() { | ||
createOwner = msg.sender; | ||
} | ||
|
||
function transact() public { | ||
txOwner = msg.sender; | ||
} | ||
} | ||
|
||
contract Delegator { | ||
/// Retuns the current `address(this), msg.sender` as a tuple. | ||
function transact() public view returns (address, address) { | ||
address thisAddress = address(this); | ||
address msgSender = msg.sender; | ||
return (thisAddress, msgSender); | ||
} | ||
} | ||
|
||
contract ZkOwnershipTest is DSTest { | ||
Vm constant vm = Vm(HEVM_ADDRESS); | ||
address OWNER_ADDRESS = address(0x11abcd); | ||
address TX_ADDRESS = address(0x22abcd); | ||
|
||
function testZkOwnership() public { | ||
// set owner balance to 0 to make sure deployment fails | ||
// if it's used for payment | ||
vm.deal(OWNER_ADDRESS, 0); | ||
vm.prank(OWNER_ADDRESS); | ||
MyOwnable ownable = new MyOwnable(); | ||
|
||
vm.deal(TX_ADDRESS, 0); | ||
vm.prank(TX_ADDRESS); | ||
ownable.transact(); | ||
|
||
assertEq(OWNER_ADDRESS, ownable.createOwner()); | ||
assertEq(TX_ADDRESS, ownable.txOwner()); | ||
} | ||
|
||
function testZkOwnershipDelegateCall() public { | ||
Delegator target = new Delegator(); | ||
address thisAddress = address(this); | ||
address msgSender = msg.sender; | ||
|
||
(bool success, bytes memory data) = | ||
address(target).delegatecall(abi.encodeWithSelector(target.transact.selector)); | ||
(address thisAddressTx, address msgSenderTx) = abi.decode(data, (address, address)); | ||
|
||
assert(success); | ||
assertEq(thisAddressTx, thisAddress); | ||
assertEq(msgSenderTx, msgSender); | ||
} | ||
} |