public void DoFailuresWithWmiEnabled() { int numberOfEvents = 50; using (WmiEventWatcher eventListener = new WmiEventWatcher(numberOfEvents)) { HashAlgorithmProvider hashProvider = new HashAlgorithmProvider(typeof(SHA1Managed), false); InstrumentationAttacherFactory attacherFactory = new InstrumentationAttacherFactory(); IInstrumentationAttacher binder = attacherFactory.CreateBinder(hashProvider.GetInstrumentationEventProvider(), new object[] { "foo", true, true, true }, new ConfigurationReflectionCache()); binder.BindInstrumentation(); for (int i = 0; i < numberOfEvents; i++) { try { hashProvider.CreateHash(null); } catch (Exception) { } } eventListener.WaitForEvents(); Assert.AreEqual(numberOfEvents, eventListener.EventsReceived.Count); } }
public void DoLotsOfConnectionFailures() { int numberOfEvents = 50; using (WmiEventWatcher eventListener = new WmiEventWatcher(numberOfEvents)) { SqlDatabase db = new SqlDatabase("BadConnectionString"); DataInstrumentationListener listener = new DataInstrumentationListener("foo", true, true, true); DataInstrumentationListenerBinder binder = new DataInstrumentationListenerBinder(); binder.Bind(db.GetInstrumentationEventProvider(), listener); for (int i = 0; i < numberOfEvents; i++) { try { db.ExecuteScalar(CommandType.Text, "Select count(*) from Region"); } catch { } } eventListener.WaitForEvents(); Assert.AreEqual(numberOfEvents, eventListener.EventsReceived.Count); Assert.AreEqual("ConnectionFailedEvent", eventListener.EventsReceived[0].ClassPath.ClassName); Assert.AreEqual("foo", eventListener.EventsReceived[0].GetPropertyValue("InstanceName")); Assert.AreEqual(db.ConnectionStringWithoutCredentials, eventListener.EventsReceived[0].GetPropertyValue("ConnectionString")); } }
public void AuthorizationFailedFires2WmiEvents() { AzManAuthorizationProvider instrumentedAzman = new AzManAuthorizationProvider(data.StoreLocation, data.Application, data.AuditIdentifierPrefix, data.Scope); AuthorizationProviderInstrumentationListener listener = new AuthorizationProviderInstrumentationListener("foo", false, false, true); ReflectionInstrumentationBinder binder = new ReflectionInstrumentationBinder(); binder.Bind(instrumentedAzman.GetInstrumentationEventProvider(), listener); using (WmiEventWatcher eventWatcher = new WmiEventWatcher(2)) { bool res = instrumentedAzman.Authorize(cryptographyProviderCollection, unauthorizedTask); eventWatcher.WaitForEvents(); Assert.AreEqual(2, eventWatcher.EventsReceived.Count); Assert.AreEqual("AuthorizationCheckPerformedEvent", eventWatcher.EventsReceived[0].ClassPath.ClassName); Assert.AreEqual("foo", eventWatcher.EventsReceived[0].Properties["InstanceName"].Value); Assert.AreEqual(cryptographyProviderCollection.Identity.Name, eventWatcher.EventsReceived[0].Properties["UserName"].Value); Assert.AreEqual(unauthorizedTask, eventWatcher.EventsReceived[0].Properties["TaskName"].Value); Assert.AreEqual("AuthorizationCheckFailedEvent", eventWatcher.EventsReceived[1].ClassPath.ClassName); Assert.AreEqual("foo", eventWatcher.EventsReceived[1].Properties["InstanceName"].Value); Assert.AreEqual(cryptographyProviderCollection.Identity.Name, eventWatcher.EventsReceived[1].Properties["UserName"].Value); Assert.AreEqual(unauthorizedTask, eventWatcher.EventsReceived[1].Properties["TaskName"].Value); } }
public void FireSimpleEvent() { using (WmiEventWatcher watcher = new WmiEventWatcher(1)) { BaseWmiEvent myEvent = new TestEvent("Hello, World"); System.Management.Instrumentation.Instrumentation.Fire(myEvent); watcher.WaitForEvents(); } }
public void FireSimpleEvent() { using (WmiEventWatcher watcher = new WmiEventWatcher(1)) { BaseWmiEvent myEvent = new TestEvent("Hello, World"); System.Management.Instrumentation.Instrumentation.Fire(myEvent); watcher.WaitForEvents(); } }
public void SecurityCacheCheckDoesNotifyWmiIfEnabled() { new ReflectionInstrumentationBinder().Bind(instrumentationProvider, enabledInstrumentationListener); using (WmiEventWatcher eventListener = new WmiEventWatcher(numberOfEvents)) { FireSecurityCacheReadPerformed(); eventListener.WaitForEvents(); Assert.AreEqual(numberOfEvents, eventListener.EventsReceived.Count); } }
public void AuthorizationCheckDoesNotNotifyWmiIfDisabled() { new ReflectionInstrumentationBinder().Bind(instrumentationProvider, disabledInstrumentationListener); using (WmiEventWatcher eventListener = new WmiEventWatcher(numberOfEvents)) { FireAuthorizationCheckPerformed(); eventListener.WaitForEvents(); Assert.AreEqual(0, eventListener.EventsReceived.Count); } }
public void ServiceFailureWithExceptionFiresWmiEvent() { DistributorEventLogger logger = new DistributorEventLogger(TestApplicationName); using (WmiEventWatcher eventListener = new WmiEventWatcher(1)) { logger.LogServiceFailure(message, GetException(), TraceEventType.Error); eventListener.WaitForEvents(); Assert.AreEqual(1, eventListener.EventsReceived.Count); Assert.AreEqual("DistributorServiceFailureEvent", eventListener.EventsReceived[0].ClassPath.ClassName); Assert.IsTrue(((string)eventListener.EventsReceived[0].GetPropertyValue("FailureMessage")).StartsWith(message)); } }
public void Send100Events() { using (WmiEventWatcher watcher = new WmiEventWatcher(100)) { for (int i = 0; i < 100; i++) { BaseWmiEvent myEvent = new TestEvent("" + i); System.Management.Instrumentation.Instrumentation.Fire(myEvent); } watcher.WaitForEvents(); Assert.AreEqual(100, watcher.EventsReceived.Count); Assert.AreEqual("50", watcher.EventsReceived[50].Properties["Text"].Value); } }
public void ServiceStartedFiresWmiEvent() { DistributorEventLogger logger = new DistributorEventLogger(TestApplicationName); using (WmiEventWatcher eventListener = new WmiEventWatcher(1)) { logger.LogServiceStarted(); eventListener.WaitForEvents(); Assert.AreEqual(1, eventListener.EventsReceived.Count); Assert.AreEqual("DistributorServiceLifecycleEvent", eventListener.EventsReceived[0].ClassPath.ClassName); Assert.AreEqual(true, eventListener.EventsReceived[0].GetPropertyValue("Started")); } }
public void CacheScavengingWithInstrumentationDisabledDoesNotFireWmiEvent() { CachingInstrumentationListener listener = new CachingInstrumentationListener(instanceName, false, false, false, formatter); using (WmiEventWatcher eventListener = new WmiEventWatcher(1)) { CacheScavengedEventArgs args = new CacheScavengedEventArgs(10); listener.CacheScavenged(null, args); eventListener.WaitForEvents(); Assert.AreEqual(0, eventListener.EventsReceived.Count); } }
public void DefaultLoggerFiresWmiEvent() { DefaultCachingEventLogger logger = new DefaultCachingEventLogger(false, true); ConfigurationErrorsException exception = new ConfigurationErrorsException(exceptionMessage); using (WmiEventWatcher eventListener = new WmiEventWatcher(1)) { logger.LogConfigurationError(instanceName, exception); eventListener.WaitForEvents(); Assert.AreEqual(1, eventListener.EventsReceived.Count); Assert.AreEqual("CacheConfigurationFailureEvent", eventListener.EventsReceived[0].ClassPath.ClassName); Assert.AreEqual(instanceName, eventListener.EventsReceived[0].GetPropertyValue("InstanceName")); } }
public void CacheCallbackFailureWithInstrumentationDisabledDoesNotFireWmiEvent() { CachingInstrumentationListener listener = new CachingInstrumentationListener(instanceName, false, false, false, formatter); Exception exception = new Exception(exceptionMessage); using (WmiEventWatcher eventListener = new WmiEventWatcher(1)) { CacheCallbackFailureEventArgs args = new CacheCallbackFailureEventArgs(key, exception); listener.CacheCallbackFailed(null, args); eventListener.WaitForEvents(); Assert.AreEqual(0, eventListener.EventsReceived.Count); } }
public void Send100Events() { using (WmiEventWatcher watcher = new WmiEventWatcher(100)) { for (int i = 0; i < 100; i++) { BaseWmiEvent myEvent = new TestEvent("" + i); System.Management.Instrumentation.Instrumentation.Fire(myEvent); } watcher.WaitForEvents(); Assert.AreEqual(100, watcher.EventsReceived.Count); Assert.AreEqual("50", watcher.EventsReceived[50].Properties["Text"].Value); } }
public void CacheScavengingWithInstrumentationEnabledDoesFireWmiEvent() { CachingInstrumentationListener listener = new CachingInstrumentationListener(instanceName, false, false, true, formatter); using (WmiEventWatcher eventListener = new WmiEventWatcher(1)) { CacheScavengedEventArgs args = new CacheScavengedEventArgs(10); listener.CacheScavenged(null, args); eventListener.WaitForEvents(); Assert.AreEqual(1, eventListener.EventsReceived.Count); Assert.AreEqual("CacheScavengedEvent", eventListener.EventsReceived[0].ClassPath.ClassName); Assert.AreEqual(instanceName, eventListener.EventsReceived[0].GetPropertyValue("InstanceName")); Assert.AreEqual(10L, eventListener.EventsReceived[0].GetPropertyValue("ItemsScavenged")); } }
public void CacheCallbackFailureWithInstrumentationEnabledDoesFireWmiEvent() { CachingInstrumentationListener listener = new CachingInstrumentationListener(instanceName, false, false, true, formatter); Exception exception = new Exception(exceptionMessage); using (WmiEventWatcher eventListener = new WmiEventWatcher(1)) { CacheCallbackFailureEventArgs args = new CacheCallbackFailureEventArgs(key, exception); listener.CacheCallbackFailed(null, args); eventListener.WaitForEvents(); Assert.AreEqual(1, eventListener.EventsReceived.Count); Assert.AreEqual("CacheCallbackFailureEvent", eventListener.EventsReceived[0].ClassPath.ClassName); Assert.AreEqual(key, eventListener.EventsReceived[0].GetPropertyValue("Key")); Assert.IsTrue(eventListener.EventsReceived[0].GetPropertyValue("ExceptionMessage").ToString().IndexOf(exceptionMessage) > -1); } }
public void CommandFailureFiresWmiEvent() { using (WmiEventWatcher eventListener = new WmiEventWatcher(1)) { Database db = DatabaseFactory.CreateDatabase(); try { db.ExecuteNonQuery(CommandType.StoredProcedure, "BadCommandText"); } catch { } eventListener.WaitForEvents(); Assert.AreEqual(1, eventListener.EventsReceived.Count); Assert.AreEqual("CommandFailedEvent", eventListener.EventsReceived[0].ClassPath.ClassName); Assert.AreEqual("Service_Dflt", eventListener.EventsReceived[0].GetPropertyValue("InstanceName")); Assert.AreEqual(db.ConnectionStringWithoutCredentials, eventListener.EventsReceived[0].GetPropertyValue("ConnectionString")); Assert.AreEqual("BadCommandText", eventListener.EventsReceived[0].GetPropertyValue("CommandText")); } }
public void CreatedProviderHasCorrectInstrumentationListener() { SecurityCacheProviderFactory factory = new SecurityCacheProviderFactory(GetConfigurationSource()); ISecurityCacheProvider provider = factory.Create("provider1"); Assert.AreEqual(typeof(MockSecurityCacheProvider), provider.GetType()); object instrumentationProvider = ((MockSecurityCacheProvider)provider).GetInstrumentationEventProvider(); Assert.AreSame(typeof(SecurityCacheProviderInstrumentationProvider), instrumentationProvider.GetType()); SecurityCacheProviderInstrumentationProvider castedInstrumentationProvider = (SecurityCacheProviderInstrumentationProvider)instrumentationProvider; using (WmiEventWatcher eventWatcher = new WmiEventWatcher(1)) { IToken token = new GuidToken(); castedInstrumentationProvider.FireSecurityCacheReadPerformed(SecurityEntityType.Identity, token); eventWatcher.WaitForEvents(); Assert.AreEqual(1, eventWatcher.EventsReceived.Count); Assert.AreEqual("SecurityCacheReadPerformedEvent", eventWatcher.EventsReceived[0].ClassPath.ClassName); Assert.AreEqual(SecurityEntityType.Identity.ToString(), eventWatcher.EventsReceived[0].Properties["EntityType"].Value); Assert.AreEqual("provider1", eventWatcher.EventsReceived[0].Properties["InstanceName"].Value); Assert.AreEqual(token.Value, eventWatcher.EventsReceived[0].Properties["TokenUsed"].Value); } }
public void WmiEventFiredWhenAskingForDatabaseWithUnknownName() { using (WmiEventWatcher eventListener = new WmiEventWatcher(1)) { try { Database db = DatabaseFactory.CreateDatabase("ThisIsAnUnknownKey"); } catch (ConfigurationErrorsException) { eventListener.WaitForEvents(); Assert.AreEqual(1, eventListener.EventsReceived.Count); Assert.AreEqual("DataConfigurationFailureEvent", eventListener.EventsReceived[0].ClassPath.ClassName); Assert.AreEqual("ThisIsAnUnknownKey", eventListener.EventsReceived[0].GetPropertyValue("InstanceName")); string exceptionMessage = (string)eventListener.EventsReceived[0].GetPropertyValue("ExceptionMessage"); Assert.IsFalse(-1 == exceptionMessage.IndexOf("ThisIsAnUnknownKey")); return; } Assert.Fail("ConfigurationErrorsException expected"); } }
public void WmiEventFiredWhenCreatingUndefinedPolicy() { using (WmiEventWatcher eventListener = new WmiEventWatcher(1)) { try { ExceptionPolicy.HandleException(new Exception(), "ThisIsAnUnknownKey"); } catch (ConfigurationErrorsException) { eventListener.WaitForEvents(); Assert.AreEqual(1, eventListener.EventsReceived.Count); Assert.AreEqual("ExceptionHandlingConfigurationFailureEvent", eventListener.EventsReceived[0].ClassPath.ClassName); Assert.AreEqual("ThisIsAnUnknownKey", eventListener.EventsReceived[0].GetPropertyValue("PolicyName")); string exceptionMessage = (string)eventListener.EventsReceived[0].GetPropertyValue("ExceptionMessage"); Assert.IsFalse(-1 == exceptionMessage.IndexOf("ThisIsAnUnknownKey")); } } }
public void RetrieveCachedIdentityFiresWmiEvent() { SecurityCacheProvider securityCache = SecurityCacheFactory.GetSecurityCacheProvider() as SecurityCacheProvider; SecurityCacheProviderInstrumentationListener listener = new SecurityCacheProviderInstrumentationListener("foo", false, false, true); ReflectionInstrumentationBinder binder = new ReflectionInstrumentationBinder(); binder.Bind(securityCache.GetInstrumentationEventProvider(), listener); using (WmiEventWatcher eventWatcher = new WmiEventWatcher(1)) { IToken token = new GuidToken(); object profile = securityCache.GetIdentity(token); eventWatcher.WaitForEvents(); Assert.AreEqual(1, eventWatcher.EventsReceived.Count); Assert.AreEqual("SecurityCacheReadPerformedEvent", eventWatcher.EventsReceived[0].ClassPath.ClassName); Assert.AreEqual(SecurityEntityType.Identity.ToString(), eventWatcher.EventsReceived[0].Properties["EntityType"].Value); Assert.AreEqual("foo", eventWatcher.EventsReceived[0].Properties["InstanceName"].Value); Assert.AreEqual(token.Value, eventWatcher.EventsReceived[0].Properties["TokenUsed"].Value); } }
public void AuthorizeFiresWmiEvent() { AzManAuthorizationProvider instrumentedAzman = new AzManAuthorizationProvider(data.StoreLocation, data.Application, data.AuditIdentifierPrefix, data.Scope); AuthorizationProviderInstrumentationListener listener = new AuthorizationProviderInstrumentationListener("foo", false, false, true); ReflectionInstrumentationBinder binder = new ReflectionInstrumentationBinder(); binder.Bind(instrumentedAzman.GetInstrumentationEventProvider(), listener); using (WmiEventWatcher eventWatcher = new WmiEventWatcher(1)) { bool res = instrumentedAzman.Authorize(cryptographyProviderCollection, authorizedTask); eventWatcher.WaitForEvents(); Thread.Sleep(500); Assert.AreEqual(1, eventWatcher.EventsReceived.Count); Assert.AreEqual("foo", eventWatcher.EventsReceived[0].Properties["InstanceName"].Value); Assert.AreEqual(cryptographyProviderCollection.Identity.Name, eventWatcher.EventsReceived[0].Properties["UserName"].Value); Assert.AreEqual(authorizedTask, eventWatcher.EventsReceived[0].Properties["TaskName"].Value); } }
public void WmiEventFiredWhenLoggingConfigurationIsCorrupt() { //corrupt the logging configuration SysConfig.Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None); LoggingSettings rwLoggingSettings = (LoggingSettings)config.GetSection(LoggingSettings.SectionName); string originalFirstTraceListenerName = rwLoggingSettings.TraceListeners.Get(0).Name; rwLoggingSettings.TraceSources.Get(0).TraceListeners.Get(0).Name = "wrongName"; config.Save(ConfigurationSaveMode.Full); ConfigurationManager.RefreshSection(LoggingSettings.SectionName); using (WmiEventWatcher eventListener = new WmiEventWatcher(1)) { try { Logger.Write("Some message"); } catch (ConfigurationErrorsException) { eventListener.WaitForEvents(); Assert.AreEqual(1, eventListener.EventsReceived.Count); Assert.AreEqual("LoggingConfigurationFailureEvent", eventListener.EventsReceived[0].ClassPath.ClassName); string exceptionMessage = (string)eventListener.EventsReceived[0].GetPropertyValue("ExceptionMessage"); Assert.IsTrue(exceptionMessage.Contains("wrongName")); } finally { //restore the logging configuration rwLoggingSettings.TraceSources.Get(0).TraceListeners.Get(0).Name = originalFirstTraceListenerName; config.Save(ConfigurationSaveMode.Full); ConfigurationManager.RefreshSection(LoggingSettings.SectionName); } } }
public void WmiFiredWhenDeliveryToErrorSourceFails() { TraceListener badTraceListener = new BadTraceListener(new Exception("test exception")); LogSource badSource = new LogSource("badSource"); badSource.Listeners.Add(badTraceListener); Dictionary<string, LogSource> logSources = new Dictionary<string,LogSource>(); logSources.Add("foo", badSource); LogWriter writer = new LogWriter(new List<ILogFilter>(), logSources, badSource, "foo"); new ReflectionInstrumentationBinder().Bind(writer.GetInstrumentationEventProvider(), new LoggingInstrumentationListener(false, false, true)); using (WmiEventWatcher eventListener = new WmiEventWatcher(1)) { writer.Write(CommonUtil.GetDefaultLogEntry()); eventListener.WaitForEvents(); Assert.AreEqual(1, eventListener.EventsReceived.Count); Assert.AreEqual("LoggingFailureLoggingErrorEvent", eventListener.EventsReceived[0].ClassPath.ClassName); string exceptionMessage = (string) eventListener.EventsReceived[0].GetPropertyValue("ExceptionMessage"); Assert.IsTrue(-1 != exceptionMessage.IndexOf("test exception")); Assert.IsNotNull(eventListener.EventsReceived[0].GetPropertyValue("ErrorMessage")); } }