Skip to content
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

[Fuzz] Add Fuzz testing for RegistryPreview #37607

Open
wants to merge 18 commits into
base: main
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
add annotations and change net7.0--net8.0
  • Loading branch information
chenmy77 committed Feb 24, 2025
commit d1b4fee0b6e0a8a6d9a331c29b89cfdbbd262925
20 changes: 15 additions & 5 deletions src/modules/registrypreview/RegistryPreview.FuzzTests/FuzzTests.cs
Original file line number Diff line number Diff line change
@@ -18,10 +18,12 @@ public class FuzzTests
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);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Based on the comments, the result represents registry file content, so the name filenameText might be confusing. Would registryContent be a better choice?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, I also think so,registryContent would be a better choice than filenameText. I will modify it.


string[] registryLines = filenameText.Split("\r");
@@ -32,15 +34,17 @@ public static void FuzzCheckKeyLineForBrackets(ReadOnlySpan<byte> input)
}

// 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];
registryLine = registryLines[index]; // Extract content after the header

ParseHelper.ProcessRegistryLine(registryLine);
if (registryLine.StartsWith("[-", StringComparison.InvariantCulture))
@@ -49,11 +53,15 @@ public static void FuzzCheckKeyLineForBrackets(ReadOnlySpan<byte> input)
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
@@ -62,12 +70,12 @@ public static void FuzzCheckKeyLineForBrackets(ReadOnlySpan<byte> input)
}
}

// Case 1: Fuzz test for StripFirstAndLast
public static void FuzzStripFirstAndLast(ReadOnlySpan<byte> input)
{
string registryLine;

var filenameText = GenerateRegistryHeader(input);
Console.WriteLine($"\n input1 is {filenameText}");

filenameText = filenameText.Replace("\r\n", "\r");
string[] registryLines = filenameText.Split("\r");
@@ -87,7 +95,6 @@ public static void FuzzStripFirstAndLast(ReadOnlySpan<byte> input)

int index = 1;
registryLine = registryLines[index];
Console.WriteLine($"\n registryLine1 is {registryLine}");

ParseHelper.ProcessRegistryLine(registryLine);

@@ -98,23 +105,24 @@ public static void FuzzStripFirstAndLast(ReadOnlySpan<byte> input)

string imageName = DELETEDKEYIMAGE;
ParseHelper.CheckKeyLineForBrackets(ref registryLine, ref imageName);
Console.WriteLine($"\n CheckKeyLineForBrackets is {registryLine}");

// Fuzz test for the StripFirstAndLast method
registryLine = ParseHelper.StripFirstAndLast(registryLine);
}
else if (registryLine.StartsWith('['))
{
string imageName = KEYIMAGE;
ParseHelper.CheckKeyLineForBrackets(ref registryLine, ref imageName);
Console.WriteLine($"\n CheckKeyLineForBrackets2 is {registryLine}");

// Fuzz test for the StripFirstAndLast method
registryLine = ParseHelper.StripFirstAndLast(registryLine);
}
else if (registryLine.StartsWith('"') && registryLine.EndsWith("=-", StringComparison.InvariantCulture))
{
registryLine = registryLine.Replace("=-", string.Empty);

// remove the "'s without removing all of them
// Fuzz test for the StripFirstAndLast method
registryLine = ParseHelper.StripFirstAndLast(registryLine);
}
else if (registryLine.StartsWith('"'))
@@ -131,6 +139,8 @@ public static void FuzzStripFirstAndLast(ReadOnlySpan<byte> input)

// trim the whitespace and quotes from the name
name = name.Trim();

// Fuzz test for the StripFirstAndLast method
name = ParseHelper.StripFirstAndLast(name);
}
else
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net7.0</TargetFramework>
<LangVersion>latest</LangVersion>
<TargetFramework>net8.0-windows10.0.19041.0</TargetFramework>
<Platforms>x64;ARM64</Platforms>
<LangVersion>latest</LangVersion>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
Loading
Oops, something went wrong.