param( [string] $LibraryPath = (Join-Path $PSScriptRoot '../../../target/debug/terminal.dll') ) $ErrorActionPreference = 'Stop' $library = (Resolve-Path $LibraryPath).Path $directory = Join-Path ([System.IO.Path]::GetTempPath()) ('terminal-logging-smoke-' + [guid]::NewGuid()) [System.IO.Directory]::CreateDirectory($directory) | Out-Null Add-Type -TypeDefinition @' using System; using System.Runtime.InteropServices; using System.Text; public static class TerminalLoggingSmokeHost { [UnmanagedFunctionPointer(CallingConvention.Cdecl)] private delegate IntPtr Create(); [UnmanagedFunctionPointer(CallingConvention.Cdecl)] private delegate int HandleOperation(IntPtr handle); [UnmanagedFunctionPointer(CallingConvention.Cdecl)] private delegate int Initialize(IntPtr handle, byte[] level, UIntPtr levelLength, byte[] directory, UIntPtr directoryLength, uint maxFiles); [UnmanagedFunctionPointer(CallingConvention.Cdecl)] private delegate int LastError(IntPtr handle, byte[] buffer, ref UIntPtr length); private static void Expect(int actual, int expected, string operation) { if (actual != expected) throw new InvalidOperationException(operation + ": expected " + expected + ", got " + actual); } public static void Run(string libraryPath, string directory) { IntPtr library = NativeLibrary.Load(libraryPath); var create = Marshal.GetDelegateForFunctionPointer(NativeLibrary.GetExport(library, "terminal_service_create")); var destroy = Marshal.GetDelegateForFunctionPointer(NativeLibrary.GetExport(library, "terminal_service_destroy")); var initialize = Marshal.GetDelegateForFunctionPointer(NativeLibrary.GetExport(library, "terminal_logging_init")); var shutdown = Marshal.GetDelegateForFunctionPointer(NativeLibrary.GetExport(library, "terminal_logging_shutdown")); var lastError = Marshal.GetDelegateForFunctionPointer(NativeLibrary.GetExport(library, "terminal_service_last_error")); byte[] path = Encoding.UTF8.GetBytes(directory); byte[] level = Encoding.UTF8.GetBytes("info"); byte[] invalid = Encoding.UTF8.GetBytes("invalid"); IntPtr handle = create(); if (handle == IntPtr.Zero) throw new InvalidOperationException("create returned null"); try { Expect(shutdown(IntPtr.Zero), -1, "null handle"); Expect(shutdown(handle), 0, "shutdown before init"); Expect(initialize(handle, invalid, (UIntPtr)invalid.Length, path, (UIntPtr)path.Length, 10), -2, "invalid level"); UIntPtr length = UIntPtr.Zero; Expect(lastError(handle, null, ref length), 0, "last_error size"); byte[] error = new byte[checked((int)length.ToUInt64())]; Expect(lastError(handle, error, ref length), 0, "last_error text"); if (!Encoding.UTF8.GetString(error).StartsWith("INVALID_ARGUMENT:")) throw new InvalidOperationException("unexpected last_error"); Expect(initialize(handle, level, (UIntPtr)level.Length, path, (UIntPtr)path.Length, 10), 0, "initialize"); Expect(initialize(handle, level, (UIntPtr)level.Length, path, (UIntPtr)path.Length, 10), -3, "duplicate initialize"); Expect(shutdown(handle), 0, "shutdown"); Expect(shutdown(handle), 0, "repeat shutdown"); Expect(initialize(handle, level, (UIntPtr)level.Length, path, (UIntPtr)path.Length, 10), -3, "initialize after shutdown"); } finally { Expect(shutdown(handle), 0, "final shutdown"); Expect(destroy(handle), 0, "destroy"); } } } '@ try { [TerminalLoggingSmokeHost]::Run($library, $directory) $files = @(Get-ChildItem -LiteralPath $directory -Filter 'terminal.*.log' -File) if ($files.Count -ne 1) { throw "Expected one daily log file, got $($files.Count)" } $contents = [System.IO.File]::ReadAllText($files[0].FullName) foreach ($message in @('terminal logging initialized', 'terminal logging shutdown')) { if ([regex]::Matches($contents, [regex]::Escape($message)).Count -ne 1) { throw "Expected exactly one '$message' record in the flushed log" } } if ($contents.Contains([char]27)) { throw 'File output contains ANSI escape characters' } Write-Output "PASS: DLL exports, error codes, one-time initialization and shutdown flush ($($files[0].Name))" } finally { Remove-Item -LiteralPath $directory -Recurse -Force }