toxcore/testing/fuzzing/bootstrap_harness.cc

46 lines
1.3 KiB
C++
Raw Normal View History

#include <cassert>
#include <cstring>
#include "../../toxcore/tox.h"
#include "../../toxcore/tox_struct.h"
#include "fuzz_adapter.h"
extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size);
extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
{
network_adapter_init(data, size);
Tox_Err_New error_new;
Tox *tox = tox_new(nullptr, &error_new);
uint64_t clock = 0;
mono_time_set_current_time_callback(
tox->mono_time,
[](Mono_Time *mono_time, void *user_data) { return *static_cast<uint64_t *>(user_data); },
&clock);
assert(tox != nullptr);
assert(error_new == TOX_ERR_NEW_OK);
uint8_t pub_key[TOX_PUBLIC_KEY_SIZE] = {0};
const bool success = tox_bootstrap(tox, "127.0.0.1", 12345, pub_key, nullptr);
assert(success);
/*
* The iteration count here is a magic value in the literal sense, too small
* and coverage will be bad, too big and fuzzing will not be efficient.
* NOTE: This should be fine tuned after gathering some experience.
*/
for (uint32_t i = 0; i < 50; ++i) {
tox_iterate(tox, nullptr);
// Move the clock forward a decent amount so all the time-based checks
// trigger more quickly.
clock += 200;
}
tox_kill(tox);
return 0; // Non-zero return values are reserved for future use.
}