Add some unit tests for util.h.

This commit is contained in:
iphydf 2018-02-03 15:44:43 +00:00
parent dcd439a5c3
commit 3fcc9a3c83
No known key found for this signature in database
GPG Key ID: 3855DBA2D74403C9
4 changed files with 73 additions and 0 deletions

View File

@ -470,6 +470,7 @@ endfunction()
# The actual unit tests follow.
#
unit_test(toxcore crypto_core)
unit_test(toxcore util)
################################################################################
#

View File

@ -72,6 +72,15 @@ cc_library(
],
)
cc_test(
name = "util_test",
srcs = ["util_test.cpp"],
deps = [
":network",
"@gtest",
],
)
cc_library(
name = "ping_array",
srcs = ["ping_array.c"],

View File

@ -32,6 +32,10 @@
#include "logger.h"
#ifdef __cplusplus
extern "C" {
#endif
#define MIN(a,b) (((a)<(b))?(a):(b))
#define PAIR(TYPE1__, TYPE2__) struct { TYPE1__ first; TYPE2__ second; }
@ -61,4 +65,8 @@ int load_state(load_state_callback_func load_state_callback, Logger *log, void *
/* Returns -1 if failed or 0 if success */
int create_recursive_mutex(pthread_mutex_t *mutex);
#ifdef __cplusplus
} // extern "C"
#endif
#endif /* UTIL_H */

55
toxcore/util_test.cpp Normal file
View File

@ -0,0 +1,55 @@
#include "util.h"
#include "crypto_core.h"
#include <gtest/gtest.h>
TEST(Util, UnixTimeIncreasesOverTime)
{
unix_time_update();
uint64_t const start = unix_time();
while (start == unix_time()) {
unix_time_update();
}
uint64_t const end = unix_time();
EXPECT_GT(end, start);
}
TEST(Util, IsTimeout)
{
uint64_t const start = unix_time();
EXPECT_FALSE(is_timeout(start, 1));
while (start == unix_time()) {
unix_time_update();
}
EXPECT_TRUE(is_timeout(start, 1));
}
TEST(Util, TwoRandomIdsAreNotEqual)
{
uint8_t pk1[CRYPTO_PUBLIC_KEY_SIZE];
uint8_t sk1[CRYPTO_SECRET_KEY_SIZE];
uint8_t pk2[CRYPTO_PUBLIC_KEY_SIZE];
uint8_t sk2[CRYPTO_SECRET_KEY_SIZE];
crypto_new_keypair(pk1, sk1);
crypto_new_keypair(pk2, sk2);
EXPECT_FALSE(id_equal(pk1, pk2));
}
TEST(Util, IdCopyMakesKeysEqual)
{
uint8_t pk1[CRYPTO_PUBLIC_KEY_SIZE];
uint8_t sk1[CRYPTO_SECRET_KEY_SIZE];
uint8_t pk2[CRYPTO_PUBLIC_KEY_SIZE] = {0};
crypto_new_keypair(pk1, sk1);
id_copy(pk2, pk1);
EXPECT_TRUE(id_equal(pk1, pk2));
}