public void WhenParentAndChildNonDefault_ThenOverlayByReturnsCorrectValues()
        {
            using (var key = this.hkcu.CreateSubKey(TestKeyPath))
            {
                var parent = RegistrySecureStringSetting.FromKey(
                    "test",
                    "title",
                    "description",
                    "category",
                    key,
                    DataProtectionScope.CurrentUser);
                parent.Value = "red";
                Assert.IsFalse(parent.IsDefault);

                var child = RegistrySecureStringSetting.FromKey(
                    "test",
                    "title",
                    "description",
                    "category",
                    key,
                    DataProtectionScope.CurrentUser);
                child.Value = "green";
                Assert.IsFalse(child.IsDefault);

                var effective = parent.OverlayBy(child);
                Assert.AreNotSame(effective, parent);
                Assert.AreNotSame(effective, child);

                Assert.AreEqual("green", ((SecureString)effective.Value).AsClearText());
                Assert.AreEqual("red", ((SecureString)effective.DefaultValue).AsClearText());
                Assert.IsFalse(effective.IsDefault);
            }
        }
        public void WhenSettingIsNull_ThenSaveResetsRegistry()
        {
            using (var key = this.hkcu.CreateSubKey(TestKeyPath))
            {
                var setting = RegistrySecureStringSetting.FromKey(
                    "test",
                    "title",
                    "description",
                    "category",
                    key,
                    DataProtectionScope.CurrentUser);

                setting.Value = SecureStringExtensions.FromClearText("red");
                setting.Save(key);

                Assert.IsNotNull(key.GetValue("test"));

                // Now write again.

                setting = RegistrySecureStringSetting.FromKey(
                    "test",
                    "title",
                    "description",
                    "category",
                    key,
                    DataProtectionScope.CurrentUser);

                setting.Value = null;
                setting.Save(key);

                Assert.IsNull(key.GetValue("test"));
            }
        }
        public void WhenParentAndChildDefault_ThenOverlayByReturnsCorrectValues()
        {
            using (var key = this.hkcu.CreateSubKey(TestKeyPath))
            {
                var parent = RegistrySecureStringSetting.FromKey(
                    "test",
                    "title",
                    "description",
                    "category",
                    key,
                    DataProtectionScope.CurrentUser);
                Assert.IsTrue(parent.IsDefault);

                var child = RegistrySecureStringSetting.FromKey(
                    "test",
                    "title",
                    "description",
                    "category",
                    key,
                    DataProtectionScope.CurrentUser);

                var effective = parent.OverlayBy(child);
                Assert.AreNotSame(effective, parent);
                Assert.AreNotSame(effective, child);

                Assert.IsNull(effective.DefaultValue);
                Assert.IsNull(effective.Value);
                Assert.IsTrue(effective.IsDefault);
            }
        }
 public static AuthSettings FromKey(RegistryKey registryKey)
 {
     return(new AuthSettings()
     {
         Credentials = RegistrySecureStringSetting.FromKey(
             "Credentials",
             "OAuth credentials",
             null,
             null,
             registryKey,
             DataProtectionScope.CurrentUser)
     });
 }
        public void WhenValueIsOfWrongType_ThenSetValueRaisesInvalidCastException()
        {
            using (var key = this.hkcu.CreateSubKey(TestKeyPath))
            {
                var setting = RegistrySecureStringSetting.FromKey(
                    "test",
                    "title",
                    "description",
                    "category",
                    key,
                    DataProtectionScope.CurrentUser);

                Assert.Throws <InvalidCastException>(() => setting.Value = 1);
            }
        }
        public void WhenValueIsString_ThenSetValueParsesValue()
        {
            using (var key = this.hkcu.CreateSubKey(TestKeyPath))
            {
                var setting = RegistrySecureStringSetting.FromKey(
                    "test",
                    "title",
                    "description",
                    "category",
                    key,
                    DataProtectionScope.CurrentUser);

                setting.Value = "secret";

                Assert.AreEqual("secret", setting.ClearTextValue);
            }
        }
        public void WhenParentIsNonDefaultAndChildSetToOriginalDefault_ThenIsDefaultReturnsFalse()
        {
            using (var key = this.hkcu.CreateSubKey(TestKeyPath))
            {
                var parent = RegistrySecureStringSetting.FromKey(
                    "test",
                    "title",
                    "description",
                    "category",
                    key,
                    DataProtectionScope.CurrentUser);
                parent.Value = "red";
                Assert.IsFalse(parent.IsDefault);

                var intermediate = RegistrySecureStringSetting.FromKey(
                    "test",
                    "title",
                    "description",
                    "category",
                    key,
                    DataProtectionScope.CurrentUser);
                Assert.IsTrue(intermediate.IsDefault);

                var child = RegistrySecureStringSetting.FromKey(
                    "test",
                    "title",
                    "description",
                    "category",
                    key,
                    DataProtectionScope.CurrentUser);

                var effective = parent
                                .OverlayBy(intermediate)
                                .OverlayBy(child);
                Assert.AreNotSame(effective, parent);
                Assert.AreNotSame(effective, intermediate);
                Assert.AreNotSame(effective, child);

                effective.Value = "black";

                Assert.AreEqual("black", ((SecureString)effective.Value).AsClearText());
                Assert.AreEqual("red", ((SecureString)effective.DefaultValue).AsClearText());
                Assert.IsFalse(effective.IsDefault);
            }
        }
        public void WhenValueDiffersFromDefault_ThenSetValueSucceedsAndSettingIsDirty()
        {
            using (var key = this.hkcu.CreateSubKey(TestKeyPath))
            {
                var setting = RegistrySecureStringSetting.FromKey(
                    "test",
                    "title",
                    "description",
                    "category",
                    key,
                    DataProtectionScope.CurrentUser);

                setting.Value = SecureStringExtensions.FromClearText("yellow");

                Assert.IsFalse(setting.IsDefault);
                Assert.IsTrue(setting.IsDirty);
            }
        }
        public void WhenValueAndDefaultAreNull_ThenSetValueSucceedsAndSettingIsNotDirty()
        {
            using (var key = this.hkcu.CreateSubKey(TestKeyPath))
            {
                var setting = RegistrySecureStringSetting.FromKey(
                    "test",
                    "title",
                    "description",
                    "category",
                    key,
                    DataProtectionScope.CurrentUser);

                setting.Value = null;

                Assert.IsTrue(setting.IsDefault);
                Assert.IsFalse(setting.IsDirty);
            }
        }
        public void WhenValueIsNull_ThenSetValueResetsToDefault()
        {
            using (var key = this.hkcu.CreateSubKey(TestKeyPath))
            {
                var setting = RegistrySecureStringSetting.FromKey(
                    "test",
                    "title",
                    "description",
                    "category",
                    key,
                    DataProtectionScope.CurrentUser);

                setting.Value = SecureStringExtensions.FromClearText("blue");
                setting.Value = null;

                Assert.IsNull(setting.Value);
                Assert.IsTrue(setting.IsDefault);
            }
        }
        public void WhenRegistryKeyIsNull_ThenFromKeyUsesDefaults()
        {
            using (var key = this.hkcu.CreateSubKey(TestKeyPath))
            {
                var setting = RegistrySecureStringSetting.FromKey(
                    "test",
                    "title",
                    "description",
                    "category",
                    null,
                    DataProtectionScope.CurrentUser);

                Assert.AreEqual("test", setting.Key);
                Assert.AreEqual("title", setting.Title);
                Assert.AreEqual("description", setting.Description);
                Assert.AreEqual("category", setting.Category);
                Assert.IsNull(setting.Value);
                Assert.IsTrue(setting.IsDefault);
                Assert.IsFalse(setting.IsDirty);
            }
        }
        public void WhenRegistryValueContainsGibberish_ThenFromKeyUsesDefaults()
        {
            using (var key = this.hkcu.CreateSubKey(TestKeyPath))
            {
                key.SetValue("test", Encoding.ASCII.GetBytes("gibberish"), RegistryValueKind.Binary);

                var setting = RegistrySecureStringSetting.FromKey(
                    "test",
                    "title",
                    "description",
                    "category",
                    key,
                    DataProtectionScope.CurrentUser);

                Assert.AreEqual("test", setting.Key);
                Assert.AreEqual("title", setting.Title);
                Assert.AreEqual("description", setting.Description);
                Assert.AreEqual("category", setting.Category);
                Assert.IsNull(setting.Value);
                Assert.IsNull(setting.ClearTextValue);
                Assert.IsTrue(setting.IsDefault);
                Assert.IsFalse(setting.IsDirty);
            }
        }
        public void WhenRegistryValueExists_ThenFromKeyUsesValue()
        {
            using (var key = this.hkcu.CreateSubKey(TestKeyPath))
            {
                var setting = RegistrySecureStringSetting.FromKey(
                    "test",
                    "title",
                    "description",
                    "category",
                    key,
                    DataProtectionScope.CurrentUser);

                setting.Value = SecureStringExtensions.FromClearText("red");
                setting.Save(key);

                Assert.IsNotNull(key.GetValue("test"));

                // Now read again.

                setting = RegistrySecureStringSetting.FromKey(
                    "test",
                    "title",
                    "description",
                    "category",
                    key,
                    DataProtectionScope.CurrentUser);

                Assert.AreEqual("test", setting.Key);
                Assert.AreEqual("title", setting.Title);
                Assert.AreEqual("description", setting.Description);
                Assert.AreEqual("category", setting.Category);
                Assert.AreEqual("red", setting.ClearTextValue);
                Assert.IsFalse(setting.IsDefault);
                Assert.IsFalse(setting.IsDirty);
            }
        }
        protected void InitializeFromKey(RegistryKey key)
        {
            //
            // RDP Settings.
            //
            this.RdpUsername = RegistryStringSetting.FromKey(
                "Username",
                "Username",
                "Windows logon username",
                Categories.RdpCredentials,
                null,
                key,
                _ => true);
            this.RdpPassword = RegistrySecureStringSetting.FromKey(
                "Password",
                "Password",
                "Windows logon password",
                Categories.RdpCredentials,
                key,
                DataProtectionScope.CurrentUser);
            this.RdpDomain = RegistryStringSetting.FromKey(
                "Domain",
                "Domain",
                "Windows logon domain",
                Categories.RdpCredentials,
                null,
                key,
                _ => true);
            this.RdpConnectionBar = RegistryEnumSetting <RdpConnectionBarState> .FromKey(
                "ConnectionBar",
                "Show connection bar",
                "Show connection bar in full-screen mode",
                Categories.RdpDisplay,
                RdpConnectionBarState._Default,
                key);

            this.RdpDesktopSize = RegistryEnumSetting <RdpDesktopSize> .FromKey(
                "DesktopSize",
                "Desktop size",
                "Size of remote desktop",
                Categories.RdpDisplay,
                ConnectionSettings.RdpDesktopSize._Default,
                key);

            this.RdpAuthenticationLevel = RegistryEnumSetting <RdpAuthenticationLevel> .FromKey(
                "AuthenticationLevel",
                "Server authentication",
                "Require server authentication when connecting",
                Categories.RdpConnection,
                ConnectionSettings.RdpAuthenticationLevel._Default,
                key);

            this.RdpColorDepth = RegistryEnumSetting <RdpColorDepth> .FromKey(
                "ColorDepth",
                "Color depth",
                "Color depth of remote desktop",
                Categories.RdpDisplay,
                ConnectionSettings.RdpColorDepth._Default,
                key);

            this.RdpAudioMode = RegistryEnumSetting <RdpAudioMode> .FromKey(
                "AudioMode",
                "Audio mode",
                "Redirect audio when playing on server",
                Categories.RdpResources,
                ConnectionSettings.RdpAudioMode._Default,
                key);

            this.RdpRedirectClipboard = RegistryEnumSetting <RdpRedirectClipboard> .FromKey(
                "RedirectClipboard",
                "Redirect clipboard",
                "Allow clipboard contents to be shared with remote desktop",
                Categories.RdpResources,
                ConnectionSettings.RdpRedirectClipboard._Default,
                key);

            this.RdpUserAuthenticationBehavior = RegistryEnumSetting <RdpUserAuthenticationBehavior> .FromKey(
                "RdpUserAuthenticationBehavior",
                null, // Hidden.
                null, // Hidden.
                null, // Hidden.
                ConnectionSettings.RdpUserAuthenticationBehavior._Default,
                key);

            this.RdpBitmapPersistence = RegistryEnumSetting <RdpBitmapPersistence> .FromKey(
                "BitmapPersistence",
                "Bitmap caching",
                "Use persistent bitmap cache",
                Categories.RdpResources,
                ConnectionSettings.RdpBitmapPersistence._Default,
                key);

            this.RdpConnectionTimeout = RegistryDwordSetting.FromKey(
                "ConnectionTimeout",
                null, // Hidden.
                null, // Hidden.
                null, // Hidden.
                30,
                key,
                0, 300);
            this.RdpCredentialGenerationBehavior = RegistryEnumSetting <RdpCredentialGenerationBehavior> .FromKey(
                "CredentialGenerationBehavior",
                null, // Hidden.
                null, // Hidden.
                null, // Hidden.
                ConnectionSettings.RdpCredentialGenerationBehavior._Default,
                key);

            this.RdpPort = RegistryDwordSetting.FromKey(
                "RdpPort",
                "Server port",
                "Server port",
                Categories.RdpConnection,
                3389,
                key,
                1,
                ushort.MaxValue);

            //
            // SSH Settings.
            //
            this.SshPort = RegistryDwordSetting.FromKey(
                "SshPort",
                "Server port",
                "Server port",
                Categories.SshConnection,
                22,
                key,
                1,
                ushort.MaxValue);
            this.SshUsername = RegistryStringSetting.FromKey(
                "SshUsername",
                "Username",
                "Preferred Linux username (only applicable if OS Login is disabled)",
                Categories.SshCredentials,
                null,
                key,
                username => string.IsNullOrEmpty(username) ||
                AuthorizedKey.IsValidUsername(username));
            this.SshConnectionTimeout = RegistryDwordSetting.FromKey(
                "SshConnectionTimeout",
                null, // Hidden.
                null, // Hidden.
                null, // Hidden.
                30,
                key,
                0, 300);

            Debug.Assert(this.Settings.All(s => s != null));
        }
 public static ApplicationSettings FromKey(RegistryKey registryKey)
 {
     return(new ApplicationSettings()
     {
         IsMainWindowMaximized = RegistryBoolSetting.FromKey(
             "IsMainWindowMaximized",
             "IsMainWindowMaximized",
             null,
             null,
             false,
             registryKey),
         MainWindowHeight = RegistryDwordSetting.FromKey(
             "MainWindowHeight",
             "MainWindowHeight",
             null,
             null,
             0,
             registryKey,
             0,
             ushort.MaxValue),
         MainWindowWidth = RegistryDwordSetting.FromKey(
             "WindowWidth",
             "WindowWidth",
             null,
             null,
             0,
             registryKey,
             0,
             ushort.MaxValue),
         IsUpdateCheckEnabled = RegistryBoolSetting.FromKey(
             "IsUpdateCheckEnabled",
             "IsUpdateCheckEnabled",
             null,
             null,
             true,
             registryKey),
         LastUpdateCheck = RegistryQwordSetting.FromKey(
             "LastUpdateCheck",
             "LastUpdateCheck",
             null,
             null,
             0,
             registryKey,
             0,
             long.MaxValue),
         IsPreviewFeatureSetEnabled = RegistryBoolSetting.FromKey(
             "IsPreviewFeatureSetEnabled",
             "IsPreviewFeatureSetEnabled",
             null,
             null,
             false,
             registryKey),
         ProxyUrl = RegistryStringSetting.FromKey(
             "ProxyUrl",
             "ProxyUrl",
             null,
             null,
             null,
             registryKey,
             url => url == null || Uri.TryCreate(url, UriKind.Absolute, out Uri _)),
         ProxyPacUrl = RegistryStringSetting.FromKey(
             "ProxyPacUrl",
             "ProxyPacUrl",
             null,
             null,
             null,
             registryKey,
             url => url == null || Uri.TryCreate(url, UriKind.Absolute, out Uri _)),
         ProxyUsername = RegistryStringSetting.FromKey(
             "ProxyUsername",
             "ProxyUsername",
             null,
             null,
             null,
             registryKey,
             _ => true),
         ProxyPassword = RegistrySecureStringSetting.FromKey(
             "ProxyPassword",
             "ProxyPassword",
             null,
             null,
             registryKey,
             DataProtectionScope.CurrentUser),
         IsDeviceCertificateAuthenticationEnabled = RegistryBoolSetting.FromKey(
             "IsDeviceCertificateAuthenticationEnabled",
             "IsDeviceCertificateAuthenticationEnabled",
             null,
             null,
             false,
             registryKey),
         FullScreenDevices = RegistryStringSetting.FromKey(
             "FullScreenDevices",
             "FullScreenDevices",
             null,
             null,
             null,
             registryKey,
             _ => true),
         IncludeOperatingSystems = RegistryEnumSetting <OperatingSystems> .FromKey(
             "IncludeOperatingSystems",
             "IncludeOperatingSystems",
             null,
             null,
             OperatingSystems.Windows,
             registryKey)
     });
Example #16
0
        protected void InitializeFromKey(RegistryKey key)
        {
            this.Username = RegistryStringSetting.FromKey(
                "Username",
                "Username",
                "Windows logon username",
                "Credentials",
                null,
                key,
                _ => true);
            this.Password = RegistrySecureStringSetting.FromKey(
                "Password",
                "Password",
                "Windows logon password",
                "Credentials",
                key,
                DataProtectionScope.CurrentUser);
            this.Domain = RegistryStringSetting.FromKey(
                "Domain",
                "Domain",
                "Windows logon domain",
                "Credentials",
                null,
                key,
                _ => true);
            this.ConnectionBar = RegistryEnumSetting <RdpConnectionBarState> .FromKey(
                "ConnectionBar",
                "Show connection bar",
                "Show connection bar in full-screen mode",
                "Display",
                RdpConnectionBarState._Default,
                key);

            this.DesktopSize = RegistryEnumSetting <RdpDesktopSize> .FromKey(
                "DesktopSize",
                "Desktop size",
                "Size of remote desktop",
                "Display",
                RdpDesktopSize._Default,
                key);

            this.AuthenticationLevel = RegistryEnumSetting <RdpAuthenticationLevel> .FromKey(
                "AuthenticationLevel",
                "Server authentication",
                "Require server authentication when connecting",
                "Connection",
                RdpAuthenticationLevel._Default,
                key);

            this.ColorDepth = RegistryEnumSetting <RdpColorDepth> .FromKey(
                "ColorDepth",
                "Color depth",
                "Color depth of remote desktop",
                "Display",
                RdpColorDepth._Default,
                key);

            this.AudioMode = RegistryEnumSetting <RdpAudioMode> .FromKey(
                "AudioMode",
                "Audio mode",
                "Redirect audio when playing on server",
                "Local resources",
                RdpAudioMode._Default,
                key);

            this.RedirectClipboard = RegistryEnumSetting <RdpRedirectClipboard> .FromKey(
                "RedirectClipboard",
                "Redirect clipboard",
                "Allow clipboard contents to be shared with remote desktop",
                "Local resources",
                RdpRedirectClipboard._Default,
                key);

            this.UserAuthenticationBehavior = RegistryEnumSetting <RdpUserAuthenticationBehavior> .FromKey(
                "RdpUserAuthenticationBehavior",
                null, // Hidden.
                null, // Hidden.
                null, // Hidden.
                RdpUserAuthenticationBehavior._Default,
                key);

            this.BitmapPersistence = RegistryEnumSetting <RdpBitmapPersistence> .FromKey(
                "BitmapPersistence",
                "Bitmap caching",
                "Use persistent bitmap cache",
                "Performance",
                RdpBitmapPersistence._Default,
                key);

            this.ConnectionTimeout = RegistryDwordSetting.FromKey(
                "ConnectionTimeout",
                null, // Hidden.
                null, // Hidden.
                null, // Hidden.
                30,
                key,
                0, 300);
            this.CredentialGenerationBehavior = RegistryEnumSetting <RdpCredentialGenerationBehavior> .FromKey(
                "CredentialGenerationBehavior",
                null, // Hidden.
                null, // Hidden.
                null, // Hidden.
                RdpCredentialGenerationBehavior._Default,
                key);

            this.RdpPort = RegistryDwordSetting.FromKey(
                "RdpPort",
                "RDP port",
                "RDP port",
                "Connection",
                3389,
                key,
                1,
                ushort.MaxValue);

            Debug.Assert(this.Settings.All(s => s != null));
        }