public void TextListenerCantAccessFileToWrite() { SampleListener listener = new SampleListener(); listener.EnableEvents(IdentityModelEventSource.Logger, EventLevel.Informational); // default log file cannot be accessed because it is in use. Should throw an IO exception. string fileName = Guid.NewGuid().ToString() + ".txt"; FileStream fileStream = File.Create(fileName); Assert.Throws <IOException>(() => { new TextWriterEventListener(fileName); }); Assert.Contains("MIML10001: ", listener.TraceBuffer); fileStream.Dispose(); File.Delete(fileName); // file specified by user cannot be accessed. fileName = Guid.NewGuid().ToString() + ".txt"; fileStream = File.Create(fileName); FileInfo fileInfo = new FileInfo(fileName); fileInfo.IsReadOnly = true; Assert.Throws <UnauthorizedAccessException>(() => { new TextWriterEventListener(fileName); }); fileInfo.IsReadOnly = false; fileStream.Dispose(); File.Delete(fileName); }
public void PrepareMessageWithNoArguments() { SampleListener listener = new SampleListener(); IdentityModelEventSource.Logger.LogLevel = EventLevel.Error; listener.EnableEvents(IdentityModelEventSource.Logger, EventLevel.Error); var exception = LogHelper.LogExceptionMessage(new ArgumentException("This is the first parameter '{0}'. This is the second parameter '{1}'.")); }
public void LogException() { var messageWithParams = Guid.NewGuid().ToString() + "{0}"; var guid1 = Guid.NewGuid().ToString(); var guid2 = Guid.NewGuid().ToString(); var guid3 = Guid.NewGuid().ToString(); var guid4 = Guid.NewGuid().ToString(); var guid5 = Guid.NewGuid().ToString(); SampleListener listener = new SampleListener(); IdentityModelEventSource.Logger.LogLevel = EventLevel.Critical; listener.EnableEvents(IdentityModelEventSource.Logger, EventLevel.Critical); // default logs at Error var exception = LogHelper.LogExceptionMessage(new ArgumentException(guid1)); Assert.Equal(typeof(ArgumentException), exception.GetType()); Assert.True(string.IsNullOrEmpty(listener.TraceBuffer)); Assert.Contains(guid1, exception.Message); listener = new SampleListener(); IdentityModelEventSource.Logger.LogLevel = EventLevel.Error; listener.EnableEvents(IdentityModelEventSource.Logger, EventLevel.Error); exception = LogHelper.LogExceptionMessage(new ArgumentException(guid1)); Assert.Equal(typeof(ArgumentException), exception.GetType()); Assert.Contains(guid1, exception.Message); exception = LogHelper.LogExceptionMessage(new ArgumentException(String.Format(CultureInfo.InvariantCulture, messageWithParams, guid2))); Assert.Contains(guid2, exception.Message); Assert.Equal(typeof(ArgumentException), exception.GetType()); exception = LogHelper.LogExceptionMessage(EventLevel.Error, new ArgumentException(String.Format(CultureInfo.InvariantCulture, messageWithParams, guid3))); Assert.Contains(guid3, exception.Message); exception = LogHelper.LogExceptionMessage(EventLevel.Error, new ArgumentException(String.Format(CultureInfo.InvariantCulture, messageWithParams, guid4), new NotSupportedException())); Assert.Contains(guid4, exception.Message); Assert.NotNull(exception.InnerException); Assert.Equal(typeof(NotSupportedException), exception.InnerException.GetType()); exception = LogHelper.LogExceptionMessage(EventLevel.Informational, new ArgumentException(String.Format(CultureInfo.InvariantCulture, messageWithParams, guid5), new NotSupportedException())); Assert.Contains(guid5, exception.Message); Assert.NotNull(exception.InnerException); Assert.Equal(typeof(NotSupportedException), exception.InnerException.GetType()); Assert.Contains(guid1, listener.TraceBuffer); Assert.Contains(guid2, listener.TraceBuffer); Assert.Contains(guid3, listener.TraceBuffer); Assert.Contains(guid4, listener.TraceBuffer); Assert.DoesNotContain(guid5, listener.TraceBuffer); }
public void LogExceptionAsArgument() { SampleListener listener = new SampleListener(); IdentityModelEventSource.Logger.LogLevel = EventLevel.Error; listener.EnableEvents(IdentityModelEventSource.Logger, EventLevel.Error); LogHelper.LogExceptionMessage(EventLevel.Error, new ArgumentException(LogHelper.FormatInvariant("Main exception 1: {0}", new SecurityTokenCompressionFailedException("custom inner exception")))); LogHelper.LogExceptionMessage(EventLevel.Error, new ArgumentException(LogHelper.FormatInvariant("Main exception 2: {0}", new InvalidOperationException("system exception")))); Assert.Contains("Main exception 1: Microsoft.IdentityModel.Tokens.SecurityTokenCompressionFailedException: custom inner exception", listener.TraceBuffer); Assert.Contains("Main exception 2: [PII of type 'System.InvalidOperationException' is hidden. For more details, see https://aka.ms/IdentityModel/PII.]", listener.TraceBuffer); }
public void LogNullArgument() { SampleListener listener = new SampleListener(); IdentityModelEventSource.Logger.LogLevel = EventLevel.Error; listener.EnableEvents(IdentityModelEventSource.Logger, EventLevel.Error); string algorithm = null; LogHelper.LogExceptionMessage(new NotSupportedException(LogHelper.FormatInvariant("Algorithm not supported exception 1: {0}", algorithm))); LogHelper.LogExceptionMessage(new NotSupportedException(LogHelper.FormatInvariant("Algorithm not supported exception 2: {0}", LogHelper.MarkAsNonPII(algorithm)))); Assert.Contains("Algorithm not supported exception 1: [PII of type 'Null' is hidden. For more details, see https://aka.ms/IdentityModel/PII.]", listener.TraceBuffer); Assert.Contains("Algorithm not supported exception 2: Null", listener.TraceBuffer); }
public void TestLogLevel() { SampleListener listener = new SampleListener(); IdentityModelEventSource.Logger.LogLevel = EventLevel.Informational; listener.EnableEvents(IdentityModelEventSource.Logger, EventLevel.Verbose); var guid1 = Guid.NewGuid().ToString(); var guid2 = Guid.NewGuid().ToString(); LogHelper.LogVerbose(guid1); LogHelper.LogInformation(guid2); Assert.DoesNotContain(guid1, listener.TraceBuffer); Assert.Contains(guid2, listener.TraceBuffer); }
public void LogMessage() { SampleListener listener = new SampleListener(); IdentityModelEventSource.Logger.LogLevel = EventLevel.Warning; listener.EnableEvents(IdentityModelEventSource.Logger, EventLevel.Verbose); TokenValidationParameters validationParameters = new TokenValidationParameters() { ValidateAudience = false }; // This should log a warning about not validating the audience Validators.ValidateAudience(null, null, validationParameters); Assert.Contains("IDX10233: ", listener.TraceBuffer); }
public void LogWsFederationExceptions() { SampleListener listener = new SampleListener(); IdentityModelEventSource.Logger.LogLevel = EventLevel.Error; listener.EnableEvents(IdentityModelEventSource.Logger, EventLevel.Error); var exception1 = LogHelper.LogExceptionMessage(new ArgumentNullException("test1")); var exception2 = LogHelper.LogExceptionMessage(new WsFederationException(LogHelper.FormatInvariant(TestMessageTwoParams, "test2", "test3"), exception1)); var exception3 = LogHelper.LogExceptionMessage(new WsFederationReadException(LogHelper.FormatInvariant(TestMessageOneParam, "test4"), exception2)); Assert.Contains("test1", listener.TraceBuffer); Assert.DoesNotContain("test2", listener.TraceBuffer); Assert.DoesNotContain("test3", listener.TraceBuffer); Assert.DoesNotContain("test4", listener.TraceBuffer); }
public void LogTokenExceptions() { SampleListener listener = new SampleListener(); IdentityModelEventSource.Logger.LogLevel = EventLevel.Error; listener.EnableEvents(IdentityModelEventSource.Logger, EventLevel.Error); var exception1 = LogHelper.LogExceptionMessage(new ArgumentNullException("test1")); var exception2 = LogHelper.LogExceptionMessage(new SecurityTokenDecryptionFailedException(LogHelper.FormatInvariant(TestMessageTwoParams, "test2", "test3"), exception1)); var exception3 = LogHelper.LogExceptionMessage(new SecurityTokenEncryptionFailedException(LogHelper.FormatInvariant(TestMessageOneParam, "test4"), exception2)); var exception4 = LogHelper.LogExceptionMessage(new SecurityTokenEncryptionKeyNotFoundException(LogHelper.FormatInvariant(TestMessageOneParam, "test5"), exception3)); var exception5 = LogHelper.LogExceptionMessage(new SecurityTokenException(LogHelper.FormatInvariant(TestMessageOneParam, "test6"), exception4)); var exception6 = LogHelper.LogExceptionMessage(new SecurityTokenExpiredException(LogHelper.FormatInvariant(TestMessageOneParam, "test7"), exception5)); var exception7 = LogHelper.LogExceptionMessage(new SecurityTokenInvalidAudienceException(LogHelper.FormatInvariant(TestMessageOneParam, "test8"), exception6)); var exception8 = LogHelper.LogExceptionMessage(new SecurityTokenInvalidIssuerException(LogHelper.FormatInvariant(TestMessageOneParam, "test9"), exception7)); var exception9 = LogHelper.LogExceptionMessage(new SecurityTokenInvalidLifetimeException(LogHelper.FormatInvariant(TestMessageOneParam, "test10"), exception8)); var exception10 = LogHelper.LogExceptionMessage(new SecurityTokenInvalidSignatureException(LogHelper.FormatInvariant(TestMessageOneParam, "test11"), exception9)); var exception11 = LogHelper.LogExceptionMessage(new SecurityTokenInvalidSigningKeyException(LogHelper.FormatInvariant(TestMessageOneParam, "test12"), exception10)); var exception12 = LogHelper.LogExceptionMessage(new SecurityTokenKeyWrapException(LogHelper.FormatInvariant(TestMessageOneParam, "test13"), exception11)); var exception13 = LogHelper.LogExceptionMessage(new SecurityTokenNoExpirationException(LogHelper.FormatInvariant(TestMessageOneParam, "test14"), exception12)); var exception14 = LogHelper.LogExceptionMessage(new SecurityTokenNotYetValidException(LogHelper.FormatInvariant(TestMessageOneParam, "test15"), exception13)); var exception15 = LogHelper.LogExceptionMessage(new SecurityTokenReplayAddFailedException(LogHelper.FormatInvariant(TestMessageOneParam, "test16"), exception14)); var exception16 = LogHelper.LogExceptionMessage(new SecurityTokenReplayDetectedException(LogHelper.FormatInvariant(TestMessageOneParam, "test17"), exception15)); var exception17 = LogHelper.LogExceptionMessage(new SecurityTokenSignatureKeyNotFoundException(LogHelper.FormatInvariant(TestMessageOneParam, "test18"), exception16)); var exception18 = LogHelper.LogExceptionMessage(new SecurityTokenValidationException(LogHelper.FormatInvariant(TestMessageOneParam, "test19"), exception17)); Assert.Contains("test1", listener.TraceBuffer); Assert.DoesNotContain("test2", listener.TraceBuffer); Assert.DoesNotContain("test3", listener.TraceBuffer); Assert.DoesNotContain("test4", listener.TraceBuffer); Assert.DoesNotContain("test5", listener.TraceBuffer); Assert.DoesNotContain("test6", listener.TraceBuffer); Assert.DoesNotContain("test7", listener.TraceBuffer); Assert.DoesNotContain("test8", listener.TraceBuffer); Assert.DoesNotContain("test9", listener.TraceBuffer); Assert.DoesNotContain("test10", listener.TraceBuffer); Assert.DoesNotContain("test11", listener.TraceBuffer); Assert.DoesNotContain("test12", listener.TraceBuffer); Assert.DoesNotContain("test13", listener.TraceBuffer); Assert.DoesNotContain("test14", listener.TraceBuffer); Assert.DoesNotContain("test15", listener.TraceBuffer); Assert.DoesNotContain("test16", listener.TraceBuffer); Assert.DoesNotContain("test17", listener.TraceBuffer); Assert.DoesNotContain("test18", listener.TraceBuffer); Assert.DoesNotContain("test19", listener.TraceBuffer); }
public void LogMessageAndThrowException() { SampleListener listener = new SampleListener(); // since null parameters exceptions are logged at Verbose level IdentityModelEventSource.Logger.LogLevel = EventLevel.Verbose; listener.EnableEvents(IdentityModelEventSource.Logger, EventLevel.Verbose); var guid = Guid.NewGuid().ToString(); try { throw LogHelper.LogArgumentNullException(guid); } catch (Exception ex) { Assert.Equal(typeof(ArgumentNullException), ex.GetType()); Assert.Contains(guid, listener.TraceBuffer); } }
public void LogOpenIdConnectProtocolExceptions() { SampleListener listener = new SampleListener(); IdentityModelEventSource.Logger.LogLevel = EventLevel.Error; listener.EnableEvents(IdentityModelEventSource.Logger, EventLevel.Error); var exception1 = LogHelper.LogExceptionMessage(new ArgumentNullException("test1")); var exception2 = LogHelper.LogExceptionMessage(new OpenIdConnectProtocolException(LogHelper.FormatInvariant(TestMessageTwoParams, "test2", "test3"), exception1)); var exception3 = LogHelper.LogExceptionMessage(new OpenIdConnectProtocolInvalidAtHashException(LogHelper.FormatInvariant(TestMessageOneParam, "test4"), exception2)); var exception4 = LogHelper.LogExceptionMessage(new OpenIdConnectProtocolInvalidCHashException(LogHelper.FormatInvariant(TestMessageOneParam, "test5"), exception3)); var exception5 = LogHelper.LogExceptionMessage(new OpenIdConnectProtocolInvalidNonceException(LogHelper.FormatInvariant(TestMessageOneParam, "test6"), exception4)); var exception6 = LogHelper.LogExceptionMessage(new OpenIdConnectProtocolInvalidStateException(LogHelper.FormatInvariant(TestMessageOneParam, "test7"), exception5)); Assert.Contains("test1", listener.TraceBuffer); Assert.DoesNotContain("test2", listener.TraceBuffer); Assert.DoesNotContain("test3", listener.TraceBuffer); Assert.DoesNotContain("test4", listener.TraceBuffer); Assert.DoesNotContain("test5", listener.TraceBuffer); Assert.DoesNotContain("test6", listener.TraceBuffer); Assert.DoesNotContain("test7", listener.TraceBuffer); }