Skip to content

Commit

Permalink
pw_hdlc: Add fuzz tests
Browse files Browse the repository at this point in the history
Change-Id: Ifce388d6f62b68a1ba6603758ef646eeeee8aa8c
Reviewed-on: https://pigweed-review.googlesource.com/c/pigweed/pigweed/+/143473
Reviewed-by: Alexei Frolov <[email protected]>
Commit-Queue: Aaron Green <[email protected]>
  • Loading branch information
nopsledder authored and CQ Bot Account committed May 15, 2023
1 parent adf7465 commit bf4c22f
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 2 deletions.
2 changes: 1 addition & 1 deletion pw_hdlc/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -119,9 +119,9 @@ pw_cc_test(
srcs = ["decoder_test.cc"],
deps = [
":pw_hdlc",
"//pw_fuzzer:fuzztest",
"//pw_result",
"//pw_stream",
"//pw_unit_test",
],
)

Expand Down
5 changes: 4 additions & 1 deletion pw_hdlc/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,10 @@ pw_python_action("generate_decoder_test") {
}

pw_test("decoder_test") {
deps = [ ":pw_hdlc" ]
deps = [
":pw_hdlc",
"$dir_pw_fuzzer:fuzztest",
]
source_gen_deps = [ ":generate_decoder_test" ]
sources = [ "decoder_test.cc" ]
}
Expand Down
1 change: 1 addition & 0 deletions pw_hdlc/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,7 @@ pw_add_test(pw_hdlc.decoder_test
decoder_test.cc
PRIVATE_DEPS
pw_bytes
pw_fuzzer.fuzztest
pw_hdlc
GROUPS
modules
Expand Down
14 changes: 14 additions & 0 deletions pw_hdlc/decoder_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
#include <array>
#include <cstddef>

#include "fuzztest/fuzztest.h"
#include "gtest/gtest.h"
#include "pw_bytes/array.h"
#include "pw_hdlc/internal/protocol.h"
Expand All @@ -25,6 +26,7 @@ namespace pw::hdlc {
namespace {

using std::byte;
using namespace fuzztest;

TEST(Frame, Fields) {
static constexpr auto kFrameData =
Expand Down Expand Up @@ -152,5 +154,17 @@ TEST(Decoder, TooLargeForBuffer_DecodesNextFrame) {
EXPECT_EQ(OkStatus(), decoder.Process(kFlag).status());
}

void ProcessNeverCrashes(ConstByteSpan data) {
DecoderBuffer<1024> decoder;
for (byte b : data) {
if (decoder.Process(b).status() != Status::Unavailable()) {
decoder.Clear();
}
}
}

FUZZ_TEST(Decoder, ProcessNeverCrashes)
.WithDomains(Arbitrary<std::vector<std::byte>>());

} // namespace
} // namespace pw::hdlc

0 comments on commit bf4c22f

Please sign in to comment.