Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: Add more unit tests for add_to_list. #2531

Merged
merged 1 commit into from
Jan 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
14 changes: 13 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -477,14 +477,25 @@ install_module(toxcore DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/tox)
#
################################################################################

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

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()
target_link_libraries(unit_${target}_test PRIVATE toxcore_shared)
endif()
target_link_libraries(unit_${target}_test PRIVATE GTest::GTest GTest::Main)
target_link_libraries(unit_${target}_test PRIVATE GTest::gtest GTest::gtest_main GTest::gmock)
set_target_properties(unit_${target}_test PROPERTIES COMPILE_FLAGS "${TEST_CXX_FLAGS}")
add_test(NAME ${target} COMMAND ${CROSSCOMPILING_EMULATOR} unit_${target}_test)
set_property(TEST ${target} PROPERTY ENVIRONMENT "LLVM_PROFILE_FILE=${target}.profraw")
Expand All @@ -504,6 +515,7 @@ if(GTEST_FOUND)
unit_test(toxcore mem)
unit_test(toxcore mono_time)
unit_test(toxcore ping_array)
unit_test(toxcore test_util)
unit_test(toxcore tox)
unit_test(toxcore util)
endif()
Expand Down
2 changes: 1 addition & 1 deletion other/analysis/gen-file.sh
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ CPPFLAGS+=("-Itoxav")
CPPFLAGS+=("-Itoxencryptsave")
CPPFLAGS+=("-Ithird_party/cmp")

LDFLAGS=("-lopus" "-lsodium" "-lvpx" "-lpthread" "-lconfig" "-lgtest")
LDFLAGS=("-lopus" "-lsodium" "-lvpx" "-lpthread" "-lconfig" "-lgmock" "-lgtest")
LDFLAGS+=("-fuse-ld=gold")
LDFLAGS+=("-Wl,--detect-odr-violations")
LDFLAGS+=("-Wl,--warn-common")
Expand Down
2 changes: 1 addition & 1 deletion other/analysis/run-clang
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ run() {
"${CPPFLAGS[@]}" \
"${LDFLAGS[@]}" \
"$@" \
-std=c++11 \
-std=c++17 \
-Werror \
-Weverything \
-Wno-alloca \
Expand Down
2 changes: 1 addition & 1 deletion other/analysis/run-clang-analyze
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ run() {
clang++ --analyze amalgamation.cc \
"${CPPFLAGS[@]}" \
"$@" \
-std=c++11
-std=c++20
}

. other/analysis/variants.sh
2 changes: 2 additions & 0 deletions other/analysis/run-cppcheck
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ CPPCHECK_CXX+=("--suppress=AssignmentAddressToInteger")
CPPCHECK_CXX+=("--suppress=cstyleCast")
# Used in Messenger.c for a static_assert(...)
CPPCHECK_CXX+=("--suppress=sizeofFunctionCall")
# This is outdated. Range-for is a good choice.
CPPCHECK_CXX+=("--suppress=useStlAlgorithm")

run() {
echo "Running cppcheck in variant '$*'"
Expand Down
2 changes: 1 addition & 1 deletion other/analysis/run-gcc
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ run() {
"${CPPFLAGS[@]}" \
"${LDFLAGS[@]}" \
"$@" \
-std=c++11 \
-std=c++17 \
-fdiagnostics-color=always \
-Wall \
-Wextra \
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
2 changes: 1 addition & 1 deletion other/docker/doxygen/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ ENV LANG=en_US.UTF-8 \
LC_CTYPE=en_US.UTF-8 \
LC_ALL=en_US.UTF-8

RUN apk add --no-cache doxygen git graphviz \
RUN apk add --no-cache doxygen git graphviz texlive \
&& git clone --depth=1 https://github.com/jothepro/doxygen-awesome-css.git /work/doxygen-awesome-css
WORKDIR /work
COPY . /work/
Expand Down
3 changes: 3 additions & 0 deletions sonar-project.properties
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,6 @@ sonar.sources=.

# Encoding of the source code.
sonar.sourceEncoding=UTF-8

# More precise Python version.
sonar.python.version=3.11
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.hh"],
)

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.hh"],
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.hh"],
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.hh"],
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
Loading