Skip to content

Commit 6d416dc

Browse files
committed
migrating FuzzPayloadDecoder FuzzTest
1 parent d968206 commit 6d416dc

File tree

2 files changed

+47
-16
lines changed

2 files changed

+47
-16
lines changed

build/config/compiler/BUILD.gn

+8-1
Original file line numberDiff line numberDiff line change
@@ -357,13 +357,20 @@ config("runtime_default") {
357357
}
358358
if (current_os == "linux" || current_os == "tizen" || current_os == "webos") {
359359
libs = [
360-
"atomic",
361360
"dl",
362361
"pthread",
363362
"rt",
364363
]
365364
}
366365

366+
#TODO: see what to do with this workaround
367+
# work around because when building pw_fuzzer, the downstreamed pw_fuzzer toolchain makes us use --sysroot flag in the clang++ invocation; the sysroot flag points to the "clang_sysroot" subdirectory of the CIPD environemnt,
368+
# the clang_sysroot folder is populated from CIPD (fuchsia/third_party/sysroot/linux) which does not include the libatomic library and makes the FuzzTest linking fail
369+
# FYI, using the sysroot flag is only activated when pw_toolchain_OSS_FUZZ_ENABLED=false
370+
if (is_pw_fuzz == false) {
371+
libs += [ "atomic" ]
372+
}
373+
367374
cflags = []
368375
ldflags = []
369376

src/lib/format/tests/FuzzPayloadDecoderPW.cpp

+39-15
Original file line numberDiff line numberDiff line change
@@ -23,46 +23,70 @@
2323
#include <tlv/meta/clusters_meta.h>
2424
#include <tlv/meta/protocols_meta.h>
2525

26-
#include <pw_unit_test/framework.h>
2726
#include <pw_fuzzer/fuzztest.h>
27+
#include <pw_unit_test/framework.h>
2828

2929
namespace {
3030

3131
using namespace chip::Decoders;
3232
using namespace chip::FlatTree;
3333
using namespace chip::TLV;
3434
using namespace chip::TLVMeta;
35+
using namespace fuzztest;
3536

36-
void RunDecodePW(const std::vector<std::uint8_t>& bytes)
37+
void RunDecodePW(const std::vector<std::uint8_t> & bytes, chip::Protocols::Id mProtocol, uint8_t mMessageType)
3738
{
38-
const uint8_t* const data{bytes.data()};
39-
const int size{static_cast<int>(bytes.size())};
40-
41-
chip::ByteSpan payload(data, size);
4239

4340
PayloadDecoderInitParams params;
4441
params.SetProtocolDecodeTree(chip::TLVMeta::protocols_meta).SetClusterDecodeTree(chip::TLVMeta::clusters_meta);
42+
// Trying Different Protocols
43+
params.SetProtocol(mProtocol);
44+
// Trying different MessageTypes
45+
params.SetMessageType(mMessageType);
46+
chip::Decoders::PayloadDecoder<64, 128> decoder(params);
4547

48+
const uint8_t * const data{ bytes.data() };
49+
const int size{ static_cast<int>(bytes.size()) };
4650

47-
// Try some SC variants
48-
params.SetProtocol(chip::Protocols::SecureChannel::Id);
49-
params.SetMessageType(0);
50-
51-
chip::Decoders::PayloadDecoder<64, 128> decoder(params);
51+
chip::ByteSpan payload(data, size);
5252

5353
decoder.StartDecoding(payload);
5454

55-
printf("Test is running");
56-
5755
PayloadEntry entry;
5856
while (decoder.Next(entry))
5957
{
6058
// Nothing to do ...
6159
}
60+
61+
// TODO: remove
62+
// PRINT BYTES: To check the combination of bytes being printed
63+
// std::cout << "bytes: ";
64+
// for (const auto& byte : bytes) {
65+
// std::cout << static_cast<int>(byte) << " ";
66+
// }
67+
// std::cout <<std::endl << std::endl;
68+
69+
// printing Protocol IDs
70+
// std::cout << "protocol ID: " << mProtocol.GetProtocolId()<<std::endl;
71+
72+
// printing mMessageType
73+
// std::cout << "mMessageType: " << mMessageType << std::endl;
6274
}
6375

76+
// This allows us to fuzz test with all combinations of protocols
77+
auto ProtocolIDs()
78+
{
79+
return ElementOf({ chip::Protocols::SecureChannel::Id, chip::Protocols::InteractionModel::Id, chip::Protocols::BDX::Id,
80+
chip::Protocols::UserDirectedCommissioning::Id });
81+
}
6482

83+
FUZZ_TEST(PayloadDecoder, RunDecodePW).WithDomains(Arbitrary<std::vector<std::uint8_t>>(), ProtocolIDs(), Arbitrary<uint8_t>());
6584

85+
// TODO: remove
86+
// this test just to make sure regular unit tests are working
87+
TEST(PayloadDecoder, OnePlustTwoIsTwoPlusOne)
88+
{
89+
EXPECT_EQ(1 + 2, 2 + 1);
90+
}
6691

67-
FUZZ_TEST(PayloadDecoder, RunDecodePW);
68-
}
92+
} // namespace

0 commit comments

Comments
 (0)