|
| 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