Skip to content

Commit f5e997b

Browse files
authored
Add mapping tests (#223)
* Base test, clippy stopped working * Test complete * Tests inside tests * Public function * Delete C add_mapping
1 parent a58e5cf commit f5e997b

File tree

9 files changed

+355
-304
lines changed

9 files changed

+355
-304
lines changed

program/c/src/oracle/oracle.c

Lines changed: 1 addition & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -59,46 +59,6 @@ static bool valid_writable_account( SolParameters *prm,
5959
is_rent_exempt( *ka->lamports, ka->data_len );
6060
}
6161

62-
static uint64_t add_mapping( SolParameters *prm, SolAccountInfo *ka )
63-
{
64-
// Account (1) is the tail or last mapping account in the chain
65-
// Account (2) is the new mapping account and will become the new tail
66-
// Verify that these are signed, writable accounts with correct ownership
67-
// and size
68-
if ( prm->ka_num != 3 ||
69-
!valid_funding_account( &ka[0] ) ||
70-
!valid_signable_account( prm, &ka[1], sizeof( pc_map_table_t ) ) ||
71-
!valid_signable_account( prm, &ka[2], sizeof( pc_map_table_t ) ) ) {
72-
return ERROR_INVALID_ARGUMENT;
73-
}
74-
// Verify that last mapping account in chain is initialized, full
75-
// and not pointing to a another account in the chain
76-
// Also verify that the new account is uninitialized
77-
cmd_hdr_t *hdr = (cmd_hdr_t*)prm->data;
78-
pc_map_table_t *pptr = (pc_map_table_t*)ka[1].data;
79-
pc_map_table_t *nptr = (pc_map_table_t*)ka[2].data;
80-
if ( pptr->magic_ != PC_MAGIC ||
81-
pptr->ver_ != hdr->ver_ ||
82-
pptr->type_ != PC_ACCTYPE_MAPPING ||
83-
nptr->magic_ != 0 ||
84-
pptr->num_ < PC_MAP_TABLE_SIZE ||
85-
nptr->num_ != 0 ||
86-
!pc_pub_key_is_zero( &pptr->next_ ) ) {
87-
return ERROR_INVALID_ARGUMENT;
88-
}
89-
// Initialize new account and set version number
90-
sol_memset( nptr, 0, sizeof( pc_map_table_t ) );
91-
nptr->magic_ = PC_MAGIC;
92-
nptr->ver_ = hdr->ver_;
93-
nptr->type_ = PC_ACCTYPE_MAPPING;
94-
nptr->size_ = sizeof( pc_map_table_t ) - sizeof( nptr->prod_ );
95-
96-
// Set last mapping account to point to this mapping account
97-
pc_pub_key_t *nkey = (pc_pub_key_t*)ka[2].key;
98-
pc_pub_key_assign( &pptr->next_, nkey );
99-
return SUCCESS;
100-
}
101-
10262
static uint64_t add_product( SolParameters *prm, SolAccountInfo *ka )
10363
{
10464
// Account (1) is the mapping account that we're going to add to and
@@ -513,7 +473,7 @@ static uint64_t dispatch( SolParameters *prm, SolAccountInfo *ka )
513473
case e_cmd_upd_price_no_fail_on_error: return upd_price_no_fail_on_error( prm, ka );
514474
// init_mapping is overridden in Rust, but still implemented here to make the C unit tests pass.
515475
case e_cmd_init_mapping: return ERROR_INVALID_ARGUMENT;
516-
case e_cmd_add_mapping: return add_mapping( prm, ka );
476+
case e_cmd_add_mapping: return ERROR_INVALID_ARGUMENT;
517477
case e_cmd_add_product: return add_product( prm, ka );
518478
case e_cmd_upd_product: return upd_product( prm, ka );
519479
case e_cmd_add_price: return add_price( prm, ka );

program/c/src/oracle/test_oracle.c

Lines changed: 0 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -7,81 +7,6 @@ uint64_t MAPPING_ACCOUNT_LAMPORTS = 143821440;
77
uint64_t PRODUCT_ACCOUNT_LAMPORTS = 4454400;
88
uint64_t PRICE_ACCOUNT_LAMPORTS = 23942400;
99

10-
Test(oracle, add_mapping ) {
11-
// start with perfect inputs
12-
cmd_hdr_t idata = {
13-
.ver_ = PC_VERSION,
14-
.cmd_ = e_cmd_add_mapping
15-
};
16-
pc_map_table_t tptr[1];
17-
sol_memset( tptr, 0, sizeof( pc_map_table_t ) );
18-
tptr->magic_ = PC_MAGIC;
19-
tptr->ver_ = PC_VERSION;
20-
tptr->num_ = PC_MAP_TABLE_SIZE;
21-
tptr->type_ = PC_ACCTYPE_MAPPING;
22-
23-
SolPubkey p_id = {.x = { 0xff, }};
24-
SolPubkey pkey = {.x = { 1, }};
25-
SolPubkey tkey = {.x = { 2, }};
26-
SolPubkey mkey = {.x = { 3, }};
27-
uint64_t pqty = 100;
28-
pc_map_table_t mptr[1];
29-
sol_memset( mptr, 0, sizeof( pc_map_table_t ) );
30-
SolAccountInfo acc[] = {{
31-
.key = &pkey,
32-
.lamports = &pqty,
33-
.data_len = 0,
34-
.data = NULL,
35-
.owner = NULL,
36-
.rent_epoch = 0,
37-
.is_signer = true,
38-
.is_writable = true,
39-
.executable = false
40-
},{
41-
.key = &tkey,
42-
.lamports = &MAPPING_ACCOUNT_LAMPORTS,
43-
.data_len = sizeof( pc_map_table_t ),
44-
.data = (uint8_t*)tptr,
45-
.owner = &p_id,
46-
.rent_epoch = 0,
47-
.is_signer = true,
48-
.is_writable = true,
49-
.executable = false
50-
},{
51-
.key = &mkey,
52-
.lamports = &MAPPING_ACCOUNT_LAMPORTS,
53-
.data_len = sizeof( pc_map_table_t ),
54-
.data = (uint8_t*)mptr,
55-
.owner = &p_id,
56-
.rent_epoch = 0,
57-
.is_signer = true,
58-
.is_writable = true,
59-
.executable = false
60-
}};
61-
SolParameters prm = {
62-
.ka = acc,
63-
.ka_num = 3,
64-
.data = (const uint8_t*)&idata,
65-
.data_len = sizeof( idata ),
66-
.program_id = &p_id
67-
};
68-
cr_assert( SUCCESS == dispatch( &prm, acc ) );
69-
cr_assert( mptr->magic_ == PC_MAGIC );
70-
cr_assert( mptr->ver_ == PC_VERSION );
71-
cr_assert( pc_pub_key_equal( &tptr->next_, (pc_pub_key_t*)&mkey ) );
72-
cr_assert( pc_pub_key_is_zero( &mptr->next_ ) );
73-
pc_pub_key_set_zero( &tptr->next_ );
74-
sol_memset( mptr, 0, sizeof( pc_map_table_t ) );
75-
tptr->num_ = 0;
76-
cr_assert( ERROR_INVALID_ARGUMENT == dispatch( &prm, acc ) );
77-
cr_assert( pc_pub_key_is_zero( &tptr->next_ ) );
78-
tptr->num_ = PC_MAP_TABLE_SIZE;
79-
tptr->magic_ = 0;
80-
cr_assert( ERROR_INVALID_ARGUMENT == dispatch( &prm, acc ) );
81-
tptr->magic_ = PC_MAGIC;
82-
cr_assert( SUCCESS == dispatch( &prm, acc ) );
83-
}
84-
8510
Test(oracle, add_product) {
8611
// start with perfect inputs
8712
cmd_add_product_t idata = {

program/rust/src/lib.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,12 @@ mod error;
1010
mod log;
1111
mod processor;
1212
mod rust_oracle;
13-
mod test_oracle;
1413
mod time_machine_types;
1514
mod utils;
1615

16+
#[cfg(test)]
17+
mod tests;
18+
1719
use crate::c_oracle_header::SUCCESSFULLY_UPDATED_AGGREGATE;
1820
use crate::error::{
1921
OracleError,

program/rust/src/log.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ pub fn pre_log(accounts: &[AccountInfo], instruction_data: &[u8]) -> ProgramResu
2020
.try_into()
2121
.map_err(|_| OracleError::IntegerCastingError)?;
2222

23-
2423
match instruction_id {
2524
command_t_e_cmd_upd_price | command_t_e_cmd_agg_price => {
2625
let instruction: &cmd_upd_price = load::<cmd_upd_price>(instruction_data)?;

program/rust/src/rust_oracle.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,6 @@ pub fn update_version(
6666
// Ok(SUCCESS)
6767
}
6868

69-
7069
/// initialize the first mapping account in a new linked-list of mapping accounts
7170
/// accounts[0] funding account [signer writable]
7271
/// accounts[1] new mapping account [signer writable]
@@ -208,7 +207,7 @@ pub fn clear_account(account: &AccountInfo) -> Result<(), ProgramError> {
208207
/// Mutably borrow the data in `account` as a mapping account, validating that the account
209208
/// is properly formatted. Any mutations to the returned value will be reflected in the
210209
/// account data. Use this to read already-initialized accounts.
211-
fn load_mapping_account_mut<'a>(
210+
pub fn load_mapping_account_mut<'a>(
212211
account: &'a AccountInfo,
213212
expected_version: u32,
214213
) -> Result<RefMut<'a, pc_map_table_t>, ProgramError> {
@@ -226,7 +225,7 @@ fn load_mapping_account_mut<'a>(
226225

227226
/// Initialize account as a new mapping account. This function will zero out any existing data in
228227
/// the account.
229-
fn initialize_mapping_account(account: &AccountInfo, version: u32) -> Result<(), ProgramError> {
228+
pub fn initialize_mapping_account(account: &AccountInfo, version: u32) -> Result<(), ProgramError> {
230229
clear_account(account)?;
231230

232231
let mut mapping_account = load_account_as_mut::<pc_map_table_t>(account)?;
@@ -259,6 +258,6 @@ fn load_product_account_mut<'a>(
259258
}
260259

261260
// Assign pubkey bytes from source to target, fails if source is not 32 bytes
262-
fn pubkey_assign(target: &mut pc_pub_key_t, source: &[u8]) {
261+
pub fn pubkey_assign(target: &mut pc_pub_key_t, source: &[u8]) {
263262
unsafe { target.k1_.copy_from_slice(source) }
264263
}

program/rust/src/test_oracle.rs

Lines changed: 0 additions & 182 deletions
This file was deleted.

program/rust/src/tests/mod.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
mod test_add_mapping;
2+
mod test_init_mapping;

0 commit comments

Comments
 (0)