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

Adding denylist tests #113

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
171 changes: 164 additions & 7 deletions program/tests/additional_signer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,12 @@ use mpl_token_auth_rules::{
payload::Payload,
state::{Rule, RuleSetV1},
};
use solana_program::instruction::AccountMeta;
use solana_program_test::tokio;
use solana_program::{instruction::AccountMeta, pubkey::Pubkey};
use solana_program_test::{tokio, ProgramTestContext};
use solana_sdk::{signature::Signer, signer::keypair::Keypair};
use utils::{program_test, Operation};

#[tokio::test]
async fn test_additional_signer() {
let mut context = program_test().start_with_context().await;

async fn create_rule_set(context: &mut ProgramTestContext) -> (Pubkey, Keypair) {
// --------------------------------
// Create RuleSet
// --------------------------------
Expand All @@ -42,14 +39,90 @@ async fn test_additional_signer() {

// Put the RuleSet on chain.
let rule_set_addr =
create_rule_set_on_chain!(&mut context, rule_set, "test rule_set".to_string()).await;
create_rule_set_on_chain!(context, rule_set, "test rule_set".to_string()).await;

(rule_set_addr, adtl_signer)
}

async fn create_not_rule_set(context: &mut ProgramTestContext) -> (Pubkey, Keypair) {
// --------------------------------
// Create RuleSet
// --------------------------------
// Create some rules.
let adtl_signer = Keypair::new();
let adtl_signer_rule = Rule::Not {
rule: Box::new(Rule::AdditionalSigner {
account: adtl_signer.pubkey(),
}),
};

// Create a RuleSet.
let mut rule_set = RuleSetV1::new("test rule_set".to_string(), context.payer.pubkey());
rule_set
.add(
Operation::Transfer {
scenario: utils::TransferScenario::Holder,
}
.to_string(),
adtl_signer_rule,
)
.unwrap();

// Put the RuleSet on chain.
let rule_set_addr =
create_rule_set_on_chain!(context, rule_set, "test rule_set".to_string()).await;

(rule_set_addr, adtl_signer)
}

#[tokio::test]
async fn test_additional_signer_missing_account() {
let mut context = program_test().start_with_context().await;

let (rule_set_addr, _) = create_rule_set(&mut context).await;
let mint = Keypair::new().pubkey();

// --------------------------------
// Validate fail missing account
// --------------------------------
// Create a Keypair to simulate a token mint address.

// Create a `validate` instruction WITHOUT the additional signer.
let validate_ix = ValidateBuilder::new()
.rule_set_pda(rule_set_addr)
.mint(mint)
.additional_rule_accounts(vec![])
.build(ValidateArgs::V1 {
operation: Operation::Transfer {
scenario: utils::TransferScenario::Holder,
}
.to_string(),
payload: Payload::default(),
update_rule_state: false,
rule_set_revision: None,
})
.unwrap()
.instruction();

// Fail to validate Transfer operation.
let err = process_failing_validate_ix!(&mut context, validate_ix, vec![], None).await;

// Check that error is what we expect.
assert_custom_error!(err, RuleSetError::MissingAccount);
}

#[tokio::test]
async fn test_not_additional_signer_missing_account() {
let mut context = program_test().start_with_context().await;

let (rule_set_addr, _) = create_not_rule_set(&mut context).await;
let mint = Keypair::new().pubkey();

// --------------------------------
// Validate fail missing account
// --------------------------------
// Create a Keypair to simulate a token mint address.

// Create a `validate` instruction WITHOUT the additional signer.
let validate_ix = ValidateBuilder::new()
.rule_set_pda(rule_set_addr)
Expand All @@ -72,10 +145,19 @@ async fn test_additional_signer() {

// Check that error is what we expect.
assert_custom_error!(err, RuleSetError::MissingAccount);
}

#[tokio::test]
async fn test_additional_signer_not_signer() {
let mut context = program_test().start_with_context().await;

let (rule_set_addr, adtl_signer) = create_rule_set(&mut context).await;
let mint = Keypair::new().pubkey();

// --------------------------------
// Validate fail not a signer
// --------------------------------

// Create a `validate` instruction WITH the additional account but not as a signer.
let validate_ix = ValidateBuilder::new()
.rule_set_pda(rule_set_addr)
Expand All @@ -98,6 +180,46 @@ async fn test_additional_signer() {

// Check that error is what we expect.
assert_custom_error!(err, RuleSetError::AdditionalSignerCheckFailed);
}

#[tokio::test]
async fn test_not_additional_signer_not_signer() {
let mut context = program_test().start_with_context().await;

let (rule_set_addr, adtl_signer) = create_not_rule_set(&mut context).await;
let mint = Keypair::new().pubkey();

// --------------------------------
// Validate passes when not a signer
// --------------------------------

// Create a `validate` instruction WITH the additional account but not as a signer.
let validate_ix = ValidateBuilder::new()
.rule_set_pda(rule_set_addr)
.mint(mint)
.additional_rule_accounts(vec![AccountMeta::new_readonly(adtl_signer.pubkey(), false)])
.build(ValidateArgs::V1 {
operation: Operation::Transfer {
scenario: utils::TransferScenario::Holder,
}
.to_string(),
payload: Payload::default(),
update_rule_state: false,
rule_set_revision: None,
})
.unwrap()
.instruction();

// Validate Transfer operation.
process_passing_validate_ix!(&mut context, validate_ix, vec![], None).await;
}

#[tokio::test]
async fn test_additional_signer_pass() {
let mut context = program_test().start_with_context().await;

let (rule_set_addr, adtl_signer) = create_rule_set(&mut context).await;
let mint = Keypair::new().pubkey();

// --------------------------------
// Validate pass
Expand All @@ -122,3 +244,38 @@ async fn test_additional_signer() {
// Validate Transfer operation.
process_passing_validate_ix!(&mut context, validate_ix, vec![&adtl_signer], None).await;
}

#[tokio::test]
async fn test_not_additional_signer_fail() {
let mut context = program_test().start_with_context().await;

let (rule_set_addr, adtl_signer) = create_not_rule_set(&mut context).await;
let mint = Keypair::new().pubkey();

// --------------------------------
// Validate fail
// --------------------------------
// Create a `validate` instruction WITH the additional signer.
let validate_ix = ValidateBuilder::new()
.rule_set_pda(rule_set_addr)
.mint(mint)
.additional_rule_accounts(vec![AccountMeta::new_readonly(adtl_signer.pubkey(), true)])
.build(ValidateArgs::V1 {
operation: Operation::Transfer {
scenario: utils::TransferScenario::Holder,
}
.to_string(),
payload: Payload::default(),
update_rule_state: false,
rule_set_revision: None,
})
.unwrap()
.instruction();

// Validate Transfer operation.
let err =
process_failing_validate_ix!(&mut context, validate_ix, vec![&adtl_signer], None).await;

// Check that error is what we expect.
assert_custom_error!(err, RuleSetError::AdditionalSignerCheckFailed);
}
Loading