From 202adb1bf14faf8ae94e3163975e0fd630bf472e Mon Sep 17 00:00:00 2001 From: Tyera Eulberg Date: Tue, 5 Mar 2019 00:14:51 -0700 Subject: [PATCH] Create failing test --- Cargo.lock | 1 + programs/system/Cargo.toml | 4 +- programs/system/tests/system.rs | 104 ++++++++++++++++++++++++++++++++ 3 files changed, 108 insertions(+), 1 deletion(-) create mode 100644 programs/system/tests/system.rs diff --git a/Cargo.lock b/Cargo.lock index dad92f14e..b3a4c7a88 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2316,6 +2316,7 @@ dependencies = [ "bincode 1.1.2 (registry+https://github.com/rust-lang/crates.io-index)", "log 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)", "serde 1.0.89 (registry+https://github.com/rust-lang/crates.io-index)", + "solana-runtime 0.12.0", "solana-sdk 0.12.0", ] diff --git a/programs/system/Cargo.toml b/programs/system/Cargo.toml index 4546089e2..91a9af2f6 100644 --- a/programs/system/Cargo.toml +++ b/programs/system/Cargo.toml @@ -14,6 +14,9 @@ log = "0.4.2" serde = "1.0.89" solana-sdk = { path = "../../sdk", version = "0.12.0" } +[dev-dependencies] +solana-runtime = { path = "../../runtime", version = "0.12.0" } + [lib] name = "solana_system_program" @@ -21,4 +24,3 @@ name = "solana_system_program" # allocates Rust memory. # cc: https://github.com/solana-labs/solana/issues/2004#issuecomment-444570081 crate-type = ["lib"] - diff --git a/programs/system/tests/system.rs b/programs/system/tests/system.rs new file mode 100644 index 000000000..1379c7d7e --- /dev/null +++ b/programs/system/tests/system.rs @@ -0,0 +1,104 @@ +use solana_runtime::bank::{Bank, Result}; +use solana_sdk::genesis_block::GenesisBlock; +use solana_sdk::pubkey::Pubkey; +use solana_sdk::signature::{Keypair, KeypairUtil}; +use solana_sdk::system_program; +use solana_sdk::system_transaction::SystemTransaction; + +struct SystemBank<'a> { + bank: &'a Bank, +} + +impl<'a> SystemBank<'a> { + fn new(bank: &'a Bank) -> Self { + bank.add_native_program("solana_system_program", &system_program::id()); + Self { bank } + } + fn create_account(&self, from_keypair: &Keypair, to: Pubkey, lamports: u64) -> Result<()> { + let blockhash = self.bank.last_blockhash(); + let tx = SystemTransaction::new_account(from_keypair, to, lamports, blockhash, 0); + self.bank.process_transaction(&tx) + } + fn move_lamports(&self, from_keypair: &Keypair, to: Pubkey, lamports: u64) -> Result<()> { + let blockhash = self.bank.last_blockhash(); + let tx = SystemTransaction::new_move(from_keypair, to, lamports, blockhash, 0); + self.bank.process_transaction(&tx) + } +} + +#[test] +fn test_create_cannot_overwrite_used_account() { + let (genesis_block, from_keypair) = GenesisBlock::new(10_000); + let bank = Bank::new(&genesis_block); + let system_bank = SystemBank::new(&bank); + + // create_account on uninitialized account should work + let system_account = Keypair::new().pubkey(); + system_bank + .create_account(&from_keypair, system_account, 100) + .unwrap(); + assert_eq!(system_bank.bank.get_balance(&system_account), 100); + + // Create an account assigned to another program + let other_account = Keypair::new().pubkey(); + let program_id = Pubkey::new(&[9; 32]); + let tx = SystemTransaction::new_program_account( + &from_keypair, + other_account, + system_bank.bank.last_blockhash(), + 1, + 0, + program_id, + 0, + ); + system_bank.bank.process_transaction(&tx).unwrap(); + assert_eq!(system_bank.bank.get_balance(&other_account), 1); + assert_eq!( + system_bank.bank.get_account(&other_account).unwrap().owner, + program_id + ); + + // create_account on an initialized account should fail + system_bank + .create_account(&from_keypair, other_account, 100) + .unwrap(); + assert_eq!(system_bank.bank.get_balance(&other_account), 1); + assert_eq!( + system_bank.bank.get_account(&other_account).unwrap().owner, + program_id + ); +} +#[test] +fn test_move_can_fund_used_account() { + let (genesis_block, from_keypair) = GenesisBlock::new(10_000); + let bank = Bank::new(&genesis_block); + let system_bank = SystemBank::new(&bank); + + // Create an account assigned to another program + let other_account = Keypair::new().pubkey(); + let program_id = Pubkey::new(&[9; 32]); + let tx = SystemTransaction::new_program_account( + &from_keypair, + other_account, + system_bank.bank.last_blockhash(), + 1, + 0, + program_id, + 0, + ); + system_bank.bank.process_transaction(&tx).unwrap(); + assert_eq!(system_bank.bank.get_balance(&other_account), 1); + assert_eq!( + system_bank.bank.get_account(&other_account).unwrap().owner, + program_id + ); + + system_bank + .move_lamports(&from_keypair, other_account, 100) + .unwrap(); + assert_eq!(system_bank.bank.get_balance(&other_account), 101); + assert_eq!( + system_bank.bank.get_account(&other_account).unwrap().owner, + program_id + ); +}