Skip to content

Commit

Permalink
Add some fuzz testing for WriteBitBuffer
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 705135901
  • Loading branch information
trevorknight authored and jwcullen committed Dec 12, 2024
1 parent 4d029f9 commit 0a42034
Show file tree
Hide file tree
Showing 2 changed files with 103 additions and 0 deletions.
11 changes: 11 additions & 0 deletions iamf/common/tests/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -78,3 +78,14 @@ cc_test(
"@com_google_googletest//:gtest_main",
],
)

cc_test(
name = "write_bit_buffer_fuzz_test",
size = "small",
srcs = ["write_bit_buffer_fuzz_test.cc"],
deps = [
"//iamf/common:write_bit_buffer",
"@com_google_fuzztest//fuzztest",
"@com_google_googletest//:gtest_main",
],
)
92 changes: 92 additions & 0 deletions iamf/common/tests/write_bit_buffer_fuzz_test.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
/*
* Copyright (c) 2024, Alliance for Open Media. All rights reserved
*
* This source code is subject to the terms of the BSD 3-Clause Clear License
* and the Alliance for Open Media Patent License 1.0. If the BSD 3-Clause Clear
* License was not distributed with this source code in the LICENSE file, you
* can obtain it at www.aomedia.org/license/software-license/bsd-3-c-c. If the
* Alliance for Open Media Patent License 1.0 was not distributed with this
* source code in the PATENTS file, you can obtain it at
* www.aomedia.org/license/patent.
*/
#include <sys/types.h>

#include <cstdint>

#include "fuzztest/fuzztest.h"
#include "gtest/gtest.h"
#include "iamf/common/write_bit_buffer.h"

namespace iamf_tools {
namespace {

void WriteSignedLiteral(uint32_t data, int num_bits) {
WriteBitBuffer wb(0);
auto status = wb.WriteUnsignedLiteral(data, num_bits);
if (status.ok()) {
EXPECT_EQ(wb.bit_offset(), num_bits);
} else {
EXPECT_EQ(wb.bit_offset(), 0);
}
}

FUZZ_TEST(WriteBitBufferFuzzTest, WriteSignedLiteral);

void WriteSignedLiteral64(uint64_t data, int num_bits) {
WriteBitBuffer wb(0);
auto status = wb.WriteUnsignedLiteral(data, num_bits);
if (status.ok()) {
EXPECT_EQ(wb.bit_offset(), num_bits);
} else {
EXPECT_EQ(wb.bit_offset(), 0);
}
}

FUZZ_TEST(WriteBitBufferFuzzTest, WriteSignedLiteral64);

void WriteSigned8(int8_t data) {
WriteBitBuffer wb(0);
auto status = wb.WriteSigned8(data);
if (status.ok()) {
EXPECT_EQ(wb.bit_offset(), 8);
} else {
EXPECT_EQ(wb.bit_offset(), 0);
}
}

FUZZ_TEST(WriteBitBufferFuzzTest, WriteSigned8);

void WriteSigned16(int16_t data) {
WriteBitBuffer wb(0);
auto status = wb.WriteSigned16(data);
if (status.ok()) {
EXPECT_EQ(wb.bit_offset(), 16);
} else {
EXPECT_EQ(wb.bit_offset(), 0);
}
}

FUZZ_TEST(WriteBitBufferFuzzTest, WriteSigned16);

void WriteString(const std::string& data) {
WriteBitBuffer wb(0);
auto status = wb.WriteString(data);
}

FUZZ_TEST(WriteBitBufferFuzzTest, WriteString);

void WriteUint8Vector(const std::vector<uint8_t>& data) {
WriteBitBuffer wb(0);
auto status = wb.WriteUint8Vector(data);
if (status.ok()) {
EXPECT_EQ(wb.bit_offset(), data.size() * 8);
EXPECT_EQ(wb.bit_buffer(), data);
} else {
EXPECT_EQ(wb.bit_offset(), 0);
}
}

FUZZ_TEST(WriteBitBufferFuzzTest, WriteUint8Vector);

} // namespace
} // namespace iamf_tools

0 comments on commit 0a42034

Please sign in to comment.