public void OpenBaseKey_should_throw_an_exception_for_anything_other_than_x64()
        {
            var    registry = new FakeRegistry();
            Action action   = () => registry.OpenBaseKey(RegistryHive.LocalMachine, RegistryView.Default);

            action.Should().ThrowExactly <ArgumentException>().And.ParamName.Should().Be("view");
        }
        public void OpenSubKey_should_return_null_if_the_key_does_not_exist()
        {
            var registry = new FakeRegistry();

            using (IWin32RegistryKey hkcu = registry.OpenBaseKey(RegistryHive.CurrentUser, RegistryView.Registry64))
            {
                hkcu.OpenSubKey("NotThere").Should().BeNull();
            }
        }
        public void GetValue_should_return_the_default_value_if_not_present()
        {
            var registry = new FakeRegistry();

            using (IWin32RegistryKey hkcu = registry.OpenBaseKey(RegistryHive.CurrentUser, RegistryView.Registry64))
                using (IWin32RegistryKey subKey = hkcu.CreateSubKey("SubKey"))
                {
                    subKey.GetValue("NoValue", 100).Should().Be(100);
                }
        }
        public void CreateSubKey_should_create_a_key_if_it_does_not_exist()
        {
            var registry = new FakeRegistry();

            using (IWin32RegistryKey hkcu = registry.OpenBaseKey(RegistryHive.CurrentUser, RegistryView.Registry64))
                using (hkcu.CreateSubKey("SubKey"))
                {
                    hkcu.OpenSubKey("SubKey").Should().NotBeNull();
                }
        }
        public void SetValue_should_create_the_value_if_not_present()
        {
            var registry = new FakeRegistry();

            using (IWin32RegistryKey hkcu = registry.OpenBaseKey(RegistryHive.CurrentUser, RegistryView.Registry64))
                using (IWin32RegistryKey subKey = hkcu.CreateSubKey("SubKey", writable: true))
                {
                    subKey.SetValue("ValueName", 123, RegistryValueKind.DWord);
                    subKey.GetValue("ValueName").Should().Be(123);
                }
        }
        public void SetValue_should_throw_an_UnauthorizedAccessException_if_it_is_not_writable()
        {
            var registry = new FakeRegistry();

            using (IWin32RegistryKey hkcu = registry.OpenBaseKey(RegistryHive.CurrentUser, RegistryView.Registry64))
                using (IWin32RegistryKey subKey = hkcu.CreateSubKey("SubKey", writable: false))
                {
                    Action action = () => subKey.SetValue("ValueName", 123, RegistryValueKind.DWord);
                    action.Should().ThrowExactly <UnauthorizedAccessException>();
                }
        }
        public void OpenSubKey_and_CreateSubKey_should_accept_nested_paths()
        {
            var registry = new FakeRegistry();

            using (IWin32RegistryKey hkcu = registry.OpenBaseKey(RegistryHive.CurrentUser, RegistryView.Registry64))
                using (IWin32RegistryKey subKey = hkcu.CreateSubKey(@"a\b\c"))
                {
                    hkcu.OpenSubKey(@"a\b\c").Should().NotBeNull();
                    hkcu.OpenSubKey(@"a\b").Should().NotBeNull();
                    hkcu.OpenSubKey(@"a").Should().NotBeNull();
                }
        }
        public void OpenSubKey_should_throw_ObjectDisposedException_if_closed()
        {
            var registry = new FakeRegistry();

            using (IWin32RegistryKey hkcu = registry.OpenBaseKey(RegistryHive.CurrentUser, RegistryView.Registry64))
            {
                IWin32RegistryKey subKey = hkcu.CreateSubKey("SubKey", writable: false);
                subKey.Dispose();

                Action action = () => subKey.OpenSubKey("ValueName");
                action.Should().ThrowExactly <ObjectDisposedException>();
            }
        }
        public void Closing_a_base_key_does_not_throw_ObjectDisposedException()
        {
            var realKey = RegistryKey.OpenBaseKey(RegistryHive.CurrentUser, RegistryView.Registry64);

            realKey.Dispose();
            Action action = () => realKey.CreateSubKey(TestPath);

            action.Should().NotThrow();

            var fakeRegistry          = new FakeRegistry();
            IWin32RegistryKey fakeKey = fakeRegistry.OpenBaseKey(RegistryHive.CurrentUser, RegistryView.Registry64);

            fakeKey.Dispose();
            action = () => fakeKey.CreateSubKey(TestPath);
            action.Should().NotThrow();
        }
        public void An_operation_on_a_disposed_key_throws_the_same_exception_as_the_real_registry()
        {
            Exception expectedException = null;

            using (var hkcu = RegistryKey.OpenBaseKey(RegistryHive.CurrentUser, RegistryView.Registry64))
            {
                RegistryKey testKey = hkcu.CreateSubKey(TestPath, writable: false);
                testKey.Dispose();

                try
                {
                    testKey.GetValue("TestValue");
                }
                catch (Exception e)
                {
                    expectedException = e;
                }
                finally
                {
                    hkcu.DeleteSubKey(TestPath, throwOnMissingSubKey: false);
                }

                expectedException.Should().NotBeNull();
            }

            var fakeRegistry = new FakeRegistry();

            using (IWin32RegistryKey hkcu = fakeRegistry.OpenBaseKey(RegistryHive.CurrentUser, RegistryView.Registry64))
            {
                IWin32RegistryKey testKey = hkcu.CreateSubKey(TestPath, writable: false);
                testKey.Dispose();

                // ReSharper disable once AccessToDisposedClosure
                Action action = () => testKey.SetValue("TestValue", 123, RegistryValueKind.DWord);
                action.Should()
                .Throw <Exception>()
                .And.Should()
                .BeEquivalentTo(
                    expectedException,
                    options => options.Excluding(e => e.TargetSite)
                    .Excluding(e => e.StackTrace)
                    .Excluding(e => e.Source)
                    .Excluding(info => info.SelectedMemberInfo.Name == "IPForWatsonBuckets"));
            }
        }
 public Key(FakeRegistry registry, string fullPath, bool writable)
 {
     _registry = registry;
     FullPath  = fullPath;
     Writable  = writable;
 }