diff --git a/lib/cgo/tests/check_cipher.address.common.c b/lib/cgo/tests/check_cipher.address.common.c index 7a87dfb77..bce877630 100644 --- a/lib/cgo/tests/check_cipher.address.common.c +++ b/lib/cgo/tests/check_cipher.address.common.c @@ -47,8 +47,7 @@ START_TEST(TestAddressString) cipher__PubKey pk; cipher__SecKey sk; cipher__Address addr, addr2, addr3; - char buf[1024] = {0}; - GoString str = {.p=buff, .n=sizeof(buff)}; + GoString str = {buff, 0}; GoUint32 err = SKY_cipher_GenerateKeyPair(&pk, &sk); ck_assert(err == SKY_OK); @@ -132,23 +131,19 @@ START_TEST(TestAddressFromBytes) cipher__SecKey sk; cipher__PubKey pk; GoSlice bytes; - coin__UxArray tempBytes; + GoSlice_ tempBytes; GoUint32 err = SKY_cipher_GenerateKeyPair(&pk, &sk); ck_assert(err == SKY_OK); SKY_cipher_AddressFromPubKey(&pk, &addr); tempBytes.data = buff; - tempBytes.len = sizeof(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, sizeof(*buff)); - bytes.cap = tempBytes.cap; - bytes.len = tempBytes.len; - bytes.data = calloc(tempBytes.cap, sizeof(*buff)); - memcpy(bytes.data, tempBytes.data, tempBytes.len); + copyGoSlice_toGoSlice(&bytes, &tempBytes, tempBytes.len); err = SKY_cipher_AddressFromBytes(bytes, &addr2); ck_assert_msg(err == SKY_OK, "convert bytes to SKY address"); diff --git a/lib/cgo/tests/check_cipher.crypto.common.c b/lib/cgo/tests/check_cipher.crypto.common.c index 7fa9ca7ad..792dc81c6 100644 --- a/lib/cgo/tests/check_cipher.crypto.common.c +++ b/lib/cgo/tests/check_cipher.crypto.common.c @@ -16,8 +16,7 @@ START_TEST(TestNewPubKey) cipher__SecKey sk; slice.data = buff; - slice.len = 0; - slice.cap = sizeof(buff); + slice.cap = 101; randBytes(&slice, 31); slice.len = 31; @@ -292,7 +291,7 @@ START_TEST(TestSigHex) ck_assert(errorcode == SKY_OK); char buffer[100]; - GoString_ tmp_str = {.p = buffer, .n = sizeof(buffer)}; + GoString_ tmp_str = {buffer, 0}; SKY_cipher_Sig_Hex(&s, &tmp_str); str.p = tmp_str.p; str.n = tmp_str.n; diff --git a/lib/cgo/tests/check_cipher.hash.common.c b/lib/cgo/tests/check_cipher.hash.common.c index 4f0687676..9a698d1e1 100644 --- a/lib/cgo/tests/check_cipher.hash.common.c +++ b/lib/cgo/tests/check_cipher.hash.common.c @@ -103,16 +103,12 @@ START_TEST(TestSHA256KnownValue) SKY_cipher_SumSHA256(slice_input, &sha); - char raw_buf[sizeof(cipher__SHA256) * 2 + 1] = {0}; - GoString_ tmp_output = { - .p = raw_buf, - .n = sizeof(raw_buf) - }; + GoString_ tmp_output; SKY_cipher_SHA256_Hex(&sha, &tmp_output); registerMemCleanup((void*)tmp_output.p); - ck_assert_str_eq(tmp_output.p, vals[i].output); + ck_assert(strcmp(tmp_output.p, vals[i].output) == SKY_OK); } } END_TEST @@ -147,26 +143,18 @@ START_TEST(TestSHA256Hex) memset(&h, 0, sizeof(h)); randBytes(&slice, 32); SKY_cipher_SHA256_Set(&h, slice); - char raw_buf[sizeof(cipher__SHA256) * 2 + 1] = {0}; - GoString_ s = { - .p = raw_buf, - .n = sizeof(raw_buf) - }; - + GoString_ s; + SKY_cipher_SHA256_Hex(&h, &s); registerMemCleanup((void*)s.p); cipher__SHA256 h2; - GoString tmpS = {.p = s.p, .n = s.n}; + GoString tmpS = {s.p, s.n}; error = SKY_cipher_SHA256FromHex(tmpS, &h2); ck_assert(error == SKY_OK); ck_assert(isU8Eq(h, h2, 32)); - char raw_buf2[sizeof(cipher__SHA256) * 2 + 1] = {0}; - GoString_ s2 = { - .p = raw_buf2, - .n = sizeof(raw_buf2) - }; + GoString_ s2; SKY_cipher_SHA256_Hex(&h2, &s2); registerMemCleanup((void*)s2.p); ck_assert_str_eq(s.p, s2.p); @@ -260,12 +248,12 @@ Suite *common_check_cipher_hash(void) TCase *tc; tc = tcase_create("check_cipher.hash"); + tcase_add_test(tc, TestSHA256Set); tcase_add_test(tc, TestAddSHA256); tcase_add_test(tc, TestHashRipemd160); tcase_add_test(tc, TestSHA256KnownValue); tcase_add_test(tc, TestSumSHA256); tcase_add_test(tc, TestSHA256Hex); - tcase_add_test(tc, TestSHA256Set); tcase_add_test(tc, TestSHA256FromHex); tcase_add_test(tc, TestSHA256Null); suite_add_tcase(s, tc); diff --git a/lib/cgo/tests/testutils/common.c b/lib/cgo/tests/testutils/common.c index 171fd5296..700e2a7f8 100644 --- a/lib/cgo/tests/testutils/common.c +++ b/lib/cgo/tests/testutils/common.c @@ -32,14 +32,14 @@ void * registerMemCleanup(void *p) { return p; } -// FIXME this should be documented as memory initializer -// a better approach can be follow the RAII pattern instead of using the -// registerMemCleanup int copyGoSlice_toGoSlice(GoSlice* pdest, GoSlice_* psource, int elem_size){ pdest->len = psource->len; - pdest->cap = psource->cap; - pdest->data = calloc(psource->cap, elem_size); - registerMemCleanup(pdest->data); - memcpy(pdest->data, psource->data, psource->len * elem_size); + pdest->cap = psource->len; + int size = pdest->len * elem_size; + pdest->data = malloc(size); + if( pdest->data == NULL ) + return SKY_ERROR; + registerMemCleanup( pdest->data ); + memcpy(pdest->data, psource->data, size ); return SKY_OK; }