Skip to content

Commit 3cb9d9c

Browse files
committed
net: make CaptureMessage() mockable
Rename `CaptureMessage()` to `CaptureMessageToFile()` and introduce a `std::function` variable called `CaptureMessage` whose value can be changed by unit tests, should they need to inspect message contents.
1 parent 43868ba commit 3cb9d9c

File tree

3 files changed

+23
-3
lines changed

3 files changed

+23
-3
lines changed

src/net.cpp

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3085,7 +3085,10 @@ uint64_t CConnman::CalculateKeyedNetGroup(const CAddress& ad) const
30853085
return GetDeterministicRandomizer(RANDOMIZER_ID_NETGROUP).Write(vchNetGroup.data(), vchNetGroup.size()).Finalize();
30863086
}
30873087

3088-
void CaptureMessage(const CAddress& addr, const std::string& msg_type, const Span<const unsigned char>& data, bool is_incoming)
3088+
void CaptureMessageToFile(const CAddress& addr,
3089+
const std::string& msg_type,
3090+
const Span<const unsigned char>& data,
3091+
bool is_incoming)
30893092
{
30903093
// Note: This function captures the message at the time of processing,
30913094
// not at socket receive/send time.
@@ -3112,3 +3115,9 @@ void CaptureMessage(const CAddress& addr, const std::string& msg_type, const Spa
31123115
ser_writedata32(f, size);
31133116
f.write(AsBytes(data));
31143117
}
3118+
3119+
std::function<void(const CAddress& addr,
3120+
const std::string& msg_type,
3121+
const Span<const unsigned char>& data,
3122+
bool is_incoming)>
3123+
CaptureMessage = CaptureMessageToFile;

src/net.h

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
#include <condition_variable>
3232
#include <cstdint>
3333
#include <deque>
34+
#include <functional>
3435
#include <map>
3536
#include <memory>
3637
#include <optional>
@@ -1272,7 +1273,17 @@ class CConnman
12721273
};
12731274

12741275
/** Dump binary message to file, with timestamp */
1275-
void CaptureMessage(const CAddress& addr, const std::string& msg_type, const Span<const unsigned char>& data, bool is_incoming);
1276+
void CaptureMessageToFile(const CAddress& addr,
1277+
const std::string& msg_type,
1278+
const Span<const unsigned char>& data,
1279+
bool is_incoming);
1280+
1281+
/** Defaults to `CaptureMessageToFile()`, but can be overridden by unit tests. */
1282+
extern std::function<void(const CAddress& addr,
1283+
const std::string& msg_type,
1284+
const Span<const unsigned char>& data,
1285+
bool is_incoming)>
1286+
CaptureMessage;
12761287

12771288
struct NodeEvictionCandidate
12781289
{

test/functional/p2p_message_capture.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
MSGTYPE_SIZE = 12
2121

2222
def mini_parser(dat_file):
23-
"""Parse a data file created by CaptureMessage.
23+
"""Parse a data file created by CaptureMessageToFile.
2424
2525
From the data file we'll only check the structure.
2626

0 commit comments

Comments
 (0)