From d624a3c4c993e6ee6aff0de2cac6a25df37f49a2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?G=C3=BCnther=20Foidl?= Date: Mon, 15 Jul 2019 20:08:52 +0200 Subject: [PATCH] Project added for fuzz-testing (#74) * Project added for fuzz-testing https://github.com/Metalnem/sharpfuzz is used for this. * Fuzz-buildconfig otherwise CI will fail, because instrumented isn't checked in * Update fuzz/gfoidl.Base64.FuzzTests/Program.cs --- .gitignore | 1 + fuzz/gfoidl.Base64.FuzzTests/Program.cs | 45 +++++++++++++++++++ .../gfoidl.Base64.FuzzTests.csproj | 19 ++++++++ fuzz/init.sh | 10 +++++ fuzz/run.sh | 3 ++ fuzz/testcases/1.txt | 1 + gfoidl.Base64.sln | 24 ++++++++++ 7 files changed, 103 insertions(+) create mode 100644 fuzz/gfoidl.Base64.FuzzTests/Program.cs create mode 100644 fuzz/gfoidl.Base64.FuzzTests/gfoidl.Base64.FuzzTests.csproj create mode 100644 fuzz/init.sh create mode 100644 fuzz/run.sh create mode 100644 fuzz/testcases/1.txt diff --git a/.gitignore b/.gitignore index 0870941..ab674a7 100644 --- a/.gitignore +++ b/.gitignore @@ -220,3 +220,4 @@ _Pvt_Extensions # Projekte mit Schema nach gfoidl /source/packages /NuGet-Packed +/fuzz/instrumented diff --git a/fuzz/gfoidl.Base64.FuzzTests/Program.cs b/fuzz/gfoidl.Base64.FuzzTests/Program.cs new file mode 100644 index 0000000..5e3047c --- /dev/null +++ b/fuzz/gfoidl.Base64.FuzzTests/Program.cs @@ -0,0 +1,45 @@ +using System; +using System.Text; +using SharpFuzz; + +// see https://github.com/Metalnem/sharpfuzz-samples + +namespace gfoidl.Base64.FuzzTests +{ + class Program + { + static void Main(string[] args) + { + if (args.Length < 1) + { + Console.WriteLine("Fuzzing method must be given"); + Environment.Exit(1); + } + + switch (args[0]) + { + case "Base64_Default_String": Fuzzer.Run(Base64_Default_String); break; + case "Base64_Url_String" : Fuzzer.Run(Base64_Url_String); break; + default: + Console.WriteLine($"Unknown fuzzing function: {args[0]}"); + Environment.Exit(2); + throw null; + } + } + //--------------------------------------------------------------------- + private static void Base64_Default_String(string input) => Base64_String(input, Base64.Default); + private static void Base64_Url_String(string input) => Base64_String(input, Base64.Url); + //--------------------------------------------------------------------- + private static void Base64_String(string input, Base64 encoder) + { + byte[] inputBytes = Encoding.UTF8.GetBytes(input); + string base64 = encoder.Encode(inputBytes); + byte[] decoded = encoder.Decode(base64); + + if (!inputBytes.AsSpan().SequenceEqual(decoded)) + { + throw new Exception("Roundtripping failed"); + } + } + } +} diff --git a/fuzz/gfoidl.Base64.FuzzTests/gfoidl.Base64.FuzzTests.csproj b/fuzz/gfoidl.Base64.FuzzTests/gfoidl.Base64.FuzzTests.csproj new file mode 100644 index 0000000..71b9d28 --- /dev/null +++ b/fuzz/gfoidl.Base64.FuzzTests/gfoidl.Base64.FuzzTests.csproj @@ -0,0 +1,19 @@ + + + + Exe + netcoreapp3.0 + + + + + + + + + + ..\instrumented\gfoidl.Base64.dll + + + + diff --git a/fuzz/init.sh b/fuzz/init.sh new file mode 100644 index 0000000..5488ef8 --- /dev/null +++ b/fuzz/init.sh @@ -0,0 +1,10 @@ +#!/bin/bash + +echo "See https://github.com/Metalnem/sharpfuzz for setup" + +lib=gfoidl.Base64.dll + +mkdir ./instrumented +cp ../source/gfoidl.Base64/bin/Release/netcoreapp3.0/$lib ./instrumented/$lib + +sharpfuzz ./instrumented/$lib diff --git a/fuzz/run.sh b/fuzz/run.sh new file mode 100644 index 0000000..f81197a --- /dev/null +++ b/fuzz/run.sh @@ -0,0 +1,3 @@ +#!/bin/bash + +afl-fuzz -i testcases -o findings -m 10000 dotnet gfoidl.Base64.FuzzTests/bin/Debug/netcoreapp3.0/gfoidl.Base64.FuzzTests.dll $* diff --git a/fuzz/testcases/1.txt b/fuzz/testcases/1.txt new file mode 100644 index 0000000..28cf676 --- /dev/null +++ b/fuzz/testcases/1.txt @@ -0,0 +1 @@ +Hallo Welt! diff --git a/gfoidl.Base64.sln b/gfoidl.Base64.sln index b7774d6..dccd37e 100644 --- a/gfoidl.Base64.sln +++ b/gfoidl.Base64.sln @@ -54,28 +54,50 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "perf", "perf", "{35F8F60F-B EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "gfoidl.Base64.Benchmarks", "perf\gfoidl.Base64.Benchmarks\gfoidl.Base64.Benchmarks.csproj", "{41097931-26CD-4203-B69C-A14CA5FBB69D}" EndProject +Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "fuzz", "fuzz", "{89ADD037-F9FC-4ADE-99B8-D89307057409}" + ProjectSection(SolutionItems) = preProject + fuzz\init.sh = fuzz\init.sh + fuzz\run.sh = fuzz\run.sh + EndProjectSection +EndProject +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "gfoidl.Base64.FuzzTests", "fuzz\gfoidl.Base64.FuzzTests\gfoidl.Base64.FuzzTests.csproj", "{DC515055-B8E7-4C25-B4CF-703292C4F975}" +EndProject +Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "testcases", "testcases", "{18096666-86BE-4AEA-AE79-E4528DFFC43C}" + ProjectSection(SolutionItems) = preProject + fuzz\testcases\1.txt = fuzz\testcases\1.txt + EndProjectSection +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU + Fuzz|Any CPU = Fuzz|Any CPU Release|Any CPU = Release|Any CPU EndGlobalSection GlobalSection(ProjectConfigurationPlatforms) = postSolution {3C85A6AA-23A7-43CC-A744-BD225193369F}.Debug|Any CPU.ActiveCfg = Debug|Any CPU {3C85A6AA-23A7-43CC-A744-BD225193369F}.Debug|Any CPU.Build.0 = Debug|Any CPU + {3C85A6AA-23A7-43CC-A744-BD225193369F}.Fuzz|Any CPU.ActiveCfg = Debug|Any CPU {3C85A6AA-23A7-43CC-A744-BD225193369F}.Release|Any CPU.ActiveCfg = Release|Any CPU {3C85A6AA-23A7-43CC-A744-BD225193369F}.Release|Any CPU.Build.0 = Release|Any CPU {3385B383-37A8-4D67-9C84-4B1F32EF15F5}.Debug|Any CPU.ActiveCfg = Debug|Any CPU {3385B383-37A8-4D67-9C84-4B1F32EF15F5}.Debug|Any CPU.Build.0 = Debug|Any CPU + {3385B383-37A8-4D67-9C84-4B1F32EF15F5}.Fuzz|Any CPU.ActiveCfg = Debug|Any CPU {3385B383-37A8-4D67-9C84-4B1F32EF15F5}.Release|Any CPU.ActiveCfg = Release|Any CPU {3385B383-37A8-4D67-9C84-4B1F32EF15F5}.Release|Any CPU.Build.0 = Release|Any CPU {C642C23E-7465-4E63-B059-CD67B8C89555}.Debug|Any CPU.ActiveCfg = Debug|Any CPU {C642C23E-7465-4E63-B059-CD67B8C89555}.Debug|Any CPU.Build.0 = Debug|Any CPU + {C642C23E-7465-4E63-B059-CD67B8C89555}.Fuzz|Any CPU.ActiveCfg = Debug|Any CPU {C642C23E-7465-4E63-B059-CD67B8C89555}.Release|Any CPU.ActiveCfg = Release|Any CPU {C642C23E-7465-4E63-B059-CD67B8C89555}.Release|Any CPU.Build.0 = Release|Any CPU {41097931-26CD-4203-B69C-A14CA5FBB69D}.Debug|Any CPU.ActiveCfg = Debug|Any CPU {41097931-26CD-4203-B69C-A14CA5FBB69D}.Debug|Any CPU.Build.0 = Debug|Any CPU + {41097931-26CD-4203-B69C-A14CA5FBB69D}.Fuzz|Any CPU.ActiveCfg = Debug|Any CPU {41097931-26CD-4203-B69C-A14CA5FBB69D}.Release|Any CPU.ActiveCfg = Release|Any CPU {41097931-26CD-4203-B69C-A14CA5FBB69D}.Release|Any CPU.Build.0 = Release|Any CPU + {DC515055-B8E7-4C25-B4CF-703292C4F975}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {DC515055-B8E7-4C25-B4CF-703292C4F975}.Fuzz|Any CPU.ActiveCfg = Debug|Any CPU + {DC515055-B8E7-4C25-B4CF-703292C4F975}.Fuzz|Any CPU.Build.0 = Debug|Any CPU + {DC515055-B8E7-4C25-B4CF-703292C4F975}.Release|Any CPU.ActiveCfg = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE @@ -88,6 +110,8 @@ Global {BBE8166F-3522-4A1F-87F7-1D88280529C5} = {42D5558B-424A-4A6F-BFD7-F82D344CD4F5} {C642C23E-7465-4E63-B059-CD67B8C89555} = {A52BCD9C-4216-42E5-AB37-FD5F6C46A9E7} {41097931-26CD-4203-B69C-A14CA5FBB69D} = {35F8F60F-BAC0-487A-83EB-8588797F3B1F} + {DC515055-B8E7-4C25-B4CF-703292C4F975} = {89ADD037-F9FC-4ADE-99B8-D89307057409} + {18096666-86BE-4AEA-AE79-E4528DFFC43C} = {89ADD037-F9FC-4ADE-99B8-D89307057409} EndGlobalSection GlobalSection(ExtensibilityGlobals) = postSolution SolutionGuid = {A902DC31-2F3E-4397-93D1-5004F325B71C}