-
Notifications
You must be signed in to change notification settings - Fork 7.2k
[Fuzz] Add Fuzz testing for RegistryPreview #37607
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 13 commits
Commits
Show all changes
22 commits
Select commit
Hold shift + click to select a range
b205794
Add fuzz test cases
chenmy77 8aefd45
Merge remote-tracking branch 'origin/main' into dev/mengyuanchen/add_…
chenmy77 88f5e65
add fuzz tests framework in registrypreview
chenmy77 5e28035
update code
chenmy77 330e292
add registrypreview fuzzing code
chenmy77 6ee7dba
merge main into code
chenmy77 d1b4fee
add annotations and change net7.0--net8.0
chenmy77 63724c8
Merge remote-tracking branch 'origin/main' into dev/mengyuanchen/add_…
chenmy77 a6a9f92
merge main into code
chenmy77 16c2c2b
add registry fuzz sln
chenmy77 f0951bb
change fuzzing tests scope
chenmy77 11471bd
remove unuse annotations
chenmy77 8ca1c2f
fix typos
chenmy77 a7b8a27
change public parser to internel and private
chenmy77 ad15dd1
change public to internal
chenmy77 136b7ca
Merge remote-tracking branch 'origin/main' into dev/mengyuanchen/add_…
chenmy77 e8269b8
fix linelower error and modify filenametext to registryContent
chenmy77 0438b20
Revert "fix linelower error and modify filenametext to registryContent"
chenmy77 7812c10
add fuzz tests in sln
chenmy77 d5526d1
modify typos
chenmy77 46b3528
clean code
chenmy77 f55289d
add annotations
chenmy77 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
177 changes: 177 additions & 0 deletions
177
src/modules/registrypreview/RegistryPreview.FuzzTests/FuzzTests.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,177 @@ | ||
// Copyright (c) Microsoft Corporation | ||
// The Microsoft Corporation licenses this file to you under the MIT license. | ||
// See the LICENSE file in the project root for more information. | ||
using System; | ||
using System.Diagnostics; | ||
using System.Globalization; | ||
using System.Resources; | ||
using Microsoft.VisualStudio.TestTools.UnitTesting; | ||
using Microsoft.Win32; | ||
using RegistryPreviewUILib; | ||
|
||
namespace RegistryPreview.FuzzTests | ||
{ | ||
public class FuzzTests | ||
{ | ||
private const string REGISTRYHEADER4 = "regedit4"; | ||
private const string REGISTRYHEADER5 = "windows registry editor version 5.00"; | ||
private const string KEYIMAGE = "ms-appx:///Assets/RegistryPreview/folder32.png"; | ||
private const string DELETEDKEYIMAGE = "ms-appx:///Assets/RegistryPreview/deleted-folder32.png"; | ||
|
||
// Case 1: Fuzz test for CheckKeyLineForBrackets | ||
public static void FuzzCheckKeyLineForBrackets(ReadOnlySpan<byte> input) | ||
{ | ||
string registryLine; | ||
|
||
// Simulate registry file content as filenameText | ||
var filenameText = GenerateRegistryHeader(input); | ||
|
||
string[] registryLines = filenameText.Split("\r"); | ||
|
||
if (registryLines.Length <= 1) | ||
{ | ||
return; | ||
} | ||
|
||
// REG files have to start with one of two headers and it's case-insensitive | ||
// The header in the registry file is either REGISTRYHEADER4 or REGISTRYHEADER5 | ||
registryLine = registryLines[0]; | ||
|
||
// Check if the registry header is valid | ||
if (!IsValidRegistryHeader(registryLine)) | ||
{ | ||
return; | ||
} | ||
|
||
int index = 1; | ||
registryLine = registryLines[index]; // Extract content after the header | ||
|
||
ParseHelper.ProcessRegistryLine(registryLine); | ||
if (registryLine.StartsWith("[-", StringComparison.InvariantCulture)) | ||
{ | ||
// remove the - as we won't need it but it will get special treatment in the UI | ||
registryLine = registryLine.Remove(1, 1); | ||
|
||
string imageName = DELETEDKEYIMAGE; | ||
|
||
// Fuzz test for the CheckKeyLineForBrackets method | ||
ParseHelper.CheckKeyLineForBrackets(ref registryLine, ref imageName); | ||
} | ||
else if (registryLine.StartsWith('[')) | ||
{ | ||
string imageName = KEYIMAGE; | ||
|
||
// Fuzz test for the CheckKeyLineForBrackets method | ||
ParseHelper.CheckKeyLineForBrackets(ref registryLine, ref imageName); | ||
} | ||
else | ||
{ | ||
return; | ||
} | ||
} | ||
|
||
// Case 2: Fuzz test for StripFirstAndLast | ||
public static void FuzzStripFirstAndLast(ReadOnlySpan<byte> input) | ||
{ | ||
string registryLine; | ||
|
||
var filenameText = GenerateRegistryHeader(input); | ||
|
||
filenameText = filenameText.Replace("\r\n", "\r"); | ||
string[] registryLines = filenameText.Split("\r"); | ||
|
||
if (registryLines.Length <= 1) | ||
{ | ||
return; | ||
} | ||
|
||
// REG files have to start with one of two headers and it's case-insensitive | ||
registryLine = registryLines[0]; | ||
|
||
if (!IsValidRegistryHeader(registryLine)) | ||
{ | ||
return; | ||
} | ||
|
||
int index = 1; | ||
registryLine = registryLines[index]; | ||
|
||
ParseHelper.ProcessRegistryLine(registryLine); | ||
|
||
if (registryLine.StartsWith("[-", StringComparison.InvariantCulture)) | ||
{ | ||
// remove the - as we won't need it but it will get special treatment in the UI | ||
registryLine = registryLine.Remove(1, 1); | ||
|
||
string imageName = DELETEDKEYIMAGE; | ||
ParseHelper.CheckKeyLineForBrackets(ref registryLine, ref imageName); | ||
|
||
// Fuzz test for the StripFirstAndLast method | ||
registryLine = ParseHelper.StripFirstAndLast(registryLine); | ||
} | ||
else if (registryLine.StartsWith('[')) | ||
{ | ||
string imageName = KEYIMAGE; | ||
ParseHelper.CheckKeyLineForBrackets(ref registryLine, ref imageName); | ||
|
||
// Fuzz test for the StripFirstAndLast method | ||
registryLine = ParseHelper.StripFirstAndLast(registryLine); | ||
} | ||
else if (registryLine.StartsWith('"') && registryLine.EndsWith("=-", StringComparison.InvariantCulture)) | ||
{ | ||
registryLine = registryLine.Replace("=-", string.Empty); | ||
chenmy77 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
// remove the "'s without removing all of them | ||
// Fuzz test for the StripFirstAndLast method | ||
registryLine = ParseHelper.StripFirstAndLast(registryLine); | ||
} | ||
else if (registryLine.StartsWith('"')) | ||
{ | ||
int equal = registryLine.IndexOf('='); | ||
if ((equal < 0) || (equal > registryLine.Length - 1)) | ||
{ | ||
// something is very wrong | ||
return; | ||
} | ||
|
||
// set the name and the value | ||
string name = registryLine.Substring(0, equal); | ||
|
||
// trim the whitespace and quotes from the name | ||
name = name.Trim(); | ||
|
||
// Fuzz test for the StripFirstAndLast method | ||
name = ParseHelper.StripFirstAndLast(name); | ||
} | ||
else | ||
{ | ||
return; | ||
} | ||
} | ||
|
||
public static string GenerateRegistryHeader(ReadOnlySpan<byte> input) | ||
{ | ||
string header = new Random().Next(2) == 0 ? REGISTRYHEADER4 : REGISTRYHEADER5; | ||
|
||
string inputText = System.Text.Encoding.UTF8.GetString(input); | ||
string filenameText = header + "\r\n" + inputText; | ||
|
||
return filenameText.Replace("\r\n", "\r"); | ||
} | ||
|
||
private static bool IsValidRegistryHeader(string line) | ||
{ | ||
// Convert the line to lowercase once for comparison | ||
var lineLower = line.ToLowerInvariant(); | ||
|
||
switch (line) | ||
chenmy77 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
{ | ||
case REGISTRYHEADER4: | ||
case REGISTRYHEADER5: | ||
return true; | ||
default: | ||
return false; | ||
} | ||
} | ||
} | ||
} |
5 changes: 5 additions & 0 deletions
5
src/modules/registrypreview/RegistryPreview.FuzzTests/MSTestSettings.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
// Copyright (c) Microsoft Corporation | ||
// The Microsoft Corporation licenses this file to you under the MIT license. | ||
// See the LICENSE file in the project root for more information. | ||
|
||
[assembly: Parallelize(Scope = ExecutionScope.MethodLevel)] |
90 changes: 90 additions & 0 deletions
90
src/modules/registrypreview/RegistryPreview.FuzzTests/OneFuzzConfig.json
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
// Copyright (c) Microsoft Corporation | ||
// The Microsoft Corporation licenses this file to you under the MIT license. | ||
// See the LICENSE file in the project root for more information. | ||
{ | ||
"configVersion": 3, | ||
"entries": [ | ||
{ | ||
"fuzzer": { | ||
"$type": "libfuzzerDotNet", | ||
"dll": "RegistryPreview.FuzzTests.dll", | ||
"class": "RegistryPreview.FuzzTests.FuzzTests", | ||
"method": "FuzzCheckKeyLineForBrackets", | ||
"FuzzingTargetBinaries": [ | ||
"PowerToys.RegistryPreview.dll" | ||
] | ||
}, | ||
"adoTemplate": { | ||
// supply the values appropriate to your | ||
// project, where bugs will be filed | ||
"org": "microsoft", | ||
"project": "OS", | ||
"AssignedTo": "mengyuanchen@microsoft.com", | ||
"AreaPath": "OS\\Windows Client and Services\\WinPD\\DEEP-Developer Experience, Ecosystem and Partnerships\\SHINE\\PowerToys", | ||
"IterationPath": "OS\\Future" | ||
}, | ||
"jobNotificationEmail": "mengyuanchen@microsoft.com", | ||
"skip": false, | ||
"rebootAfterSetup": false, | ||
"oneFuzzJobs": [ | ||
// at least one job is required | ||
{ | ||
"projectName": "RegistryPreview", | ||
"targetName": "RegistryPreview-dotnet-CheckKeyLineForBrackets-fuzzer" | ||
} | ||
], | ||
"jobDependencies": [ | ||
// this should contain, at minimum, | ||
// the DLL and PDB files | ||
// you will need to add any other files required | ||
// (globs are supported) | ||
"RegistryPreview.FuzzTests.dll", | ||
"RegistryPreview.FuzzTests.pdb", | ||
"Microsoft.Windows.SDK.NET.dll", | ||
"WinRT.Runtime.dll" | ||
], | ||
"SdlWorkItemId": 49911822 | ||
}, | ||
{ | ||
"fuzzer": { | ||
"$type": "libfuzzerDotNet", | ||
"dll": "RegistryPreview.FuzzTests.dll", | ||
"class": "RegistryPreview.FuzzTests.FuzzTests", | ||
"method": "FuzzStripFirstAndLast", | ||
"FuzzingTargetBinaries": [ | ||
"PowerToys.RegistryPreview.dll" | ||
] | ||
}, | ||
"adoTemplate": { | ||
// supply the values appropriate to your | ||
// project, where bugs will be filed | ||
"org": "microsoft", | ||
"project": "OS", | ||
"AssignedTo": "mengyuanchen@microsoft.com", | ||
"AreaPath": "OS\\Windows Client and Services\\WinPD\\DEEP-Developer Experience, Ecosystem and Partnerships\\SHINE\\PowerToys", | ||
"IterationPath": "OS\\Future" | ||
}, | ||
"jobNotificationEmail": "mengyuanchen@microsoft.com", | ||
"skip": false, | ||
"rebootAfterSetup": false, | ||
"oneFuzzJobs": [ | ||
// at least one job is required | ||
{ | ||
"projectName": "RegistryPreview", | ||
"targetName": "RegistryPreview-dotnet-StripFirstAndLasts-fuzzer" | ||
} | ||
], | ||
"jobDependencies": [ | ||
// this should contain, at minimum, | ||
// the DLL and PDB files | ||
// you will need to add any other files required | ||
// (globs are supported) | ||
"RegistryPreview.FuzzTests.dll", | ||
"RegistryPreview.FuzzTests.pdb", | ||
"Microsoft.Windows.SDK.NET.dll", | ||
"WinRT.Runtime.dll" | ||
], | ||
"SdlWorkItemId": 49911822 | ||
} | ||
] | ||
} |
33 changes: 33 additions & 0 deletions
33
src/modules/registrypreview/RegistryPreview.FuzzTests/RegistryPreview.FuzzTests.csproj
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<TargetFramework>net8.0-windows10.0.19041.0</TargetFramework> | ||
chenmy77 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
<Platforms>x64;ARM64</Platforms> | ||
<LangVersion>latest</LangVersion> | ||
<ImplicitUsings>enable</ImplicitUsings> | ||
<Nullable>enable</Nullable> | ||
</PropertyGroup> | ||
|
||
<PropertyGroup> | ||
<OutputPath>..\..\..\..\$(Platform)\$(Configuration)\tests\RegistryPreview.FuzzTests\</OutputPath> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<Compile Include="..\RegistryPreviewUILib\ParseHelper.cs" Link="ParseHelper.cs" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<Content Include="OneFuzzConfig.json"> | ||
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory> | ||
</Content> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="MSTest" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<Using Include="Microsoft.VisualStudio.TestTools.UnitTesting" /> | ||
</ItemGroup> | ||
|
||
</Project> |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.