Remove a bunch of unused test code

The tests were testing nothing but themselves!
This commit is contained in:
Eilon Lipton 2015-10-15 23:00:10 -07:00
parent 3c925fc4bf
commit b5712ef176
7 changed files with 3 additions and 408 deletions

View File

@ -1,57 +0,0 @@
// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System;
using System.Collections.Generic;
using System.Diagnostics;
using Microsoft.Extensions.Logging;
namespace Microsoft.AspNet.Authentication.Tests.OpenIdConnect
{
public class InMemoryLogger : ILogger, IDisposable
{
LogLevel _logLevel = 0;
public InMemoryLogger(LogLevel logLevel = LogLevel.Debug)
{
_logLevel = logLevel;
}
List<LogEntry> _logEntries = new List<LogEntry>();
public IDisposable BeginScopeImpl(object state)
{
return this;
}
public void Dispose()
{
}
public bool IsEnabled(LogLevel logLevel)
{
return (logLevel >= _logLevel);
}
public void Log(LogLevel logLevel, int eventId, object state, Exception exception, Func<object, Exception, string> formatter)
{
if (IsEnabled(logLevel))
{
var logEntry =
new LogEntry
{
EventId = eventId,
Exception = exception,
Formatter = formatter,
Level = logLevel,
State = state,
};
_logEntries.Add(logEntry);
Debug.WriteLine(logEntry.ToString());
}
}
public List<LogEntry> Logs { get { return _logEntries; } }
}
}

View File

@ -1,41 +0,0 @@
// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System;
using Microsoft.Extensions.Logging;
namespace Microsoft.AspNet.Authentication.Tests.OpenIdConnect
{
public class InMemoryLoggerFactory : ILoggerFactory
{
InMemoryLogger _logger;
LogLevel _logLevel = LogLevel.Debug;
public InMemoryLoggerFactory(LogLevel logLevel)
{
_logLevel = logLevel;
_logger = new InMemoryLogger(_logLevel);
}
public LogLevel MinimumLevel
{
get { return _logLevel; }
set { _logLevel = value; }
}
public void AddProvider(ILoggerProvider provider)
{
}
public ILogger CreateLogger(string categoryName)
{
return _logger;
}
public void Dispose()
{
}
public InMemoryLogger Logger { get { return _logger; } }
}
}

View File

@ -1,41 +0,0 @@
// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
// this controls if the logs are written to the console.
// they can be reviewed for general content.
using System;
using Microsoft.Extensions.Logging;
namespace Microsoft.AspNet.Authentication.Tests.OpenIdConnect
{
public class LogEntry
{
public LogEntry() { }
public int EventId { get; set; }
public Exception Exception { get; set; }
public Func<object, Exception, string> Formatter { get; set; }
public LogLevel Level { get; set; }
public object State { get; set; }
public override string ToString()
{
if (Formatter != null)
{
return Formatter(this.State, this.Exception);
}
else
{
string message = (Formatter != null ? Formatter(State, Exception) : (State?.ToString() ?? "null"));
message += ", LogLevel: " + Level.ToString();
message += ", EventId: " + EventId.ToString();
message += ", Exception: " + (Exception == null ? "null" : Exception.Message);
return message;
}
}
}
}

View File

@ -1,148 +0,0 @@
// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
// this controls if the logs are written to the console.
// they can be reviewed for general content.
using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Extensions.Logging;
namespace Microsoft.AspNet.Authentication.Tests.OpenIdConnect
{
public class LoggingUtilities
{
static List<LogEntry> CompleteLogEntries;
static Dictionary<string, LogLevel> LogEntries;
static LoggingUtilities()
{
LogEntries =
new Dictionary<string, LogLevel>()
{
{ "OIDCH_0000:", LogLevel.Debug },
{ "OIDCH_0001:", LogLevel.Debug },
{ "OIDCH_0002:", LogLevel.Verbose },
{ "OIDCH_0003:", LogLevel.Verbose },
{ "OIDCH_0004:", LogLevel.Warning },
{ "OIDCH_0005:", LogLevel.Error },
{ "OIDCH_0006:", LogLevel.Error },
{ "OIDCH_0007:", LogLevel.Verbose },
{ "OIDCH_0008:", LogLevel.Verbose },
{ "OIDCH_0009:", LogLevel.Verbose },
{ "OIDCH_0010:", LogLevel.Error },
{ "OIDCH_0011:", LogLevel.Error },
{ "OIDCH_0012:", LogLevel.Verbose },
{ "OIDCH_0013:", LogLevel.Verbose },
{ "OIDCH_0014:", LogLevel.Debug },
{ "OIDCH_0015:", LogLevel.Verbose },
{ "OIDCH_0016:", LogLevel.Verbose },
{ "OIDCH_0017:", LogLevel.Error },
{ "OIDCH_0018:", LogLevel.Verbose },
{ "OIDCH_0019:", LogLevel.Verbose },
{ "OIDCH_0020:", LogLevel.Debug },
{ "OIDCH_0021:", LogLevel.Verbose },
{ "OIDCH_0026:", LogLevel.Error },
{ "OIDCH_0038:", LogLevel.Debug },
{ "OIDCH_0040:", LogLevel.Debug },
{ "OIDCH_0042:", LogLevel.Debug },
{ "OIDCH_0043:", LogLevel.Verbose },
{ "OIDCH_0044:", LogLevel.Verbose },
{ "OIDCH_0045:", LogLevel.Debug }
};
BuildLogEntryList();
}
/// <summary>
/// Builds the complete list of OpenIdConnect log entries that are available in the runtime.
/// </summary>
private static void BuildLogEntryList()
{
CompleteLogEntries = new List<LogEntry>();
foreach (var entry in LogEntries)
{
CompleteLogEntries.Add(new LogEntry { State = entry.Key, Level = entry.Value });
}
}
/// <summary>
/// Adds to errors if a variation if any are found.
/// </summary>
/// <param name="variation">if this has been seen before, errors will be appended, test results are easier to understand if this is unique.</param>
/// <param name="capturedLogs">these are the logs the runtime generated</param>
/// <param name="expectedLogs">these are the errors that were expected</param>
/// <param name="errors">the dictionary to record any errors</param>
public static void CheckLogs(List<LogEntry> capturedLogs, List<LogEntry> expectedLogs, List<Tuple<LogEntry, LogEntry>> errors)
{
if (capturedLogs.Count >= expectedLogs.Count)
{
for (int i = 0; i < capturedLogs.Count; i++)
{
if (i + 1 > expectedLogs.Count)
{
errors.Add(new Tuple<LogEntry, LogEntry>(capturedLogs[i], null));
}
else
{
if (!TestUtilities.AreEqual<LogEntry>(capturedLogs[i], expectedLogs[i]))
{
errors.Add(new Tuple<LogEntry, LogEntry>(capturedLogs[i], expectedLogs[i]));
}
}
}
}
else
{
for (int i = 0; i < expectedLogs.Count; i++)
{
if (i + 1 > capturedLogs.Count)
{
errors.Add(new Tuple<LogEntry, LogEntry>(null, expectedLogs[i]));
}
else
{
if (!TestUtilities.AreEqual<LogEntry>(expectedLogs[i], capturedLogs[i]))
{
errors.Add(new Tuple<LogEntry, LogEntry>(capturedLogs[i], expectedLogs[i]));
}
}
}
}
}
public static string LoggingErrors(List<Tuple<LogEntry, LogEntry>> errors)
{
string loggingErrors = null;
if (errors.Count > 0)
{
var stringBuilder = new StringBuilder();
stringBuilder.AppendLine("");
foreach (var error in errors)
{
stringBuilder.AppendLine("*Captured*, *Expected* : *" + (error.Item1?.ToString() ?? "null") + "*, *" + (error.Item2?.ToString() ?? "null") + "*");
}
loggingErrors = stringBuilder.ToString();
}
return loggingErrors;
}
/// <summary>
/// Populates a list of expected log entries for a test variation.
/// </summary>
/// <param name="items">the index for the <see cref="LogEntry"/> in CompleteLogEntries of interest.</param>
/// <returns>a <see cref="List{LogEntry}"/> that represents the expected entries for a test variation.</returns>
public static List<LogEntry> PopulateLogEntries(int[] items)
{
var entries = new List<LogEntry>();
foreach (var item in items)
{
entries.Add(CompleteLogEntries[item]);
}
return entries;
}
}
}

View File

@ -14,7 +14,6 @@ using Microsoft.AspNet.Builder;
using Microsoft.AspNet.Http.Authentication;
using Microsoft.AspNet.TestHost;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.WebEncoders;
using Microsoft.IdentityModel.Protocols.OpenIdConnect;
using Xunit;
@ -30,29 +29,6 @@ namespace Microsoft.AspNet.Authentication.Tests.OpenIdConnect
private static SecurityToken specCompliantOpenIdConnect = new JwtSecurityToken("issuer", "audience", new List<Claim> { new Claim("iat", EpochTime.GetIntDate(DateTime.UtcNow).ToString()), new Claim("nonce", nonceForOpenIdConnect) }, DateTime.UtcNow, DateTime.UtcNow + TimeSpan.FromDays(1));
private const string ExpectedStateParameter = "expectedState";
/// <summary>
/// Sanity check that logging is filtering, hi / low water marks are checked
/// </summary>
[Fact]
public void LoggingLevel()
{
var logger = new InMemoryLogger(LogLevel.Debug);
Assert.True(logger.IsEnabled(LogLevel.Critical));
Assert.True(logger.IsEnabled(LogLevel.Debug));
Assert.True(logger.IsEnabled(LogLevel.Error));
Assert.True(logger.IsEnabled(LogLevel.Information));
Assert.True(logger.IsEnabled(LogLevel.Verbose));
Assert.True(logger.IsEnabled(LogLevel.Warning));
logger = new InMemoryLogger(LogLevel.Critical);
Assert.True(logger.IsEnabled(LogLevel.Critical));
Assert.False(logger.IsEnabled(LogLevel.Debug));
Assert.False(logger.IsEnabled(LogLevel.Error));
Assert.False(logger.IsEnabled(LogLevel.Information));
Assert.False(logger.IsEnabled(LogLevel.Verbose));
Assert.False(logger.IsEnabled(LogLevel.Warning));
}
[Theory, MemberData(nameof(AuthenticateCoreStateDataSet))]
public async Task AuthenticateCoreState(Action<OpenIdConnectOptions> action, OpenIdConnectMessage message)
{

View File

@ -21,7 +21,7 @@ namespace Microsoft.AspNet.Authentication.Tests.OpenIdConnect
OpenIdConnectHandler _handler;
public OpenIdConnectMiddlewareForTestingAuthenticate(
RequestDelegate next,
RequestDelegate next,
IDataProtectionProvider dataProtectionProvider,
ILoggerFactory loggerFactory,
IUrlEncoder encoder,
@ -34,9 +34,6 @@ namespace Microsoft.AspNet.Authentication.Tests.OpenIdConnect
: base(next, dataProtectionProvider, loggerFactory, encoder, services, sharedOptions, options, htmlEncoder)
{
_handler = handler;
var customFactory = loggerFactory as InMemoryLoggerFactory;
if (customFactory != null)
Logger = customFactory.Logger;
}
protected override AuthenticationHandler<OpenIdConnectOptions> CreateHandler()

View File

@ -1,7 +1,6 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System;
using Microsoft.IdentityModel.Protocols;
using Microsoft.IdentityModel.Protocols.OpenIdConnect;
@ -14,97 +13,7 @@ namespace Microsoft.AspNet.Authentication.Tests.OpenIdConnect
{
public const string DefaultHost = @"http://localhost";
public static bool AreEqual<T>(object obj1, object obj2, Func<object, object, bool> comparer = null) where T : class
{
if (obj1 == null && obj2 == null)
{
return true;
}
if (obj1 == null || obj2 == null)
{
return false;
}
if (obj1.GetType() != obj2.GetType())
{
return false;
}
if (obj1.GetType() != typeof(T))
{
return false;
}
if (comparer != null)
{
return comparer(obj1, obj2);
}
if (typeof(T) == typeof(LogEntry))
{
return AreEqual(obj1 as LogEntry, obj2 as LogEntry);
}
else if (typeof(T) == typeof(Exception))
{
return AreEqual(obj1 as Exception, obj2 as Exception);
}
throw new ArithmeticException("Unknown type, no comparer. Type: " + typeof(T).ToString());
}
/// <summary>
/// Never call this method directly, call AreObjectsEqual, as it deals with nulls and types"/>
/// </summary>
/// <param name="logEntry1"></param>
/// <param name="logEntry2"></param>
/// <returns></returns>
private static bool AreEqual(LogEntry logEntry1, LogEntry logEntry2)
{
if (logEntry1.EventId != logEntry2.EventId)
{
return false;
}
if (logEntry1.State == null && logEntry2.State == null)
{
return true;
}
if (logEntry1.State == null)
{
return false;
}
if (logEntry2.State == null)
{
return false;
}
string logValue1 = logEntry1.Formatter == null ? logEntry1.State.ToString() : logEntry1.Formatter(logEntry1.State, logEntry1.Exception);
string logValue2 = logEntry2.Formatter == null ? logEntry2.State.ToString() : logEntry2.Formatter(logEntry2.State, logEntry2.Exception);
return (logValue1.StartsWith(logValue2) || (logValue2.StartsWith(logValue1)));
}
/// <summary>
/// Never call this method directly, call AreObjectsEqual, as it deals with nulls and types"/>
/// </summary>
/// <param name="exception1"></param>
/// <param name="exception2"></param>
/// <returns></returns>
private static bool AreEqual(Exception exception1, Exception exception2)
{
if (!string.Equals(exception1.Message, exception2.Message))
{
return false;
}
return AreEqual<Exception>(exception1.InnerException, exception2.InnerException);
}
static public IConfigurationManager<OpenIdConnectConfiguration> DefaultOpenIdConnectConfigurationManager
public static IConfigurationManager<OpenIdConnectConfiguration> DefaultOpenIdConnectConfigurationManager
{
get
{
@ -112,7 +21,7 @@ namespace Microsoft.AspNet.Authentication.Tests.OpenIdConnect
}
}
static public OpenIdConnectConfiguration DefaultOpenIdConnectConfiguration
public static OpenIdConnectConfiguration DefaultOpenIdConnectConfiguration
{
get
{