Skip to content

Commit

Permalink
Ignore invalid sub reg keys (#18)
Browse files Browse the repository at this point in the history
  • Loading branch information
Gitii authored Aug 17, 2024
1 parent d79b29d commit 27bf7ec
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 0 deletions.
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

0 comments on commit 27bf7ec

Please sign in to comment.