From 797632b1ca265730ff0f6faccebc92e544d0c27a Mon Sep 17 00:00:00 2001 From: "Mengyuan Chen (from Dev Box)" Date: Thu, 27 Mar 2025 21:08:28 +0800 Subject: [PATCH] add fuzztests for addSingleKeyToTextRemap --- PowerToys.sln | 11 +++++ .../Interop/KeyboardManagerInterop.cs | 4 ++ .../keyboardmanager.FuzzTests/FuzzTests.cs | 28 +++++++++++ .../KeyboardManager.FuzzTests.csproj | 38 +++++++++++++++ .../MSTestSettings.cs | 5 ++ .../OneFuzzConfig.json | 47 +++++++++++++++++++ 6 files changed, 133 insertions(+) create mode 100644 src/modules/keyboardmanager/keyboardmanager.FuzzTests/FuzzTests.cs create mode 100644 src/modules/keyboardmanager/keyboardmanager.FuzzTests/KeyboardManager.FuzzTests.csproj create mode 100644 src/modules/keyboardmanager/keyboardmanager.FuzzTests/MSTestSettings.cs create mode 100644 src/modules/keyboardmanager/keyboardmanager.FuzzTests/OneFuzzConfig.json diff --git a/PowerToys.sln b/PowerToys.sln index c5d1f9176f..94ec5c360b 100644 --- a/PowerToys.sln +++ b/PowerToys.sln @@ -646,6 +646,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Hosts.FuzzTests", "src\modu EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Hosts.UITests", "src\modules\Hosts\Hosts.UITests\Hosts.UITests.csproj", "{4E0AE3A4-2EE0-44D7-A2D0-8769977254A0}" EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "KeyboardManager.FuzzTests", "src\modules\keyboardmanager\keyboardmanager.FuzzTests\KeyboardManager.FuzzTests.csproj", "{73C8A408-5A6F-4750-A90B-E972E455A7C9}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|ARM64 = Debug|ARM64 @@ -2294,6 +2296,14 @@ Global {4E0AE3A4-2EE0-44D7-A2D0-8769977254A0}.Release|ARM64.Build.0 = Release|ARM64 {4E0AE3A4-2EE0-44D7-A2D0-8769977254A0}.Release|x64.ActiveCfg = Release|x64 {4E0AE3A4-2EE0-44D7-A2D0-8769977254A0}.Release|x64.Build.0 = Release|x64 + {73C8A408-5A6F-4750-A90B-E972E455A7C9}.Debug|ARM64.ActiveCfg = Debug|ARM64 + {73C8A408-5A6F-4750-A90B-E972E455A7C9}.Debug|ARM64.Build.0 = Debug|ARM64 + {73C8A408-5A6F-4750-A90B-E972E455A7C9}.Debug|x64.ActiveCfg = Debug|x64 + {73C8A408-5A6F-4750-A90B-E972E455A7C9}.Debug|x64.Build.0 = Debug|x64 + {73C8A408-5A6F-4750-A90B-E972E455A7C9}.Release|ARM64.ActiveCfg = Release|ARM64 + {73C8A408-5A6F-4750-A90B-E972E455A7C9}.Release|ARM64.Build.0 = Release|ARM64 + {73C8A408-5A6F-4750-A90B-E972E455A7C9}.Release|x64.ActiveCfg = Release|x64 + {73C8A408-5A6F-4750-A90B-E972E455A7C9}.Release|x64.Build.0 = Release|x64 EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE @@ -2534,6 +2544,7 @@ Global {4382A954-179A-4078-92AF-715187DFFF50} = {38BDB927-829B-4C65-9CD9-93FB05D66D65} {EBED240C-8702-452D-B764-6DB9DA9179AF} = {F05E590D-AD46-42BE-9C25-6A63ADD2E3EA} {4E0AE3A4-2EE0-44D7-A2D0-8769977254A0} = {F05E590D-AD46-42BE-9C25-6A63ADD2E3EA} + {73C8A408-5A6F-4750-A90B-E972E455A7C9} = {38BDB927-829B-4C65-9CD9-93FB05D66D65} EndGlobalSection GlobalSection(ExtensibilityGlobals) = postSolution SolutionGuid = {C3A2F9D1-7930-4EF4-A6FC-7EE0A99821D0} diff --git a/src/modules/keyboardmanager/KeyboardManagerEditorUI/Interop/KeyboardManagerInterop.cs b/src/modules/keyboardmanager/KeyboardManagerEditorUI/Interop/KeyboardManagerInterop.cs index 59a7063c6c..cd44ca853c 100644 --- a/src/modules/keyboardmanager/KeyboardManagerEditorUI/Interop/KeyboardManagerInterop.cs +++ b/src/modules/keyboardmanager/KeyboardManagerEditorUI/Interop/KeyboardManagerInterop.cs @@ -61,6 +61,10 @@ namespace KeyboardManagerEditorUI.Interop [return: MarshalAs(UnmanagedType.Bool)] internal static extern bool AddSingleKeyRemap(IntPtr config, int originalKey, int targetKey); + [DllImport(DllName)] + [return: MarshalAs(UnmanagedType.Bool)] + internal static extern bool AddSingleKeyToTextRemap(IntPtr config, int originalKey, [MarshalAs(UnmanagedType.LPWStr)] string text); + [DllImport(DllName)] [return: MarshalAs(UnmanagedType.Bool)] internal static extern bool AddSingleKeyToShortcutRemap(IntPtr config, int originalKey, [MarshalAs(UnmanagedType.LPWStr)] string targetKeys); diff --git a/src/modules/keyboardmanager/keyboardmanager.FuzzTests/FuzzTests.cs b/src/modules/keyboardmanager/keyboardmanager.FuzzTests/FuzzTests.cs new file mode 100644 index 0000000000..2afab63420 --- /dev/null +++ b/src/modules/keyboardmanager/keyboardmanager.FuzzTests/FuzzTests.cs @@ -0,0 +1,28 @@ +// 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.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using KeyboardManagerEditorUI.Interop; + +namespace KeyboardManager.FuzzTests +{ + public class FuzzTests + { + // Case1: Fuzzing method for ParseSingleKeyRemap + private static IntPtr _configHandle; + + public static void FuzzAddSingleKeyToTextRemap(ReadOnlySpan input) + { + string remap = Encoding.UTF8.GetString(input); + _configHandle = KeyboardManagerInterop.CreateMappingConfiguration(); + + int originalKey = 28; + + _ = KeyboardManagerInterop.AddSingleKeyToTextRemap(_configHandle, originalKey, remap); + } + } +} diff --git a/src/modules/keyboardmanager/keyboardmanager.FuzzTests/KeyboardManager.FuzzTests.csproj b/src/modules/keyboardmanager/keyboardmanager.FuzzTests/KeyboardManager.FuzzTests.csproj new file mode 100644 index 0000000000..c32fe81734 --- /dev/null +++ b/src/modules/keyboardmanager/keyboardmanager.FuzzTests/KeyboardManager.FuzzTests.csproj @@ -0,0 +1,38 @@ + + + + net8.0-windows10.0.19041.0 + latest + enable + enable + + + + ..\..\..\..\$(Platform)\$(Configuration)\tests\KeyboardManager.FuzzTests\ + + + + + PreserveNewest + + + + + + + + + + + + + + + + + + PreserveNewest + + + + diff --git a/src/modules/keyboardmanager/keyboardmanager.FuzzTests/MSTestSettings.cs b/src/modules/keyboardmanager/keyboardmanager.FuzzTests/MSTestSettings.cs new file mode 100644 index 0000000000..5b05c0b86e --- /dev/null +++ b/src/modules/keyboardmanager/keyboardmanager.FuzzTests/MSTestSettings.cs @@ -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)] diff --git a/src/modules/keyboardmanager/keyboardmanager.FuzzTests/OneFuzzConfig.json b/src/modules/keyboardmanager/keyboardmanager.FuzzTests/OneFuzzConfig.json new file mode 100644 index 0000000000..40b6072c85 --- /dev/null +++ b/src/modules/keyboardmanager/keyboardmanager.FuzzTests/OneFuzzConfig.json @@ -0,0 +1,47 @@ +{ + "configVersion": 3, + "entries": [ + { + "fuzzer": { + "$type": "libfuzzerDotNet", + "dll": "KeyBoardManager.FuzzTests.dll", + "class": "KeyBoardManager.FuzzTests.FuzzTests", + "method": "FuzzAddSingleKeyToTextRemap", + "FuzzingTargetBinaries": [ + "PowerToys.KeyBoardManager.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": "KeyBoardManager", + "targetName": "KeyBoardManager-FuzzAddSingleKeyToTextRemap" + } + ], + "jobDependencies": [ + // this should contain, at minimum, + // the DLL and PDB files + // you will need to add any other files required + // (globs are supported) + "KeyBoardManager.FuzzTests.dll", + "KeyBoardManager.FuzzTests.pdb", + "PowerToys.KeyboardManagerEditorLibraryWrapper.dll", + "Microsoft.Windows.SDK.NET.dll", + "WinRT.Runtime.dll" + ], + "SdlWorkItemId": 49911822 + } + ] +} \ No newline at end of file