public void NullKeysTest()
        {
            var package = new Mock <IReAttachPackage>(MockBehavior.Strict);

            package.Setup(p => p.OpenUserRegistryRoot()).Returns <IRegistryKey>(null);
            package.Setup(p => p.Reporter).Returns(new ReAttachTraceReporter());

            var repository = new ReAttachRegistryRepository(package.Object);

            Assert.IsNull(repository.LoadTargets(),
                          "Null key should have resulted in empty result from load method.");
            Assert.IsFalse(repository.SaveTargets(new ReAttachTargetList(ReAttachConstants.ReAttachHistorySize)),
                           "Null key should have resulted in false result from save method.");

            var key = new Mock <IRegistryKey>(MockBehavior.Strict);

            key.Setup(k => k.OpenSubKey(It.IsAny <string>())).Returns <IRegistryKey>(null);
            key.Setup(k => k.CreateSubKey(It.IsAny <string>())).Returns <IRegistryKey>(null);
            key.Setup(k => k.Close());
            package.Setup(p => p.OpenUserRegistryRoot()).Returns(key.Object);

            Assert.IsNull(repository.LoadTargets(),
                          "Null subkey should have resulted in empty result from load method.");
            Assert.IsFalse(repository.SaveTargets(new ReAttachTargetList(ReAttachConstants.ReAttachHistorySize)),
                           "Null subkey should have resulted in false result from save method.");
        }
        public void LoadInvalidDataTest()
        {
            const string json1 = "{\"ProcessId\":7024,\"ProcessName\":\"test1.exe\",\"ProcessPath\":\"test1.exe\",\"ProcessUser\":\"TEST1\",\"ServerName\":\"\",\"IsAttached\":false,\"IsLocal\":true,\"Engine\":null}";
            const string json2 = "{\"ProcessId\":7025,\"ProcessName\":\"test2.exe\",\"ProcessPath\":\"test2.exe\",\"ProcessUser\":\"TEST2\",\"ServerName\":\"\",\"IsAttached\":false,\"IsLocal\":true,\"Engine\":null}";

            var key    = new Mock <IRegistryKey>();
            var subkey = new Mock <IRegistryKey>();

            key.Setup(k => k.OpenSubKey(ReAttachConstants.ReAttachRegistryKeyName)).Returns(subkey.Object);

            subkey.Setup(k => k.GetValue(ReAttachConstants.ReAttachRegistryHistoryKeyPrefix + 1)).Returns(json1);
            subkey.Setup(k => k.GetValue(ReAttachConstants.ReAttachRegistryHistoryKeyPrefix + 2)).Returns("invalid-json-item");
            subkey.Setup(k => k.GetValue(ReAttachConstants.ReAttachRegistryHistoryKeyPrefix + 3)).Returns(json2);

            var package = new Mock <IReAttachPackage>();

            package.Setup(p => p.OpenUserRegistryRoot()).Returns(key.Object);
            var repository = new ReAttachRegistryRepository(package.Object);

            var result = repository.LoadTargets();

            Assert.IsNotNull(result, "Empty set loaded resulted in null result, should result in empty list.");
            Assert.AreEqual(2, result.Count, "Invalid number of results loaded.");

            for (var i = 1; i <= 2; i++)
            {
                subkey.Verify(k => k.GetValue(ReAttachConstants.ReAttachRegistryHistoryKeyPrefix + i));
            }
        }
        public void LoadErrorTest()
        {
            var key     = new Mock <IRegistryKey>();
            var package = new Mock <IReAttachPackage>();

            package.Setup(p => p.OpenUserRegistryRoot()).Returns(key.Object);
            package.Setup(p => p.Reporter).Returns(new ReAttachTraceReporter());
            var repository = new ReAttachRegistryRepository(package.Object);

            Assert.IsNull(repository.LoadTargets(), "Non-null result on first load, no key should mean null return value.");

            key.Setup(k => k.OpenSubKey(It.IsAny <string>())).
            Throws(new SecurityException("Simulating no access. :)"));

            Assert.IsNull(repository.LoadTargets(), "Either SecurityException wasn't thrown, or there's a problem with error handling in load method.");
            key.Verify(k => k.OpenSubKey(It.IsAny <string>()), Times.Exactly(2));
        }
        public void LoadEmptyTest()
        {
            var key    = new Mock <IRegistryKey>();
            var subkey = new Mock <IRegistryKey>();

            key.Setup(k => k.OpenSubKey(ReAttachConstants.ReAttachRegistryKeyName)).Returns(subkey.Object);

            var package = new Mock <IReAttachPackage>();

            package.Setup(p => p.OpenUserRegistryRoot()).Returns(key.Object);
            var repository = new ReAttachRegistryRepository(package.Object);

            var result = repository.LoadTargets();

            Assert.IsNotNull(result, "Empty set loaded resulted in null result, should result in empty list.");
            Assert.AreEqual(0, result.Count, "Empty set loaded resulted in non-empty result, should result in empty list.");
            key.Verify(k => k.OpenSubKey(It.IsAny <string>()), Times.Once());
        }
        public void LoadTest()
        {
            const string json = "{\"ProcessId\":7024,\"ProcessName\":\"test1.exe\",\"ProcessPath\":\"PROCESS-PATH\",\"ProcessUser\":\"TEST1\",\"ServerName\":\"\",\"IsAttached\":false,\"IsLocal\":true,\"Engine\":null}";

            var key    = new Mock <IRegistryKey>();
            var subkey = new Mock <IRegistryKey>();

            key.Setup(k => k.OpenSubKey(ReAttachConstants.ReAttachRegistryKeyName)).Returns(subkey.Object);

            const int items = 3;

            for (var i = 1; i <= items; i++)
            {
                subkey.Setup(k => k.GetValue(ReAttachConstants.ReAttachRegistryHistoryKeyPrefix + i))
                .Returns(json.Replace("PROCESS-PATH", "process" + i));
            }
            var package = new Mock <IReAttachPackage>();

            package.Setup(p => p.OpenUserRegistryRoot()).Returns(key.Object);
            var repository = new ReAttachRegistryRepository(package.Object);

            var result = repository.LoadTargets();

            Assert.IsNotNull(result, "Empty set loaded resulted in null result, should result in empty list.");
            Assert.AreEqual(items, result.Count, "Invalid number of results loaded.");

            for (var i = 0; i < items; i++)
            {
                Assert.AreEqual("process" + (i + 1), result[i].ProcessPath, "Mismatching path found for item " + i);
            }

            key.Verify(k => k.OpenSubKey(It.IsAny <string>()), Times.Once());
            for (var i = 1; i <= items; i++)
            {
                subkey.Verify(k => k.GetValue(ReAttachConstants.ReAttachRegistryHistoryKeyPrefix + i));
            }
        }