Skip to content

Commit

Permalink
Add C Serialization Tests for #17217 (#17294)
Browse files Browse the repository at this point in the history
  • Loading branch information
jackcmay authored May 17, 2021
1 parent 7ea1131 commit f15dd1b
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 0 deletions.
34 changes: 34 additions & 0 deletions programs/bpf/c/src/ser/ser.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/**
* @brief Example C-based BPF sanity rogram that prints out the parameters
* passed to it
*/
#include <solana_sdk.h>

extern uint64_t entrypoint(const uint8_t *input) {
SolAccountInfo ka[1];
SolParameters params = (SolParameters){.ka = ka};

sol_log(__FILE__);

if (!sol_deserialize(input, &params, SOL_ARRAY_SIZE(ka))) {
return ERROR_INVALID_ARGUMENT;
}

char ka_data[] = {1, 2, 3};
SolPubkey ka_owner;
sol_assert(params.ka_num == 2);
for (int i = 0; i < 2; i++) {
sol_assert(*params.ka[i].lamports == 42);
sol_assert(!sol_memcmp(params.ka[i].data, ka_data, 4));
sol_assert(SolPubkey_same(params.ka[i].owner, &ka_owner));
sol_assert(params.ka[i].is_signer == false);
sol_assert(params.ka[i].is_writable == false);
sol_assert(params.ka[i].executable == false);
}

char data[] = {4, 5, 6, 7};
sol_assert(params.data_len = 4);
sol_assert(!sol_memcmp(params.data, data, 4));

return SUCCESS;
}
32 changes: 32 additions & 0 deletions programs/bpf/tests/programs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1912,6 +1912,38 @@ fn test_program_bpf_disguised_as_bpf_loader() {
}
}

#[test]
#[cfg(feature = "bpf_c")]
fn test_program_bpf_c_dup() {
solana_logger::setup();

let GenesisConfigInfo {
genesis_config,
mint_keypair,
..
} = create_genesis_config(50);
let mut bank = Bank::new(&genesis_config);
let (name, id, entrypoint) = solana_bpf_loader_program!();
bank.add_builtin(&name, id, entrypoint);

let account_address = Pubkey::new_unique();
let account =
AccountSharedData::new_data(42, &[1_u8, 2, 3], &solana_sdk::system_program::id()).unwrap();
bank.store_account(&account_address, &account);

let bank_client = BankClient::new(bank);

let program_id = load_bpf_program(&bank_client, &bpf_loader::id(), &mint_keypair, "ser");
let account_metas = vec![
AccountMeta::new_readonly(account_address, false),
AccountMeta::new_readonly(account_address, false),
];
let instruction = Instruction::new_with_bytes(program_id, &[4, 5, 6, 7], account_metas);
bank_client
.send_and_confirm_instruction(&mint_keypair, instruction)
.unwrap();
}

#[cfg(feature = "bpf_rust")]
#[test]
fn test_program_bpf_upgrade_via_cpi() {
Expand Down

0 comments on commit f15dd1b

Please sign in to comment.