Skip to content

Commit

Permalink
Create fuzz tests for MemoryBasedReadBitBuffer
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 705160522
  • Loading branch information
trevorknight authored and jwcullen committed Dec 12, 2024
1 parent 14d4e9e commit d079e0c
Show file tree
Hide file tree
Showing 4 changed files with 150 additions and 0 deletions.
11 changes: 11 additions & 0 deletions iamf/common/read_bit_buffer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -309,6 +309,9 @@ absl::Status ReadBitBuffer::ReadUnsignedLiteralInternal(const int num_bits,
if (num_bits > max_num_bits) {
return absl::InvalidArgumentError("num_bits must be <= max_num_bits.");
}
if (num_bits < 0) {
return absl::InvalidArgumentError("num_bits must be >= 0.");
}
if (buffer_bit_offset_ < 0) {
return absl::InvalidArgumentError("buffer_bit_offset_ must be >= 0.");
}
Expand Down Expand Up @@ -370,6 +373,10 @@ absl::Status ReadBitBuffer::ReadUnsignedLiteralInternal(const int num_bits,
std::unique_ptr<MemoryBasedReadBitBuffer>
MemoryBasedReadBitBuffer::CreateFromVector(int64_t capacity,
const std::vector<uint8_t>& source) {
if (capacity < 0) {
LOG(ERROR) << "MemoryBasedReadBitBuffer capacity must be >= 0.";
return nullptr;
}
return absl::WrapUnique(new MemoryBasedReadBitBuffer(capacity, source));
}

Expand Down Expand Up @@ -397,6 +404,10 @@ MemoryBasedReadBitBuffer::MemoryBasedReadBitBuffer(
std::unique_ptr<FileBasedReadBitBuffer>
FileBasedReadBitBuffer::CreateFromFilePath(
const int64_t capacity, const std::filesystem::path& file_path) {
if (capacity < 0) {
LOG(ERROR) << "FileBasedReadBitBuffer capacity must be >= 0.";
return nullptr;
}
if (!std::filesystem::exists(file_path)) {
LOG(ERROR) << "File not found: " << file_path;
return nullptr;
Expand Down
14 changes: 14 additions & 0 deletions iamf/common/tests/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,20 @@ cc_test(
],
)

cc_test(
name = "read_bit_buffer_fuzz_test",
size = "small",
srcs = ["read_bit_buffer_fuzz_test.cc"],
deps = [
"//iamf/common:read_bit_buffer",
"//iamf/obu:types",
"@com_google_absl//absl/status",
"@com_google_absl//absl/types:span",
"@com_google_fuzztest//fuzztest",
"@com_google_googletest//:gtest_main",
],
)

cc_library(
name = "test_utils",
testonly = True,
Expand Down
113 changes: 113 additions & 0 deletions iamf/common/tests/read_bit_buffer_fuzz_test.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
#include <sys/types.h>

#include <cstdint>
#include <string>
#include <vector>

#include "absl/status/status.h"
#include "absl/types/span.h"
#include "fuzztest/fuzztest.h"
#include "iamf/common/read_bit_buffer.h"
#include "iamf/obu/types.h"

namespace iamf_tools {
namespace {

void ReadUnsignedLiteral64NoUndefinedBehavior(std::vector<uint8_t> data,
int num_bits) {
std::unique_ptr<MemoryBasedReadBitBuffer> rb =
MemoryBasedReadBitBuffer::CreateFromVector(256, data);
uint64_t read_data;
absl::Status status = rb->ReadUnsignedLiteral(num_bits, read_data);
}

FUZZ_TEST(ReadBitBufferFuzzTest, ReadUnsignedLiteral64NoUndefinedBehavior);

void ReadUnsignedLiteral32NoUndefinedBehavior(std::vector<uint8_t> data,
int num_bits) {
std::unique_ptr<MemoryBasedReadBitBuffer> rb =
MemoryBasedReadBitBuffer::CreateFromVector(256, data);
uint32_t read_data;
absl::Status status = rb->ReadUnsignedLiteral(num_bits, read_data);
}

FUZZ_TEST(ReadBitBufferFuzzTest, ReadUnsignedLiteral32NoUndefinedBehavior);

void ReadUnsignedLiteral16NoUndefinedBehavior(std::vector<uint8_t> data,
int num_bits) {
std::unique_ptr<MemoryBasedReadBitBuffer> rb =
MemoryBasedReadBitBuffer::CreateFromVector(256, data);
uint16_t read_data;
absl::Status status = rb->ReadUnsignedLiteral(num_bits, read_data);
}

FUZZ_TEST(ReadBitBufferFuzzTest, ReadUnsignedLiteral16NoUndefinedBehavior);

void ReadUnsignedLiteral8NoUndefinedBehavior(std::vector<uint8_t> data,
int num_bits) {
std::unique_ptr<MemoryBasedReadBitBuffer> rb =
MemoryBasedReadBitBuffer::CreateFromVector(256, data);
uint8_t read_data;
absl::Status status = rb->ReadUnsignedLiteral(num_bits, read_data);
}

FUZZ_TEST(ReadBitBufferFuzzTest, ReadUnsignedLiteral8NoUndefinedBehavior);

void ReadSigned16NoUndefinedBehavior(std::vector<uint8_t> data) {
std::unique_ptr<MemoryBasedReadBitBuffer> rb =
MemoryBasedReadBitBuffer::CreateFromVector(256, data);
int16_t read_data;
absl::Status status = rb->ReadSigned16(read_data);
}

FUZZ_TEST(ReadBitBufferFuzzTest, ReadSigned16NoUndefinedBehavior);

void ReadStringNoUndefinedBehavior(std::vector<uint8_t> data) {
std::unique_ptr<MemoryBasedReadBitBuffer> rb =
MemoryBasedReadBitBuffer::CreateFromVector(256, data);
std::string read_data;
absl::Status status = rb->ReadString(read_data);
}

FUZZ_TEST(ReadBitBufferFuzzTest, ReadStringNoUndefinedBehavior);

void ReadULeb128NoUndefinedBehavior(std::vector<uint8_t> data) {
std::unique_ptr<MemoryBasedReadBitBuffer> rb =
MemoryBasedReadBitBuffer::CreateFromVector(256, data);
DecodedUleb128 read_data;
absl::Status status = rb->ReadULeb128(read_data);
}

FUZZ_TEST(ReadBitBufferFuzzTest, ReadULeb128NoUndefinedBehavior);

void ReadIso14496_1ExpandedNoUndefinedBehavior(std::vector<uint8_t> data,
uint32_t max_class_size) {
std::unique_ptr<MemoryBasedReadBitBuffer> rb =
MemoryBasedReadBitBuffer::CreateFromVector(256, data);
uint32_t read_data;
absl::Status status = rb->ReadIso14496_1Expanded(max_class_size, read_data);
}

FUZZ_TEST(ReadBitBufferFuzzTest, ReadIso14496_1ExpandedNoUndefinedBehavior);

void ReadUint8SpanNoUndefinedBehavior(std::vector<uint8_t> data) {
std::unique_ptr<MemoryBasedReadBitBuffer> rb =
MemoryBasedReadBitBuffer::CreateFromVector(256, data);
std::vector<uint8_t> read_data(data.size());
absl::Span<uint8_t> span(read_data);
absl::Status status = rb->ReadUint8Span(span);
}

FUZZ_TEST(ReadBitBufferFuzzTest, ReadUint8SpanNoUndefinedBehavior);

void ReadBooleanNoUndefinedBehavior(std::vector<uint8_t> data) {
std::unique_ptr<MemoryBasedReadBitBuffer> rb =
MemoryBasedReadBitBuffer::CreateFromVector(256, data);
bool read_data;
absl::Status status = rb->ReadBoolean(read_data);
}

FUZZ_TEST(ReadBitBufferFuzzTest, ReadBooleanNoUndefinedBehavior);

} // namespace
} // namespace iamf_tools
12 changes: 12 additions & 0 deletions iamf/common/tests/read_bit_buffer_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,18 @@ using ::absl_testing::StatusIs;
constexpr int kBitsPerByte = 8;
constexpr int kMaxUint32 = std::numeric_limits<uint32_t>::max();

TEST(MemoryBasedReadBitBufferTest, CreateFromVectorFailsWithNegativeCapacity) {
const std::vector<uint8_t> source_data = {0x01, 0x23, 0x45, 0x68, 0x89};
EXPECT_THAT(MemoryBasedReadBitBuffer::CreateFromVector(-1, source_data),
::testing::IsNull());
}

TEST(FileBasedReadBitBufferTest, CreateFromFilePathFailsWithNegativeCapacity) {
const auto file_path = GetAndCleanupOutputFileName(".iamf");
EXPECT_THAT(FileBasedReadBitBuffer::CreateFromFilePath(-1, file_path),
::testing::IsNull());
}

template <typename BufferReaderType>
std::unique_ptr<BufferReaderType> CreateConcreteReadBitBuffer(
int64_t capacity, std::vector<uint8_t>& source_data);
Expand Down

0 comments on commit d079e0c

Please sign in to comment.