diff options
author | iphydf <iphydf@users.noreply.github.com> | 2018-02-03 15:44:43 +0000 |
---|---|---|
committer | iphydf <iphydf@users.noreply.github.com> | 2018-02-08 12:54:39 +0000 |
commit | 3fcc9a3c83c807792846ae2c9f98aeb447e62805 (patch) | |
tree | 757f7e6a50b32534dcf0fa38007dbf0265eeb7ef | |
parent | dcd439a5c3d80cd89c493a900c78e2a398f65248 (diff) |
Add some unit tests for util.h.
-rw-r--r-- | CMakeLists.txt | 1 | ||||
-rw-r--r-- | toxcore/BUILD.bazel | 9 | ||||
-rw-r--r-- | toxcore/util.h | 8 | ||||
-rw-r--r-- | toxcore/util_test.cpp | 55 |
4 files changed, 73 insertions, 0 deletions
diff --git a/CMakeLists.txt b/CMakeLists.txt index 742facd1..a52f11bb 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt | |||
@@ -470,6 +470,7 @@ endfunction() | |||
470 | # The actual unit tests follow. | 470 | # The actual unit tests follow. |
471 | # | 471 | # |
472 | unit_test(toxcore crypto_core) | 472 | unit_test(toxcore crypto_core) |
473 | unit_test(toxcore util) | ||
473 | 474 | ||
474 | ################################################################################ | 475 | ################################################################################ |
475 | # | 476 | # |
diff --git a/toxcore/BUILD.bazel b/toxcore/BUILD.bazel index 3b96dd6d..934ff16c 100644 --- a/toxcore/BUILD.bazel +++ b/toxcore/BUILD.bazel | |||
@@ -72,6 +72,15 @@ cc_library( | |||
72 | ], | 72 | ], |
73 | ) | 73 | ) |
74 | 74 | ||
75 | cc_test( | ||
76 | name = "util_test", | ||
77 | srcs = ["util_test.cpp"], | ||
78 | deps = [ | ||
79 | ":network", | ||
80 | "@gtest", | ||
81 | ], | ||
82 | ) | ||
83 | |||
75 | cc_library( | 84 | cc_library( |
76 | name = "ping_array", | 85 | name = "ping_array", |
77 | srcs = ["ping_array.c"], | 86 | srcs = ["ping_array.c"], |
diff --git a/toxcore/util.h b/toxcore/util.h index 8777e191..8c86158b 100644 --- a/toxcore/util.h +++ b/toxcore/util.h | |||
@@ -32,6 +32,10 @@ | |||
32 | 32 | ||
33 | #include "logger.h" | 33 | #include "logger.h" |
34 | 34 | ||
35 | #ifdef __cplusplus | ||
36 | extern "C" { | ||
37 | #endif | ||
38 | |||
35 | #define MIN(a,b) (((a)<(b))?(a):(b)) | 39 | #define MIN(a,b) (((a)<(b))?(a):(b)) |
36 | #define PAIR(TYPE1__, TYPE2__) struct { TYPE1__ first; TYPE2__ second; } | 40 | #define PAIR(TYPE1__, TYPE2__) struct { TYPE1__ first; TYPE2__ second; } |
37 | 41 | ||
@@ -61,4 +65,8 @@ int load_state(load_state_callback_func load_state_callback, Logger *log, void * | |||
61 | /* Returns -1 if failed or 0 if success */ | 65 | /* Returns -1 if failed or 0 if success */ |
62 | int create_recursive_mutex(pthread_mutex_t *mutex); | 66 | int create_recursive_mutex(pthread_mutex_t *mutex); |
63 | 67 | ||
68 | #ifdef __cplusplus | ||
69 | } // extern "C" | ||
70 | #endif | ||
71 | |||
64 | #endif /* UTIL_H */ | 72 | #endif /* UTIL_H */ |
diff --git a/toxcore/util_test.cpp b/toxcore/util_test.cpp new file mode 100644 index 00000000..8de63848 --- /dev/null +++ b/toxcore/util_test.cpp | |||
@@ -0,0 +1,55 @@ | |||
1 | #include "util.h" | ||
2 | |||
3 | #include "crypto_core.h" | ||
4 | |||
5 | #include <gtest/gtest.h> | ||
6 | |||
7 | TEST(Util, UnixTimeIncreasesOverTime) | ||
8 | { | ||
9 | unix_time_update(); | ||
10 | uint64_t const start = unix_time(); | ||
11 | |||
12 | while (start == unix_time()) { | ||
13 | unix_time_update(); | ||
14 | } | ||
15 | |||
16 | uint64_t const end = unix_time(); | ||
17 | EXPECT_GT(end, start); | ||
18 | } | ||
19 | |||
20 | TEST(Util, IsTimeout) | ||
21 | { | ||
22 | uint64_t const start = unix_time(); | ||
23 | EXPECT_FALSE(is_timeout(start, 1)); | ||
24 | |||
25 | while (start == unix_time()) { | ||
26 | unix_time_update(); | ||
27 | } | ||
28 | |||
29 | EXPECT_TRUE(is_timeout(start, 1)); | ||
30 | } | ||
31 | |||
32 | TEST(Util, TwoRandomIdsAreNotEqual) | ||
33 | { | ||
34 | uint8_t pk1[CRYPTO_PUBLIC_KEY_SIZE]; | ||
35 | uint8_t sk1[CRYPTO_SECRET_KEY_SIZE]; | ||
36 | uint8_t pk2[CRYPTO_PUBLIC_KEY_SIZE]; | ||
37 | uint8_t sk2[CRYPTO_SECRET_KEY_SIZE]; | ||
38 | |||
39 | crypto_new_keypair(pk1, sk1); | ||
40 | crypto_new_keypair(pk2, sk2); | ||
41 | |||
42 | EXPECT_FALSE(id_equal(pk1, pk2)); | ||
43 | } | ||
44 | |||
45 | TEST(Util, IdCopyMakesKeysEqual) | ||
46 | { | ||
47 | uint8_t pk1[CRYPTO_PUBLIC_KEY_SIZE]; | ||
48 | uint8_t sk1[CRYPTO_SECRET_KEY_SIZE]; | ||
49 | uint8_t pk2[CRYPTO_PUBLIC_KEY_SIZE] = {0}; | ||
50 | |||
51 | crypto_new_keypair(pk1, sk1); | ||
52 | id_copy(pk2, pk1); | ||
53 | |||
54 | EXPECT_TRUE(id_equal(pk1, pk2)); | ||
55 | } | ||