Add GoodBad C# example project, integration test (#2148)

This commit is contained in:
Joe Ranweiler
2022-07-12 12:07:30 -07:00
committed by GitHub
parent c1f9d738d9
commit 69b11f60c2
4 changed files with 141 additions and 2 deletions

View File

@ -0,0 +1,46 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
namespace GoodBad;
public class BinaryParser
{
int count = 0;
public void ProcessInput(ReadOnlySpan<byte> data) {
if (data.Length < 4) {
return;
}
if (data[0] == 'b') { count++; }
if (data[1] == 'a') { count++; }
if (data[2] == 'd') { count++; }
if (data[3] == '!') { count++; }
// Simulate an out-of-bounds access while parsing.
if (count >= 4) {
var _ = data[0xdead];
}
}
}
public class Fuzzer {
/// Preferred test method.
public static void TestInput(ReadOnlySpan<byte> data) {
var parser = new BinaryParser();
parser.ProcessInput(data);
}
/// Backwards-compatible test method for legacy code that can't use `Span` types.
///
/// Incurs an extra copy of `data` per fuzzing iteration.
public static void TestInputCompat(byte[] data) {
var parser = new BinaryParser();
parser.ProcessInput(data);
}
/// Invalid static method that has a fuzzing-incompatible signature.
public static void BadSignature(ReadOnlySpan<int> data) {
return;
}
}

View File

@ -0,0 +1,10 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<LangVersion>10.0</LangVersion>
<TargetFramework>net6.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
</Project>