public void TestSaveAndRemoveSettings()
        {
            var repository = new SqlRewriteDbRepository(new SqlConnection(Settings.Default.ConnectionString));
            var settings   = new SqlRewriteSettings
            {
                Id = 0, // This will cause insertion
                ProcessNameRegEx         = ".*",
                MachineRegEx             = ".*",
                StackFrameIgnorePrefixes = "[mscorlib].\r\nSqlInterceptorsTests."
            };

            repository.SaveSqlRewriteSettings(settings);
            try
            {
                Assert.AreNotEqual(0, settings.Id);
                var items = repository.LoadSqlRewriteSettings();
                SettingsExists(items, settings);
                repository.RemoveSqlRewriteSettings(settings.Id);
                items = repository.LoadSqlRewriteSettings();
                Assert.ThrowsException <Exception>(() => { SettingsExists(items, settings); });
            }
            finally
            {
                try
                {
                    repository.RemoveSqlRewriteSettings(settings.Id);
                }
                catch (Exception)
                {
                    // Ignore exception
                }
            }
        }
コード例 #2
0
        public void TestApplySettings()
        {
            using var conn = new SqlConnection(Settings.Default.ConnectionString);
            var repo = new SqlRewriteDbRepository(conn);

            repo.RemoveAllSqlRewriteSettings();
            var settings = new SqlRewriteSettings()
            {
                MachineRegEx               = Settings.Default.MachineNameMatchString,
                ProcessNameRegEx           = "Program Files",
                Enabled                    = true,
                HashInjectionEnabled       = false,
                RegExInjectionEnabled      = false,
                StackFrameInjectionEnabled = false,
                StackFrameIgnorePrefixes   = "Microsoft.VisualStudio.TestTools.UnitTesting"
            };

            repo.SaveSqlRewriteSettings(settings);
            using var service = new SqlRewriteRuleService(repo);
            Assert.IsFalse(service.Enabled);
            Assert.IsTrue(SqlCommandRegExProcessor.RegExInjectionEnabled);
            Assert.IsTrue(SqlCommandTextStackTraceInjector.HashInjectionEnabled);
            Assert.IsTrue(SqlCommandTextStackTraceInjector.StackInjectionEnabled);
            Assert.AreEqual("", SqlCommandTextStackTraceInjector.StackFrameIgnorePrefixes);
            service.Enabled = true;
            service.ApplySettingsFromRepository();
            Assert.IsTrue(service.Enabled);
            Assert.IsFalse(SqlCommandRegExProcessor.RegExInjectionEnabled);
            Assert.IsFalse(SqlCommandTextStackTraceInjector.HashInjectionEnabled);
            Assert.IsFalse(SqlCommandTextStackTraceInjector.StackInjectionEnabled);
            Assert.AreEqual("Microsoft.VisualStudio.TestTools.UnitTesting", SqlCommandTextStackTraceInjector.StackFrameIgnorePrefixes);
            repo.RemoveSqlRewriteSettings(settings.Id);
            settings.Enabled = false;
            settings.HashInjectionEnabled       = true;
            settings.RegExInjectionEnabled      = true;
            settings.StackFrameInjectionEnabled = true;
            settings.StackFrameIgnorePrefixes   = "";
            repo.SaveSqlRewriteSettings(settings);
            service.ApplySettingsFromRepository();
            Assert.IsFalse(service.Enabled);
            Assert.IsTrue(SqlCommandRegExProcessor.RegExInjectionEnabled);
            Assert.IsTrue(SqlCommandTextStackTraceInjector.HashInjectionEnabled);
            Assert.IsTrue(SqlCommandTextStackTraceInjector.StackInjectionEnabled);
            Assert.AreEqual("", SqlCommandTextStackTraceInjector.StackFrameIgnorePrefixes);
            service.RemoveSettings(settings.Id);
            var id = service.StoreCurrentSettings(settings.MachineRegEx, settings.ProcessNameRegEx);

            Assert.AreNotEqual(settings.Id, id);
            service.ApplySettingsFromRepository();
            Assert.IsFalse(service.Enabled);
            Assert.IsTrue(SqlCommandRegExProcessor.RegExInjectionEnabled);
            Assert.IsTrue(SqlCommandTextStackTraceInjector.HashInjectionEnabled);
            Assert.IsTrue(SqlCommandTextStackTraceInjector.StackInjectionEnabled);
            Assert.AreEqual("", SqlCommandTextStackTraceInjector.StackFrameIgnorePrefixes);
            service.RemoveSettings(id);
            // ReSharper disable once AccessToDisposedClosure
            Assert.ThrowsException <SqlRewriteRuleDbRepositoryException>(() => { service.RemoveSettings(id); });
        }