Skip to content

Commit

Permalink
recator
Browse files Browse the repository at this point in the history
move TestNewPubKey from lib/cgo/tests/check_cipher.crypto.c to lib/cgo/tests/check_cipher.crypto.common.c
ref #34
  • Loading branch information
Alvaro Denis committed May 16, 2019
1 parent 9038eeb commit 6dc0acc
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 41 deletions.
41 changes: 0 additions & 41 deletions lib/cgo/tests/check_cipher.crypto.c
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand Down Expand Up @@ -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);
Expand Down
68 changes: 68 additions & 0 deletions lib/cgo/tests/check_cipher.crypto.common.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
#include <stdio.h>
#include <stdlib.h>

#include <check.h>
#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;
}

1 change: 1 addition & 0 deletions lib/cgo/tests/test_main.c
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down
1 change: 1 addition & 0 deletions lib/cgo/tests/test_main_common.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
1 change: 1 addition & 0 deletions lib/cgo/tests/test_main_hw.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit 6dc0acc

Please sign in to comment.