Skip to content

Commit

Permalink
test: Add C++ classes wrapping system interfaces.
Browse files Browse the repository at this point in the history
These are more convenient and safer than the manual vtables we have in
the fuzzer support code. We can override individual member functions,
and C++ will take care of correctly casting and offsetting this-pointers
when needed.
  • Loading branch information
iphydf committed Jan 13, 2024
1 parent 4cea4f9 commit bcb6592
Show file tree
Hide file tree
Showing 16 changed files with 323 additions and 54 deletions.
2 changes: 2 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -481,6 +481,8 @@ add_library(test_util STATIC
toxcore/DHT_test_util.hh
toxcore/crypto_core_test_util.cc
toxcore/crypto_core_test_util.hh
toxcore/mem_test_util.cc
toxcore/mem_test_util.hh
toxcore/network_test_util.cc
toxcore/network_test_util.hh
toxcore/test_util.cc
Expand Down
2 changes: 1 addition & 1 deletion testing/fuzzing/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ function(fuzz_test target source_dir)
set(CORPUS_DIR ${CMAKE_CURRENT_SOURCE_DIR}/toktok-fuzzer/corpus/${target}_fuzz_test)
file(GLOB CORPUS "${CORPUS_DIR}/*")
add_executable(${target}_fuzz_test ${source_dir}/${target}_fuzz_test.cc)
target_link_libraries(${target}_fuzz_test PRIVATE toxcore_fuzz fuzz_support ${LIBFUZZER_LINKER_FLAGS})
target_link_libraries(${target}_fuzz_test PRIVATE fuzz_support test_util toxcore_fuzz ${LIBFUZZER_LINKER_FLAGS})
if(CORPUS)
add_test(NAME ${target}_fuzz COMMAND ${CROSSCOMPILING_EMULATOR} ${target}_fuzz_test -max_total_time=10 ${CORPUS})
set_property(TEST ${target}_fuzz PROPERTY ENVIRONMENT "LLVM_PROFILE_FILE=${target}.profraw;srcdir=${CMAKE_CURRENT_SOURCE_DIR}")
Expand Down
53 changes: 39 additions & 14 deletions toxcore/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
load("@rules_cc//cc:defs.bzl", "cc_test")
load("@rules_cc//cc:defs.bzl", "cc_library", "cc_test")
load("@rules_fuzzing//fuzzing:cc_defs.bzl", "cc_fuzz_test")
load("//tools:no_undefined.bzl", "cc_library")

exports_files(
srcs = [
Expand All @@ -12,6 +11,7 @@ exports_files(

cc_library(
name = "test_util",
testonly = True,
srcs = ["test_util.cc"],
hdrs = ["test_util.hh"],
)
Expand Down Expand Up @@ -52,6 +52,17 @@ cc_library(
],
)

cc_library(
name = "mem_test_util",
testonly = True,
srcs = ["mem_test_util.cc"],
hdrs = ["mem_test_util.hh"],
deps = [
":mem",
":test_util",
],
)

cc_test(
name = "mem_test",
size = "small",
Expand Down Expand Up @@ -162,6 +173,7 @@ cc_library(

cc_library(
name = "crypto_core_test_util",
testonly = True,
srcs = ["crypto_core_test_util.cc"],
hdrs = ["crypto_core_test_util.hh"],
deps = [
Expand Down Expand Up @@ -240,6 +252,7 @@ cc_test(
size = "small",
srcs = ["mono_time_test.cc"],
deps = [
":mem_test_util",
":mono_time",
"@com_google_googletest//:gtest",
"@com_google_googletest//:gtest_main",
Expand Down Expand Up @@ -292,6 +305,7 @@ cc_library(

cc_library(
name = "network_test_util",
testonly = True,
srcs = ["network_test_util.cc"],
hdrs = ["network_test_util.hh"],
deps = [
Expand All @@ -307,6 +321,7 @@ cc_test(
srcs = ["network_test.cc"],
deps = [
":network",
":network_test_util",
"@com_google_googletest//:gtest",
"@com_google_googletest//:gtest_main",
],
Expand Down Expand Up @@ -341,6 +356,7 @@ cc_test(
size = "small",
srcs = ["ping_array_test.cc"],
deps = [
":mem_test_util",
":mono_time",
":ping_array",
"@com_google_googletest//:gtest",
Expand Down Expand Up @@ -399,6 +415,7 @@ cc_library(

cc_library(
name = "DHT_test_util",
testonly = True,
srcs = ["DHT_test_util.cc"],
hdrs = ["DHT_test_util.hh"],
deps = [
Expand All @@ -418,6 +435,7 @@ cc_test(
":DHT",
":DHT_test_util",
":crypto_core",
":mem_test_util",
":network_test_util",
"@com_google_googletest//:gtest",
"@com_google_googletest//:gtest_main",
Expand Down Expand Up @@ -468,7 +486,7 @@ cc_fuzz_test(
name = "forwarding_fuzz_test",
size = "small",
srcs = ["forwarding_fuzz_test.cc"],
#corpus = ["//tools/toktok-fuzzer/corpus:forwarding_fuzz_test"],
corpus = ["//tools/toktok-fuzzer/corpus:forwarding_fuzz_test"],
deps = [
":forwarding",
"//c-toxcore/testing/fuzzing:fuzz_support",
Expand Down Expand Up @@ -641,12 +659,26 @@ cc_test(
srcs = ["group_announce_test.cc"],
deps = [
":group_announce",
":mem_test_util",
":mono_time",
"@com_google_googletest//:gtest",
"@com_google_googletest//:gtest_main",
],
)

cc_fuzz_test(
name = "group_announce_fuzz_test",
size = "small",
testonly = True,
srcs = ["group_announce_fuzz_test.cc"],
corpus = ["//tools/toktok-fuzzer/corpus:group_announce_fuzz_test"],
deps = [
":group_announce",
":mem_test_util",
"//c-toxcore/testing/fuzzing:fuzz_support",
],
)

cc_library(
name = "group_onion_announce",
srcs = ["group_onion_announce.c"],
Expand All @@ -663,17 +695,6 @@ cc_library(
],
)

cc_fuzz_test(
name = "group_announce_fuzz_test",
size = "small",
srcs = ["group_announce_fuzz_test.cc"],
#corpus = ["//tools/toktok-fuzzer/corpus:group_announce_fuzz_test"],
deps = [
":group_announce",
"//c-toxcore/testing/fuzzing:fuzz_support",
],
)

cc_library(
name = "onion_client",
srcs = ["onion_client.c"],
Expand Down Expand Up @@ -759,8 +780,10 @@ cc_test(
srcs = ["group_moderation_test.cc"],
deps = [
":crypto_core",
":crypto_core_test_util",
":group_moderation",
":logger",
":mem_test_util",
":util",
"@com_google_googletest//:gtest",
"@com_google_googletest//:gtest_main",
Expand All @@ -770,10 +793,12 @@ cc_test(
cc_fuzz_test(
name = "group_moderation_fuzz_test",
size = "small",
testonly = True,
srcs = ["group_moderation_fuzz_test.cc"],
corpus = ["//tools/toktok-fuzzer/corpus:group_moderation_fuzz_test"],
deps = [
":group_moderation",
":mem_test_util",
"//c-toxcore/testing/fuzzing:fuzz_support",
],
)
Expand Down
5 changes: 3 additions & 2 deletions toxcore/DHT_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#include "DHT_test_util.hh"
#include "crypto_core.h"
#include "crypto_core_test_util.hh"
#include "mem_test_util.hh"
#include "network_test_util.hh"

namespace {
Expand Down Expand Up @@ -326,8 +327,8 @@ TEST(Request, CreateAndParse)
TEST(AnnounceNodes, SetAndTest)
{
Test_Random rng;
const Network *ns = system_network();
const Memory *mem = system_memory();
Test_Memory mem;
Test_Network ns;

Logger *log = logger_new();
ASSERT_NE(log, nullptr);
Expand Down
3 changes: 2 additions & 1 deletion toxcore/group_announce_fuzz_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#include <vector>

#include "../testing/fuzzing/fuzz_support.h"
#include "mem_test_util.hh"

namespace {

Expand Down Expand Up @@ -48,7 +49,7 @@ void TestUnpackPublicAnnounce(Fuzz_Data &input)

void TestDoGca(Fuzz_Data &input)
{
const Memory *mem = system_memory();
Test_Memory mem;
std::unique_ptr<Logger, void (*)(Logger *)> logger(logger_new(), logger_kill);

uint64_t clock = 1;
Expand Down
3 changes: 2 additions & 1 deletion toxcore/group_announce_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,14 @@

#include <gtest/gtest.h>

#include "mem_test_util.hh"
#include "mono_time.h"

namespace {

struct Announces : ::testing::Test {
protected:
const Memory *mem_ = system_memory();
Test_Memory mem_;
uint64_t clock_ = 1000;
Mono_Time *mono_time_ = nullptr;
GC_Announces_List *gca_ = nullptr;
Expand Down
4 changes: 3 additions & 1 deletion toxcore/group_moderation_fuzz_test.cc
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
#include "group_moderation.h"

#include "../testing/fuzzing/fuzz_support.h"
#include "mem_test_util.hh"

namespace {

void TestModListUnpack(Fuzz_Data &input)
{
CONSUME1_OR_RETURN(const uint16_t, num_mods, input);
Moderation mods{system_memory()};
Test_Memory mem;
Moderation mods{mem};
mod_list_unpack(&mods, input.data(), input.size(), num_mods);
mod_list_cleanup(&mods);
}
Expand Down
Loading

0 comments on commit bcb6592

Please sign in to comment.