|
| 1 | +#![cfg(feature = "test-sbf")] |
| 2 | + |
| 3 | +mod common; |
| 4 | + |
| 5 | +use { |
| 6 | + common::{setup, upgradeable_state_account}, |
| 7 | + mollusk_svm::result::Check, |
| 8 | + solana_loader_v3_program::{instruction::set_authority_checked, state::UpgradeableLoaderState}, |
| 9 | + solana_sdk::{account::AccountSharedData, program_error::ProgramError, pubkey::Pubkey}, |
| 10 | +}; |
| 11 | + |
| 12 | +#[test] |
| 13 | +fn fail_invalid_account_state() { |
| 14 | + let mollusk = setup(); |
| 15 | + |
| 16 | + let buffer = Pubkey::new_unique(); |
| 17 | + let current_authority = Pubkey::new_unique(); |
| 18 | + let new_authority = Pubkey::new_unique(); |
| 19 | + |
| 20 | + let elf = &[3; 5_000]; |
| 21 | + |
| 22 | + mollusk.process_and_validate_instruction( |
| 23 | + &set_authority_checked(&buffer, ¤t_authority, &new_authority), |
| 24 | + &[ |
| 25 | + ( |
| 26 | + buffer, |
| 27 | + // Invalid account state (no authority). |
| 28 | + upgradeable_state_account( |
| 29 | + &UpgradeableLoaderState::Program { |
| 30 | + programdata_address: Pubkey::new_unique(), |
| 31 | + }, |
| 32 | + elf, |
| 33 | + false, |
| 34 | + ), |
| 35 | + ), |
| 36 | + (current_authority, AccountSharedData::default()), |
| 37 | + (new_authority, AccountSharedData::default()), |
| 38 | + ], |
| 39 | + &[Check::err(ProgramError::InvalidArgument)], |
| 40 | + ); |
| 41 | +} |
| 42 | + |
| 43 | +#[test] |
| 44 | +fn buffer_fail_buffer_immutable() { |
| 45 | + let mollusk = setup(); |
| 46 | + |
| 47 | + let buffer = Pubkey::new_unique(); |
| 48 | + let current_authority = Pubkey::new_unique(); |
| 49 | + let new_authority = Pubkey::new_unique(); |
| 50 | + |
| 51 | + let elf = &[3; 5_000]; |
| 52 | + |
| 53 | + mollusk.process_and_validate_instruction( |
| 54 | + &set_authority_checked(&buffer, ¤t_authority, &new_authority), |
| 55 | + &[ |
| 56 | + ( |
| 57 | + buffer, |
| 58 | + upgradeable_state_account( |
| 59 | + &UpgradeableLoaderState::Buffer { |
| 60 | + authority_address: None, // Immutable. |
| 61 | + }, |
| 62 | + elf, |
| 63 | + false, |
| 64 | + ), |
| 65 | + ), |
| 66 | + (current_authority, AccountSharedData::default()), |
| 67 | + (new_authority, AccountSharedData::default()), |
| 68 | + ], |
| 69 | + &[Check::err(ProgramError::Immutable)], |
| 70 | + ); |
| 71 | +} |
| 72 | + |
| 73 | +#[test] |
| 74 | +fn buffer_fail_incorrect_authority() { |
| 75 | + let mollusk = setup(); |
| 76 | + |
| 77 | + let buffer = Pubkey::new_unique(); |
| 78 | + let current_authority = Pubkey::new_unique(); |
| 79 | + let new_authority = Pubkey::new_unique(); |
| 80 | + |
| 81 | + let elf = &[3; 5_000]; |
| 82 | + |
| 83 | + mollusk.process_and_validate_instruction( |
| 84 | + &set_authority_checked(&buffer, ¤t_authority, &new_authority), |
| 85 | + &[ |
| 86 | + ( |
| 87 | + buffer, |
| 88 | + upgradeable_state_account( |
| 89 | + &UpgradeableLoaderState::Buffer { |
| 90 | + authority_address: Some(Pubkey::new_unique()), // Incorrect authority. |
| 91 | + }, |
| 92 | + elf, |
| 93 | + false, |
| 94 | + ), |
| 95 | + ), |
| 96 | + (current_authority, AccountSharedData::default()), |
| 97 | + (new_authority, AccountSharedData::default()), |
| 98 | + ], |
| 99 | + &[Check::err(ProgramError::IncorrectAuthority)], |
| 100 | + ); |
| 101 | +} |
| 102 | + |
| 103 | +#[test] |
| 104 | +fn buffer_fail_authority_not_signer() { |
| 105 | + let mollusk = setup(); |
| 106 | + |
| 107 | + let buffer = Pubkey::new_unique(); |
| 108 | + let current_authority = Pubkey::new_unique(); |
| 109 | + let new_authority = Pubkey::new_unique(); |
| 110 | + |
| 111 | + let elf = &[3; 5_000]; |
| 112 | + |
| 113 | + let mut instruction = set_authority_checked(&buffer, ¤t_authority, &new_authority); |
| 114 | + instruction.accounts[1].is_signer = false; // Not a signer. |
| 115 | + |
| 116 | + mollusk.process_and_validate_instruction( |
| 117 | + &instruction, |
| 118 | + &[ |
| 119 | + ( |
| 120 | + buffer, |
| 121 | + upgradeable_state_account( |
| 122 | + &UpgradeableLoaderState::Buffer { |
| 123 | + authority_address: Some(current_authority), |
| 124 | + }, |
| 125 | + elf, |
| 126 | + false, |
| 127 | + ), |
| 128 | + ), |
| 129 | + (current_authority, AccountSharedData::default()), |
| 130 | + (new_authority, AccountSharedData::default()), |
| 131 | + ], |
| 132 | + &[Check::err(ProgramError::MissingRequiredSignature)], |
| 133 | + ); |
| 134 | +} |
| 135 | + |
| 136 | +#[test] |
| 137 | +fn buffer_fail_new_authority_not_signer() { |
| 138 | + let mollusk = setup(); |
| 139 | + |
| 140 | + let buffer = Pubkey::new_unique(); |
| 141 | + let current_authority = Pubkey::new_unique(); |
| 142 | + let new_authority = Pubkey::new_unique(); |
| 143 | + |
| 144 | + let elf = &[3; 5_000]; |
| 145 | + |
| 146 | + let mut instruction = set_authority_checked(&buffer, ¤t_authority, &new_authority); |
| 147 | + instruction.accounts[2].is_signer = false; // Not a signer. |
| 148 | + |
| 149 | + mollusk.process_and_validate_instruction( |
| 150 | + &instruction, |
| 151 | + &[ |
| 152 | + ( |
| 153 | + buffer, |
| 154 | + upgradeable_state_account( |
| 155 | + &UpgradeableLoaderState::Buffer { |
| 156 | + authority_address: Some(current_authority), |
| 157 | + }, |
| 158 | + elf, |
| 159 | + false, |
| 160 | + ), |
| 161 | + ), |
| 162 | + (current_authority, AccountSharedData::default()), |
| 163 | + (new_authority, AccountSharedData::default()), |
| 164 | + ], |
| 165 | + &[Check::err(ProgramError::MissingRequiredSignature)], |
| 166 | + ); |
| 167 | +} |
| 168 | + |
| 169 | +#[test] |
| 170 | +fn buffer_success() { |
| 171 | + let mollusk = setup(); |
| 172 | + |
| 173 | + let buffer = Pubkey::new_unique(); |
| 174 | + let current_authority = Pubkey::new_unique(); |
| 175 | + let new_authority = Pubkey::new_unique(); |
| 176 | + |
| 177 | + let elf = &[3; 5_000]; |
| 178 | + |
| 179 | + let check_data = |authority_address: Option<Pubkey>| { |
| 180 | + let mut data = vec![0; UpgradeableLoaderState::size_of_buffer_metadata()]; |
| 181 | + bincode::serialize_into( |
| 182 | + &mut data[..], |
| 183 | + &UpgradeableLoaderState::Buffer { authority_address }, |
| 184 | + ) |
| 185 | + .unwrap(); |
| 186 | + data.extend_from_slice(elf); |
| 187 | + data |
| 188 | + }; |
| 189 | + |
| 190 | + mollusk.process_and_validate_instruction( |
| 191 | + &set_authority_checked(&buffer, ¤t_authority, &new_authority), |
| 192 | + &[ |
| 193 | + ( |
| 194 | + buffer, |
| 195 | + upgradeable_state_account( |
| 196 | + &UpgradeableLoaderState::Buffer { |
| 197 | + authority_address: Some(current_authority), |
| 198 | + }, |
| 199 | + elf, |
| 200 | + false, |
| 201 | + ), |
| 202 | + ), |
| 203 | + (current_authority, AccountSharedData::default()), |
| 204 | + (new_authority, AccountSharedData::default()), |
| 205 | + ], |
| 206 | + &[ |
| 207 | + Check::success(), |
| 208 | + Check::account(&buffer) |
| 209 | + .data( |
| 210 | + &check_data(Some(new_authority)), // Updated. |
| 211 | + ) |
| 212 | + .build(), |
| 213 | + ], |
| 214 | + ); |
| 215 | +} |
| 216 | + |
| 217 | +#[test] |
| 218 | +fn programdata_fail_not_upgradeable() { |
| 219 | + let mollusk = setup(); |
| 220 | + |
| 221 | + let programdata = Pubkey::new_unique(); |
| 222 | + let current_authority = Pubkey::new_unique(); |
| 223 | + let new_authority = Pubkey::new_unique(); |
| 224 | + |
| 225 | + let elf = &[3; 5_000]; |
| 226 | + |
| 227 | + mollusk.process_and_validate_instruction( |
| 228 | + &set_authority_checked(&programdata, ¤t_authority, &new_authority), |
| 229 | + &[ |
| 230 | + ( |
| 231 | + programdata, |
| 232 | + upgradeable_state_account( |
| 233 | + &UpgradeableLoaderState::ProgramData { |
| 234 | + slot: 2, |
| 235 | + upgrade_authority_address: None, // Immutable. |
| 236 | + }, |
| 237 | + elf, |
| 238 | + false, |
| 239 | + ), |
| 240 | + ), |
| 241 | + (current_authority, AccountSharedData::default()), |
| 242 | + (new_authority, AccountSharedData::default()), |
| 243 | + ], |
| 244 | + &[Check::err(ProgramError::Immutable)], |
| 245 | + ); |
| 246 | +} |
| 247 | + |
| 248 | +#[test] |
| 249 | +fn programdata_fail_incorrect_authority() { |
| 250 | + let mollusk = setup(); |
| 251 | + |
| 252 | + let programdata = Pubkey::new_unique(); |
| 253 | + let current_authority = Pubkey::new_unique(); |
| 254 | + let new_authority = Pubkey::new_unique(); |
| 255 | + |
| 256 | + let elf = &[3; 5_000]; |
| 257 | + |
| 258 | + mollusk.process_and_validate_instruction( |
| 259 | + &set_authority_checked(&programdata, ¤t_authority, &new_authority), |
| 260 | + &[ |
| 261 | + ( |
| 262 | + programdata, |
| 263 | + upgradeable_state_account( |
| 264 | + &UpgradeableLoaderState::ProgramData { |
| 265 | + slot: 2, |
| 266 | + upgrade_authority_address: Some(Pubkey::new_unique()), // Incorrect authority. |
| 267 | + }, |
| 268 | + elf, |
| 269 | + false, |
| 270 | + ), |
| 271 | + ), |
| 272 | + (current_authority, AccountSharedData::default()), |
| 273 | + (new_authority, AccountSharedData::default()), |
| 274 | + ], |
| 275 | + &[Check::err(ProgramError::IncorrectAuthority)], |
| 276 | + ); |
| 277 | +} |
| 278 | + |
| 279 | +#[test] |
| 280 | +fn programdata_fail_authority_not_signer() { |
| 281 | + let mollusk = setup(); |
| 282 | + |
| 283 | + let programdata = Pubkey::new_unique(); |
| 284 | + let current_authority = Pubkey::new_unique(); |
| 285 | + let new_authority = Pubkey::new_unique(); |
| 286 | + |
| 287 | + let elf = &[3; 5_000]; |
| 288 | + |
| 289 | + let mut instruction = set_authority_checked(&programdata, ¤t_authority, &new_authority); |
| 290 | + instruction.accounts[1].is_signer = false; // Not a signer. |
| 291 | + |
| 292 | + mollusk.process_and_validate_instruction( |
| 293 | + &instruction, |
| 294 | + &[ |
| 295 | + ( |
| 296 | + programdata, |
| 297 | + upgradeable_state_account( |
| 298 | + &UpgradeableLoaderState::ProgramData { |
| 299 | + slot: 2, |
| 300 | + upgrade_authority_address: Some(current_authority), |
| 301 | + }, |
| 302 | + elf, |
| 303 | + false, |
| 304 | + ), |
| 305 | + ), |
| 306 | + (current_authority, AccountSharedData::default()), |
| 307 | + (new_authority, AccountSharedData::default()), |
| 308 | + ], |
| 309 | + &[Check::err(ProgramError::MissingRequiredSignature)], |
| 310 | + ); |
| 311 | +} |
| 312 | + |
| 313 | +#[test] |
| 314 | +fn programdata_fail_new_authority_not_signer() { |
| 315 | + let mollusk = setup(); |
| 316 | + |
| 317 | + let programdata = Pubkey::new_unique(); |
| 318 | + let current_authority = Pubkey::new_unique(); |
| 319 | + let new_authority = Pubkey::new_unique(); |
| 320 | + |
| 321 | + let elf = &[3; 5_000]; |
| 322 | + |
| 323 | + let mut instruction = set_authority_checked(&programdata, ¤t_authority, &new_authority); |
| 324 | + instruction.accounts[2].is_signer = false; // Not a signer. |
| 325 | + |
| 326 | + mollusk.process_and_validate_instruction( |
| 327 | + &instruction, |
| 328 | + &[ |
| 329 | + ( |
| 330 | + programdata, |
| 331 | + upgradeable_state_account( |
| 332 | + &UpgradeableLoaderState::ProgramData { |
| 333 | + slot: 2, |
| 334 | + upgrade_authority_address: Some(current_authority), |
| 335 | + }, |
| 336 | + elf, |
| 337 | + false, |
| 338 | + ), |
| 339 | + ), |
| 340 | + (current_authority, AccountSharedData::default()), |
| 341 | + (new_authority, AccountSharedData::default()), |
| 342 | + ], |
| 343 | + &[Check::err(ProgramError::MissingRequiredSignature)], |
| 344 | + ); |
| 345 | +} |
| 346 | + |
| 347 | +#[test] |
| 348 | +fn programdata_success() { |
| 349 | + let mollusk = setup(); |
| 350 | + |
| 351 | + let programdata = Pubkey::new_unique(); |
| 352 | + let current_authority = Pubkey::new_unique(); |
| 353 | + let new_authority = Pubkey::new_unique(); |
| 354 | + |
| 355 | + let elf = &[3; 5_000]; |
| 356 | + |
| 357 | + let check_data = |upgrade_authority_address: Option<Pubkey>| { |
| 358 | + let mut data = vec![0; UpgradeableLoaderState::size_of_programdata_metadata()]; |
| 359 | + bincode::serialize_into( |
| 360 | + &mut data[..], |
| 361 | + &UpgradeableLoaderState::ProgramData { |
| 362 | + slot: 0, |
| 363 | + upgrade_authority_address, |
| 364 | + }, |
| 365 | + ) |
| 366 | + .unwrap(); |
| 367 | + data.extend_from_slice(elf); |
| 368 | + data |
| 369 | + }; |
| 370 | + |
| 371 | + mollusk.process_and_validate_instruction( |
| 372 | + &set_authority_checked(&programdata, ¤t_authority, &new_authority), |
| 373 | + &[ |
| 374 | + ( |
| 375 | + programdata, |
| 376 | + upgradeable_state_account( |
| 377 | + &UpgradeableLoaderState::ProgramData { |
| 378 | + slot: 0, |
| 379 | + upgrade_authority_address: Some(current_authority), |
| 380 | + }, |
| 381 | + elf, |
| 382 | + false, |
| 383 | + ), |
| 384 | + ), |
| 385 | + (current_authority, AccountSharedData::default()), |
| 386 | + (new_authority, AccountSharedData::default()), |
| 387 | + ], |
| 388 | + &[ |
| 389 | + Check::success(), |
| 390 | + Check::account(&programdata) |
| 391 | + .data( |
| 392 | + &check_data(Some(new_authority)), // Updated. |
| 393 | + ) |
| 394 | + .build(), |
| 395 | + ], |
| 396 | + ); |
| 397 | +} |
0 commit comments