Skip to content

Commit a70aeae

Browse files
woody-applebzbarsky-apple
authored andcommittedJul 30, 2024·
Add a Base38 fuzzer.
This is split out from project-chip#34311 to see which part of that PR is leading to clang-tidy failures.
1 parent 78adc4d commit a70aeae

File tree

3 files changed

+105
-0
lines changed

3 files changed

+105
-0
lines changed
 

‎src/setup_payload/tests/BUILD.gn

+18
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import("//build_overrides/chip.gni")
1717
import("//build_overrides/pigweed.gni")
1818

1919
import("${chip_root}/build/chip/chip_test_suite.gni")
20+
import("${chip_root}/build/chip/fuzz_test.gni")
2021

2122
chip_test_suite("tests") {
2223
output_name = "libSetupPayloadTests"
@@ -38,3 +39,20 @@ chip_test_suite("tests") {
3839
"${chip_root}/src/setup_payload",
3940
]
4041
}
42+
43+
if (enable_fuzz_test_targets) {
44+
chip_fuzz_target("fuzz-setup-payload-base38") {
45+
sources = [ "FuzzBase38.cpp" ]
46+
public_deps = [
47+
"${chip_root}/src/platform/logging:stdio",
48+
"${chip_root}/src/setup_payload",
49+
]
50+
}
51+
chip_fuzz_target("fuzz-setup-payload-base38-decode") {
52+
sources = [ "FuzzBase38Decode.cpp" ]
53+
public_deps = [
54+
"${chip_root}/src/platform/logging:stdio",
55+
"${chip_root}/src/setup_payload",
56+
]
57+
}
58+
}
+63
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
#include <cstddef>
2+
#include <cstdint>
3+
#include <fstream>
4+
#include <iostream>
5+
#include <string.h>
6+
7+
#include <setup_payload/Base38Decode.h>
8+
#include <setup_payload/Base38Encode.h>
9+
10+
using namespace chip;
11+
12+
/**
13+
* @file
14+
* This file describes a base38 roundtrip Fuzzer.
15+
* It starts by encoding the fuzzing value passed
16+
* in Base38. The value encoded will then be decoded.
17+
* The fuzzer verify that the decoded value is the same
18+
* as the one in input.
19+
*/
20+
21+
extern "C" int LLVMFuzzerTestOneInput(const uint8_t * data, size_t len)
22+
{
23+
size_t outputSizeNeeded = base38EncodedLength(len);
24+
const size_t kMaxOutputSize = 512;
25+
26+
if (outputSizeNeeded > kMaxOutputSize)
27+
{
28+
return 0;
29+
}
30+
31+
ByteSpan span(data, len);
32+
char encodedBuf[kMaxOutputSize];
33+
MutableCharSpan encodedSpan(encodedBuf);
34+
CHIP_ERROR encodingError = base38Encode(span, encodedSpan);
35+
36+
if (encodingError != CHIP_NO_ERROR)
37+
{
38+
__builtin_trap();
39+
}
40+
41+
std::string base38EncodedString(encodedSpan.data(), encodedSpan.size());
42+
43+
std::vector<uint8_t> decodedData;
44+
CHIP_ERROR decodingError = base38Decode(base38EncodedString, decodedData);
45+
46+
if (decodingError == CHIP_NO_ERROR)
47+
{
48+
if (decodedData.size() != len)
49+
{
50+
__builtin_trap();
51+
}
52+
53+
if (memcmp(data, decodedData.data(), len) != 0)
54+
{
55+
__builtin_trap();
56+
}
57+
}
58+
else
59+
{
60+
__builtin_trap();
61+
}
62+
return 0;
63+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
#include <cstddef>
2+
#include <cstdint>
3+
#include <iostream>
4+
5+
#include <setup_payload/Base38Decode.h>
6+
7+
using namespace chip;
8+
9+
/**
10+
* @file
11+
* This file describes a Fuzzer for decoding base38 encoded strings.
12+
*/
13+
14+
extern "C" int LLVMFuzzerTestOneInput(const uint8_t * data, size_t len)
15+
{
16+
std::string base38EncodedString(reinterpret_cast<const char *>(data), len);
17+
std::vector<uint8_t> decodedData;
18+
19+
// Ignoring return value, because in general the data is garbage and won't decode properly.
20+
// We're just testing that the decoder does not crash on the fuzzer-generated inputs.
21+
chip::base38Decode(base38EncodedString, decodedData);
22+
23+
return 0;
24+
}

0 commit comments

Comments
 (0)