From 66fd0148bca6fb3898544c26064dab86f0cbc86c Mon Sep 17 00:00:00 2001 From: Alvaro Denis Date: Wed, 15 May 2019 17:18:52 -0400 Subject: [PATCH] refactor move TestDecodeBase58Address from lib/cgo/tests/check_cipher.address.c to lib/cgo/tests/check_cipher.address.common.c finally removed lib/cgo/tests/check_cipher.address.c as all address related test can be run against hardware wallet ref #34 --- lib/cgo/tests/check_cipher.address.c | 111 -------------------- lib/cgo/tests/check_cipher.address.common.c | 85 +++++++++++++++ 2 files changed, 85 insertions(+), 111 deletions(-) delete mode 100644 lib/cgo/tests/check_cipher.address.c diff --git a/lib/cgo/tests/check_cipher.address.c b/lib/cgo/tests/check_cipher.address.c deleted file mode 100644 index 6e8b7eacb..000000000 --- a/lib/cgo/tests/check_cipher.address.c +++ /dev/null @@ -1,111 +0,0 @@ -#include "libskycoin.h" -#include "skyerrors.h" -#include "skystring.h" -#include "skytest.h" -#include -#include -#include -#include - -#define SKYCOIN_ADDRESS_VALID "2GgFvqoyk9RjwVzj8tqfcXVXB4orBwoc9qv" - -// TestSuite(cipher_address, .init = setup, .fini = teardown); - -extern unsigned char buff[1024]; - -START_TEST(TestDecodeBase58Address) -{ - GoString strAddr = {SKYCOIN_ADDRESS_VALID, 35}; - cipher__Address addr; - GoUint32 err = SKY_cipher_DecodeBase58Address(strAddr, &addr); - ck_assert_int_eq(err, SKY_OK); - - char tempStr[50]; - int errorcode; - - // preceding whitespace is invalid - strcpy(tempStr, " "); - strcat(tempStr, SKYCOIN_ADDRESS_VALID); - strAddr.p = tempStr; - strAddr.n = strlen(tempStr); - errorcode = SKY_cipher_DecodeBase58Address(strAddr, &addr); - ck_assert_msg(errorcode == SKY_ERROR, "preceding whitespace is invalid"); - - // preceding zeroes are invalid - strcpy(tempStr, "000"); - strcat(tempStr, SKYCOIN_ADDRESS_VALID); - strAddr.p = tempStr; - strAddr.n = strlen(tempStr); - errorcode = SKY_cipher_DecodeBase58Address(strAddr, &addr); - ck_assert_msg(errorcode == SKY_ERROR, "leading zeroes prefix are invalid"); - - // trailing whitespace is invalid - strcpy(tempStr, SKYCOIN_ADDRESS_VALID); - strcat(tempStr, " "); - strAddr.p = tempStr; - strAddr.n = strlen(tempStr); - errorcode = SKY_cipher_DecodeBase58Address(strAddr, &addr); - ck_assert_msg(errorcode == SKY_ERROR, "trailing whitespace is invalid"); - - // trailing zeroes are invalid - strcpy(tempStr, SKYCOIN_ADDRESS_VALID); - strcat(tempStr, "000"); - strAddr.p = tempStr; - strAddr.n = strlen(tempStr); - errorcode = SKY_cipher_DecodeBase58Address(strAddr, &addr); - ck_assert_msg(errorcode == SKY_ERROR, "trailing zeroes suffix are invalid"); - - cipher__PubKey p; - cipher__SecKey s; - errorcode = SKY_cipher_GenerateKeyPair(&p, &s); - ck_assert(errorcode == SKY_OK); - cipher__Address a; - errorcode = SKY_cipher_AddressFromPubKey(&p, &a); - ck_assert(errorcode == SKY_OK); - GoSlice b; - coin__UxArray Cub; - Cub.data = buff; - Cub.len = 0; - Cub.cap = sizeof(buff); - errorcode = SKY_cipher_Address_Bytes(&addr, &Cub); - ck_assert_msg(errorcode == SKY_OK, "Fail SKY_cipher_Address_Bytes"); - b.cap = Cub.cap; - b.data = Cub.data; - b.len = Cub.len; - - int len_b = b.len; - char bufferHead[1024]; - GoString_ h = {bufferHead, 0}; - b.len = (GoInt)(len_b / 2); - errorcode = SKY_base58_Hex2Base58(b, &h); - ck_assert(errorcode == SKY_OK); - char bufferHeadTmp[1024]; - GoString tmph = {bufferHeadTmp, 0}; - tmph.n = h.n; - tmph.p = h.p; - errorcode = SKY_cipher_DecodeBase58Address(tmph, &addr); - ck_assert_msg(errorcode == SKY_ErrAddressInvalidLength, "Fail %X", errorcode); - b.len = len_b; - errorcode = SKY_base58_Hex2Base58(b, &h); - ck_assert(errorcode == SKY_OK); - tmph.n = h.n; - tmph.p = h.p; - errorcode = SKY_cipher_DecodeBase58Address(tmph, &addr); - ck_assert_msg(errorcode == SKY_OK, "Fail %X", errorcode); -} -END_TEST - -// define test suite and cases -Suite* cipher_address(void) -{ - Suite* s = suite_create("Load cipher.address"); - TCase* tc; - - tc = tcase_create("cipher.address"); - tcase_add_checked_fixture(tc, setup, teardown); - tcase_add_test(tc, TestDecodeBase58Address); - suite_add_tcase(s, tc); - tcase_set_timeout(tc, 150); - - return s; -} \ No newline at end of file diff --git a/lib/cgo/tests/check_cipher.address.common.c b/lib/cgo/tests/check_cipher.address.common.c index 3e783fa0d..bce877630 100644 --- a/lib/cgo/tests/check_cipher.address.common.c +++ b/lib/cgo/tests/check_cipher.address.common.c @@ -8,6 +8,8 @@ #include "skystring.h" #include "skytest.h" +#define SKYCOIN_ADDRESS_VALID "2GgFvqoyk9RjwVzj8tqfcXVXB4orBwoc9qv" + // buffer big enough to hold all kind of data needed by test cases unsigned char buff[1024]; @@ -168,6 +170,88 @@ START_TEST(TestAddressFromBytes) } END_TEST +START_TEST(TestDecodeBase58Address) +{ + GoString strAddr = {SKYCOIN_ADDRESS_VALID, 35}; + cipher__Address addr; + GoUint32 err = SKY_cipher_DecodeBase58Address(strAddr, &addr); + ck_assert_int_eq(err, SKY_OK); + + char tempStr[50]; + int errorcode; + + // preceding whitespace is invalid + strcpy(tempStr, " "); + strcat(tempStr, SKYCOIN_ADDRESS_VALID); + strAddr.p = tempStr; + strAddr.n = strlen(tempStr); + errorcode = SKY_cipher_DecodeBase58Address(strAddr, &addr); + ck_assert_msg(errorcode == SKY_ERROR, "preceding whitespace is invalid"); + + // preceding zeroes are invalid + strcpy(tempStr, "000"); + strcat(tempStr, SKYCOIN_ADDRESS_VALID); + strAddr.p = tempStr; + strAddr.n = strlen(tempStr); + errorcode = SKY_cipher_DecodeBase58Address(strAddr, &addr); + ck_assert_msg(errorcode == SKY_ERROR, "leading zeroes prefix are invalid"); + + // trailing whitespace is invalid + strcpy(tempStr, SKYCOIN_ADDRESS_VALID); + strcat(tempStr, " "); + strAddr.p = tempStr; + strAddr.n = strlen(tempStr); + errorcode = SKY_cipher_DecodeBase58Address(strAddr, &addr); + ck_assert_msg(errorcode == SKY_ERROR, "trailing whitespace is invalid"); + + // trailing zeroes are invalid + strcpy(tempStr, SKYCOIN_ADDRESS_VALID); + strcat(tempStr, "000"); + strAddr.p = tempStr; + strAddr.n = strlen(tempStr); + errorcode = SKY_cipher_DecodeBase58Address(strAddr, &addr); + ck_assert_msg(errorcode == SKY_ERROR, "trailing zeroes suffix are invalid"); + + cipher__PubKey p; + cipher__SecKey s; + errorcode = SKY_cipher_GenerateKeyPair(&p, &s); + ck_assert(errorcode == SKY_OK); + cipher__Address a; + errorcode = SKY_cipher_AddressFromPubKey(&p, &a); + ck_assert(errorcode == SKY_OK); + GoSlice b; + coin__UxArray Cub; + Cub.data = buff; + Cub.len = 0; + Cub.cap = sizeof(buff); + errorcode = SKY_cipher_Address_Bytes(&addr, &Cub); + ck_assert_msg(errorcode == SKY_OK, "Fail SKY_cipher_Address_Bytes"); + b.cap = Cub.cap; + b.data = Cub.data; + b.len = Cub.len; + + int len_b = b.len; + char bufferHead[1024]; + GoString_ h = {bufferHead, 0}; + b.len = (GoInt)(len_b / 2); + errorcode = SKY_base58_Hex2Base58(b, &h); + ck_assert(errorcode == SKY_OK); + char bufferHeadTmp[1024]; + GoString tmph = {bufferHeadTmp, 0}; + tmph.n = h.n; + tmph.p = h.p; + errorcode = SKY_cipher_DecodeBase58Address(tmph, &addr); + ck_assert_msg(errorcode == SKY_ErrAddressInvalidLength, "Fail %X", errorcode); + b.len = len_b; + errorcode = SKY_base58_Hex2Base58(b, &h); + ck_assert(errorcode == SKY_OK); + tmph.n = h.n; + tmph.p = h.p; + errorcode = SKY_cipher_DecodeBase58Address(tmph, &addr); + ck_assert_msg(errorcode == SKY_OK, "Fail %X", errorcode); +} +END_TEST + // define test suite and cases Suite *common_check_cipher_address(void) { @@ -180,6 +264,7 @@ Suite *common_check_cipher_address(void) tcase_add_test(tc, TestAddressBulk); tcase_add_test(tc, TestAddressNull); tcase_add_test(tc, TestAddressFromBytes); + tcase_add_test(tc, TestDecodeBase58Address); suite_add_tcase(s, tc); tcase_set_timeout(tc, 150);