Skip to content

Commit

Permalink
test(zk): Migrate ownership zksync tests to cargo test (#523)
Browse files Browse the repository at this point in the history
migrate ownership zksync tests to cargo test

Co-authored-by: Jrigada <[email protected]>
  • Loading branch information
Jrigada and Jrigada authored Aug 14, 2024
1 parent 7f38caa commit 5c7b918
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 0 deletions.
1 change: 1 addition & 0 deletions crates/forge/tests/it/zk/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,5 @@ mod factory;
mod fuzz;
mod invariant;
mod logs;
mod ownership;
mod repros;
20 changes: 20 additions & 0 deletions crates/forge/tests/it/zk/ownership.rs
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;
}
62 changes: 62 additions & 0 deletions testdata/zk/Ownership.t.sol
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);
}
}

0 comments on commit 5c7b918

Please sign in to comment.