From 930fd3486de584e5101555563dbf52a34fdf9891 Mon Sep 17 00:00:00 2001 From: Sung Yoon Whang Date: Fri, 6 Apr 2018 11:39:23 -0700 Subject: [PATCH] Test windows event log (dotnet/coreclr#17401) * Windows Event Log tests * Add test for windows event log * Make it run under one executable * Removing duplicate files * remove more useless file * fix formatting for reference to Diagnostics.EventLog * Windows Event Log test is not supported outside Windows * More logging to see why failures are occuring... * Fix typo * fix more typos * just make the test pass on non-windows platforms * Make the test more reliable with timing Commit migrated from https://github.com/dotnet/coreclr/commit/e7c755118a97ed9cfb1b24a3c1e82007793fc227 --- .../test_dependencies.csproj | 3 + .../WindowsEventLog/WindowsEventLog.cs | 135 ++++++++++++++++++ .../WindowsEventLog/WindowsEventLog.csproj | 39 +++++ .../tests/testsUnsupportedOutsideWindows.txt | 1 + 4 files changed, 178 insertions(+) create mode 100644 src/coreclr/tests/src/baseservices/exceptions/WindowsEventLog/WindowsEventLog.cs create mode 100644 src/coreclr/tests/src/baseservices/exceptions/WindowsEventLog/WindowsEventLog.csproj diff --git a/src/coreclr/tests/src/Common/test_dependencies/test_dependencies.csproj b/src/coreclr/tests/src/Common/test_dependencies/test_dependencies.csproj index 8945355364e..63374916555 100644 --- a/src/coreclr/tests/src/Common/test_dependencies/test_dependencies.csproj +++ b/src/coreclr/tests/src/Common/test_dependencies/test_dependencies.csproj @@ -25,6 +25,9 @@ $(MicrosoftPrivateCoreFxNETCoreAppPackageVersion) + + $(MicrosoftPrivateCoreFxNETCoreAppPackageVersion) + netcoreapp2.1 diff --git a/src/coreclr/tests/src/baseservices/exceptions/WindowsEventLog/WindowsEventLog.cs b/src/coreclr/tests/src/baseservices/exceptions/WindowsEventLog/WindowsEventLog.cs new file mode 100644 index 00000000000..ac37e5a9f31 --- /dev/null +++ b/src/coreclr/tests/src/baseservices/exceptions/WindowsEventLog/WindowsEventLog.cs @@ -0,0 +1,135 @@ +using System; +using System.Diagnostics; +using System.IO; +using System.Threading; + +class Program +{ + private static void UnhandledException() + { + A(); + } + + private static void FailFast() + { + try + { + A(); + } + catch (ArgumentException ex) + { + Environment.FailFast("failing fast", ex); + } + + } + + private static void A() + { + B(); + } + + private static void B() + { + throw new ArgumentException("my ae"); + } + + private static bool LaunchTest(string testName, string[] logEntriesToCheck) + { + DateTime dt = DateTime.Now; + + Process testProcess = new Process(); + string currentPath = Directory.GetCurrentDirectory(); + string corerunPath = Environment.GetEnvironmentVariable("CORE_ROOT") + "\\corerun.exe"; + + testProcess.StartInfo.FileName = corerunPath; + testProcess.StartInfo.Arguments = currentPath + "\\WindowsEventLog.exe " + testName; + testProcess.StartInfo.EnvironmentVariables["CORE_ROOT"] = Environment.GetEnvironmentVariable("CORE_ROOT"); + testProcess.StartInfo.UseShellExecute = false; + + testProcess.Start(); + testProcess.WaitForExit(); + + Thread.Sleep(2000); + + EventLog log = new EventLog("Application"); + + foreach (EventLogEntry entry in log.Entries) + { + int checkCount = 0; + if (entry.TimeGenerated > dt) + { + String source = entry.Source; + String message = entry.Message; + + foreach (string logEntry in logEntriesToCheck) + { + Console.WriteLine("Checking for existence of : " + logEntry); + if (message.Contains(logEntry)) + checkCount += 1; + else + Console.WriteLine("Couldn't find it in: " + message); + } + + if (source.Contains(".NET Runtime") && checkCount == logEntriesToCheck.Length) + return true; + else if (source.Contains(".NET Runtime")) + { + Console.WriteLine("*** Event Log ***"); + Console.WriteLine(message); + } + } + } + return false; + } + + private static bool RunUnhandledExceptionTest() + { + string[] logEntriesToCheck = {"unhandled exception", "my ae", "ArgumentException"}; + return LaunchTest("UnhandledException", logEntriesToCheck); + } + + private static bool RunFailFastTest() + { + string[] logEntriesToCheck = {"The application requested process termination through System.Environment.FailFast(string message).", "failing fast", "ArgumentException"}; + return LaunchTest("FailFast", logEntriesToCheck); + } + + + public static int Main(string[] args) + { + if (args.Length == 0) // When invoked with no args, launch itself with appropriate args to cause various exceptions + { + if (!System.Runtime.InteropServices.RuntimeInformation.OSDescription.Contains("Windows")) + { + return 100; + } + + if (!RunUnhandledExceptionTest()) + { + Console.WriteLine("WindowsEventLog Test: UnhandledExceptionTest failed."); + return 1; + } + + if (!RunFailFastTest()) + { + Console.WriteLine("WindowsEventLog Test: FailFastTest failed."); + return 1; + } + + return 100; + } + + Debug.Assert(args.Length == 1); + + if (args[0] == "UnhandledException") + { + UnhandledException(); + } + else if (args[0] == "FailFast") + { + FailFast(); + } + + return 100; // Should never reach here + } +} diff --git a/src/coreclr/tests/src/baseservices/exceptions/WindowsEventLog/WindowsEventLog.csproj b/src/coreclr/tests/src/baseservices/exceptions/WindowsEventLog/WindowsEventLog.csproj new file mode 100644 index 00000000000..ef03066a216 --- /dev/null +++ b/src/coreclr/tests/src/baseservices/exceptions/WindowsEventLog/WindowsEventLog.csproj @@ -0,0 +1,39 @@ + + + + + Debug + AnyCPU + WindowsEventLog + 2.0 + {95DFC527-4DC1-495E-97D7-E94EE1F7140D} + Exe + {786C830F-07A1-408B-BD7F-6EE04809D6DB};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC} + ..\..\ + + $(DefineConstants);STATIC + + + + + + + + true + + + + False + + + + + + + + + + + + + diff --git a/src/coreclr/tests/testsUnsupportedOutsideWindows.txt b/src/coreclr/tests/testsUnsupportedOutsideWindows.txt index fa7489e1a19..d80a0380563 100644 --- a/src/coreclr/tests/testsUnsupportedOutsideWindows.txt +++ b/src/coreclr/tests/testsUnsupportedOutsideWindows.txt @@ -1,5 +1,6 @@ baseservices/exceptions/regressions/Dev11/147911/test147911/test147911.sh baseservices/exceptions/regressions/V1/SEH/VJ/UnmanagedToManaged/UnmanagedToManaged.sh +baseservices/exceptions/WindowsEventLog/WindowsEventLog.sh baseservices/threading/commitstackonlyasneeded/DefaultStackCommit/DefaultStackCommit.sh baseservices/threading/interlocked/compareexchange/compareexchangetneg/compareexchangetneg.sh baseservices/threading/monitor/pulse/monitorpulse02/monitorpulse02.sh -- GitLab