|
| 1 | +use std::mem::size_of; |
| 2 | + |
| 3 | +use solana_program::account_info::AccountInfo; |
| 4 | +use solana_program::clock::Epoch; |
| 5 | +use solana_program::native_token::LAMPORTS_PER_SOL; |
| 6 | +use solana_program::program_error::ProgramError; |
| 7 | +use solana_program::pubkey::Pubkey; |
| 8 | +use solana_program::rent::Rent; |
| 9 | +use solana_program::system_program; |
| 10 | + |
| 11 | +use crate::c_oracle_header::{ |
| 12 | + cmd_set_min_pub_t, |
| 13 | + command_t_e_cmd_set_min_pub, |
| 14 | + pc_price_t, |
| 15 | + PC_VERSION, |
| 16 | +}; |
| 17 | +use crate::deserialize::load_mut; |
| 18 | +use crate::rust_oracle::{ |
| 19 | + initialize_checked, |
| 20 | + load_checked, |
| 21 | + set_min_pub, |
| 22 | +}; |
| 23 | + |
| 24 | +#[test] |
| 25 | +fn test_set_min_pub() { |
| 26 | + let mut instruction_data = [0u8; size_of::<cmd_set_min_pub_t>()]; |
| 27 | + |
| 28 | + let program_id = Pubkey::new_unique(); |
| 29 | + let funding_key = Pubkey::new_unique(); |
| 30 | + let price_key = Pubkey::new_unique(); |
| 31 | + |
| 32 | + let system_program = system_program::id(); |
| 33 | + let mut funding_balance = LAMPORTS_PER_SOL.clone(); |
| 34 | + let funding_account = AccountInfo::new( |
| 35 | + &funding_key, |
| 36 | + true, |
| 37 | + true, |
| 38 | + &mut funding_balance, |
| 39 | + &mut [], |
| 40 | + &system_program, |
| 41 | + false, |
| 42 | + Epoch::default(), |
| 43 | + ); |
| 44 | + |
| 45 | + let mut price_balance = Rent::minimum_balance(&Rent::default(), size_of::<pc_price_t>()); |
| 46 | + let mut price_raw_data = [0u8; size_of::<pc_price_t>()]; |
| 47 | + let price_account = AccountInfo::new( |
| 48 | + &price_key, |
| 49 | + true, |
| 50 | + true, |
| 51 | + &mut price_balance, |
| 52 | + &mut price_raw_data, |
| 53 | + &program_id, |
| 54 | + false, |
| 55 | + Epoch::default(), |
| 56 | + ); |
| 57 | + |
| 58 | + initialize_checked::<pc_price_t>(&price_account, PC_VERSION).unwrap(); |
| 59 | + assert_eq!(get_min_pub(&price_account), Ok(0)); |
| 60 | + |
| 61 | + populate_instruction(&mut instruction_data, 10); |
| 62 | + assert!(set_min_pub( |
| 63 | + &program_id, |
| 64 | + &[funding_account.clone(), price_account.clone()], |
| 65 | + &instruction_data |
| 66 | + ) |
| 67 | + .is_ok()); |
| 68 | + assert_eq!(get_min_pub(&price_account), Ok(10)); |
| 69 | + |
| 70 | + populate_instruction(&mut instruction_data, 2); |
| 71 | + assert!(set_min_pub( |
| 72 | + &program_id, |
| 73 | + &[funding_account.clone(), price_account.clone()], |
| 74 | + &instruction_data |
| 75 | + ) |
| 76 | + .is_ok()); |
| 77 | + assert_eq!(get_min_pub(&price_account), Ok(2)); |
| 78 | +} |
| 79 | + |
| 80 | +// Create an upd_product instruction that sets the product metadata to strings |
| 81 | +fn populate_instruction(instruction_data: &mut [u8], min_pub: u8) -> () { |
| 82 | + let mut hdr = load_mut::<cmd_set_min_pub_t>(instruction_data).unwrap(); |
| 83 | + hdr.ver_ = PC_VERSION; |
| 84 | + hdr.cmd_ = command_t_e_cmd_set_min_pub as i32; |
| 85 | + hdr.min_pub_ = min_pub; |
| 86 | +} |
| 87 | + |
| 88 | +fn get_min_pub(account: &AccountInfo) -> Result<u8, ProgramError> { |
| 89 | + Ok(load_checked::<pc_price_t>(account, PC_VERSION)?.min_pub_) |
| 90 | +} |
0 commit comments