using System.Globalization; using System.IO; using Microsoft.Win32; using Titano.Core.Packaging; using Titano.Setup.Cli; using Titano.Setup.Engine; using Titano.Setup.Interop; using Titano.Testing; namespace Titano.App.Tests.Setup; /// /// What setup leaves behind and what it registers: services and startup entries of the install folder (SETUP-1, /// SETUP-2), shortcuts another product owns (SRS-09), the Server's all-users scope (SRS-06) and the file type name /// in setup's language (SRS-04). /// public sealed class SetupEngineTests(TestContext context) { [Theory] [Case(@"""C:\Program Files\Titano\Titano.Server.exe"" --service", @"C:\Program Files\Titano\Titano.Server.exe")] [Case(@"C:\Program Files\Titano\Titano.Server.exe --service", @"C:\Program Files\Titano\Titano.Server.exe")] [Case(@"""C:\Program Files\dotnet\dotnet.exe"" ""C:\Apps\Titano\Titano.Server.dll"" --service", @"C:\Apps\Titano\Titano.Server.dll")] [Case(@"C:\Windows\system32\svchost.exe -k netsvcs", @"C:\Windows\system32\svchost.exe")] [Case(@"Titano.Uploader.exe --tray", null)] [Case("", null)] public void TheProgramOfACommandLineIsItsFirstFullPath(string commandLine, string? program) => Assert.Equal(program, CommandProgram.Of(commandLine)); [Test] public void ServiceStepsTravelToTheElevatedCopy() { var arguments = new SetupArguments { Services = ServiceActions.Prune | ServiceActions.Start, Directory = @"C:\Program Files\Titano", Elevated = true }; var switches = arguments.ToSwitches().ToList(); Assert.Contains("/SERVICES=prune,start", switches); Assert.Equal(arguments, SetupArguments.Parse(switches)); Assert.Equal(ServiceActions.Remove, SetupArguments.Parse(["/SERVICES=Remove", "/DIR=C:\\Apps\\Titano"]).Services); Assert.Throws(() => SetupArguments.Parse(["/SERVICES=stop"])); Assert.Throws(() => SetupArguments.Parse(["/SERVICES=kill", "/DIR=C:\\Apps\\Titano"])); Assert.Throws(() => SetupArguments.Parse(["/SERVICES=none", "/DIR=C:\\Apps\\Titano"])); } [Test] [WindowsOnly] public void AServiceWhoseProgramIsGoneIsPrunedAndNothingStartsIt() { var folder = context.CreateTempDirectory("services"); // No service is registered from a fresh folder, and nothing is asked of the Service Control Manager. Assert.Empty(InstalledServices.Find(folder)); Assert.Equal(SetupExitCode.Success, InstalledServices.Apply(folder, ServiceActions.Remove)); var gone = new InstalledService("TitanoTest" + Guid.NewGuid().ToString("N"), "Titano Server", Path.Combine(folder, "Titano.Server.exe"), AutoStart: true); Assert.False(InstalledServices.IsRunning(gone)); Assert.True(InstalledServices.NeedsWork([gone], ServiceActions.Prune)); Assert.True(InstalledServices.NeedsWork([gone], ServiceActions.Remove)); Assert.False(InstalledServices.NeedsWork([gone], ServiceActions.Stop)); Assert.False(InstalledServices.NeedsWork([gone], ServiceActions.Start)); Assert.False(InstalledServices.NeedsWork([], ServiceActions.Remove)); } [Test] [WindowsOnly] public void UninstallRemovesOnlyTheStartupEntriesOfItsFolder() { var folder = Path.Combine(context.CreateTempDirectory("startup"), "Titano Uploader"); const string testsKey = @"Software\Titano.Tests"; var keyPath = testsKey + @"\" + Guid.NewGuid().ToString("N"); try { using (var run = Registry.CurrentUser.CreateSubKey(keyPath, writable: true)) { run.SetValue("Titano Uploader", $"\"{Path.Combine(folder, "Titano.Uploader.exe")}\" --tray"); run.SetValue("Unquoted", Path.Combine(folder, "tools", "Helper.exe") + " /quiet"); run.SetValue("Other", @"""C:\Program Files\Other\Other.exe"" --background"); run.SetValue("Sibling", $"\"{folder} 2\\Titano.Uploader.exe\""); Assert.SequenceEqual(["Titano Uploader", "Unquoted"], StartupEntries.RemoveInside(run, folder).Order(StringComparer.Ordinal)); Assert.SequenceEqual(["Other", "Sibling"], run.GetValueNames().Order(StringComparer.Ordinal)); } } finally { Registry.CurrentUser.DeleteSubKeyTree(keyPath, throwOnMissingSubKey: false); bool empty; using (var tests = Registry.CurrentUser.OpenSubKey(testsKey)) empty = tests is { SubKeyCount: 0, ValueCount: 0 }; if (empty) Registry.CurrentUser.DeleteSubKey(testsKey, throwOnMissingSubKey: false); } } [Test] [WindowsOnly] public void AShortcutTellsWhichProgramItStarts() { var folder = context.CreateTempDirectory("links"); var target = Path.Combine(folder, "Product A", "Titano.Uploader.exe"); Directory.CreateDirectory(Path.GetDirectoryName(target)!); File.WriteAllBytes(target, [0x4D, 0x5A]); var link = Path.Combine(folder, "Titano Uploader.lnk"); ShellLink.Create(link, target, "Titano Uploader"); // The shell may store the long form of a short (8.3) temporary path: the same file either way. var stored = Assert.NotNull(ShellLink.TargetOf(link)); Assert.Equal(Path.GetFileName(target), Path.GetFileName(stored)); Assert.FileExists(stored); var productA = Path.GetDirectoryName(stored)!; Assert.True(CommandProgram.IsPathInside(stored, productA)); Assert.False(CommandProgram.IsPathInside(stored, Path.Combine(Path.GetDirectoryName(productA)!, "Product B"))); Assert.False(CommandProgram.IsPathInside(stored, productA + " 2")); Assert.Null(ShellLink.TargetOf(Path.Combine(folder, "missing.lnk"))); var notALink = Path.Combine(folder, "not a link.lnk"); File.WriteAllText(notALink, "plain text"); Assert.Null(ShellLink.TargetOf(notALink)); } [Test] [WindowsOnly] public void TheServerMakesANewInstallationAnAllUsersOne() { var manifest = Manifest(); var plan = InstallPlan.Resolve(manifest, new SetupArguments { Components = ["Titano", "Server"] }); Assert.Equal(InstallScope.AllUsers, plan.Scope); Assert.Equal(SetupPaths.DefaultDirectory(InstallScope.AllUsers, manifest.Product), plan.Directory); var desktop = InstallPlan.Resolve(manifest, new SetupArguments { Components = ["Titano"] }); Assert.Equal(InstallScope.CurrentUser, desktop.Scope); Assert.True(desktop.CanInstall(manifest.FindComponent("Server")!)); // A per-user installation upgraded in place keeps its scope, so it cannot take the Server. var perUser = desktop with { Existing = new ExistingInstall(InstallScope.CurrentUser, desktop.Directory, "0.9.0") }; Assert.False(perUser.CanInstall(manifest.FindComponent("Server")!)); Assert.True(perUser.CanInstall(manifest.FindComponent("Titano")!)); } [Test] public void TheProjectFileTypeIsNamedInSetupsLanguage() { var association = new PayloadAssociation { Component = "Titano", Extension = ".titano", ProgId = "Titano.Project", Description = "Titano project", Target = "Titano.exe" }; Assert.Equal("Titano project", InstallEngine.FileTypeName(association, CultureInfo.GetCultureInfo("en"))); Assert.Equal("Progetto Titano", InstallEngine.FileTypeName(association, CultureInfo.GetCultureInfo("it"))); var unknown = new PayloadAssociation { Component = "Titano", Extension = ".tlx", ProgId = "Titano.Unknown", Description = "Something else", Target = "Titano.exe" }; Assert.Equal("Something else", InstallEngine.FileTypeName(unknown, CultureInfo.GetCultureInfo("it"))); } /// A manifest under a product name no machine has installed, so the registry holds no installation of it. private static PayloadManifest Manifest() => new() { Product = "TitanoTest" + Guid.NewGuid().ToString("N")[..12], Version = "1.0.0", Components = [ new PayloadComponent { Id = "Titano", DisplayName = "Titano", DefaultSelected = true }, new PayloadComponent { Id = "Server", DisplayName = "Titano Server", AllUsersOnly = true }, ], }; }