// 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.Framework.Logging; namespace Microsoft.AspNet.Authentication.Tests.OpenIdConnect { public class LoggingUtilities { static List CompleteLogEntries; static Dictionary LogEntries; static LoggingUtilities() { LogEntries = new Dictionary() { { "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(); } /// /// Builds the complete list of OpenIdConnect log entries that are available in the runtime. /// private static void BuildLogEntryList() { CompleteLogEntries = new List(); foreach (var entry in LogEntries) { CompleteLogEntries.Add(new LogEntry { State = entry.Key, Level = entry.Value }); } } /// /// Adds to errors if a variation if any are found. /// /// if this has been seen before, errors will be appended, test results are easier to understand if this is unique. /// these are the logs the runtime generated /// these are the errors that were expected /// the dictionary to record any errors public static void CheckLogs(List capturedLogs, List expectedLogs, List> errors) { if (capturedLogs.Count >= expectedLogs.Count) { for (int i = 0; i < capturedLogs.Count; i++) { if (i + 1 > expectedLogs.Count) { errors.Add(new Tuple(capturedLogs[i], null)); } else { if (!TestUtilities.AreEqual(capturedLogs[i], expectedLogs[i])) { errors.Add(new Tuple(capturedLogs[i], expectedLogs[i])); } } } } else { for (int i = 0; i < expectedLogs.Count; i++) { if (i + 1 > capturedLogs.Count) { errors.Add(new Tuple(null, expectedLogs[i])); } else { if (!TestUtilities.AreEqual(expectedLogs[i], capturedLogs[i])) { errors.Add(new Tuple(capturedLogs[i], expectedLogs[i])); } } } } } public static string LoggingErrors(List> 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; } /// /// Populates a list of expected log entries for a test variation. /// /// the index for the in CompleteLogEntries of interest. /// a that represents the expected entries for a test variation. public static List PopulateLogEntries(int[] items) { var entries = new List(); foreach (var item in items) { entries.Add(CompleteLogEntries[item]); } return entries; } } }