From 9a90451bcd2c70ce403d48a70062bf154579b1c7 Mon Sep 17 00:00:00 2001 From: Alvaro Denis Date: Wed, 15 May 2019 16:50:32 -0400 Subject: [PATCH] refactor move TestAddressFromBytes from lib/cgo/tests/check_cipher.address.c to lib/cgo/tests/check_cipher.address.common.c ref #34 --- lib/cgo/tests/check_cipher.address.c | 46 --------------------- lib/cgo/tests/check_cipher.address.common.c | 46 +++++++++++++++++++++ 2 files changed, 46 insertions(+), 46 deletions(-) diff --git a/lib/cgo/tests/check_cipher.address.c b/lib/cgo/tests/check_cipher.address.c index 61bf5506d..6e8b7eacb 100644 --- a/lib/cgo/tests/check_cipher.address.c +++ b/lib/cgo/tests/check_cipher.address.c @@ -95,51 +95,6 @@ START_TEST(TestDecodeBase58Address) } END_TEST -START_TEST(TestAddressFromBytes) -{ - cipher__Address addr, addr2; - cipher__SecKey sk; - cipher__PubKey pk; - GoSlice bytes; - GoSlice_ tempBytes; - - GoUint32 err = SKY_cipher_GenerateKeyPair(&pk, &sk); - ck_assert(err == SKY_OK); - SKY_cipher_AddressFromPubKey(&pk, &addr); - - tempBytes.data = buff; - tempBytes.len = 0; - tempBytes.cap = sizeof(buff); - - SKY_cipher_Address_Bytes(&addr, &tempBytes); - ck_assert_msg(tempBytes.len > 0, "address bytes written"); - copyGoSlice_toGoSlice(&bytes, &tempBytes, tempBytes.len); - err = SKY_cipher_AddressFromBytes(bytes, &addr2); - ck_assert_msg(err == SKY_OK, "convert bytes to SKY address"); - - ck_assert_msg(isAddressEq(&addr, &addr2), "Not equal Address"); - - int bytes_len = bytes.len; - - bytes.len = bytes.len - 2; - ck_assert_msg(SKY_cipher_AddressFromBytes(bytes, &addr2) == - SKY_ErrAddressInvalidLength, - "no SKY address due to short bytes length"); - - bytes.len = bytes_len; - ((char*)bytes.data)[bytes.len - 1] = '2'; - err = SKY_cipher_AddressFromBytes(bytes, &addr2); - ck_assert_msg(err == SKY_ErrAddressInvalidChecksum, - "no SKY address due to corrupted bytes %X", err); - - addr.Version = 2; - SKY_cipher_Address_Bytes(&addr, &tempBytes); - copyGoSlice_toGoSlice(&bytes, &tempBytes, tempBytes.len); - err = SKY_cipher_AddressFromBytes(bytes, &addr2); - ck_assert_msg(err == SKY_ErrAddressInvalidVersion, "Invalid version"); -} -END_TEST - // define test suite and cases Suite* cipher_address(void) { @@ -149,7 +104,6 @@ Suite* cipher_address(void) tc = tcase_create("cipher.address"); tcase_add_checked_fixture(tc, setup, teardown); tcase_add_test(tc, TestDecodeBase58Address); - tcase_add_test(tc, TestAddressFromBytes); suite_add_tcase(s, tc); tcase_set_timeout(tc, 150); diff --git a/lib/cgo/tests/check_cipher.address.common.c b/lib/cgo/tests/check_cipher.address.common.c index 938e06bdf..3e783fa0d 100644 --- a/lib/cgo/tests/check_cipher.address.common.c +++ b/lib/cgo/tests/check_cipher.address.common.c @@ -123,6 +123,51 @@ START_TEST(TestAddressNull) } END_TEST +START_TEST(TestAddressFromBytes) +{ + cipher__Address addr, addr2; + cipher__SecKey sk; + cipher__PubKey pk; + GoSlice bytes; + GoSlice_ tempBytes; + + GoUint32 err = SKY_cipher_GenerateKeyPair(&pk, &sk); + ck_assert(err == SKY_OK); + SKY_cipher_AddressFromPubKey(&pk, &addr); + + tempBytes.data = buff; + tempBytes.len = 0; + tempBytes.cap = sizeof(buff); + + SKY_cipher_Address_Bytes(&addr, &tempBytes); + ck_assert_msg(tempBytes.len > 0, "address bytes written"); + copyGoSlice_toGoSlice(&bytes, &tempBytes, tempBytes.len); + err = SKY_cipher_AddressFromBytes(bytes, &addr2); + ck_assert_msg(err == SKY_OK, "convert bytes to SKY address"); + + ck_assert_msg(isAddressEq(&addr, &addr2), "Not equal Address"); + + int bytes_len = bytes.len; + + bytes.len = bytes.len - 2; + ck_assert_msg(SKY_cipher_AddressFromBytes(bytes, &addr2) == + SKY_ErrAddressInvalidLength, + "no SKY address due to short bytes length"); + + bytes.len = bytes_len; + ((char*)bytes.data)[bytes.len - 1] = '2'; + err = SKY_cipher_AddressFromBytes(bytes, &addr2); + ck_assert_msg(err == SKY_ErrAddressInvalidChecksum, + "no SKY address due to corrupted bytes %X", err); + + addr.Version = 2; + SKY_cipher_Address_Bytes(&addr, &tempBytes); + copyGoSlice_toGoSlice(&bytes, &tempBytes, tempBytes.len); + err = SKY_cipher_AddressFromBytes(bytes, &addr2); + ck_assert_msg(err == SKY_ErrAddressInvalidVersion, "Invalid version"); +} +END_TEST + // define test suite and cases Suite *common_check_cipher_address(void) { @@ -134,6 +179,7 @@ Suite *common_check_cipher_address(void) tcase_add_test(tc, TestAddressString); tcase_add_test(tc, TestAddressBulk); tcase_add_test(tc, TestAddressNull); + tcase_add_test(tc, TestAddressFromBytes); suite_add_tcase(s, tc); tcase_set_timeout(tc, 150);