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