Skip to content

Commit

Permalink
refactor
Browse files Browse the repository at this point in the history
move TestPubKeyFromHex from lib/cgo/tests/check_cipher.crypto.c to lib/cgo/tests/check_cipher.crypto.common.c
ref #34
  • Loading branch information
stdevAlDen committed May 16, 2019
1 parent 6dc0acc commit f1f8671
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 40 deletions.
40 changes: 0 additions & 40 deletions lib/cgo/tests/check_cipher.crypto.c
Original file line number Diff line number Diff line change
Expand Up @@ -13,45 +13,6 @@



START_TEST(TestPubKeyFromHex)
{
cipher__PubKey p, p1;
cipher__SecKey sk;
GoString s;
unsigned char buff[51];
char sbuff[101];
GoSlice slice = {(void*)buff, 0, 51};
unsigned int errorcode;

// Invalid hex
s.n = 0;
errorcode = SKY_cipher_PubKeyFromHex(s, &p1);
ck_assert_msg(errorcode == SKY_ErrInvalidLengthPubKey, "TestPubKeyFromHex: Invalid hex. Empty string");

s.p = "cascs";
s.n = strlen(s.p);
errorcode = SKY_cipher_PubKeyFromHex(s, &p1);
ck_assert_msg(errorcode == SKY_ErrInvalidPubKey, "TestPubKeyFromHex: Invalid hex. Bad chars");

// Invalid hex length
SKY_cipher_GenerateKeyPair(&p, &sk);
memcpy(slice.data, (void*)p, sizeof(p));
slice.len = sizeof(p);
bytesnhex(&p[0], sbuff, slice.len >> 1);
s.p = sbuff;
s.n = strlen(s.p);
errorcode = SKY_cipher_PubKeyFromHex(s, &p1);
ck_assert_msg(errorcode == SKY_ErrInvalidLengthPubKey, "TestPubKeyFromHex: Invalid hex length");

// Valid
bytesnhex(p, sbuff, sizeof(p));
s.p = sbuff;
s.n = sizeof(p) << 1;
errorcode = SKY_cipher_PubKeyFromHex(s, &p1);
ck_assert_msg(errorcode == SKY_OK, "TestPubKeyFromHex: Valid. No panic.");
ck_assert(isPubKeyEq(&p, &p1));
}
END_TEST

START_TEST(TestPubKeyHex)
{
Expand Down Expand Up @@ -818,7 +779,6 @@ Suite* cipher_crypto(void)

tc = tcase_create("cipher.crypto");
tcase_add_checked_fixture(tc, setup, teardown);
tcase_add_test(tc, TestPubKeyFromHex);
tcase_add_test(tc, TestPubKeyHex);
tcase_add_test(tc, TestPubKeyVerify);
tcase_add_test(tc, TestPubKeyVerifyNil);
Expand Down
41 changes: 41 additions & 0 deletions lib/cgo/tests/check_cipher.crypto.common.c
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,46 @@ START_TEST(TestNewPubKey)
}
END_TEST

START_TEST(TestPubKeyFromHex)
{
cipher__PubKey p, p1;
cipher__SecKey sk;
GoString s;
unsigned char buff[51];
char sbuff[101];
GoSlice slice = {(void*)buff, 0, 51};
unsigned int errorcode;

// Invalid hex
s.n = 0;
errorcode = SKY_cipher_PubKeyFromHex(s, &p1);
ck_assert_msg(errorcode == SKY_ErrInvalidLengthPubKey, "TestPubKeyFromHex: Invalid hex. Empty string");

s.p = "cascs";
s.n = strlen(s.p);
errorcode = SKY_cipher_PubKeyFromHex(s, &p1);
ck_assert_msg(errorcode == SKY_ErrInvalidPubKey, "TestPubKeyFromHex: Invalid hex. Bad chars");

// Invalid hex length
SKY_cipher_GenerateKeyPair(&p, &sk);
memcpy(slice.data, (void*)p, sizeof(p));
slice.len = sizeof(p);
bytesnhex(&p[0], sbuff, slice.len >> 1);
s.p = sbuff;
s.n = strlen(s.p);
errorcode = SKY_cipher_PubKeyFromHex(s, &p1);
ck_assert_msg(errorcode == SKY_ErrInvalidLengthPubKey, "TestPubKeyFromHex: Invalid hex length");

// Valid
bytesnhex(p, sbuff, sizeof(p));
s.p = sbuff;
s.n = sizeof(p) << 1;
errorcode = SKY_cipher_PubKeyFromHex(s, &p1);
ck_assert_msg(errorcode == SKY_OK, "TestPubKeyFromHex: Valid. No panic.");
ck_assert(isPubKeyEq(&p, &p1));
}
END_TEST

// define test suite and cases
Suite *common_check_cipher_crypto(void)
{
Expand All @@ -60,6 +100,7 @@ Suite *common_check_cipher_crypto(void)

tc = tcase_create("check_cipher.crypto");
tcase_add_test(tc, TestNewPubKey);
tcase_add_test(tc, TestPubKeyFromHex);
suite_add_tcase(s, tc);
tcase_set_timeout(tc, 150);

Expand Down

0 comments on commit f1f8671

Please sign in to comment.