Skip to content

Commit

Permalink
test: Add more unit tests for add_to_list.
Browse files Browse the repository at this point in the history
  • Loading branch information
iphydf committed Jan 10, 2024
1 parent 05ce5c1 commit a910f04
Show file tree
Hide file tree
Showing 20 changed files with 590 additions and 50 deletions.
1 change: 1 addition & 0 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ jobs:
cmake
git
libconfig-dev
libgmock-dev
libgtest-dev
libopus-dev
libsodium-dev
Expand Down
1 change: 1 addition & 0 deletions .github/scripts/coverage-linux
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ set -eu
NPROC=$(nproc)

sudo apt-get install -y --no-install-recommends \
libgmock-dev \
libgtest-dev \
libopus-dev \
libsodium-dev \
Expand Down
11 changes: 11 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -477,8 +477,19 @@ install_module(toxcore DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/tox)
#
################################################################################

add_library(test_util STATIC
toxcore/DHT_test_util.cc
toxcore/DHT_test_util.h
toxcore/crypto_core_test_util.cc
toxcore/crypto_core_test_util.h
toxcore/network_test_util.cc
toxcore/network_test_util.h
toxcore/test_util.cc
toxcore/test_util.h)

function(unit_test subdir target)
add_executable(unit_${target}_test ${subdir}/${target}_test.cc)
target_link_libraries(unit_${target}_test PRIVATE test_util)
if(TARGET toxcore_static)
target_link_libraries(unit_${target}_test PRIVATE toxcore_static)
else()
Expand Down
2 changes: 1 addition & 1 deletion other/bootstrap_daemon/docker/tox-bootstrapd.sha256
Original file line number Diff line number Diff line change
@@ -1 +1 @@
870f1e19aa3f3f802c7e53af3848df6a0f7af9ad4c98213aa1578fa325b30fad /usr/local/bin/tox-bootstrapd
bc830120a87517f830eb85494b769c523bd1696328938d46e9eac1eefea61d38 /usr/local/bin/tox-bootstrapd
1 change: 1 addition & 0 deletions other/docker/circleci/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ RUN apt-get update && \
clang \
cmake \
libconfig-dev \
libgmock-dev \
libgtest-dev \
libopus-dev \
libsodium-dev \
Expand Down
5 changes: 4 additions & 1 deletion other/docker/coverage/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ RUN apt-get update && \
git \
golang-1.18 \
libconfig-dev \
libgmock-dev \
libgtest-dev \
libopus-dev \
libsodium-dev \
Expand All @@ -25,9 +26,11 @@ RUN apt-get update && \
&& apt-get clean \
&& rm -rf /var/lib/apt/lists/* \
&& pip3 install --no-cache-dir gcovr
# strip gtest so it doesn't end up in stack traces. This speeds up the
# strip gtest/gmock so it doesn't end up in stack traces. This speeds up the
# mallocfail run below by a lot.
RUN ["strip", "-g",\
"/usr/lib/x86_64-linux-gnu/libgmock.a",\
"/usr/lib/x86_64-linux-gnu/libgmock_main.a",\
"/usr/lib/x86_64-linux-gnu/libgtest.a",\
"/usr/lib/x86_64-linux-gnu/libgtest_main.a"]
RUN ["curl", "-s", "https://codecov.io/bash", "-o", "/usr/local/bin/codecov"]
Expand Down
53 changes: 53 additions & 0 deletions toxcore/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,23 @@ exports_files(
visibility = ["//c-toxcore:__pkg__"],
)

cc_library(
name = "test_util",
srcs = ["test_util.cc"],
hdrs = ["test_util.h"],
)

cc_test(
name = "test_util_test",
size = "small",
srcs = ["test_util_test.cc"],
deps = [
":crypto_core_test_util",
"@com_google_googletest//:gtest",
"@com_google_googletest//:gtest_main",
],
)

cc_library(
name = "attributes",
hdrs = ["attributes.h"],
Expand Down Expand Up @@ -142,13 +159,24 @@ cc_library(
],
)

cc_library(
name = "crypto_core_test_util",
srcs = ["crypto_core_test_util.cc"],
hdrs = ["crypto_core_test_util.h"],
deps = [
":crypto_core",
":test_util",
],
)

cc_test(
name = "crypto_core_test",
size = "small",
srcs = ["crypto_core_test.cc"],
flaky = True,
deps = [
":crypto_core",
":crypto_core_test_util",
":util",
"@com_google_googletest//:gtest",
"@com_google_googletest//:gtest_main",
Expand Down Expand Up @@ -261,6 +289,16 @@ cc_library(
],
)

cc_library(
name = "network_test_util",
srcs = ["network_test_util.cc"],
hdrs = ["network_test_util.h"],
deps = [
":crypto_core",
":network",
],
)

cc_test(
name = "network_test",
size = "small",
Expand Down Expand Up @@ -357,13 +395,28 @@ cc_library(
],
)

cc_library(
name = "DHT_test_util",
srcs = ["DHT_test_util.cc"],
hdrs = ["DHT_test_util.h"],
deps = [
":DHT",
":crypto_core",
":crypto_core_test_util",
":network_test_util",
":test_util",
],
)

cc_test(
name = "DHT_test",
size = "small",
srcs = ["DHT_test.cc"],
deps = [
":DHT",
":DHT_test_util",
":crypto_core",
":network_test_util",
"@com_google_googletest//:gtest",
"@com_google_googletest//:gtest_main",
],
Expand Down
19 changes: 12 additions & 7 deletions toxcore/DHT.c
Original file line number Diff line number Diff line change
Expand Up @@ -750,16 +750,21 @@ static bool client_or_ip_port_in_list(const Logger *log, const Mono_Time *mono_t
return true;
}

bool add_to_list(Node_format *nodes_list, uint32_t length, const uint8_t *pk, const IP_Port *ip_port,
const uint8_t *cmp_pk)
bool add_to_list(
Node_format *nodes_list, uint32_t length, const uint8_t pk[CRYPTO_PUBLIC_KEY_SIZE],
const IP_Port *ip_port, const uint8_t cmp_pk[CRYPTO_PUBLIC_KEY_SIZE])
{
for (uint32_t i = 0; i < length; ++i) {
if (id_closest(cmp_pk, nodes_list[i].public_key, pk) == 2) {
Node_format *node = &nodes_list[i];

if (id_closest(cmp_pk, node->public_key, pk) == 2) {
uint8_t pk_bak[CRYPTO_PUBLIC_KEY_SIZE];
memcpy(pk_bak, nodes_list[i].public_key, CRYPTO_PUBLIC_KEY_SIZE);
const IP_Port ip_port_bak = nodes_list[i].ip_port;
memcpy(nodes_list[i].public_key, pk, CRYPTO_PUBLIC_KEY_SIZE);
nodes_list[i].ip_port = *ip_port;
memcpy(pk_bak, node->public_key, CRYPTO_PUBLIC_KEY_SIZE);

const IP_Port ip_port_bak = node->ip_port;
memcpy(node->public_key, pk, CRYPTO_PUBLIC_KEY_SIZE);

node->ip_port = *ip_port;

if (i != length - 1) {
add_to_list(nodes_list, length, pk_bak, &ip_port_bak, cmp_pk);
Expand Down
3 changes: 2 additions & 1 deletion toxcore/DHT.h
Original file line number Diff line number Diff line change
Expand Up @@ -370,7 +370,8 @@ unsigned int bit_by_bit_cmp(const uint8_t *pk1, const uint8_t *pk2);
*/
non_null()
bool add_to_list(
Node_format *nodes_list, uint32_t length, const uint8_t *pk, const IP_Port *ip_port, const uint8_t *cmp_pk);
Node_format *nodes_list, uint32_t length, const uint8_t pk[CRYPTO_PUBLIC_KEY_SIZE],
const IP_Port *ip_port, const uint8_t cmp_pk[CRYPTO_PUBLIC_KEY_SIZE]);

/** Return 1 if node can be added to close list, 0 if it can't. */
non_null()
Expand Down
Loading

0 comments on commit a910f04

Please sign in to comment.