Skip to content

Commit dfc7f62

Browse files
committed
a4-ff: add Packet prefix-cap boundary tests + fix core_test link order
Add packet_test.cpp covering MAX_PREFIX_LENGTH (16) boundary behaviour of the Packet read-constructor (Bug-9 hardening): zero/under/at-cap sizes succeed, over-cap throws std::ios_base::failure, and UAF max-size garbage is rejected without crashing. Wire packet_test.cpp into core_test and fix the pre-existing static-link order bug that left core_test (and the bucket of test/ binaries) RED: libltc_coin.a, pulled in transitively, references core::timestamp() after core has already been processed by ld, leaving it undefined. Linking c2pool_merged_mining after core re-injects core via merged_mining PUBLIC dependency, resolving the late reference. CMake-only, runtime-behaviour preserving. ctest core_test: 32/32 pass.
1 parent 986f4fc commit dfc7f62

2 files changed

Lines changed: 66 additions & 2 deletions

File tree

src/core/test/CMakeLists.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
if (BUILD_TESTING AND (GTest_FOUND OR GTEST_FOUND))
2-
add_executable(core_test pack_test.cpp events_test.cpp chain_test.cpp)
3-
target_link_libraries(core_test PRIVATE GTest::gtest_main core GTest::gtest)
2+
add_executable(core_test pack_test.cpp events_test.cpp chain_test.cpp packet_test.cpp)
3+
target_link_libraries(core_test PRIVATE GTest::gtest_main core c2pool_merged_mining GTest::gtest)
44

55
include(GoogleTest)
66
include_directories(${gtest_SOURCE_DIR}/include ${gtest_SOURCE_DIR})

src/core/test/packet_test.cpp

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
#include <gtest/gtest.h>
2+
3+
#include <cstddef>
4+
#include <limits>
5+
#include <ios>
6+
#include <vector>
7+
8+
#include <core/pack.hpp>
9+
#include <core/pack_types.hpp>
10+
#include <core/message.hpp>
11+
#include <core/hash.hpp>
12+
#include <core/packet.hpp>
13+
14+
// A4 (Bug-9 hardening) cap-boundary tests for the Packet read-constructor.
15+
// The read ctor caps prefix_length at MAX_PREFIX_LENGTH (16) and throws
16+
// std::ios_base::failure on over-cap values, which is how UAF garbage from a
17+
// destroyed m_node (Bug-3-family rapid disconnect/reconnect) is rejected
18+
// without crashing the process. See src/core/packet.hpp.
19+
namespace {
20+
21+
constexpr size_t kCap = 16; // must track MAX_PREFIX_LENGTH in packet.hpp
22+
23+
// --- at/under the cap: must succeed and size the prefix exactly ---
24+
25+
TEST(PacketPrefixCap, ZeroLengthSucceeds)
26+
{
27+
core::Packet p(0);
28+
EXPECT_EQ(p.prefix.size(), 0u);
29+
}
30+
31+
TEST(PacketPrefixCap, JustUnderCapSucceeds)
32+
{
33+
core::Packet p(kCap - 1);
34+
EXPECT_EQ(p.prefix.size(), kCap - 1);
35+
}
36+
37+
TEST(PacketPrefixCap, AtCapSucceeds)
38+
{
39+
// Boundary: exactly at the cap is the largest accepted value.
40+
core::Packet p(kCap);
41+
EXPECT_EQ(p.prefix.size(), kCap);
42+
}
43+
44+
// --- over the cap: must throw cleanly, never resize ---
45+
46+
TEST(PacketPrefixCap, JustOverCapThrows)
47+
{
48+
EXPECT_THROW({ core::Packet p(kCap + 1); }, std::ios_base::failure);
49+
}
50+
51+
TEST(PacketPrefixCap, WayOverCapThrows)
52+
{
53+
EXPECT_THROW({ core::Packet p(1024); }, std::ios_base::failure);
54+
}
55+
56+
TEST(PacketPrefixCap, UafGarbageMaxSizeThrows)
57+
{
58+
// Simulates the garbage size_t read from a freed m_node vector; pre-cap
59+
// this reached resize() and aborted via std::length_error.
60+
EXPECT_THROW({ core::Packet p(std::numeric_limits<size_t>::max()); },
61+
std::ios_base::failure);
62+
}
63+
64+
} // namespace

0 commit comments

Comments
 (0)