Green Sky
a3126d581b
82460b212 feat: add ngc events 24b54722a fix: Ensure we have allocators available for the error paths. 48dbcfebc cleanup: Remove redundant `-DSODIUM_EXPORT` from definitions. 0cef46ee9 cleanup: Fix a few more clang-tidy warnings. 0c5b918e9 cleanup: Fix a few more clang-tidy warnings. 4d3c97f49 cleanup: Enforce stricter identifier naming using clang-tidy. a549807df refactor: Add `mem` module to allow tests to override allocators. 6133fb153 chore: Add devcontainer setup for codespaces. 620e07ecd chore: Set a timeout for tests started using Conan c0ec33b16 chore: Migrate Windows CI from Appveyor to Azure DevOps 8ed47f3ef fix incorrect documentation a1e245841 docs: Fix doxygen config and remove some redundant comments. b0f633185 chore: Fix the Android CI job 7469a529b fix: Add missing `#include <array>`. 2b1a6b0d2 add missing ngc constants getter declarations and definitions 2e02d5637 chore: Add missing module dependencies. REVERT: 67badf694 feat: add ngc events git-subtree-dir: external/toxcore/c-toxcore git-subtree-split: 82460b2124216af1ac9d63060de310a682a2fd15
97 lines
2.5 KiB
C++
97 lines
2.5 KiB
C++
/* SPDX-License-Identifier: GPL-3.0-or-later
|
|
* Copyright © 2022 The TokTok team.
|
|
*/
|
|
|
|
#ifndef C_TOXCORE_TESTING_FUZZING_FUZZ_TOX_H
|
|
#define C_TOXCORE_TESTING_FUZZING_FUZZ_TOX_H
|
|
|
|
#include <cassert>
|
|
#include <memory>
|
|
|
|
#include "../../toxcore/DHT.h"
|
|
#include "../../toxcore/logger.h"
|
|
#include "../../toxcore/network.h"
|
|
#include "fuzz_support.h"
|
|
|
|
constexpr uint16_t SIZE_IP_PORT = SIZE_IP6 + sizeof(uint16_t);
|
|
|
|
template <typename T>
|
|
using Ptr = std::unique_ptr<T, void (*)(T *)>;
|
|
|
|
/** @brief Construct any Tox resource using fuzzer input data.
|
|
*
|
|
* Constructs (or fails by returning) a valid object of type T and passes it to
|
|
* a function specified on the rhs of `>>`. Takes care of cleaning up the
|
|
* resource after the specified function returns.
|
|
*
|
|
* Some `with` instances require additional inputs such as the `Fuzz_Data`
|
|
* reference or a logger.
|
|
*/
|
|
template <typename T>
|
|
struct with;
|
|
|
|
/** @brief Construct a Logger without logging callback.
|
|
*/
|
|
template <>
|
|
struct with<Logger> {
|
|
template <typename F>
|
|
void operator>>(F &&f)
|
|
{
|
|
Ptr<Logger> logger(logger_new(), logger_kill);
|
|
assert(logger != nullptr);
|
|
f(std::move(logger));
|
|
}
|
|
};
|
|
|
|
/** @brief Construct an IP_Port by unpacking fuzzer input with `unpack_ip_port`.
|
|
*/
|
|
template <>
|
|
struct with<IP_Port> {
|
|
Fuzz_Data &input_;
|
|
|
|
template <typename F>
|
|
void operator>>(F &&f)
|
|
{
|
|
CONSUME_OR_RETURN(const uint8_t *ipp_packed, input_, SIZE_IP_PORT);
|
|
IP_Port ipp;
|
|
unpack_ip_port(&ipp, ipp_packed, SIZE_IP6, true);
|
|
|
|
f(ipp);
|
|
}
|
|
};
|
|
|
|
/** @brief Construct a Networking_Core object using the Network vtable passed.
|
|
*
|
|
* Use `with<Logger>{} >> with<Networking_Core>{input, ns, mem} >> ...` to construct
|
|
* a logger and pass it to the Networking_Core constructor function.
|
|
*/
|
|
template <>
|
|
struct with<Networking_Core> {
|
|
Fuzz_Data &input_;
|
|
const Network *ns_;
|
|
const Memory *mem_;
|
|
Ptr<Logger> logger_{nullptr, logger_kill};
|
|
|
|
friend with operator>>(with<Logger> f, with self)
|
|
{
|
|
f >> [&self](Ptr<Logger> logger) { self.logger_ = std::move(logger); };
|
|
return self;
|
|
}
|
|
|
|
template <typename F>
|
|
void operator>>(F &&f)
|
|
{
|
|
with<IP_Port>{input_} >> [&f, this](const IP_Port &ipp) {
|
|
Ptr<Networking_Core> net(
|
|
new_networking_ex(logger_.get(), mem_, ns_, &ipp.ip, ipp.port, ipp.port + 100, nullptr),
|
|
kill_networking);
|
|
if (net == nullptr) {
|
|
return;
|
|
}
|
|
f(std::move(net));
|
|
};
|
|
}
|
|
};
|
|
|
|
#endif // C_TOXCORE_TESTING_FUZZING_FUZZ_TOX_H
|