Skip to content

Commit

Permalink
Revert "fix bugs in tests suits ref #34"
Browse files Browse the repository at this point in the history
This reverts commit 3d20e38.

ref #34
  • Loading branch information
Alvaro Denis committed May 23, 2019
1 parent f93a34c commit 0cb4f33
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 38 deletions.
13 changes: 4 additions & 9 deletions lib/cgo/tests/check_cipher.address.common.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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");

Expand Down
5 changes: 2 additions & 3 deletions lib/cgo/tests/check_cipher.crypto.common.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down
26 changes: 7 additions & 19 deletions lib/cgo/tests/check_cipher.hash.common.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down
14 changes: 7 additions & 7 deletions lib/cgo/tests/testutils/common.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

0 comments on commit 0cb4f33

Please sign in to comment.