test: Add some simple tests for list.c.

These are pretty useless, but I'm adding the list_test.cc file for future
better tests to be written in.
This commit is contained in:
iphydf 2021-12-06 21:38:54 +00:00
parent 713ce6108c
commit 2db6599be5
No known key found for this signature in database
GPG Key ID: 3855DBA2D74403C9
4 changed files with 58 additions and 1 deletions

View File

@ -72,7 +72,7 @@ int main(int argc, char *argv[])
if (argc == 5 && argv[1][0] == 's') {
unsigned char *secret_key = hex_string_to_bin(argv[2]);
unsigned char *data;
unsigned char *data = nullptr;
int size = load_file(argv[3], &data);
if (size < 0) {
@ -82,25 +82,31 @@ int main(int argc, char *argv[])
unsigned long long smlen;
unsigned char *sm = (unsigned char *)malloc(size + crypto_sign_ed25519_BYTES * 2);
crypto_sign_ed25519(sm, &smlen, data, size, secret_key);
free(data);
free(secret_key);
if (smlen - size != crypto_sign_ed25519_BYTES) {
free(sm);
goto fail;
}
FILE *f = fopen(argv[4], "wb");
if (f == nullptr) {
free(sm);
goto fail;
}
memcpy(sm + smlen, sm, crypto_sign_ed25519_BYTES); // Move signature from beginning to end of file.
if (fwrite(sm + (smlen - size), 1, smlen, f) != smlen) {
fclose(f);
free(sm);
goto fail;
}
fclose(f);
free(sm);
printf("Signed successfully.\n");
}
@ -117,14 +123,20 @@ int main(int argc, char *argv[])
memcpy(signe, data + size - crypto_sign_ed25519_BYTES,
crypto_sign_ed25519_BYTES); // Move signature from end to beginning of file.
memcpy(signe + crypto_sign_ed25519_BYTES, data, size - crypto_sign_ed25519_BYTES);
free(data);
unsigned char *m = (unsigned char *)malloc(size);
unsigned long long mlen;
if (crypto_sign_ed25519_open(m, &mlen, signe, size, public_key) == -1) {
printf("Failed checking sig.\n");
free(m);
free(signe);
goto fail;
}
free(m);
free(signe);
printf("Checked successfully.\n");
}

View File

@ -47,6 +47,16 @@ cc_library(
deps = [":ccompat"],
)
cc_test(
name = "list_test",
size = "small",
srcs = ["list_test.cc"],
deps = [
":list",
"@com_google_googletest//:gtest_main",
],
)
cc_library(
name = "logger",
srcs = ["logger.c"],

View File

@ -13,6 +13,10 @@
#include <stdint.h>
#ifdef __cplusplus
extern "C" {
#endif
typedef struct BS_List {
uint32_t n; // number of elements
uint32_t capacity; // number of elements memory is allocated for
@ -57,4 +61,8 @@ int bs_list_add(BS_List *list, const uint8_t *data, int id);
*/
int bs_list_remove(BS_List *list, const uint8_t *data, int id);
#ifdef __cplusplus
} // extern "C"
#endif
#endif

27
toxcore/list_test.cc Normal file
View File

@ -0,0 +1,27 @@
#include "list.h"
#include <gtest/gtest.h>
namespace {
TEST(List, CreateAndDestroyWithNonZeroSize) {
BS_List list;
bs_list_init(&list, sizeof(int), 10);
bs_list_free(&list);
}
TEST(List, CreateAndDestroyWithZeroSize) {
BS_List list;
bs_list_init(&list, sizeof(int), 0);
bs_list_free(&list);
}
TEST(List, DeleteFromEmptyList) {
BS_List list;
bs_list_init(&list, sizeof(int), 0);
const uint8_t data[sizeof(int)] = {0};
bs_list_remove(&list, data, 0);
bs_list_free(&list);
}
} // namespace