diff --git a/sdk/program/src/sysvar/mod.rs b/sdk/program/src/sysvar/mod.rs index f96c456d8c..39af3b2fc8 100644 --- a/sdk/program/src/sysvar/mod.rs +++ b/sdk/program/src/sysvar/mod.rs @@ -94,7 +94,7 @@ mod tests { #[repr(C)] #[derive(Serialize, Deserialize, Debug, Default, PartialEq)] - pub struct TestSysvar { + struct TestSysvar { something: Pubkey, } crate::declare_id!("TestSysvar111111111111111111111111111111111"); @@ -138,31 +138,4 @@ mod tests { account_info.data = Rc::new(RefCell::new(&mut small_data)); assert_eq!(test_sysvar.to_account_info(&mut account_info), None); } - - #[test] - fn test_sysvar_keyed_account_to_from() { - let test_sysvar = TestSysvar::default(); - let key = crate::sysvar::tests::id(); - let wrong_key = Pubkey::new_unique(); - - let account = test_sysvar.create_account(42); - let test_sysvar = TestSysvar::from_account(&account).unwrap(); - assert_eq!(test_sysvar, TestSysvar::default()); - - let mut account = Account::new(42, TestSysvar::size_of(), &key); - test_sysvar.to_account(&mut account).unwrap(); - let test_sysvar = TestSysvar::from_account(&account).unwrap(); - assert_eq!(test_sysvar, TestSysvar::default()); - - let account = RefCell::new(account); - let keyed_account = KeyedAccount::new(&key, false, &account); - let new_test_sysvar = TestSysvar::from_keyed_account(&keyed_account).unwrap(); - assert_eq!(test_sysvar, new_test_sysvar); - - let keyed_account = KeyedAccount::new(&wrong_key, false, &account); - assert_eq!( - TestSysvar::from_keyed_account(&keyed_account), - Err(InstructionError::InvalidArgument) - ); - } } diff --git a/sdk/src/keyed_account.rs b/sdk/src/keyed_account.rs index 67f7141ef1..daa784841e 100644 --- a/sdk/src/keyed_account.rs +++ b/sdk/src/keyed_account.rs @@ -217,3 +217,50 @@ pub fn from_keyed_account( } S::from_account(&*keyed_account.try_account_ref()?).ok_or(InstructionError::InvalidArgument) } + +#[cfg(test)] +mod tests { + use super::*; + use crate::pubkey::Pubkey; + use std::cell::RefCell; + + #[repr(C)] + #[derive(Serialize, Deserialize, Debug, Default, PartialEq)] + struct TestSysvar { + something: Pubkey, + } + crate::declare_id!("TestSysvar111111111111111111111111111111111"); + impl solana_program::sysvar::SysvarId for TestSysvar { + fn check_id(pubkey: &crate::pubkey::Pubkey) -> bool { + check_id(pubkey) + } + } + impl Sysvar for TestSysvar {} + + #[test] + fn test_sysvar_keyed_account_to_from() { + let test_sysvar = TestSysvar::default(); + let key = crate::keyed_account::tests::id(); + let wrong_key = Pubkey::new_unique(); + + let account = test_sysvar.create_account(42); + let test_sysvar = TestSysvar::from_account(&account).unwrap(); + assert_eq!(test_sysvar, TestSysvar::default()); + + let mut account = Account::new(42, TestSysvar::size_of(), &key); + test_sysvar.to_account(&mut account).unwrap(); + let test_sysvar = TestSysvar::from_account(&account).unwrap(); + assert_eq!(test_sysvar, TestSysvar::default()); + + let account = RefCell::new(account); + let keyed_account = KeyedAccount::new(&key, false, &account); + let new_test_sysvar = from_keyed_account::(&keyed_account).unwrap(); + assert_eq!(test_sysvar, new_test_sysvar); + + let keyed_account = KeyedAccount::new(&wrong_key, false, &account); + assert_eq!( + from_keyed_account::(&keyed_account), + Err(InstructionError::InvalidArgument) + ); + } +}