Skip to content

Commit 3f86877

Browse files
committed
cleaning-up tests
1 parent 561c637 commit 3f86877

File tree

5 files changed

+35
-26
lines changed

5 files changed

+35
-26
lines changed

src/credentials/tests/FuzzChipCertPW.cpp

+5-9
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66

77
#include "credentials/CHIPCert.h"
88

9+
namespace {
10+
911
using namespace chip;
1012
using namespace chip::Credentials;
1113

@@ -47,11 +49,6 @@ void ChipCertFuzzer(const std::vector<std::uint8_t> & bytes)
4749
(void) ExtractSubjectDNFromChipCert(span, subjectDN);
4850
}
4951

50-
{
51-
ChipCertificateData certData;
52-
(void) DecodeChipCert(span, certData);
53-
}
54-
5552
{
5653
uint8_t outCertBuf[kMaxDERCertLength];
5754
MutableByteSpan outCert(outCertBuf);
@@ -61,13 +58,12 @@ void ChipCertFuzzer(const std::vector<std::uint8_t> & bytes)
6158
{
6259
// TODO: #34352 To Move this to a Fixture once Errors related to FuzzTest Fixtures are resolved
6360
ASSERT_EQ(chip::Platform::MemoryInit(), CHIP_NO_ERROR);
64-
ByteSpan span(bytes.data(), bytes.size());
6561
ValidateChipRCAC(span);
6662
chip::Platform::MemoryShutdown();
6763
}
6864
}
6965

70-
FUZZ_TEST(ChipCert, ChipCertFuzzer).WithDomains(Arbitrary<std::vector<std::uint8_t>>());
66+
FUZZ_TEST(FuzzChipCert, ChipCertFuzzer).WithDomains(Arbitrary<std::vector<std::uint8_t>>());
7167

7268
// The Property function for DecodeChipCertFuzzer, The FUZZ_TEST Macro will call this function.
7369
void DecodeChipCertFuzzer(const std::vector<std::uint8_t> & bytes, BitFlags<CertDecodeFlags> aDecodeFlag)
@@ -88,12 +84,12 @@ void DecodeChipCertFuzzer(const std::vector<std::uint8_t> & bytes, BitFlags<Cert
8884
// https://github.com/google/fuzztest/blob/main/doc/domains-reference.md#elementof-domains-element-of
8985
auto AnyCertDecodeFlag()
9086
{
91-
9287
constexpr BitFlags<CertDecodeFlags> NullDecodeFlag;
9388
constexpr BitFlags<CertDecodeFlags> GenTBSHashFlag(CertDecodeFlags::kGenerateTBSHash);
9489
constexpr BitFlags<CertDecodeFlags> TrustAnchorFlag(CertDecodeFlags::kIsTrustAnchor);
9590

9691
return ElementOf<CertDecodeFlags>({ NullDecodeFlag, GenTBSHashFlag, TrustAnchorFlag });
9792
}
9893

99-
FUZZ_TEST(ChipCert, DecodeChipCertFuzzer).WithDomains(Arbitrary<std::vector<std::uint8_t>>(), AnyCertDecodeFlag());
94+
FUZZ_TEST(FuzzChipCert, DecodeChipCertFuzzer).WithDomains(Arbitrary<std::vector<std::uint8_t>>(), AnyCertDecodeFlag());
95+
} // namespace

src/lib/core/tests/FuzzTlvReaderPW.cpp

+7-2
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
#include "lib/core/TLV.h"
99
#include "lib/core/TLVUtilities.h"
1010

11+
namespace {
12+
1113
using chip::TLV::TLVReader;
1214

1315
using namespace fuzztest;
@@ -20,11 +22,14 @@ static CHIP_ERROR FuzzIterator(const TLVReader & aReader, size_t aDepth, void *
2022
return CHIP_NO_ERROR;
2123
}
2224

23-
void FuzzTlvRead(const std::vector<std::uint8_t> & bytes)
25+
// The Property Function
26+
void FuzzTlvReader(const std::vector<std::uint8_t> & bytes)
2427
{
2528
TLVReader reader;
2629
reader.Init(bytes.data(), bytes.size());
2730
chip::TLV::Utilities::Iterate(reader, FuzzIterator, nullptr);
2831
}
32+
// Fuzz tests are instantiated with the FUZZ_TEST macro
33+
FUZZ_TEST(TLVReader, FuzzTlvReader).WithDomains(Arbitrary<std::vector<std::uint8_t>>());
2934

30-
FUZZ_TEST(ChipCert, FuzzTlvRead).WithDomains(Arbitrary<std::vector<std::uint8_t>>());
35+
} // namespace

src/lib/dnssd/minimal_mdns/tests/FuzzPacketParsingPW.cpp

+9-7
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,11 @@
77
#include <lib/dnssd/minimal_mdns/Parser.h>
88
#include <lib/dnssd/minimal_mdns/RecordData.h>
99

10+
namespace {
11+
1012
using namespace fuzztest;
1113
using namespace std;
1214

13-
namespace {
14-
1515
using namespace chip;
1616
using namespace mdns::Minimal;
1717

@@ -57,8 +57,6 @@ class FuzzDelegate : public ParserDelegate
5757
mdns::Minimal::BytesRange mPacketRange;
5858
};
5959

60-
} // namespace
61-
6260
void PacketParserFuzz(const std::vector<std::uint8_t> & bytes)
6361
{
6462
BytesRange packet(bytes.data(), bytes.data() + bytes.size());
@@ -67,6 +65,8 @@ void PacketParserFuzz(const std::vector<std::uint8_t> & bytes)
6765
mdns::Minimal::ParsePacket(packet, &delegate);
6866
}
6967

68+
FUZZ_TEST(MinimalmDNS, PacketParserFuzz).WithDomains(Arbitrary<vector<uint8_t>>());
69+
7070
class TxtRecordAccumulator : public TxtRecordDelegate
7171
{
7272
public:
@@ -89,7 +89,8 @@ class TxtRecordAccumulator : public TxtRecordDelegate
8989
}
9090
};
9191

92-
void TxtResponderFuzz2(const std::vector<std::uint8_t> & aRecord)
92+
// The Property Function
93+
void TxtResponderFuzz(const std::vector<std::uint8_t> & aRecord)
9394
{
9495

9596
bool equal_sign_present = false;
@@ -126,5 +127,6 @@ void TxtResponderFuzz2(const std::vector<std::uint8_t> & aRecord)
126127
}
127128
}
128129

129-
FUZZ_TEST(MinimalmDNS, TxtResponderFuzz2).WithDomains(Arbitrary<vector<uint8_t>>().WithMaxSize(254));
130-
FUZZ_TEST(MinimalmDNS, PacketParserFuzz).WithDomains(Arbitrary<vector<uint8_t>>());
130+
FUZZ_TEST(MinimalmDNS, TxtResponderFuzz).WithDomains(Arbitrary<vector<uint8_t>>().WithMaxSize(254));
131+
132+
} // namespace

src/lib/format/tests/FuzzPayloadDecoderPW.cpp

+3-1
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,9 @@ void RunDecodeFuzz(const std::vector<std::uint8_t> & bytes, chip::Protocols::Id
5959
}
6060
}
6161

62-
// This allows us to create a FuzzTest "Input Domain" to fuzz test with all the combinations of protocols.
62+
// This function allows us to fuzz using one of four protocols; by using FuzzTests's `ElementOf` API, we define an
63+
// input domain by explicitly enumerating the set of values in it More Info:
64+
// https://github.com/google/fuzztest/blob/main/doc/domains-reference.md#elementof-domains-element-of
6365
auto AnyProtocolID()
6466
{
6567
return ElementOf({ chip::Protocols::SecureChannel::Id, chip::Protocols::InteractionModel::Id, chip::Protocols::BDX::Id,

src/setup_payload/tests/FuzzBase38PW.cpp

+11-7
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
#include <setup_payload/Base38Decode.h>
1010
#include <setup_payload/Base38Encode.h>
1111

12+
namespace {
13+
1214
using namespace fuzztest;
1315
using namespace chip;
1416

@@ -23,6 +25,9 @@ void Base38DecodeFuzz(const std::vector<uint8_t> & bytes)
2325
chip::base38Decode(base38EncodedString, decodedData);
2426
}
2527

28+
// The invocation of the FuzzTest
29+
FUZZ_TEST(Base38Decoder, Base38DecodeFuzz).WithDomains(Arbitrary<std::vector<uint8_t>>());
30+
2631
/* The property function for a base38 roundtrip Fuzzer.
2732
* It starts by encoding the fuzzing value passed
2833
* into Base38. The encoded value will then be decoded.
@@ -55,6 +60,10 @@ void Base38RoundTripFuzz(const std::vector<uint8_t> & bytes)
5560
ASSERT_EQ(decodedData, bytes);
5661
}
5762

63+
// Max size of the vector is defined as 306 since that will give an outputSizeNeeded of 511 which is less than the required
64+
// kMaxOutputSize
65+
FUZZ_TEST(Base38Decoder, Base38RoundTripFuzz).WithDomains(Arbitrary<std::vector<uint8_t>>().WithMaxSize(306));
66+
5867
void FuzzQRCodeSetupPayloadParser(const std::string & s)
5968
{
6069
chip::Platform::MemoryInit();
@@ -63,11 +72,6 @@ void FuzzQRCodeSetupPayloadParser(const std::string & s)
6372
QRCodeSetupPayloadParser(s).populatePayload(payload);
6473
}
6574

66-
// The invocation of the FuzzTest
67-
FUZZ_TEST(Base38Decoder, Base38DecodeFuzz).WithDomains(Arbitrary<std::vector<uint8_t>>());
68-
69-
// Max size of the vector is defined as 306 since that will give an outputSizeNeeded of 511 which is less than the required
70-
// kMaxOutputSize
71-
FUZZ_TEST(Base38Decoder, Base38RoundTripFuzz).WithDomains(Arbitrary<std::vector<uint8_t>>().WithMaxSize(306));
72-
7375
FUZZ_TEST(Base38Decoder, FuzzQRCodeSetupPayloadParser).WithDomains(Arbitrary<std::string>());
76+
77+
} // namespace

0 commit comments

Comments
 (0)