allocate.rs 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. mod setup;
  2. use {
  3. mollusk_svm::result::Check,
  4. solana_account::Account,
  5. solana_account_info::MAX_PERMITTED_DATA_INCREASE,
  6. solana_program_error::ProgramError,
  7. solana_pubkey::Pubkey,
  8. solana_system_interface::{
  9. error::SystemError, instruction::allocate, MAX_PERMITTED_DATA_LENGTH,
  10. },
  11. };
  12. const SPACE: u64 = 10_000;
  13. #[test]
  14. fn fail_account_not_signer() {
  15. let mollusk = setup::setup();
  16. let pubkey = Pubkey::new_unique();
  17. let mut instruction = allocate(&pubkey, SPACE);
  18. instruction.accounts[0].is_signer = false;
  19. mollusk.process_and_validate_instruction(
  20. &instruction,
  21. &[(pubkey, Account::default())],
  22. &[Check::err(ProgramError::MissingRequiredSignature)],
  23. );
  24. }
  25. #[test]
  26. fn fail_account_already_in_use() {
  27. let mollusk = setup::setup();
  28. let pubkey = Pubkey::new_unique();
  29. let account = Account {
  30. data: vec![4; 32], // Has data
  31. ..Account::default()
  32. };
  33. mollusk.process_and_validate_instruction(
  34. &allocate(&pubkey, SPACE),
  35. &[(pubkey, account)],
  36. &[Check::err(ProgramError::Custom(
  37. SystemError::AccountAlreadyInUse as u32,
  38. ))],
  39. );
  40. let account = Account {
  41. owner: Pubkey::new_unique(), // Not System
  42. ..Account::default()
  43. };
  44. mollusk.process_and_validate_instruction(
  45. &allocate(&pubkey, SPACE),
  46. &[(pubkey, account)],
  47. &[Check::err(ProgramError::Custom(
  48. SystemError::AccountAlreadyInUse as u32,
  49. ))],
  50. );
  51. }
  52. #[test]
  53. fn fail_space_too_large() {
  54. let mollusk = setup::setup();
  55. let pubkey = Pubkey::new_unique();
  56. let space_too_large = MAX_PERMITTED_DATA_LENGTH + 1;
  57. mollusk.process_and_validate_instruction(
  58. &allocate(&pubkey, space_too_large),
  59. &[(pubkey, Account::default())],
  60. &[Check::err(ProgramError::Custom(
  61. SystemError::InvalidAccountDataLength as u32,
  62. ))],
  63. );
  64. }
  65. // [TODO: CORE_BPF]: This verifies the concern for the `realloc` issue.
  66. #[test]
  67. fn fail_space_too_large_for_realloc() {
  68. let mollusk = setup::setup();
  69. let pubkey = Pubkey::new_unique();
  70. let space_too_large_for_realloc = MAX_PERMITTED_DATA_INCREASE + 1;
  71. mollusk.process_and_validate_instruction(
  72. &allocate(&pubkey, space_too_large_for_realloc as u64),
  73. &[(pubkey, Account::default())],
  74. &[Check::err(ProgramError::InvalidRealloc)], // See...?
  75. );
  76. }
  77. #[test]
  78. fn success() {
  79. let mollusk = setup::setup();
  80. let pubkey = Pubkey::new_unique();
  81. mollusk.process_and_validate_instruction(
  82. &allocate(&pubkey, SPACE),
  83. &[(pubkey, Account::default())],
  84. &[
  85. Check::success(),
  86. Check::account(&pubkey).space(SPACE as usize).build(),
  87. ],
  88. );
  89. }