|
| 1 | +#[cfg(test)] |
| 2 | +mod test { |
| 3 | + use crate::c_oracle_header::{ |
| 4 | + cmd_hdr_t, |
| 5 | + command_t_e_cmd_init_mapping, |
| 6 | + pc_map_table_t, |
| 7 | + PC_ACCTYPE_MAPPING, |
| 8 | + PC_MAGIC, |
| 9 | + PC_VERSION, |
| 10 | + }; |
| 11 | + use crate::rust_oracle::{ |
| 12 | + clear_account, |
| 13 | + init_mapping, |
| 14 | + load_account_as, |
| 15 | + }; |
| 16 | + use bytemuck::bytes_of; |
| 17 | + use solana_program::account_info::AccountInfo; |
| 18 | + use solana_program::clock::Epoch; |
| 19 | + use solana_program::native_token::LAMPORTS_PER_SOL; |
| 20 | + use solana_program::program_error::ProgramError; |
| 21 | + use solana_program::pubkey::Pubkey; |
| 22 | + use solana_program::rent::Rent; |
| 23 | + use solana_program::system_program; |
| 24 | + use std::cell::RefCell; |
| 25 | + use std::mem::size_of; |
| 26 | + use std::rc::Rc; |
| 27 | + |
| 28 | + #[test] |
| 29 | + fn test_init_mapping() { |
| 30 | + let hdr: cmd_hdr_t = cmd_hdr_t { |
| 31 | + ver_: PC_VERSION, |
| 32 | + cmd_: command_t_e_cmd_init_mapping as i32, |
| 33 | + }; |
| 34 | + let instruction_data = bytes_of::<cmd_hdr_t>(&hdr); |
| 35 | + |
| 36 | + let program_id = Pubkey::new_unique(); |
| 37 | + let program_id_2 = Pubkey::new_unique(); |
| 38 | + let funding_key = Pubkey::new_unique(); |
| 39 | + let mapping_key = Pubkey::new_unique(); |
| 40 | + let system_program = system_program::id(); |
| 41 | + |
| 42 | + let mut funding_balance = LAMPORTS_PER_SOL.clone(); |
| 43 | + let mut funding_account = AccountInfo::new( |
| 44 | + &funding_key, |
| 45 | + true, |
| 46 | + true, |
| 47 | + &mut funding_balance, |
| 48 | + &mut [], |
| 49 | + &system_program, |
| 50 | + false, |
| 51 | + Epoch::default(), |
| 52 | + ); |
| 53 | + |
| 54 | + let mut mapping_balance = |
| 55 | + Rent::minimum_balance(&Rent::default(), size_of::<pc_map_table_t>()); |
| 56 | + let mut mapping_raw_data = [0u8; size_of::<pc_map_table_t>()]; |
| 57 | + |
| 58 | + let mut mapping_account = AccountInfo::new( |
| 59 | + &mapping_key, |
| 60 | + true, |
| 61 | + true, |
| 62 | + &mut mapping_balance, |
| 63 | + &mut mapping_raw_data, |
| 64 | + &program_id, |
| 65 | + false, |
| 66 | + Epoch::default(), |
| 67 | + ); |
| 68 | + |
| 69 | + assert!(init_mapping( |
| 70 | + &program_id, |
| 71 | + &[funding_account.clone(), mapping_account.clone()], |
| 72 | + instruction_data |
| 73 | + ) |
| 74 | + .is_ok()); |
| 75 | + |
| 76 | + { |
| 77 | + let mapping_data = load_account_as::<pc_map_table_t>(&mapping_account).unwrap(); |
| 78 | + |
| 79 | + assert_eq!(mapping_data.ver_, PC_VERSION); |
| 80 | + assert_eq!(mapping_data.magic_, PC_MAGIC); |
| 81 | + assert_eq!(mapping_data.type_, PC_ACCTYPE_MAPPING); |
| 82 | + assert_eq!(mapping_data.size_, 56); |
| 83 | + } |
| 84 | + |
| 85 | + assert_eq!( |
| 86 | + init_mapping( |
| 87 | + &program_id, |
| 88 | + &[funding_account.clone(), mapping_account.clone()], |
| 89 | + instruction_data |
| 90 | + ), |
| 91 | + Err(ProgramError::InvalidArgument) |
| 92 | + ); |
| 93 | + |
| 94 | + clear_account(&mapping_account).unwrap(); |
| 95 | + |
| 96 | + assert_eq!( |
| 97 | + init_mapping(&program_id, &[funding_account.clone()], instruction_data), |
| 98 | + Err(ProgramError::InvalidArgument) |
| 99 | + ); |
| 100 | + |
| 101 | + funding_account.is_signer = false; |
| 102 | + |
| 103 | + assert_eq!( |
| 104 | + init_mapping( |
| 105 | + &program_id, |
| 106 | + &[funding_account.clone(), mapping_account.clone()], |
| 107 | + instruction_data |
| 108 | + ), |
| 109 | + Err(ProgramError::InvalidArgument) |
| 110 | + ); |
| 111 | + |
| 112 | + funding_account.is_signer = true; |
| 113 | + mapping_account.is_signer = false; |
| 114 | + |
| 115 | + assert_eq!( |
| 116 | + init_mapping( |
| 117 | + &program_id, |
| 118 | + &[funding_account.clone(), mapping_account.clone()], |
| 119 | + instruction_data |
| 120 | + ), |
| 121 | + Err(ProgramError::InvalidArgument) |
| 122 | + ); |
| 123 | + |
| 124 | + mapping_account.is_signer = true; |
| 125 | + funding_account.is_writable = false; |
| 126 | + |
| 127 | + assert_eq!( |
| 128 | + init_mapping( |
| 129 | + &program_id, |
| 130 | + &[funding_account.clone(), mapping_account.clone()], |
| 131 | + instruction_data |
| 132 | + ), |
| 133 | + Err(ProgramError::InvalidArgument) |
| 134 | + ); |
| 135 | + |
| 136 | + funding_account.is_writable = true; |
| 137 | + mapping_account.is_writable = false; |
| 138 | + |
| 139 | + assert_eq!( |
| 140 | + init_mapping( |
| 141 | + &program_id, |
| 142 | + &[funding_account.clone(), mapping_account.clone()], |
| 143 | + instruction_data |
| 144 | + ), |
| 145 | + Err(ProgramError::InvalidArgument) |
| 146 | + ); |
| 147 | + |
| 148 | + mapping_account.is_writable = true; |
| 149 | + mapping_account.owner = &program_id_2; |
| 150 | + |
| 151 | + assert_eq!( |
| 152 | + init_mapping( |
| 153 | + &program_id, |
| 154 | + &[funding_account.clone(), mapping_account.clone()], |
| 155 | + instruction_data |
| 156 | + ), |
| 157 | + Err(ProgramError::InvalidArgument) |
| 158 | + ); |
| 159 | + |
| 160 | + mapping_account.owner = &program_id; |
| 161 | + let prev_data = mapping_account.data; |
| 162 | + mapping_account.data = Rc::new(RefCell::new(&mut [])); |
| 163 | + |
| 164 | + assert_eq!( |
| 165 | + init_mapping( |
| 166 | + &program_id, |
| 167 | + &[funding_account.clone(), mapping_account.clone()], |
| 168 | + instruction_data |
| 169 | + ), |
| 170 | + Err(ProgramError::InvalidArgument) |
| 171 | + ); |
| 172 | + |
| 173 | + mapping_account.data = prev_data; |
| 174 | + |
| 175 | + assert!(init_mapping( |
| 176 | + &program_id, |
| 177 | + &[funding_account.clone(), mapping_account.clone()], |
| 178 | + instruction_data |
| 179 | + ) |
| 180 | + .is_ok()); |
| 181 | + } |
| 182 | +} |
0 commit comments