Matching ExactName for known Win32 Programs (#6924)

* Matching explorer.exe also for explorer
Added unit test
Optimized multiple iterations for IProgram

* Fixed linter

Co-authored-by: p-storm <paul.de.man@gmail.com>
This commit is contained in:
P-Storm
2020-10-07 18:23:47 +02:00
committed by GitHub
parent a2fce22973
commit 8b66932036
4 changed files with 52 additions and 27 deletions

View File

@@ -63,7 +63,7 @@ namespace Microsoft.Plugin.Program.UnitTests.Programs
AppType = Win32Program.ApplicationType.Win32Application, AppType = Win32Program.ApplicationType.Win32Application,
}; };
private static readonly Win32Program _fileExplorer = new Win32Program private static readonly Win32Program _fileExplorerLink = new Win32Program
{ {
Name = "File Explorer", Name = "File Explorer",
ExecutableName = "File Explorer.lnk", ExecutableName = "File Explorer.lnk",
@@ -72,6 +72,15 @@ namespace Microsoft.Plugin.Program.UnitTests.Programs
AppType = Win32Program.ApplicationType.Win32Application, AppType = Win32Program.ApplicationType.Win32Application,
}; };
private static readonly Win32Program _fileExplorer = new Win32Program
{
Name = "File Explorer",
ExecutableName = "explorer.exe",
FullPath = "c:\\windows\\explorer.exe",
LnkResolvedPath = null,
AppType = Win32Program.ApplicationType.Win32Application,
};
private static readonly Win32Program _wordpad = new Win32Program private static readonly Win32Program _wordpad = new Win32Program
{ {
Name = "Wordpad", Name = "Wordpad",
@@ -273,7 +282,7 @@ namespace Microsoft.Plugin.Program.UnitTests.Programs
// Arrange // Arrange
List<Win32Program> prgms = new List<Win32Program> List<Win32Program> prgms = new List<Win32Program>
{ {
_fileExplorer, _fileExplorerLink,
}; };
// Act // Act
@@ -403,6 +412,14 @@ namespace Microsoft.Plugin.Program.UnitTests.Programs
Assert.IsTrue(_commandPrompt.QueryEqualsNameForRunCommands(query)); Assert.IsTrue(_commandPrompt.QueryEqualsNameForRunCommands(query));
} }
[TestCase("explorer")]
[TestCase("explorer.exe")]
public void Win32ApplicationsShouldNotFilterWhenExecutingNameOrNameIsUsed(string query)
{
// Even if there is an exact match in the name or exe name, win32 applications should never be filtered
Assert.IsTrue(_fileExplorer.QueryEqualsNameForRunCommands(query));
}
[TestCase("cmd")] [TestCase("cmd")]
[TestCase("Cmd")] [TestCase("Cmd")]
[TestCase("CMD")] [TestCase("CMD")]

View File

@@ -4,6 +4,7 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Collections.Immutable;
using System.Diagnostics; using System.Diagnostics;
using System.Linq; using System.Linq;
using System.Threading.Tasks; using System.Threading.Tasks;
@@ -70,39 +71,42 @@ namespace Microsoft.Plugin.Program
public List<Result> Query(Query query) public List<Result> Query(Query query)
{ {
foreach (var programArgumentParser in _programArgumentParsers) var sources = _programArgumentParsers
{ .Where(programArgumentParser => programArgumentParser.Enabled);
if (!programArgumentParser.Enabled)
{
continue;
}
foreach (var programArgumentParser in sources)
{
if (!programArgumentParser.TryParse(query, out var program, out var programArguments)) if (!programArgumentParser.TryParse(query, out var program, out var programArguments))
{ {
continue; continue;
} }
var results1 = _win32ProgramRepository.AsParallel() return Query(program, programArguments).ToList();
.Where(p => p.Enabled)
.Select(p => p.Result(program, programArguments, _context.API));
var results2 = _packageRepository.AsParallel()
.Where(p => p.Enabled)
.Select(p => p.Result(program, programArguments, _context.API));
var result = results1.Concat(results2).Where(r => r != null && r.Score > 0);
if (result.Any())
{
var maxScore = result.Max(x => x.Score);
result = result.Where(x => x.Score > Settings.MinScoreThreshold * maxScore);
}
return result.ToList();
} }
return new List<Result>(0); return new List<Result>(0);
} }
private IEnumerable<Result> Query(string program, string programArguments)
{
var result = _win32ProgramRepository
.Concat<IProgram>(_packageRepository)
.AsParallel()
.Where(p => p.Enabled)
.Select(p => p.Result(program, programArguments, _context.API))
.Where(r => r?.Score > 0)
.ToArray();
if (result.Any())
{
var maxScore = result.Max(x => x.Score);
return result
.Where(x => x.Score > Settings.MinScoreThreshold * maxScore);
}
return Enumerable.Empty<Result>();
}
public void Init(PluginInitContext context) public void Init(PluginInitContext context)
{ {
_context = context ?? throw new ArgumentNullException(nameof(context)); _context = context ?? throw new ArgumentNullException(nameof(context));

View File

@@ -20,5 +20,7 @@ namespace Microsoft.Plugin.Program.Programs
string Description { get; set; } string Description { get; set; }
string Location { get; } string Location { get; }
bool Enabled { get; set; }
} }
} }

View File

@@ -170,10 +170,12 @@ namespace Microsoft.Plugin.Program.Programs
public bool QueryEqualsNameForRunCommands(string query) public bool QueryEqualsNameForRunCommands(string query)
{ {
if (query != null && AppType == ApplicationType.RunCommand if (query != null && AppType == ApplicationType.RunCommand)
&& !query.Equals(Name, StringComparison.OrdinalIgnoreCase))
{ {
return false; if (!query.Equals(Name, StringComparison.OrdinalIgnoreCase) && !query.Equals(ExecutableName, StringComparison.OrdinalIgnoreCase))
{
return false;
}
} }
return true; return true;