Files
Alphaeus Mote ce67fa2d6b feat(registry): shared registry paths, values and .reg parsing
- RegistryPath reads HKLM\..., HKEY_LOCAL_MACHINE\..., PowerShell drives
  (HKLM:\, HKCU:), Registry:: and Microsoft.PowerShell.Core\Registry::
  provider paths, regedit's Computer\..., forward slashes (when there is no
  backslash), quotes, brackets and stray backslashes, into one canonical form.
- RegistryValue carries a value by type (text, lines, unsigned number as a
  string, hex) and turns into raw bytes and back.
- RegFile parses Version 5.00 and REGEDIT4 text ([key], [-key], @=, "x"=-,
  dword:, hex:, hex(n): with continuations, escapes, comments), reporting
  each skipped line; Decode reads UTF-16 with BOM, UTF-8 and ANSI.
  RegFileWriter writes regedit-style exports.
- Registry operations ride DeviceFileRequest/Reply (Registry field), with
  registry-disabled and no-user statuses; API routes and DTOs, including the
  import preview.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-06 14:46:15 -05:00

126 lines
6.0 KiB
C#

using Shared.Contracts.Registry;
using Xunit;
namespace Shared.Tests;
/// <summary>Every way people write a registry key comes to one canonical form.</summary>
public class RegistryPathTests
{
[Theory]
[InlineData(@"HKLM\SOFTWARE\Contoso", @"HKEY_LOCAL_MACHINE\SOFTWARE\Contoso")]
[InlineData(@"HKEY_LOCAL_MACHINE\SOFTWARE\Contoso", @"HKEY_LOCAL_MACHINE\SOFTWARE\Contoso")]
[InlineData(@"hklm\software\contoso", @"HKEY_LOCAL_MACHINE\software\contoso")]
[InlineData(@"Hkey_Local_Machine\SOFTWARE", @"HKEY_LOCAL_MACHINE\SOFTWARE")]
[InlineData(@"HKLM:\SOFTWARE\Contoso", @"HKEY_LOCAL_MACHINE\SOFTWARE\Contoso")]
[InlineData(@"HKLM:SOFTWARE\Contoso", @"HKEY_LOCAL_MACHINE\SOFTWARE\Contoso")]
[InlineData(@"hkcu:\Software", @"HKEY_CURRENT_USER\Software")]
[InlineData("HKCU:", "HKEY_CURRENT_USER")]
[InlineData(@"HKCU:\", "HKEY_CURRENT_USER")]
[InlineData(@"HKU:\.DEFAULT", @"HKEY_USERS\.DEFAULT")]
[InlineData(@"Registry::HKEY_LOCAL_MACHINE\SOFTWARE", @"HKEY_LOCAL_MACHINE\SOFTWARE")]
[InlineData(@"registry::HKLM\SOFTWARE", @"HKEY_LOCAL_MACHINE\SOFTWARE")]
[InlineData(@"Microsoft.PowerShell.Core\Registry::HKEY_USERS\.DEFAULT", @"HKEY_USERS\.DEFAULT")]
[InlineData(@"Microsoft.PowerShell.Core\Registry::HKCU\Software", @"HKEY_CURRENT_USER\Software")]
[InlineData(@"Computer\HKEY_CLASSES_ROOT\.txt", @"HKEY_CLASSES_ROOT\.txt")]
[InlineData(@"computer\hkcu\Console", @"HKEY_CURRENT_USER\Console")]
[InlineData(@"\Computer\HKEY_LOCAL_MACHINE\SYSTEM", @"HKEY_LOCAL_MACHINE\SYSTEM")]
[InlineData("HKLM/SOFTWARE/Contoso", @"HKEY_LOCAL_MACHINE\SOFTWARE\Contoso")]
[InlineData("Computer/HKEY_LOCAL_MACHINE/SOFTWARE/", @"HKEY_LOCAL_MACHINE\SOFTWARE")]
[InlineData(@"HKLM\SOFTWARE\Contoso\", @"HKEY_LOCAL_MACHINE\SOFTWARE\Contoso")]
[InlineData(@"\\HKLM\\SOFTWARE\\\Contoso\\", @"HKEY_LOCAL_MACHINE\SOFTWARE\Contoso")]
[InlineData(" \"HKCU\\Software\" ", @"HKEY_CURRENT_USER\Software")]
[InlineData("'HKCU\\Software'", @"HKEY_CURRENT_USER\Software")]
[InlineData(@"[HKCC\System]", @"HKEY_CURRENT_CONFIG\System")]
[InlineData(@"HKCR\MIME\Database\Content Type\text/html", @"HKEY_CLASSES_ROOT\MIME\Database\Content Type\text/html")]
[InlineData(@"HKCR\*\shell\Open with Notepad", @"HKEY_CLASSES_ROOT\*\shell\Open with Notepad")]
[InlineData("HKU", "HKEY_USERS")]
[InlineData(@"HKU\", "HKEY_USERS")]
[InlineData(@"hkey_current_config\Software", @"HKEY_CURRENT_CONFIG\Software")]
[InlineData(@"HKCU\Software\Key With Spaces\ and.dots ", @"HKEY_CURRENT_USER\Software\Key With Spaces\ and.dots")]
public void AcceptedFormsComeToTheCanonicalOne(string input, string expected)
{
Assert.True(RegistryPath.TryParse(input, out var path, out var error), error);
Assert.Equal(expected, path.ToString());
Assert.Equal(expected, RegistryPath.Normalize(input));
Assert.Equal(expected, RegistryPath.Parse(path.ToString()).ToString());
}
[Theory]
[InlineData("")]
[InlineData(" ")]
[InlineData(null)]
[InlineData("Computer")]
[InlineData(@"Computer\")]
[InlineData(@"HKXX\Software")]
[InlineData(@"SOFTWARE\Microsoft")]
[InlineData(@"C:\Windows")]
[InlineData("HKEY_PERFORMANCE_DATA")]
[InlineData("Registry::")]
[InlineData("HKLM\\a\0b")]
public void OtherTextIsRefusedWithAReason(string? input)
{
Assert.False(RegistryPath.TryParse(input, out var path, out var error));
Assert.Null(path);
Assert.False(string.IsNullOrWhiteSpace(error));
Assert.Null(RegistryPath.Normalize(input));
Assert.Throws<ArgumentException>(() => RegistryPath.Parse(input));
}
[Fact]
public void AKeyNameIsAtMost255Characters()
{
Assert.True(RegistryPath.TryParse($@"HKCU\{new string('a', 255)}", out _, out _));
Assert.False(RegistryPath.TryParse($@"HKCU\{new string('a', 256)}", out _, out var error));
Assert.Contains("255", error);
}
[Fact]
public void PartsParentAndChild()
{
var path = RegistryPath.Parse(@"HKLM:\SOFTWARE\Contoso\App");
Assert.Equal(RegistryHives.LocalMachine, path.Hive);
Assert.Equal(@"SOFTWARE\Contoso\App", path.SubKey);
Assert.Equal("App", path.Name);
Assert.False(path.IsHive);
Assert.Equal(@"HKEY_LOCAL_MACHINE\SOFTWARE\Contoso", path.Parent!.ToString());
Assert.Equal("HKEY_LOCAL_MACHINE", path.Parent.Parent!.Parent!.ToString());
Assert.Null(path.Parent.Parent.Parent.Parent);
Assert.Equal(@"HKEY_LOCAL_MACHINE\SOFTWARE\Contoso\App\Child", path.Child("Child").ToString());
Assert.Equal(@"HKEY_LOCAL_MACHINE\Top", RegistryPath.Parse("HKLM").Child("Top").ToString());
Assert.Throws<ArgumentException>(() => path.Child(@"a\b"));
Assert.Throws<ArgumentException>(() => path.Child(string.Empty));
Assert.True(path.Parent.Contains(path));
Assert.True(RegistryPath.Parse(@"hklm\software\CONTOSO").Contains(path));
Assert.False(path.Contains(path.Parent));
Assert.False(RegistryPath.Parse(@"HKLM\SOFTWARE\Cont").Contains(path));
Assert.False(RegistryPath.Parse("HKCU").Contains(path));
Assert.True(RegistryPath.Parse("HKLM").Contains(path));
}
[Fact]
public void HivesHaveLongAndShortNames()
{
foreach (var hive in RegistryHives.All)
{
Assert.Equal(hive, RegistryHives.Canonical(hive));
Assert.Equal(hive, RegistryHives.Canonical(RegistryHives.Short(hive)));
Assert.Equal(hive, RegistryHives.Canonical(RegistryHives.Short(hive).ToLowerInvariant()));
}
Assert.Null(RegistryHives.Canonical("HKPD"));
}
[Theory]
[InlineData(null, "64")]
[InlineData("", "64")]
[InlineData("64", "64")]
[InlineData("32", "32")]
[InlineData("32-bit", "32")]
[InlineData("Registry32", "32")]
[InlineData("x86", "32")]
[InlineData("anything", "64")]
public void ViewsAre64Or32(string? view, string expected) => Assert.Equal(expected, RegistryViews.Normalize(view));
}