mirror of
https://github.com/irungentoo/toxcore.git
synced 2024-03-22 13:30:51 +08:00
Add some unit tests for util.h.
This commit is contained in:
parent
dcd439a5c3
commit
3fcc9a3c83
|
@ -470,6 +470,7 @@ endfunction()
|
|||
# The actual unit tests follow.
|
||||
#
|
||||
unit_test(toxcore crypto_core)
|
||||
unit_test(toxcore util)
|
||||
|
||||
################################################################################
|
||||
#
|
||||
|
|
|
@ -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"],
|
||||
|
|
|
@ -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
55
toxcore/util_test.cpp
Normal 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));
|
||||
}
|
Loading…
Reference in New Issue
Block a user