From 8b3a68f58e4feda61db7e7d25c7d336aac22458e Mon Sep 17 00:00:00 2001 From: xRushG <145561288+xRushG@users.noreply.github.com> Date: Tue, 11 Jun 2024 09:07:04 +0200 Subject: [PATCH] Removing all unnecessary old files --- .../WindowsRegistry/IRegistryAdvanced.cs | 55 -- .../WindowsRegistry/IRegistryAdvancedRead.cs | 49 -- .../Tools/WindowsRegistry/IRegistryWrite.cs | 26 - .../Tools/WindowsRegistry/WindowsRegistry.cs | 531 ------------------ .../WindowsRegistryAdvanced.cs | 118 ---- .../WindowsRegistry/WindowsRegistryKey.cs | 116 ---- .../WindowsRegistryKeyBoolean.cs | 75 --- .../WindowsRegistryKeyInteger.cs | 78 --- .../WindowsRegistryKeyString.cs | 121 ---- .../Registry/WindowsRegistryAdvancedTests.cs | 188 ------- .../Tools/Registry/WindowsRegistryKeyTests.cs | 386 ------------- 11 files changed, 1743 deletions(-) delete mode 100644 mRemoteNG/Tools/WindowsRegistry/IRegistryAdvanced.cs delete mode 100644 mRemoteNG/Tools/WindowsRegistry/IRegistryAdvancedRead.cs delete mode 100644 mRemoteNG/Tools/WindowsRegistry/IRegistryWrite.cs delete mode 100644 mRemoteNG/Tools/WindowsRegistry/WindowsRegistry.cs delete mode 100644 mRemoteNG/Tools/WindowsRegistry/WindowsRegistryAdvanced.cs delete mode 100644 mRemoteNG/Tools/WindowsRegistry/WindowsRegistryKey.cs delete mode 100644 mRemoteNG/Tools/WindowsRegistry/WindowsRegistryKeyBoolean.cs delete mode 100644 mRemoteNG/Tools/WindowsRegistry/WindowsRegistryKeyInteger.cs delete mode 100644 mRemoteNG/Tools/WindowsRegistry/WindowsRegistryKeyString.cs delete mode 100644 mRemoteNGTests/Tools/Registry/WindowsRegistryAdvancedTests.cs delete mode 100644 mRemoteNGTests/Tools/Registry/WindowsRegistryKeyTests.cs diff --git a/mRemoteNG/Tools/WindowsRegistry/IRegistryAdvanced.cs b/mRemoteNG/Tools/WindowsRegistry/IRegistryAdvanced.cs deleted file mode 100644 index 64960110..00000000 --- a/mRemoteNG/Tools/WindowsRegistry/IRegistryAdvanced.cs +++ /dev/null @@ -1,55 +0,0 @@ -using Microsoft.Win32; -using System; -using System.Collections.Generic; -using System.Runtime.Versioning; - -namespace mRemoteNG.Tools.WindowsRegistry -{ - [SupportedOSPlatform("windows")] - /// - /// Interface for the RegistryWorker class providing functionality to interact with the Windows Registry to retrieve registry settings. - /// - public interface IRegistryAdvanced - { - #region WindowsRegistry reader - string[] GetSubKeyNames(RegistryHive hive, string path); - string GetPropertyValue(WindowsRegistryKey key); - string GetPropertyValue(RegistryHive hive, string path, string name); - - bool GetBoolValue(RegistryHive hive, string path, string propertyName, bool defaultValue = false); - int GetDwordValue(RegistryHive hive, string path, string propertyName, int defaultValue = 0); - - WindowsRegistryKey GetWindowsRegistryKey(RegistryHive hive, string path, string name); - WindowsRegistryKey GetWindowsRegistryKey(WindowsRegistryKey key); - - List GetRegistryEntries(RegistryHive hive, string path); - List GetRegistryEntryiesRecursive(RegistryHive hive, string path); - #endregion - - #region WindowsRegistry writer - void SetRegistryValue(WindowsRegistryKey key); - void SetRegistryValue(RegistryHive hive, string path, string name, object value, RegistryValueKind valueKind); - void DeleteRegistryKey(RegistryHive hive, string path, bool ignoreNotFound = false); - #endregion - - #region WindowsRegistry tools - RegistryHive ConvertStringToRegistryHive(string hiveString); - RegistryValueKind ConvertStringToRegistryValueKind(string valueType); - RegistryValueKind ConvertTypeToRegistryValueKind(Type valueType); - Type ConvertRegistryValueKindToType(RegistryValueKind valueKind); - #endregion - - #region WindowsRegistryAdvanced GetInteger - WindowsRegistryKeyInteger GetInteger(RegistryHive hive, string path, string propertyName, int? defaultValue = null); - #endregion - - #region WindowsRegistryAdvanced GetString - WindowsRegistryKeyString GetString(RegistryHive hive, string path, string propertyName, string defaultValue = null); - WindowsRegistryKeyString GetStringValidated(RegistryHive hive, string path, string propertyName, string[] allowedValues, bool caseSensitive = false, string defaultValue = null); - #endregion - - #region WindowsRegistryAdvanced GetBoolean - WindowsRegistryKeyBoolean GetBoolean(RegistryHive hive, string path, string propertyName, bool? defaultValue = null); - #endregion - } -} \ No newline at end of file diff --git a/mRemoteNG/Tools/WindowsRegistry/IRegistryAdvancedRead.cs b/mRemoteNG/Tools/WindowsRegistry/IRegistryAdvancedRead.cs deleted file mode 100644 index 8d865e17..00000000 --- a/mRemoteNG/Tools/WindowsRegistry/IRegistryAdvancedRead.cs +++ /dev/null @@ -1,49 +0,0 @@ -using Microsoft.Win32; -using System; -using System.Collections.Generic; -using System.Runtime.Versioning; - -namespace mRemoteNG.Tools.WindowsRegistry -{ - [SupportedOSPlatform("windows")] - /// - /// Interface for the RegistryWorker class providing functionality to interact with the Windows Registry to retrieve registry settings. - /// - public interface IRegistryAdvancedRead - { - #region WindowsRegistry reader - string[] GetSubKeyNames(RegistryHive hive, string path); - string GetPropertyValue(WindowsRegistryKey key); - string GetPropertyValue(RegistryHive hive, string path, string name); - - bool GetBoolValue(RegistryHive hive, string path, string propertyName, bool defaultValue = false); - int GetDwordValue(RegistryHive hive, string path, string propertyName, int defaultValue = 0); - - WindowsRegistryKey GetWindowsRegistryKey(RegistryHive hive, string path, string name); - WindowsRegistryKey GetWindowsRegistryKey(WindowsRegistryKey key); - - List GetRegistryEntries(RegistryHive hive, string path); - List GetRegistryEntryiesRecursive(RegistryHive hive, string path); - #endregion - - #region WindowsRegistry tools - RegistryHive ConvertStringToRegistryHive(string hiveString); - RegistryValueKind ConvertStringToRegistryValueKind(string valueType); - RegistryValueKind ConvertTypeToRegistryValueKind(Type valueType); - Type ConvertRegistryValueKindToType(RegistryValueKind valueKind); - #endregion - - #region WindowsRegistryAdvanced GetInteger - WindowsRegistryKeyInteger GetInteger(RegistryHive hive, string path, string propertyName, int? defaultValue = null); - #endregion - - #region WindowsRegistryAdvanced GetString - WindowsRegistryKeyString GetString(RegistryHive hive, string path, string propertyName, string defaultValue = null); - WindowsRegistryKeyString GetStringValidated(RegistryHive hive, string path, string propertyName, string[] allowedValues, bool caseSensitive = false, string defaultValue = null); - #endregion - - #region WindowsRegistryAdvanced GetBoolean - WindowsRegistryKeyBoolean GetBoolean(RegistryHive hive, string path, string propertyName, bool? defaultValue = null); - #endregion - } -} \ No newline at end of file diff --git a/mRemoteNG/Tools/WindowsRegistry/IRegistryWrite.cs b/mRemoteNG/Tools/WindowsRegistry/IRegistryWrite.cs deleted file mode 100644 index edc5d510..00000000 --- a/mRemoteNG/Tools/WindowsRegistry/IRegistryWrite.cs +++ /dev/null @@ -1,26 +0,0 @@ -using Microsoft.Win32; -using System; -using System.Runtime.Versioning; - -namespace mRemoteNG.Tools.WindowsRegistry -{ - [SupportedOSPlatform("windows")] - /// - /// Interface for the Registry class providing methods for interacting with write actions in the Windows Registry. - /// - public interface IRegistryWrite - { - #region registry writer - void SetRegistryValue(WindowsRegistryKey key); - void SetRegistryValue(RegistryHive hive, string path, string name, object value, RegistryValueKind valueKind); - - #endregion - - #region registry tools - RegistryHive ConvertStringToRegistryHive(string hiveString); - RegistryValueKind ConvertStringToRegistryValueKind(string valueType); - RegistryValueKind ConvertTypeToRegistryValueKind(Type valueType); - Type ConvertRegistryValueKindToType(RegistryValueKind valueKind); - #endregion - } -} \ No newline at end of file diff --git a/mRemoteNG/Tools/WindowsRegistry/WindowsRegistry.cs b/mRemoteNG/Tools/WindowsRegistry/WindowsRegistry.cs deleted file mode 100644 index c7c00f8e..00000000 --- a/mRemoteNG/Tools/WindowsRegistry/WindowsRegistry.cs +++ /dev/null @@ -1,531 +0,0 @@ -using Microsoft.Win32; -using System; -using System.Collections.Generic; -using System.IO; -using System.Linq; -using System.Runtime.Versioning; -using System.Security; - -namespace mRemoteNG.Tools.WindowsRegistry -{ - /// - /// This class interacting with the Windows Registry. - /// - [SupportedOSPlatform("windows")] - public class WindowsRegistry : IRegistry, IRegistryRead, IRegistryWrite - { - #region public read - /// - /// Retrieves the names of subkeys under a specified registry key path. - /// - /// The RegistryHive where the subkeys are located. - /// The path to the registry key containing the subkeys. - /// An array of strings containing the names of subkeys, or an empty array if no subkeys are found. - public string[] GetSubKeyNames(RegistryHive hive, string path) - { - if (hive == 0) - throw new ArgumentException("Unknown or unsupported RegistryHive value.", nameof(hive)); - path.ThrowIfNull(nameof(path)); - - using (DisposableOptional key = OpenSubKey(hive, path)) - { - return key.Any() - ? key.First().GetSubKeyNames() - : new string[0]; - } - } - - /// - /// Retrieves the value of a specific property within a Windows Registry key and returns it as an Optional. - /// - /// The WindowsRegistryKey containing information about the registry property. - /// An Optional containing the property value, or Optional.Empty if the value is not found. - public string GetPropertyValue(WindowsRegistryKey key) - { - if (!key.IsKeyReadable()) - throw new InvalidOperationException("The Windows Registry key is not ready for reading."); - - return GetPropertyValue(key.Hive, key.Path, key.Name); - } - - /// - /// Retrieves the value of a specific property within the Windows Registry and returns it as an Optional. - /// - /// The RegistryHive where the property is located. - /// The path to the registry key containing the property. - /// The name of the property to retrieve. - /// An Optional containing the property value, or Optional.Empty if the value is not found. - public string GetPropertyValue(RegistryHive hive, string path, string name) - { - if (hive == 0) - throw new ArgumentException("Unknown or unsupported RegistryHive value.", nameof(hive)); - path.ThrowIfNull(nameof(path)); - name.ThrowIfNull(nameof(name)); - - using (DisposableOptional key = OpenSubKey(hive, path)) - { - if (!key.Any()) - return null; - - object keyValue = key.First().GetValue(name); - - if (keyValue == null) - return null; - - return keyValue.ToString(); - } - } - - /// - /// Gets a boolean value from the Windows Registry based on the specified registry path and property name. - /// If the value is not found or cannot be parsed, it returns a specified default value. - /// - /// The Registry hive where the value is located. - /// The registry path to the key containing the property. - /// The name of the property to retrieve. - /// The default value to return if the property is not found or cannot be parsed. Default is false. - /// The boolean value of the specified property or the default value if not found or cannot be parsed. - public bool GetBoolValue(RegistryHive hive, string path, string propertyName, bool defaultValue = false) - { - string value = GetPropertyValue(hive, path, propertyName); - - if (!string.IsNullOrEmpty(value)) - { - if (int.TryParse(value, out int intValue)) - return intValue == 1; - if (bool.TryParse(value, out bool boolValue)) - return boolValue; - } - - return defaultValue; - } - - // - /// Retrieves a DWORD value from the Windows Registry, with an optional default value. - /// - /// The Registry hive to access. - /// The Registry path containing the key. - /// The name of the Registry property. - /// The default value to return if the property is not found or cannot be parsed. - /// The DWORD value from the Registry, or the specified default value. - public int GetDwordValue(RegistryHive hive, string path, string propertyName, int defaultValue = 0) - { - string value = GetPropertyValue(hive, path, propertyName); - - if (int.TryParse(value, out int intValue)) - { - return intValue; - } - - return defaultValue; - } - - /// - /// Retrieves a WindowsRegistryKey object for a specific registry hive, path, and value name. - /// - /// The RegistryHive of the key. - /// The path of the key. - /// The name of the value to retrieve. - /// A WindowsRegistryKey object representing the specified registry key and value. - public WindowsRegistryKey GetWindowsRegistryKey(RegistryHive hive, string path, string name) - { - WindowsRegistryKey key = new() - { - Hive = hive, - Path = path, - Name = name - }; - - return GetWindowsRegistryKey(key); - } - - /// - /// Retrieves a WindowsRegistryKey object for a specific WindowsRegistryKey, populating its value and value kind. - /// - /// A WindowsRegistryKey object containing the hive, path, value name and more. - /// A WindowsRegistryKey object representing the specified registry key and value, with value and value kind populated. - public WindowsRegistryKey GetWindowsRegistryKey(WindowsRegistryKey key) - { - if (!key.IsKeyReadable()) - throw new InvalidOperationException("The Windows Registry key is not ready for reading."); - - using (RegistryKey baseKey = RegistryKey.OpenBaseKey(key.Hive, RegistryView.Default), subKey = baseKey.OpenSubKey(key.Path)) - { - if (subKey != null) - { - object value = subKey.GetValue(key.Name); - if (value != null) - key.Value = value.ToString(); - - if (TestValueKindExists(subKey, key.Name, out RegistryValueKind ValueKind)) - key.ValueKind = ValueKind; - } - } - - return key; - } - - /// - /// Retrieves a list of registry entries (properties) and their values under a given key path. - /// - /// The RegistryHive of the key path. - /// The path of the key from which to retrieve values. - /// A list of WindowsRegistryKey objects, each representing a value within the specified registry key path. - public List GetRegistryEntries(RegistryHive hive, string path) - { - if (hive == 0) - throw new ArgumentException("Unknown or unsupported RegistryHive value.", nameof(hive)); - path.ThrowIfNull(nameof(path)); - - List list = []; - using (RegistryKey baseKey = RegistryKey.OpenBaseKey(hive, RegistryView.Default), key = baseKey.OpenSubKey(path)) - { - if (key != null) - { - foreach (string valueName in key.GetValueNames()) - { - list.Add(new WindowsRegistryKey - { - Hive = hive, - Path = path, - Name = valueName, - Value = key.GetValue(valueName).ToString(), - ValueKind = key.GetValueKind(valueName) - } - ); - } - } - - } - return list; - } - - /// - /// Recursively retrieves registry entries under a given key path and its subkeys. - /// - /// The RegistryHive of the key path. - /// The path of the key from which to retrieve values. - /// A list of WindowsRegistryKey objects, each representing a value within the specified registry key path. - public List GetRegistryEntryiesRecursive(RegistryHive hive, string path) - { - if (hive == 0) - throw new ArgumentException("Unknown or unsupported RegistryHive value.", nameof(hive)); - path.ThrowIfNull(nameof(path)); - - List list = GetRegistryEntries(hive, path); - using (RegistryKey baseKey = RegistryKey.OpenBaseKey(hive, RegistryView.Default), key = baseKey.OpenSubKey(path)) - { - if (key != null) - { - foreach (string subPathName in key.GetSubKeyNames()) - { - string subKey = $"{path}\\{subPathName}"; - list.AddRange(GetRegistryEntryiesRecursive(hive, subKey)); - } - } - } - - return list; - } - #endregion - - #region public write methods - /// - /// Sets the value of a specific property within a registry key using individual parameters. - /// - /// The registry hive. - /// The path to the registry key containing the property. - /// The name of the property to set. - /// The value to set for the property. - /// The data type of the value to set. - public void SetRegistryValue(RegistryHive hive, string path, string name, object value, RegistryValueKind valueKind) - { - WindowsRegistryKey key = (new WindowsRegistryKey - { - Hive = hive, - Path = path, - Name = name, - Value = value.ToString(), - ValueKind = valueKind - }); - - CreateOrSetRegistryValue(key); - } - - /// - /// Sets the value of a specific property within a registry key using the provided WindowsRegistryKey object. - /// - /// The WindowsRegistryKey object containing information about the registry property. - public void SetRegistryValue(WindowsRegistryKey key) - { - CreateOrSetRegistryValue(key); - } - - /// - /// Deletes a registry key and its subkeys. - /// - /// The registry hive to open. - /// The path of the registry key to delete. - /// Set to true to ignore if the key is not found. - public void DeleteRegistryKey(RegistryHive hive, string path, bool ignoreNotFound = false) - { - try - { - using (RegistryKey key = RegistryKey.OpenBaseKey(hive, RegistryView.Default)) - { - if (key != null) - { - key.DeleteSubKeyTree(path, ignoreNotFound); - } - } - } - catch (Exception ex) - { - // Handle any exceptions according to your requirements - Console.WriteLine($"Error deleting registry key: {ex.Message}"); - throw; - } - } - #endregion - - #region public methods - /// - /// Converts a string representation of a Registry Hive to the corresponding RegistryHive enum value. - /// - /// A string representation of a Registry Hive, not case-sensitive. - /// The RegistryHive enum value corresponding to the provided string representation. - /// Thrown if the provided string does not match a valid Registry Hive. - public RegistryHive ConvertStringToRegistryHive(string hiveString) - { - switch (hiveString.ToLower()) - { - // ClassesRoot - case "hkcr": - case "hkey_classes_root": - case "classesroot": - return RegistryHive.ClassesRoot; - - // CurrentUser - case "hkcu": - case "hkey_current_user": - case "currentuser": - return RegistryHive.CurrentUser; - - // LocalMachine - case "hklm": - case "hkey_local_machine": - case "localmachine": - return RegistryHive.LocalMachine; - - // Users - case "hku": - case "hkey_users": - case "users": - return RegistryHive.Users; - - // CurrentConfig - case "hkcc": - case "hkey_current_config": - case "currentconfig": - return RegistryHive.CurrentConfig; - - default: - throw new ArgumentException("Invalid registry hive string.", nameof(hiveString)); - } - } - - /// - /// Converts a string representation of a RegistryValueKind to the corresponding RegistryValueKind enum value. - /// - /// A string representation of a RegistryValueKind, not case-sensitive. - /// The RegistryValueKind enum value corresponding to the provided string representation. - /// Thrown if the provided string does not match a valid RegistryValueKind. - public RegistryValueKind ConvertStringToRegistryValueKind(string valueType) - { - switch (valueType.ToLower()) - { - // REG_SZ - case "string": - case "reg_sz": - return RegistryValueKind.String; - - // REG_DWORD - case "dword": - case "reg_dword": - return RegistryValueKind.DWord; - - // REG_BINARY - case "binary": - case "reg_binary": - return RegistryValueKind.Binary; - - // REG_QWORD - case "qword": - case "reg_qword": - return RegistryValueKind.QWord; - - // REG_MULTI_SZ - case "multistring": - case "reg_multi_sz": - return RegistryValueKind.MultiString; - - // REG_EXPAND_SZ - case "expandstring": - case "reg_expand_sz": - return RegistryValueKind.ExpandString; - - default: - throw new ArgumentException("Invalid RegistryValueKind string representation.", nameof(valueType)); - } - } - - - /// - /// Converts a .NET data type to the corresponding RegistryValueKind. - /// - /// The .NET data type to convert. - /// The corresponding RegistryValueKind. - public RegistryValueKind ConvertTypeToRegistryValueKind(Type valueType) - { - switch (Type.GetTypeCode(valueType)) - { - case TypeCode.String: - return RegistryValueKind.String; - case TypeCode.Int32: - return RegistryValueKind.DWord; - case TypeCode.Int64: - return RegistryValueKind.QWord; - case TypeCode.Boolean: - return RegistryValueKind.DWord; - case TypeCode.Byte: - return RegistryValueKind.Binary; - /* - case TypeCode.Single: - return RegistryValueKind; - case TypeCode.Double: - return RegistryValueKind.String; - case TypeCode.DateTime: - return RegistryValueKind.String; // DateTime can be stored as a string or other types - case TypeCode.Char: - return RegistryValueKind.String; // Char can be stored as a string or other types - case TypeCode.Decimal: - return RegistryValueKind.String; // Decimal can be stored as a string or other types - */ - default: - return RegistryValueKind.String; // Default to String for unsupported types - } - } - - /// - /// Converts a RegistryValueKind enumeration value to its corresponding .NET Type. - /// - /// The RegistryValueKind value to be converted. - /// The .NET Type that corresponds to the given RegistryValueKind. - public Type ConvertRegistryValueKindToType(RegistryValueKind valueKind) - { - switch (valueKind) - { - case RegistryValueKind.String: - case RegistryValueKind.ExpandString: - return typeof(string); - case RegistryValueKind.DWord: - return typeof(int); - case RegistryValueKind.QWord: - return typeof(long); - case RegistryValueKind.Binary: - return typeof(byte[]); - case RegistryValueKind.MultiString: - return typeof(string[]); - case RegistryValueKind.Unknown: - default: - return typeof(object); - } - } - #endregion - - #region private methods - /// - /// Opens a subkey within the Windows Registry under the specified hive and key path. - /// - /// The Windows Registry hive where the subkey is located. - /// The path to the subkey to be opened. - /// - /// A disposable optional object containing the opened registry subkey if successful; - /// otherwise, it returns an empty optional object. - /// - private DisposableOptional OpenSubKey(RegistryHive hive, string keyPath) - { - switch (hive) - { - case RegistryHive.ClassesRoot: - return new DisposableOptional(Registry.ClassesRoot.OpenSubKey(keyPath)); - case RegistryHive.CurrentConfig: - return new DisposableOptional(Registry.CurrentConfig.OpenSubKey(keyPath)); - case RegistryHive.CurrentUser: - return new DisposableOptional(Registry.CurrentUser.OpenSubKey(keyPath)); - case RegistryHive.Users: - return new DisposableOptional(Registry.Users.OpenSubKey(keyPath)); - case RegistryHive.LocalMachine: - return new DisposableOptional(Registry.LocalMachine.OpenSubKey(keyPath)); - default: - throw new ArgumentOutOfRangeException(nameof(hive), hive, null); - } - } - - /// - /// Attempts to retrieve the value kind of a specific property within a registry subkey. - /// - /// The registry subkey from which to retrieve the value kind. - /// The name of the property for which to retrieve the value kind. - /// An output parameter that will contain the retrieved value kind, or RegistryValueKind.Unknown if the property or value kind is not found. - /// True if the operation is successful, otherwise false. - private bool TestValueKindExists(RegistryKey subKey, string valueName, out RegistryValueKind valueKind) - { - // Set a default value - valueKind = RegistryValueKind.Unknown; - try - { - valueKind = subKey.GetValueKind(valueName); - return true; - } - catch (Exception) - { - return false; - } - } - - /// - /// Creates or sets the value of a specific property within a registry key. - /// - /// The WindowsRegistryKey object containing information about the registry property. - /// Thrown when error by writing to the Windows Registry key. - private void CreateOrSetRegistryValue(WindowsRegistryKey key) - { - try - { - if (!key.IsKeyWritable()) - throw new ArgumentNullException("The Windows Registry key is not ready for writing."); - - using RegistryKey baseKey = RegistryKey.OpenBaseKey(key.Hive, RegistryView.Default); - RegistryKey registryKey = baseKey.OpenSubKey(key.Path, true); - - if (registryKey == null) - { - // The specified subkey doesn't exist, so create it. - using RegistryKey newKey = baseKey.CreateSubKey(key.Path); - newKey.SetValue(key.Name, key.Value, key.ValueKind); - } - else - { - registryKey.SetValue(key.Name, key.Value, key.ValueKind); - } - } - catch (Exception ex) - { - throw new InvalidOperationException("Error writing to the Windows Registry key.", ex); - } - } - - #endregion - } -} \ No newline at end of file diff --git a/mRemoteNG/Tools/WindowsRegistry/WindowsRegistryAdvanced.cs b/mRemoteNG/Tools/WindowsRegistry/WindowsRegistryAdvanced.cs deleted file mode 100644 index d019d0a3..00000000 --- a/mRemoteNG/Tools/WindowsRegistry/WindowsRegistryAdvanced.cs +++ /dev/null @@ -1,118 +0,0 @@ -using Microsoft.Win32; -using mRemoteNG.App.Info; -using mRemoteNG.Security.SymmetricEncryption; -using System.Runtime.Versioning; - -namespace mRemoteNG.Tools.WindowsRegistry -{ - [SupportedOSPlatform("windows")] - /// - /// Extends the functionality of interacting with the Windows Registry, building upon the base WindowsRegistry class. - /// - public class WindowsRegistryAdvanced : WindowsRegistry , IRegistryAdvanced, IRegistryAdvancedRead - { - #region dword methods - /// - /// Retrieves a DWORD (32-bit integer) value from the Windows Registry based on the specified registry information. - /// - /// The registry hive. - /// The path to the registry key. - /// The name of the registry property. - /// Optional default value to be used if the registry key is not present (default is null). - /// A WindowsRegistryKeyInteger instance representing the retrieved DWORD value. - public WindowsRegistryKeyInteger GetInteger(RegistryHive hive, string path, string propertyName, int? defaultValue = null) - { - // Retrieve the Windows Registry key - WindowsRegistryKey key = GetWindowsRegistryKey(hive, path, propertyName); - - // Create a WindowsRegistryKeyInteger instance and initialize it from the retrieved key - WindowsRegistryKeyInteger IntKey = new(); - IntKey.ConvertFromWindowsRegistryKey(key); - - return IntKey; - } - #endregion - - #region string methods - /// - /// Retrieves a string value from the Windows Registry based on the specified registry information. - /// - /// The registry hive. - /// The path to the registry key. - /// The name of the registry property. - /// Optional default value to be used if the registry key is not present (default is null). - /// A WindowsRegistryKeyString instance representing the retrieved string value. - public WindowsRegistryKeyString GetString(RegistryHive hive, string path, string propertyName, string defaultValue = null) - { - // Retrieve the Windows Registry key - WindowsRegistryKey key = GetWindowsRegistryKey(hive, path, propertyName); - - // Create a WindowsRegistryKeyString instance and initialize it from the retrieved key - WindowsRegistryKeyString StrKey = new(); - StrKey.ConvertFromWindowsRegistryKey(key, defaultValue); - - return StrKey; - } - - /// - /// Retrieves a string value from the Windows Registry based on the specified registry information and validates it against a set of allowed values. - /// - /// The registry hive. - /// The path to the registry key. - /// The name of the registry property. - /// An array of valid values against which the retrieved string is validated. - /// Optional parameter indicating whether the validation is case-sensitive (default is false). - /// A WindowsRegistryKeyString instance representing the retrieved and validated string value. - public WindowsRegistryKeyString GetStringValidated(RegistryHive hive, string path, string propertyName, string[] allowedValues, bool caseSensitive = false, string defaultValue = null) - { - // Retrieve the Windows Registry key - WindowsRegistryKey key = GetWindowsRegistryKey(hive, path, propertyName); - - // Create a WindowsRegistryKeyString instance and initialize it from the retrieved key - WindowsRegistryKeyString StrKey = new() - { - AllowedValues = allowedValues, - IsCaseSensitiveValidation = caseSensitive - }; - StrKey.ConvertFromWindowsRegistryKey(key, defaultValue); - - return StrKey; - } - - /*public WindowsRegistryKeySecureString GetSecureString(RegistryHive hive, string path, string propertyName) - { - // Retrieve the Windows Registry key, the key should be encrypted - var key = GetWindowsRegistryKey(hive, path, propertyName); - - // Create a WindowsRegistryKeyBoolean instance and initialize it from the retrieved key - WindowsRegistryKeySecureString secureKey = new (); // class not exsists - secureKey.ConvertFromWindowsRegistryKey(key); // no default possible! - return secureKey - }*/ - - #endregion - - #region bool methods - /// - /// Retrieves a boolean value from the Windows Registry based on the specified registry information. - /// - /// The registry hive. - /// The path to the registry key. - /// The name of the registry property. - /// An optional default value to use if the registry key is not present or if the value is not a valid boolean. - /// A WindowsRegistryKeyBoolean instance representing the retrieved boolean value. - public WindowsRegistryKeyBoolean GetBoolean(RegistryHive hive, string path, string propertyName, bool? defaultValue = null) - { - // Retrieve the Windows Registry key - WindowsRegistryKey key = GetWindowsRegistryKey(hive, path, propertyName); - - // Create a WindowsRegistryKeyBoolean instance and initialize it from the retrieved key - WindowsRegistryKeyBoolean boolKey = new (); - boolKey.ConvertFromWindowsRegistryKey(key, defaultValue); - - return boolKey; - } - - #endregion - } -} \ No newline at end of file diff --git a/mRemoteNG/Tools/WindowsRegistry/WindowsRegistryKey.cs b/mRemoteNG/Tools/WindowsRegistry/WindowsRegistryKey.cs deleted file mode 100644 index f1308269..00000000 --- a/mRemoteNG/Tools/WindowsRegistry/WindowsRegistryKey.cs +++ /dev/null @@ -1,116 +0,0 @@ -using System; -using System.Runtime.Versioning; -using Microsoft.Win32; - - -namespace mRemoteNG.Tools.WindowsRegistry -{ - /// - /// Represents a Windows Registry key with a default string value, providing a flexible abstraction for registry operations. - /// This class can be extended by inherited classes to customize behavior for specific data types. - /// - [SupportedOSPlatform("windows")] - public class WindowsRegistryKey - { - #region properties - - #region Property registryHive - public RegistryHive Hive - { - get { return _Hive; } - set - { - if (value == 0) - throw new ArgumentNullException("RegistryHive unknown."); - - _Hive = value; - } - } - private RegistryHive _Hive { get; set; } - #endregion - - #region Property path - public string Path - { - get { return _Path; } - set - { - value.ThrowIfNull(nameof(value)); - _Path = value; - } - } - private string _Path { get; set; } - #endregion - - #region Property name - public string Name - { - get { return _Name; } - set - { - value.ThrowIfNull(nameof(value)); - _Name = value; - } - } - private string _Name { get; set; } - #endregion - - #region Property value - public virtual string Value - { - get => _value; - set - { - _value = value; - UpdateIsProvidedState(); - } - } - private string _value; - #endregion - - public RegistryValueKind ValueKind { get; set; } = RegistryValueKind.Unknown; - - public bool IsKeyPresent { get; set; } = false; - #endregion - - #region public methods - /// - /// Checks if the Windows Registry key is ready for reading by ensuring that the hive, - /// path, and name properties are set. - /// - /// True if the key is ready for reading, otherwise false. - public bool IsKeyReadable() { - return (IsHiveSet() && IsPathSet() && IsNameSet()); - } - - /// - /// Checks if the Windows Registry key is ready for a write operation. - /// The key is considered write-ready if none of the following conditions are met: - /// - The hive is set - /// - The registry value type is set - /// - The key path is set - /// - The value name is set - /// - /// Returns true if the key is write-ready, otherwise false. - public bool IsKeyWritable() { - return (IsHiveSet() && IsValueKindSet() && IsPathSet() && IsNameSet()); - } - #endregion - - #region protected methods - protected void UpdateIsProvidedState() - { - // Key is present when RegistryKey value is not null - IsKeyPresent = Value != null; - } - - protected bool IsHiveSet() => Hive != 0; - - protected bool IsValueKindSet() => ValueKind != 0; - - protected bool IsPathSet() => Path != null; - - protected bool IsNameSet() => Name != null; - #endregion - } -} diff --git a/mRemoteNG/Tools/WindowsRegistry/WindowsRegistryKeyBoolean.cs b/mRemoteNG/Tools/WindowsRegistry/WindowsRegistryKeyBoolean.cs deleted file mode 100644 index aad4265d..00000000 --- a/mRemoteNG/Tools/WindowsRegistry/WindowsRegistryKeyBoolean.cs +++ /dev/null @@ -1,75 +0,0 @@ -using System; -using System.Runtime.Versioning; - -namespace mRemoteNG.Tools.WindowsRegistry -{ - /// - /// Represents a boolean Windows Registry key, extending the base class WindowsRegistryKey. - /// - [SupportedOSPlatform("windows")] - public class WindowsRegistryKeyBoolean : WindowsRegistryKey - { - /// - /// Gets or sets the default boolean value for a Windows Registry key. - /// - /// - /// The default value is initially set to `false`. - /// - public bool DefaultValue { get; private set; } = false; - - /// - /// Gets or sets the boolean value for a Windows Registry key. - /// - public new bool Value - { - get => BoolValue; - private set - { - BoolValue = value; - UpdateIsProvidedState(); - } - } - private bool BoolValue; - - /// - /// Converts and initializes a WindowsRegistryKeyBoolean from a base WindowsRegistryKey, with an optional default value. - /// - /// The base WindowsRegistryKey to convert from. - /// Optional default value to set for the WindowsRegistryKeyBoolean. - public void ConvertFromWindowsRegistryKey(WindowsRegistryKey baseKey, bool? defaultValue = null) - { - SetDefaultValue(defaultValue); - FromBaseKey(baseKey); - } - - private void FromBaseKey(WindowsRegistryKey baseKey) - { - if (baseKey == null) - throw new ArgumentNullException(nameof(baseKey)); - - Hive = baseKey.Hive; - Path = baseKey.Path; - Name = baseKey.Name; - ValueKind = baseKey.ValueKind; - IsKeyPresent = baseKey.IsKeyPresent; - - ConvertToBool(baseKey.Value); - } - - private void SetDefaultValue(bool? defaultValue) - { - if (defaultValue.HasValue) - DefaultValue = (bool)defaultValue; - } - - private void ConvertToBool(string newValue) - { - if (IsKeyPresent && bool.TryParse(newValue, out bool boolValue)) - BoolValue = boolValue; - else if (IsKeyPresent && int.TryParse(newValue, out int intValue)) - BoolValue = intValue == 1; - else - BoolValue = DefaultValue; - } - } -} diff --git a/mRemoteNG/Tools/WindowsRegistry/WindowsRegistryKeyInteger.cs b/mRemoteNG/Tools/WindowsRegistry/WindowsRegistryKeyInteger.cs deleted file mode 100644 index aeb6818d..00000000 --- a/mRemoteNG/Tools/WindowsRegistry/WindowsRegistryKeyInteger.cs +++ /dev/null @@ -1,78 +0,0 @@ -using Microsoft.Win32; -using System; -using System.Runtime.Versioning; - -namespace mRemoteNG.Tools.WindowsRegistry -{ - /// - /// Represents a integer Windows Registry key, extending the base class WindowsRegistryKey. - /// - [SupportedOSPlatform("windows")] - public class WindowsRegistryKeyInteger : WindowsRegistryKey - { - /// - /// Gets or sets the default integer value for a Windows Registry key. - /// - /// - /// The default value is initially set to `-1`. - /// - public int DefaultValue { get; private set; } = -1; - - - /// - /// Gets or sets the integer value for a Windows Registry key. - /// - public new int Value - { - get => IntegerValue; - private set - { - IntegerValue = value; - UpdateIsProvidedState(); - } - } - private int IntegerValue; - - /// - /// Converts and initializes a WindowsRegistryKeyInteger from a base WindowsRegistryKey, with an optional default value. - /// - /// The base WindowsRegistryKey to convert from. - /// Optional default value to set for the WindowsRegistryKeyBoolean. - public void ConvertFromWindowsRegistryKey(WindowsRegistryKey baseKey, int? defaultValue = null) - { - SetDefaultValue(defaultValue); - FromBaseKey(baseKey); - } - - private void FromBaseKey(WindowsRegistryKey baseKey) - { - if (baseKey == null) - throw new ArgumentNullException(nameof(baseKey)); - - Hive = baseKey.Hive; - Path = baseKey.Path; - Name = baseKey.Name; - ValueKind = baseKey.ValueKind; - IsKeyPresent = baseKey.IsKeyPresent; - - ConvertToInteger(baseKey.Value); - } - - private void SetDefaultValue (int? defaultValue) - { - if (defaultValue.HasValue) - DefaultValue = (int)defaultValue; - } - - private void ConvertToInteger(string newValue) - { - if (ValueKind != RegistryValueKind.DWord) - IsKeyPresent = false; - - if (IsKeyPresent && int.TryParse(newValue.ToString(), out int intValue)) - IntegerValue = intValue; - else - IntegerValue = DefaultValue; - } - } -} diff --git a/mRemoteNG/Tools/WindowsRegistry/WindowsRegistryKeyString.cs b/mRemoteNG/Tools/WindowsRegistry/WindowsRegistryKeyString.cs deleted file mode 100644 index d616754d..00000000 --- a/mRemoteNG/Tools/WindowsRegistry/WindowsRegistryKeyString.cs +++ /dev/null @@ -1,121 +0,0 @@ -using Microsoft.Win32; -using System; -using System.Linq; -using System.Runtime.Versioning; - -namespace mRemoteNG.Tools.WindowsRegistry -{ - /// - /// Represents a string Windows Registry key, extending the base class WindowsRegistryKey, can be evaluated with a value set. - /// - [SupportedOSPlatform("windows")] - public class WindowsRegistryKeyString : WindowsRegistryKey - { - /// - /// Gets or sets the default integer value for a Windows Registry key. - /// - public string DefaultValue { get; private set; } - - /// - /// Gets or sets an array of allowed values for validation. - /// - public string[] AllowedValues { get; set; } - - /// - /// Gets or sets a boolean flag indicating whether validation is case-sensitive. - /// - public bool IsCaseSensitiveValidation { get; set; } = false; - - /// - /// Gets a boolean indicating whether the value is valid based on the validation rules. - /// - public bool IsValid { get; private set; } = false; - - - public new string Value - { - get => StringValue; - private set - { - StringValue = value; - UpdateIsProvidedState(); - Validate(); - } - } - private string StringValue; - - /// - /// Converts and initializes a WindowsRegistryKeyString from a base WindowsRegistryKey, with an optional default value. - /// - /// The base WindowsRegistryKey to convert from. - /// Optional default value to set for the WindowsRegistryKeyBoolean. - public void ConvertFromWindowsRegistryKey(WindowsRegistryKey baseKey, string defaultValue = null) - { - SetDefaultValue(defaultValue); - FromBaseKey(baseKey); - Validate(); - } - - private void FromBaseKey(WindowsRegistryKey baseKey) - { - if (baseKey == null) - throw new ArgumentNullException(nameof(baseKey)); - - Hive = baseKey.Hive; - Path = baseKey.Path; - Name = baseKey.Name; - ValueKind = baseKey.ValueKind; - IsKeyPresent = baseKey.IsKeyPresent; - - ConvertToString(baseKey.Value); - } - - private void SetDefaultValue (string defaultValue) - { - DefaultValue = defaultValue; - } - private void ConvertToString(string newValue) - { - if (IsKeyPresent && newValue != null) - StringValue = newValue; - else - StringValue = DefaultValue; - } - - /// - /// Validates a Windows Registry key value against a set of allowed values, considering case sensitivity. - /// - /// Array of allowed values. - /// Optional parameter to specify case sensitivity in validation. - public void Validate(string[] allowedValues = null, bool? caseSensitive = null) - { - // Key must be present to evaluate - if (!IsKeyPresent) - return; - - if (caseSensitive.HasValue) - IsCaseSensitiveValidation = caseSensitive.Value; - - if (allowedValues != null && allowedValues.Length >= 1) - AllowedValues = allowedValues; - - // AllowedValues array cannot be null or empty. - if (AllowedValues == null || AllowedValues.Length == 0 || !IsKeyPresent) - return; - - if (IsKeyPresent && AllowedValues.Any(v => - IsCaseSensitiveValidation ? v == Value : v.Equals(Value, StringComparison.OrdinalIgnoreCase))) - { - // Set to true when the value is found in the valid values - IsValid = true; - } - else - { - // Set to false when the value is not found in the valid values - IsValid = false; - StringValue = DefaultValue; - } - } - - } -} diff --git a/mRemoteNGTests/Tools/Registry/WindowsRegistryAdvancedTests.cs b/mRemoteNGTests/Tools/Registry/WindowsRegistryAdvancedTests.cs deleted file mode 100644 index 09382235..00000000 --- a/mRemoteNGTests/Tools/Registry/WindowsRegistryAdvancedTests.cs +++ /dev/null @@ -1,188 +0,0 @@ -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)); - }); - } - - [Test] - public void GetDwordValue_returnIntegerValue() - { - int value = GetDwordValue(_TestHive, _TestRootKey, "TestInteger"); - Assert.That(value, Is.EqualTo(4711)); - } - #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 - } -} diff --git a/mRemoteNGTests/Tools/Registry/WindowsRegistryKeyTests.cs b/mRemoteNGTests/Tools/Registry/WindowsRegistryKeyTests.cs deleted file mode 100644 index dad5b54d..00000000 --- a/mRemoteNGTests/Tools/Registry/WindowsRegistryKeyTests.cs +++ /dev/null @@ -1,386 +0,0 @@ -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() - { - 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() - { - 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() - { - AllowedValues = new[] { "BiG BAng", "Big Bang Theory", "The Big Bang Theory" }, - 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() - { - AllowedValues = new[] { "Big Bang", "Big Bang Theory", "The Big Bang Theory" }, - 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() - { - 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() - { - 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 - } -}