Skip to content

Commit 32b68cf

Browse files
committed
cleanup: Some more test cleanups, removing overly smart code.
1 parent 0426624 commit 32b68cf

File tree

11 files changed

+133
-127
lines changed

11 files changed

+133
-127
lines changed

testing/fuzzing/e2e_fuzz_test.cc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
#include <fstream>
99
#include <vector>
1010

11+
#include "../../toxcore/crypto_core.h"
1112
#include "../../toxcore/tox.h"
1213
#include "../../toxcore/tox_dispatch.h"
1314
#include "../../toxcore/tox_events.h"

testing/fuzzing/fuzz_support.cc

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,16 @@ struct Network_Addr {
3030
size_t size;
3131
};
3232

33+
System::System(std::unique_ptr<Tox_System> in_sys, std::unique_ptr<Memory> in_mem,
34+
std::unique_ptr<Network> in_ns, std::unique_ptr<Random> in_rng)
35+
: sys(std::move(in_sys))
36+
, mem(std::move(in_mem))
37+
, ns(std::move(in_ns))
38+
, rng(std::move(in_rng))
39+
{
40+
}
41+
System::System(System &&) = default;
42+
3343
System::~System() { }
3444

3545
static int recv_common(Fuzz_Data &input, uint8_t *buf, size_t buf_len)

testing/fuzzing/fuzz_support.h

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@
99
#include <cstdlib>
1010
#include <deque>
1111
#include <memory>
12-
#include <vector>
1312
#include <unordered_map>
1413
#include <utility>
14+
#include <vector>
1515

1616
#include "../../toxcore/tox.h"
1717

@@ -20,8 +20,10 @@ struct Fuzz_Data {
2020
std::size_t size;
2121

2222
Fuzz_Data(const uint8_t *input_data, std::size_t input_size)
23-
: data(input_data), size(input_size)
24-
{}
23+
: data(input_data)
24+
, size(input_size)
25+
{
26+
}
2527

2628
Fuzz_Data &operator=(const Fuzz_Data &rhs) = delete;
2729
Fuzz_Data(const Fuzz_Data &rhs) = delete;
@@ -93,14 +95,20 @@ struct Fuzz_Data {
9395
} \
9496
DECL = INPUT.consume(SIZE)
9597

98+
#define CONSUME_OR_RETURN_VAL(DECL, INPUT, SIZE, VAL) \
99+
if (INPUT.size < SIZE) { \
100+
return VAL; \
101+
} \
102+
DECL = INPUT.consume(SIZE)
103+
96104
inline void fuzz_select_target(uint8_t selector, Fuzz_Data &input)
97105
{
98106
// The selector selected no function, so we do nothing and rely on the
99107
// fuzzer to come up with a better selector.
100108
}
101109

102110
template <typename Arg, typename... Args>
103-
void fuzz_select_target(uint8_t selector, Fuzz_Data &input, Arg &&fn, Args &&... args)
111+
void fuzz_select_target(uint8_t selector, Fuzz_Data &input, Arg &&fn, Args &&...args)
104112
{
105113
if (selector == sizeof...(Args)) {
106114
return fn(input);
@@ -109,7 +117,7 @@ void fuzz_select_target(uint8_t selector, Fuzz_Data &input, Arg &&fn, Args &&...
109117
}
110118

111119
template <typename... Args>
112-
void fuzz_select_target(const uint8_t *data, std::size_t size, Args &&... args)
120+
void fuzz_select_target(const uint8_t *data, std::size_t size, Args &&...args)
113121
{
114122
Fuzz_Data input{data, size};
115123

@@ -127,6 +135,10 @@ struct System {
127135
std::unique_ptr<Network> ns;
128136
std::unique_ptr<Random> rng;
129137

138+
System(std::unique_ptr<Tox_System> sys, std::unique_ptr<Memory> mem,
139+
std::unique_ptr<Network> ns, std::unique_ptr<Random> rng);
140+
System(System &&);
141+
130142
// Not inline because sizeof of the above 2 structs is not known everywhere.
131143
~System();
132144

testing/fuzzing/fuzz_tox.h

Lines changed: 1 addition & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -1,96 +1,17 @@
11
/* SPDX-License-Identifier: GPL-3.0-or-later
2-
* Copyright © 2022 The TokTok team.
2+
* Copyright © 2022-2024 The TokTok team.
33
*/
44

55
#ifndef C_TOXCORE_TESTING_FUZZING_FUZZ_TOX_H
66
#define C_TOXCORE_TESTING_FUZZING_FUZZ_TOX_H
77

8-
#include <cassert>
98
#include <memory>
109

11-
#include "../../toxcore/DHT.h"
12-
#include "../../toxcore/logger.h"
1310
#include "../../toxcore/network.h"
14-
#include "fuzz_support.h"
1511

1612
constexpr uint16_t SIZE_IP_PORT = SIZE_IP6 + sizeof(uint16_t);
1713

1814
template <typename T>
1915
using Ptr = std::unique_ptr<T, void (*)(T *)>;
2016

21-
/** @brief Construct any Tox resource using fuzzer input data.
22-
*
23-
* Constructs (or fails by returning) a valid object of type T and passes it to
24-
* a function specified on the rhs of `>>`. Takes care of cleaning up the
25-
* resource after the specified function returns.
26-
*
27-
* Some `with` instances require additional inputs such as the `Fuzz_Data`
28-
* reference or a logger.
29-
*/
30-
template <typename T>
31-
struct with;
32-
33-
/** @brief Construct a Logger without logging callback.
34-
*/
35-
template <>
36-
struct with<Logger> {
37-
template <typename F>
38-
void operator>>(F &&f)
39-
{
40-
Ptr<Logger> logger(logger_new(), logger_kill);
41-
assert(logger != nullptr);
42-
f(std::move(logger));
43-
}
44-
};
45-
46-
/** @brief Construct an IP_Port by unpacking fuzzer input with `unpack_ip_port`.
47-
*/
48-
template <>
49-
struct with<IP_Port> {
50-
Fuzz_Data &input_;
51-
52-
template <typename F>
53-
void operator>>(F &&f)
54-
{
55-
CONSUME_OR_RETURN(const uint8_t *ipp_packed, input_, SIZE_IP_PORT);
56-
IP_Port ipp;
57-
unpack_ip_port(&ipp, ipp_packed, SIZE_IP6, true);
58-
59-
f(ipp);
60-
}
61-
};
62-
63-
/** @brief Construct a Networking_Core object using the Network vtable passed.
64-
*
65-
* Use `with<Logger>{} >> with<Networking_Core>{input, ns, mem} >> ...` to construct
66-
* a logger and pass it to the Networking_Core constructor function.
67-
*/
68-
template <>
69-
struct with<Networking_Core> {
70-
Fuzz_Data &input_;
71-
const Network *ns_;
72-
const Memory *mem_;
73-
Ptr<Logger> logger_{nullptr, logger_kill};
74-
75-
friend with operator>>(with<Logger> f, with self)
76-
{
77-
f >> [&self](Ptr<Logger> logger) { self.logger_ = std::move(logger); };
78-
return self;
79-
}
80-
81-
template <typename F>
82-
void operator>>(F &&f)
83-
{
84-
with<IP_Port>{input_} >> [&f, this](const IP_Port &ipp) {
85-
Ptr<Networking_Core> net(
86-
new_networking_ex(logger_.get(), mem_, ns_, &ipp.ip, ipp.port, ipp.port + 100, nullptr),
87-
kill_networking);
88-
if (net == nullptr) {
89-
return;
90-
}
91-
f(std::move(net));
92-
};
93-
}
94-
};
95-
9617
#endif // C_TOXCORE_TESTING_FUZZING_FUZZ_TOX_H

testing/fuzzing/protodump_reduce.cc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#include <cassert>
22
#include <cstdio>
33

4+
#include "../../toxcore/crypto_core.h"
45
#include "../../toxcore/tox.h"
56
#include "../../toxcore/tox_dispatch.h"
67
#include "../../toxcore/tox_events.h"

toxcore/BUILD.bazel

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -296,6 +296,7 @@ cc_library(
296296
deps = [
297297
":crypto_core",
298298
":network",
299+
":test_util",
299300
],
300301
)
301302

toxcore/DHT_test.cc

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -333,13 +333,13 @@ TEST(AnnounceNodes, SetAndTest)
333333
ASSERT_NE(log, nullptr);
334334
Mono_Time *mono_time = mono_time_new(mem, nullptr, nullptr);
335335
ASSERT_NE(mono_time, nullptr);
336-
Networking_Core *net = new_networking_no_udp(log, mem, ns);
336+
Ptr<Networking_Core> net(new_networking_no_udp(log, mem, ns));
337337
ASSERT_NE(net, nullptr);
338-
DHT *dht = new_dht(log, mem, rng, ns, mono_time, net, true, true);
338+
Ptr<DHT> dht(new_dht(log, mem, rng, ns, mono_time, net.get(), true, true));
339339
ASSERT_NE(dht, nullptr);
340340

341341
uint8_t pk_data[CRYPTO_PUBLIC_KEY_SIZE];
342-
memcpy(pk_data, dht_get_self_public_key(dht), sizeof(pk_data));
342+
memcpy(pk_data, dht_get_self_public_key(dht.get()), sizeof(pk_data));
343343
PublicKey self_pk(to_array(pk_data));
344344

345345
PublicKey pk1 = random_pk(rng);
@@ -353,20 +353,20 @@ TEST(AnnounceNodes, SetAndTest)
353353
IP_Port ip_port = {0};
354354
ip_port.ip.family = net_family_ipv4();
355355

356-
set_announce_node(dht, pk1.data());
357-
set_announce_node(dht, pk2.data());
356+
set_announce_node(dht.get(), pk1.data());
357+
set_announce_node(dht.get(), pk2.data());
358358

359-
EXPECT_TRUE(addto_lists(dht, &ip_port, pk1.data()));
360-
EXPECT_TRUE(addto_lists(dht, &ip_port, pk2.data()));
359+
EXPECT_TRUE(addto_lists(dht.get(), &ip_port, pk1.data()));
360+
EXPECT_TRUE(addto_lists(dht.get(), &ip_port, pk2.data()));
361361

362362
Node_format nodes[MAX_SENT_NODES];
363-
EXPECT_EQ(0, get_close_nodes(dht, self_pk.data(), nodes, net_family_unspec(), true, true));
364-
set_announce_node(dht, pk1.data());
365-
set_announce_node(dht, pk2.data());
366-
EXPECT_EQ(2, get_close_nodes(dht, self_pk.data(), nodes, net_family_unspec(), true, true));
363+
EXPECT_EQ(
364+
0, get_close_nodes(dht.get(), self_pk.data(), nodes, net_family_unspec(), true, true));
365+
set_announce_node(dht.get(), pk1.data());
366+
set_announce_node(dht.get(), pk2.data());
367+
EXPECT_EQ(
368+
2, get_close_nodes(dht.get(), self_pk.data(), nodes, net_family_unspec(), true, true));
367369

368-
kill_dht(dht);
369-
kill_networking(net);
370370
mono_time_free(mem, mono_time);
371371
logger_kill(log);
372372
}

toxcore/DHT_test_util.hh

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@
44
#include <iosfwd>
55

66
#include "DHT.h"
7+
#include "test_util.hh"
8+
9+
template <>
10+
struct Deleter<DHT> : Function_Deleter<DHT, kill_dht> { };
711

812
bool operator==(Node_format const &a, Node_format const &b);
913

toxcore/forwarding_fuzz_test.cc

Lines changed: 68 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,47 +1,87 @@
11
#include "forwarding.h"
22

33
#include <cassert>
4+
#include <cstring>
45
#include <memory>
6+
#include <optional>
57

68
#include "../testing/fuzzing/fuzz_support.h"
79
#include "../testing/fuzzing/fuzz_tox.h"
810

911
namespace {
1012

13+
std::optional<std::tuple<IP_Port, IP_Port, const uint8_t *, size_t>> prepare(Fuzz_Data &input)
14+
{
15+
CONSUME_OR_RETURN_VAL(const uint8_t *ipp_packed, input, SIZE_IP_PORT, std::nullopt);
16+
IP_Port ipp;
17+
unpack_ip_port(&ipp, ipp_packed, SIZE_IP6, true);
18+
19+
CONSUME_OR_RETURN_VAL(const uint8_t *forwarder_packed, input, SIZE_IP_PORT, std::nullopt);
20+
IP_Port forwarder;
21+
unpack_ip_port(&forwarder, forwarder_packed, SIZE_IP6, true);
22+
23+
// 2 bytes: size of the request
24+
CONSUME_OR_RETURN_VAL(const uint8_t *data_size_bytes, input, sizeof(uint16_t), std::nullopt);
25+
uint16_t data_size;
26+
std::memcpy(&data_size, data_size_bytes, sizeof(uint16_t));
27+
28+
// data bytes (max 64K)
29+
CONSUME_OR_RETURN_VAL(const uint8_t *data, input, data_size, std::nullopt);
30+
31+
return {{ipp, forwarder, data, data_size}};
32+
}
33+
1134
void TestSendForwardRequest(Fuzz_Data &input)
1235
{
13-
const Network *ns = system_network(); // TODO(iphydf): fuzz_network
14-
assert(ns != nullptr);
15-
const Memory *mem = system_memory(); // TODO(iphydf): fuzz_memory
16-
assert(mem != nullptr);
17-
18-
with<Logger>{} >> with<Networking_Core>{input, ns, mem} >> [&input](Ptr<Networking_Core> net) {
19-
with<IP_Port>{input} >> [net = std::move(net), &input](const IP_Port &forwarder) {
20-
CONSUME1_OR_RETURN(const uint16_t chain_length, input);
21-
const uint16_t chain_keys_size = chain_length * CRYPTO_PUBLIC_KEY_SIZE;
22-
CONSUME_OR_RETURN(const uint8_t *chain_keys, input, chain_keys_size);
23-
24-
send_forward_request(
25-
net.get(), &forwarder, chain_keys, chain_length, input.data, input.size);
26-
};
27-
};
36+
CONSUME1_OR_RETURN(const uint16_t chain_length, input);
37+
const uint16_t chain_keys_size = chain_length * CRYPTO_PUBLIC_KEY_SIZE;
38+
CONSUME_OR_RETURN(const uint8_t *chain_keys, input, chain_keys_size);
39+
40+
auto prep = prepare(input);
41+
if (!prep.has_value()) {
42+
return;
43+
}
44+
auto [ipp, forwarder, data, data_size] = prep.value();
45+
46+
// rest of the fuzz data is input for malloc and network
47+
Fuzz_System sys(input);
48+
49+
Ptr<Logger> logger(logger_new(), logger_kill);
50+
51+
Ptr<Networking_Core> net(new_networking_ex(logger.get(), sys.mem.get(), sys.ns.get(), &ipp.ip,
52+
ipp.port, ipp.port + 100, nullptr),
53+
kill_networking);
54+
if (net == nullptr) {
55+
return;
56+
}
57+
58+
send_forward_request(net.get(), &forwarder, chain_keys, chain_length, data, data_size);
2859
}
2960

3061
void TestForwardReply(Fuzz_Data &input)
3162
{
32-
const Network *ns = system_network(); // TODO(iphydf): fuzz_network
33-
assert(ns != nullptr);
34-
const Memory *mem = system_memory(); // TODO(iphydf): fuzz_memory
35-
assert(mem != nullptr);
36-
37-
with<Logger>{} >> with<Networking_Core>{input, ns, mem} >> [&input](Ptr<Networking_Core> net) {
38-
with<IP_Port>{input} >> [net = std::move(net), &input](const IP_Port &forwarder) {
39-
CONSUME1_OR_RETURN(const uint16_t sendback_length, input);
40-
CONSUME_OR_RETURN(const uint8_t *sendback, input, sendback_length);
41-
42-
forward_reply(net.get(), &forwarder, sendback, sendback_length, input.data, input.size);
43-
};
44-
};
63+
CONSUME1_OR_RETURN(const uint16_t sendback_length, input);
64+
CONSUME_OR_RETURN(const uint8_t *sendback, input, sendback_length);
65+
66+
auto prep = prepare(input);
67+
if (!prep.has_value()) {
68+
return;
69+
}
70+
auto [ipp, forwarder, data, data_size] = prep.value();
71+
72+
// rest of the fuzz data is input for malloc and network
73+
Fuzz_System sys(input);
74+
75+
Ptr<Logger> logger(logger_new(), logger_kill);
76+
77+
Ptr<Networking_Core> net(new_networking_ex(logger.get(), sys.mem.get(), sys.ns.get(), &ipp.ip,
78+
ipp.port, ipp.port + 100, nullptr),
79+
kill_networking);
80+
if (net == nullptr) {
81+
return;
82+
}
83+
84+
forward_reply(net.get(), &forwarder, sendback, sendback_length, data, data_size);
4585
}
4686

4787
} // namespace

toxcore/network_test_util.hh

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,14 @@
11
#ifndef C_TOXCORE_TOXCORE_NETWORK_TEST_UTIL_H
22
#define C_TOXCORE_TOXCORE_NETWORK_TEST_UTIL_H
33

4-
#include <ostream>
4+
#include <iosfwd>
55

66
#include "crypto_core.h"
77
#include "network.h"
8+
#include "test_util.hh"
9+
10+
template <>
11+
struct Deleter<Networking_Core> : Function_Deleter<Networking_Core, kill_networking> { };
812

913
IP_Port random_ip_port(const Random *rng);
1014

0 commit comments

Comments
 (0)