From 2db6599be5ca0b5253a67f8929ccaf1df2b6a5cd Mon Sep 17 00:00:00 2001 From: iphydf Date: Mon, 6 Dec 2021 21:38:54 +0000 Subject: [PATCH] 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. --- other/fun/sign.c | 14 +++++++++++++- toxcore/BUILD.bazel | 10 ++++++++++ toxcore/list.h | 8 ++++++++ toxcore/list_test.cc | 27 +++++++++++++++++++++++++++ 4 files changed, 58 insertions(+), 1 deletion(-) create mode 100644 toxcore/list_test.cc diff --git a/other/fun/sign.c b/other/fun/sign.c index 547af311..56dcc8e1 100644 --- a/other/fun/sign.c +++ b/other/fun/sign.c @@ -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"); } diff --git a/toxcore/BUILD.bazel b/toxcore/BUILD.bazel index 0bdd9c72..e6ac3901 100644 --- a/toxcore/BUILD.bazel +++ b/toxcore/BUILD.bazel @@ -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"], diff --git a/toxcore/list.h b/toxcore/list.h index afe4240b..a9a8b653 100644 --- a/toxcore/list.h +++ b/toxcore/list.h @@ -13,6 +13,10 @@ #include +#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 diff --git a/toxcore/list_test.cc b/toxcore/list_test.cc new file mode 100644 index 00000000..ab4663fb --- /dev/null +++ b/toxcore/list_test.cc @@ -0,0 +1,27 @@ +#include "list.h" + +#include + +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