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

Ignore invalid sub reg keys #18

Merged
merged 1 commit into from
Aug 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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
54 changes: 54 additions & 0 deletions Community.Wsl.Sdk.Tests/WslApiTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
using FluentAssertions;
using NUnit.Framework;
using System;
using Community.Wsx.Shared;
using FakeItEasy;
using Microsoft.VisualBasic;

namespace Community.Wsl.Sdk.Tests;

public class WslApiTests
{
[Test]
public void GetDistributionList_ShouldSkipNonGuidSubKeys()
{
var currentUser = A.Fake<IRegistryKey>();
var lxssKey = A.Fake<IRegistryKey>();

var validSubKey = A.Fake<IRegistryKey>();
var validSubKeyGuid = Guid.NewGuid().ToString();

var invalidSubKeyGuid = "invalid";

var reg = A.Fake<IRegistry>();

var randomString = "string";

A.CallTo(() => reg.GetCurrentUser()).Returns(currentUser);
A.CallTo(() => currentUser.OpenSubKey(A<string>.Ignored)).Returns(lxssKey);
A.CallTo(() => lxssKey.GetSubKeyNames()).Returns([validSubKeyGuid, invalidSubKeyGuid]);
A.CallTo(() => lxssKey.OpenSubKey(validSubKeyGuid)).Returns(validSubKey);

A.CallTo(() => validSubKey.GetValue<string>(A<string>.Ignored)).Returns(randomString);
A.CallTo(() => validSubKey.GetValue<int>(A<string>.Ignored)).Returns(0);
A.CallTo(() => validSubKey.GetValue(A<string>.Ignored, A<string>.Ignored)).Returns(randomString);

var wsl = new WslApi(reg);

var list = wsl.GetDistributionList();

list.Should().HaveCount(1);

list[0].Should().BeEquivalentTo(new DistroInfo()
{
DistroId = Guid.Parse(validSubKeyGuid),
DistroName = randomString,
KernelCommandLine = [randomString],
IsDefault = false,
WslVersion = 0,
DistroFlags = 0,
DefaultUid = 0,
DefaultEnvironmentVariables = []
}, (options) => options.ComparingByMembers<DistroInfo>().Excluding((info => info.BasePath)));
}
}
1 change: 1 addition & 0 deletions Community.Wsl.Sdk/WslApi.cs
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ public IReadOnlyList<DistroInfo> GetDistributionList()

return lxssKey
.GetSubKeyNames()
.Where((keyName => Guid.TryParse(keyName, out _)))
.Select(
(keyName) =>
ReadFromRegistryKey(
Expand Down
Loading