Skip to content

Commit

Permalink
Add testcases (#44)
Browse files Browse the repository at this point in the history
Add test cases:
* The witnesses is none
* The output data is big enough(700K)
* Add a 700K witness
* Args is big enough (700K)
  • Loading branch information
joii2020 authored Mar 6, 2024
1 parent 50c7adf commit 45ef4e5
Show file tree
Hide file tree
Showing 3 changed files with 89 additions and 4 deletions.
6 changes: 3 additions & 3 deletions c/basic.mol
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,14 @@ table ScriptInfo {
vector ScriptInfoVec <ScriptInfo>;

table ResolvedInputs {
outputs: CellOutputVec,
outputs_data: BytesVec,
outputs: CellOutputVec,
outputs_data: BytesVec,
}

table BuildingPacketV1 {
message: Message,
payload: Transaction,
resolved_inputs: ResolvedInputs,
resolved_inputs: ResolvedInputs,
change_output: Uint32Opt,
script_infos: ScriptInfoVec,
lock_actions: ActionVec,
Expand Down
3 changes: 3 additions & 0 deletions tests/omni_lock_rust/tests/misc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,9 @@ pub const ERROR_COBUILD_MOL2_ERR_DATA: i8 = 0x07;
pub const ERROR_SIGHASHALL_DUP: i8 = 113;
pub const MOL2_ERR_OVERFLOW: i8 = 8; // parse witnesses error

pub const ERROR_IDENTITY_WRONG_ARGS: i8 = 71;
pub const ERROR_ARGS_FORMAT: i8 = 87;

// https://github.com/bitcoin-core/secp256k1/blob/d373bf6d08c82ac5496bf8103698c9f54d8d99d2/include/secp256k1.h#L219
pub const SECP256K1_TAG_PUBKEY_EVEN: u8 = 0x02;
pub const SECP256K1_TAG_PUBKEY_ODD: u8 = 0x03;
Expand Down
84 changes: 83 additions & 1 deletion tests/omni_lock_rust/tests/test_omni_lock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ use ckb_types::{
bytes::Bytes,
bytes::BytesMut,
core::{cell::ResolvedTransaction, EpochNumberWithFraction, HeaderView},
packed::WitnessArgs,
packed::{CellInput, WitnessArgs},
prelude::*,
H256,
};
Expand Down Expand Up @@ -1205,3 +1205,85 @@ fn test_cobuild_check_action_script_hash_is_in_2_outputs() {
let verify_result = verifier.verify(MAX_CYCLES);
verify_result.expect("pass verification");
}

#[test]
fn test_none_witness() {
let mut data_loader = DummyDataLoader::new();

let mut config = TestConfig::new(IDENTITY_FLAGS_BITCOIN, false);
config
.set_chain_config(Box::new(BitcoinConfig { sign_vtype: BITCOIN_V_TYPE_P2PKHUNCOMPRESSED, pubkey_err: false }));

let tx = gen_tx(&mut data_loader, &mut config);
let tx = sign_tx(&mut data_loader, tx, &mut config);
let tx = tx.as_advanced_builder().set_witnesses(Vec::new()).build();

let resolved_tx: ResolvedTransaction = build_resolved_tx(&data_loader, &tx);

let mut verifier = verify_tx(resolved_tx, data_loader);
verifier.set_debug_printer(debug_printer);
let verify_result = verifier.verify(MAX_CYCLES);
assert_script_error(verify_result.unwrap_err(), ERROR_IDENTITY_WRONG_ARGS);
}

#[test]
fn test_big_output_data() {
let mut data_loader = DummyDataLoader::new();

let mut config = TestConfig::new(IDENTITY_FLAGS_BITCOIN, false);
config
.set_chain_config(Box::new(BitcoinConfig { sign_vtype: BITCOIN_V_TYPE_P2PKHUNCOMPRESSED, pubkey_err: false }));

let tx = gen_tx(&mut data_loader, &mut config);
let tx = tx.as_advanced_builder().set_outputs_data(vec![[0u8; 1024 * 700].pack()]).build();

let tx = sign_tx(&mut data_loader, tx, &mut config);
let resolved_tx: ResolvedTransaction = build_resolved_tx(&data_loader, &tx);

let mut verifier = verify_tx(resolved_tx, data_loader);
verifier.set_debug_printer(debug_printer);
let verify_result = verifier.verify(MAX_CYCLES);
verify_result.expect("pass verification");
}

#[test]
fn test_big_witness() {
let mut data_loader = DummyDataLoader::new();

let mut config = TestConfig::new(IDENTITY_FLAGS_BITCOIN, false);
config
.set_chain_config(Box::new(BitcoinConfig { sign_vtype: BITCOIN_V_TYPE_P2PKHUNCOMPRESSED, pubkey_err: false }));

let tx = gen_tx(&mut data_loader, &mut config);
let tx = tx.as_advanced_builder().witness([0u8; 1024 * 700].pack()).build();

let tx = sign_tx(&mut data_loader, tx, &mut config);
let resolved_tx: ResolvedTransaction = build_resolved_tx(&data_loader, &tx);

let mut verifier = verify_tx(resolved_tx, data_loader);
verifier.set_debug_printer(debug_printer);
let verify_result = verifier.verify(MAX_CYCLES);
verify_result.expect("pass verification");
}

#[test]
fn test_big_script() {
let mut data_loader = DummyDataLoader::new();

let mut config = TestConfig::new(IDENTITY_FLAGS_BITCOIN, false);
config
.set_chain_config(Box::new(BitcoinConfig { sign_vtype: BITCOIN_V_TYPE_P2PKHUNCOMPRESSED, pubkey_err: false }));

let lock_args = config.gen_args();

let lock_args = Bytes::from(vec![lock_args.to_vec(), vec![0u8; 1024 * 700]].concat());

let tx = gen_tx_with_grouped_args(&mut data_loader, vec![(lock_args, 1)], &mut config);
let tx = sign_tx(&mut data_loader, tx, &mut config);
let resolved_tx: ResolvedTransaction = build_resolved_tx(&data_loader, &tx);

let mut verifier = verify_tx(resolved_tx, data_loader);
verifier.set_debug_printer(debug_printer);
let verify_result = verifier.verify(MAX_CYCLES);
assert_script_error(verify_result.unwrap_err(), ERROR_ARGS_FORMAT);
}

0 comments on commit 45ef4e5

Please sign in to comment.