From 1667a59ae487e4198205cd983605d090b2d5cf21 Mon Sep 17 00:00:00 2001 From: Maykel Arias Torres Date: Thu, 5 Sep 2019 13:31:46 -0400 Subject: [PATCH] [libc][cgo] refs #105 Finalized test cipher.bip32 --- include/skyerrors.h | 4 ++ lib/cgo/libsky_error.go | 13 ++++ lib/cgo/tests/check_cipher.address.common.c | 6 +- lib/cgo/tests/check_cipher.bip32.bip32.c | 67 +++++++++++++++++++++ lib/cgo/tests/check_coin.transactions.c | 18 +++--- lib/cgo/tests/test_main.c | 36 +++++------ 6 files changed, 114 insertions(+), 30 deletions(-) diff --git a/include/skyerrors.h b/include/skyerrors.h index 07660d44b..1addfee8f 100644 --- a/include/skyerrors.h +++ b/include/skyerrors.h @@ -112,6 +112,10 @@ #define SKY_ErrMaxDepthReached 0x02000043 #define SKY_ErrInvalidCoinType 0x02000044 #define SKY_ErrInvalidAccount 0x02000045 +#define SKY_ErrPathNoMaster 0x02000046 +#define SKY_ErrPathChildMaster 0x02000047 +#define SKY_ErrPathNodeNotNumber 0x02000048 +#define SKY_ErrPathNodeNumberTooLarge 0x02000049 // cli error codes #define SKY_ErrTemporaryInsufficientBalance 0x03000000 diff --git a/lib/cgo/libsky_error.go b/lib/cgo/libsky_error.go index 3c7fbe3d4..2b68c12f5 100644 --- a/lib/cgo/libsky_error.go +++ b/lib/cgo/libsky_error.go @@ -204,6 +204,15 @@ const ( SKY_ErrInvalidCoinType // ErrInvalidAccount account is >= 0x80000000 SKY_ErrInvalidAccount + // bip32.path + // SKY_ErrPathNoMaster HD wallet path does not start with m + SKY_ErrPathNoMaster + // SKY_ErrPathChildMaster HD wallet path contains m in a child node + SKY_ErrPathChildMaster + // SKY_ErrPathNodeNotNumber HD wallet path node is not a valid uint32 number + SKY_ErrPathNodeNotNumber + // SKY_ErrPathNodeNumberTooLarge HD wallet path node is >= 2^31 + SKY_ErrPathNodeNumberTooLarge ) // Error codes defined in cli package @@ -634,6 +643,10 @@ var ( bip32.ErrMaxDepthReached: SKY_ErrMaxDepthReached, bip44.ErrInvalidCoinType: SKY_ErrInvalidCoinType, bip44.ErrInvalidAccount: SKY_ErrInvalidAccount, + bip32.ErrPathNoMaster: SKY_ErrPathNoMaster, + bip32.ErrPathChildMaster: SKY_ErrPathChildMaster, + bip32.ErrPathNodeNotNumber: SKY_ErrPathNodeNotNumber, + bip32.ErrPathNodeNumberTooLarge: SKY_ErrPathNodeNumberTooLarge, } ) diff --git a/lib/cgo/tests/check_cipher.address.common.c b/lib/cgo/tests/check_cipher.address.common.c index 8f7092883..fd08635c9 100644 --- a/lib/cgo/tests/check_cipher.address.common.c +++ b/lib/cgo/tests/check_cipher.address.common.c @@ -115,9 +115,9 @@ Suite* common_check_cipher_address(void) tc = tcase_create("cipher.address.common"); tcase_add_checked_fixture(tc, setup, teardown); - // tcase_add_test(tc, TestAddressFromBytes); - // tcase_add_test(tc, TestAddressVerify); - // tcase_add_test(tc, TestAddressNull); + tcase_add_test(tc, TestAddressFromBytes); + tcase_add_test(tc, TestAddressVerify); + tcase_add_test(tc, TestAddressNull); suite_add_tcase(s, tc); tcase_set_timeout(tc, 150); diff --git a/lib/cgo/tests/check_cipher.bip32.bip32.c b/lib/cgo/tests/check_cipher.bip32.bip32.c index f3b4d7892..fd4b52f24 100755 --- a/lib/cgo/tests/check_cipher.bip32.bip32.c +++ b/lib/cgo/tests/check_cipher.bip32.bip32.c @@ -1043,6 +1043,72 @@ START_TEST(TestCantCreateHardenedPublicChild) ck_assert_int_eq(err, SKY_ErrHardenedChildPublicKey); } END_TEST + +typedef struct +{ + GoString seed; + GoString path; + GoString key; + GoUint32 err; +} cases_Str; + + +START_TEST(TestNewPrivateKeyFromPath) +{ + cases_Str cases[MAXBUFFER]; + // 0 + cases[0].seed.p = "6162636465666768696A6B6C6D6E6F707172737475767778797A"; + cases[0].seed.n = 52; + cases[0].path.p = "m"; + cases[0].path.n = 1; + cases[0].key.p = "xprv9s21ZrQH143K3GfuLFf1UxUB4GzmFav1hrzTG1bPorBTejryu4YfYVxZn6LNmwfvsi6uj1Wyv9vLDPsfKDuuqwEqYier1ZsbgWVd9NCieNv"; + cases[0].key.n = 111; + cases[0].err = SKY_OK; + // 1 + cases[1].seed.p = "6162636465666768696A6B6C6D6E6F707172737475767778797A"; + cases[1].seed.n = 52; + cases[1].path.p = "m/1'"; + cases[1].path.n = 4; + cases[1].key.p = "xprv9uWf8oyvCHcAUg3kSjSroz67s7M3qJRWmNcdVwYGf91GFsaAatsVVp1bjH7z3WiWevqB7WK92B415oBwcahjoMvvb4mopPyqZUDeVW4168c"; + cases[1].key.n = 111; + cases[1].err = SKY_OK; + // 2 + cases[2].seed.p = "6162636465666768696A6B6C6D6E6F707172737475767778797A"; + cases[2].seed.n = 52; + cases[2].path.p = "m/1'/foo"; + cases[2].path.n = 8; + cases[2].key.p = ""; + cases[2].key.n = 0; + cases[2].err = SKY_ErrPathNodeNotNumber; + // 3 + cases[3].seed.p = "6162"; + cases[3].seed.n = 4; + cases[3].path.p = "m/1"; + cases[3].path.n = 3; + cases[3].key.p = ""; + cases[3].key.n = 0; + cases[3].err = SKY_ErrInvalidSeedLength; + + for (size_t i = 0; i < 4; i++) { + cases_Str tc = cases[i]; + GoUint8 bufferseed[MAXBUFFER]; + GoSlice seed = {bufferseed, 0, MAXBUFFER}; + GoUint32 err = SKY_base58_String2Hex(tc.seed, &seed); + ck_assert(err == SKY_OK); + + PrivateKey__Handle k = 0; + err = SKY_bip32_NewPrivateKeyFromPath(seed, tc.path, &k); + ck_assert(err == tc.err); + if (err == SKY_OK) { + GoUint8 bufferk_string[MAXBUFFER]; + GoString k_string = {bufferk_string, 0}; + err = SKY_bip32_PrivateKey_String(k, &k_string); + ck_assert(err == SKY_OK); + ck_assert(isGoStringEq(tc.key, k_string)); + } + } +} +END_TEST Suite* cipher_bip32(void) { Suite* s = suite_create("Load cipher.bip32"); @@ -1056,6 +1122,7 @@ Suite* cipher_bip32(void) tcase_add_test(tc, TestDeserializePrivateInvalidStrings); tcase_add_test(tc, TestDeserializePublicInvalidStrings); tcase_add_test(tc, TestCantCreateHardenedPublicChild); + tcase_add_test(tc, TestNewPrivateKeyFromPath); suite_add_tcase(s, tc); tcase_set_timeout(tc, 150); diff --git a/lib/cgo/tests/check_coin.transactions.c b/lib/cgo/tests/check_coin.transactions.c index 40e6674b0..5472679bf 100644 --- a/lib/cgo/tests/check_coin.transactions.c +++ b/lib/cgo/tests/check_coin.transactions.c @@ -1169,15 +1169,15 @@ Suite* coin_transaction_fork(void) tc = tcase_create("coin.transaction_fork"); tcase_add_checked_fixture(tc, setup, teardown); - // #if __linux__ - // tcase_add_test_raise_signal(tc, TestTransactionPushInput, SKY_ABORT); - // tcase_add_test_raise_signal(tc, TestTransactionSignInputs, SKY_ABORT); - // tcase_add_test_raise_signal(tc, TestTransactionVerifyInput, SKY_ABORT); - // #elif __APPLE__ - // tcase_add_exit_test(tc, TestTransactionPushInput, SKY_ABORT); - // tcase_add_exit_test(tc, TestTransactionSignInputs, SKY_ABORT); - // tcase_add_test_raise_signal(tc, TestTransactionVerifyInput, SKY_ABORT); - // #endif +#if __linux__ + tcase_add_test_raise_signal(tc, TestTransactionPushInput, SKY_ABORT); + tcase_add_test_raise_signal(tc, TestTransactionSignInputs, SKY_ABORT); + tcase_add_test_raise_signal(tc, TestTransactionVerifyInput, SKY_ABORT); +#elif __APPLE__ + tcase_add_exit_test(tc, TestTransactionPushInput, SKY_ABORT); + tcase_add_exit_test(tc, TestTransactionSignInputs, SKY_ABORT); + tcase_add_test_raise_signal(tc, TestTransactionVerifyInput, SKY_ABORT); +#endif suite_add_tcase(s, tc); tcase_set_timeout(tc, 150); return s; diff --git a/lib/cgo/tests/test_main.c b/lib/cgo/tests/test_main.c index c7dd3c747..cb57f4e76 100644 --- a/lib/cgo/tests/test_main.c +++ b/lib/cgo/tests/test_main.c @@ -7,25 +7,25 @@ int main(void) int number_failed_fork = 0; SRunner* sr = srunner_create(common_check_cipher_address()); SRunner* sr_fork = srunner_create(coin_transaction_fork()); - // srunner_add_suite(sr, common_check_cipher_hash()); - // srunner_add_suite(sr, common_check_cipher_crypto()); - // srunner_add_suite(sr, cipher_bitcoin()); - // srunner_add_suite(sr, cipher_crypto()); - // srunner_add_suite(sr, cipher_secp256k1()); - // srunner_add_suite(sr, cipher_encrypt_scrypt_chacha20poly1305()); - // srunner_add_suite(sr, cipher_hash()); - // srunner_add_suite(sr, check_cipher_address()); + srunner_add_suite(sr, common_check_cipher_hash()); + srunner_add_suite(sr, common_check_cipher_crypto()); + srunner_add_suite(sr, cipher_bitcoin()); + srunner_add_suite(sr, cipher_crypto()); + srunner_add_suite(sr, cipher_secp256k1()); + srunner_add_suite(sr, cipher_encrypt_scrypt_chacha20poly1305()); + srunner_add_suite(sr, cipher_hash()); + srunner_add_suite(sr, check_cipher_address()); srunner_add_suite(sr, cipher_bip32()); - // srunner_add_suite(sr, cipher_bip44()); - // srunner_add_suite(sr, coin_blocks()); - // srunner_add_suite(sr, coin_coin()); - // srunner_add_suite(sr, coin_math()); - // srunner_add_suite(sr, coin_output()); - // srunner_add_suite(sr, coin_transaction()); - // srunner_add_suite(sr, param_distribution()); - // srunner_add_suite(sr, util_droplet()); - // srunner_add_suite(sr, util_fee()); - // srunner_add_suite(sr, cipher_testsuite()); + srunner_add_suite(sr, cipher_bip44()); + srunner_add_suite(sr, coin_blocks()); + srunner_add_suite(sr, coin_coin()); + srunner_add_suite(sr, coin_math()); + srunner_add_suite(sr, coin_output()); + srunner_add_suite(sr, coin_transaction()); + srunner_add_suite(sr, param_distribution()); + srunner_add_suite(sr, util_droplet()); + srunner_add_suite(sr, util_fee()); + srunner_add_suite(sr, cipher_testsuite()); srunner_set_fork_status(sr, CK_NOFORK); srunner_set_fork_status(sr_fork, CK_FORK); srunner_run_all(sr, CK_VERBOSE);