Skip to content

Commit

Permalink
chore: organising test cases
Browse files Browse the repository at this point in the history
  • Loading branch information
HinsonSIDAN committed Mar 25, 2024
1 parent f33ff18 commit f304403
Show file tree
Hide file tree
Showing 11 changed files with 305 additions and 273 deletions.
22 changes: 0 additions & 22 deletions rust/src/core/common/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,37 +4,15 @@ pub fn bytes_to_hex(bytes: &[u8]) -> String {
hex::encode(bytes)
}

#[test]
fn test_bytes_to_hex() {
let bytes = vec![0, 1, 2, 3, 4, 5];
assert_eq!(bytes_to_hex(&bytes), "000102030405");
}

pub fn hex_to_bytes(hex: &str) -> Result<Vec<u8>, hex::FromHexError> {
hex::decode(hex)
}

#[test]
fn test_hex_to_bytes() {
let bytes = vec![0, 1, 2, 3, 4, 255];
assert_eq!(hex_to_bytes("0001020304ff").unwrap(), bytes);
}

pub fn string_to_hex(s: &str) -> String {
hex::encode(s)
}

#[test]
fn test_string_to_hex() {
assert_eq!(string_to_hex("DELTA"), "44454c5441");
}

pub fn hex_to_string(hex: &str) -> Result<String, std::str::Utf8Error> {
let bytes = hex::decode(hex).unwrap();
Ok(std::str::from_utf8(&bytes)?.to_string())
}

#[test]
fn test_hex_to_string() {
assert_eq!(hex_to_string("44454c5441").unwrap(), "DELTA");
}
134 changes: 0 additions & 134 deletions rust/src/core/common/plutus_types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -104,137 +104,3 @@ pub fn pub_key_hash(pub_key_hash: &str) -> Value {
pub fn posix_time(posix_time: i64) -> Value {
integer(posix_time)
}

#[test]
fn test_con_str() {
let correct_con_str = "{\"constructor\":10,\"fields\":[{\"bytes\":\"hello\"}]}";
assert_eq!(
con_str(10, json!([builtin_byte_string("hello")])).to_string(),
correct_con_str
);
}

#[test]
fn test_con_str0() {
let correct_con_str0 = "{\"constructor\":0,\"fields\":{\"bytes\":\"hello\"}}";
assert_eq!(
con_str0(builtin_byte_string("hello")).to_string(),
correct_con_str0
);
}

#[test]
fn test_con_str1() {
let correct_con_str1 = "{\"constructor\":1,\"fields\":{\"bytes\":\"hello\"}}";
assert_eq!(
con_str1(builtin_byte_string("hello")).to_string(),
correct_con_str1
);
}

#[test]
fn test_con_str2() {
let correct_con_str2 = "{\"constructor\":2,\"fields\":{\"bytes\":\"hello\"}}";
assert_eq!(
con_str2(builtin_byte_string("hello")).to_string(),
correct_con_str2
);
}

#[test]
fn test_bool() {
let correct_bool = "{\"constructor\":1,\"fields\":[]}";
assert_eq!(bool(true).to_string(), correct_bool);
}

#[test]
fn test_builtin_byte_string() {
let correct_builtin_byte_string = "{\"bytes\":\"hello\"}";
assert_eq!(
builtin_byte_string("hello").to_string(),
correct_builtin_byte_string
);
}

#[test]
fn test_integer() {
let correct_integer = "{\"int\":1}";
assert_eq!(integer(1).to_string(), correct_integer);
}

#[test]
fn test_list() {
let correct_list = "{\"list\":[1,2,3]}";
assert_eq!(list(vec![1, 2, 3]).to_string(), correct_list);
}

#[test]
fn test_maybe_staking_hash() {
let correct_maybe_staking_hash = "{\"constructor\":0,\"fields\":[{\"constructor\":0,\"fields\":[{\"constructor\":0,\"fields\":[{\"bytes\":\"hello\"}]}]}]}";
assert_eq!(
maybe_staking_hash("hello").to_string(),
correct_maybe_staking_hash
);
}

#[test]
fn test_pub_key_address() {
let correct_pub_key_address = "{\"constructor\":0,\"fields\":[{\"constructor\":0,\"fields\":[{\"bytes\":\"8f2ac4b2a57a90feb7717c7361c7043af6c3646e9db2b0e616482f73\"}]},{\"constructor\":0,\"fields\":[{\"constructor\":0,\"fields\":[{\"constructor\":0,\"fields\":[{\"bytes\":\"039506b8e57e150bb66f6134f3264d50c3b70ce44d052f4485cf388f\"}]}]}]}]}";
assert_eq!(
pub_key_address(
"8f2ac4b2a57a90feb7717c7361c7043af6c3646e9db2b0e616482f73",
Some("039506b8e57e150bb66f6134f3264d50c3b70ce44d052f4485cf388f")
)
.to_string(),
correct_pub_key_address
);
}

#[test]
fn test_script_address() {
let correct_script_address = "{\"constructor\":0,\"fields\":[{\"constructor\":1,\"fields\":[{\"bytes\":\"hello\"}]},{\"constructor\":1,\"fields\":[]}]}";
assert_eq!(
script_address("hello", None).to_string(),
correct_script_address
);
}

#[test]
fn test_asset_class() {
let correct_asset_class =
"{\"constructor\":0,\"fields\":[{\"bytes\":\"hello\"},{\"bytes\":\"world\"}]}";
assert_eq!(
asset_class("hello", "world").to_string(),
correct_asset_class
);
}

#[test]
fn test_tx_out_ref() {
let correct_tx_out_ref = "{\"constructor\":0,\"fields\":[{\"constructor\":0,\"fields\":[{\"bytes\":\"hello\"}]},{\"int\":12}]}";
assert_eq!(tx_out_ref("hello", 12).to_string(), correct_tx_out_ref);
}

#[test]
fn test_assoc_map() {
let correct_assoc_map =
"{\"map\":[{\"k\":{\"bytes\":\"hello\"},\"v\":{\"bytes\":\"world\"}},{\"k\":{\"bytes\":\"123\"},\"v\":{\"bytes\":\"456\"}}]}";
assert_eq!(
assoc_map(vec![
(builtin_byte_string("hello"), builtin_byte_string("world")),
(builtin_byte_string("123"), builtin_byte_string("456"))
])
.to_string(),
correct_assoc_map
);
}

#[test]
fn test_tuple() {
let correct_tuple =
"{\"constructor\":0,\"fields\":[{\"bytes\":\"hello\"},{\"bytes\":\"world\"}]}";
assert_eq!(
tuple(builtin_byte_string("hello"), builtin_byte_string("world")).to_string(),
correct_tuple
);
}
16 changes: 0 additions & 16 deletions rust/src/core/utils/aiken.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,22 +53,6 @@ pub fn apply_params_to_script(
Ok(bytes_to_hex(&bytes))
}

#[test]
fn test_apply_params_to_script() {
use serde_json::{json, to_string};
let script =
"584501000032323232323222533300432323253330073370e900018041baa0011324a2600c0022c60120026012002600600229309b2b118021baa0015734aae7555cf2ba157441";
let params = vec![to_string(&json!({ "bytes": "1234"})).unwrap()];

let mut aiken_params = JsVecString::new();
aiken_params.add(params[0].clone());

assert_eq!(
apply_params_to_script(aiken_params, script.to_string()).unwrap(),
"584f584d010000332323232323222533300432323253330073370e900018041baa0011324a2600c0022c60120026012002600600229309b2b118021baa0015734aae7555cf2ba157449801034212340001"
);
}

pub fn apply_params_to_plutus_script(
params: &PlutusList,
plutus_script: PlutusScript,
Expand Down
26 changes: 0 additions & 26 deletions rust/src/core/utils/transaction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,32 +13,6 @@ pub fn calculate_tx_hash(tx_hex: &str) -> String {
csl::crypto::TransactionHash::from(blake2b256(&csl_tx.raw_body())).to_hex()
}

#[test]
fn test_calculate_tx_hash() {
let tx_hex = "84a30081825820cc24e6f228e04d98c80088c830a363fff80a2437959f826e1a5b4c01ec912d0f010182a200581d605ca51b304b1f79d92eada8c58c513e969458dcd27ce4f5bc47823ffa011a001c0242a200581d601fd5bab167338971d92b4d8f0bdf57d889903e6e934e7ea38c7dadf1011b0000000252fe47ac021a00028759a100818258201557f444f3ae6e61dfed593ae15ec8dbd57b8138972bf16fde5b4c559f41549b5840b8317b840d4e908cd6a69bad0d294a593a40812749ccacdea993c660952a57cdf89428934973848a1437820b9f0e5784ddc01eb049415d4189977fdc32fda904f5f6";
let tx_hash = calculate_tx_hash(tx_hex);
assert_eq!(
tx_hash,
"c162f8abf8405b1d7f8f7677bc391b2d8f1911e73035cb97634b2dede72404cf"
)
}

#[test]
fn test_calculate_tx_hash_2() {
let tx_hex = "84a400828258200f88c351c8afb3494b70dc2128e61289ea279fee7516db2c58e1562ce8576bbd028258208bbb363df8e0bcadf6b4ac473a06d94d75be243e0772ffbfc34571ea39873a5c000182a3005839008f2ac4b2a57a90feb7717c7361c7043af6c3646e9db2b0e616482f73039506b8e57e150bb66f6134f3264d50c3b70ce44d052f4485cf388f01821a0012593aa1581c5066154a102ee037390c5236f78db23239b49c5748d3d349f3ccf04ba144555344581a00989680028201d81843d87980825839008f2ac4b2a57a90feb7717c7361c7043af6c3646e9db2b0e616482f73039506b8e57e150bb66f6134f3264d50c3b70ce44d052f4485cf388f1ab51927b1021a0002a04509a0a0f5f6";
let signed_tx_hex = "84a400828258200f88c351c8afb3494b70dc2128e61289ea279fee7516db2c58e1562ce8576bbd028258208bbb363df8e0bcadf6b4ac473a06d94d75be243e0772ffbfc34571ea39873a5c000182a3005839008f2ac4b2a57a90feb7717c7361c7043af6c3646e9db2b0e616482f73039506b8e57e150bb66f6134f3264d50c3b70ce44d052f4485cf388f01821a0012593aa1581c5066154a102ee037390c5236f78db23239b49c5748d3d349f3ccf04ba144555344581a00989680028201d81843d87980825839008f2ac4b2a57a90feb7717c7361c7043af6c3646e9db2b0e616482f73039506b8e57e150bb66f6134f3264d50c3b70ce44d052f4485cf388f1ab51927b1021a0002a04509a0a10081825820eb125c9530b870bab17f5f30dcbf029929c4d8743e4eaaf71a5e883d41a236ce5840bc63f18abf97e386743b00baf8e829a73e19bf600c8bdfc0e53d14c5171c7f6d62adc5a7081b06465d7003641ec2406421316424d216e06605323ebc68c1600cf5f6";
let tx_hash_from_unsigned_hex = calculate_tx_hash(tx_hex);
let tx_hash_from_signed_hex = calculate_tx_hash(signed_tx_hex);
assert_eq!(
tx_hash_from_unsigned_hex,
"e8b7aefcee2953cf55a01c97565cfe9d414a21e17064d8fcef1f632f7311f933"
);
assert_eq!(
tx_hash_from_signed_hex,
"e8b7aefcee2953cf55a01c97565cfe9d414a21e17064d8fcef1f632f7311f933"
)
}

#[wasm_bindgen]
pub fn sign_transaction(tx_hex: String, signing_keys: JsVecString) -> String {
let unsigned_transaction: csl::Transaction = csl::Transaction::from_hex(&tx_hex).unwrap();
Expand Down
71 changes: 71 additions & 0 deletions rust/tests/address_tests.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
mod address_tests {
use sidan_csl_rs::{
core::utils::{script_to_address, serialize_bech32_address},
model::SerializedAddress,
};

#[test]
fn test_script_to_address() {
let addr = script_to_address(
0,
"ac43e2abd0909c559966056aaa35d2172717174e224feb81e34c306a".to_string(),
None,
);
assert!(addr == "addr_test1wzky8c4t6zgfc4vevczk42346gtjw9chfc3yl6upudxrq6sghz0nw");

let base_addr = script_to_address(
0,
"e55a6e7c9f4e96692a3c23a56f126911cc70a29d2e2ac967dc644432".to_string(),
Some("6d913965402b012050e09f12012c533e6c33678d1c5ed2154b328d25".to_string()),
);

assert!(base_addr == "addr_test1zrj45mnuna8fv6f28s362mcjdygucu9zn5hz4jt8m3jygvndjyuk2sptqys9pcylzgqjc5e7dsek0rgutmfp2jej35jseqau4y");
}

#[test]
fn test_serialize_address() {
let addr1 = "addr_test1qz8j439j54afpl4hw978xcw8qsa0dsmyd6wm9v8xzeyz7ucrj5rt3et7z59mvmmpxnejvn2scwmseezdq5h5fpw08z8s8d93my";
let addr1_result = serialize_bech32_address(addr1.to_string());
assert!(
addr1_result
== SerializedAddress::new(
"8f2ac4b2a57a90feb7717c7361c7043af6c3646e9db2b0e616482f73".to_string(),
"".to_string(),
"039506b8e57e150bb66f6134f3264d50c3b70ce44d052f4485cf388f".to_string()
)
);

let addr2 = "addr_test1zqjmsmh2sjjy508e3068pck6lgp23k2msypgc52cxcgzjlju5ayjvx4rk9a29n2tqf4uv4nvfv2yy8tqs0kuue8luh9s5cdt49";
let addr2_result = serialize_bech32_address(addr2.to_string());
assert!(
addr2_result
== SerializedAddress::new(
"".to_string(),
"25b86eea84a44a3cf98bf470e2dafa02a8d95b81028c51583610297e".to_string(),
"5ca749261aa3b17aa2cd4b026bc6566c4b14421d6083edce64ffe5cb".to_string()
)
);

let addr3 = "addr_test1vpw22xesfv0hnkfw4k5vtrz386tfgkxu6f7wfadug7prl7s6gt89x";
let addr3_result = serialize_bech32_address(addr3.to_string());
assert!(
addr3_result
== SerializedAddress::new(
"5ca51b304b1f79d92eada8c58c513e969458dcd27ce4f5bc47823ffa".to_string(),
"".to_string(),
"".to_string()
)
);

let addr4 = "addr_test1qqmrzjhtanauj20wg37uk58adyrqfm82a9qr52vdnv0e54r42v0mu8ngky0f5yxmh3wl3z0da2fryk59kavth0u8xhvsufgmc8";
let addr4_result = serialize_bech32_address(addr4.to_string());
assert!(
addr4_result
== SerializedAddress::new(
"36314aebecfbc929ee447dcb50fd690604eceae9403a298d9b1f9a54".to_string(),
"".to_string(),
"75531fbe1e68b11e9a10dbbc5df889edea92325a85b758bbbf8735d9".to_string()
)
)
}
}
19 changes: 19 additions & 0 deletions rust/tests/aiken_tests.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
mod aiken_tests {
use sidan_csl_rs::{core::utils::apply_params_to_script, model::JsVecString};

#[test]
fn test_apply_params_to_script() {
use serde_json::{json, to_string};
let script =
"584501000032323232323222533300432323253330073370e900018041baa0011324a2600c0022c60120026012002600600229309b2b118021baa0015734aae7555cf2ba157441";
let params = vec![to_string(&json!({ "bytes": "1234"})).unwrap()];

let mut aiken_params = JsVecString::new();
aiken_params.add(params[0].clone());

assert_eq!(
apply_params_to_script(aiken_params, script.to_string()).unwrap(),
"584f584d010000332323232323222533300432323253330073370e900018041baa0011324a2600c0022c60120026012002600600229309b2b118021baa0015734aae7555cf2ba157449801034212340001"
);
}
}
Loading

0 comments on commit f304403

Please sign in to comment.