|
| 1 | +mod setup; |
| 2 | + |
| 3 | +use { |
| 4 | + mollusk_svm::result::Check, |
| 5 | + solana_account::Account, |
| 6 | + solana_account_info::MAX_PERMITTED_DATA_INCREASE, |
| 7 | + solana_program_error::ProgramError, |
| 8 | + solana_pubkey::Pubkey, |
| 9 | + solana_system_interface::{ |
| 10 | + error::SystemError, instruction::allocate_with_seed, MAX_PERMITTED_DATA_LENGTH, |
| 11 | + }, |
| 12 | +}; |
| 13 | + |
| 14 | +const BASE: Pubkey = Pubkey::new_from_array([6; 32]); |
| 15 | +const OWNER: Pubkey = Pubkey::new_from_array([8; 32]); |
| 16 | +const SEED: &str = "seed"; |
| 17 | +const SPACE: u64 = 10_000; |
| 18 | + |
| 19 | +#[test] |
| 20 | +fn fail_address_with_seed_mismatch() { |
| 21 | + let mollusk = setup::setup(); |
| 22 | + |
| 23 | + let pubkey = Pubkey::new_unique(); // Not derived from base + seed. |
| 24 | + |
| 25 | + mollusk.process_and_validate_instruction( |
| 26 | + &allocate_with_seed(&pubkey, &BASE, SEED, SPACE, &OWNER), |
| 27 | + &[(pubkey, Account::default()), (BASE, Account::default())], |
| 28 | + &[Check::err(ProgramError::Custom( |
| 29 | + SystemError::AddressWithSeedMismatch as u32, |
| 30 | + ))], |
| 31 | + ); |
| 32 | +} |
| 33 | + |
| 34 | +#[test] |
| 35 | +fn fail_base_not_signer() { |
| 36 | + let mollusk = setup::setup(); |
| 37 | + |
| 38 | + let pubkey = Pubkey::create_with_seed(&BASE, &SEED, &OWNER).unwrap(); |
| 39 | + |
| 40 | + let mut instruction = allocate_with_seed(&pubkey, &BASE, SEED, SPACE, &OWNER); |
| 41 | + instruction.accounts[1].is_signer = false; |
| 42 | + |
| 43 | + mollusk.process_and_validate_instruction( |
| 44 | + &instruction, |
| 45 | + &[(pubkey, Account::default()), (BASE, Account::default())], |
| 46 | + &[Check::err(ProgramError::MissingRequiredSignature)], |
| 47 | + ); |
| 48 | +} |
| 49 | + |
| 50 | +#[test] |
| 51 | +fn fail_base_key_mismatch() { |
| 52 | + let mollusk = setup::setup(); |
| 53 | + |
| 54 | + let pubkey = Pubkey::create_with_seed(&BASE, &SEED, &OWNER).unwrap(); |
| 55 | + let not_the_base = Pubkey::new_unique(); |
| 56 | + |
| 57 | + let mut instruction = allocate_with_seed(&pubkey, &BASE, SEED, SPACE, &OWNER); |
| 58 | + instruction.accounts[1].pubkey = not_the_base; |
| 59 | + |
| 60 | + mollusk.process_and_validate_instruction( |
| 61 | + &instruction, |
| 62 | + &[ |
| 63 | + (pubkey, Account::default()), |
| 64 | + (not_the_base, Account::default()), |
| 65 | + ], |
| 66 | + &[Check::err(ProgramError::MissingRequiredSignature)], |
| 67 | + ); |
| 68 | +} |
| 69 | + |
| 70 | +#[test] |
| 71 | +fn fail_account_already_in_use() { |
| 72 | + let mollusk = setup::setup(); |
| 73 | + |
| 74 | + let pubkey = Pubkey::create_with_seed(&BASE, &SEED, &OWNER).unwrap(); |
| 75 | + |
| 76 | + let account = Account { |
| 77 | + data: vec![4; 32], // Has data |
| 78 | + ..Account::default() |
| 79 | + }; |
| 80 | + |
| 81 | + mollusk.process_and_validate_instruction( |
| 82 | + &allocate_with_seed(&pubkey, &BASE, SEED, SPACE, &OWNER), |
| 83 | + &[(pubkey, account), (BASE, Account::default())], |
| 84 | + &[Check::err(ProgramError::Custom( |
| 85 | + SystemError::AccountAlreadyInUse as u32, |
| 86 | + ))], |
| 87 | + ); |
| 88 | + |
| 89 | + let account = Account { |
| 90 | + owner: Pubkey::new_unique(), // Not System |
| 91 | + ..Account::default() |
| 92 | + }; |
| 93 | + |
| 94 | + mollusk.process_and_validate_instruction( |
| 95 | + &allocate_with_seed(&pubkey, &BASE, SEED, SPACE, &OWNER), |
| 96 | + &[(pubkey, account), (BASE, Account::default())], |
| 97 | + &[Check::err(ProgramError::Custom( |
| 98 | + SystemError::AccountAlreadyInUse as u32, |
| 99 | + ))], |
| 100 | + ); |
| 101 | +} |
| 102 | + |
| 103 | +#[test] |
| 104 | +fn fail_space_too_large() { |
| 105 | + let mollusk = setup::setup(); |
| 106 | + |
| 107 | + let pubkey = Pubkey::create_with_seed(&BASE, &SEED, &OWNER).unwrap(); |
| 108 | + |
| 109 | + let space_too_large = MAX_PERMITTED_DATA_LENGTH + 1; |
| 110 | + |
| 111 | + mollusk.process_and_validate_instruction( |
| 112 | + &allocate_with_seed(&pubkey, &BASE, SEED, space_too_large, &OWNER), |
| 113 | + &[(pubkey, Account::default()), (BASE, Account::default())], |
| 114 | + &[Check::err(ProgramError::Custom( |
| 115 | + SystemError::InvalidAccountDataLength as u32, |
| 116 | + ))], |
| 117 | + ); |
| 118 | +} |
| 119 | + |
| 120 | +// [TODO: CORE_BPF]: This verifies the concern for the `realloc` issue. |
| 121 | +#[test] |
| 122 | +fn fail_space_too_large_for_realloc() { |
| 123 | + let mollusk = setup::setup(); |
| 124 | + |
| 125 | + let pubkey = Pubkey::create_with_seed(&BASE, &SEED, &OWNER).unwrap(); |
| 126 | + |
| 127 | + let space_too_large_for_realloc = MAX_PERMITTED_DATA_INCREASE + 1; |
| 128 | + |
| 129 | + mollusk.process_and_validate_instruction( |
| 130 | + &allocate_with_seed( |
| 131 | + &pubkey, |
| 132 | + &BASE, |
| 133 | + SEED, |
| 134 | + space_too_large_for_realloc as u64, |
| 135 | + &OWNER, |
| 136 | + ), |
| 137 | + &[(pubkey, Account::default()), (BASE, Account::default())], |
| 138 | + &[Check::err(ProgramError::InvalidRealloc)], // See...? |
| 139 | + ); |
| 140 | +} |
| 141 | + |
| 142 | +#[test] |
| 143 | +fn success() { |
| 144 | + let mollusk = setup::setup(); |
| 145 | + |
| 146 | + let pubkey = Pubkey::create_with_seed(&BASE, &SEED, &OWNER).unwrap(); |
| 147 | + |
| 148 | + mollusk.process_and_validate_instruction( |
| 149 | + &allocate_with_seed(&pubkey, &BASE, SEED, SPACE, &OWNER), |
| 150 | + &[(pubkey, Account::default()), (BASE, Account::default())], |
| 151 | + &[ |
| 152 | + Check::success(), |
| 153 | + Check::account(&pubkey) |
| 154 | + .owner(&OWNER) |
| 155 | + .space(SPACE as usize) |
| 156 | + .build(), |
| 157 | + ], |
| 158 | + ); |
| 159 | +} |
0 commit comments