Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Migrate security tests #1034

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/tests.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@
mod mocks;
// #[cfg(test)]
// mod presets;
// #[cfg(test)]
// mod security;
#[cfg(test)]
mod security;
#[cfg(test)]
mod token;
// #[cfg(test)]
Expand Down
64 changes: 42 additions & 22 deletions src/tests/security/test_pausable.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@ use openzeppelin::security::PausableComponent::{InternalImpl, PausableImpl};
use openzeppelin::security::PausableComponent::{Paused, Unpaused};
use openzeppelin::security::PausableComponent;
use openzeppelin::tests::mocks::pausable_mocks::PausableMock;
use openzeppelin::tests::utils::constants::{CALLER, ZERO};
use openzeppelin::tests::utils;
use openzeppelin::tests::utils::constants::CALLER;
use openzeppelin::tests::utils::events::EventSpyExt;
use snforge_std::EventSpy;
use snforge_std::{spy_events, test_address, start_cheat_caller_address};
use starknet::ContractAddress;
use starknet::contract_address_const;
use starknet::testing;

type ComponentState = PausableComponent::ComponentState<PausableMock::ContractState>;

Expand Down Expand Up @@ -73,11 +73,13 @@ fn test_assert_not_paused_when_not_paused() {
#[test]
fn test_pause_when_unpaused() {
let mut state = COMPONENT_STATE();
testing::set_caller_address(CALLER());
let contract_address = test_address();

let mut spy = spy_events();
start_cheat_caller_address(contract_address, CALLER());
state.pause();

assert_event_paused(CALLER());
spy.assert_only_event_paused(contract_address, CALLER());
assert!(state.is_paused());
}

Expand All @@ -96,14 +98,15 @@ fn test_pause_when_paused() {
#[test]
fn test_unpause_when_paused() {
let mut state = COMPONENT_STATE();
testing::set_caller_address(CALLER());
let contract_address = test_address();

let mut spy = spy_events();
start_cheat_caller_address(test_address(), CALLER());
state.pause();
utils::drop_event(ZERO());

state.unpause();

assert_event_unpaused(CALLER());
spy.assert_event_paused(contract_address, CALLER());
spy.assert_only_event_unpaused(contract_address, CALLER());
assert!(!state.is_paused());
}

Expand All @@ -119,16 +122,33 @@ fn test_unpause_when_unpaused() {
// Helpers
//

fn assert_event_paused(account: ContractAddress) {
let event = utils::pop_log::<PausableComponent::Event>(ZERO()).unwrap();
let expected = PausableComponent::Event::Paused(Paused { account });
assert!(event == expected);
utils::assert_no_events_left(ZERO());
}

fn assert_event_unpaused(account: ContractAddress) {
let event = utils::pop_log::<PausableComponent::Event>(ZERO()).unwrap();
let expected = PausableComponent::Event::Unpaused(Unpaused { account });
assert!(event == expected);
utils::assert_no_events_left(ZERO());
#[generate_trait]
impl PausableSpyHelpersImpl of PausableSpyHelpers {
fn assert_event_paused(
ref self: EventSpy, contract: ContractAddress, account: ContractAddress
) {
let expected = PausableComponent::Event::Paused(Paused { account });
self.assert_emitted_single(contract, expected);
}

fn assert_only_event_paused(
ref self: EventSpy, contract: ContractAddress, account: ContractAddress,
) {
self.assert_event_paused(contract, account);
self.assert_no_events_left_from(contract);
}

fn assert_event_unpaused(
ref self: EventSpy, contract: ContractAddress, account: ContractAddress
) {
let expected = PausableComponent::Event::Unpaused(Unpaused { account });
self.assert_emitted_single(contract, expected);
}

fn assert_only_event_unpaused(
ref self: EventSpy, contract: ContractAddress, account: ContractAddress,
) {
self.assert_event_unpaused(contract, account);
self.assert_no_events_left_from(contract);
}
}
23 changes: 7 additions & 16 deletions src/tests/security/test_reentrancyguard.cairo
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use openzeppelin::security::ReentrancyGuardComponent::InternalImpl;
use openzeppelin::security::ReentrancyGuardComponent;
use openzeppelin::tests::mocks::reentrancy_mocks::{
Attacker, ReentrancyMock, IReentrancyMockDispatcher, IReentrancyMockDispatcherTrait
ReentrancyMock, IReentrancyMockDispatcher, IReentrancyMockDispatcherTrait
};
use openzeppelin::tests::utils;
use starknet::storage::StorageMemberAccessTrait;
Expand All @@ -14,7 +14,7 @@ fn COMPONENT_STATE() -> ComponentState {

fn deploy_mock() -> IReentrancyMockDispatcher {
let calldata = array![];
let address = utils::deploy(ReentrancyMock::TEST_CLASS_HASH, calldata);
let address = utils::declare_and_deploy("ReentrancyMock", calldata);
IReentrancyMockDispatcher { contract_address: address }
}

Expand Down Expand Up @@ -64,35 +64,26 @@ fn test_reentrancy_guard_end() {
//

#[test]
#[should_panic(
expected: (
'ReentrancyGuard: reentrant call',
'ENTRYPOINT_FAILED',
'ENTRYPOINT_FAILED',
'ENTRYPOINT_FAILED'
),
)]
#[should_panic(expected: ('ReentrancyGuard: reentrant call',))]
fn test_remote_callback() {
let contract = deploy_mock();

// Deploy attacker
let calldata = ArrayTrait::new();
let attacker_addr = utils::deploy(Attacker::TEST_CLASS_HASH, calldata);
let calldata = array![];
let attacker_addr = utils::declare_and_deploy("Attacker", calldata);

contract.count_and_call(attacker_addr);
}

#[test]
#[should_panic(expected: ('ReentrancyGuard: reentrant call', 'ENTRYPOINT_FAILED'))]
#[should_panic(expected: ('ReentrancyGuard: reentrant call',))]
fn test_local_recursion() {
let contract = deploy_mock();
contract.count_local_recursive(10);
}

#[test]
#[should_panic(
expected: ('ReentrancyGuard: reentrant call', 'ENTRYPOINT_FAILED', 'ENTRYPOINT_FAILED')
)]
#[should_panic(expected: ('ReentrancyGuard: reentrant call',))]
fn test_external_recursion() {
let contract = deploy_mock();
contract.count_external_recursive(10);
Expand Down
Loading