solana/programs/bpf/tests/programs.rs

408 lines
17 KiB
Rust
Raw Normal View History

#[cfg(any(feature = "bpf_c", feature = "bpf_rust"))]
2019-03-02 21:33:02 -08:00
mod bpf {
2020-01-28 17:03:37 -08:00
use solana_runtime::{
bank::Bank,
bank_client::BankClient,
genesis_utils::{create_genesis_config, GenesisConfigInfo},
loader_utils::load_program,
};
2020-03-06 18:18:01 -08:00
use solana_sdk::{bpf_loader, pubkey::Pubkey, signature::Keypair};
use std::{env, fs::File, io::Read, path::PathBuf};
2019-03-02 21:33:02 -08:00
/// BPF program file extension
const PLATFORM_FILE_EXTENSION_BPF: &str = "so";
2019-03-02 21:33:02 -08:00
/// Create a BPF program file name
fn create_bpf_path(name: &str) -> PathBuf {
let mut pathbuf = {
let current_exe = env::current_exe().unwrap();
PathBuf::from(current_exe.parent().unwrap().parent().unwrap())
};
pathbuf.push("bpf/");
pathbuf.push(name);
pathbuf.set_extension(PLATFORM_FILE_EXTENSION_BPF);
pathbuf
}
2020-03-06 18:18:01 -08:00
fn load_bpf_program(bank_client: &BankClient, payer_keypair: &Keypair, name: &str) -> Pubkey {
let path = create_bpf_path(name);
println!("path {:?}", path);
let mut file = File::open(path).unwrap();
let mut elf = Vec::new();
file.read_to_end(&mut elf).unwrap();
load_program(bank_client, payer_keypair, &bpf_loader::id(), elf)
}
2019-03-02 21:33:02 -08:00
#[cfg(feature = "bpf_c")]
mod bpf_c {
use super::*;
use solana_runtime::loader_utils::create_invoke_instruction;
2020-01-28 17:03:37 -08:00
use solana_sdk::{
account::Account,
client::SyncClient,
instruction::{AccountMeta, Instruction, InstructionError},
2020-01-28 17:03:37 -08:00
pubkey::Pubkey,
signature::Signer,
transaction::TransactionError,
2020-01-28 17:03:37 -08:00
};
2020-03-06 18:18:01 -08:00
use std::sync::Arc;
2019-03-02 21:33:02 -08:00
#[test]
fn test_program_bpf_c() {
solana_logger::setup();
let programs = [
("bpf_to_bpf", true),
("multiple_static", true),
("noop", true),
("noop++", true),
("panic", false),
("relative_call", true),
("struct_pass", true),
("struct_ret", true),
2019-03-02 21:33:02 -08:00
];
for program in programs.iter() {
println!("Test program: {:?}", program.0);
2019-03-02 21:33:02 -08:00
let GenesisConfigInfo {
genesis_config,
mint_keypair,
..
} = create_genesis_config(50);
let bank = Bank::new(&genesis_config);
let bank_client = BankClient::new(bank);
2019-03-02 21:33:02 -08:00
// Call user program
2020-03-06 18:18:01 -08:00
let program_id = load_bpf_program(&bank_client, &mint_keypair, program.0);
let instruction =
create_invoke_instruction(mint_keypair.pubkey(), program_id, &1u8);
let result = bank_client.send_instruction(&mint_keypair, instruction);
if program.1 {
assert!(result.is_ok());
} else {
assert!(result.is_err());
}
2019-03-02 21:33:02 -08:00
}
}
#[test]
fn test_program_bpf_c_duplicate_accounts() {
solana_logger::setup();
let GenesisConfigInfo {
genesis_config,
mint_keypair,
..
} = create_genesis_config(50);
let bank = Arc::new(Bank::new(&genesis_config));
let bank_client = BankClient::new_shared(&bank);
2020-03-06 18:18:01 -08:00
let program_id = load_bpf_program(&bank_client, &mint_keypair, "dup_accounts");
let payee_account = Account::new(10, 1, &program_id);
let payee_pubkey = Pubkey::new_rand();
bank.store_account(&payee_pubkey, &payee_account);
let account = Account::new(10, 1, &program_id);
let pubkey = Pubkey::new_rand();
let account_metas = vec![
AccountMeta::new(mint_keypair.pubkey(), true),
AccountMeta::new(payee_pubkey, false),
AccountMeta::new(pubkey, false),
AccountMeta::new(pubkey, false),
];
bank.store_account(&pubkey, &account);
let instruction = Instruction::new(program_id, &1u8, account_metas.clone());
let result = bank_client.send_instruction(&mint_keypair, instruction);
let data = bank_client.get_account_data(&pubkey).unwrap().unwrap();
assert!(result.is_ok());
assert_eq!(data[0], 1);
bank.store_account(&pubkey, &account);
let instruction = Instruction::new(program_id, &2u8, account_metas.clone());
let result = bank_client.send_instruction(&mint_keypair, instruction);
let data = bank_client.get_account_data(&pubkey).unwrap().unwrap();
assert!(result.is_ok());
assert_eq!(data[0], 2);
bank.store_account(&pubkey, &account);
let instruction = Instruction::new(program_id, &3u8, account_metas.clone());
let result = bank_client.send_instruction(&mint_keypair, instruction);
let data = bank_client.get_account_data(&pubkey).unwrap().unwrap();
assert!(result.is_ok());
assert_eq!(data[0], 3);
bank.store_account(&pubkey, &account);
let instruction = Instruction::new(program_id, &4u8, account_metas.clone());
let result = bank_client.send_instruction(&mint_keypair, instruction);
let lamports = bank_client.get_balance(&pubkey).unwrap();
assert!(result.is_ok());
assert_eq!(lamports, 11);
bank.store_account(&pubkey, &account);
let instruction = Instruction::new(program_id, &5u8, account_metas.clone());
let result = bank_client.send_instruction(&mint_keypair, instruction);
let lamports = bank_client.get_balance(&pubkey).unwrap();
assert!(result.is_ok());
assert_eq!(lamports, 12);
bank.store_account(&pubkey, &account);
let instruction = Instruction::new(program_id, &6u8, account_metas.clone());
let result = bank_client.send_instruction(&mint_keypair, instruction);
let lamports = bank_client.get_balance(&pubkey).unwrap();
assert!(result.is_ok());
assert_eq!(lamports, 13);
}
#[test]
fn test_program_bpf_c_error_handling() {
solana_logger::setup();
let GenesisConfigInfo {
genesis_config,
mint_keypair,
..
} = create_genesis_config(50);
let bank = Bank::new(&genesis_config);
let bank_client = BankClient::new(bank);
2020-03-06 18:18:01 -08:00
let program_id = load_bpf_program(&bank_client, &mint_keypair, "error_handling");
let account_metas = vec![AccountMeta::new(mint_keypair.pubkey(), true)];
let instruction = Instruction::new(program_id, &1u8, account_metas.clone());
let result = bank_client.send_instruction(&mint_keypair, instruction);
assert!(result.is_ok());
let instruction = Instruction::new(program_id, &2u8, account_metas.clone());
let result = bank_client.send_instruction(&mint_keypair, instruction);
assert_eq!(
result.unwrap_err().unwrap(),
TransactionError::InstructionError(0, InstructionError::InvalidAccountData)
);
let instruction = Instruction::new(program_id, &3u8, account_metas.clone());
let result = bank_client.send_instruction(&mint_keypair, instruction);
assert_eq!(
result.unwrap_err().unwrap(),
TransactionError::InstructionError(0, InstructionError::CustomError(42))
);
let instruction = Instruction::new(program_id, &4u8, account_metas.clone());
let result = bank_client.send_instruction(&mint_keypair, instruction);
assert_eq!(
result.unwrap_err().unwrap(),
TransactionError::InstructionError(0, InstructionError::InvalidError)
);
let instruction = Instruction::new(program_id, &5u8, account_metas.clone());
let result = bank_client.send_instruction(&mint_keypair, instruction);
assert_eq!(
result.unwrap_err().unwrap(),
TransactionError::InstructionError(0, InstructionError::InvalidError)
);
let instruction = Instruction::new(program_id, &6u8, account_metas.clone());
let result = bank_client.send_instruction(&mint_keypair, instruction);
assert_eq!(
result.unwrap_err().unwrap(),
TransactionError::InstructionError(0, InstructionError::InvalidInstructionData)
);
}
}
2019-03-02 21:33:02 -08:00
#[cfg(feature = "bpf_rust")]
mod bpf_rust {
use super::*;
2020-01-28 17:03:37 -08:00
use solana_sdk::{
account::Account,
client::SyncClient,
clock::DEFAULT_SLOTS_PER_EPOCH,
instruction::{AccountMeta, Instruction, InstructionError},
2020-01-28 17:03:37 -08:00
pubkey::Pubkey,
signature::{Keypair, Signer},
2020-01-28 17:03:37 -08:00
sysvar::{clock, fees, rent, rewards, slot_hashes, stake_history},
transaction::TransactionError,
2020-01-28 17:03:37 -08:00
};
2019-09-09 10:55:35 -07:00
use std::sync::Arc;
2019-03-02 21:33:02 -08:00
#[test]
fn test_program_bpf_rust() {
solana_logger::setup();
2019-05-21 11:22:33 -07:00
let programs = [
("solana_bpf_rust_128bit", true),
2019-06-10 11:00:15 -07:00
("solana_bpf_rust_alloc", true),
2019-06-20 16:07:12 -07:00
("solana_bpf_rust_dep_crate", true),
2019-09-09 10:55:35 -07:00
("solana_bpf_rust_external_spend", false),
2019-06-10 11:00:15 -07:00
("solana_bpf_rust_iter", true),
("solana_bpf_rust_many_args", true),
2019-06-10 11:00:15 -07:00
("solana_bpf_rust_noop", true),
("solana_bpf_rust_panic", false),
("solana_bpf_rust_param_passing", true),
("solana_bpf_rust_sysval", true),
2019-05-21 11:22:33 -07:00
];
2019-03-02 21:33:02 -08:00
for program in programs.iter() {
2020-03-06 18:18:01 -08:00
println!("Test program: {:?}", program.0);
2019-03-02 21:33:02 -08:00
let GenesisConfigInfo {
genesis_config,
mint_keypair,
..
} = create_genesis_config(50);
let bank = Arc::new(Bank::new(&genesis_config));
// Create bank with specific slot, used by solana_bpf_rust_sysvar test
let bank =
Bank::new_from_parent(&bank, &Pubkey::default(), DEFAULT_SLOTS_PER_EPOCH + 1);
let bank_client = BankClient::new(bank);
2019-03-21 07:14:14 -07:00
2019-03-02 21:33:02 -08:00
// Call user program
2020-03-06 18:18:01 -08:00
let program_id = load_bpf_program(&bank_client, &mint_keypair, program.0);
2019-05-21 11:22:33 -07:00
let account_metas = vec![
2019-06-10 11:00:15 -07:00
AccountMeta::new(mint_keypair.pubkey(), true),
2019-05-21 11:22:33 -07:00
AccountMeta::new(Keypair::new().pubkey(), false),
2019-09-09 10:55:35 -07:00
AccountMeta::new(clock::id(), false),
AccountMeta::new(fees::id(), false),
AccountMeta::new(rewards::id(), false),
AccountMeta::new(slot_hashes::id(), false),
AccountMeta::new(stake_history::id(), false),
AccountMeta::new(rent::id(), false),
2019-05-21 11:22:33 -07:00
];
let instruction = Instruction::new(program_id, &1u8, account_metas);
let result = bank_client.send_instruction(&mint_keypair, instruction);
2019-06-10 11:00:15 -07:00
if program.1 {
assert!(result.is_ok());
} else {
assert!(result.is_err());
}
2019-03-02 21:33:02 -08:00
}
}
#[test]
fn test_program_bpf_rust_duplicate_accounts() {
solana_logger::setup();
let GenesisConfigInfo {
genesis_config,
mint_keypair,
..
} = create_genesis_config(50);
let bank = Arc::new(Bank::new(&genesis_config));
let bank_client = BankClient::new_shared(&bank);
2020-03-06 18:18:01 -08:00
let program_id =
load_bpf_program(&bank_client, &mint_keypair, "solana_bpf_rust_dup_accounts");
let payee_account = Account::new(10, 1, &program_id);
let payee_pubkey = Pubkey::new_rand();
bank.store_account(&payee_pubkey, &payee_account);
let account = Account::new(10, 1, &program_id);
let pubkey = Pubkey::new_rand();
let account_metas = vec![
AccountMeta::new(mint_keypair.pubkey(), true),
AccountMeta::new(payee_pubkey, false),
AccountMeta::new(pubkey, false),
AccountMeta::new(pubkey, false),
];
bank.store_account(&pubkey, &account);
let instruction = Instruction::new(program_id, &1u8, account_metas.clone());
let result = bank_client.send_instruction(&mint_keypair, instruction);
let data = bank_client.get_account_data(&pubkey).unwrap().unwrap();
assert!(result.is_ok());
assert_eq!(data[0], 1);
bank.store_account(&pubkey, &account);
let instruction = Instruction::new(program_id, &2u8, account_metas.clone());
let result = bank_client.send_instruction(&mint_keypair, instruction);
let data = bank_client.get_account_data(&pubkey).unwrap().unwrap();
assert!(result.is_ok());
assert_eq!(data[0], 2);
bank.store_account(&pubkey, &account);
let instruction = Instruction::new(program_id, &3u8, account_metas.clone());
let result = bank_client.send_instruction(&mint_keypair, instruction);
let data = bank_client.get_account_data(&pubkey).unwrap().unwrap();
assert!(result.is_ok());
assert_eq!(data[0], 3);
bank.store_account(&pubkey, &account);
let instruction = Instruction::new(program_id, &4u8, account_metas.clone());
let result = bank_client.send_instruction(&mint_keypair, instruction);
let lamports = bank_client.get_balance(&pubkey).unwrap();
assert!(result.is_ok());
assert_eq!(lamports, 11);
bank.store_account(&pubkey, &account);
let instruction = Instruction::new(program_id, &5u8, account_metas.clone());
let result = bank_client.send_instruction(&mint_keypair, instruction);
let lamports = bank_client.get_balance(&pubkey).unwrap();
assert!(result.is_ok());
assert_eq!(lamports, 12);
bank.store_account(&pubkey, &account);
let instruction = Instruction::new(program_id, &6u8, account_metas.clone());
let result = bank_client.send_instruction(&mint_keypair, instruction);
let lamports = bank_client.get_balance(&pubkey).unwrap();
assert!(result.is_ok());
assert_eq!(lamports, 13);
}
#[test]
fn test_program_bpf_rust_error_handling() {
solana_logger::setup();
let GenesisConfigInfo {
genesis_config,
mint_keypair,
..
} = create_genesis_config(50);
let bank = Bank::new(&genesis_config);
let bank_client = BankClient::new(bank);
2020-03-06 18:18:01 -08:00
let program_id = load_bpf_program(
&bank_client,
&mint_keypair,
"solana_bpf_rust_error_handling",
);
let account_metas = vec![AccountMeta::new(mint_keypair.pubkey(), true)];
let instruction = Instruction::new(program_id, &1u8, account_metas.clone());
let result = bank_client.send_instruction(&mint_keypair, instruction);
assert!(result.is_ok());
let instruction = Instruction::new(program_id, &2u8, account_metas.clone());
let result = bank_client.send_instruction(&mint_keypair, instruction);
assert_eq!(
result.unwrap_err().unwrap(),
TransactionError::InstructionError(0, InstructionError::InvalidAccountData)
);
let instruction = Instruction::new(program_id, &3u8, account_metas.clone());
let result = bank_client.send_instruction(&mint_keypair, instruction);
assert_eq!(
result.unwrap_err().unwrap(),
TransactionError::InstructionError(0, InstructionError::CustomError(0))
);
let instruction = Instruction::new(program_id, &4u8, account_metas.clone());
let result = bank_client.send_instruction(&mint_keypair, instruction);
assert_eq!(
result.unwrap_err().unwrap(),
TransactionError::InstructionError(0, InstructionError::CustomError(42))
);
let instruction = Instruction::new(program_id, &6u8, account_metas.clone());
let result = bank_client.send_instruction(&mint_keypair, instruction);
assert_eq!(
result.unwrap_err().unwrap(),
TransactionError::InstructionError(0, InstructionError::AccountBorrowFailed)
);
let instruction = Instruction::new(program_id, &7u8, account_metas.clone());
let result = bank_client.send_instruction(&mint_keypair, instruction);
assert_eq!(
result.unwrap_err().unwrap(),
TransactionError::InstructionError(0, InstructionError::InvalidInstructionData)
);
}
}
}