From 6dc0accb30d6e752956a7e383d766602c6cf04d4 Mon Sep 17 00:00:00 2001 From: Alvaro Denis Date: Thu, 16 May 2019 15:45:07 -0400 Subject: [PATCH] recator move TestNewPubKey from lib/cgo/tests/check_cipher.crypto.c to lib/cgo/tests/check_cipher.crypto.common.c ref #34 --- lib/cgo/tests/check_cipher.crypto.c | 41 ------------- lib/cgo/tests/check_cipher.crypto.common.c | 68 ++++++++++++++++++++++ lib/cgo/tests/test_main.c | 1 + lib/cgo/tests/test_main_common.h | 1 + lib/cgo/tests/test_main_hw.c | 1 + 5 files changed, 71 insertions(+), 41 deletions(-) create mode 100644 lib/cgo/tests/check_cipher.crypto.common.c diff --git a/lib/cgo/tests/check_cipher.crypto.c b/lib/cgo/tests/check_cipher.crypto.c index 13eeec93d..822fb42e8 100644 --- a/lib/cgo/tests/check_cipher.crypto.c +++ b/lib/cgo/tests/check_cipher.crypto.c @@ -9,49 +9,9 @@ // TestSuite(cipher_crypto, .init = setup, .fini = teardown); -START_TEST(TestNewPubKey) -{ - unsigned char buff[101]; - GoSlice slice; - cipher__PubKey pk, pk2; - cipher__SecKey sk; - - slice.data = buff; - slice.cap = 101; - - randBytes(&slice, 31); - slice.len = 31; - unsigned int errorcode = SKY_cipher_NewPubKey(slice, &pk); - ck_assert_msg(errorcode == SKY_ErrInvalidLengthPubKey, "31 random bytes"); - - randBytes(&slice, 32); - errorcode = SKY_cipher_NewPubKey(slice, &pk); - ck_assert_msg(errorcode == SKY_ErrInvalidLengthPubKey, "32 random bytes"); - randBytes(&slice, 34); - errorcode = SKY_cipher_NewPubKey(slice, &pk); - ck_assert_msg(errorcode == SKY_ErrInvalidLengthPubKey, "34 random bytes"); - slice.len = 0; - errorcode = SKY_cipher_NewPubKey(slice, &pk); - ck_assert_msg(errorcode == SKY_ErrInvalidLengthPubKey, "0 random bytes"); - randBytes(&slice, 100); - errorcode = SKY_cipher_NewPubKey(slice, &pk); - ck_assert_msg(errorcode == SKY_ErrInvalidLengthPubKey, "100 random bytes"); - - randBytes(&slice, 33); - errorcode = SKY_cipher_NewPubKey(slice, &pk); - ck_assert_msg(errorcode != SKY_OK, "33 random bytes"); - - SKY_cipher_GenerateKeyPair(&pk, &sk); - GoSlice buffer = {pk, sizeof(pk), sizeof(pk)}; - errorcode = SKY_cipher_NewPubKey(buffer, &pk2); - ck_assert_msg(errorcode == SKY_OK); - - ck_assert(isPubKeyEq(&pk, &pk2)); -} -END_TEST START_TEST(TestPubKeyFromHex) { @@ -858,7 +818,6 @@ Suite* cipher_crypto(void) tc = tcase_create("cipher.crypto"); tcase_add_checked_fixture(tc, setup, teardown); - tcase_add_test(tc, TestNewPubKey); tcase_add_test(tc, TestPubKeyFromHex); tcase_add_test(tc, TestPubKeyHex); tcase_add_test(tc, TestPubKeyVerify); diff --git a/lib/cgo/tests/check_cipher.crypto.common.c b/lib/cgo/tests/check_cipher.crypto.common.c new file mode 100644 index 000000000..9bf38bf6d --- /dev/null +++ b/lib/cgo/tests/check_cipher.crypto.common.c @@ -0,0 +1,68 @@ +#include +#include + +#include +#include "libskycoin.h" +#include "skyerrors.h" +#include "skyassert.h" +#include "skystring.h" +#include "skytest.h" + +START_TEST(TestNewPubKey) +{ + unsigned char buff[101]; + GoSlice slice; + cipher__PubKey pk, pk2; + cipher__SecKey sk; + + slice.data = buff; + slice.cap = 101; + + randBytes(&slice, 31); + slice.len = 31; + unsigned int errorcode = SKY_cipher_NewPubKey(slice, &pk); + ck_assert_msg(errorcode == SKY_ErrInvalidLengthPubKey, "31 random bytes"); + + randBytes(&slice, 32); + errorcode = SKY_cipher_NewPubKey(slice, &pk); + ck_assert_msg(errorcode == SKY_ErrInvalidLengthPubKey, "32 random bytes"); + + randBytes(&slice, 34); + errorcode = SKY_cipher_NewPubKey(slice, &pk); + ck_assert_msg(errorcode == SKY_ErrInvalidLengthPubKey, "34 random bytes"); + + slice.len = 0; + errorcode = SKY_cipher_NewPubKey(slice, &pk); + ck_assert_msg(errorcode == SKY_ErrInvalidLengthPubKey, "0 random bytes"); + + randBytes(&slice, 100); + errorcode = SKY_cipher_NewPubKey(slice, &pk); + ck_assert_msg(errorcode == SKY_ErrInvalidLengthPubKey, "100 random bytes"); + + randBytes(&slice, 33); + errorcode = SKY_cipher_NewPubKey(slice, &pk); + ck_assert_msg(errorcode != SKY_OK, "33 random bytes"); + + SKY_cipher_GenerateKeyPair(&pk, &sk); + GoSlice buffer = {pk, sizeof(pk), sizeof(pk)}; + errorcode = SKY_cipher_NewPubKey(buffer, &pk2); + ck_assert_msg(errorcode == SKY_OK); + + ck_assert(isPubKeyEq(&pk, &pk2)); +} +END_TEST + +// define test suite and cases +Suite *common_check_cipher_crypto(void) +{ + Suite *s = suite_create("Load common check_cipher.crypto"); + TCase *tc; + + tc = tcase_create("check_cipher.crypto"); + tcase_add_test(tc, TestNewPubKey); + 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 9771f00ee..34411e7af 100644 --- a/lib/cgo/tests/test_main.c +++ b/lib/cgo/tests/test_main.c @@ -9,6 +9,7 @@ int main(void) SRunner* sr_fork = srunner_create(coin_transaction_fork()); srunner_add_suite(sr, common_check_cipher_hash()); srunner_add_suite(sr, common_check_cipher_address()); + 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_encrypt_scrypt_chacha20poly1305()); diff --git a/lib/cgo/tests/test_main_common.h b/lib/cgo/tests/test_main_common.h index 4a5ac2f84..7bcd03c3f 100644 --- a/lib/cgo/tests/test_main_common.h +++ b/lib/cgo/tests/test_main_common.h @@ -6,5 +6,6 @@ Suite *common_check_cipher_hash(void); Suite *common_check_cipher_address(void); +Suite *common_check_cipher_crypto(void); #endif // TEST_MAIN_COMMON_H diff --git a/lib/cgo/tests/test_main_hw.c b/lib/cgo/tests/test_main_hw.c index ce284e064..84467877a 100644 --- a/lib/cgo/tests/test_main_hw.c +++ b/lib/cgo/tests/test_main_hw.c @@ -10,6 +10,7 @@ int main(void) int number_failed_fork = 0; SRunner *sr = srunner_create(common_check_cipher_hash()); srunner_add_suite(sr, common_check_cipher_address()); + srunner_add_suite(sr, common_check_cipher_crypto()); srunner_run_all(sr, CK_VERBOSE); number_failed = srunner_ntests_failed(sr); srunner_free(sr);