From 7f00fbe138313b7088e8196fe14a0f647dcccd5f Mon Sep 17 00:00:00 2001 From: Perelyn <64838956+Perelyn-sama@users.noreply.github.com> Date: Wed, 29 Oct 2025 04:20:37 +0100 Subject: [PATCH] add rust test for cpi example --- .../native/programs/hand/Cargo.toml | 15 +++- .../native/programs/hand/tests/test.rs | 73 +++++++++++++++++++ .../native/programs/lever/Cargo.toml | 5 ++ 3 files changed, 92 insertions(+), 1 deletion(-) create mode 100644 basics/cross-program-invocation/native/programs/hand/tests/test.rs diff --git a/basics/cross-program-invocation/native/programs/hand/Cargo.toml b/basics/cross-program-invocation/native/programs/hand/Cargo.toml index 94935576..26bc992a 100644 --- a/basics/cross-program-invocation/native/programs/hand/Cargo.toml +++ b/basics/cross-program-invocation/native/programs/hand/Cargo.toml @@ -6,6 +6,8 @@ edition = "2021" [features] no-entrypoint = [] cpi = ["no-entrypoint"] +custom-heap = [] +custom-panic = [] [dependencies] borsh = "1.5.7" @@ -13,6 +15,17 @@ borsh-derive = "1.5.7" solana-program = "3.0" cross-program-invocatio-native-lever = { path = "../lever", features = ["cpi"] } - [lib] crate-type = ["cdylib", "lib"] + +[dev-dependencies] +litesvm = "0.8.1" +solana-instruction = "3.0.0" +solana-keypair = "3.0.1" +solana-native-token = "3.0.0" +solana-pubkey = "3.0.0" +solana-transaction = "3.0.1" +solana-system-interface = {version = "2.0.0", features = ["bincode"]} + +[lints.rust] +unexpected_cfgs = { level = "warn", check-cfg = ['cfg(target_os, values("solana"))'] } diff --git a/basics/cross-program-invocation/native/programs/hand/tests/test.rs b/basics/cross-program-invocation/native/programs/hand/tests/test.rs new file mode 100644 index 00000000..cd66ed90 --- /dev/null +++ b/basics/cross-program-invocation/native/programs/hand/tests/test.rs @@ -0,0 +1,73 @@ +use cross_program_invocatio_native_lever::{PowerStatus, SetPowerStatus}; +use litesvm::LiteSVM; +use solana_instruction::{AccountMeta, Instruction}; +use solana_keypair::{Keypair, Signer}; +use solana_native_token::LAMPORTS_PER_SOL; +use solana_pubkey::Pubkey; +use solana_transaction::Transaction; + +#[test] +fn test_cpi() { + let hand_program_id = Pubkey::new_unique(); + let lever_program_id = Pubkey::new_unique(); + let hand_program_bytes = + include_bytes!("../../../target/deploy/cross_program_invocatio_native_hand.so"); + let lever_program_bytes = + include_bytes!("../../../target/deploy/cross_program_invocatio_native_lever.so"); + + let payer = Keypair::new(); + let power_account = Keypair::new(); + + let mut svm = LiteSVM::new(); + + svm.add_program(hand_program_id, hand_program_bytes) + .unwrap(); + svm.add_program(lever_program_id, lever_program_bytes) + .unwrap(); + + svm.airdrop(&payer.pubkey(), LAMPORTS_PER_SOL * 10).unwrap(); + + let data = borsh::to_vec(&PowerStatus { is_on: true }).unwrap(); + + let initiate_lever_ix = Instruction { + program_id: lever_program_id, + accounts: vec![ + AccountMeta::new(power_account.pubkey(), true), + AccountMeta::new(payer.pubkey(), true), + AccountMeta::new(solana_system_interface::program::ID, false), + ], + data, + }; + + let tx = Transaction::new_signed_with_payer( + &[initiate_lever_ix], + Some(&payer.pubkey()), + &[&payer, &power_account], + svm.latest_blockhash(), + ); + + let _ = svm.send_transaction(tx).is_ok(); + + let data = borsh::to_vec(&SetPowerStatus { + name: "Chris".to_string(), + }) + .unwrap(); + + let pull_lever_ix = Instruction { + program_id: hand_program_id, + accounts: vec![ + AccountMeta::new(power_account.pubkey(), false), + AccountMeta::new(lever_program_id, false), + ], + data, + }; + + let tx = Transaction::new_signed_with_payer( + &[pull_lever_ix], + Some(&payer.pubkey()), + &[&payer], + svm.latest_blockhash(), + ); + + let _ = svm.send_transaction(tx).is_ok(); +} diff --git a/basics/cross-program-invocation/native/programs/lever/Cargo.toml b/basics/cross-program-invocation/native/programs/lever/Cargo.toml index d4377523..84b93055 100644 --- a/basics/cross-program-invocation/native/programs/lever/Cargo.toml +++ b/basics/cross-program-invocation/native/programs/lever/Cargo.toml @@ -6,6 +6,8 @@ edition = "2021" [features] no-entrypoint = [] cpi = ["no-entrypoint"] +custom-heap = [] +custom-panic = [] [dependencies] borsh = "1.5.7" @@ -15,3 +17,6 @@ solana-system-interface = {version = "2.0.0", features = ["bincode"]} [lib] crate-type = ["cdylib", "lib"] + +[lints.rust] +unexpected_cfgs = { level = "warn", check-cfg = ['cfg(target_os, values("solana"))'] }