Skip to content

Commit 0befdb4

Browse files
Added buffer overflow tests
1 parent 319aa79 commit 0befdb4

File tree

3 files changed

+74
-0
lines changed

3 files changed

+74
-0
lines changed

tests/_test_amulet_io.py.cpp

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -241,6 +241,74 @@ void test_read_string(EndianState endian, bool read_offset)
241241
}
242242
}
243243

244+
void test_read_overflow()
245+
{
246+
{
247+
std::string value("", 0);
248+
size_t index = 0;
249+
Amulet::BinaryReader reader(value, index);
250+
std::uint32_t int32 = 0;
251+
ASSERT_RAISES(std::out_of_range, reader.read_numeric_into<std::uint32_t>(int32))
252+
ASSERT_RAISES(std::out_of_range, reader.read_numeric<std::uint32_t>())
253+
ASSERT_RAISES(std::out_of_range, reader.read_bytes(4))
254+
ASSERT_RAISES(std::out_of_range, reader.read_string(4))
255+
ASSERT_RAISES(std::out_of_range, reader.read_size_and_bytes())
256+
ASSERT_RAISES(std::out_of_range, reader.read_size_and_string())
257+
ASSERT_EQUAL(size_t, 0, reader.get_position())
258+
ASSERT_EQUAL(bool, false, reader.has_more_data())
259+
}
260+
{
261+
std::string value("\x00\x00", 2);
262+
size_t index = 0;
263+
Amulet::BinaryReader reader(value, index);
264+
std::uint32_t int32 = 0;
265+
ASSERT_RAISES(std::out_of_range, reader.read_numeric_into<std::uint32_t>(int32))
266+
ASSERT_RAISES(std::out_of_range, reader.read_numeric<std::uint32_t>())
267+
ASSERT_RAISES(std::out_of_range, reader.read_bytes(4))
268+
ASSERT_RAISES(std::out_of_range, reader.read_string(4))
269+
ASSERT_RAISES(std::out_of_range, reader.read_size_and_bytes())
270+
ASSERT_RAISES(std::out_of_range, reader.read_size_and_string())
271+
ASSERT_EQUAL(size_t, 0, reader.get_position())
272+
ASSERT_EQUAL(bool, true, reader.has_more_data())
273+
}
274+
{
275+
std::string value("\x01\x00\x00\x00", 4);
276+
size_t index = 0;
277+
Amulet::BinaryReader reader(value, index);
278+
std::uint32_t int32 = 0;
279+
ASSERT_RAISES(std::out_of_range, reader.read_size_and_bytes<std::uint32_t>())
280+
ASSERT_EQUAL(size_t, 4, reader.get_position())
281+
ASSERT_EQUAL(bool, false, reader.has_more_data())
282+
}
283+
{
284+
std::string value("\x01\x00\x00\x00", 4);
285+
size_t index = 0;
286+
Amulet::BinaryReader reader(value, index);
287+
std::uint32_t int32 = 0;
288+
ASSERT_RAISES(std::out_of_range, reader.read_size_and_string<std::uint32_t>())
289+
ASSERT_EQUAL(size_t, 4, reader.get_position())
290+
ASSERT_EQUAL(bool, false, reader.has_more_data())
291+
}
292+
{
293+
std::string value("\x02\x00\x00\x00\x00", 5);
294+
size_t index = 0;
295+
Amulet::BinaryReader reader(value, index);
296+
std::uint32_t int32 = 0;
297+
ASSERT_RAISES(std::out_of_range, reader.read_size_and_bytes<std::uint32_t>())
298+
ASSERT_EQUAL(size_t, 4, reader.get_position())
299+
ASSERT_EQUAL(bool, true, reader.has_more_data())
300+
}
301+
{
302+
std::string value("\x02\x00\x00\x00\x00", 5);
303+
size_t index = 0;
304+
Amulet::BinaryReader reader(value, index);
305+
std::uint32_t int32 = 0;
306+
ASSERT_RAISES(std::out_of_range, reader.read_size_and_string<std::uint32_t>())
307+
ASSERT_EQUAL(size_t, 4, reader.get_position())
308+
ASSERT_EQUAL(bool, true, reader.has_more_data())
309+
}
310+
}
311+
244312
void test_write_numeric(EndianState endian)
245313
{
246314
Amulet::BinaryWriter writer = endian == EndianState::Big ? Amulet::BinaryWriter(std::endian::big)
@@ -288,6 +356,7 @@ PYBIND11_MODULE(_test_amulet_io, m)
288356

289357
m.def("test_read_numeric", &test_read_numeric);
290358
m.def("test_read_string", &test_read_string);
359+
m.def("test_read_overflow", &test_read_overflow);
291360
m.def("test_write_numeric", &test_write_numeric);
292361
m.def("test_write_string", &test_write_string);
293362
}

tests/_test_amulet_io.pyi

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,6 @@ def test_read_numeric(
99
endian_data: EndianState, read_offset: bool, read_into: bool
1010
) -> None: ...
1111
def test_read_string(endian_data: EndianState, read_offset: bool) -> None: ...
12+
def test_read_overflow() -> None: ...
1213
def test_write_numeric(endian_data: EndianState) -> None: ...
1314
def test_write_string(endian_data: EndianState) -> None: ...

tests/test_amulet_io.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
EndianState,
55
test_read_numeric,
66
test_read_string,
7+
test_read_overflow,
78
test_write_numeric,
89
test_write_string,
910
)
@@ -23,6 +24,9 @@ def test_read(self) -> None:
2324
with self.subTest(endian_state=endian_state, read_offset=read_offset):
2425
test_read_string(endian_state, read_offset)
2526

27+
def test_read_errors(self) -> None:
28+
test_read_overflow()
29+
2630
def test_write(self) -> None:
2731
for endian_state in (EndianState.Default, EndianState.Little, EndianState.Big):
2832
with self.subTest(endian_state=endian_state):

0 commit comments

Comments
 (0)