diff --git a/lib/cgo/tests/check_cipher.address.c b/lib/cgo/tests/check_cipher.address.c new file mode 100644 index 000000000..d61b977bd --- /dev/null +++ b/lib/cgo/tests/check_cipher.address.c @@ -0,0 +1,200 @@ +#include +#include + +#include +#include + +#include "libskycoin.h" +#include "skyerrors.h" +#include "skyassert.h" +#include "skystring.h" +#include "skytest.h" + +#define SKYCOIN_ADDRESS_VALID "2GgFvqoyk9RjwVzj8tqfcXVXB4orBwoc9qv" + + +START_TEST(TestAddressRoundtrip) +{ + cipher__PubKey p; + cipher__SecKey s; + GoUint32_ error; + error = SKY_cipher_GenerateKeyPair(&p, &s); + ck_assert_int_eq(error, SKY_OK); + cipher__Address a; + error = SKY_cipher_AddressFromPubKey(&p, &a); + ck_assert_int_eq(error, SKY_OK); + unsigned char buffera_bytes[1024]; + coin__UxArray a_bytes = {buffera_bytes, 0, 1024}; + error = SKY_cipher_Address_Bytes(&a, &a_bytes); + ck_assert_int_eq(error, SKY_OK); + cipher__Address a2; + GoSlice TMP_a_bytes = {a_bytes.data, a_bytes.len, a_bytes.cap}; + error = SKY_cipher_AddressFromBytes(TMP_a_bytes, &a2); + ck_assert_int_eq(error, SKY_OK); + ck_assert(isAddressEq(&a, &a2)); + GoString_ str_a; + GoString_ str_a2; + error = SKY_cipher_Address_String(&a, &str_a); + error = SKY_cipher_Address_String(&a2, &str_a2); + ck_assert(isGoString_Eq(str_a2, str_a)); +} +END_TEST + +START_TEST(TestAddressString) +{ + cipher__PubKey pk; + cipher__SecKey sk; + cipher__Address addr, addr2, addr3; + GoUint8 buff[1024] = {0}; + GoString str = {buff, 0}; + + GoUint32 err = SKY_cipher_GenerateKeyPair(&pk, &sk); + ck_assert(err == SKY_OK); + err = SKY_cipher_AddressFromPubKey(&pk, &addr); + ck_assert(err == SKY_OK); + GoString_ tmpstr = {str.p, str.n}; + + err = SKY_cipher_Address_String(&addr, &tmpstr); + ck_assert(err == SKY_OK); + str.n = tmpstr.n; + str.p = tmpstr.p; + ck_assert(SKY_cipher_DecodeBase58Address(str, &addr2) == SKY_OK); + ck_assert(isAddressEq(&addr, &addr2)); + + SKY_cipher_Address_String(&addr2, (GoString_*)&str); + ck_assert(SKY_cipher_DecodeBase58Address(str, &addr3) == SKY_OK); + ck_assert(isAddressEq(&addr, &addr2)); +} +END_TEST + +START_TEST(TestAddressBulk) +{ + unsigned char buff[50]; + GoSlice slice = {buff, 0, 50}; + int i; + for (i = 0; i < 1024; ++i) { + GoUint32 err; + randBytes(&slice, 32); + cipher__PubKey pubkey; + cipher__SecKey seckey; + err = SKY_cipher_GenerateDeterministicKeyPair(slice, &pubkey, &seckey); + ck_assert(err == SKY_OK); + cipher__Address addr; + err = SKY_cipher_AddressFromPubKey(&pubkey, &addr); + ck_assert(err == SKY_OK); + err = SKY_cipher_Address_Verify(&addr, &pubkey); + ck_assert(err == SKY_OK); + + GoString_ tempstrAddr; + err = SKY_cipher_Address_String(&addr, &tempstrAddr); + ck_assert(err == SKY_OK); + registerMemCleanup((void*)tempstrAddr.p); + cipher__Address addr2; + GoString strAddr; + strAddr.n = tempstrAddr.n; + strAddr.p = tempstrAddr.p; + err = SKY_cipher_DecodeBase58Address(strAddr, &addr2); + ck_assert(err == SKY_OK); + ck_assert(isAddressEq(&addr, &addr2)); + } +} +END_TEST + +START_TEST(TestDecodeBase58Address) +{ + GoString strAddr = {.p = SKYCOIN_ADDRESS_VALID, .n = 35}; + cipher__Address addr; + GoUint32 err = SKY_cipher_DecodeBase58Address(strAddr, &addr); + ck_assert_int_eq(err, SKY_OK); + GoUint8 buff[1024]; + char tempStr[50]; + GoUint32 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; + h.n = sizeof(bufferHead); + 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* check_cipher_address(void) +{ + Suite* s = suite_create("Load check_cipher_address.address"); + TCase* tc; + + tc = tcase_create("check_cipher.address"); + tcase_add_checked_fixture(tc, setup, teardown); + tcase_add_test(tc, TestAddressRoundtrip); + tcase_add_test(tc, TestAddressString); + tcase_add_test(tc, TestAddressBulk); + tcase_add_test(tc, TestDecodeBase58Address); + + return s; +} diff --git a/lib/cgo/tests/check_cipher.address.common.c b/lib/cgo/tests/check_cipher.address.common.c index bd5c7e4ac..fe9796651 100644 --- a/lib/cgo/tests/check_cipher.address.common.c +++ b/lib/cgo/tests/check_cipher.address.common.c @@ -12,34 +12,6 @@ #define SKYCOIN_ADDRESS_VALID "2GgFvqoyk9RjwVzj8tqfcXVXB4orBwoc9qv" - -START_TEST(TestAddressRoundtrip) -{ - cipher__PubKey p; - cipher__SecKey s; - GoUint32_ error; - error = SKY_cipher_GenerateKeyPair(&p, &s); - ck_assert_int_eq(error, SKY_OK); - cipher__Address a; - error = SKY_cipher_AddressFromPubKey(&p, &a); - ck_assert_int_eq(error, SKY_OK); - unsigned char buffera_bytes[1024]; - coin__UxArray a_bytes = {buffera_bytes, 0, 1024}; - error = SKY_cipher_Address_Bytes(&a, &a_bytes); - ck_assert_int_eq(error, SKY_OK); - cipher__Address a2; - GoSlice TMP_a_bytes = {a_bytes.data, a_bytes.len, a_bytes.cap}; - error = SKY_cipher_AddressFromBytes(TMP_a_bytes, &a2); - ck_assert_int_eq(error, SKY_OK); - ck_assert(isAddressEq(&a, &a2)); - GoString_ str_a; - GoString_ str_a2; - error = SKY_cipher_Address_String(&a, &str_a); - error = SKY_cipher_Address_String(&a2, &str_a2); - ck_assert(isGoString_Eq(str_a2, str_a)); -} -END_TEST - START_TEST(TestAddressVerify) { cipher__PubKey pubkey; @@ -69,66 +41,6 @@ START_TEST(TestAddressVerify) } END_TEST -START_TEST(TestAddressString) -{ - cipher__PubKey pk; - cipher__SecKey sk; - cipher__Address addr, addr2, addr3; - GoUint8 buff[1024]; - GoString str = {buff, 0}; - - GoUint32 err = SKY_cipher_GenerateKeyPair(&pk, &sk); - ck_assert(err == SKY_OK); - err = SKY_cipher_AddressFromPubKey(&pk, &addr); - ck_assert(err == SKY_OK); - GoString_ tmpstr = {str.p, str.n}; - - err = SKY_cipher_Address_String(&addr, &tmpstr); - ck_assert(err == SKY_OK); - str.n = tmpstr.n; - str.p = tmpstr.p; - ck_assert(SKY_cipher_DecodeBase58Address(str, &addr2) == SKY_OK); - ck_assert(isAddressEq(&addr, &addr2)); - - SKY_cipher_Address_String(&addr2, (GoString_*)&str); - ck_assert(SKY_cipher_DecodeBase58Address(str, &addr3) == SKY_OK); - ck_assert(isAddressEq(&addr, &addr2)); -} -END_TEST - -START_TEST(TestAddressBulk) -{ - unsigned char buff[50]; - GoSlice slice = {buff, 0, 50}; - int i; - for (i = 0; i < 1024; ++i) { - GoUint32 err; - randBytes(&slice, 32); - cipher__PubKey pubkey; - cipher__SecKey seckey; - err = SKY_cipher_GenerateDeterministicKeyPair(slice, &pubkey, &seckey); - ck_assert(err == SKY_OK); - cipher__Address addr; - err = SKY_cipher_AddressFromPubKey(&pubkey, &addr); - ck_assert(err == SKY_OK); - err = SKY_cipher_Address_Verify(&addr, &pubkey); - ck_assert(err == SKY_OK); - - GoString_ tempstrAddr; - err = SKY_cipher_Address_String(&addr, &tempstrAddr); - ck_assert(err == SKY_OK); - registerMemCleanup((void*)tempstrAddr.p); - cipher__Address addr2; - GoString strAddr; - strAddr.n = tempstrAddr.n; - strAddr.p = tempstrAddr.p; - err = SKY_cipher_DecodeBase58Address(strAddr, &addr2); - ck_assert(err == SKY_OK); - ck_assert(isAddressEq(&addr, &addr2)); - } -} -END_TEST - START_TEST(TestAddressNull) { cipher__Address a; @@ -198,103 +110,17 @@ 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); - GoUint8 buff[1024]; - 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) { Suite* s = suite_create("Load common_check_cipher_address.address"); TCase* tc; - tc = tcase_create("check_cipher.address"); + tc = tcase_create("check_cipher.address.common"); tcase_add_checked_fixture(tc, setup, teardown); - tcase_add_test(tc, TestAddressRoundtrip); tcase_add_test(tc, TestAddressVerify); - tcase_add_test(tc, TestAddressString); - 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); diff --git a/lib/cgo/tests/test_main.c b/lib/cgo/tests/test_main.c index 3caa18cb9..cb57f4e76 100644 --- a/lib/cgo/tests/test_main.c +++ b/lib/cgo/tests/test_main.c @@ -14,6 +14,7 @@ int main(void) 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()); diff --git a/lib/cgo/tests/test_main.h b/lib/cgo/tests/test_main.h index 29c0a1973..9cf1259b0 100644 --- a/lib/cgo/tests/test_main.h +++ b/lib/cgo/tests/test_main.h @@ -31,5 +31,6 @@ Suite *coin_transaction_fork(void); Suite *param_distribution(void); Suite *util_droplet(void); Suite *util_fee(void); +Suite* check_cipher_address(void); #endif \ No newline at end of file