Skip to content

Commit

Permalink
fix bugs in tests suits ref #34
Browse files Browse the repository at this point in the history
  • Loading branch information
Alvaro Denis committed May 21, 2019
1 parent 985bfa8 commit 3d20e38
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 20 deletions.
13 changes: 9 additions & 4 deletions lib/cgo/tests/check_cipher.address.common.c
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,8 @@ START_TEST(TestAddressString)
cipher__PubKey pk;
cipher__SecKey sk;
cipher__Address addr, addr2, addr3;
GoString str = {buff, 0};
char buf[1024] = {0};
GoString str = {.p=buff, .n=sizeof(buff)};

GoUint32 err = SKY_cipher_GenerateKeyPair(&pk, &sk);
ck_assert(err == SKY_OK);
Expand Down Expand Up @@ -131,19 +132,23 @@ START_TEST(TestAddressFromBytes)
cipher__SecKey sk;
cipher__PubKey pk;
GoSlice bytes;
GoSlice_ tempBytes;
coin__UxArray tempBytes;

GoUint32 err = SKY_cipher_GenerateKeyPair(&pk, &sk);
ck_assert(err == SKY_OK);
SKY_cipher_AddressFromPubKey(&pk, &addr);

tempBytes.data = buff;
tempBytes.len = 0;
tempBytes.len = sizeof(buff);
tempBytes.cap = sizeof(buff);

SKY_cipher_Address_Bytes(&addr, &tempBytes);
ck_assert_msg(tempBytes.len > 0, "address bytes written");
copyGoSlice_toGoSlice(&bytes, &tempBytes, tempBytes.len);
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);
err = SKY_cipher_AddressFromBytes(bytes, &addr2);
ck_assert_msg(err == SKY_OK, "convert bytes to SKY address");

Expand Down
5 changes: 3 additions & 2 deletions lib/cgo/tests/check_cipher.crypto.common.c
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ START_TEST(TestNewPubKey)
cipher__SecKey sk;

slice.data = buff;
slice.cap = 101;
slice.len = 0;
slice.cap = sizeof(buff);

randBytes(&slice, 31);
slice.len = 31;
Expand Down Expand Up @@ -291,7 +292,7 @@ START_TEST(TestSigHex)

ck_assert(errorcode == SKY_OK);
char buffer[100];
GoString_ tmp_str = {buffer, 0};
GoString_ tmp_str = {.p = buffer, .n = sizeof(buffer)};
SKY_cipher_Sig_Hex(&s, &tmp_str);
str.p = tmp_str.p;
str.n = tmp_str.n;
Expand Down
26 changes: 19 additions & 7 deletions lib/cgo/tests/check_cipher.hash.common.c
Original file line number Diff line number Diff line change
Expand Up @@ -103,12 +103,16 @@ START_TEST(TestSHA256KnownValue)

SKY_cipher_SumSHA256(slice_input, &sha);

GoString_ tmp_output;
char raw_buf[sizeof(cipher__SHA256) * 2 + 1] = {0};
GoString_ tmp_output = {
.p = raw_buf,
.n = sizeof(raw_buf)
};

SKY_cipher_SHA256_Hex(&sha, &tmp_output);
registerMemCleanup((void*)tmp_output.p);

ck_assert(strcmp(tmp_output.p, vals[i].output) == SKY_OK);
ck_assert_str_eq(tmp_output.p, vals[i].output);
}
}
END_TEST
Expand Down Expand Up @@ -143,18 +147,26 @@ START_TEST(TestSHA256Hex)
memset(&h, 0, sizeof(h));
randBytes(&slice, 32);
SKY_cipher_SHA256_Set(&h, slice);
GoString_ s;

char raw_buf[sizeof(cipher__SHA256) * 2 + 1] = {0};
GoString_ s = {
.p = raw_buf,
.n = sizeof(raw_buf)
};

SKY_cipher_SHA256_Hex(&h, &s);
registerMemCleanup((void*)s.p);

cipher__SHA256 h2;
GoString tmpS = {s.p, s.n};
GoString tmpS = {.p = s.p, .n = s.n};
error = SKY_cipher_SHA256FromHex(tmpS, &h2);
ck_assert(error == SKY_OK);
ck_assert(isU8Eq(h, h2, 32));

GoString_ s2;
char raw_buf2[sizeof(cipher__SHA256) * 2 + 1] = {0};
GoString_ s2 = {
.p = raw_buf2,
.n = sizeof(raw_buf2)
};
SKY_cipher_SHA256_Hex(&h2, &s2);
registerMemCleanup((void*)s2.p);
ck_assert_str_eq(s.p, s2.p);
Expand Down Expand Up @@ -248,12 +260,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->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 );
pdest->cap = psource->cap;
pdest->data = calloc(psource->cap, elem_size);
registerMemCleanup(pdest->data);
memcpy(pdest->data, psource->data, psource->len * elem_size);
return SKY_OK;
}

0 comments on commit 3d20e38

Please sign in to comment.