extend Registry handling capabilities in preparation for Registry settings

Enhanced the functionality of Registry handling in preparation for managing Registry settings
This commit is contained in:
Schmitti91
2024-01-23 12:54:43 +01:00
parent 45149d6547
commit 5f5700b948
14 changed files with 1240 additions and 160 deletions

View File

@@ -0,0 +1,181 @@
using Microsoft.Win32;
using mRemoteNG.Tools.WindowsRegistry;
using NUnit.Framework;
namespace mRemoteNGTests.Tools.Registry
{
internal class WindowsRegistryAdvancedTests : WindowsRegistryAdvanced
{
private const string _TestRootKey = @"Software\mRemoteNGTest";
private const RegistryHive _TestHive = RegistryHive.CurrentUser;
[SetUp]
public void Setup()
{
// GetBoolean && GetBoolValue (GetBoolValue -> Not Advanced but not tested jet)
SetRegistryValue(_TestHive, _TestRootKey, "TestBoolAsString", "true", RegistryValueKind.String);
SetRegistryValue(_TestHive, _TestRootKey, "TestBoolAsDWord", 0, RegistryValueKind.DWord);
// GetInteger Tests
SetRegistryValue(_TestHive, _TestRootKey, "TestInteger", "4711", RegistryValueKind.DWord);
// GetString Tests
SetRegistryValue(_TestHive, _TestRootKey, "TestString1", "Banane", RegistryValueKind.String);
SetRegistryValue(_TestHive, _TestRootKey, "TestString2", "Hund", RegistryValueKind.String);
}
[TearDown]
public void Cleanup()
{
// Delete the registry keys here
DeleteRegistryKey(_TestHive, _TestRootKey, true);
}
#region GetBoolean() Tests
// Non object returns
[Test]
public void GetBooleanFromString_ReturnsTrue()
{
var key = GetBoolean(_TestHive, _TestRootKey, "TestBoolAsString");
Assert.That(key.Value, Is.EqualTo(true));
}
[Test]
public void GetBooleanFromDword_ReturnsFalse()
{
var key = GetBoolean(_TestHive, _TestRootKey, "TestBoolAsDWord");
Assert.That(key.Value, Is.EqualTo(false));
}
[Test]
public void GetBooleanNotProvided_ReturnsDefaultTrue()
{
var key = GetBoolean(_TestHive, _TestRootKey, "TestBoolNotProvided", true);
Assert.Multiple(() =>
{
Assert.That(key.Value, Is.EqualTo(true), "Value should be the default (true)");
Assert.That(key.IsKeyPresent, Is.EqualTo(false), "IsProvided should be false");
});
}
#endregion
#region GetBoolValue()___No Object, just bool value returns
[Test]
public void GetBoolValueFromString_ReturnsTrue()
{
var key = GetBoolValue(_TestHive, _TestRootKey, "TestBoolAsString");
Assert.That(key, Is.EqualTo(true));
}
[Test]
public void GetBoolValueFromDword_ReturnsFalse()
{
var key = GetBoolValue(_TestHive, _TestRootKey, "TestBoolAsDWord", true);
Assert.That(key, Is.EqualTo(false));
}
[Test]
public void GetBoolValue_ReturnsDefaultTrue()
{
var key = GetBoolValue(_TestHive, _TestRootKey, "TestBoolNotProvided", true);
Assert.That(key, Is.EqualTo(true));
}
#endregion
#region GetInteger()
[Test]
public void GetInteger()
{
var key = GetInteger(_TestHive, _TestRootKey, "TestInteger");
Assert.Multiple(() =>
{
Assert.That(key.Value, Is.EqualTo(4711));
Assert.That(key.IsKeyPresent, Is.EqualTo(true));
});
}
[Test]
public void GetInteger_returnObjectDefault()
{
var key = GetInteger(_TestHive, _TestRootKey, "TestIntegerNotProvided");
Assert.Multiple(() =>
{
Assert.That(key.Value, Is.EqualTo(-1), "Value should be the default (-1)");
Assert.That(key.IsKeyPresent, Is.EqualTo(false));
});
}
[Test]
public void GetInteger_returnSpecifiedDefault()
{
var key = GetInteger(_TestHive, _TestRootKey, "TestIntegerNotProvided", 2096);
Assert.Multiple(() =>
{
Assert.That(key.Value, Is.EqualTo(-1), "Value should be the default (-1)");
Assert.That(key.IsKeyPresent, Is.EqualTo(false));
});
}
#endregion
#region GetString()
[Test]
public void GetString()
{
var key = GetString(_TestHive, _TestRootKey, "TestString1");
Assert.Multiple(() =>
{
Assert.That(key.Value, Is.EqualTo("Banane"));
Assert.That(key.IsKeyPresent, Is.EqualTo(true));
});
}
[Test]
public void GetString_ReturnsDefault()
{
var key = GetString(_TestHive, _TestRootKey, "TestStringNotProvided", "Banane");
Assert.Multiple(() =>
{
Assert.That(key.Value, Is.EqualTo("Banane"));
Assert.That(key.IsKeyPresent, Is.EqualTo(false));
});
}
[Test]
public void GetStringValidated_Valid()
{
string[] fruits = { "Banane", "Erdbeere", "Apfel" };
var key = GetStringValidated(_TestHive, _TestRootKey, "TestString1", fruits);
Assert.Multiple(() =>
{
Assert.That(key.Value, Is.EqualTo("Banane"));
Assert.That(key.IsKeyPresent, Is.EqualTo(true));
Assert.That(key.IsValid, Is.EqualTo(true));
});
}
[Test]
public void GetStringValidated_NotValidNull()
{
string[] fruits = { "Banane", "Erdbeere", "Apfel" };
var key = GetStringValidated(_TestHive, _TestRootKey, "TestString2", fruits);
Assert.Multiple(() =>
{
Assert.That(key.Value, Is.EqualTo(null));
Assert.That(key.IsKeyPresent, Is.EqualTo(true));
Assert.That(key.IsValid, Is.EqualTo(false));
});
}
[Test]
public void GetStringValidated_NotValidDefault()
{
string[] fruits = { "Banane", "Erdbeere", "Apfel" };
var key = GetStringValidated(_TestHive, _TestRootKey, "TestString2", fruits, false, "Banane");
Assert.Multiple(() =>
{
Assert.That(key.Value, Is.EqualTo("Banane"));
Assert.That(key.IsKeyPresent, Is.EqualTo(true));
Assert.That(key.IsValid, Is.EqualTo(false));
});
}
#endregion
}
}

View File

@@ -0,0 +1,374 @@
using Microsoft.Win32;
using mRemoteNG.Tools.WindowsRegistry;
using NUnit.Framework;
using static System.Windows.Forms.VisualStyles.VisualStyleElement.Rebar;
namespace mRemoteNGTests.Tools.Registry
{
internal class WindowsRegistryKeyTests
{
private WindowsRegistryKey CompleteRegistryKey { get; set; }
private WindowsRegistryKey PartialRegistryKey { get; set; }
[SetUp]
public void Setup()
{
CompleteRegistryKey = new WindowsRegistryKey()
{
Hive = RegistryHive.CurrentUser,
ValueKind = RegistryValueKind.String,
Name = "Test",
Path = @"SOFTWARE\TEST\TEST\Test",
Value = "CompleteRegistryKey"
};
PartialRegistryKey = new WindowsRegistryKey()
{
Hive = RegistryHive.CurrentUser,
ValueKind = RegistryValueKind.DWord,
};
}
#region WindowsRegistryKey() tests
[Test]
public void WindowsRegistryKeyReadable()
{
Assert.That(CompleteRegistryKey.IsKeyReadable, Is.EqualTo(true));
}
[Test]
public void WindowsRegistryKeyNotReadable()
{
Assert.That(PartialRegistryKey.IsKeyReadable, Is.EqualTo(false));
}
[Test]
public void WindowsRegistryKeyWriteable()
{
Assert.That(CompleteRegistryKey.IsKeyWritable, Is.EqualTo(true));
}
[Test]
public void WindowsRegistryKeyNotWriteable()
{
Assert.That(PartialRegistryKey.IsKeyWritable, Is.EqualTo(false));
}
[Test]
public void WindowsRegistryKeyProvided()
{
Assert.That(CompleteRegistryKey.IsKeyPresent, Is.EqualTo(true));
}
[Test]
public void WindowsRegistryKeyNotProvided()
{
Assert.That(PartialRegistryKey.IsKeyPresent, Is.EqualTo(false));
}
#endregion
#region WindowsRegistryKeyBoolean tests
[Test]
public void WindowsRegistryKeyBoolean_FromStringTrue()
{
WindowsRegistryKey TestKey = new()
{
Hive = RegistryHive.CurrentUser,
ValueKind = RegistryValueKind.String,
Name = "TestBoolString",
Path = @"SOFTWARE\Test",
Value = "true"
};
WindowsRegistryKeyBoolean boolKey = new ();
boolKey.ConvertFromWindowsRegistryKey(TestKey);
Assert.That(boolKey.IsKeyPresent, Is.EqualTo(true));
Assert.That(boolKey.Value, Is.EqualTo(true));
}
[Test]
public void WindowsRegistryKeyBoolean_FromStringFalse()
{
WindowsRegistryKey TestKey = new()
{
Hive = RegistryHive.CurrentUser,
ValueKind = RegistryValueKind.String,
Name = "TestBoolString",
Path = @"SOFTWARE\Test",
Value = "false"
};
WindowsRegistryKeyBoolean boolKey = new();
boolKey.ConvertFromWindowsRegistryKey(TestKey);
Assert.That(boolKey.IsKeyPresent, Is.EqualTo(true));
Assert.That(boolKey.Value, Is.EqualTo(false));
}
[Test]
public void WindowsRegistryKeyBoolean_FromDwordTrue()
{
WindowsRegistryKey TestKey = new()
{
Hive = RegistryHive.CurrentUser,
ValueKind = RegistryValueKind.DWord,
Name = "TestBoolString",
Path = @"SOFTWARE\Test",
Value = "1"
};
WindowsRegistryKeyBoolean boolKey = new();
boolKey.ConvertFromWindowsRegistryKey(TestKey);
Assert.That(boolKey.IsKeyPresent, Is.EqualTo(true));
Assert.That(boolKey.Value, Is.EqualTo(true));
}
[Test]
public void WindowsRegistryKeyBoolean_FromDwordFalse()
{
WindowsRegistryKey TestKey = new()
{
Hive = RegistryHive.CurrentUser,
ValueKind = RegistryValueKind.DWord,
Name = "TestBoolString",
Path = @"SOFTWARE\Test",
Value = "0"
};
WindowsRegistryKeyBoolean boolKey = new();
boolKey.ConvertFromWindowsRegistryKey(TestKey);
Assert.That(boolKey.IsKeyPresent, Is.EqualTo(true));
Assert.That(boolKey.Value, Is.EqualTo(false));
}
[Test]
public void WindowsRegistryKeyBoolean_ReturnDefault()
{
WindowsRegistryKey TestKey = new()
{
Hive = RegistryHive.CurrentUser,
ValueKind = RegistryValueKind.DWord,
Name = "TestBoolString",
Path = @"SOFTWARE\Test",
Value = null
};
WindowsRegistryKeyBoolean boolKey = new();
boolKey.ConvertFromWindowsRegistryKey(TestKey, true);
Assert.That(boolKey.IsKeyPresent, Is.EqualTo(false));
Assert.That(boolKey.Value, Is.EqualTo(true));
}
#endregion
#region WindowsRegistryKeyInteger tests
[Test]
public void WindowsRegistryKeyInteger()
{
WindowsRegistryKey TestKey = new()
{
Hive = RegistryHive.CurrentUser,
ValueKind = RegistryValueKind.DWord,
Name = "TestIntigerString",
Path = @"SOFTWARE\Test",
Value = "4711"
};
WindowsRegistryKeyInteger IntKey = new();
IntKey.ConvertFromWindowsRegistryKey(TestKey);
Assert.That(IntKey.IsKeyPresent, Is.EqualTo(true));
Assert.That(IntKey.Value, Is.EqualTo(4711));
}
[Test]
public void WindowsRegistryKeyInteger_ReturnDefault()
{
WindowsRegistryKey TestKey = new()
{
Hive = RegistryHive.CurrentUser,
ValueKind = RegistryValueKind.DWord,
Name = "TestIntigerString",
Path = @"SOFTWARE\Test",
Value = null
};
WindowsRegistryKeyInteger IntKey = new();
IntKey.ConvertFromWindowsRegistryKey(TestKey, 2096);
Assert.That(IntKey.IsKeyPresent, Is.EqualTo(false));
Assert.That(IntKey.Value, Is.EqualTo(2096));
}
#endregion
#region WindowsRegistryKeyString tests
[Test]
public void WindowsRegistryKeyString()
{
WindowsRegistryKey TestKey = new()
{
Hive = RegistryHive.CurrentUser,
ValueKind = RegistryValueKind.DWord,
Name = "TestRegString",
Path = @"SOFTWARE\Test",
Value = "The Big Bang Theory"
};
WindowsRegistryKeyString StrKey = new();
StrKey.ConvertFromWindowsRegistryKey(TestKey);
Assert.That(StrKey.IsKeyPresent, Is.EqualTo(true));
Assert.That(StrKey.Value, Is.EqualTo("The Big Bang Theory"));
}
[Test]
public void WindowsRegistryKeyString_ReturnDefault()
{
WindowsRegistryKey TestKey = new()
{
Hive = RegistryHive.CurrentUser,
ValueKind = RegistryValueKind.DWord,
Name = "TestRegString",
Path = @"SOFTWARE\Test",
Value = null
};
WindowsRegistryKeyString StrKey = new();
StrKey.ConvertFromWindowsRegistryKey(TestKey, "South Park");
Assert.That(StrKey.IsKeyPresent, Is.EqualTo(false));
Assert.That(StrKey.Value, Is.EqualTo("South Park"));
}
[Test]
public void WindowsRegistryKeyString_ValidateSuccess()
{
WindowsRegistryKey TestKey = new()
{
Hive = RegistryHive.CurrentUser,
ValueKind = RegistryValueKind.DWord,
Name = "TestRegString",
Path = @"SOFTWARE\Test",
Value = "Big Bang"
};
WindowsRegistryKeyString StrKey = new();
StrKey.AllowedValues = new[] { "Big Bang", "Big Bang Theory", "The Big Bang Theory" };
StrKey.ConvertFromWindowsRegistryKey(TestKey);
Assert.That(StrKey.IsKeyPresent, Is.EqualTo(true));
Assert.That(StrKey.IsValid, Is.EqualTo(true));
}
[Test]
public void WindowsRegistryKeyString_ValidateNotSuccess()
{
WindowsRegistryKey TestKey = new()
{
Hive = RegistryHive.CurrentUser,
ValueKind = RegistryValueKind.DWord,
Name = "TestRegString",
Path = @"SOFTWARE\Test",
Value = "ig ang"
};
WindowsRegistryKeyString StrKey = new();
StrKey.AllowedValues = new[] { "Big Bang", "Big Bang Theory", "The Big Bang Theory" };
StrKey.ConvertFromWindowsRegistryKey(TestKey);
Assert.That(StrKey.IsKeyPresent, Is.EqualTo(true));
Assert.That(StrKey.IsValid, Is.EqualTo(false));
}
[Test]
public void WindowsRegistryKeyString_ValidateSuccessCase()
{
WindowsRegistryKey TestKey = new()
{
Hive = RegistryHive.CurrentUser,
ValueKind = RegistryValueKind.DWord,
Name = "TestRegString",
Path = @"SOFTWARE\Test",
Value = "BiG BAng"
};
WindowsRegistryKeyString StrKey = new();
StrKey.AllowedValues = new[] { "BiG BAng", "Big Bang Theory", "The Big Bang Theory" };
StrKey.IsCaseSensitiveValidation = true;
StrKey.ConvertFromWindowsRegistryKey(TestKey);
Assert.That(StrKey.IsKeyPresent, Is.EqualTo(true));
Assert.That(StrKey.IsValid, Is.EqualTo(true));
}
[Test]
public void WindowsRegistryKeyString_ValidateNotSuccessCase()
{
WindowsRegistryKey TestKey = new()
{
Hive = RegistryHive.CurrentUser,
ValueKind = RegistryValueKind.DWord,
Name = "TestRegString",
Path = @"SOFTWARE\Test",
Value = "BiG BAng"
};
WindowsRegistryKeyString StrKey = new();
StrKey.AllowedValues = new[] { "Big Bang", "Big Bang Theory", "The Big Bang Theory" };
StrKey.IsCaseSensitiveValidation = true;
StrKey.ConvertFromWindowsRegistryKey(TestKey);
Assert.That(StrKey.IsKeyPresent, Is.EqualTo(true));
Assert.That(StrKey.IsValid, Is.EqualTo(false));
}
[Test]
public void WindowsRegistryKeyString_ValidateNotSuccessReturnNull()
{
WindowsRegistryKey TestKey = new()
{
Hive = RegistryHive.CurrentUser,
ValueKind = RegistryValueKind.DWord,
Name = "TestRegString",
Path = @"SOFTWARE\Test",
Value = "ig ang"
};
WindowsRegistryKeyString StrKey = new();
StrKey.AllowedValues = new[] { "Big Bang", "Big Bang Theory", "The Big Bang Theory" };
StrKey.ConvertFromWindowsRegistryKey(TestKey);
Assert.That(StrKey.IsKeyPresent, Is.EqualTo(true));
Assert.That(StrKey.IsValid, Is.EqualTo(false));
Assert.That(StrKey.Value, Is.EqualTo(null));
}
[Test]
public void WindowsRegistryKeyString_ValidateNotSuccessValidValue()
{
WindowsRegistryKey TestKey = new()
{
Hive = RegistryHive.CurrentUser,
ValueKind = RegistryValueKind.DWord,
Name = "TestRegString",
Path = @"SOFTWARE\Test",
Value = "ig ang"
};
WindowsRegistryKeyString StrKey = new();
StrKey.AllowedValues = new[] { "Big Bang", "Big Bang Theory", "The Big Bang Theory" };
StrKey.ConvertFromWindowsRegistryKey(TestKey, "Big Bang Theory");
Assert.That(StrKey.IsKeyPresent, Is.EqualTo(true));
Assert.That(StrKey.IsValid, Is.EqualTo(false));
Assert.That(StrKey.Value, Is.EqualTo("Big Bang Theory"));
}
#endregion
}
}

View File

@@ -1,6 +1,7 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
using Microsoft.Win32;
using mRemoteNG.Tools.WindowsRegistry;
using NUnit.Framework;
@@ -9,14 +10,12 @@ namespace mRemoteNGTests.Tools.Registry
{
public class WindowsRegistryTests
{
private IRegistry _registry;
private IRegistryRead _registryReader;
private IRegistryWrite _registryWriter;
[SetUp]
public void Setup()
{
_registry = new WindowsRegistry();
_registryReader = new WindowsRegistry();
_registryWriter = new WindowsRegistry();
}
@@ -28,11 +27,13 @@ namespace mRemoteNGTests.Tools.Registry
var subKeyNames = _registryReader.GetSubKeyNames(RegistryHive.CurrentUser, "Software");
Assert.That(subKeyNames, Does.Contain("Microsoft"));
}
[Test]
public void GetSubkeyNamesThrowsIfGivenNullKeyPath()
{
Assert.Throws<ArgumentNullException>(() => _registryReader.GetSubKeyNames(RegistryHive.CurrentUser, null));
}
[Test]
public void GetSubkeyNamesThrowsIfGivenUnknownHive()
{
@@ -45,13 +46,13 @@ namespace mRemoteNGTests.Tools.Registry
public void CanGetPropertyValue()
{
var keyValue = _registryReader.GetPropertyValue(RegistryHive.ClassesRoot, @".dll\PersistentHandler", "");
Assert.That(keyValue.FirstOrDefault(), Is.EqualTo("{098f2470-bae0-11cd-b579-08002b30bfeb}"));
Assert.That(keyValue, Is.EqualTo("{098f2470-bae0-11cd-b579-08002b30bfeb}"));
}
[Test]
public void CanGetPropertyValueByRegistryKeyObject()
{
WindowsRegistryKey key = new WindowsRegistryKey
WindowsRegistryKey key = new()
{
Hive = RegistryHive.ClassesRoot,
Path = @".dll\PersistentHandler",
@@ -59,7 +60,7 @@ namespace mRemoteNGTests.Tools.Registry
};
var keyValue = _registryReader.GetPropertyValue(key);
Assert.That(keyValue.FirstOrDefault(), Is.EqualTo("{098f2470-bae0-11cd-b579-08002b30bfeb}"));
Assert.That(keyValue, Is.EqualTo("{098f2470-bae0-11cd-b579-08002b30bfeb}"));
}
[Test]
public void GetPropertyValueThrowsIfGivenNullKeyPath()
@@ -85,7 +86,7 @@ namespace mRemoteNGTests.Tools.Registry
[Test]
public void CanGetWindowsRegistryKeyByObject()
{
WindowsRegistryKey key = new WindowsRegistryKey
WindowsRegistryKey key = new()
{
Hive = RegistryHive.ClassesRoot,
Path = @".dll\PersistentHandler",
@@ -107,11 +108,11 @@ namespace mRemoteNGTests.Tools.Registry
[Test]
public void GetWindowsRegistryThrowNotReadable()
{
WindowsRegistryKey key = new WindowsRegistryKey
WindowsRegistryKey key = new()
{
Hive = RegistryHive.ClassesRoot,
};
Assert.Throws<InvalidOperationException>(() => _registryReader.GetWindowsRegistryKey(key));
}
#endregion
@@ -137,7 +138,7 @@ namespace mRemoteNGTests.Tools.Registry
public void IsWindowsRegistryKeyValid()
{
// Tests property rules of WindowsRegistryKey
WindowsRegistryKey key = new WindowsRegistryKey();
WindowsRegistryKey key = new();
Assert.Multiple(() =>
{
@@ -152,26 +153,27 @@ namespace mRemoteNGTests.Tools.Registry
[Test]
public void WindowsRegistryKeyThrowHiveNullException()
{
WindowsRegistryKey key = new WindowsRegistryKey();
WindowsRegistryKey key = new();
Assert.Throws<ArgumentNullException>(() => key.Hive = 0, "Expected IsHiveValid to throw ArgumentNullException");
}
[Test]
public void WindowsRegistryKeyThrowValueKindNullException()
public void WindowsRegistryKeyValueKindUnknown()
{
WindowsRegistryKey key = new WindowsRegistryKey();
Assert.Throws<ArgumentNullException>(() => key.ValueKind = 0, "Expected IsValueKindValid to throw ArgumentNullException");
WindowsRegistryKey key = new();
Assert.That(key.ValueKind, Is.EqualTo(RegistryValueKind.Unknown));
}
[Test]
public void WindowsRegistryKeyThrowPathNullException()
{
WindowsRegistryKey key = new WindowsRegistryKey();
WindowsRegistryKey key = new();
Assert.Throws<ArgumentNullException>(() => key.Path = null, "Expected IsPathValid to throw ArgumentNullException");
}
[Test]
public void WindowsRegistryKeyThrowNameNullException()
{
WindowsRegistryKey key = new WindowsRegistryKey();
WindowsRegistryKey key = new();
Assert.Throws<ArgumentNullException>(() => key.Name = null, "Expected IsNameValid to throw ArgumentNullException");
}
#endregion