-
Notifications
You must be signed in to change notification settings - Fork 50
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add new AddContainingCharacterMutator(#411)
AddContainingCharacterMutator helps with scenarios of going from squat candidate, to popular package name, and then confirming that said squat candidate is a squat on the popular package.
- Loading branch information
Showing
4 changed files
with
78 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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
38 changes: 38 additions & 0 deletions
38
src/oss-find-squats-lib/Mutators/AddContainingCharacterMutator.cs
This file contains 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,38 @@ | ||
// Copyright (c) Microsoft Corporation. Licensed under the MIT License. | ||
|
||
namespace Microsoft.CST.OpenSource.FindSquats.Mutators | ||
{ | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
|
||
/// <summary> | ||
/// Generates mutations for adding a character in the string, where the character being added already exists in the string. | ||
/// </summary> | ||
/// <example>requests -> reuquests. But won't generate something like requests -> regquests</example> | ||
public class AddContainingCharacterMutator : IMutator | ||
{ | ||
public MutatorType Kind { get; } = MutatorType.AddContainingCharacter; | ||
|
||
public IEnumerable<Mutation> Generate(string arg) | ||
{ | ||
var chars = arg.Distinct().ToArray(); | ||
for (int i = 0; i < arg.Length+1; i++) | ||
{ | ||
// Can't add a character before an @ for a scoped package. | ||
if (i == 0 && arg[i] == '@') | ||
{ | ||
continue; | ||
} | ||
|
||
foreach (var c in chars) | ||
{ | ||
yield return new Mutation( | ||
mutated: $"{arg[..i]}{c}{arg[i..]}", | ||
original: arg, | ||
mutator: Kind, | ||
reason: $"Containing Character Added: {c}"); | ||
} | ||
} | ||
} | ||
} | ||
} |
This file contains 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
This file contains 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 |
---|---|---|
|
@@ -565,6 +565,41 @@ public async Task NewtonsoftMutations_Succeeds_Async() | |
CollectionAssert.AreEquivalent(squattingPackages, resultingMutationNames); | ||
} | ||
|
||
[TestMethod] | ||
public async Task RequestsMutations_Succeeds_Async() | ||
{ | ||
// arrange | ||
PackageURL requests = new("pkg:pypi/[email protected]"); | ||
|
||
string[] squattingPackages = new[] | ||
{ | ||
"pkg:pypi/reuquests", // AddContainingCharacter | ||
"pkg:pypi/requestss", // DoubleHit | ||
"pkg:pypi/reqests", // RemovedCharacter | ||
"pkg:pypi/request", // RemovedCharacter | ||
"pkg:pypi/requets", // RemovedCharacter | ||
}; | ||
|
||
IHttpClientFactory httpClientFactory = | ||
FindSquatsHelper.SetupHttpCalls(purl: requests, validSquats: squattingPackages); | ||
|
||
IManagerPackageActions<NuGetPackageVersionMetadata> packageActions = PackageActionsHelper<NuGetPackageVersionMetadata>.SetupPackageActions(requests, validSquats: squattingPackages) ?? throw new InvalidOperationException(); | ||
Dictionary<string, ProjectManagerFactory.ConstructProjectManager> overrideDict = ProjectManagerFactory.GetDefaultManagers(httpClientFactory); | ||
|
||
overrideDict[NuGetProjectManager.Type] = directory => | ||
new NuGetProjectManager(directory, packageActions, httpClientFactory); | ||
|
||
FindPackageSquats findPackageSquats = new(new ProjectManagerFactory(overrideDict), requests); | ||
|
||
// act | ||
IDictionary<string, IList<Mutation>>? squatCandidates = findPackageSquats.GenerateSquatCandidates(); | ||
List<FindPackageSquatResult> existingMutations = await findPackageSquats.FindExistingSquatsAsync(squatCandidates, new MutateOptions(){UseCache = false}).ToListAsync(); | ||
Assert.IsNotNull(existingMutations); | ||
Assert.IsTrue(existingMutations.Any()); | ||
string[] resultingMutationNames = existingMutations.Select(m => m.MutatedPackageUrl.ToString()).ToArray(); | ||
CollectionAssert.AreEquivalent(squattingPackages, resultingMutationNames); | ||
} | ||
|
||
[TestMethod] | ||
public async Task ScopedPackage_Succeeds_Async() | ||
{ | ||
|