From fb482bac2091049c496c4007f010c677831ae04e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=C3=A3o=20Novo?= <34069419+TeknoPT@users.noreply.github.com> Date: Wed, 6 Dec 2023 19:01:15 +0000 Subject: [PATCH] Tests updates. - Fixed some tests --- tests/test_appname_cmd.py | 2 +- tests/test_name_version.py | 4 +- tests/test_pubkey_cmd.py | 6 +- unit-tests/test_tx_parser.c | 136 ++++++++++++++++++++++++++++++++++++ 4 files changed, 142 insertions(+), 6 deletions(-) diff --git a/tests/test_appname_cmd.py b/tests/test_appname_cmd.py index e100ed3..4786192 100644 --- a/tests/test_appname_cmd.py +++ b/tests/test_appname_cmd.py @@ -9,4 +9,4 @@ def test_app_name(backend): # Send the GET_APP_NAME instruction to the app response = client.get_app_name() # Assert that we have received the correct appname - assert unpack_get_app_name_response(response.data) == "Boilerplate" + assert unpack_get_app_name_response(response.data) == "Phantasma" diff --git a/tests/test_name_version.py b/tests/test_name_version.py index a7e2ea2..33dbd9c 100644 --- a/tests/test_name_version.py +++ b/tests/test_name_version.py @@ -11,5 +11,5 @@ def test_get_app_and_version(backend, backend_name): # Use an helper to parse the response, assert the values app_name, version = unpack_get_app_and_version_response(response.data) - assert app_name == "Boilerplate" - assert version == "2.1.0" + assert app_name == "Phantasma" + assert version == "1.0.2" diff --git a/tests/test_pubkey_cmd.py b/tests/test_pubkey_cmd.py index f15d5f3..8beb1c4 100644 --- a/tests/test_pubkey_cmd.py +++ b/tests/test_pubkey_cmd.py @@ -10,7 +10,7 @@ # In this test we check that the GET_PUBLIC_KEY works in non-confirmation mode def test_get_public_key_no_confirm(backend): - for path in ["m/44'/1'/0'/0/0", "m/44'/1'/0/0/0", "m/44'/1'/911'/0/0", "m/44'/1'/255/255/255", "m/44'/1'/2147483647/0/0/0/0/0/0/0"]: + for path in ["m/44'/60'/0'/0/0"]: client = BoilerplateCommandSender(backend) response = client.get_public_key(path=path).data _, public_key, _, chain_code = unpack_get_public_key_response(response) @@ -23,7 +23,7 @@ def test_get_public_key_no_confirm(backend): # In this test we check that the GET_PUBLIC_KEY works in confirmation mode def test_get_public_key_confirm_accepted(firmware, backend, navigator, test_name): client = BoilerplateCommandSender(backend) - path = "m/44'/1'/0'/0/0" + path = "m/44'/60'/0'/0/0" with client.get_public_key_with_confirmation(path=path): if firmware.device.startswith("nano"): navigator.navigate_until_text_and_compare(NavInsID.RIGHT_CLICK, @@ -53,7 +53,7 @@ def test_get_public_key_confirm_accepted(firmware, backend, navigator, test_name # In this test we check that the GET_PUBLIC_KEY in confirmation mode replies an error if the user refuses def test_get_public_key_confirm_refused(firmware, backend, navigator, test_name): client = BoilerplateCommandSender(backend) - path = "m/44'/1'/0'/0/0" + path = "m/44'/60'/0'/0/0" if firmware.device.startswith("nano"): with pytest.raises(ExceptionRAPDU) as e: diff --git a/unit-tests/test_tx_parser.c b/unit-tests/test_tx_parser.c index 02cd5ad..6f8fa82 100644 --- a/unit-tests/test_tx_parser.c +++ b/unit-tests/test_tx_parser.c @@ -11,6 +11,10 @@ #include "transaction/deserialize.h" #include "types.h" +#define RAW_TX_LEN (sizeof(hex_tx)-1)/2 + +#define RAW_SCRIPT_LEN (sizeof(hex_script)-1)/2 + static void test_tx_serialization(void **state) { (void) state; @@ -73,6 +77,138 @@ static void test_tx_serialization(void **state) { assert_memory_equal(raw_tx, output, sizeof(raw_tx)); } +static int hex_str_to_bytes(uint8_t* out, const char* in, size_t out_len_max) +{ + const int in_len = strnlen(in, out_len_max * 2); + if (in_len % 2 != 0) { + return -1; // error, in str len should be even + } + + // calc actual out len + const int out_len = out_len_max < (in_len / 2) ? out_len_max : (in_len / 2); + + for (int i = 0; i < out_len; i++) { + char ch0 = in[2 * i]; + char ch1 = in[2 * i + 1]; + uint8_t nib0 = ((ch0 & 0xF) + (ch0 >> 6)) | ((ch0 >> 3) & 0x8); + uint8_t nib1 = ((ch1 & 0xF) + (ch1 >> 6)) | ((ch1 >> 3) & 0x8); + uint8_t c = (nib0 << 4) | nib1; + out[i] = c; + } + return out_len; +} + +static void assert_stringn_equal(const uint8_t *a, const char *b, const size_t len) { + uint8_t a0[len+1]; + memset(a0, 0, sizeof(a0)); + memmove(a0, a, len); + assert_string_equal(a0,b); + +} + +static void test_tx_serialization_v2(void **state) { + (void) state; + + transaction_t tx; + + char hex_script[] = + "0D000402313003000D000402313003000D000423533131313131313131313131" + "313131313131313131313131313131313131313131313103000D00042F50324B" + "376E3170566679335A4D536578747258316E6A337859645251766A446E744547" + "416631783262376836526B3403000D000408416C6C6F7747617303000D000403" + "6761732D00012E010D000402313003000D0004074F50434F44455303000D0004" + "2F50324B376E3170566679335A4D536578747258316E6A337859645251766A44" + "6E744547416631783262376836526B3403000D00042F50324B376E3170566679" + "335A4D536578747258316E6A337859645251766A446E74454741663178326237" + "6836526B3403000D00041652756E74696D652E5472616E73666572546F6B656E" + "7307000D00042F50324B376E3170566679335A4D536578747258316E6A337859" + "645251766A446E744547416631783262376836526B3403000D0004085370656E" + "6447617303000D0004036761732D00012E010B"; + + char hex_tx[] = "076d61696e6e6574046d61696efd73010d000402313003000d00040231300300" + "0d00042353313131313131313131313131313131313131313131313131313131" + "3131313131313103000d00042f50324b376e3170566679335a4d536578747258" + "316e6a337859645251766a446e744547416631783262376836526b3403000d00" + "0408416c6c6f7747617303000d0004036761732d00012e010d00040231300300" + "0d0004074f50434f44455303000d00042f50324b376e3170566679335a4d5365" + "78747258316e6a337859645251766a446e744547416631783262376836526b34" + "03000d00042f50324b376e3170566679335a4d536578747258316e6a33785964" + "5251766a446e744547416631783262376836526b3403000d00041652756e7469" + "6d652e5472616e73666572546f6b656e7307000d00042f50324b376e31705666" + "79335a4d536578747258316e6a337859645251766a446e744547416631783262" + "376836526b3403000d0004085370656e6447617303000d0004036761732d0001" + "2e010b000000000c7068616E7461736D612D7473"; + assert_int_equal(sizeof(hex_tx), 0x0329); + + // clang-format off + uint8_t raw_tx[RAW_TX_LEN] = {0}; + assert_int_equal(sizeof(raw_tx), 0x194); + + hex_str_to_bytes(raw_tx,hex_tx,sizeof(raw_tx)); + + uint8_t raw_script[RAW_SCRIPT_LEN] = {0}; + assert_int_equal(sizeof(raw_script), 0x173); + + hex_str_to_bytes(raw_script,hex_script,sizeof(raw_script)); + + buffer_t buf = {.ptr = raw_tx, .size = sizeof(raw_tx), .offset = 0}; + + parser_status_e status = transaction_deserialize(&buf, &tx); + + // for(int i = 0; i < tx.script_len; i++) { + // uint8_t c = *(tx.script+i); + // uint8_t s = *(raw_script+i); + // uint8_t s2 = *(tx.script_buf.ptr+i); + // bool match = i < tx.script_buf.offset; + // printf("hex_t_bytes %d %d %02X %02X %02X %c\n", match, i, c, s, s2, s2); + // } + + printf("buf.offset %lu\n", buf.offset); + printf("buf.size %lu\n", buf.size); + + printf("tx.script_buf.offset %lu\n", tx.script_buf.offset); + printf("tx.script_buf.size %lu\n", tx.script_buf.size); + + assert_int_equal(tx.allow_gas.args[0].load.opcode, 13); + assert_int_equal(tx.allow_gas.args[0].load.reg, 0); + assert_int_equal(tx.allow_gas.args[0].load.type, 4); + assert_int_equal(tx.allow_gas.args[0].load.buf.size, 2); + assert_stringn_equal(tx.allow_gas.args[0].load.buf.ptr, + "10", tx.allow_gas.args[0].load.buf.size); + + assert_int_equal(tx.allow_gas.args[0].push.opcode, 3); + assert_int_equal(tx.allow_gas.args[0].push.reg, 0); + + // value + assert_stringn_equal(tx.transfer_tokens.args[0].load.buf.ptr, + "10", tx.transfer_tokens.args[0].load.buf.size); + + assert_stringn_equal(tx.from,"P2K7n1pVfy3ZMSextrX1nj3xYdRQvjDntEGAf1x2b7h6Rk4", tx.from_len); + assert_stringn_equal(tx.to,"P2K7n1pVfy3ZMSextrX1nj3xYdRQvjDntEGAf1x2b7h6Rk4", tx.to_len); + + // yes OPCODES is a fake token name. + assert_stringn_equal(tx.token,"OPCODES", tx.token_len); + assert_stringn_equal(tx.value,"10", tx.value_len); + assert_stringn_equal(tx.gas_limit,"10", tx.gas_limit_len); + assert_stringn_equal(tx.gas_price,"10", tx.gas_price_len); + + //assert_int_equal(tx.opcode_offsets_ix, 0); + + //assert_int_equal(tx.script_buf.offset, tx.script_buf.size); + + assert_int_equal(status, PARSING_OK); + + assert_stringn_equal(tx.nexus, "mainnet", tx.nexus_len); + assert_stringn_equal(tx.chain, "main", tx.chain_len); + assert_stringn_equal(tx.payload, "phantasma-ts", tx.payload_len); + + uint8_t output[RAW_TX_LEN]; + int length = transaction_serialize(&tx, output, sizeof(output)); + assert_int_equal(length, sizeof(raw_tx)); + assert_memory_equal(raw_tx, output, sizeof(raw_tx)); +} + + int main() { const struct CMUnitTest tests[] = {cmocka_unit_test(test_tx_serialization)};