From 1a70a2a25b2f1966e943a293c657df8f26cc2f2f Mon Sep 17 00:00:00 2001 From: Michael Vines Date: Fri, 6 Nov 2020 09:05:50 -0800 Subject: [PATCH] cargo-test-bpf now sets the "test-bpf" feature for crate tests The feature allows for tests to distinguish between `cargo test` and `cargo test-bpf` primarily for the purpose of excluding CPI tests that require the system program under `cargo test`, as the path to enabling CPI in `cargo test`-based testing is unclear --- program-test/src/lib.rs | 22 ++++++++++++++++------ sdk/cargo-test-bpf/src/main.rs | 16 ++++++++++++++++ 2 files changed, 32 insertions(+), 6 deletions(-) diff --git a/program-test/src/lib.rs b/program-test/src/lib.rs index 46e05fa70..9a3bf95f3 100644 --- a/program-test/src/lib.rs +++ b/program-test/src/lib.rs @@ -116,12 +116,12 @@ pub fn builtin_process_instruction( }) .collect(); - // Execute the BPF entrypoint + // Execute the program let result = process_instruction(program_id, &account_infos, input).map_err(to_instruction_error); if result.is_ok() { - // Commit changes to the KeyedAccounts + // Commit AccountInfo changes back into KeyedAccounts for keyed_account in keyed_accounts { let mut account = keyed_account.account.borrow_mut(); let key = keyed_account.unsigned_key(); @@ -195,7 +195,7 @@ impl program_stubs::SyscallStubs for SyscallStubs { signers_seeds: &[&[&[u8]]], ) -> ProgramResult { // - // TODO: Merge the business logic between here and the BPF invoke path in + // TODO: Merge the business logic below with the BPF invoke path in // programs/bpf_loader/src/syscalls.rs // info!("SyscallStubs::sol_invoke_signed()"); @@ -305,10 +305,20 @@ impl program_stubs::SyscallStubs for SyscallStubs { let mut data = account_info.try_borrow_mut_data()?; let new_data = &account.borrow().data; - if data.len() != new_data.len() { - // TODO: Figure out how to change the callers account data size + if *account_info.owner != account.borrow().owner { + // TODO: Figure out how to allow the System Program to change the account owner panic!( - "Account resizing ({} -> {}) not supported yet", + "Account ownership change not supported yet: {} -> {}. \ + Consider making this test conditional on `#[cfg(feature = \"test-bpf\")]`", + *account_info.owner, + account.borrow().owner + ); + } + if data.len() != new_data.len() { + // TODO: Figure out how to allow the System Program to resize the account data + panic!( + "Account data resizing not supported yet: {} -> {}. \ + Consider making this test conditional on `#[cfg(feature = \"test-bpf\")]`", data.len(), new_data.len() ); diff --git a/sdk/cargo-test-bpf/src/main.rs b/sdk/cargo-test-bpf/src/main.rs index 405bd6c0f..991406e33 100644 --- a/sdk/cargo-test-bpf/src/main.rs +++ b/sdk/cargo-test-bpf/src/main.rs @@ -74,6 +74,15 @@ fn test_bpf(config: Config) { exit(1); }); + let root_package = metadata.root_package().unwrap_or_else(|| { + eprintln!( + "Workspace does not have a root package: {}", + metadata.workspace_root.display() + ); + exit(1); + }); + let set_test_bpf_feature = root_package.features.contains_key("test-bpf"); + let bpf_out_dir = config .bpf_out_dir .unwrap_or_else(|| format!("{}", metadata.target_directory.join("deploy").display())); @@ -108,6 +117,13 @@ fn test_bpf(config: Config) { env::set_var("BPF_OUT_DIR", bpf_out_dir); cargo_args.insert(0, "test"); + + // If the program crate declares the "test-bpf" feature, pass it along to the tests so they can + // distinguish between `cargo test` and `cargo test-bpf` + if set_test_bpf_feature { + cargo_args.push("--features"); + cargo_args.push("test-bpf"); + } for extra_cargo_test_arg in &config.extra_cargo_test_args { cargo_args.push(&extra_cargo_test_arg); }