diff --git a/AGENTS.md b/AGENTS.md index 55dde61da1..6809416135 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -115,13 +115,14 @@ in the next section. `UniGetUI.PackageEngine.Managers.MacPkg` (id `pkg`, macOS only) installs IT-published `.pkg` and `.dmg` files through the Software Catalog; the administrator workflow is in `docs/PKG_FEEDS.md`. +Its format-agnostic parts live in the shared feed project described in the next section. - Its sources are not added from the UI: each catalog of the machine-wide `SoftwareCatalog.json` that has a `pkgFeed` declares one, named by the catalog's id and shown by its name - (`PkgCatalogFeeds`, `PkgFeedSource`), with `Sources.CanBeEdited` false. The catalog page + (`CatalogFeeds`, `InstallerFeedSource`), with `Sources.CanBeEdited` false. The catalog page then lists every package of that feed in the catalog (`SoftwareCatalogViewModel.ExpandPkgFeedsAsync`). - A feed is a `file:` folder whose `.pkg` files are read directly, or an `https:` folder read through - the `index.json` that `unigetui pkg-index ` writes (`PkgIndexCommand`). + the `index.json` that `unigetui pkg-index ` writes (`PkgIndexCommand`, an `IFeedIndexer`). - Identity comes from the file itself: for a `.pkg`, `XarArchive` + `PkgMetadata` (the first component of a Distribution, else `PackageInfo`, plus its outermost app); for a `.dmg`, `DiskImage` mounts it read-only and reads the app at its root. Installed state comes from @@ -151,6 +152,53 @@ in the next section. - The Catalog Editor's **Package feed** dialog (`PkgFeedEditor`, `PkgFeedEditorViewModel`) edits `pkgFeed`/`allowUnsignedPackages` and previews a feed through `PkgFeedInspector`. +## Installer feeds shared by macOS and Windows + +`UniGetUI.PackageEngine.Managers.Generic.InstallerFeeds` holds what does not depend on the file +format: the catalog's feed declarations (`CatalogFeeds`), feed locations and index path checks +(`FeedLocation`), the cached folder/web listing with its last-known fallback (`FeedListing`), +the `index.json` schema (`FeedIndex`), sources (`InstallerFeedSource`, `InstallerFeedSourceHelper`), +staging (`FeedStaging`) and the `pkg-index` command (`FeedIndexCommand` over an `IFeedIndexer`). + +One `pkgFeed` and one `index.json` serve both platforms. macOS entries stay under `packages`, the +key older Macs read in full, and Windows entries go under `windowsPackages`, which they ignore, so a +Mac never lists an installer it cannot run, whatever its release. Each reader also refuses entries +whose file extension it does not own. `pkg-index` writes its own platform's list and keeps the other +one while its files still hash the same, so a mixed folder is indexed by running it once on each. + +## Windows installer packages (.exe and .msi) + +`UniGetUI.PackageEngine.Managers.WinInstaller` (id `installer`, Windows only) is the Windows +counterpart of `pkg`; the administrator workflow is in `docs/WINDOWS_INSTALLER_FEEDS.md`. + +- Identity comes from the file: an `.msi` through msi.dll (`MsiDatabase`: Property table and the + summary Template), keyed by its `UpgradeCode`; an `.exe` through a managed, bounded PE reader + (`PeReader`: version resource, sections, appended data, largest icon) and `InstallerKinds.Detect` + (Inno Setup, NSIS, WiX Burn, InstallShield). A `.json` beside the file + (`InstallerManifest`) fills in or overrides anything, and is required for an unknown installer's + silent switches. `pkg-index` bakes all of it into the entry's `windows` block. +- Installed state comes from Apps and Features (`ArpEntries`, both registry views of HKLM and + HKCU), matched by `InstalledMatcher`: the manifest's `uninstallKey`, else an MSI's related + products or a bundle's `BundleUpgradeCode`, else, for an `.exe` only, its cleaned display name + with an agreeing publisher. `InstallReceipts` remembers the registered version an install left, + so an installer whose `DisplayVersion` differs from its own version is not offered forever. +- Installs run in-process (`WinInstallerRunner`): stage a private copy held open against writes, + verify the index hash, the identity (re-read with the original manifest) and Authenticode + (`Authenticode`, WinVerifyTrust, waived only by `allowUnsignedPackages`), then run it silently, + an `.msi` through `runas` unless it is per-user, an `.exe` unelevated unless its manifest says + `"scope": "machine"` (Inno and Burn elevate themselves; ERROR_ELEVATION_REQUIRED retries + elevated); `ProhibitElevation` refuses. Uninstalls run the registered command made silent and, + for an `.exe`, wait for the entry to disappear, since NSIS uninstallers return at once. An entry + elevates only when it is per-machine, which for Windows Installer products comes from their + `AssignmentType`, not the hive: per-user MSI products are registered under HKLM too. +- Tests swap `ArpEntries.Source`, `InstalledMatcher.RelatedProducts`, `Authenticode.Verify` and + `WinInstallerRunner.RunProcess`; `WinInstallerTests` builds real `.msi` fixtures through msi.dll + and `.exe` fixtures from `cmd.exe` plus an appended NSIS or Inno header. + `WinInstallerEndToEndTests` installs, updates and uninstalls real per-user MSI and Inno Setup + packages (folder and web feeds, and through the operation classes) when + `UNIGETUI_INSTALLER_E2E=1`, and per-machine ones behind UAC when it is `elevated`; otherwise + they report as skipped. + ## Version comparison across ecosystems `CoreTools.VersionStringToStruct` treats `-`, `_`, `/` and `#` as plain numeric separators and diff --git a/docs/CLI.md b/docs/CLI.md index 05e1064f34..1e34cadd75 100644 --- a/docs/CLI.md +++ b/docs/CLI.md @@ -357,7 +357,7 @@ These parameters are accepted by the app executables in addition to the automati | `--delete-shortcuts [...]` | Deletes desktop or Start Menu shortcuts, then quits. | Used by UniGetUI to remove shortcuts the current user is not allowed to delete; refuses any path that is not a `.lnk` or `.url` under a desktop or Start Menu folder. | | `` | Loads a valid bundle file into the Package Bundles page. | Supported extensions include `.ubundle`, `.json`, `.yaml`, and `.xml`. | | `pkg-helper install-daemon` / `remove-daemon` | Installs (or removes) the root service that lets standard users install catalog packages without an administrator password. | macOS, as root. `pkg-helper serve` is what the service runs. See [PKG_FEEDS.md](PKG_FEEDS.md). | -| `pkg-index ` | Writes the `index.json` that publishes a folder of macOS `.pkg`, `.dmg` and `.zip` files as a web feed, then quits. | macOS and Linux builds only; `.dmg` files are read on macOS only. Exits 3 when a file in the folder could not be read. See [PKG_FEEDS.md](PKG_FEEDS.md). | +| `pkg-index ` | Writes the `index.json` that publishes a folder of packages as a web feed, then quits: `.exe` and `.msi` files on Windows, `.pkg`, `.dmg` and `.zip` files elsewhere. Entries another platform wrote are kept while their files are unchanged. | `.dmg` files are read on macOS only. Exits 3 when a file in the folder could not be read. See [PKG_FEEDS.md](PKG_FEEDS.md) and [WINDOWS_INSTALLER_FEEDS.md](WINDOWS_INSTALLER_FEEDS.md). | ## Other environment variables diff --git a/docs/PKG_FEEDS.md b/docs/PKG_FEEDS.md index 5a570566ca..5445093bbd 100644 --- a/docs/PKG_FEEDS.md +++ b/docs/PKG_FEEDS.md @@ -4,6 +4,9 @@ The software catalog can offer apps that no package manager carries: an administ the vendor's `.pkg`, `.dmg` or `.zip` unchanged, and users install and update it from the **Software Catalog** page. +Windows `.exe` and `.msi` installers are published the same way, from the same feed if you like: +see [WINDOWS_INSTALLER_FEEDS.md](WINDOWS_INSTALLER_FEEDS.md). + There is nothing to repackage and no detection rule to write. UniGetUI reads each file's identifier, version and app from the file itself, then checks the Mac: diff --git a/docs/WINDOWS_INSTALLER_FEEDS.md b/docs/WINDOWS_INSTALLER_FEEDS.md new file mode 100644 index 0000000000..bc355570f2 --- /dev/null +++ b/docs/WINDOWS_INSTALLER_FEEDS.md @@ -0,0 +1,172 @@ +# Publishing Windows apps (.exe and .msi) + +The software catalog can offer apps that no package manager carries: an administrator publishes +the vendor's `.exe` or `.msi` unchanged, and users install, update and uninstall it from the +**Software Catalog** page. This is the Windows counterpart of the macOS feeds described in +[PKG_FEEDS.md](PKG_FEEDS.md), and one feed folder can serve both. + +For most installers there is nothing to write. UniGetUI reads each file's identity, version and +silent switches from the file itself, then looks for the app in **Apps and Features** (the +`Uninstall` registry keys of the machine and of the user): + +- **`.msi`:** identified by its `UpgradeCode` (or its `ProductCode` when it has none), at its + `ProductVersion`. Installed when Windows Installer lists a product of that upgrade code, or the + product itself. Installed with `msiexec /i /qn /norestart`. +- **`.exe`:** identified by the product name in its version resource, at its product version. + Installed when an Apps and Features entry carries that name (a trailing version or architecture + is ignored) from the same publisher. UniGetUI recognizes these installer technologies and their + silent switches: + + | Technology | Recognized by | Install | Uninstall | + |---|---|---|---| + | Inno Setup | its setup data or version comment | `/VERYSILENT /SUPPRESSMSGBOXES /NORESTART /SP-` | `/VERYSILENT /SUPPRESSMSGBOXES /NORESTART` | + | NSIS | the header of its appended data | `/S` | `/S` | + | WiX bundle (Burn) | its `.wixburn` section | `/quiet /norestart` | `/quiet /norestart` | + + Any other `.exe` (InstallShield, Advanced Installer, a vendor's own bootstrapper) needs its + silent switches written in a manifest, below. + +So an app that was deployed by Intune, Configuration Manager or by hand is recognized, and updates +are offered for it. + +## Set up a feed once + +A feed is a folder, a share or a web folder holding `.exe` and `.msi` files. In the Catalog Editor, +select a catalog and choose **Package feed**: enter the folder or web address, then **Check feed** +lists every package it publishes and every file it skips, with the reason. Or declare it as the +`pkgFeed` of a catalog in the machine-wide catalog file, `%ProgramData%\Devolutions\UniGetUI\SoftwareCatalog.json`: + +```json +{ + "version": 1, + "catalogs": [ + { + "id": "windows-apps", + "name": "Windows apps", + "packages": [], + "pkgFeed": "\\\\fileserver\\software\\windows" + } + ] +} +``` + +Every package in the feed appears in that catalog. `packages` may stay empty; any entry listed +there as well is shown alongside the feed's packages. Outside the catalog page the id is the +package's source, as in `Installer packages: windows-apps`. + +A feed can be: + +| Location | Example | What UniGetUI reads | +|---|---|---| +| A web folder (https) | `https://software.example.com/windows/` | `index.json` in that folder | +| A local folder or a share | `\\fileserver\software\windows` | the `.exe` and `.msi` files directly | + +Subfolders up to three levels deep are read too. Files whose name contains `uninst` are skipped, +since they are uninstallers. + +### Signatures + +Before installing, UniGetUI checks the installer's Authenticode signature the way Windows checks a +download (`WinVerifyTrust`, with revocation checks). Vendors' installers are signed. An installer +that is unsigned, tampered with, or signed by a certificate this computer does not trust is not +installed. + +To publish your own unsigned installers, add `"allowUnsignedPackages": true` to that catalog, and +keep its feed where only administrators can change it. + +## When the file cannot say it: the manifest + +A `.json` file with the installer's name (`Vendor.exe` → `Vendor.json`) fills in or overrides what +UniGetUI reads. Every field is optional: + +```json +{ + "id": "Vendor.Tool", + "name": "Vendor Tool", + "version": "5.1", + "installArguments": "-s -norestart", + "uninstallArguments": "-s", + "displayName": "Vendor Tool Pro", + "uninstallKey": "{6B1F...}_is1", + "upgradeCode": "{11111111-2222-3333-4444-555555555555}", + "publisher": "Vendor Inc.", + "architectures": ["x64"], + "scope": "user" +} +``` + +- `installArguments`: the switches that install without any window or restart. Required for an + installer UniGetUI does not recognize; for an `.msi` they replace `/qn /norestart`. +- `uninstallArguments`: the switches added to the uninstaller the app registers (its + `UninstallString`) to make it silent. Given, they are used even when the app registers a silent + command of its own. For an `.msi` they replace `/qn /norestart`. +- `uninstallKey`: the exact name of the key the installer writes under `...\CurrentVersion\Uninstall`. + The most reliable way to recognize an installed `.exe`; with it, names are not compared. +- `displayName`: the name the app shows in Apps and Features, when it differs from its product name. +- `upgradeCode`: for a WiX bundle, its upgrade code, which recognizes every version of it. +- `architectures`: `x86`, `x64` or `arm64`. An `.msi` says it itself; an `.exe` is usually a 32-bit + installer whatever it installs, so give it when the feed holds one build per architecture. +- `scope`: `user` or `machine`. An `.exe` starts without administrator rights and asks for them + itself when it needs them, as Inno Setup, WiX bundles and most installers do; give `machine` for + one that fails instead of asking, so that UniGetUI shows the UAC prompt first. An `.msi` is + installed for every user, behind the UAC prompt, unless it is a per-user package + (no `ALLUSERS`, or `ALLUSERS=2` with `MSIINSTALLPERUSER=1`) or the manifest gives `user`. + +The manifest is read with the file: changing it is enough, no restart needed. + +## Publish a package + +**Folder or share feed:** copy the installer, and its manifest if it needs one, into the folder. + +**Web feed:** copy the files into the folder you publish, then regenerate its index and upload the folder: + +```powershell +& "$env:ProgramFiles\UniGetUI\UniGetUI.exe" pkg-index D:\publish\windows +``` + +The command reads every `.exe` and `.msi` in the folder, with their manifests, and writes +`index.json` next to them, with each package's identity, version, silent switches and SHA-256. +Run it again after every change. A file it cannot read is reported and left out of the index. + +A folder can hold the packages of both platforms. Run `pkg-index` on Windows for the `.exe` and +`.msi` files and on a Mac for the `.pkg`, `.dmg` and `.zip` files: each keeps the entries the other +wrote, as long as those files are unchanged. Windows entries are listed separately in `index.json`, +so Macs, including those on older UniGetUI releases, never offer a Windows installer, and Windows +never offers a Mac package. + +Each package shows the icon of its installer: an `.exe`'s own icon, or the product icon an `.msi` +gives Apps and Features (`ARPPRODUCTICON`, else its first shortcut's icon). To use another icon, +put a `.png`, `.jpg` or `.ico` with the same name next to the file (`Zoom.exe` → `Zoom.png`). + +To offer an update, publish the newer file. When the feed holds several versions of a package, +users get the newest one that runs on their computer, and a native build is preferred: on an Arm +computer an `arm64` build wins over an `x64` one, which wins over an `x86` one. + +## What users see + +- **Install** and **Update** copy the installer from the share (or download it), check its hash, + its identity and its signature, then run it silently. An installer for every user asks for + administrator rights through the UAC prompt; a per-user one installs without it. Once it runs it + cannot be canceled. +- **Uninstall** runs what the app registered in Apps and Features, made silent: `msiexec /x` for an + `.msi`, the app's silent uninstall command when it registers one, else its uninstaller with the + switches of its technology or of the manifest. An app UniGetUI cannot uninstall silently is refused. +- Some installers register a slightly different version than their own (with a build number added + or dropped). After UniGetUI installs a package, it remembers which version that left, so the same + update is not offered again. +- When the feed is unreachable (off the VPN, share not reachable), UniGetUI keeps showing the + packages it last listed. Installing needs the feed again. + +## Limits + +- An `.msi` must be a single file: one that installs from a cabinet beside it (`data1.cab`) or from + uncompressed files is skipped, with the reason shown by **Check feed** and `pkg-index`. +- An `.exe` without a version in its version resource needs `version` in its manifest. +- An `.exe` is recognized by name only when the manifest gives no `uninstallKey`, so an app renamed + in Apps and Features is not recognized without it. +- When the name matches only once a version is removed from it (`Microsoft .NET SDK 8.0.133`), the + entry must share the package's major version. Products installed side by side within one major + version (SDK feature bands, runtimes) cannot be told apart by name: give them an `uninstallKey` + or `upgradeCode`. +- InstallShield and other installers have no standard silent switches; their manifest must give them. +- There is no helper service as on macOS: an installer for every user always asks for administrator rights. diff --git a/src/Languages/lang_en.json b/src/Languages/lang_en.json index 18127a2bc5..75413d8934 100644 --- a/src/Languages/lang_en.json +++ b/src/Languages/lang_en.json @@ -257,6 +257,7 @@ "Catalog Editor is disabled": "Catalog Editor is disabled", "Enable the Catalog Editor in Experimental settings and developer options, then restart UniGetUI.": "Enable the Catalog Editor in Experimental settings and developer options, then restart UniGetUI.", "Apps (.pkg and .dmg) that your organization publishes in its software catalog.": "Apps (.pkg and .dmg) that your organization publishes in its software catalog.", + "Apps (.exe and .msi) that your organization publishes in its software catalog.": "Apps (.exe and .msi) that your organization publishes in its software catalog.", "Installer package (.pkg)": "Installer package (.pkg)", "Disk image (.dmg)": "Disk image (.dmg)", "Zip archive (.zip)": "Zip archive (.zip)", @@ -265,9 +266,11 @@ "{0} packages found, {1} files skipped.": "{0} packages found, {1} files skipped.", "The feed could not be read: {0}": "The feed could not be read: {0}", "Every .pkg, .dmg and .zip file in this folder or web address appears in the catalog. Leave it empty for a catalog without a feed.": "Every .pkg, .dmg and .zip file in this folder or web address appears in the catalog. Leave it empty for a catalog without a feed.", + "Every .exe and .msi file in this folder or web address appears in the catalog. Leave it empty for a catalog without a feed.": "Every .exe and .msi file in this folder or web address appears in the catalog. Leave it empty for a catalog without a feed.", "Package feed location": "Package feed location", "Choose folder...": "Choose folder...", "Allow packages that are not signed and notarized (for your own packages; keep the feed where only administrators can change it)": "Allow packages that are not signed and notarized (for your own packages; keep the feed where only administrators can change it)", + "Allow installers without a valid Authenticode signature (for your own packages; keep the feed where only administrators can change it)": "Allow installers without a valid Authenticode signature (for your own packages; keep the feed where only administrators can change it)", "Check feed": "Check feed", "Package feed": "Package feed", "Choose the folder of the package feed": "Choose the folder of the package feed", diff --git a/src/UniGetUI.Avalonia.slnx b/src/UniGetUI.Avalonia.slnx index 2f97496272..466acb20d3 100644 --- a/src/UniGetUI.Avalonia.slnx +++ b/src/UniGetUI.Avalonia.slnx @@ -143,6 +143,12 @@ + + + + + + diff --git a/src/UniGetUI.Avalonia/Models/SoftwareCatalog.cs b/src/UniGetUI.Avalonia/Models/SoftwareCatalog.cs index ff79b6bb7d..6bd664e67f 100644 --- a/src/UniGetUI.Avalonia/Models/SoftwareCatalog.cs +++ b/src/UniGetUI.Avalonia/Models/SoftwareCatalog.cs @@ -147,9 +147,9 @@ internal static bool HasUsableCatalog(string filePath) { try { - // A pkgFeed fills its catalog only on macOS, where the Installer packages manager exists. + // A pkgFeed fills its catalog only on macOS and Windows, where an Installer packages manager exists. return File.Exists(filePath) && Parse(File.ReadAllText(filePath)).Any(c => c.Packages.Length > 0 - || (OperatingSystem.IsMacOS() && !string.IsNullOrWhiteSpace(c.PkgFeed))); + || ((OperatingSystem.IsMacOS() || OperatingSystem.IsWindows()) && !string.IsNullOrWhiteSpace(c.PkgFeed))); } catch (Exception ex) when (ex is IOException or UnauthorizedAccessException or JsonException) { diff --git a/src/UniGetUI.Avalonia/Program.cs b/src/UniGetUI.Avalonia/Program.cs index 907146225d..5a722efd2d 100644 --- a/src/UniGetUI.Avalonia/Program.cs +++ b/src/UniGetUI.Avalonia/Program.cs @@ -2,7 +2,11 @@ using Avalonia; using UniGetUI.Avalonia.Infrastructure; using UniGetUI.Core.Data; -#if !WINDOWS +#if WINDOWS +using UniGetUI.Interface; +using UniGetUI.PackageEngine.Managers.InstallerFeeds; +using UniGetUI.PackageEngine.Managers.WinInstallerManager; +#else using UniGetUI.PackageEngine.Managers.MacPkgManager; #endif using UniGetUI.Tui; @@ -50,7 +54,14 @@ public static void Main(string[] args) return; } -#if !WINDOWS +#if WINDOWS + if (FeedIndexCommand.IsCommand(args)) + { + WindowsConsoleHost.PrepareCliIO(); + Environment.ExitCode = FeedIndexCommand.Run(args[1..], Console.Out, Console.Error, new WinInstallerIndexer()); + return; + } +#else if (PkgIndexCommand.IsCommand(args)) { Environment.ExitCode = PkgIndexCommand.Run(args[1..], Console.Out, Console.Error); diff --git a/src/UniGetUI.Avalonia/ViewModels/DialogPages/PkgFeedEditorViewModel.cs b/src/UniGetUI.Avalonia/ViewModels/DialogPages/PkgFeedEditorViewModel.cs index 9640addbb0..c528306e74 100644 --- a/src/UniGetUI.Avalonia/ViewModels/DialogPages/PkgFeedEditorViewModel.cs +++ b/src/UniGetUI.Avalonia/ViewModels/DialogPages/PkgFeedEditorViewModel.cs @@ -3,7 +3,10 @@ using CommunityToolkit.Mvvm.Input; using UniGetUI.Core.Logging; using UniGetUI.Core.Tools; -#if !WINDOWS +using UniGetUI.PackageEngine.Managers.InstallerFeeds; +#if WINDOWS +using UniGetUI.PackageEngine.Managers.WinInstallerManager; +#else using UniGetUI.PackageEngine.Managers.MacPkgManager; #endif @@ -13,7 +16,7 @@ namespace UniGetUI.Avalonia.ViewModels; public sealed record FeedPreviewRow(string Title, string Detail); /// -/// Edits a catalog's package feed (where IT drops macOS .pkg, .dmg and .zip files) and previews what +/// Edits a catalog's package feed (where IT drops .pkg, .dmg and .zip files for macOS, .exe and .msi for Windows) and previews what /// it publishes, so a typo or an unreadable file shows up before the catalog is deployed. /// public partial class PkgFeedEditorViewModel : ViewModelBase @@ -59,17 +62,21 @@ public PkgFeedEditorViewModel(string? location, bool allowUnsignedPackages, Func ? "" : CoreTools.Translate("Enter an https address or the full path of a folder."); - /// Previews read feeds with the macOS reader, which only macOS and Linux builds include. - public static bool CanPreview => !OperatingSystem.IsWindows(); + public static bool CanPreview => true; - private static bool IsValidLocation(string text) - { -#if WINDOWS - return Uri.TryCreate(text, UriKind.Absolute, out Uri? uri) && (uri.IsFile || uri.Scheme == Uri.UriSchemeHttps); -#else - return PkgFeedInspector.IsValidLocation(text); -#endif - } + public static string FeedDescription => OperatingSystem.IsWindows() + ? CoreTools.Translate("Every .exe and .msi file in this folder or web address appears in the catalog. Leave it empty for a catalog without a feed.") + : CoreTools.Translate("Every .pkg, .dmg and .zip file in this folder or web address appears in the catalog. Leave it empty for a catalog without a feed."); + + public static string UnsignedDescription => OperatingSystem.IsWindows() + ? CoreTools.Translate("Allow installers without a valid Authenticode signature (for your own packages; keep the feed where only administrators can change it)") + : CoreTools.Translate("Allow packages that are not signed and notarized (for your own packages; keep the feed where only administrators can change it)"); + + public static string LocationPlaceholder => OperatingSystem.IsWindows() + ? @"\\server\software\windows" + : "https://software.example.com/mac/"; + + private static bool IsValidLocation(string text) => FeedLocation.TryParse(text, out _); [RelayCommand] private async Task BrowseAsync() @@ -90,9 +97,12 @@ private async Task CheckAsync() Summary = CoreTools.Translate("Reading the feed..."); try { -#if !WINDOWS string location = Location.Trim(); +#if WINDOWS + var inspection = await Task.Run(() => WinInstallerFeedInspector.Inspect(location)); +#else var inspection = await Task.Run(() => PkgFeedInspector.Inspect(location)); +#endif foreach (var package in inspection.Packages.OrderBy(p => p.Name, StringComparer.CurrentCultureIgnoreCase)) Packages.Add(new FeedPreviewRow($"{package.Name} {package.Version}", $"{package.Id} · {package.Kind} · {package.File}" @@ -101,7 +111,6 @@ private async Task CheckAsync() Problems.Add(new FeedPreviewRow(problem.File, problem.Reason)); Warning = inspection.Warning ?? ""; Summary = CoreTools.Translate("{0} packages found, {1} files skipped.", Packages.Count, Problems.Count); -#endif } catch (Exception ex) { diff --git a/src/UniGetUI.Avalonia/ViewModels/SoftwarePages/SoftwareCatalogViewModel.cs b/src/UniGetUI.Avalonia/ViewModels/SoftwarePages/SoftwareCatalogViewModel.cs index 7cdd77ac90..eedf010a6b 100644 --- a/src/UniGetUI.Avalonia/ViewModels/SoftwarePages/SoftwareCatalogViewModel.cs +++ b/src/UniGetUI.Avalonia/ViewModels/SoftwarePages/SoftwareCatalogViewModel.cs @@ -229,7 +229,7 @@ public void LoadCatalogs(IReadOnlyList catalogs) _allPackages.Add(new CatalogTileViewModel(entry, catalog) { Owner = this, - SourceLabel = FindManager(entry)?.Id == PkgManagerId + SourceLabel = FindManager(entry)?.Id is { } managerId && PkgManagerIds.Contains(managerId) && feedNames.TryGetValue(entry.Source, out string? feedName) ? feedName : entry.Source, }); } @@ -409,8 +409,8 @@ public async Task RefreshAsync() private static IPackageManager? FindManager(CatalogEntry entry) => PEInterface.Managers.FirstOrDefault(entry.MatchesManager); - // The macOS Installer packages manager, which serves each catalog's pkgFeed as a source named by the catalog's id. - private const string PkgManagerId = "pkg"; + // The Installer packages manager (macOS .pkg, Windows .exe and .msi), which serves each catalog's pkgFeed as a source named by the catalog's id. + private static readonly string[] PkgManagerIds = ["pkg", "installer"]; /// /// Adds every package of a catalog's pkgFeed to that catalog, so that publishing a .pkg in the @@ -419,7 +419,7 @@ public async Task RefreshAsync() internal static async Task ExpandPkgFeedsAsync(CatalogDefinition[] catalogs) { if (catalogs.All(c => c.PkgFeed is null) - || PEInterface.Managers.FirstOrDefault(m => m.Id == PkgManagerId) is not { } manager + || PEInterface.Managers.FirstOrDefault(m => PkgManagerIds.Contains(m.Id)) is not { } manager || !manager.IsReady()) return catalogs; // A refresh shows what IT just published, not the feed listing kept for icon lookups. diff --git a/src/UniGetUI.Avalonia/Views/DialogPages/PkgFeedEditor.axaml b/src/UniGetUI.Avalonia/Views/DialogPages/PkgFeedEditor.axaml index 89b07ff250..2efde23ebe 100644 --- a/src/UniGetUI.Avalonia/Views/DialogPages/PkgFeedEditor.axaml +++ b/src/UniGetUI.Avalonia/Views/DialogPages/PkgFeedEditor.axaml @@ -8,12 +8,12 @@ + Text="{x:Static vm:PkgFeedEditorViewModel.FeedDescription}"/> internal static class PkgFeed { - internal const string IndexFileName = "index.json"; - private const int MaxScanDepth = 3; - private const int MaxIndexBytes = 8 * 1024 * 1024; - private static readonly TimeSpan IndexTimeout = TimeSpan.FromSeconds(60); - private static readonly TimeSpan IndexLifetime = TimeSpan.FromMinutes(5); private static readonly string[] IconExtensions = [".png", ".jpg", ".jpeg"]; private readonly record struct ParsedFile(long Size, DateTime WriteTimeUtc, PkgMetadata Metadata); - private readonly record struct CachedIndex(DateTime FetchedUtc, IReadOnlyList Items); private static readonly ConcurrentDictionary ParsedFiles = new(StringComparer.Ordinal); - private static readonly ConcurrentDictionary Indexes = new(); + private static readonly FeedListing Listing = new(ScanFolder, ParseIndex, "PkgFeeds"); - /// - /// Turns the location an administrator wrote (an https URL, a file URL or an absolute path) - /// into a feed address. Plain http is refused except on loopback, since the index pins hashes. - /// - public static bool TryParseLocation(string? text, out Uri location) - { - location = null!; - text = text?.Trim(); - if (string.IsNullOrEmpty(text)) - return false; - if (Path.IsPathRooted(text) && !text.Contains("://", StringComparison.Ordinal)) - { - location = new Uri(Path.GetFullPath(text)); - return true; - } - if (!Uri.TryCreate(text, UriKind.Absolute, out Uri? uri)) - return false; - if (uri.IsFile || uri.Scheme == Uri.UriSchemeHttps || (uri.Scheme == Uri.UriSchemeHttp && uri.IsLoopback)) - { - location = uri; - return true; - } - return false; - } - - public static void ForgetCachedIndexes() - { - Indexes.Clear(); - Folders.Clear(); - } - - private static readonly ConcurrentDictionary> LastKnown = new(); + public static void ForgetCachedIndexes() => Listing.ForgetCachedIndexes(); /// /// Lists a feed. When it cannot be reached (off the VPN, share not mounted) the last listing is @@ -86,61 +48,7 @@ public static void ForgetCachedIndexes() /// refuses that fallback, for what authorizes an install or checks a feed. /// public static IReadOnlyList List(Uri location, CancellationToken cancellationToken = default, - bool currentOnly = false) - { - try - { - var items = location.IsFile ? ScanFolderCached(location.LocalPath, cancellationToken) : ReadIndex(location, cancellationToken); - LastKnown[location] = items; - return items; - } - catch (Exception ex) when (!currentOnly && (ex is not OperationCanceledException || !cancellationToken.IsCancellationRequested)) - { - if (LastKnown.TryGetValue(location, out var known) || TryReadSavedIndex(location, out known)) - { - Logger.Warn($"The package feed {location} is unreachable ({ex.Message}); using its last known contents."); - return known; - } - throw; - } - } - - private static string SavedIndexPath(Uri indexUrl) => Path.Combine(CoreData.UniGetUICacheDirectory_Data, "PkgFeeds", - Convert.ToHexStringLower(SHA256.HashData(System.Text.Encoding.UTF8.GetBytes(indexUrl.AbsoluteUri))) + ".json"); - - private static void SaveIndex(Uri indexUrl, byte[] json) - { - try - { - string path = SavedIndexPath(indexUrl); - Directory.CreateDirectory(Path.GetDirectoryName(path)!); - File.WriteAllBytes(path, json); - } - catch (Exception ex) when (ex is IOException or UnauthorizedAccessException) - { - Logger.Warn($"Could not keep a copy of the package feed index {indexUrl}: {ex.Message}"); - } - } - - private static bool TryReadSavedIndex(Uri location, out IReadOnlyList items) - { - items = []; - if (location.IsFile) - return false; - try - { - Uri indexUrl = IndexUrl(location); - string path = SavedIndexPath(indexUrl); - if (!File.Exists(path)) - return false; - items = ParseIndex(File.ReadAllBytes(path), indexUrl); - return true; - } - catch (Exception ex) when (ex is IOException or UnauthorizedAccessException or JsonException or InvalidDataException) - { - return false; - } - } + bool currentOnly = false) => Listing.List(location, cancellationToken, currentOnly); /// /// The newest package of each identifier that this Mac can run, preferring a native build over @@ -186,20 +94,6 @@ public static int CompareReleases(PkgFeedItem a, PkgFeedItem b, Func Folders = new(StringComparer.Ordinal); - - private static IReadOnlyList ScanFolderCached(string directory, CancellationToken cancellationToken) - { - if (Folders.TryGetValue(directory, out var cached) && DateTime.UtcNow - cached.FetchedUtc < FolderLifetime) - return cached.Items; - var items = ScanFolder(directory, cancellationToken); - Folders[directory] = new CachedIndex(DateTime.UtcNow, items); - return items; - } - // A file read for the first time mounts a disk image or reads a package's table of contents, so // several are read at once to keep a cold scan of a large feed within the listing timeout. internal static IReadOnlyList ScanFolder(string directory, CancellationToken cancellationToken) @@ -214,20 +108,9 @@ internal static IReadOnlyList ScanFolder(string directory, Cancella return items.OfType().ToArray(); } - internal static IEnumerable EnumeratePackages(string directory) - { - var options = new EnumerationOptions - { - RecurseSubdirectories = true, - MaxRecursionDepth = MaxScanDepth, - IgnoreInaccessible = true, - AttributesToSkip = FileAttributes.Hidden | FileAttributes.System, - }; - // Bundle-style packages are directories; only flat packages, disk images and archives (files) are read. - return Directory.EnumerateFiles(directory, "*", options) - .Where(f => FeedFile.Extensions.Contains(Path.GetExtension(f), StringComparer.OrdinalIgnoreCase)) - .Order(StringComparer.Ordinal); - } + // Bundle-style packages are directories; only flat packages, disk images and archives (files) are read. + internal static IEnumerable EnumeratePackages(string directory) => + FeedLocation.EnumerateFiles(directory, FeedFile.Extensions); // Listings run in parallel at startup, and a second mount of a disk image that is already // attached comes back empty, so each file is read by one listing at a time. @@ -268,7 +151,7 @@ internal static PkgMetadata ReadFile(string file) if (metadata is null) { metadata = PkgMetadata.Read(file, File.Exists(icon) ? null : icon, iconFromPayload: false); - TryWriteFile(saved, PkgJson.SerializeToUtf8Bytes(metadata, PkgMetadataJsonContext.Default.PkgMetadata)); + TryWriteFile(saved, FeedJson.SerializeToUtf8Bytes(metadata, PkgMetadataJsonContext.Default.PkgMetadata)); } ParsedFiles[file] = new ParsedFile(info.Length, info.LastWriteTimeUtc, metadata); return metadata; @@ -350,7 +233,7 @@ private static string ExtractedIconPath(FileInfo package) => Path.Combine( try { return File.Exists(saved) - ? PkgJson.Deserialize(File.ReadAllBytes(saved), PkgMetadataJsonContext.Default.PkgMetadata) + ? FeedJson.Deserialize(File.ReadAllBytes(saved), PkgMetadataJsonContext.Default.PkgMetadata) : null; } catch (Exception ex) when (ex is IOException or UnauthorizedAccessException or JsonException) @@ -374,67 +257,20 @@ private static void TryWriteFile(string path, byte[] contents) // ── Web feeds ────────────────────────────────────────────────────────── - internal static Uri IndexUrl(Uri location) => - location.AbsolutePath.EndsWith(".json", StringComparison.OrdinalIgnoreCase) - ? location - : new Uri(location.AbsoluteUri.TrimEnd('/') + "/" + IndexFileName); - - private static IReadOnlyList ReadIndex(Uri location, CancellationToken cancellationToken) - { - Uri indexUrl = IndexUrl(location); - if (Indexes.TryGetValue(indexUrl, out var cached) && DateTime.UtcNow - cached.FetchedUtc < IndexLifetime) - return cached.Items; - - // The whole exchange has one deadline: the client's own timeout ends once the headers arrive. - using var deadline = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken); - deadline.CancelAfter(IndexTimeout); - using var limited = new MemoryStream(); - try - { - using var client = new HttpClient(CoreTools.GenericHttpClientParameters); - client.DefaultRequestHeaders.UserAgent.ParseAdd(CoreData.UserAgentString); - using var response = client.GetAsync(indexUrl, HttpCompletionOption.ResponseHeadersRead, deadline.Token) - .GetAwaiter().GetResult(); - if (response.StatusCode == HttpStatusCode.NotFound) - throw new FileNotFoundException($"The package feed has no index at {indexUrl}. Run 'unigetui pkg-index' on its folder."); - response.EnsureSuccessStatusCode(); - if (response.Content.Headers.ContentLength > MaxIndexBytes) - throw new InvalidDataException($"The package feed index at {indexUrl} is larger than {MaxIndexBytes} bytes."); - - using var body = response.Content.ReadAsStream(deadline.Token); - byte[] buffer = new byte[81920]; - int read; - while ((read = body.ReadAsync(buffer, deadline.Token).AsTask().GetAwaiter().GetResult()) > 0) - { - if (limited.Length + read > MaxIndexBytes) - throw new InvalidDataException($"The package feed index at {indexUrl} is larger than {MaxIndexBytes} bytes."); - limited.Write(buffer, 0, read); - } - } - catch (OperationCanceledException) when (!cancellationToken.IsCancellationRequested) - { - throw new IOException($"The package feed index at {indexUrl} did not arrive within {IndexTimeout.TotalSeconds} seconds."); - } - - byte[] json = limited.ToArray(); - var items = ParseIndex(json, indexUrl); - Indexes[indexUrl] = new CachedIndex(DateTime.UtcNow, items); - SaveIndex(indexUrl, json); - return items; - } - internal static IReadOnlyList ParseIndex(byte[] json, Uri indexUrl) { - var index = PkgJson.Deserialize(json, PkgFeedJsonContext.Default.PkgFeedIndex) + var index = FeedJson.Deserialize(json, FeedIndexJsonContext.Default.FeedIndex) ?? throw new InvalidDataException("The package feed index is empty."); - if (index.Version != PkgFeedIndex.CurrentVersion) + if (index.Version != FeedIndex.CurrentVersion) throw new InvalidDataException($"Unsupported package feed index version: {index.Version}."); var items = new List(); - foreach (var entry in index.Packages ?? []) + foreach (var entry in index.EntriesFor(FeedPlatform.MacOS)) { + if (entry is not null && !FeedLocation.HasExtension(entry.File ?? "", FeedFile.Extensions)) + continue; if (entry is null || string.IsNullOrWhiteSpace(entry.Id) || string.IsNullOrWhiteSpace(entry.Version) - || !IsSha256(entry.Sha256) || ResolveWithin(indexUrl, entry.File) is not { } file) + || !FeedLocation.IsSha256(entry.Sha256) || FeedLocation.ResolveWithin(indexUrl, entry.File) is not { } file) { Logger.Warn($"Skipping an invalid entry ({entry?.Id ?? "null"}) in the package feed index {indexUrl}."); continue; @@ -447,32 +283,10 @@ internal static IReadOnlyList ParseIndex(byte[] json, Uri indexUrl) entry.Sha256.ToLowerInvariant(), entry.Size, entry.Architectures ?? [], - ResolveWithin(indexUrl, entry.Icon), - entry.App?.ToInfo(), + FeedLocation.ResolveWithin(indexUrl, entry.Icon), + PkgIndexApp.ToInfo(entry.App), entry.WrapsPackage is true)); } return items; } - - // Index paths are relative and may not climb out of the feed, so an index cannot point - // installs at another host or scheme. - internal static Uri? ResolveWithin(Uri indexUrl, string? relative) - { - if (string.IsNullOrWhiteSpace(relative) || relative.Contains('\\') || relative.StartsWith('/') - || relative.Contains("://", StringComparison.Ordinal) - || relative.Split('/').Any(segment => segment is ".." or "." or "")) - return null; - var resolved = new Uri(indexUrl, string.Join('/', relative.Split('/').Select(Uri.EscapeDataString))); - string root = indexUrl.AbsoluteUri[..(indexUrl.AbsoluteUri.LastIndexOf('/') + 1)]; - return resolved.AbsoluteUri.StartsWith(root, StringComparison.Ordinal) ? resolved : null; - } - - internal static bool IsSha256(string? value) => - value is { Length: 64 } && value.All(Uri.IsHexDigit); - - internal static string ComputeSha256(string file) - { - using var stream = File.OpenRead(file); - return Convert.ToHexStringLower(SHA256.HashData(stream)); - } } diff --git a/src/UniGetUI.PackageEngine.Managers.MacPkg/Internal/PkgFeedIndex.cs b/src/UniGetUI.PackageEngine.Managers.MacPkg/Internal/PkgFeedIndex.cs deleted file mode 100644 index 1da7886a31..0000000000 --- a/src/UniGetUI.PackageEngine.Managers.MacPkg/Internal/PkgFeedIndex.cs +++ /dev/null @@ -1,85 +0,0 @@ -using System.Text.Json.Serialization; - -namespace UniGetUI.PackageEngine.Managers.MacPkgManager.Internal; - -/// -/// The index.json of a web feed, written by unigetui pkg-index next to the .pkg files. -/// A folder feed needs none: its packages are read directly. -/// -internal sealed class PkgFeedIndex -{ - public const int CurrentVersion = 1; - - [JsonPropertyName("version")] - public required int Version { get; init; } - - [JsonPropertyName("packages")] - public required PkgFeedIndexEntry[] Packages { get; init; } -} - -internal sealed class PkgFeedIndexEntry -{ - [JsonPropertyName("id")] - public required string Id { get; init; } - - [JsonPropertyName("name")] - public required string Name { get; init; } - - [JsonPropertyName("version")] - public required string Version { get; init; } - - // Relative to the index, with forward slashes. - [JsonPropertyName("file")] - public required string File { get; init; } - - [JsonPropertyName("sha256")] - public required string Sha256 { get; init; } - - [JsonPropertyName("size")] - public long Size { get; init; } - - [JsonPropertyName("architectures")] - [JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)] - public string[]? Architectures { get; init; } - - [JsonPropertyName("icon")] - [JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)] - public string? Icon { get; init; } - - // The app the package installs, which also marks it installed when it got there another way. - [JsonPropertyName("app")] - [JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)] - public PkgFeedIndexApp? App { get; init; } - - // A disk image that holds an installer package rather than an app. - [JsonPropertyName("wrapsPackage")] - [JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)] - public bool? WrapsPackage { get; init; } -} - -internal sealed class PkgFeedIndexApp -{ - [JsonPropertyName("id")] - public required string Id { get; init; } - - [JsonPropertyName("path")] - public required string Path { get; init; } - - [JsonPropertyName("version")] - [JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)] - public string? Version { get; init; } - - public static PkgFeedIndexApp? From(AppBundleInfo? app) => - app is null ? null : new() { Id = app.BundleId, Path = app.Path, Version = app.Version }; - - // Only an absolute .app path is kept: it is read to detect the app, never written to. - public AppBundleInfo? ToInfo() => - !string.IsNullOrWhiteSpace(Id) && Path is { Length: > 1 } path && path.StartsWith('/') - && path.TrimEnd('/').EndsWith(".app", StringComparison.OrdinalIgnoreCase) && !path.Split('/').Contains("..") - ? new AppBundleInfo(Id.Trim(), path.TrimEnd('/'), string.IsNullOrWhiteSpace(Version) ? null : Version.Trim()) - : null; -} - -[JsonSourceGenerationOptions(WriteIndented = true)] -[JsonSerializable(typeof(PkgFeedIndex))] -internal partial class PkgFeedJsonContext : JsonSerializerContext; diff --git a/src/UniGetUI.PackageEngine.Managers.MacPkg/Internal/PkgHelperClient.cs b/src/UniGetUI.PackageEngine.Managers.MacPkg/Internal/PkgHelperClient.cs index 45b9053e77..71b535e169 100644 --- a/src/UniGetUI.PackageEngine.Managers.MacPkg/Internal/PkgHelperClient.cs +++ b/src/UniGetUI.PackageEngine.Managers.MacPkg/Internal/PkgHelperClient.cs @@ -3,6 +3,7 @@ using System.Text.Json; using System.Text.Json.Serialization; using UniGetUI.PackageEngine.Enums; +using UniGetUI.PackageEngine.Managers.InstallerFeeds; namespace UniGetUI.PackageEngine.Managers.MacPkgManager.Internal; @@ -117,7 +118,7 @@ internal static class PkgHelperClient await using var stream = new NetworkStream(socket, ownsSocket: false); var request = new PkgHelperRequest { Catalog = catalog, Id = item.Id, Version = item.Version }; await stream.WriteAsync(Encoding.UTF8.GetBytes( - PkgJson.Serialize(request, PkgHelperJsonContext.Default.PkgHelperRequest) + "\n")); + FeedJson.Serialize(request, PkgHelperJsonContext.Default.PkgHelperRequest) + "\n")); log("Installing through the UniGetUI package helper, which needs no administrator password..."); // Once sent, the install is not canceled: the helper runs it as root to the end, as a prompt would. @@ -146,7 +147,7 @@ await stream.WriteAsync(Encoding.UTF8.GetBytes( try { if (line.StartsWith('{')) - message = PkgJson.Deserialize(line, PkgHelperJsonContext.Default.PkgHelperMessage); + message = FeedJson.Deserialize(line, PkgHelperJsonContext.Default.PkgHelperMessage); } catch (JsonException) { } if (message is null) diff --git a/src/UniGetUI.PackageEngine.Managers.MacPkg/Internal/PkgIndexApp.cs b/src/UniGetUI.PackageEngine.Managers.MacPkg/Internal/PkgIndexApp.cs new file mode 100644 index 0000000000..8446389324 --- /dev/null +++ b/src/UniGetUI.PackageEngine.Managers.MacPkg/Internal/PkgIndexApp.cs @@ -0,0 +1,16 @@ +using UniGetUI.PackageEngine.Managers.InstallerFeeds; + +namespace UniGetUI.PackageEngine.Managers.MacPkgManager.Internal; + +internal static class PkgIndexApp +{ + public static FeedIndexApp? From(AppBundleInfo? app) => + app is null ? null : new() { Id = app.BundleId, Path = app.Path, Version = app.Version }; + + // Only an absolute .app path is kept: it is read to detect the app, never written to. + public static AppBundleInfo? ToInfo(FeedIndexApp? app) => + app is not null && !string.IsNullOrWhiteSpace(app.Id) && app.Path is { Length: > 1 } path && path.StartsWith('/') + && path.TrimEnd('/').EndsWith(".app", StringComparison.OrdinalIgnoreCase) && !path.Split('/').Contains("..") + ? new AppBundleInfo(app.Id.Trim(), path.TrimEnd('/'), string.IsNullOrWhiteSpace(app.Version) ? null : app.Version.Trim()) + : null; +} diff --git a/src/UniGetUI.PackageEngine.Managers.MacPkg/Internal/PkgInstaller.cs b/src/UniGetUI.PackageEngine.Managers.MacPkg/Internal/PkgInstaller.cs index c4da48bba9..1cb38bffd2 100644 --- a/src/UniGetUI.PackageEngine.Managers.MacPkg/Internal/PkgInstaller.cs +++ b/src/UniGetUI.PackageEngine.Managers.MacPkg/Internal/PkgInstaller.cs @@ -1,8 +1,7 @@ using System.Diagnostics; -using System.Security.Cryptography; -using UniGetUI.Core.Data; using UniGetUI.Core.Tools; using UniGetUI.PackageEngine.Enums; +using UniGetUI.PackageEngine.Managers.InstallerFeeds; namespace UniGetUI.PackageEngine.Managers.MacPkgManager.Internal; @@ -85,7 +84,7 @@ private static async Task InstallAsRootProcessAsync(string ex { progress(OperationProgress.ForStage(OperationProgressStage.Installing)); log("Asking for administrator rights; the package is then downloaded, checked and installed as root..."); - string order = PkgJson.Serialize(PkgElevatedInstall.From(item, allowUnsigned), + string order = FeedJson.Serialize(PkgElevatedInstall.From(item, allowUnsigned), PkgHelperJsonContext.Default.PkgElevatedInstall); string command = $"{CoreTools.QuoteForPosixShell(executable)} {PkgHelperCommand.Command} " + $"{PkgHelperCommand.InstallItemVerb} {CoreTools.QuoteForPosixShell(order)} 2>&1"; @@ -214,63 +213,12 @@ internal static async Task ExtractAppAsync(string zip, string destinatio } /// Copies a folder-feed package, or downloads a web-feed one, to the given path. - public static async Task StageAsync(PkgFeedItem item, string destination, Action log, - Action progress, CancellationToken cancellationToken) - { - progress(OperationProgress.ForStage(OperationProgressStage.Downloading)); - if (item.Location.IsFile) - { - log($"Copying {item.Location.LocalPath}..."); - await using var source = File.OpenRead(item.Location.LocalPath); - await using var target = File.Create(destination); - await CopyAsync(source, target, source.Length, progress, cancellationToken); - return; - } - - log($"Downloading {item.Location}..."); - using var client = new HttpClient(CoreTools.GenericHttpClientParameters); - client.DefaultRequestHeaders.UserAgent.ParseAdd(CoreData.UserAgentString); - using var response = await client.GetAsync(item.Location, HttpCompletionOption.ResponseHeadersRead, cancellationToken); - response.EnsureSuccessStatusCode(); - await using (var body = await response.Content.ReadAsStreamAsync(cancellationToken)) - await using (var target = File.Create(destination)) - await CopyAsync(body, target, response.Content.Headers.ContentLength ?? item.Size, progress, cancellationToken); - } - - private static async Task CopyAsync(Stream source, Stream target, long total, Action progress, - CancellationToken cancellationToken) - { - byte[] buffer = new byte[1024 * 1024]; - long copied = 0; - using var stalled = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken); - while (true) - { - // A source that stops sending must not hold the install, nor the helper's one install at a time. - stalled.CancelAfter(StallTimeout); - int read; - try { read = await source.ReadAsync(buffer, stalled.Token); } - catch (OperationCanceledException) when (!cancellationToken.IsCancellationRequested) - { - throw new IOException($"The package stopped arriving for {StallTimeout.TotalSeconds} seconds."); - } - if (read == 0) - break; - await target.WriteAsync(buffer.AsMemory(0, read), cancellationToken); - copied += read; - progress(OperationProgress.FromDownload((ulong)copied, (ulong)Math.Max(total, 0))); - } - } + public static Task StageAsync(PkgFeedItem item, string destination, Action log, + Action progress, CancellationToken cancellationToken) => + FeedStaging.StageAsync(item.Location, item.Size, destination, log, progress, cancellationToken); /// Refuses a staged file whose hash differs from the one the feed index announces. - internal static void VerifyHash(PkgFeedItem item, string staged) - { - if (item.Sha256 is not { } expected) - return; - using var stream = File.OpenRead(staged); - string actual = Convert.ToHexStringLower(SHA256.HashData(stream)); - if (!string.Equals(actual, expected, StringComparison.OrdinalIgnoreCase)) - throw new InvalidDataException($"The downloaded package's SHA-256 is {actual}, but the feed index announces {expected}."); - } + internal static void VerifyHash(PkgFeedItem item, string staged) => FeedStaging.VerifyHash(item.Sha256, staged); /// Refuses a file that is no longer what the feed listed, such as a folder feed's file replaced since. internal static void VerifyIdentity(PkgFeedItem item, PkgMetadata staged) @@ -281,7 +229,6 @@ internal static void VerifyIdentity(PkgFeedItem item, PkgMetadata staged) } internal const string SpctlPath = "/usr/sbin/spctl"; - private static readonly TimeSpan StallTimeout = TimeSpan.FromMinutes(2); /// /// Asks Gatekeeper, as macOS does for downloads, whether the package or app is signed by an identified diff --git a/src/UniGetUI.PackageEngine.Managers.MacPkg/MacPkg.cs b/src/UniGetUI.PackageEngine.Managers.MacPkg/MacPkg.cs index 70d5dbb340..a676f48574 100644 --- a/src/UniGetUI.PackageEngine.Managers.MacPkg/MacPkg.cs +++ b/src/UniGetUI.PackageEngine.Managers.MacPkg/MacPkg.cs @@ -5,6 +5,7 @@ using UniGetUI.PackageEngine.Classes.Manager; using UniGetUI.PackageEngine.Interfaces; using UniGetUI.PackageEngine.ManagerClasses.Manager; +using UniGetUI.PackageEngine.Managers.InstallerFeeds; using UniGetUI.PackageEngine.Managers.MacPkgManager.Internal; using UniGetUI.PackageEngine.PackageClasses; @@ -62,7 +63,7 @@ public MacPkg() DefaultSource = new ManagerSource(this, "Installer packages", new Uri("file:///")), }; - SourcesHelper = new MacPkgSourceHelper(this); + SourcesHelper = new InstallerFeedSourceHelper(this); DetailsHelper = new MacPkgDetailsHelper(this); OperationHelper = new MacPkgOperationHelper(this); } @@ -85,7 +86,7 @@ public override void RefreshPackageIndexes() { PkgFeed.ForgetCachedIndexes(); // A catalog edited since the last listing may name other feeds. - ((MacPkgSourceHelper)SourcesHelper).ForgetSources(); + ((InstallerFeedSourceHelper)SourcesHelper).ForgetSources(); } // ── Listing ──────────────────────────────────────────────────────────── @@ -110,8 +111,8 @@ public override void RefreshPackageIndexes() } /// The feed package behind a package, at the requested version or else the newest one. - internal PkgFeedSource? FindFeedSource(IPackage package) => - SourcesHelper.GetSources().OfType().FirstOrDefault(s => s.Name == package.Source.Name); + internal InstallerFeedSource? FindFeedSource(IPackage package) => + SourcesHelper.GetSources().OfType().FirstOrDefault(s => s.Name == package.Source.Name); internal PkgFeedItem? FindFeedItem(IPackage package, string? version = null, bool currentOnly = false) { diff --git a/src/UniGetUI.PackageEngine.Managers.MacPkg/PkgFeedInspector.cs b/src/UniGetUI.PackageEngine.Managers.MacPkg/PkgFeedInspector.cs index 4704c91daa..b5d1654802 100644 --- a/src/UniGetUI.PackageEngine.Managers.MacPkg/PkgFeedInspector.cs +++ b/src/UniGetUI.PackageEngine.Managers.MacPkg/PkgFeedInspector.cs @@ -1,18 +1,8 @@ +using UniGetUI.PackageEngine.Managers.InstallerFeeds; using UniGetUI.PackageEngine.Managers.MacPkgManager.Internal; namespace UniGetUI.PackageEngine.Managers.MacPkgManager; -/// A package a feed publishes, as the Catalog Editor previews it. -public sealed record FeedPackage(string Name, string Id, string Version, string File, string Kind, - IReadOnlyList Architectures); - -/// A file in a feed that UniGetUI cannot offer, and why. -public sealed record FeedProblem(string File, string Reason); - -/// What a feed offers: its packages, the files it skips, and a warning about where it lives. -public sealed record FeedInspection(IReadOnlyList Packages, IReadOnlyList Problems, - string? Warning); - /// /// Previews a feed for the administrator writing a catalog: everything it publishes, every file it /// cannot read with the reason, and whether the root helper would accept a folder feed. @@ -20,12 +10,12 @@ public sealed record FeedInspection(IReadOnlyList Packages, IReadOn public static class PkgFeedInspector { /// Whether a feed location is usable: an https URL, a file URL or an absolute folder path. - public static bool IsValidLocation(string? text) => PkgFeed.TryParseLocation(text, out _); + public static bool IsValidLocation(string? text) => FeedLocation.TryParse(text, out _); /// Reads the feed fresh. Throws when the feed itself cannot be reached or its index read. public static FeedInspection Inspect(string text) { - if (!PkgFeed.TryParseLocation(text, out Uri location)) + if (!FeedLocation.TryParse(text, out Uri location)) throw new InvalidDataException($"\"{text}\" is not an https URL or an absolute folder path."); PkgFeed.ForgetCachedIndexes(); if (!location.IsFile) diff --git a/src/UniGetUI.PackageEngine.Managers.MacPkg/PkgHelperCommand.cs b/src/UniGetUI.PackageEngine.Managers.MacPkg/PkgHelperCommand.cs index 031948b073..eb5cb15f2c 100644 --- a/src/UniGetUI.PackageEngine.Managers.MacPkg/PkgHelperCommand.cs +++ b/src/UniGetUI.PackageEngine.Managers.MacPkg/PkgHelperCommand.cs @@ -3,6 +3,7 @@ using System.Security; using System.Text; using System.Text.Json; +using UniGetUI.PackageEngine.Managers.InstallerFeeds; using UniGetUI.PackageEngine.Managers.MacPkgManager.Internal; namespace UniGetUI.PackageEngine.Managers.MacPkgManager; @@ -151,12 +152,12 @@ internal static string DaemonPlist(string executable) => $""" private static int InstallItem(string json, TextWriter output) { void Write(PkgHelperMessage message) => - output.WriteLine(PkgJson.Serialize(message, PkgHelperJsonContext.Default.PkgHelperMessage)); + output.WriteLine(FeedJson.Serialize(message, PkgHelperJsonContext.Default.PkgHelperMessage)); PkgHelperMessage result; try { - var order = PkgJson.Deserialize(json, PkgHelperJsonContext.Default.PkgElevatedInstall) + var order = FeedJson.Deserialize(json, PkgHelperJsonContext.Default.PkgElevatedInstall) ?? throw new InvalidDataException("The install order is empty."); result = InstallItemAsync(order.ToItem(), order.AllowUnsigned, text => Write(new PkgHelperMessage { Log = text })) .GetAwaiter().GetResult(); @@ -223,7 +224,7 @@ private static async Task HandleAsync(Socket client, Action journal) async Task Send(PkgHelperMessage message) { byte[] line = Encoding.UTF8.GetBytes( - PkgJson.Serialize(message, PkgHelperJsonContext.Default.PkgHelperMessage) + "\n"); + FeedJson.Serialize(message, PkgHelperJsonContext.Default.PkgHelperMessage) + "\n"); await writeLock.WaitAsync(); try { @@ -242,7 +243,7 @@ async Task Send(PkgHelperMessage message) { using var timeout = new CancellationTokenSource(TimeSpan.FromSeconds(10)); string line = await ReadLineAsync(stream, timeout.Token); - var request = PkgJson.Deserialize(line, PkgHelperJsonContext.Default.PkgHelperRequest) + var request = FeedJson.Deserialize(line, PkgHelperJsonContext.Default.PkgHelperRequest) ?? throw new InvalidDataException("The request is empty."); journal($"Asked to install {request.Id} {request.Version} from catalog {request.Catalog}."); await OneInstallAtATime.WaitAsync(); @@ -298,9 +299,9 @@ internal static async Task InstallAsync(PkgHelperRequest reque string version = request.Version ?? ""; if (!IsField(request.Catalog) || !IsField(request.Id) || (version.Length > 0 && !IsField(version))) return Refuse("The request names no valid catalog, package or version."); - if (TrustedPath.WhyUntrusted(PkgCatalogFeeds.CatalogPath) is { } catalogReason) + if (TrustedPath.WhyUntrusted(CatalogFeeds.CatalogPath) is { } catalogReason) return Refuse($"The software catalog is not protected, so the helper does not act on it: {catalogReason}"); - var feeds = PkgCatalogFeeds.Load() + var feeds = CatalogFeeds.Load() .Where(f => string.Equals(f.Id, request.Catalog, StringComparison.OrdinalIgnoreCase)).Take(1).ToArray(); if (feeds.Length == 0) return Refuse($"No catalog with the id {request.Catalog} declares a package feed."); diff --git a/src/UniGetUI.PackageEngine.Managers.MacPkg/PkgIndexCommand.cs b/src/UniGetUI.PackageEngine.Managers.MacPkg/PkgIndexCommand.cs index a58da31fc5..45c6956f66 100644 --- a/src/UniGetUI.PackageEngine.Managers.MacPkg/PkgIndexCommand.cs +++ b/src/UniGetUI.PackageEngine.Managers.MacPkg/PkgIndexCommand.cs @@ -1,76 +1,53 @@ +using UniGetUI.PackageEngine.Managers.InstallerFeeds; using UniGetUI.PackageEngine.Managers.MacPkgManager.Internal; namespace UniGetUI.PackageEngine.Managers.MacPkgManager; /// -/// unigetui pkg-index <folder>: writes the index.json that serves a folder of .pkg and .dmg files -/// as a web feed, with each package's identifier, version, app and SHA-256 read from the files themselves. +/// unigetui pkg-index <folder> on a Mac: indexes the .pkg, .dmg and .zip files of a folder, with +/// each package's identifier, version, app and SHA-256 read from the files themselves. /// -public static class PkgIndexCommand +public sealed class PkgIndexCommand : IFeedIndexer { - public const string Command = "pkg-index"; + public const string Command = FeedIndexCommand.Command; - public static bool IsCommand(string[] args) => - args.Length > 0 && string.Equals(args[0], Command, StringComparison.OrdinalIgnoreCase); + public static bool IsCommand(string[] args) => FeedIndexCommand.IsCommand(args); - public static int Run(string[] args, TextWriter output, TextWriter error) - { - if (args.Length != 1 || args[0] is "-h" or "--help") - { - output.WriteLine($"Usage: unigetui {Command} "); - output.WriteLine("Writes index.json into , listing every .pkg and .dmg in it and its subfolders,"); - output.WriteLine("so the folder can be published on a web server as a package feed."); - return args.Length == 1 ? 0 : 2; - } - - string folder = Path.GetFullPath(args[0]); - if (!Directory.Exists(folder)) - { - error.WriteLine($"The folder {folder} does not exist."); - return 1; - } + public static int Run(string[] args, TextWriter output, TextWriter error) => + FeedIndexCommand.Run(args, output, error, new PkgIndexCommand()); - var (index, skipped) = Build(folder); - string destination = Path.Combine(folder, PkgFeed.IndexFileName); - string temporary = destination + ".tmp"; - File.WriteAllText(temporary, PkgJson.Serialize(index, PkgFeedJsonContext.Default.PkgFeedIndex) + Environment.NewLine); - File.Move(temporary, destination, overwrite: true); + public FeedPlatform Platform => FeedPlatform.MacOS; - foreach (var entry in index.Packages) - output.WriteLine($" {entry.File}: {entry.Name} ({entry.Id} {entry.Version})"); - foreach (string file in skipped) - error.WriteLine($" Skipped {Path.GetRelativePath(folder, file)}: it is not a readable flat package or a disk image with one app."); - output.WriteLine($"Wrote {destination} with {index.Packages.Length} package(s)."); - return skipped.Count == 0 ? 0 : 3; - } + public IReadOnlyCollection Extensions => FeedFile.Extensions; - internal static (PkgFeedIndex Index, IReadOnlyList Skipped) Build(string folder) + public (IReadOnlyList Entries, IReadOnlyList Skipped) Build(string folder) { - var entries = new List(); - var skipped = new List(); + var entries = new List(); + var skipped = new List(); foreach (string file in PkgFeed.EnumeratePackages(folder)) { if (PkgFeed.TryReadFile(file) is not { } metadata) { - skipped.Add(file); + skipped.Add(new FeedProblem(Path.GetRelativePath(folder, file), + "it is not a readable flat package or a disk image with one app.")); continue; } string? icon = PublishIcon(file); - entries.Add(new PkgFeedIndexEntry + entries.Add(new FeedIndexEntry { Id = metadata.Id, Name = metadata.Title ?? Path.GetFileNameWithoutExtension(file), Version = metadata.Version, - File = RelativeUrlPath(folder, file), - Sha256 = PkgFeed.ComputeSha256(file), + File = FeedLocation.RelativeUrlPath(folder, file), + Sha256 = FeedLocation.ComputeSha256(file), Size = new FileInfo(file).Length, Architectures = metadata.Architectures.Count > 0 ? [.. metadata.Architectures] : null, - Icon = icon is null ? null : RelativeUrlPath(folder, icon), - App = PkgFeedIndexApp.From(metadata.App), + Icon = icon is null ? null : FeedLocation.RelativeUrlPath(folder, icon), + App = PkgIndexApp.From(metadata.App), WrapsPackage = metadata.WrapsPackage ? true : null, }); } - return (new PkgFeedIndex { Version = PkgFeedIndex.CurrentVersion, Packages = [.. entries] }, skipped); + return (entries, skipped); } // A web feed serves only what is in the folder, so an icon taken from a disk image is saved @@ -84,7 +61,4 @@ internal static (PkgFeedIndex Index, IReadOnlyList Skipped) Build(string File.Copy(icon, sibling, overwrite: false); return sibling; } - - private static string RelativeUrlPath(string folder, string file) => - Path.GetRelativePath(folder, file).Replace(Path.DirectorySeparatorChar, '/'); } diff --git a/src/UniGetUI.PackageEngine.Managers.MacPkg/UniGetUI.PackageEngine.Managers.MacPkg.csproj b/src/UniGetUI.PackageEngine.Managers.MacPkg/UniGetUI.PackageEngine.Managers.MacPkg.csproj index a0903da9f6..8a3e2321b9 100644 --- a/src/UniGetUI.PackageEngine.Managers.MacPkg/UniGetUI.PackageEngine.Managers.MacPkg.csproj +++ b/src/UniGetUI.PackageEngine.Managers.MacPkg/UniGetUI.PackageEngine.Managers.MacPkg.csproj @@ -15,6 +15,7 @@ + diff --git a/src/UniGetUI.PackageEngine.Managers.WinInstaller/Helpers/WinInstallerDetailsHelper.cs b/src/UniGetUI.PackageEngine.Managers.WinInstaller/Helpers/WinInstallerDetailsHelper.cs new file mode 100644 index 0000000000..e27d33ec37 --- /dev/null +++ b/src/UniGetUI.PackageEngine.Managers.WinInstaller/Helpers/WinInstallerDetailsHelper.cs @@ -0,0 +1,53 @@ +using UniGetUI.Core.IconEngine; +using UniGetUI.Core.Tools; +using UniGetUI.PackageEngine.Classes.Manager.BaseProviders; +using UniGetUI.PackageEngine.Interfaces; +using UniGetUI.PackageEngine.Managers.WinInstallerManager.Internal; + +namespace UniGetUI.PackageEngine.Managers.WinInstallerManager; + +internal sealed class WinInstallerDetailsHelper : BasePkgDetailsHelper +{ + private readonly WinInstaller _manager; + + public WinInstallerDetailsHelper(WinInstaller manager) + : base(manager) { _manager = manager; } + + protected override void GetDetails_UnSafe(IPackageDetails details) + { + var item = _manager.FindFeedItem(details.Package); + if (item is null) + return; + details.Version = item.Version; + details.Publisher = item.Details.Publisher; + details.InstallerUrl = item.Location; + details.InstallerHash = item.Sha256; + details.InstallerType = InstallerKinds.Describe(item.Details.Kind); + details.InstallerSize = item.Size; + details.ManifestUrl = item.Location.IsFile ? null : item.Location; + details.Description = CoreTools.Translate("Published by your organization in {0}.", details.Package.Source.Name); + } + + protected override CacheableIcon? GetIcon_UnSafe(IPackage package) + { + var item = _manager.FindFeedItem(package); + var icon = item?.Icon + ?? (item?.Location.IsFile == true && WinFeed.FindIcon(item.Location.LocalPath) is { } extracted + ? new Uri(extracted) + : null); + if (icon is null) + return null; + return icon.IsFile ? new CacheableIcon(icon.LocalPath) : new CacheableIcon(icon, package.VersionString); + } + + protected override IReadOnlyList GetScreenshots_UnSafe(IPackage package) => []; + + protected override string? GetInstallLocation_UnSafe(IPackage package) => null; + + protected override IReadOnlyList GetInstallableVersions_UnSafe(IPackage package) => + _manager.ListFeedItems(package) + .Select(i => i.Version) + .Distinct(StringComparer.Ordinal) + .OrderDescending(_manager.VersionOrder) + .ToArray(); +} diff --git a/src/UniGetUI.PackageEngine.Managers.WinInstaller/Helpers/WinInstallerOperationHelper.cs b/src/UniGetUI.PackageEngine.Managers.WinInstaller/Helpers/WinInstallerOperationHelper.cs new file mode 100644 index 0000000000..a61a4d80b4 --- /dev/null +++ b/src/UniGetUI.PackageEngine.Managers.WinInstaller/Helpers/WinInstallerOperationHelper.cs @@ -0,0 +1,80 @@ +using UniGetUI.PackageEngine.Classes.Manager.BaseProviders; +using UniGetUI.PackageEngine.Enums; +using UniGetUI.PackageEngine.Interfaces; +using UniGetUI.PackageEngine.Interfaces.ManagerProviders; +using UniGetUI.PackageEngine.Managers.WinInstallerManager.Internal; +using UniGetUI.PackageEngine.Serializable; + +namespace UniGetUI.PackageEngine.Managers.WinInstallerManager; + +internal sealed class WinInstallerOperationHelper : BasePkgOperationHelper, IManagedPackageOperationHelper +{ + private readonly WinInstaller _manager; + + public WinInstallerOperationHelper(WinInstaller manager) + : base(manager) { _manager = manager; } + + protected override IReadOnlyList _getOperationParameters( + IPackage package, InstallOptions options, OperationType operation) => + operation == OperationType.Uninstall ? ["/x", package.Id] : ["/i", package.Id]; + + protected override IReadOnlyList _getOperationParameters( + IPackage package, InstallOptions options, OperationType operation, bool standalone) => + standalone + ? throw new InvalidOperationException("Installer packages are installed by UniGetUI itself and have no command line of their own.") + : _getOperationParameters(package, options, operation); + + protected override OperationVeredict _getOperationResult( + IPackage package, OperationType operation, IReadOnlyList processOutput, int returnCode) => + returnCode == 0 ? OperationVeredict.Success : OperationVeredict.Failure; + + internal static string? TargetVersion(IPackage package, InstallOptions options, OperationType operation) => + operation == OperationType.Update + ? package.NewVersionString.Length > 0 && package.NewVersionString != package.VersionString ? package.NewVersionString : null + : options.Version.Length > 0 ? options.Version : null; + + public async Task TryPerformAsync(IPackage package, InstallOptions options, + OperationType operation, Action log, Action progress, + CancellationToken cancellationToken) => + await Task.Run(() => operation == OperationType.Uninstall + ? UninstallAsync(package, log, progress) + : InstallAsync(package, options, operation, log, progress, cancellationToken), CancellationToken.None); + + private async Task InstallAsync(IPackage package, InstallOptions options, + OperationType operation, Action log, Action progress, + CancellationToken cancellationToken) + { + string? version = TargetVersion(package, options, operation); + _manager.RefreshPackageIndexes(); + var item = _manager.FindFeedItem(package, version, currentOnly: true) + ?? throw new InvalidOperationException( + $"The package feed {package.Source.Name} does not publish {package.Id}{(version is null ? "" : " " + version)} for this computer."); + + bool allowUnsigned = _manager.FindFeedSource(package)?.AllowUnsignedPackages is true; + var verdict = await WinInstallerRunner.InstallAsync(item, allowUnsigned, log, progress, cancellationToken); + if (verdict == OperationVeredict.Success) + { + if (InstalledMatcher.Find(item, ArpEntries.Read()) is { } entry) + InstallReceipts.Record(item.Id, entry, item.Version); + else + log($"{item.Name} installed, but UniGetUI does not find it in Apps and Features."); + _manager.InvalidateInstalledPackages(); + } + return verdict; + } + + private async Task UninstallAsync(IPackage package, Action log, Action progress) + { + var installed = _manager.ListInstalled() + .FirstOrDefault(p => string.Equals(p.Item.Id, package.Id, StringComparison.OrdinalIgnoreCase)); + if (installed is null) + { + log($"{package.Name} is not listed in Apps and Features."); + return OperationVeredict.Failure; + } + var verdict = await WinInstallerRunner.UninstallAsync(package.Name, installed.Item.Details, installed.Entry, log, progress); + if (verdict == OperationVeredict.Success) + _manager.InvalidateInstalledPackages(); + return verdict; + } +} diff --git a/src/UniGetUI.PackageEngine.Managers.WinInstaller/Internal/ArpEntries.cs b/src/UniGetUI.PackageEngine.Managers.WinInstaller/Internal/ArpEntries.cs new file mode 100644 index 0000000000..c75979114c --- /dev/null +++ b/src/UniGetUI.PackageEngine.Managers.WinInstaller/Internal/ArpEntries.cs @@ -0,0 +1,86 @@ +using Microsoft.Win32; + +namespace UniGetUI.PackageEngine.Managers.WinInstallerManager.Internal; + +internal sealed record ArpEntry( + string Key, + bool PerMachine, + string? DisplayName, + string? DisplayVersion, + string? Publisher, + string? UninstallString, + string? QuietUninstallString, + bool WindowsInstaller, + bool Hidden, + IReadOnlyList BundleUpgradeCodes); + +internal static class ArpEntries +{ + private const string UninstallKey = @"SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall"; + + private static readonly (RegistryHive Hive, RegistryView View)[] Roots = + [ + (RegistryHive.LocalMachine, RegistryView.Registry64), + (RegistryHive.LocalMachine, RegistryView.Registry32), + (RegistryHive.CurrentUser, RegistryView.Registry64), + (RegistryHive.CurrentUser, RegistryView.Registry32), + ]; + + internal static Func> Source { get; set; } = ReadRegistry; + + public static IReadOnlyList Read() => Source(); + + internal static Func ProductAssignment { get; set; } = + productCode => MsiDatabase.ProductInfo(productCode, "AssignmentType"); + + internal static IReadOnlyList ReadRegistry() + { + var entries = new List(); + var seen = new HashSet<(bool, string)>(); + foreach (var (hive, view) in Roots) + { + try + { + using var root = RegistryKey.OpenBaseKey(hive, view); + using var uninstall = root.OpenSubKey(UninstallKey); + if (uninstall is null) + continue; + foreach (string name in uninstall.GetSubKeyNames()) + { + bool perMachine = hive == RegistryHive.LocalMachine; + if (!perMachine && !seen.Add((perMachine, name))) + continue; + using var key = uninstall.OpenSubKey(name); + if (key is null) + continue; + bool windowsInstaller = key.GetValue("WindowsInstaller") is int installer && installer != 0; + if (windowsInstaller && InstallerDetails.Guid(name) is { } productCode) + { + if (ProductAssignment(productCode) is not { } assignment) + continue; + perMachine = assignment == "1"; + } + entries.Add(new ArpEntry(name, perMachine, + Text(key, "DisplayName"), Text(key, "DisplayVersion"), Text(key, "Publisher"), + Text(key, "UninstallString"), Text(key, "QuietUninstallString"), + windowsInstaller, + (key.GetValue("SystemComponent") is int system && system != 0) || Text(key, "ParentKeyName") is not null, + key.GetValue("BundleUpgradeCode") switch + { + string[] codes => codes.Select(InstallerDetails.Guid).OfType().ToArray(), + string code when InstallerDetails.Guid(code) is { } guid => [guid], + _ => [], + })); + } + } + catch (Exception ex) when (ex is IOException or UnauthorizedAccessException or System.Security.SecurityException) + { + UniGetUI.Core.Logging.Logger.Warn($"Could not read the installed programs of {hive} ({view}): {ex.Message}"); + } + } + return entries; + } + + private static string? Text(RegistryKey key, string name) => + key.GetValue(name) is string value && !string.IsNullOrWhiteSpace(value) ? value.Trim() : null; +} diff --git a/src/UniGetUI.PackageEngine.Managers.WinInstaller/Internal/Authenticode.cs b/src/UniGetUI.PackageEngine.Managers.WinInstaller/Internal/Authenticode.cs new file mode 100644 index 0000000000..0c03bb16b5 --- /dev/null +++ b/src/UniGetUI.PackageEngine.Managers.WinInstaller/Internal/Authenticode.cs @@ -0,0 +1,156 @@ +using System.Runtime.InteropServices; +using System.Security.Cryptography; +using System.Security.Cryptography.X509Certificates; + +namespace UniGetUI.PackageEngine.Managers.WinInstallerManager.Internal; + +internal readonly record struct SignatureCheck(int Status, string? Signer) +{ + public bool IsValid => Status == 0; + + public string Describe() => Status switch + { + 0 => $"signed by {Signer ?? "an unknown publisher"}", + unchecked((int)0x800B0100) => "not signed", + unchecked((int)0x80096010) => "its signature does not match its contents", + unchecked((int)0x800B0109) => "signed with a certificate this computer does not trust", + unchecked((int)0x800B0101) => "signed with an expired certificate", + unchecked((int)0x800B010C) => "signed with a revoked certificate", + unchecked((int)0x800B0111) => "signed by a publisher this computer distrusts", + _ => $"its signature could not be verified (0x{Status:X8})", + }; +} + +internal static partial class Authenticode +{ + internal static Func Verify { get; set; } = VerifyFile; + + private const uint UiNone = 2; + private const uint RevokeWholeChain = 1; + private const uint ChoiceFile = 1; + private const uint StateActionVerify = 1; + private const uint StateActionClose = 2; + private const uint RevocationCheckChain = 0x40; + private static readonly Guid GenericVerifyV2 = new("00AAC56B-CD44-11d0-8CC2-00C04FC295EE"); + + internal static SignatureCheck VerifyFile(string path) + { + nint pathPointer = Marshal.StringToHGlobalUni(path); + nint fileInfoPointer = Marshal.AllocHGlobal(Marshal.SizeOf()); + try + { + Marshal.StructureToPtr(new FileInfo + { + cbStruct = (uint)Marshal.SizeOf(), + pcwszFilePath = pathPointer, + }, fileInfoPointer, false); + var data = new TrustData + { + cbStruct = (uint)Marshal.SizeOf(), + dwUIChoice = UiNone, + fdwRevocationChecks = RevokeWholeChain, + dwUnionChoice = ChoiceFile, + pFile = fileInfoPointer, + dwStateAction = StateActionVerify, + dwProvFlags = RevocationCheckChain, + }; + Guid action = GenericVerifyV2; + int status = WinVerifyTrust(nint.Zero, ref action, ref data); + string? signer = status == 0 ? ReadSigner(data.hWVTStateData) : null; + data.dwStateAction = StateActionClose; + WinVerifyTrust(nint.Zero, ref action, ref data); + return new SignatureCheck(status, signer); + } + finally + { + Marshal.DestroyStructure(fileInfoPointer); + Marshal.FreeHGlobal(fileInfoPointer); + Marshal.FreeHGlobal(pathPointer); + } + } + + private static string? ReadSigner(nint state) + { + nint provider = state == nint.Zero ? nint.Zero : WTHelperProvDataFromStateData(state); + if (provider == nint.Zero) + return null; + nint signer = WTHelperGetProvSignerFromChain(provider, 0, false, 0); + nint certificate = signer == nint.Zero ? nint.Zero : WTHelperGetProvCertFromChain(signer, 0); + if (certificate == nint.Zero) + return null; + var entry = Marshal.PtrToStructure(certificate); + if (entry.pCert == nint.Zero) + return null; + var context = Marshal.PtrToStructure(entry.pCert); + if (context.pbCertEncoded == nint.Zero || context.cbCertEncoded == 0) + return null; + byte[] encoded = new byte[context.cbCertEncoded]; + Marshal.Copy(context.pbCertEncoded, encoded, 0, encoded.Length); + try + { + using var parsed = X509CertificateLoader.LoadCertificate(encoded); + return parsed.GetNameInfo(X509NameType.SimpleName, false); + } + catch (CryptographicException) + { + return null; + } + } + + [StructLayout(LayoutKind.Sequential)] + private struct FileInfo + { + public uint cbStruct; + public nint pcwszFilePath; + public nint hFile; + public nint pgKnownSubject; + } + + [StructLayout(LayoutKind.Sequential)] + private struct TrustData + { + public uint cbStruct; + public nint pPolicyCallbackData; + public nint pSipClientData; + public uint dwUIChoice; + public uint fdwRevocationChecks; + public uint dwUnionChoice; + public nint pFile; + public uint dwStateAction; + public nint hWVTStateData; + public nint pwszUrlReference; + public uint dwProvFlags; + public uint dwUIContext; + public nint pSignatureSettings; + } + +#pragma warning disable CS0649 + private struct ProviderCertificate + { + public uint cbStruct; + public nint pCert; + } + + private struct CertificateContext + { + public uint dwCertEncodingType; + public nint pbCertEncoded; + public uint cbCertEncoded; + public nint pCertInfo; + public nint hCertStore; + } +#pragma warning restore CS0649 + + [LibraryImport("wintrust.dll", EntryPoint = "WinVerifyTrust")] + private static partial int WinVerifyTrust(nint window, ref Guid action, ref TrustData data); + + [LibraryImport("wintrust.dll", EntryPoint = "WTHelperProvDataFromStateData")] + private static partial nint WTHelperProvDataFromStateData(nint state); + + [LibraryImport("wintrust.dll", EntryPoint = "WTHelperGetProvSignerFromChain")] + private static partial nint WTHelperGetProvSignerFromChain(nint provider, uint signerIndex, + [MarshalAs(UnmanagedType.Bool)] bool counterSigner, uint counterSignerIndex); + + [LibraryImport("wintrust.dll", EntryPoint = "WTHelperGetProvCertFromChain")] + private static partial nint WTHelperGetProvCertFromChain(nint signer, uint certificateIndex); +} diff --git a/src/UniGetUI.PackageEngine.Managers.WinInstaller/Internal/InstallReceipts.cs b/src/UniGetUI.PackageEngine.Managers.WinInstaller/Internal/InstallReceipts.cs new file mode 100644 index 0000000000..11576bd8cb --- /dev/null +++ b/src/UniGetUI.PackageEngine.Managers.WinInstaller/Internal/InstallReceipts.cs @@ -0,0 +1,79 @@ +using System.Text.Json; +using System.Text.Json.Serialization; +using UniGetUI.Core.Data; +using UniGetUI.Core.Logging; +using UniGetUI.PackageEngine.Managers.InstallerFeeds; + +namespace UniGetUI.PackageEngine.Managers.WinInstallerManager.Internal; + +internal static class InstallReceipts +{ + private static readonly Lock Gate = new(); + + internal static string? TestPathOverride { get; set; } + + private static string FilePath => + TestPathOverride ?? Path.Combine(CoreData.UniGetUIDataDirectory, "InstallerFeeds", "receipts.json"); + + public static string InstalledVersion(string packageId, ArpEntry entry) + { + string registered = entry.DisplayVersion ?? "0"; + lock (Gate) + { + return Load().FirstOrDefault(r => string.Equals(r.Id, packageId, StringComparison.OrdinalIgnoreCase) + && string.Equals(r.Key, entry.Key, StringComparison.OrdinalIgnoreCase) + && r.RegisteredVersion == registered)?.FeedVersion ?? registered; + } + } + + public static void Record(string packageId, ArpEntry entry, string feedVersion) + { + lock (Gate) + { + var receipts = Load().Where(r => !string.Equals(r.Id, packageId, StringComparison.OrdinalIgnoreCase)).ToList(); + if (entry.DisplayVersion != feedVersion) + receipts.Add(new InstallReceipt { Id = packageId, Key = entry.Key, RegisteredVersion = entry.DisplayVersion ?? "0", FeedVersion = feedVersion }); + try + { + Directory.CreateDirectory(Path.GetDirectoryName(FilePath)!); + File.WriteAllBytes(FilePath, FeedJson.SerializeToUtf8Bytes(receipts.ToArray(), InstallReceiptJsonContext.Default.InstallReceiptArray)); + } + catch (Exception ex) when (ex is IOException or UnauthorizedAccessException) + { + Logger.Warn($"Could not remember the installed version of {packageId}: {ex.Message}"); + } + } + } + + private static InstallReceipt[] Load() + { + try + { + return File.Exists(FilePath) + ? FeedJson.Deserialize(File.ReadAllBytes(FilePath), InstallReceiptJsonContext.Default.InstallReceiptArray) ?? [] + : []; + } + catch (Exception ex) when (ex is IOException or UnauthorizedAccessException or JsonException) + { + return []; + } + } +} + +internal sealed class InstallReceipt +{ + [JsonPropertyName("id")] + public required string Id { get; init; } + + [JsonPropertyName("key")] + public required string Key { get; init; } + + [JsonPropertyName("registeredVersion")] + public required string RegisteredVersion { get; init; } + + [JsonPropertyName("feedVersion")] + public required string FeedVersion { get; init; } +} + +[JsonSerializable(typeof(InstallReceipt[]))] +internal partial class InstallReceiptJsonContext : JsonSerializerContext; diff --git a/src/UniGetUI.PackageEngine.Managers.WinInstaller/Internal/InstalledMatcher.cs b/src/UniGetUI.PackageEngine.Managers.WinInstaller/Internal/InstalledMatcher.cs new file mode 100644 index 0000000000..793f781c3b --- /dev/null +++ b/src/UniGetUI.PackageEngine.Managers.WinInstaller/Internal/InstalledMatcher.cs @@ -0,0 +1,99 @@ +using System.Text; +using System.Text.RegularExpressions; +using UniGetUI.Core.Tools; + +namespace UniGetUI.PackageEngine.Managers.WinInstallerManager.Internal; + +internal static partial class InstalledMatcher +{ + internal static Func> RelatedProducts { get; set; } = MsiDatabase.RelatedProducts; + + public static ArpEntry? Find(WinFeedItem item, IReadOnlyList entries) + { + var details = item.Details; + if (details.UninstallKey is { } key) + return Prefer(entries.Where(e => string.Equals(e.Key, key, StringComparison.OrdinalIgnoreCase)), details); + + if (details.UpgradeCode is { } upgradeCode) + { + var products = details.Kind == InstallerKind.Msi ? RelatedProducts(upgradeCode) : []; + if (Prefer(entries.Where(e => products.Contains(e.Key, StringComparer.OrdinalIgnoreCase) + || e.BundleUpgradeCodes.Contains(upgradeCode, StringComparer.OrdinalIgnoreCase)), details) is { } related) + return related; + } + + if (details.ProductCode is { } productCode + && Prefer(entries.Where(e => string.Equals(e.Key, productCode, StringComparison.OrdinalIgnoreCase)), details) is { } product) + return product; + + return details.Kind == InstallerKind.Msi ? null : FindByName(item, entries); + } + + private static ArpEntry? Prefer(IEnumerable entries, InstallerDetails details) => + entries.OrderByDescending(e => e.PerMachine != details.PerUser).ThenByDescending(e => e, NewestFirst).FirstOrDefault(); + + private static readonly IComparer NewestFirst = Comparer.Create((a, b) => + CoreTools.VersionStringToStruct(a.DisplayVersion ?? "").CompareTo(CoreTools.VersionStringToStruct(b.DisplayVersion ?? ""))); + + private static ArpEntry? FindByName(WinFeedItem item, IReadOnlyList entries) + { + var targets = new[] { item.Details.DisplayName, item.Name } + .Select(Normalize).Where(t => t.Length >= 2).Distinct().ToArray(); + if (targets.Length == 0) + return null; + ArpEntry? best = null; + int bestScore = 0; + foreach (var entry in entries) + { + if (entry.Hidden || entry.DisplayName is null || !PublishersAgree(item.Details.Publisher, entry.Publisher)) + continue; + string exact = Normalize(entry.DisplayName); + string cleaned = Normalize(InstallerFile.CleanName(entry.DisplayName)); + int score = targets.Contains(exact) ? 4 : targets.Contains(cleaned) ? 2 : 0; + bool innoKey = item.Details.Kind == InstallerKind.Inno && entry.Key.EndsWith("_is1", StringComparison.OrdinalIgnoreCase); + if (score == 0 || (score == 2 && !innoKey && !SameMajorVersion(entry.DisplayName, item.Version))) + continue; + if (innoKey) + score++; + if (entry.PerMachine != item.Details.PerUser) + score++; + if (score > bestScore || (score == bestScore && best is not null && NewestFirst.Compare(entry, best) > 0)) + (best, bestScore) = (entry, score); + } + return best; + } + + [GeneratedRegex(@"\d+(?:\.\d+)+")] + private static partial Regex VersionInName(); + + internal static bool SameMajorVersion(string displayName, string version) + { + var inName = VersionInName().Matches(displayName); + if (inName.Count == 0) + return true; + string major = inName[^1].Value.Split('.')[0].TrimStart('0'); + return major == version.Split('.')[0].Trim().TrimStart('0'); + } + + internal static bool PublishersAgree(string? feed, string? installed) + { + string a = Normalize(FirstWord(feed)), b = Normalize(FirstWord(installed)); + return a.Length == 0 || b.Length == 0 || a == b; + } + + private static string? FirstWord(string? text) => + text?.Split([' ', ',', '.'], StringSplitOptions.RemoveEmptyEntries).FirstOrDefault(); + + internal static string Normalize(string? text) + { + if (text is null) + return ""; + var builder = new StringBuilder(text.Length); + foreach (char c in text.Normalize(NormalizationForm.FormKD)) + { + if (char.IsLetterOrDigit(c) || c == '+' || c == '#') + builder.Append(char.ToLowerInvariant(c)); + } + return builder.ToString(); + } +} diff --git a/src/UniGetUI.PackageEngine.Managers.WinInstaller/Internal/InstallerKind.cs b/src/UniGetUI.PackageEngine.Managers.WinInstaller/Internal/InstallerKind.cs new file mode 100644 index 0000000000..a33720725b --- /dev/null +++ b/src/UniGetUI.PackageEngine.Managers.WinInstaller/Internal/InstallerKind.cs @@ -0,0 +1,81 @@ +using System.Text; + +namespace UniGetUI.PackageEngine.Managers.WinInstallerManager.Internal; + +internal enum InstallerKind +{ + Msi, + Inno, + Nsis, + Burn, + InstallShield, + Exe, +} + +internal static class InstallerKinds +{ + public static string Name(InstallerKind kind) => kind switch + { + InstallerKind.Msi => "msi", + InstallerKind.Inno => "inno", + InstallerKind.Nsis => "nsis", + InstallerKind.Burn => "burn", + InstallerKind.InstallShield => "installshield", + _ => "exe", + }; + + public static InstallerKind? Parse(string? name) => name?.Trim().ToLowerInvariant() switch + { + "msi" => InstallerKind.Msi, + "inno" => InstallerKind.Inno, + "nsis" => InstallerKind.Nsis, + "burn" or "wix" => InstallerKind.Burn, + "installshield" => InstallerKind.InstallShield, + "exe" => InstallerKind.Exe, + _ => null, + }; + + public static string Describe(InstallerKind kind) => kind switch + { + InstallerKind.Msi => "Windows Installer (.msi)", + InstallerKind.Inno => "Inno Setup (.exe)", + InstallerKind.Nsis => "NSIS (.exe)", + InstallerKind.Burn => "WiX bundle (.exe)", + InstallerKind.InstallShield => "InstallShield (.exe)", + _ => "Installer (.exe)", + }; + + private static readonly byte[] InnoSetupData = Encoding.ASCII.GetBytes("Inno Setup Setup Data"); + private static readonly byte[] NsisHeader = Encoding.ASCII.GetBytes("NullsoftInst"); + + public static InstallerKind Detect(PeInfo pe) + { + if (pe.Sections.Contains(".wixburn", StringComparer.Ordinal)) + return InstallerKind.Burn; + if (pe.OverlayHead.AsSpan().IndexOf(InnoSetupData) >= 0 + || pe.String("Comments")?.Contains("Inno Setup", StringComparison.OrdinalIgnoreCase) == true) + return InstallerKind.Inno; + if (pe.OverlayHead.AsSpan().IndexOf(NsisHeader) >= 0) + return InstallerKind.Nsis; + if (pe.Strings.Values.Any(v => v.Contains("InstallShield", StringComparison.OrdinalIgnoreCase))) + return InstallerKind.InstallShield; + return InstallerKind.Exe; + } + + public static string? InstallArguments(InstallerKind kind) => kind switch + { + InstallerKind.Msi => "/qn /norestart", + InstallerKind.Inno => "/VERYSILENT /SUPPRESSMSGBOXES /NORESTART /SP-", + InstallerKind.Nsis => "/S", + InstallerKind.Burn => "/quiet /norestart", + _ => null, + }; + + public static string? UninstallArguments(InstallerKind kind) => kind switch + { + InstallerKind.Inno => "/VERYSILENT /SUPPRESSMSGBOXES /NORESTART", + InstallerKind.Nsis => "/S", + InstallerKind.Burn => "/quiet /norestart", + _ => null, + }; +} diff --git a/src/UniGetUI.PackageEngine.Managers.WinInstaller/Internal/InstallerMetadata.cs b/src/UniGetUI.PackageEngine.Managers.WinInstaller/Internal/InstallerMetadata.cs new file mode 100644 index 0000000000..6b7ff829e9 --- /dev/null +++ b/src/UniGetUI.PackageEngine.Managers.WinInstaller/Internal/InstallerMetadata.cs @@ -0,0 +1,295 @@ +using System.Runtime.InteropServices; +using System.Text.Json; +using System.Text.Json.Serialization; +using System.Text.RegularExpressions; +using UniGetUI.PackageEngine.Managers.InstallerFeeds; + +namespace UniGetUI.PackageEngine.Managers.WinInstallerManager.Internal; + +internal sealed record InstallerDetails( + InstallerKind Kind, + string? Publisher = null, + string? ProductCode = null, + string? UpgradeCode = null, + string? UninstallKey = null, + string? DisplayName = null, + string? InstallArguments = null, + string? UninstallArguments = null, + string? Scope = null) +{ + public bool PerUser => Scope == UserScope; + + public bool PerMachine => Scope == MachineScope; + + internal const string UserScope = "user"; + internal const string MachineScope = "machine"; + + public FeedIndexWindows ToIndex() => new() + { + Installer = InstallerKinds.Name(Kind), + Publisher = Publisher, + ProductCode = ProductCode, + UpgradeCode = UpgradeCode, + UninstallKey = UninstallKey, + DisplayName = DisplayName, + InstallArguments = InstallArguments, + UninstallArguments = UninstallArguments, + Scope = Scope, + }; + + public static InstallerDetails? FromIndex(FeedIndexWindows? index) => + index is null || InstallerKinds.Parse(index.Installer) is not { } kind + ? null + : new InstallerDetails(kind, Clean(index.Publisher), Guid(index.ProductCode), Guid(index.UpgradeCode), + Clean(index.UninstallKey), Clean(index.DisplayName), Clean(index.InstallArguments), Clean(index.UninstallArguments), + ParseScope(index.Scope)); + + internal static string? Clean(string? value) => string.IsNullOrWhiteSpace(value) ? null : value.Trim(); + + internal static string? Guid(string? value) => + System.Guid.TryParse(value, out Guid guid) ? guid.ToString("B").ToUpperInvariant() : null; + + internal static string? ParseScope(string? scope) => scope?.Trim().ToLowerInvariant() switch + { + UserScope => UserScope, + MachineScope => MachineScope, + _ => null, + }; +} + +internal sealed record InstallerMetadata( + string Id, + string Name, + string Version, + IReadOnlyList Architectures, + InstallerDetails Details, + byte[]? Icon = null); + +internal static partial class InstallerFile +{ + public static readonly string[] Extensions = [".exe", ".msi"]; + private const int MaxManifestBytes = 64 * 1024; + + public static bool IsMsi(string path) => Path.GetExtension(path).Equals(".msi", StringComparison.OrdinalIgnoreCase); + + public static bool IsUninstaller(string path) => + Path.GetFileName(path).Contains("uninst", StringComparison.OrdinalIgnoreCase); + + public static string ManifestPath(string file) => Path.ChangeExtension(file, ".json"); + + public static InstallerMetadata Read(string file) + { + var manifest = ReadManifest(file); + string stem = Path.GetFileNameWithoutExtension(file); + return IsMsi(file) + ? FromMsi(MsiDatabase.Read(file), manifest, stem) + : FromExe(PeReader.Read(file), manifest, stem); + } + + internal static InstallerManifest? ReadManifest(string file) + { + string path = ManifestPath(file); + if (!File.Exists(path)) + return null; + if (new FileInfo(path).Length > MaxManifestBytes) + throw new InvalidDataException($"Its manifest {Path.GetFileName(path)} is larger than {MaxManifestBytes} bytes."); + try + { + return FeedJson.Deserialize(File.ReadAllBytes(path), InstallerManifestJsonContext.Default.InstallerManifest); + } + catch (JsonException ex) + { + throw new InvalidDataException($"Its manifest {Path.GetFileName(path)} is not valid: {ex.Message}"); + } + } + + internal static InstallerMetadata FromExe(PeInfo pe, InstallerManifest? manifest, string stem) + { + var kind = InstallerKinds.Parse(manifest?.Installer) ?? InstallerKinds.Detect(pe); + if (kind == InstallerKind.Msi) + throw new InvalidDataException("An .exe cannot be installed as an .msi."); + string name = InstallerDetails.Clean(manifest?.Name) + ?? CleanName(pe.String("ProductName")) ?? CleanName(pe.String("FileDescription"), isProductName: false) + ?? CleanName(stem, isProductName: false) ?? stem; + string version = InstallerDetails.Clean(manifest?.Version) + ?? CleanVersion(pe.String("ProductVersion")) ?? CleanVersion(pe.FixedProductVersion) + ?? CleanVersion(pe.String("FileVersion")) ?? CleanVersion(pe.FixedFileVersion) + ?? throw new InvalidDataException($"Its version resource gives no version. Write it as \"version\" in {stem}.json."); + string arguments = InstallerDetails.Clean(manifest?.InstallArguments) ?? InstallerKinds.InstallArguments(kind) + ?? throw new InvalidDataException($"UniGetUI does not know how to run this {InstallerKinds.Describe(kind)} silently. " + + $"Write its silent switches as \"installArguments\" in {stem}.json."); + IReadOnlyList architectures = manifest?.Architectures is { } listed ? Architectures(listed) + : pe.Machine switch + { + PeReader.MachineAmd64 => ["x64"], + PeReader.MachineArm64 => ["arm64"], + _ => [], + }; + var details = new InstallerDetails(kind, + InstallerDetails.Clean(manifest?.Publisher) ?? pe.String("CompanyName"), + UpgradeCode: InstallerDetails.Guid(manifest?.UpgradeCode), + UninstallKey: InstallerDetails.Clean(manifest?.UninstallKey), + DisplayName: InstallerDetails.Clean(manifest?.DisplayName), + InstallArguments: arguments, + UninstallArguments: InstallerDetails.Clean(manifest?.UninstallArguments), + Scope: InstallerDetails.ParseScope(manifest?.Scope)); + return new InstallerMetadata(InstallerDetails.Clean(manifest?.Id) ?? MakeId(name), name, version, architectures, details, pe.Icon); + } + + internal static InstallerMetadata FromMsi(MsiInfo msi, InstallerManifest? manifest, string stem) + { + string? productCode = InstallerDetails.Guid(msi.Property("ProductCode")) + ?? throw new InvalidDataException("It has no ProductCode."); + if (msi.ExternalMedia is { } external) + throw new InvalidDataException($"It installs files that are not inside it ({external}). Publish a single-file .msi, with its cabinets embedded."); + string? upgradeCode = InstallerDetails.Guid(manifest?.UpgradeCode) ?? InstallerDetails.Guid(msi.Property("UpgradeCode")); + string version = InstallerDetails.Clean(manifest?.Version) ?? msi.Property("ProductVersion") + ?? throw new InvalidDataException("It has no ProductVersion."); + string? productName = msi.Property("ProductName"); + string name = InstallerDetails.Clean(manifest?.Name) ?? CleanName(productName) ?? stem; + string? scope = InstallerDetails.ParseScope(manifest?.Scope) + ?? (msi.Property("ALLUSERS") is null || (msi.Property("ALLUSERS") == "2" && msi.Property("MSIINSTALLPERUSER") == "1") + ? InstallerDetails.UserScope + : msi.Property("ALLUSERS") == "1" ? InstallerDetails.MachineScope : null); + var details = new InstallerDetails(InstallerKind.Msi, + InstallerDetails.Clean(manifest?.Publisher) ?? msi.Property("Manufacturer"), + productCode, + upgradeCode, + InstallerDetails.Clean(manifest?.UninstallKey), + InstallerDetails.Clean(manifest?.DisplayName) ?? productName, + InstallerDetails.Clean(manifest?.InstallArguments) ?? InstallerKinds.InstallArguments(InstallerKind.Msi), + InstallerDetails.Clean(manifest?.UninstallArguments), + scope); + string id = InstallerDetails.Clean(manifest?.Id) ?? upgradeCode ?? productCode; + IReadOnlyList architectures = manifest?.Architectures is { } listed ? Architectures(listed) + : TemplateArchitectures(msi.Template); + return new InstallerMetadata(id, name, version, architectures, details, msi.Icon); + } + + internal static IReadOnlyList TemplateArchitectures(string? template) + { + string platform = (template ?? "").Split(';')[0].Trim(); + return platform.ToLowerInvariant() switch + { + "x64" or "amd64" => ["x64"], + "arm64" => ["arm64"], + "intel" or "" => ["x86"], + _ => [platform.ToLowerInvariant()], + }; + } + + internal static IReadOnlyList Architectures(IEnumerable listed) => + listed.OfType().Select(a => a.Trim().ToLowerInvariant() switch + { + "amd64" or "x86_64" => "x64", + "aarch64" => "arm64", + "i386" or "i686" => "x86", + var other => other, + }).Where(a => a.Length > 0).Distinct().ToArray(); + + public static int ArchitectureFit(IReadOnlyList architectures, Architecture machine) + { + if (architectures.Count == 0) + return 1; + int best = 0; + foreach (string architecture in architectures) + { + int fit = architecture switch + { + "x86" => machine == Architecture.X86 ? 3 : machine is Architecture.X64 or Architecture.Arm64 ? 1 : 0, + "x64" => machine == Architecture.X64 ? 3 : machine == Architecture.Arm64 ? 2 : 0, + "arm64" => machine == Architecture.Arm64 ? 3 : 0, + _ => 0, + }; + best = Math.Max(best, fit); + } + return best; + } + + [GeneratedRegex(@"^\s*v?(\d+(?:\s*[.,]\s*\d+)*)", RegexOptions.IgnoreCase)] + private static partial Regex LeadingVersion(); + + internal static string? CleanVersion(string? text) + { + if (string.IsNullOrWhiteSpace(text) || LeadingVersion().Match(text) is not { Success: true } match) + return null; + string version = string.Concat(match.Groups[1].Value.Where(c => !char.IsWhiteSpace(c))).Replace(',', '.'); + return version.Split('.').All(part => part.TrimStart('0').Length == 0) ? null : version; + } + + [GeneratedRegex(@"(\s*\([^()]*\b(?:x64|x86|arm64|64-bit|32-bit|bit|edition)\b[^()]*\)|[\s._-]+(?:(?:version\s+\d+(?:\.\d+)*|v?\d+(?:\.\d+)+)[a-z]?|x64|x86|arm64|win(?:32|64)?|64-bit|32-bit))\s*$", + RegexOptions.IgnoreCase)] + private static partial Regex TrailingVersionOrArchitecture(); + + [GeneratedRegex(@"[\s._-]+(?:setup|installer|install)\s*$", RegexOptions.IgnoreCase)] + private static partial Regex TrailingSetupWord(); + + internal static string? CleanName(string? text, bool isProductName = true) + { + if (string.IsNullOrWhiteSpace(text)) + return null; + string name = text.Trim(); + for (string previous = ""; previous != name;) + { + previous = name; + string shorter = TrailingVersionOrArchitecture().Replace(name, "").Trim(); + if (!isProductName) + shorter = TrailingSetupWord().Replace(shorter, "").Trim(); + if (shorter.Length > 0) + name = shorter; + } + return name.Length > 0 ? name : null; + } + + [GeneratedRegex(@"[^\p{L}\p{M}\p{N}+_-]+")] + private static partial Regex IdSeparators(); + + internal static string MakeId(string name) + { + string id = IdSeparators().Replace(name.Normalize(System.Text.NormalizationForm.FormC), ".").Trim('.', '-'); + return id.Length > 0 ? id : "package"; + } +} + +internal sealed class InstallerManifest +{ + [JsonPropertyName("id")] + public string? Id { get; init; } + + [JsonPropertyName("name")] + public string? Name { get; init; } + + [JsonPropertyName("version")] + public string? Version { get; init; } + + [JsonPropertyName("architectures")] + public string?[]? Architectures { get; init; } + + [JsonPropertyName("installer")] + public string? Installer { get; init; } + + [JsonPropertyName("publisher")] + public string? Publisher { get; init; } + + [JsonPropertyName("upgradeCode")] + public string? UpgradeCode { get; init; } + + [JsonPropertyName("uninstallKey")] + public string? UninstallKey { get; init; } + + [JsonPropertyName("displayName")] + public string? DisplayName { get; init; } + + [JsonPropertyName("installArguments")] + public string? InstallArguments { get; init; } + + [JsonPropertyName("uninstallArguments")] + public string? UninstallArguments { get; init; } + + [JsonPropertyName("scope")] + public string? Scope { get; init; } +} + +[JsonSourceGenerationOptions(ReadCommentHandling = JsonCommentHandling.Skip, AllowTrailingCommas = true)] +[JsonSerializable(typeof(InstallerManifest))] +internal partial class InstallerManifestJsonContext : JsonSerializerContext; diff --git a/src/UniGetUI.PackageEngine.Managers.WinInstaller/Internal/MsiDatabase.cs b/src/UniGetUI.PackageEngine.Managers.WinInstaller/Internal/MsiDatabase.cs new file mode 100644 index 0000000000..ed1ec63d96 --- /dev/null +++ b/src/UniGetUI.PackageEngine.Managers.WinInstaller/Internal/MsiDatabase.cs @@ -0,0 +1,223 @@ +using System.Runtime.InteropServices; + +namespace UniGetUI.PackageEngine.Managers.WinInstallerManager.Internal; + +internal sealed record MsiInfo(IReadOnlyDictionary Properties, string? Template, byte[]? Icon = null, + string? ExternalMedia = null) +{ + public string? Property(string name) => + Properties.TryGetValue(name, out string? value) && !string.IsNullOrWhiteSpace(value) ? value.Trim() : null; +} + +internal static unsafe partial class MsiDatabase +{ + private const uint Success = 0; + private const uint MoreData = 234; + private const uint TemplateProperty = 7; + private const int GuidLength = 38; + private const int MaxIconBytes = 4 * 1024 * 1024; + + public static MsiInfo Read(string path) + { + uint result = MsiOpenDatabase(path, 0, out nint database); + if (result != Success) + throw new InvalidDataException($"It could not be opened as a Windows Installer package (error {result})."); + try + { + var properties = new Dictionary(StringComparer.Ordinal); + if (MsiDatabaseOpenView(database, "SELECT `Property`, `Value` FROM `Property`", out nint view) != Success) + throw new InvalidDataException("It has no Property table."); + try + { + if (MsiViewExecute(view, 0) != Success) + throw new InvalidDataException("Its Property table could not be read."); + while (MsiViewFetch(view, out nint record) == Success) + { + try + { + string? name = RecordString(record, 1); + if (!string.IsNullOrEmpty(name) && properties.Count < 10000) + properties[name] = RecordString(record, 2) ?? ""; + } + finally { MsiCloseHandle(record); } + } + } + finally { MsiCloseHandle(view); } + return new MsiInfo(properties, SummaryString(database, TemplateProperty), + ReadIcon(database, properties.GetValueOrDefault("ARPPRODUCTICON")), ExternalMedia(database)); + } + finally { MsiCloseHandle(database); } + } + + private static byte[]? ReadIcon(nint database, string? productIcon) + { + IEnumerable names = productIcon is null ? [] : [productIcon]; + names = names.Concat(FirstColumn(database, "SELECT `Icon_` FROM `Shortcut`")) + .Concat(FirstColumn(database, "SELECT `Name` FROM `Icon`")); + foreach (string name in names.Where(IsIdentifier).Distinct(StringComparer.Ordinal).Take(16)) + { + if (IconData(database, name) is { } data && PeReader.IconFromFile(data) is { } icon) + return icon; + } + return null; + } + + private static string? ExternalMedia(nint database) + { + var cabinets = FirstColumn(database, "SELECT `Cabinet` FROM `Media`"); + if (cabinets.FirstOrDefault(c => !c.StartsWith('#')) is { } external) + return external; + return cabinets.Count == 0 && FirstColumn(database, "SELECT `File` FROM `File`").Count > 0 ? "uncompressed files" : null; + } + + private static bool IsIdentifier(string name) => + name.Length is > 0 and <= 72 && name.All(c => char.IsAsciiLetterOrDigit(c) || c is '_' or '.'); + + private static List FirstColumn(nint database, string query) + { + var values = new List(); + if (MsiDatabaseOpenView(database, query, out nint view) != Success) + return values; + try + { + if (MsiViewExecute(view, 0) != Success) + return values; + while (values.Count < 16 && MsiViewFetch(view, out nint record) == Success) + { + try + { + if (RecordString(record, 1) is { Length: > 0 } value) + values.Add(value); + } + finally { MsiCloseHandle(record); } + } + } + finally { MsiCloseHandle(view); } + return values; + } + + private static byte[]? IconData(nint database, string name) + { + if (MsiDatabaseOpenView(database, $"SELECT `Data` FROM `Icon` WHERE `Name` = '{name}'", out nint view) != Success) + return null; + try + { + if (MsiViewExecute(view, 0) != Success || MsiViewFetch(view, out nint record) != Success) + return null; + try + { + using var data = new MemoryStream(); + byte[] buffer = new byte[64 * 1024]; + while (true) + { + uint read = (uint)buffer.Length; + uint result; + fixed (byte* pointer = buffer) + result = MsiRecordReadStream(record, 1, pointer, ref read); + if (result != Success) + return null; + if (read == 0) + return data.ToArray(); + if (data.Length + read > MaxIconBytes) + return null; + data.Write(buffer, 0, (int)read); + } + } + finally { MsiCloseHandle(record); } + } + finally { MsiCloseHandle(view); } + } + + private static string? RecordString(nint record, uint field) + { + uint size = 0; + char empty = '\0'; + uint result = MsiRecordGetString(record, field, &empty, ref size); + if (result is not (Success or MoreData)) + return null; + char[] buffer = new char[++size]; + fixed (char* pointer = buffer) + result = MsiRecordGetString(record, field, pointer, ref size); + return result == Success ? new string(buffer, 0, (int)size) : null; + } + + private static string? SummaryString(nint database, uint property) + { + if (MsiGetSummaryInformation(database, null, 0, out nint summary) != Success) + return null; + try + { + uint size = 0; + char empty = '\0'; + uint result = MsiSummaryInfoGetProperty(summary, property, out _, out _, out _, &empty, ref size); + if (result is not (Success or MoreData)) + return null; + char[] buffer = new char[++size]; + fixed (char* pointer = buffer) + result = MsiSummaryInfoGetProperty(summary, property, out _, out _, out _, pointer, ref size); + return result == Success ? new string(buffer, 0, (int)size) : null; + } + finally { MsiCloseHandle(summary); } + } + + public static IReadOnlyList RelatedProducts(string upgradeCode) + { + var products = new List(); + char* buffer = stackalloc char[GuidLength + 1]; + for (uint index = 0; index < 256; index++) + { + uint result = MsiEnumRelatedProducts(upgradeCode, 0, index, buffer); + if (result != Success) + break; + products.Add(new string(buffer, 0, GuidLength)); + } + return products; + } + + public static string? ProductInfo(string productCode, string property) + { + uint size = 0; + char empty = '\0'; + uint result = MsiGetProductInfo(productCode, property, &empty, ref size); + if (result is not (Success or MoreData)) + return null; + char[] buffer = new char[++size]; + fixed (char* pointer = buffer) + result = MsiGetProductInfo(productCode, property, pointer, ref size); + return result == Success ? new string(buffer, 0, (int)size) : null; + } + + [LibraryImport("msi.dll", EntryPoint = "MsiOpenDatabaseW", StringMarshalling = StringMarshalling.Utf16)] + private static partial uint MsiOpenDatabase(string databasePath, nint persist, out nint database); + + [LibraryImport("msi.dll", EntryPoint = "MsiDatabaseOpenViewW", StringMarshalling = StringMarshalling.Utf16)] + private static partial uint MsiDatabaseOpenView(nint database, string query, out nint view); + + [LibraryImport("msi.dll")] + private static partial uint MsiViewExecute(nint view, nint record); + + [LibraryImport("msi.dll")] + private static partial uint MsiViewFetch(nint view, out nint record); + + [LibraryImport("msi.dll", EntryPoint = "MsiRecordGetStringW")] + private static partial uint MsiRecordGetString(nint record, uint field, char* value, ref uint size); + + [LibraryImport("msi.dll")] + private static partial uint MsiRecordReadStream(nint record, uint field, byte* data, ref uint size); + + [LibraryImport("msi.dll", EntryPoint = "MsiGetSummaryInformationW", StringMarshalling = StringMarshalling.Utf16)] + private static partial uint MsiGetSummaryInformation(nint database, string? databasePath, uint updateCount, out nint summary); + + [LibraryImport("msi.dll", EntryPoint = "MsiSummaryInfoGetPropertyW")] + private static partial uint MsiSummaryInfoGetProperty(nint summary, uint property, out uint dataType, out int integer, + out long fileTime, char* value, ref uint size); + + [LibraryImport("msi.dll", EntryPoint = "MsiEnumRelatedProductsW", StringMarshalling = StringMarshalling.Utf16)] + private static partial uint MsiEnumRelatedProducts(string upgradeCode, uint reserved, uint index, char* productCode); + + [LibraryImport("msi.dll", EntryPoint = "MsiGetProductInfoW", StringMarshalling = StringMarshalling.Utf16)] + private static partial uint MsiGetProductInfo(string product, string property, char* value, ref uint size); + + [LibraryImport("msi.dll")] + private static partial uint MsiCloseHandle(nint handle); +} diff --git a/src/UniGetUI.PackageEngine.Managers.WinInstaller/Internal/PeReader.cs b/src/UniGetUI.PackageEngine.Managers.WinInstaller/Internal/PeReader.cs new file mode 100644 index 0000000000..a3e65a1441 --- /dev/null +++ b/src/UniGetUI.PackageEngine.Managers.WinInstaller/Internal/PeReader.cs @@ -0,0 +1,335 @@ +using System.Buffers.Binary; +using System.Text; + +namespace UniGetUI.PackageEngine.Managers.WinInstallerManager.Internal; + +internal sealed record PeInfo( + ushort Machine, + IReadOnlyList Sections, + IReadOnlyDictionary Strings, + string? FixedProductVersion, + string? FixedFileVersion, + byte[] OverlayHead, + byte[]? Icon) +{ + public string? String(string key) => + Strings.TryGetValue(key, out string? value) && !string.IsNullOrWhiteSpace(value) ? value.Trim() : null; +} + +internal static class PeReader +{ + public const ushort MachineI386 = 0x014C; + public const ushort MachineAmd64 = 0x8664; + public const ushort MachineArm64 = 0xAA64; + + private const int OverlayHeadBytes = 1024 * 1024; + private const int MaxResourceBytes = 4 * 1024 * 1024; + private const int MaxResourceEntries = 4096; + private const int MaxSections = 96; + + private const uint ResourceIcon = 3; + private const uint ResourceGroupIcon = 14; + private const uint ResourceVersion = 16; + + private readonly record struct Section(string Name, uint VirtualSize, uint VirtualAddress, uint RawSize, uint RawPointer); + private readonly record struct Resource(uint Type, uint Id, uint DataRva, uint Size); + + public static PeInfo Read(string path) + { + using var stream = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.Read, 4096, FileOptions.RandomAccess); + return Read(stream); + } + + public static PeInfo Read(Stream stream) + { + long length = stream.Length; + Span dos = stackalloc byte[64]; + ReadAt(stream, 0, dos, "the DOS header"); + if (BinaryPrimitives.ReadUInt16LittleEndian(dos) != 0x5A4D) + throw new InvalidDataException("It is not a Windows executable."); + long peOffset = BinaryPrimitives.ReadUInt32LittleEndian(dos[0x3C..]); + + Span coff = stackalloc byte[24]; + ReadAt(stream, peOffset, coff, "the PE header"); + if (BinaryPrimitives.ReadUInt32LittleEndian(coff) != 0x00004550) + throw new InvalidDataException("It is not a Windows executable."); + ushort machine = BinaryPrimitives.ReadUInt16LittleEndian(coff[4..]); + int sectionCount = BinaryPrimitives.ReadUInt16LittleEndian(coff[6..]); + int optionalSize = BinaryPrimitives.ReadUInt16LittleEndian(coff[20..]); + if (sectionCount > MaxSections) + throw new InvalidDataException("Its PE header declares too many sections."); + + byte[] optional = new byte[optionalSize]; + ReadAt(stream, peOffset + 24, optional, "the optional header"); + if (optional.Length < 2) + throw new InvalidDataException("Its PE header has no optional header."); + ushort magic = BinaryPrimitives.ReadUInt16LittleEndian(optional); + bool plus = magic switch + { + 0x20B => true, + 0x10B => false, + _ => throw new InvalidDataException("Its optional header has an unknown format."), + }; + int directoriesOffset = plus ? 112 : 96; + int directoryCount = optional.Length >= directoriesOffset + ? (int)Math.Min(BinaryPrimitives.ReadUInt32LittleEndian(optional.AsSpan(directoriesOffset - 4)), 16) + : 0; + (uint Address, uint Size) Directory(int index) => + index < directoryCount && optional.Length >= directoriesOffset + 8 * (index + 1) + ? (BinaryPrimitives.ReadUInt32LittleEndian(optional.AsSpan(directoriesOffset + 8 * index)), + BinaryPrimitives.ReadUInt32LittleEndian(optional.AsSpan(directoriesOffset + 8 * index + 4))) + : (0, 0); + + byte[] table = new byte[sectionCount * 40]; + ReadAt(stream, peOffset + 24 + optionalSize, table, "the section table"); + var sections = new Section[sectionCount]; + long imageEnd = peOffset + 24 + optionalSize + table.Length; + for (int i = 0; i < sectionCount; i++) + { + var entry = table.AsSpan(i * 40, 40); + string name = Encoding.ASCII.GetString(entry[..8]).TrimEnd('\0'); + sections[i] = new Section(name, + BinaryPrimitives.ReadUInt32LittleEndian(entry[8..]), + BinaryPrimitives.ReadUInt32LittleEndian(entry[12..]), + BinaryPrimitives.ReadUInt32LittleEndian(entry[16..]), + BinaryPrimitives.ReadUInt32LittleEndian(entry[20..])); + if (sections[i].RawSize > 0) + imageEnd = Math.Max(imageEnd, (long)sections[i].RawPointer + sections[i].RawSize); + } + + imageEnd = Math.Min(imageEnd, length); + var (certificateOffset, _) = Directory(4); + long overlayEnd = certificateOffset >= imageEnd && certificateOffset <= length ? certificateOffset : length; + byte[] overlay = new byte[(int)Math.Min(OverlayHeadBytes, Math.Max(overlayEnd - imageEnd, 0))]; + ReadAt(stream, imageEnd, overlay, "the installer data"); + + var resources = ReadResources(stream, sections, Directory(2).Address); + var (strings, productVersion, fileVersion) = resources.FirstOrDefault(r => r.Type == ResourceVersion) is { Size: > 0 } version + ? ParseVersionInfo(ReadResource(stream, sections, version)) + : (new Dictionary(), null, null); + + return new PeInfo(machine, sections.Select(s => s.Name).ToArray(), strings, productVersion, fileVersion, overlay, + ReadIcon(stream, sections, resources)); + } + + private static void ReadAt(Stream stream, long offset, Span buffer, string what) + { + if (offset < 0 || offset + buffer.Length > stream.Length) + throw new InvalidDataException($"The file ends before {what}."); + stream.Position = offset; + stream.ReadExactly(buffer); + } + + private static long? FileOffset(IReadOnlyList
sections, uint rva, uint size) + { + foreach (var section in sections) + { + uint span = Math.Max(section.VirtualSize, section.RawSize); + if (rva >= section.VirtualAddress && rva - section.VirtualAddress < span) + { + uint delta = rva - section.VirtualAddress; + if ((long)delta + size > section.RawSize) + return null; + return (long)section.RawPointer + delta; + } + } + return null; + } + + private static List ReadResources(Stream stream, IReadOnlyList
sections, uint rootRva) + { + var resources = new List(); + if (rootRva == 0 || FileOffset(sections, rootRva, 16) is null) + return resources; + int budget = MaxResourceEntries; + + IEnumerable<(uint Id, uint Offset, bool IsDirectory)> Entries(uint directoryOffset) + { + if (FileOffset(sections, rootRva + directoryOffset, 16) is not { } at) + yield break; + byte[] header = new byte[16]; + ReadAt(stream, at, header, "a resource directory"); + int count = BinaryPrimitives.ReadUInt16LittleEndian(header.AsSpan(12)) + BinaryPrimitives.ReadUInt16LittleEndian(header.AsSpan(14)); + if (count > budget) + throw new InvalidDataException("Its resources are too many to read."); + budget -= count; + byte[] entries = new byte[count * 8]; + if (FileOffset(sections, rootRva + directoryOffset + 16, (uint)entries.Length) is not { } entriesAt) + yield break; + ReadAt(stream, entriesAt, entries, "a resource directory"); + for (int i = 0; i < count; i++) + { + uint name = BinaryPrimitives.ReadUInt32LittleEndian(entries.AsSpan(i * 8)); + uint offset = BinaryPrimitives.ReadUInt32LittleEndian(entries.AsSpan(i * 8 + 4)); + yield return ((name & 0x80000000) != 0 ? uint.MaxValue : name, offset & 0x7FFFFFFF, (offset & 0x80000000) != 0); + } + } + + foreach (var type in Entries(0)) + { + if (!type.IsDirectory || type.Id is not (ResourceIcon or ResourceGroupIcon or ResourceVersion)) + continue; + foreach (var name in Entries(type.Offset)) + { + if (!name.IsDirectory) + continue; + foreach (var language in Entries(name.Offset)) + { + if (language.IsDirectory || FileOffset(sections, rootRva + language.Offset, 16) is not { } leaf) + continue; + byte[] data = new byte[16]; + ReadAt(stream, leaf, data, "a resource"); + resources.Add(new Resource(type.Id, name.Id, BinaryPrimitives.ReadUInt32LittleEndian(data), + BinaryPrimitives.ReadUInt32LittleEndian(data.AsSpan(4)))); + break; + } + } + } + return resources; + } + + private static byte[] ReadResource(Stream stream, IReadOnlyList
sections, Resource resource) + { + if (resource.Size > MaxResourceBytes || FileOffset(sections, resource.DataRva, resource.Size) is not { } at) + throw new InvalidDataException("One of its resources lies outside the file."); + byte[] data = new byte[resource.Size]; + ReadAt(stream, at, data, "a resource"); + return data; + } + + private readonly record struct Block(string Key, int ValueOffset, int ValueLength, bool IsText, int ChildrenOffset, int End); + + private static Block? ReadBlock(byte[] data, int offset, int limit) + { + if (offset + 6 > limit) + return null; + int length = BinaryPrimitives.ReadUInt16LittleEndian(data.AsSpan(offset)); + int valueLength = BinaryPrimitives.ReadUInt16LittleEndian(data.AsSpan(offset + 2)); + bool isText = BinaryPrimitives.ReadUInt16LittleEndian(data.AsSpan(offset + 4)) == 1; + int end = offset + length; + if (length < 6 || end > limit) + return null; + int keyEnd = offset + 6; + while (keyEnd + 1 < end && (data[keyEnd] != 0 || data[keyEnd + 1] != 0)) + keyEnd += 2; + string key = Encoding.Unicode.GetString(data, offset + 6, keyEnd - offset - 6); + int valueOffset = Align(keyEnd + 2); + int valueBytes = Math.Max(0, Math.Min(isText ? valueLength * 2 : valueLength, end - valueOffset)); + return new Block(key, valueOffset, valueBytes, isText, Math.Min(Align(valueOffset + valueBytes), end), end); + } + + private static int Align(int offset) => (offset + 3) & ~3; + + private static IEnumerable Children(byte[] data, Block parent) + { + int offset = parent.ChildrenOffset; + while (ReadBlock(data, offset, parent.End) is { } child) + { + yield return child; + offset = Align(child.End); + } + } + + internal static (Dictionary Strings, string? ProductVersion, string? FileVersion) ParseVersionInfo(byte[] data) + { + var strings = new Dictionary(StringComparer.OrdinalIgnoreCase); + if (ReadBlock(data, 0, data.Length) is not { Key: "VS_VERSION_INFO" } root) + return (strings, null, null); + string? product = null, file = null; + if (root.ValueLength >= 52 && BinaryPrimitives.ReadUInt32LittleEndian(data.AsSpan(root.ValueOffset)) == 0xFEEF04BD) + { + static string Format(uint high, uint low) => $"{high >> 16}.{high & 0xFFFF}.{low >> 16}.{low & 0xFFFF}"; + var fixedInfo = data.AsSpan(root.ValueOffset); + file = Format(BinaryPrimitives.ReadUInt32LittleEndian(fixedInfo[8..]), BinaryPrimitives.ReadUInt32LittleEndian(fixedInfo[12..])); + product = Format(BinaryPrimitives.ReadUInt32LittleEndian(fixedInfo[16..]), BinaryPrimitives.ReadUInt32LittleEndian(fixedInfo[20..])); + } + var tables = Children(data, root).Where(b => b.Key == "StringFileInfo").SelectMany(b => Children(data, b)).ToArray(); + var table = tables.FirstOrDefault(t => t.Key.StartsWith("0409", StringComparison.OrdinalIgnoreCase)); + if (table.Key is null && tables.Length > 0) + table = tables[0]; + if (table.Key is not null) + { + foreach (var entry in Children(data, table)) + { + if (entry.Key.Length > 0 && entry.ValueLength > 0) + strings[entry.Key] = Encoding.Unicode.GetString(data, entry.ValueOffset, entry.ValueLength).TrimEnd('\0'); + } + } + return (strings, product, file); + } + + private static readonly byte[] PngSignature = [0x89, (byte)'P', (byte)'N', (byte)'G', 0x0D, 0x0A, 0x1A, 0x0A]; + + public static bool IsPng(byte[] image) => image.AsSpan().StartsWith(PngSignature); + + private static byte[]? ReadIcon(Stream stream, IReadOnlyList
sections, List resources) + { + if (resources.FirstOrDefault(r => r.Type == ResourceGroupIcon) is not { Size: >= 6 } group) + return null; + byte[] directory = ReadResource(stream, sections, group); + int count = BinaryPrimitives.ReadUInt16LittleEndian(directory.AsSpan(4)); + (int Score, ushort Id, byte[] Entry)? best = null; + for (int i = 0; i < count && 6 + (i + 1) * 14 <= directory.Length; i++) + { + var entry = directory.AsSpan(6 + i * 14, 14); + int width = entry[0] == 0 ? 256 : entry[0]; + int bits = BinaryPrimitives.ReadUInt16LittleEndian(entry[6..]); + int score = width * 1000 + bits; + if (best is null || score > best.Value.Score) + best = (score, BinaryPrimitives.ReadUInt16LittleEndian(entry[12..]), entry.ToArray()); + } + if (best is not { } chosen || resources.FirstOrDefault(r => r.Type == ResourceIcon && r.Id == chosen.Id) is not { Size: > 0 } icon) + return null; + byte[] image = ReadResource(stream, sections, icon); + return IsPng(image) ? image : SingleImageIcon(chosen.Entry, image); + } + + private static byte[] SingleImageIcon(ReadOnlySpan entry, byte[] image) + { + byte[] ico = new byte[6 + 16 + image.Length]; + BinaryPrimitives.WriteUInt16LittleEndian(ico.AsSpan(2), 1); + BinaryPrimitives.WriteUInt16LittleEndian(ico.AsSpan(4), 1); + entry[..8].CopyTo(ico.AsSpan(6)); + BinaryPrimitives.WriteUInt32LittleEndian(ico.AsSpan(14), (uint)image.Length); + BinaryPrimitives.WriteUInt32LittleEndian(ico.AsSpan(18), 22); + image.CopyTo(ico, 22); + return ico; + } + + public static byte[]? BestIconInIco(byte[] ico) + { + if (ico.Length < 6 || BinaryPrimitives.ReadUInt16LittleEndian(ico) != 0 || BinaryPrimitives.ReadUInt16LittleEndian(ico.AsSpan(2)) != 1) + return null; + int count = BinaryPrimitives.ReadUInt16LittleEndian(ico.AsSpan(4)); + (int Score, int Offset, int Size, int Entry)? best = null; + for (int i = 0; i < count && 6 + (i + 1) * 16 <= ico.Length; i++) + { + var entry = ico.AsSpan(6 + i * 16, 16); + long size = BinaryPrimitives.ReadUInt32LittleEndian(entry[8..]); + long offset = BinaryPrimitives.ReadUInt32LittleEndian(entry[12..]); + if (size == 0 || offset + size > ico.Length) + continue; + int score = (entry[0] == 0 ? 256 : entry[0]) * 1000 + BinaryPrimitives.ReadUInt16LittleEndian(entry[6..]); + if (best is null || score > best.Value.Score) + best = (score, (int)offset, (int)size, 6 + i * 16); + } + if (best is not { } chosen) + return null; + byte[] image = ico.AsSpan(chosen.Offset, chosen.Size).ToArray(); + return IsPng(image) ? image : SingleImageIcon(ico.AsSpan(chosen.Entry, 16), image); + } + + public static byte[]? IconFromFile(byte[] data) + { + if (data.Length < 2 || data[0] != 'M' || data[1] != 'Z') + return BestIconInIco(data); + try + { + return Read(new MemoryStream(data, writable: false)).Icon; + } + catch (Exception ex) when (ex is InvalidDataException or EndOfStreamException) + { + return null; + } + } +} diff --git a/src/UniGetUI.PackageEngine.Managers.WinInstaller/Internal/WinFeed.cs b/src/UniGetUI.PackageEngine.Managers.WinInstaller/Internal/WinFeed.cs new file mode 100644 index 0000000000..103fe07279 --- /dev/null +++ b/src/UniGetUI.PackageEngine.Managers.WinInstaller/Internal/WinFeed.cs @@ -0,0 +1,191 @@ +using System.Collections.Concurrent; +using System.Runtime.InteropServices; +using System.Security.Cryptography; +using UniGetUI.Core.Data; +using UniGetUI.Core.Logging; +using UniGetUI.PackageEngine.Managers.InstallerFeeds; + +namespace UniGetUI.PackageEngine.Managers.WinInstallerManager.Internal; + +internal sealed record WinFeedItem( + string Id, + string Name, + string Version, + Uri Location, + string? Sha256, + long Size, + IReadOnlyList Architectures, + Uri? Icon, + InstallerDetails Details); + +internal static class WinFeed +{ + private static readonly string[] IconExtensions = [".png", ".jpg", ".jpeg", ".ico"]; + + private readonly record struct ParsedFile(long Size, DateTime WriteTimeUtc, DateTime ManifestWriteTimeUtc, InstallerMetadata Metadata); + + private static readonly ConcurrentDictionary ParsedFiles = new(StringComparer.OrdinalIgnoreCase); + private static readonly ConcurrentDictionary FileLocks = new(StringComparer.OrdinalIgnoreCase); + private static readonly FeedListing Listing = new(ScanFolder, ParseIndex, "InstallerFeeds"); + + public static void ForgetCachedIndexes() => Listing.ForgetCachedIndexes(); + + internal static void ForgetReadFiles() => ParsedFiles.Clear(); + + public static IReadOnlyList List(Uri location, CancellationToken cancellationToken = default, + bool currentOnly = false) => Listing.List(location, cancellationToken, currentOnly); + + public static IReadOnlyList Latest(IEnumerable items, Func compare, + Architecture machine) + { + var latest = new List(); + foreach (var group in items.GroupBy(i => i.Id, StringComparer.OrdinalIgnoreCase)) + { + WinFeedItem? best = null; + int bestFit = 0; + foreach (var item in group) + { + int fit = InstallerFile.ArchitectureFit(item.Architectures, machine); + if (fit == 0) + continue; + int order = best is null ? 1 : compare(item.Version, best.Version) ?? 0; + if (order > 0 || (order == 0 && fit > bestFit)) + { + best = item; + bestFit = fit; + } + } + if (best is not null) + latest.Add(best); + } + return latest; + } + + internal static IEnumerable EnumeratePackages(string directory) => + FeedLocation.EnumerateFiles(directory, InstallerFile.Extensions).Where(f => !InstallerFile.IsUninstaller(f)); + + internal static IReadOnlyList ScanFolder(string directory, CancellationToken cancellationToken) + { + if (!Directory.Exists(directory)) + throw new DirectoryNotFoundException($"The package feed folder {directory} does not exist or is not mounted."); + string[] files = EnumeratePackages(directory).ToArray(); + var items = new WinFeedItem?[files.Length]; + Parallel.For(0, files.Length, new ParallelOptions { MaxDegreeOfParallelism = 4, CancellationToken = cancellationToken }, + i => items[i] = TryReadFile(files[i]) is { } metadata ? ToItem(metadata, files[i]) : null); + return items.OfType().ToArray(); + } + + internal static InstallerMetadata? TryReadFile(string file) + { + try + { + return ReadFile(file); + } + catch (Exception ex) when (IsUnreadable(ex)) + { + Logger.Warn($"Skipping {file}: {ex.Message}"); + return null; + } + } + + internal static bool IsUnreadable(Exception ex) => + ex is IOException or UnauthorizedAccessException or InvalidDataException or EndOfStreamException; + + internal static InstallerMetadata ReadFile(string file) + { + lock (FileLocks.GetOrAdd(file, _ => new object())) + { + var info = new FileInfo(file); + var manifest = new FileInfo(InstallerFile.ManifestPath(file)); + DateTime manifestTime = manifest.Exists ? manifest.LastWriteTimeUtc : DateTime.MinValue; + if (ParsedFiles.TryGetValue(file, out var cached) && cached.Size == info.Length + && cached.WriteTimeUtc == info.LastWriteTimeUtc && cached.ManifestWriteTimeUtc == manifestTime) + return cached.Metadata; + var metadata = InstallerFile.Read(file); + if (metadata.Icon is { } icon) + SaveIcon(info, icon); + ParsedFiles[file] = new ParsedFile(info.Length, info.LastWriteTimeUtc, manifestTime, metadata with { Icon = null }); + return metadata; + } + } + + private static WinFeedItem ToItem(InstallerMetadata metadata, string file) => new( + metadata.Id, + metadata.Name, + metadata.Version, + new Uri(file), + null, + new FileInfo(file).Length, + metadata.Architectures, + FindIcon(file) is { } icon ? new Uri(icon) : null, + metadata.Details); + + internal static string? FindIcon(string installerFile) + { + string stem = Path.Combine(Path.GetDirectoryName(installerFile)!, Path.GetFileNameWithoutExtension(installerFile)); + if (IconExtensions.Select(e => stem + e).FirstOrDefault(File.Exists) is { } sibling) + return sibling; + var info = new FileInfo(installerFile); + return new[] { ".png", ".ico" }.Select(e => ExtractedIconPath(info, e)).FirstOrDefault(File.Exists); + } + + internal static string? TestIconDirectoryOverride { get; set; } + + private static string ExtractedIconPath(FileInfo file, string extension) => Path.Combine( + TestIconDirectoryOverride ?? Path.Combine(CoreData.UniGetUICacheDirectory_Icons, "InstallerFeeds"), + Convert.ToHexStringLower(SHA256.HashData(System.Text.Encoding.UTF8.GetBytes( + $"{file.FullName}|{file.Length}|{file.LastWriteTimeUtc.Ticks}")))[..32] + extension); + + private static void SaveIcon(FileInfo file, byte[] icon) + { + string path = ExtractedIconPath(file, PeReader.IsPng(icon) ? ".png" : ".ico"); + if (File.Exists(path)) + return; + try + { + Directory.CreateDirectory(Path.GetDirectoryName(path)!); + string temporary = path + "." + Guid.NewGuid().ToString("N") + ".tmp"; + File.WriteAllBytes(temporary, icon); + File.Move(temporary, path, overwrite: true); + } + catch (Exception ex) when (ex is IOException or UnauthorizedAccessException) + { + Logger.Debug($"Could not keep the icon of {file.FullName}: {ex.Message}"); + } + } + + internal static IReadOnlyList ParseIndex(byte[] json, Uri indexUrl) + { + var index = FeedJson.Deserialize(json, FeedIndexJsonContext.Default.FeedIndex) + ?? throw new InvalidDataException("The package feed index is empty."); + if (index.Version != FeedIndex.CurrentVersion) + throw new InvalidDataException($"Unsupported package feed index version: {index.Version}."); + + var items = new List(); + foreach (var entry in index.EntriesFor(FeedPlatform.Windows)) + { + if (entry is not null && !FeedLocation.HasExtension(entry.File ?? "", InstallerFile.Extensions)) + continue; + if (entry is null || string.IsNullOrWhiteSpace(entry.Id) || string.IsNullOrWhiteSpace(entry.Version) + || !FeedLocation.IsSha256(entry.Sha256) || FeedLocation.ResolveWithin(indexUrl, entry.File) is not { } file + || InstallerDetails.FromIndex(entry.Windows) is not { } details + || (details.Kind == InstallerKind.Msi) != InstallerFile.IsMsi(entry.File ?? "") + || (details.InstallArguments ?? InstallerKinds.InstallArguments(details.Kind)) is null) + { + Logger.Warn($"Skipping an invalid entry ({entry?.Id ?? "null"}) in the package feed index {indexUrl}."); + continue; + } + items.Add(new WinFeedItem( + entry.Id.Trim(), + string.IsNullOrWhiteSpace(entry.Name) ? entry.Id.Trim() : entry.Name.Trim(), + entry.Version.Trim(), + file, + entry.Sha256.ToLowerInvariant(), + entry.Size, + InstallerFile.Architectures(entry.Architectures ?? []), + FeedLocation.ResolveWithin(indexUrl, entry.Icon), + details with { InstallArguments = details.InstallArguments ?? InstallerKinds.InstallArguments(details.Kind) })); + } + return items; + } +} diff --git a/src/UniGetUI.PackageEngine.Managers.WinInstaller/Internal/WinInstallerRunner.cs b/src/UniGetUI.PackageEngine.Managers.WinInstaller/Internal/WinInstallerRunner.cs new file mode 100644 index 0000000000..ee310c0063 --- /dev/null +++ b/src/UniGetUI.PackageEngine.Managers.WinInstaller/Internal/WinInstallerRunner.cs @@ -0,0 +1,266 @@ +using System.ComponentModel; +using System.Diagnostics; +using UniGetUI.Core.SettingsEngine; +using UniGetUI.Core.Tools; +using UniGetUI.PackageEngine.Enums; +using UniGetUI.PackageEngine.Managers.InstallerFeeds; + +namespace UniGetUI.PackageEngine.Managers.WinInstallerManager.Internal; + +internal sealed record InstallerProcess(string FileName, string Arguments, bool Elevated); + +internal static class WinInstallerRunner +{ + private const int ErrorCancelled = 1223; + private const int ErrorElevationRequired = 740; + private const int MsiUserExit = 1602; + private const int RestartRequired = 3010; + private const int RestartInitiated = 1641; + + internal static string MsiExecPath => Path.Combine(Environment.SystemDirectory, "msiexec.exe"); + + internal static Func> RunProcess { get; set; } = RunProcessAsync; + + internal static Func IsAdministrator { get; set; } = CoreTools.IsAdministrator; + + internal static TimeSpan UninstallSettleTime { get; set; } = TimeSpan.FromMinutes(5); + + internal static TimeSpan UninstallPollInterval { get; set; } = TimeSpan.FromSeconds(2); + + public static async Task InstallAsync(WinFeedItem item, bool allowUnsigned, Action log, + Action progress, CancellationToken cancellationToken) + { + string staging = Directory.CreateTempSubdirectory("UniGetUI-installer-").FullName; + try + { + string staged = Path.Combine(staging, StagedName(item)); + await FeedStaging.StageAsync(item.Location, item.Size, staged, log, progress, cancellationToken); + using var held = new FileStream(staged, FileMode.Open, FileAccess.Read, FileShare.Read); + FeedStaging.VerifyHash(item.Sha256, staged); + VerifyIdentity(item, staged); + VerifySignature(staged, allowUnsigned, log); + + cancellationToken.ThrowIfCancellationRequested(); + progress(OperationProgress.ForStage(OperationProgressStage.Installing)); + string logFile = Path.Combine(staging, "install.log"); + var process = InstallProcess(item, staged, logFile); + log($"Installing {item.Name} {item.Version}: {Path.GetFileName(process.FileName)} {process.Arguments}"); + if (await RunAsync(process, log) is not { } code) + return OperationVeredict.Canceled; + var verdict = Verdict(code, log); + if (verdict == OperationVeredict.Failure && File.Exists(logFile)) + LogMsiErrors(logFile, log); + return verdict; + } + finally + { + try { Directory.Delete(staging, recursive: true); } + catch (IOException) { } + catch (UnauthorizedAccessException) { } + } + } + + internal static string StagedName(WinFeedItem item) + { + string name = Uri.UnescapeDataString(Path.GetFileName(item.Location.AbsolutePath)); + string extension = item.Details.Kind == InstallerKind.Msi ? ".msi" : ".exe"; + bool safe = name.Length is > 4 and < 120 && name.EndsWith(extension, StringComparison.OrdinalIgnoreCase) + && name.All(c => char.IsAsciiLetterOrDigit(c) || c is ' ' or '.' or '_' or '-' or '(' or ')' or '+'); + return safe ? name : "setup" + extension; + } + + internal static void VerifyIdentity(WinFeedItem item, string staged) + { + if (!item.Location.IsFile) + return; + string original = item.Location.LocalPath; + var manifest = InstallerFile.ReadManifest(original); + string stem = Path.GetFileNameWithoutExtension(original); + var metadata = InstallerFile.IsMsi(staged) + ? InstallerFile.FromMsi(MsiDatabase.Read(staged), manifest, stem) + : InstallerFile.FromExe(PeReader.Read(staged), manifest, stem); + if (!string.Equals(metadata.Id, item.Id, StringComparison.OrdinalIgnoreCase) || metadata.Version != item.Version) + throw new InvalidDataException( + $"The installer is {metadata.Id} {metadata.Version}, but the feed announced {item.Id} {item.Version}. Reload the feed, then try again."); + } + + internal static void VerifySignature(string staged, bool allowUnsigned, Action log) + { + var check = Authenticode.Verify(staged); + if (check.IsValid) + log($"The installer is {check.Describe()}."); + else if (allowUnsigned) + log($"The installer is {check.Describe()}, but its catalog allows unsigned packages."); + else + throw new InvalidDataException($"Windows does not trust this installer: it is {check.Describe()}. " + + "Publish an installer with a valid Authenticode signature, or set allowUnsignedPackages on its catalog."); + } + + internal static InstallerProcess InstallProcess(WinFeedItem item, string staged, string logFile) + { + bool elevated = !IsAdministrator() && (item.Details.Kind == InstallerKind.Msi ? !item.Details.PerUser : item.Details.PerMachine); + string arguments = item.Details.InstallArguments ?? InstallerKinds.InstallArguments(item.Details.Kind) + ?? throw new InvalidDataException($"No silent switches are known for {item.Name}."); + return item.Details.Kind == InstallerKind.Msi + ? new InstallerProcess(MsiExecPath, $"/i \"{staged}\" {arguments} /l*v \"{logFile}\"", elevated) + : new InstallerProcess(staged, arguments, elevated); + } + + public static async Task UninstallAsync(string name, InstallerDetails? details, ArpEntry entry, + Action log, Action progress) + { + if (UninstallProcess(details, entry) is not { } process) + { + log($"UniGetUI does not know how to uninstall {name} silently. Uninstall it from Apps and Features, " + + "or ask your administrator to give its uninstallArguments in the feed."); + return OperationVeredict.Failure; + } + progress(OperationProgress.ForStage(OperationProgressStage.Uninstalling)); + log($"Uninstalling {name}: {Path.GetFileName(process.FileName)} {process.Arguments}"); + if (await RunAsync(process, log) is not { } code) + return OperationVeredict.Canceled; + var verdict = Verdict(code, log); + if (verdict != OperationVeredict.Success || string.Equals(process.FileName, MsiExecPath, StringComparison.OrdinalIgnoreCase)) + return verdict; + + var deadline = DateTime.UtcNow + UninstallSettleTime; + while (true) + { + if (!ArpEntries.Read().Any(e => e.PerMachine == entry.PerMachine && string.Equals(e.Key, entry.Key, StringComparison.OrdinalIgnoreCase))) + return OperationVeredict.Success; + if (DateTime.UtcNow >= deadline) + break; + await Task.Delay(UninstallPollInterval); + } + log($"The uninstaller finished, but {name} is still listed in Apps and Features."); + return OperationVeredict.Failure; + } + + internal static InstallerProcess? UninstallProcess(InstallerDetails? details, ArpEntry entry) + { + bool elevated = entry.PerMachine && !IsAdministrator(); + string? extra = details?.UninstallArguments; + if (entry.WindowsInstaller && InstallerDetails.Guid(entry.Key) is { } productCode) + return new InstallerProcess(MsiExecPath, $"/x {productCode} {extra ?? "/qn /norestart"}", elevated); + + var kind = details?.Kind is { } known && known != InstallerKind.Msi ? known + : entry.Key.EndsWith("_is1", StringComparison.OrdinalIgnoreCase) ? InstallerKind.Inno + : entry.BundleUpgradeCodes.Count > 0 ? InstallerKind.Burn + : InstallerKind.Exe; + if (extra is null && entry.QuietUninstallString is { } quiet && SplitCommandLine(quiet) is { } quietCommand) + return quietCommand with { Elevated = elevated }; + if (entry.UninstallString is null || SplitCommandLine(entry.UninstallString) is not { } command) + return null; + if ((extra ?? InstallerKinds.UninstallArguments(kind)) is not { } silent) + return null; + return command with { Arguments = $"{command.Arguments} {silent}".Trim(), Elevated = elevated }; + } + + internal static InstallerProcess? SplitCommandLine(string commandLine) + { + string text = Environment.ExpandEnvironmentVariables(commandLine).Trim(); + string file, arguments; + if (text.StartsWith('"')) + { + int end = text.IndexOf('"', 1); + if (end < 0) + return null; + file = text[1..end]; + arguments = text[(end + 1)..]; + } + else + { + int end = -1; + for (int at = text.IndexOf(".exe", StringComparison.OrdinalIgnoreCase); at >= 0; + at = text.IndexOf(".exe", at + 1, StringComparison.OrdinalIgnoreCase)) + { + if (at + 4 == text.Length || char.IsWhiteSpace(text[at + 4]) || text[at + 4] == '/') + { + end = at + 4; + break; + } + } + if (end < 0) + return null; + file = text[..end]; + arguments = text[end..]; + } + if (!Path.IsPathFullyQualified(file)) + file = Path.Combine(Environment.SystemDirectory, file); + return File.Exists(file) ? new InstallerProcess(file, arguments.Trim(), false) : null; + } + + private static async Task RunAsync(InstallerProcess process, Action log) + { + if (process.Elevated && Settings.Get(Settings.K.ProhibitElevation)) + throw new InvalidOperationException("This package installs for every user, which needs administrator rights, " + + "and elevation is prohibited in UniGetUI's settings."); + try + { + return await RunProcess(process); + } + catch (Win32Exception ex) when (ex.NativeErrorCode == ErrorElevationRequired && !process.Elevated) + { + log("The installer asks for administrator rights."); + return await RunAsync(process with { Elevated = true }, log); + } + catch (Win32Exception ex) when (ex.NativeErrorCode == ErrorCancelled) + { + log("The administrator prompt was canceled."); + return null; + } + } + + internal static async Task RunProcessAsync(InstallerProcess process) + { + var startInfo = new ProcessStartInfo(process.FileName, process.Arguments) + { + WorkingDirectory = Path.GetDirectoryName(process.FileName)!, + }; + if (process.Elevated) + { + startInfo.UseShellExecute = true; + startInfo.Verb = "runas"; + CoreTools.PrepareForegroundForElevation(); + } + else + { + startInfo.UseShellExecute = false; + startInfo.CreateNoWindow = true; + } + using var started = Process.Start(startInfo) ?? throw new InvalidOperationException($"Could not start {process.FileName}."); + await started.WaitForExitAsync(CancellationToken.None); + return started.ExitCode; + } + + internal static OperationVeredict Verdict(int code, Action log) + { + switch (code) + { + case 0: + return OperationVeredict.Success; + case RestartRequired: + case RestartInitiated: + log("Windows must restart to finish."); + return OperationVeredict.Success; + case MsiUserExit: + log("The installation was canceled."); + return OperationVeredict.Canceled; + default: + log($"The installer exited with code {code}."); + return OperationVeredict.Failure; + } + } + + private static void LogMsiErrors(string logFile, Action log) + { + try + { + foreach (string line in File.ReadLines(logFile).Where(l => l.Contains("error", StringComparison.OrdinalIgnoreCase) + && !l.Contains("ErrorDialog", StringComparison.Ordinal) && !l.Contains("Note: 1: 2205", StringComparison.Ordinal) + && !l.Contains("FROM `Error`", StringComparison.Ordinal)).TakeLast(15)) + log(line.Trim()); + } + catch (IOException) { } + } +} diff --git a/src/UniGetUI.PackageEngine.Managers.WinInstaller/InternalsVisibleTo.cs b/src/UniGetUI.PackageEngine.Managers.WinInstaller/InternalsVisibleTo.cs new file mode 100644 index 0000000000..eeb63dad19 --- /dev/null +++ b/src/UniGetUI.PackageEngine.Managers.WinInstaller/InternalsVisibleTo.cs @@ -0,0 +1,3 @@ +using System.Runtime.CompilerServices; + +[assembly: InternalsVisibleTo("UniGetUI.PackageEngine.Tests")] diff --git a/src/UniGetUI.PackageEngine.Managers.WinInstaller/UniGetUI.PackageEngine.Managers.WinInstaller.csproj b/src/UniGetUI.PackageEngine.Managers.WinInstaller/UniGetUI.PackageEngine.Managers.WinInstaller.csproj new file mode 100644 index 0000000000..642a533ece --- /dev/null +++ b/src/UniGetUI.PackageEngine.Managers.WinInstaller/UniGetUI.PackageEngine.Managers.WinInstaller.csproj @@ -0,0 +1,26 @@ + + + $(WindowsTargetFramework) + + true + + + + + + + + + + + + + + + + + + + + + diff --git a/src/UniGetUI.PackageEngine.Managers.WinInstaller/WinInstaller.cs b/src/UniGetUI.PackageEngine.Managers.WinInstaller/WinInstaller.cs new file mode 100644 index 0000000000..2310f5d97b --- /dev/null +++ b/src/UniGetUI.PackageEngine.Managers.WinInstaller/WinInstaller.cs @@ -0,0 +1,178 @@ +using System.Runtime.InteropServices; +using UniGetUI.Core.Logging; +using UniGetUI.Core.Tools; +using UniGetUI.Interface.Enums; +using UniGetUI.PackageEngine.Classes.Manager; +using UniGetUI.PackageEngine.Interfaces; +using UniGetUI.PackageEngine.ManagerClasses.Manager; +using UniGetUI.PackageEngine.Managers.InstallerFeeds; +using UniGetUI.PackageEngine.Managers.WinInstallerManager.Internal; +using UniGetUI.PackageEngine.PackageClasses; + +namespace UniGetUI.PackageEngine.Managers.WinInstallerManager; + +public sealed class WinInstaller : PackageManager +{ + public const string ManagerId = "installer"; + + public WinInstaller() + { + Dependencies = []; + + Capabilities = new ManagerCapabilities + { + CanRunAsAdmin = false, + CanUninstall = true, + InstalledPackagesKeepTheirSource = false, + CanDownloadInstaller = true, + SupportsCustomSources = true, + SupportsEmptySearchQuery = true, + SupportsCustomPackageIcons = true, + SupportsCustomVersions = true, + SerializesOperations = true, + Sources = new SourceCapabilities + { + KnowsPackageCount = false, + KnowsUpdateDate = false, + CanBeEdited = false, + }, + SupportsProxy = ProxySupport.No, + SupportsProxyAuth = false, + KnowsPackageReleaseDate = PackageReleaseDateSupport.No, + }; + + Properties = new ManagerProperties + { + Id = ManagerId, + Name = "Installer", + DisplayName = "Installer packages", + Description = CoreTools.Translate( + "Apps (.exe and .msi) that your organization publishes in its software catalog." + ), + IconId = IconType.Package, + ColorIconId = "pkg_color", + ExecutableFriendlyName = "msiexec.exe", + InstallVerb = "/i", + UpdateVerb = "/i", + UninstallVerb = "/x", + KnownSources = [], + DefaultSource = new ManagerSource(this, "Installer packages", new Uri("file:///")), + }; + + SourcesHelper = new InstallerFeedSourceHelper(this); + DetailsHelper = new WinInstallerDetailsHelper(this); + OperationHelper = new WinInstallerOperationHelper(this); + } + + public override IReadOnlyList FindCandidateExecutableFiles() => + File.Exists(WinInstallerRunner.MsiExecPath) ? [WinInstallerRunner.MsiExecPath] : []; + + protected override void _loadManagerExecutableFile(out bool found, out string path, out string callArguments) + { + var candidates = FindCandidateExecutableFiles(); + found = candidates.Count > 0; + path = found ? candidates[0] : ""; + callArguments = ""; + } + + protected override void _loadManagerVersion(out string version) => + version = $"Windows {Environment.OSVersion.Version}"; + + public override void RefreshPackageIndexes() + { + WinFeed.ForgetCachedIndexes(); + ((InstallerFeedSourceHelper)SourcesHelper).ForgetSources(); + } + + internal static Architecture Machine { get; set; } = RuntimeInformation.OSArchitecture; + + internal IReadOnlyList<(IManagerSource Source, WinFeedItem Item)> ListLatest() + { + var result = new List<(IManagerSource, WinFeedItem)>(); + foreach (var source in SourcesHelper.GetSources()) + { + try + { + foreach (var item in WinFeed.Latest(WinFeed.List(source.Url), CompareVersions, Machine)) + result.Add((source, item)); + } + catch (Exception ex) + { + Logger.Warn($"Could not list the package feed {source.Name} at {source.Url}: {ex.Message}"); + } + } + return result; + } + + internal InstallerFeedSource? FindFeedSource(IPackage package) => + SourcesHelper.GetSources().OfType().FirstOrDefault(s => s.Name == package.Source.Name); + + internal WinFeedItem? FindFeedItem(IPackage package, string? version = null, bool currentOnly = false) + { + var source = FindFeedSource(package); + if (source is null) + return null; + var items = WinFeed.List(source.Url, currentOnly: currentOnly) + .Where(i => string.Equals(i.Id, package.Id, StringComparison.OrdinalIgnoreCase)).ToArray(); + if (!string.IsNullOrEmpty(version)) + items = items.Where(i => i.Version == version).ToArray(); + return WinFeed.Latest(items, CompareVersions, Machine).FirstOrDefault(); + } + + internal IComparer VersionOrder => + Comparer.Create((a, b) => CompareVersions(a, b) ?? string.CompareOrdinal(a, b)); + + internal IReadOnlyList ListFeedItems(IPackage package) + { + var source = SourcesHelper.GetSources().FirstOrDefault(s => s.Name == package.Source.Name); + return source is null ? [] : WinFeed.List(source.Url) + .Where(i => string.Equals(i.Id, package.Id, StringComparison.OrdinalIgnoreCase) + && InstallerFile.ArchitectureFit(i.Architectures, Machine) > 0) + .ToArray(); + } + + protected override IReadOnlyList FindPackages_UnSafe(string query) + { + query = query.Trim(); + return ListLatest() + .Where(p => query.Length == 0 + || p.Item.Name.Contains(query, StringComparison.OrdinalIgnoreCase) + || p.Item.Id.Contains(query, StringComparison.OrdinalIgnoreCase)) + .Select(p => new Package(p.Item.Name, p.Item.Id, p.Item.Version, p.Source, this)) + .ToArray(); + } + + internal sealed record InstalledItem(IManagerSource Source, WinFeedItem Item, ArpEntry Entry, string Installed, bool HasUpdate); + + internal IReadOnlyList ListInstalled() + { + var latest = ListLatest(); + var entries = ArpEntries.Read(); + var seen = new HashSet(StringComparer.OrdinalIgnoreCase); + var installed = new List(); + foreach (var (source, item) in latest.OrderBy(p => p.Item.Id, StringComparer.OrdinalIgnoreCase) + .ThenByDescending(p => p.Item.Version, VersionOrder)) + { + if (seen.Contains(item.Id) || InstalledMatcher.Find(item, entries) is not { } entry) + continue; + string version = InstallReceipts.InstalledVersion(item.Id, entry); + installed.Add(new InstalledItem(source, item, entry, version, CompareVersions(item.Version, version) > 0)); + seen.Add(item.Id); + } + return installed; + } + + internal ArpEntry? FindInstalledEntry(IPackage package) => + ListInstalled().FirstOrDefault(p => string.Equals(p.Item.Id, package.Id, StringComparison.OrdinalIgnoreCase))?.Entry; + + protected override IReadOnlyList GetInstalledPackages_UnSafe() => + ListInstalled() + .Select(p => new Package(p.Item.Name, p.Item.Id, p.Installed, p.Source, this)) + .ToArray(); + + protected override IReadOnlyList GetAvailableUpdates_UnSafe() => + ListInstalled() + .Where(p => p.HasUpdate) + .Select(p => new Package(p.Item.Name, p.Item.Id, p.Installed, p.Item.Version, p.Source, this)) + .ToArray(); +} diff --git a/src/UniGetUI.PackageEngine.Managers.WinInstaller/WinInstallerFeedInspector.cs b/src/UniGetUI.PackageEngine.Managers.WinInstaller/WinInstallerFeedInspector.cs new file mode 100644 index 0000000000..512f84e149 --- /dev/null +++ b/src/UniGetUI.PackageEngine.Managers.WinInstaller/WinInstallerFeedInspector.cs @@ -0,0 +1,92 @@ +using UniGetUI.PackageEngine.Managers.InstallerFeeds; +using UniGetUI.PackageEngine.Managers.WinInstallerManager.Internal; + +namespace UniGetUI.PackageEngine.Managers.WinInstallerManager; + +public static class WinInstallerFeedInspector +{ + public static bool IsValidLocation(string? text) => FeedLocation.TryParse(text, out _); + + public static FeedInspection Inspect(string text) + { + if (!FeedLocation.TryParse(text, out Uri location)) + throw new InvalidDataException($"\"{text}\" is not an https URL or an absolute folder path."); + WinFeed.ForgetCachedIndexes(); + if (!location.IsFile) + return new FeedInspection(WinFeed.List(location, currentOnly: true).Select(Describe).ToArray(), [], null); + + string folder = location.LocalPath; + if (!Directory.Exists(folder)) + throw new DirectoryNotFoundException($"The folder {folder} does not exist or is not reachable."); + var packages = new List(); + var problems = new List(); + foreach (string file in WinFeed.EnumeratePackages(folder)) + { + try + { + var metadata = WinFeed.ReadFile(file); + packages.Add(new FeedPackage(metadata.Name, metadata.Id, metadata.Version, Path.GetRelativePath(folder, file), + InstallerKinds.Describe(metadata.Details.Kind), metadata.Architectures)); + } + catch (Exception ex) when (WinFeed.IsUnreadable(ex)) + { + problems.Add(new FeedProblem(Path.GetRelativePath(folder, file), ex.Message)); + } + } + return new FeedInspection(packages, problems, null); + } + + private static FeedPackage Describe(WinFeedItem item) => new(item.Name, item.Id, item.Version, + Uri.UnescapeDataString(Path.GetFileName(item.Location.AbsolutePath)), InstallerKinds.Describe(item.Details.Kind), + item.Architectures); +} + +public sealed class WinInstallerIndexer : IFeedIndexer +{ + public FeedPlatform Platform => FeedPlatform.Windows; + + public IReadOnlyCollection Extensions => InstallerFile.Extensions; + + public (IReadOnlyList Entries, IReadOnlyList Skipped) Build(string folder) + { + var entries = new List(); + var skipped = new List(); + foreach (string file in WinFeed.EnumeratePackages(folder)) + { + InstallerMetadata metadata; + try + { + metadata = WinFeed.ReadFile(file); + } + catch (Exception ex) when (WinFeed.IsUnreadable(ex)) + { + skipped.Add(new FeedProblem(Path.GetRelativePath(folder, file), ex.Message)); + continue; + } + string? icon = PublishIcon(file); + entries.Add(new FeedIndexEntry + { + Id = metadata.Id, + Name = metadata.Name, + Version = metadata.Version, + File = FeedLocation.RelativeUrlPath(folder, file), + Sha256 = FeedLocation.ComputeSha256(file), + Size = new FileInfo(file).Length, + Architectures = metadata.Architectures.Count > 0 ? [.. metadata.Architectures] : null, + Icon = icon is null ? null : FeedLocation.RelativeUrlPath(folder, icon), + Windows = metadata.Details.ToIndex(), + }); + } + return (entries, skipped); + } + + private static string? PublishIcon(string file) + { + string? icon = WinFeed.FindIcon(file); + if (icon is null || Path.GetDirectoryName(icon) == Path.GetDirectoryName(file)) + return icon; + string sibling = Path.ChangeExtension(file, Path.GetExtension(icon)); + File.Copy(icon, sibling, overwrite: false); + return sibling; + } +} diff --git a/src/UniGetUI.PackageEngine.PackageEngine/PEInterface.cs b/src/UniGetUI.PackageEngine.PackageEngine/PEInterface.cs index 5c63cfc0d1..6d044efdef 100644 --- a/src/UniGetUI.PackageEngine.PackageEngine/PEInterface.cs +++ b/src/UniGetUI.PackageEngine.PackageEngine/PEInterface.cs @@ -15,6 +15,7 @@ using UniGetUI.PackageEngine.Managers.PowerShellManager; using UniGetUI.PackageEngine.Managers.ScoopManager; using UniGetUI.PackageEngine.Managers.WingetManager; +using UniGetUI.PackageEngine.Managers.WinInstallerManager; #else using UniGetUI.PackageEngine.Managers.AptManager; using UniGetUI.PackageEngine.Managers.DnfManager; @@ -37,6 +38,7 @@ public static class PEInterface public static readonly WinGet? WinGet = Create(() => new WinGet()); public static readonly Scoop? Scoop = Create(() => new Scoop()); public static readonly Chocolatey? Chocolatey = Create(() => new Chocolatey()); + public static readonly WinInstaller? WinInstaller = Create(() => new WinInstaller()); #endif public static readonly Npm? Npm = Create(() => new Npm()); public static readonly Bun? Bun = Create(() => new Bun()); @@ -88,7 +90,7 @@ private static IPackageManager[] CreateManagers() { List candidates = [Npm, Bun, Pip, Cargo, Vcpkg, DotNet, PowerShell7]; #if WINDOWS - candidates.InsertRange(0, [WinGet, Scoop, Chocolatey]); + candidates.InsertRange(0, [WinGet, WinInstaller, Scoop, Chocolatey]); candidates.Add(PowerShell); #else candidates.Insert(0, Homebrew); diff --git a/src/UniGetUI.PackageEngine.PackageEngine/UniGetUI.PackageEngine.PEInterface.csproj b/src/UniGetUI.PackageEngine.PackageEngine/UniGetUI.PackageEngine.PEInterface.csproj index dd7d6bfe4c..89657353bd 100644 --- a/src/UniGetUI.PackageEngine.PackageEngine/UniGetUI.PackageEngine.PEInterface.csproj +++ b/src/UniGetUI.PackageEngine.PackageEngine/UniGetUI.PackageEngine.PEInterface.csproj @@ -15,6 +15,7 @@ + @@ -36,6 +37,10 @@ SetTargetFramework="TargetFramework=$(WindowsTargetFramework)"> false + + false + false diff --git a/src/UniGetUI.PackageEngine.Tests/InstallerFeedsTestCollection.cs b/src/UniGetUI.PackageEngine.Tests/InstallerFeedsTestCollection.cs new file mode 100644 index 0000000000..6ce1309b5d --- /dev/null +++ b/src/UniGetUI.PackageEngine.Tests/InstallerFeedsTestCollection.cs @@ -0,0 +1,4 @@ +namespace UniGetUI.PackageEngine.Tests; + +[CollectionDefinition("Installer feeds", DisableParallelization = true)] +public sealed class InstallerFeedsTestCollection; diff --git a/src/UniGetUI.PackageEngine.Tests/MacPkgTests.cs b/src/UniGetUI.PackageEngine.Tests/MacPkgTests.cs index 08bbdc39c1..e4b6705ac6 100644 --- a/src/UniGetUI.PackageEngine.Tests/MacPkgTests.cs +++ b/src/UniGetUI.PackageEngine.Tests/MacPkgTests.cs @@ -6,11 +6,13 @@ using System.Text; using System.Text.Json; using UniGetUI.Core.Tools; +using UniGetUI.PackageEngine.Managers.InstallerFeeds; using UniGetUI.PackageEngine.Managers.MacPkgManager; using UniGetUI.PackageEngine.Managers.MacPkgManager.Internal; namespace UniGetUI.PackageEngine.Tests; +[Collection("Installer feeds")] public sealed class MacPkgTests : IDisposable { private readonly string _directory = Path.Combine(Path.GetTempPath(), nameof(MacPkgTests), Guid.NewGuid().ToString("N")); @@ -224,12 +226,12 @@ public void ArchitectureFitPrefersNativeThenRosetta(string[] architectures, Arch [InlineData("relative/folder", false)] [InlineData("", false)] public void OnlyHttpsFileAndLoopbackLocationsAreFeeds(string text, bool accepted) => - Assert.Equal(accepted, PkgFeed.TryParseLocation(text, out _)); + Assert.Equal(accepted, FeedLocation.TryParse(text, out _)); [Fact] public void AnAbsolutePathIsAFolderFeed() { - Assert.True(PkgFeed.TryParseLocation(_directory, out Uri location)); + Assert.True(FeedLocation.TryParse(_directory, out Uri location)); Assert.True(location.IsFile); Assert.Equal(Path.GetFullPath(_directory), location.LocalPath); } @@ -239,7 +241,7 @@ public void AnAbsolutePathIsAFolderFeed() [InlineData("https://h/mac", "https://h/mac/index.json")] [InlineData("https://h/mac/catalog.json", "https://h/mac/catalog.json")] public void TheIndexSitsBesideThePackages(string location, string index) => - Assert.Equal(index, PkgFeed.IndexUrl(new Uri(location)).AbsoluteUri); + Assert.Equal(index, FeedLocation.IndexUrl(new Uri(location)).AbsoluteUri); [Theory] [InlineData("Zoom.pkg", "https://h/mac/Zoom.pkg")] @@ -252,7 +254,7 @@ public void TheIndexSitsBesideThePackages(string location, string index) => [InlineData("apps\\Zoom.pkg", null)] [InlineData("", null)] public void IndexPathsCannotLeaveTheFeed(string relative, string? resolved) => - Assert.Equal(resolved, PkgFeed.ResolveWithin(new Uri("https://h/mac/index.json"), relative)?.AbsoluteUri); + Assert.Equal(resolved, FeedLocation.ResolveWithin(new Uri("https://h/mac/index.json"), relative)?.AbsoluteUri); [Fact] public void AnIndexSkipsEntriesWithoutAHashOrWithAnEscapingPath() @@ -398,13 +400,112 @@ public void TheIndexCommandRecordsEveryPackageWithItsHash() byte[] json = File.ReadAllBytes(Path.Combine(_directory, "index.json")); var items = PkgFeed.ParseIndex(json, new Uri("https://h/feed/index.json")); var appItem = items.Single(i => i.Id == "com.example.app.pkg"); - Assert.Equal(PkgFeed.ComputeSha256(app), appItem.Sha256); + Assert.Equal(FeedLocation.ComputeSha256(app), appItem.Sha256); Assert.Equal(["arm64"], appItem.Architectures); Assert.Equal("https://h/feed/App.png", appItem.Icon?.AbsoluteUri); var tool = items.Single(i => i.Id == "com.example.tool.pkg"); Assert.Equal(("Tool", "2.0", "https://h/feed/tools/Tool%202.pkg"), (tool.Name, tool.Version, tool.Location.AbsoluteUri)); } + [Fact] + public void AWindowsEntryInAnIndexIsNotAMacPackage() + { + string sha = new('a', 64); + string json = $$""" + { + "version": 1, + "packages": [ + { "id": "com.example.app.pkg", "name": "App", "version": "1.0", "file": "App.pkg", "sha256": "{{sha}}" }, + { "id": "Vendor.Tool", "name": "Tool", "version": "1.0", "file": "Tool.exe", "sha256": "{{sha}}", "windows": { "installer": "nsis" } }, + { "id": "{11111111-2222-3333-4444-555555555555}", "name": "Example", "version": "1.0", "file": "Example.msi", "sha256": "{{sha}}", "windows": { "installer": "msi" } } + ] + } + """; + Assert.Equal(["com.example.app.pkg"], PkgFeed.ParseIndex(Encoding.UTF8.GetBytes(json), new Uri("https://h/feed/index.json")).Select(i => i.Id)); + } + + [Fact] + public void TheMacIndexKeepsWindowsEntriesUnderTheirOwnKeySoOlderMacsNeverSeeThem() + { + string pkg = WritePackage("mac/App.pkg", ProductArchive("com.example.app.pkg", "1.0", "App", "arm64")); + string exe = WritePackage("win/Tool.exe", Encoding.ASCII.GetBytes("MZ installer")); + WritePackage("win/Changed.msi", [1]); + string index = Path.Combine(_directory, "index.json"); + File.WriteAllText(index, $$""" + { + "version": 1, + "packages": [], + "windowsPackages": [ + { "id": "Vendor.Tool", "name": "Tool", "version": "1.0", "file": "win/Tool.exe", "sha256": "{{FeedLocation.ComputeSha256(exe)}}", "windows": { "installer": "nsis" } }, + { "id": "Vendor.Changed", "name": "Changed", "version": "1.0", "file": "win/Changed.msi", "sha256": "{{new string('0', 64)}}", "windows": { "installer": "msi" } } + ] + } + """); + + var output = new StringWriter(); + var error = new StringWriter(); + Assert.Equal(3, PkgIndexCommand.Run([_directory], output, error)); + Assert.Contains("win/Tool.exe: Tool (Vendor.Tool 1.0) (kept)", output.ToString()); + Assert.Contains("win/Changed.msi", error.ToString()); + + byte[] json = File.ReadAllBytes(index); + var written = FeedJson.Deserialize(json, FeedIndexJsonContext.Default.FeedIndex)!; + Assert.Equal(["mac/App.pkg"], written.Packages.Select(e => e.File)); + Assert.Equal(["win/Tool.exe"], written.WindowsPackages!.Select(e => e.File)); + Assert.Equal(FeedLocation.ComputeSha256(pkg), PkgFeed.ParseIndex(json, new Uri("https://h/feed/index.json")).Single().Sha256); + } + + [Fact] + public void AnIndexWithOnlyWindowsPackagesOffersNothingToAMac() + { + string sha = new('a', 64); + string json = $$""" + { + "version": 1, + "packages": [], + "windowsPackages": [ + { "id": "com.example.app.pkg", "name": "App", "version": "1.0", "file": "App.pkg", "sha256": "{{sha}}" } + ] + } + """; + Assert.Empty(PkgFeed.ParseIndex(Encoding.UTF8.GetBytes(json), new Uri("https://h/feed/index.json"))); + } + +#if WINDOWS + [Fact] + public void BothPlatformsIndexAMixedFolderInTurnWithoutLosingTheOthersEntries() + { + string pkg = WritePackage("mac/App.pkg", ProductArchive("com.example.app.pkg", "1.0", "App", "arm64")); + string exe = WritePackage("win/Tool.exe", [.. File.ReadAllBytes(Path.Combine(Environment.SystemDirectory, "cmd.exe")), + .. new byte[4], .. BitConverter.GetBytes(0xDEADBEEF), .. Encoding.ASCII.GetBytes("NullsoftInst"), .. new byte[12]]); + var indexUrl = new Uri("https://h/feed/index.json"); + string index = Path.Combine(_directory, "index.json"); + var windows = new UniGetUI.PackageEngine.Managers.WinInstallerManager.WinInstallerIndexer(); + + Assert.Equal(0, FeedIndexCommand.Run([_directory], new StringWriter(), new StringWriter(), windows)); + Assert.Empty(FeedJson.Deserialize(File.ReadAllBytes(index), FeedIndexJsonContext.Default.FeedIndex)!.Packages); + Assert.Empty(PkgFeed.ParseIndex(File.ReadAllBytes(index), indexUrl)); + + var output = new StringWriter(); + Assert.Equal(0, PkgIndexCommand.Run([_directory], output, new StringWriter())); + Assert.Contains("win/Tool.exe", output.ToString()); + Assert.Contains("(kept)", output.ToString()); + Assert.Equal(FeedLocation.ComputeSha256(pkg), PkgFeed.ParseIndex(File.ReadAllBytes(index), indexUrl).Single().Sha256); + var windowsItem = UniGetUI.PackageEngine.Managers.WinInstallerManager.Internal.WinFeed.ParseIndex(File.ReadAllBytes(index), indexUrl).Single(); + Assert.Equal((FeedLocation.ComputeSha256(exe), "https://h/feed/win/Tool.exe"), (windowsItem.Sha256, windowsItem.Location.AbsoluteUri)); + + Assert.Equal(0, FeedIndexCommand.Run([_directory], new StringWriter(), new StringWriter(), windows)); + Assert.Single(PkgFeed.ParseIndex(File.ReadAllBytes(index), indexUrl)); + Assert.Single(UniGetUI.PackageEngine.Managers.WinInstallerManager.Internal.WinFeed.ParseIndex(File.ReadAllBytes(index), indexUrl)); + + File.AppendAllText(pkg, "changed"); + var error = new StringWriter(); + Assert.Equal(3, FeedIndexCommand.Run([_directory], new StringWriter(), error, windows)); + Assert.Contains("mac/App.pkg", error.ToString()); + Assert.Empty(PkgFeed.ParseIndex(File.ReadAllBytes(index), indexUrl)); + } +#endif + [Fact] public void TheIndexCommandExplainsItsUsage() { @@ -433,7 +534,7 @@ public void FeedsAreReadFromTheCatalogsThatDeclareThem() ]} """; // Feeds are told apart by catalog id: two catalogs may share a name, not an id. - var feeds = PkgCatalogFeeds.Parse(Encoding.UTF8.GetBytes(json)); + var feeds = CatalogFeeds.Parse(Encoding.UTF8.GetBytes(json)); Assert.Equal(["a", "c", "d"], feeds.Select(f => f.Id)); Assert.Equal(["Mac apps", "Local", "Mac apps"], feeds.Select(f => f.Name)); Assert.Equal("https://software.example.com/mac/", feeds[0].Location.AbsoluteUri); @@ -444,9 +545,9 @@ public void FeedsAreReadFromTheCatalogsThatDeclareThem() [Fact] public void AMissingOrBrokenCatalogDeclaresNoFeeds() { - Assert.Empty(PkgCatalogFeeds.Load(Path.Combine(_directory, "missing.json"))); + Assert.Empty(CatalogFeeds.Load(Path.Combine(_directory, "missing.json"))); string broken = WritePackage("broken.json", Encoding.UTF8.GetBytes("{ not json")); - Assert.Empty(PkgCatalogFeeds.Load(broken)); + Assert.Empty(CatalogFeeds.Load(broken)); } // ── Receipts ─────────────────────────────────────────────────────────── @@ -506,7 +607,7 @@ public void OnlyAPlainAppNameIsCopiedToApplications(string app, string? name) public void AStagedPackageMustMatchTheFeedHashAndIdentity() { string staged = WritePackage("staged.pkg", ProductArchive("com.example.app.pkg", "1.0")); - string hash = PkgFeed.ComputeSha256(staged); + string hash = FeedLocation.ComputeSha256(staged); var item = new PkgFeedItem("com.example.app.pkg", "App", "1.0", new Uri("https://h/App.pkg"), hash, 0, [], null); PkgInstaller.VerifyHash(item, staged); @@ -674,7 +775,7 @@ await Assert.ThrowsAsync(() => PkgInstaller.InstallAsync(i [Fact] public void ACatalogCanAllowItsFeedsUnsignedPackages() { - var feeds = PkgCatalogFeeds.Parse(Encoding.UTF8.GetBytes(""" + var feeds = CatalogFeeds.Parse(Encoding.UTF8.GetBytes(""" {"version":1,"catalogs":[ {"id":"signed","name":"Signed","packages":[],"pkgFeed":"https://h/a/"}, {"id":"internal","name":"Internal","packages":[],"pkgFeed":"https://h/b/","allowUnsignedPackages":true} @@ -885,7 +986,7 @@ public void TheManagerListsFeedPackagesTheirReceiptsAndTheirUpdates() ["--pkg-info", "com.example.current.pkg"] => (0, "version: 5.0\n"), _ => (1, ""), }; - PkgCatalogFeeds.TestCatalogPathOverride = catalog; + CatalogFeeds.TestCatalogPathOverride = catalog; try { var manager = new MacPkg(); @@ -917,7 +1018,7 @@ public void TheManagerListsFeedPackagesTheirReceiptsAndTheirUpdates() finally { PkgReceipts.RunPkgUtil = previousPkgUtil; - PkgCatalogFeeds.TestCatalogPathOverride = null; + CatalogFeeds.TestCatalogPathOverride = null; } } @@ -941,7 +1042,7 @@ public void AnUpdateIsFoundWhicheverFeedOffersItAndAPinnedBuildMustRunHere() ["--pkg-info", "com.example.app.pkg"] => (0, "version: 1.0\n"), _ => (1, ""), }; - PkgCatalogFeeds.TestCatalogPathOverride = catalog; + CatalogFeeds.TestCatalogPathOverride = catalog; try { var manager = new MacPkg(); @@ -956,7 +1057,7 @@ public void AnUpdateIsFoundWhicheverFeedOffersItAndAPinnedBuildMustRunHere() finally { PkgReceipts.RunPkgUtil = previousPkgUtil; - PkgCatalogFeeds.TestCatalogPathOverride = null; + CatalogFeeds.TestCatalogPathOverride = null; } } @@ -968,7 +1069,7 @@ void Declare(params string[] ids) => File.WriteAllText(catalog, "{\"version\":1, + string.Join(',', ids.Select(id => $$"""{"id":"{{id}}","name":"{{id}}","packages":[],"pkgFeed":"https://example.com/{{id}}/"}""")) + "]}"); Declare("a"); - PkgCatalogFeeds.TestCatalogPathOverride = catalog; + CatalogFeeds.TestCatalogPathOverride = catalog; try { var manager = new MacPkg(); @@ -977,7 +1078,7 @@ void Declare(params string[] ids) => File.WriteAllText(catalog, "{\"version\":1, manager.RefreshPackageIndexes(); Assert.Equal(["a", "b"], manager.SourcesHelper.GetSources().Select(s => s.Name)); } - finally { PkgCatalogFeeds.TestCatalogPathOverride = null; } + finally { CatalogFeeds.TestCatalogPathOverride = null; } } // ── Apps ─────────────────────────────────────────────────────────────── @@ -1112,7 +1213,7 @@ public void WithoutAReceiptTheAppInPlaceMarksAPackageInstalled() var previousPkgUtil = PkgReceipts.RunPkgUtil; PkgReceipts.RunPkgUtil = (_, args) => args is ["--pkgs"] ? (0, "com.jamf.appinstallers.GitKraken\n") : (1, ""); - PkgCatalogFeeds.TestCatalogPathOverride = catalog; + CatalogFeeds.TestCatalogPathOverride = catalog; AppBundle.TestRootOverride = root; try { @@ -1127,7 +1228,7 @@ public void WithoutAReceiptTheAppInPlaceMarksAPackageInstalled() finally { PkgReceipts.RunPkgUtil = previousPkgUtil; - PkgCatalogFeeds.TestCatalogPathOverride = null; + CatalogFeeds.TestCatalogPathOverride = null; AppBundle.TestRootOverride = null; } } @@ -1150,7 +1251,7 @@ public void WhenReceiptsCannotBeReadAppsAreStillRecognized() var previousPkgUtil = PkgReceipts.RunPkgUtil; PkgReceipts.RunPkgUtil = (_, _) => (1, ""); - PkgCatalogFeeds.TestCatalogPathOverride = catalog; + CatalogFeeds.TestCatalogPathOverride = catalog; AppBundle.TestRootOverride = root; try { @@ -1160,7 +1261,7 @@ public void WhenReceiptsCannotBeReadAppsAreStillRecognized() finally { PkgReceipts.RunPkgUtil = previousPkgUtil; - PkgCatalogFeeds.TestCatalogPathOverride = null; + CatalogFeeds.TestCatalogPathOverride = null; AppBundle.TestRootOverride = null; } } @@ -1539,6 +1640,16 @@ public async Task ADiskImageHoldingAPackageIsThatPackage() } } + [Fact] + public void AFeedPackageHasNoStandaloneCommandLine() + { + var manager = new MacPkg(); + var source = new Classes.Manager.ManagerSource(manager, "mac", new Uri("https://h/")); + var package = new PackageClasses.Package("App", "com.example.app.pkg", "1.0", source, manager); + Assert.Throws(() => + manager.OperationHelper.GetStandaloneParameters(package, new Serializable.InstallOptions(), Enums.OperationType.Install)); + } + [Fact] public void AnUpdateWithoutANewerVersionInstallsTheNewestPublished() { @@ -1575,7 +1686,7 @@ public async Task AStandardUserInstallsThroughTheHelperWithoutAPrompt() string socket = Path.Combine("/tmp", $"ugpkg-{Guid.NewGuid():N}"[..20] + ".sock"); var previous = (PkgInstaller.RunProcess, PkgInstaller.IsRoot, PkgHelperClient.SocketPath); var commands = new List(); - PkgCatalogFeeds.TestCatalogPathOverride = HelperCatalog(feed); + CatalogFeeds.TestCatalogPathOverride = HelperCatalog(feed); TrustedPath.TestOverride = _ => null; PkgHelperClient.SocketPath = socket; PkgInstaller.IsRoot = () => false; @@ -1602,7 +1713,7 @@ public async Task AStandardUserInstallsThroughTheHelperWithoutAPrompt() stop.Cancel(); await server; (PkgInstaller.RunProcess, PkgInstaller.IsRoot, PkgHelperClient.SocketPath) = previous; - PkgCatalogFeeds.TestCatalogPathOverride = null; + CatalogFeeds.TestCatalogPathOverride = null; TrustedPath.TestOverride = null; } Assert.False(File.Exists(socket)); @@ -1613,7 +1724,7 @@ public async Task TheHelperRefusesWhatTheCatalogDoesNotPublishFromAProtectedPlac { string feed = Path.Combine(_directory, "feed"); WritePackage("feed/App.pkg", ProductArchive("com.example.app.pkg", "1.0", "App")); - PkgCatalogFeeds.TestCatalogPathOverride = HelperCatalog(feed); + CatalogFeeds.TestCatalogPathOverride = HelperCatalog(feed); var previous = PkgInstaller.RunProcess; bool ran = false; PkgInstaller.RunProcess = (_, _, _) => { ran = true; return Task.FromResult((0, "")); }; @@ -1640,7 +1751,7 @@ public async Task TheHelperRefusesWhatTheCatalogDoesNotPublishFromAProtectedPlac // A loopback port is free for any user to take, so a web feed must be https for the helper. ran = false; - PkgCatalogFeeds.TestCatalogPathOverride = WritePackage("http-catalog.json", Encoding.UTF8.GetBytes(""" + CatalogFeeds.TestCatalogPathOverride = WritePackage("http-catalog.json", Encoding.UTF8.GetBytes(""" {"version":1,"catalogs":[{"id":"web","name":"Web","packages":[],"pkgFeed":"http://127.0.0.1:9/","allowUnsignedPackages":true}]} """)); var refusal = await PkgHelperCommand.InstallAsync(new PkgHelperRequest { Catalog = "web", Id = "com.example.app.pkg" }, _ => { }); @@ -1651,7 +1762,7 @@ public async Task TheHelperRefusesWhatTheCatalogDoesNotPublishFromAProtectedPlac finally { PkgInstaller.RunProcess = previous; - PkgCatalogFeeds.TestCatalogPathOverride = null; + CatalogFeeds.TestCatalogPathOverride = null; TrustedPath.TestOverride = null; } } @@ -1665,7 +1776,7 @@ public async Task ARequestAsSentOverTheSocketIsAnsweredWhateverItOmits(string js // Requests are deserialized by the source generator, which leaves absent values null. string feed = Path.Combine(_directory, "feed"); WritePackage("feed/App.pkg", ProductArchive("com.example.app.pkg", "1.0", "App")); - PkgCatalogFeeds.TestCatalogPathOverride = HelperCatalog(feed); + CatalogFeeds.TestCatalogPathOverride = HelperCatalog(feed); TrustedPath.TestOverride = _ => null; var previous = PkgInstaller.RunProcess; PkgInstaller.RunProcess = (_, _, _) => Task.FromResult((0, "accepted")); @@ -1677,7 +1788,7 @@ public async Task ARequestAsSentOverTheSocketIsAnsweredWhateverItOmits(string js finally { PkgInstaller.RunProcess = previous; - PkgCatalogFeeds.TestCatalogPathOverride = null; + CatalogFeeds.TestCatalogPathOverride = null; TrustedPath.TestOverride = null; } } @@ -1726,7 +1837,7 @@ public async Task WhenTheHelperRefusesTheAdministratorPromptIsOffered() string socket = Path.Combine("/tmp", $"ugpkg-{Guid.NewGuid():N}"[..20] + ".sock"); var previous = (PkgInstaller.RunProcess, PkgInstaller.IsRoot, PkgHelperClient.SocketPath); var ran = new List(); - PkgCatalogFeeds.TestCatalogPathOverride = HelperCatalog(feed); + CatalogFeeds.TestCatalogPathOverride = HelperCatalog(feed); TrustedPath.TestOverride = path => path == feed ? "any user can change it." : null; PkgHelperClient.SocketPath = socket; PkgInstaller.IsRoot = () => false; @@ -1751,7 +1862,7 @@ public async Task WhenTheHelperRefusesTheAdministratorPromptIsOffered() stop.Cancel(); await server; (PkgInstaller.RunProcess, PkgInstaller.IsRoot, PkgHelperClient.SocketPath) = previous; - PkgCatalogFeeds.TestCatalogPathOverride = null; + CatalogFeeds.TestCatalogPathOverride = null; TrustedPath.TestOverride = null; } } diff --git a/src/UniGetUI.PackageEngine.Tests/UniGetUI.PackageEngine.Tests.csproj b/src/UniGetUI.PackageEngine.Tests/UniGetUI.PackageEngine.Tests.csproj index 2f0de906ae..78f34a13cb 100644 --- a/src/UniGetUI.PackageEngine.Tests/UniGetUI.PackageEngine.Tests.csproj +++ b/src/UniGetUI.PackageEngine.Tests/UniGetUI.PackageEngine.Tests.csproj @@ -33,6 +33,7 @@ + @@ -50,6 +51,7 @@ + diff --git a/src/UniGetUI.PackageEngine.Tests/WinInstallerEndToEndTests.cs b/src/UniGetUI.PackageEngine.Tests/WinInstallerEndToEndTests.cs new file mode 100644 index 0000000000..37c4b1c9de --- /dev/null +++ b/src/UniGetUI.PackageEngine.Tests/WinInstallerEndToEndTests.cs @@ -0,0 +1,593 @@ +#if WINDOWS +using System.Diagnostics; +using System.Net; +using System.Runtime.InteropServices; +using System.Text; +using Microsoft.Win32; +using UniGetUI.Core.Tools; +using UniGetUI.PackageEngine.Enums; +using Architecture = System.Runtime.InteropServices.Architecture; +using UniGetUI.PackageEngine.Interfaces; +using UniGetUI.PackageEngine.Interfaces.ManagerProviders; +using UniGetUI.PackageEngine.Managers.InstallerFeeds; +using UniGetUI.PackageEngine.Managers.WinInstallerManager; +using UniGetUI.PackageEngine.Managers.WinInstallerManager.Internal; +using UniGetUI.PackageEngine.Operations; +using UniGetUI.PackageEngine.PackageLoader; +using UniGetUI.PackageOperations; +using UniGetUI.PackageEngine.PackageClasses; +using UniGetUI.PackageEngine.Serializable; + +namespace UniGetUI.PackageEngine.Tests; + +public sealed class InstallerEndToEndFactAttribute : FactAttribute +{ + public const string Variable = "UNIGETUI_INSTALLER_E2E"; + + public InstallerEndToEndFactAttribute(bool needsInno = false, bool elevated = false) + { + string? mode = Environment.GetEnvironmentVariable(Variable); + if (mode is null || mode == "0") + Skip = $"Installs real packages for the current user; set {Variable}=1 to run."; + else if (elevated && mode != "elevated") + Skip = $"Installs for every user behind a UAC prompt; set {Variable}=elevated to run."; + else if (needsInno && WinInstallerEndToEndTests.InnoCompiler is null) + Skip = "Inno Setup (ISCC.exe) is not installed."; + } +} + +[Collection("Installer feeds")] +public sealed class WinInstallerEndToEndTests : IDisposable +{ + private const string Vendor = "UniGetUI Test Vendor"; + private readonly string _root = Path.Combine(Path.GetTempPath(), "UniGetUI-E2E", Guid.NewGuid().ToString("N")[..8]); + private readonly string _feed; + private readonly string _tag = Guid.NewGuid().ToString("N")[..6]; + private readonly List _log = []; + private readonly List _productCodes = []; + private readonly List _innoFolders = []; + private readonly List _registryKeys = []; + + public static string? InnoCompiler => + new[] + { + Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ProgramFilesX86), "Inno Setup 6", "ISCC.exe"), + Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ProgramFiles), "Inno Setup 6", "ISCC.exe"), + }.FirstOrDefault(File.Exists); + + public WinInstallerEndToEndTests() + { + _feed = Path.Combine(_root, "feed"); + Directory.CreateDirectory(_feed); + Directory.CreateDirectory(Path.Combine(_root, "build")); + WinFeed.TestIconDirectoryOverride = Path.Combine(_root, "icons"); + InstallReceipts.TestPathOverride = Path.Combine(_root, "receipts.json"); + WinFeed.ForgetReadFiles(); + WinFeed.ForgetCachedIndexes(); + WinInstallerRunner.IsAdministrator = CoreTools.IsAdministrator; + bool elevatedMode = Environment.GetEnvironmentVariable(InstallerEndToEndFactAttribute.Variable) == "elevated"; + WinInstallerRunner.RunProcess = process => process.Elevated && !elevatedMode + ? throw new InvalidOperationException($"Asked for administrator rights to run {process.FileName} {process.Arguments}") + : WinInstallerRunner.RunProcessAsync(process); + WinInstallerRunner.UninstallSettleTime = TimeSpan.FromMinutes(2); + WinInstallerRunner.UninstallPollInterval = TimeSpan.FromMilliseconds(500); + Authenticode.Verify = Authenticode.VerifyFile; + InstalledMatcher.RelatedProducts = MsiDatabase.RelatedProducts; + ArpEntries.Source = ArpEntries.ReadRegistry; + Catalog(_feed, allowUnsigned: true); + } + + public void Dispose() + { + foreach (string code in _productCodes) + Run(WinInstallerRunner.MsiExecPath, $"/x {code} /qn /norestart"); + foreach (string folder in _innoFolders.Where(Directory.Exists)) + foreach (string uninstaller in Directory.GetFiles(folder, "unins*.exe")) + Run(uninstaller, "/VERYSILENT /SUPPRESSMSGBOXES /NORESTART"); + foreach (string key in _registryKeys) + Registry.CurrentUser.DeleteSubKeyTree(key, throwOnMissingSubKey: false); + WinFeed.TestIconDirectoryOverride = null; + InstallReceipts.TestPathOverride = null; + CatalogFeeds.TestCatalogPathOverride = null; + WinFeed.ForgetReadFiles(); + WinFeed.ForgetCachedIndexes(); + try { Directory.Delete(_root, recursive: true); } + catch (IOException) { } + catch (UnauthorizedAccessException) { } + } + + private void Catalog(string feed, bool allowUnsigned) => + CatalogFeeds.TestCatalogPathOverride = Write(Path.Combine(_root, "SoftwareCatalog.json"), $$""" + { "version": 1, "catalogs": [ { "id": "e2e", "name": "End to end", "packages": [], + "pkgFeed": "{{feed.Replace(@"\", @"\\")}}", "allowUnsignedPackages": {{(allowUnsigned ? "true" : "false")}} } ] } + """); + + private static string Write(string path, string text) + { + Directory.CreateDirectory(Path.GetDirectoryName(path)!); + File.WriteAllText(path, text); + return path; + } + + private static int Run(string file, string arguments) + { + using var process = Process.Start(new ProcessStartInfo(file, arguments) { UseShellExecute = false, CreateNoWindow = true })!; + process.WaitForExit(TimeSpan.FromMinutes(5)); + return process.ExitCode; + } + + private WinInstaller Manager() + { + var manager = new WinInstaller(); + manager.RefreshPackageIndexes(); + WinFeed.ForgetReadFiles(); + return manager; + } + + private async Task Perform(WinInstaller manager, IPackage package, OperationType operation) + { + var helper = (IManagedPackageOperationHelper)manager.OperationHelper; + var verdict = await helper.TryPerformAsync(package, new InstallOptions(), operation, _log.Add, _ => { }, CancellationToken.None); + _log.Add($"=> {operation} {package.Id}: {verdict}"); + return verdict; + } + + private static Package Offered(WinInstaller manager, string id) + { + var (source, item) = manager.ListLatest().Single(p => p.Item.Id == id); + return new Package(item.Name, item.Id, item.Version, source, manager); + } + + private static Package Upgradable(WinInstaller manager, string id) + { + var installed = manager.ListInstalled().Single(p => p.Item.Id == id); + Assert.True(installed.HasUpdate); + return new Package(installed.Item.Name, installed.Item.Id, installed.Installed, installed.Item.Version, installed.Source, manager); + } + + private static Package Installed(WinInstaller manager, string id) + { + var installed = manager.ListInstalled().Single(p => p.Item.Id == id); + return new Package(installed.Item.Name, installed.Item.Id, installed.Installed, installed.Source, manager); + } + + private string Log => string.Join(Environment.NewLine, _log); + + private void Succeeded(OperationVeredict? verdict) => Assert.True(verdict == OperationVeredict.Success, Log); + + private static IReadOnlyList EntriesNamed(string name) => + ArpEntries.ReadRegistry().Where(e => e.DisplayName?.Contains(name, StringComparison.Ordinal) == true).ToArray(); + + // ── Windows Installer ───────────────────────────────────────────────── + + [InstallerEndToEndFact] + public async Task AnMsiInstallsUpdatesAndUninstallsForTheUser() + { + string name = $"UniGetUI E2E Msi {_tag}"; + string upgradeCode = Guid.NewGuid().ToString("B").ToUpperInvariant(); + BuildMsi(Path.Combine(_feed, "Example-1.0.msi"), name, "1.0.0", upgradeCode); + + var manager = Manager(); + Assert.Empty(manager.ListInstalled()); + Succeeded(await Perform(manager, Offered(manager, upgradeCode), OperationType.Install)); + Assert.Equal("1.0.0", MsiRegistryVersion(name)); + var first = Assert.Single(EntriesNamed(name)); + Assert.False(first.PerMachine, Log); + Assert.True(first.WindowsInstaller); + + manager = Manager(); + var installed = Assert.Single(manager.ListInstalled()); + Assert.Equal(("1.0.0", false), (installed.Installed, installed.HasUpdate)); + + BuildMsi(Path.Combine(_feed, "Example-2.0.msi"), name, "2.0.0", upgradeCode); + manager = Manager(); + Succeeded(await Perform(manager, Upgradable(manager, upgradeCode), OperationType.Update)); + Assert.Equal("2.0.0", MsiRegistryVersion(name)); + var second = Assert.Single(EntriesNamed(name)); + Assert.NotEqual(first.Key, second.Key); + Assert.Equal("2.0.0", second.DisplayVersion); + manager = Manager(); + Assert.False(Assert.Single(manager.ListInstalled()).HasUpdate, Log); + + Succeeded(await Perform(manager, Installed(manager, upgradeCode), OperationType.Uninstall)); + Assert.Empty(EntriesNamed(name)); + Assert.Null(MsiRegistryVersion(name)); + Assert.Empty(Manager().ListInstalled()); + } + + [InstallerEndToEndFact] + public async Task TheAppsOperationsInstallUpdateAndUninstallAFeedPackage() + { + _ = new DiscoverablePackagesLoader([]); + _ = new UpgradablePackagesLoader([]); + _ = new InstalledPackagesLoader([]); + string name = $"UniGetUI E2E Operations {_tag}"; + string upgradeCode = Guid.NewGuid().ToString("B").ToUpperInvariant(); + BuildMsi(Path.Combine(_feed, "Ops-1.0.msi"), name, "1.0.0", upgradeCode); + + var manager = Manager(); + using (var install = new InstallPackageOperation(Offered(manager, upgradeCode), new InstallOptions(), IgnoreParallelInstalls: true)) + await Completes(install); + Assert.Equal("1.0.0", MsiRegistryVersion(name)); + + BuildMsi(Path.Combine(_feed, "Ops-2.0.msi"), name, "2.0.0", upgradeCode); + manager = Manager(); + using (var update = new UpdatePackageOperation(Upgradable(manager, upgradeCode), new InstallOptions(), IgnoreParallelInstalls: true)) + await Completes(update); + Assert.Equal("2.0.0", MsiRegistryVersion(name)); + + manager = Manager(); + using (var uninstall = new UninstallPackageOperation(Installed(manager, upgradeCode), new InstallOptions(), IgnoreParallelInstalls: true)) + await Completes(uninstall); + Assert.Empty(EntriesNamed(name)); + Assert.Null(MsiRegistryVersion(name)); + } + + private static async Task Completes(AbstractOperation operation) + { + await operation.MainThread().WaitAsync(TimeSpan.FromMinutes(5)); + string output = string.Join(Environment.NewLine, operation.GetOutput().Select(l => l.Item1)); + Assert.True(operation.Status == OperationStatus.Succeeded, output); + } + + [InstallerEndToEndFact] + public async Task AWebFeedInstallsThroughItsIndexAndRefusesATamperedFile() + { + string name = $"UniGetUI E2E Web {_tag}"; + string upgradeCode = Guid.NewGuid().ToString("B").ToUpperInvariant(); + string msi = BuildMsi(Path.Combine(_feed, "web", "Web.msi"), name, "3.1.0", upgradeCode); + string folder = Path.GetDirectoryName(msi)!; + Assert.Equal(0, FeedIndexCommand.Run([folder], new StringWriter(), new StringWriter(), new WinInstallerIndexer())); + + using var server = new FolderServer(folder); + Catalog(server.Url, allowUnsigned: true); + var manager = Manager(); + var offered = Offered(manager, upgradeCode); + Assert.Equal("3.1.0", offered.VersionString); + Assert.StartsWith(server.Url, manager.FindFeedItem(offered)!.Location.AbsoluteUri); + + byte[] original = File.ReadAllBytes(msi); + File.WriteAllBytes(msi, [.. original, 0]); + await Assert.ThrowsAsync(() => Perform(manager, offered, OperationType.Install)); + Assert.Empty(EntriesNamed(name)); + + File.WriteAllBytes(msi, original); + Succeeded(await Perform(manager, offered, OperationType.Install)); + Assert.Contains(_log, l => l.Contains("Downloading", StringComparison.Ordinal)); + Assert.Equal("3.1.0", Assert.Single(Manager().ListInstalled()).Installed); + Succeeded(await Perform(manager, Installed(manager, upgradeCode), OperationType.Uninstall)); + Assert.Empty(EntriesNamed(name)); + } + + [InstallerEndToEndFact] + public async Task AnUnsignedInstallerIsRefusedWithoutItsCatalogsPermission() + { + string name = $"UniGetUI E2E Unsigned {_tag}"; + string upgradeCode = Guid.NewGuid().ToString("B").ToUpperInvariant(); + string msi = BuildMsi(Path.Combine(_feed, "Unsigned.msi"), name, "1.0.0", upgradeCode); + Assert.False(Authenticode.VerifyFile(msi).IsValid); + + Catalog(_feed, allowUnsigned: false); + var manager = Manager(); + var error = await Assert.ThrowsAsync(() => Perform(manager, Offered(manager, upgradeCode), OperationType.Install)); + Assert.Contains("not signed", error.Message); + Assert.Empty(EntriesNamed(name)); + } + + // ── Inno Setup ──────────────────────────────────────────────────────── + + [InstallerEndToEndFact(needsInno: true)] + public async Task AnInnoInstallerIsRecognizedByNameAcrossAMajorUpdate() + { + string name = $"UniGetUI E2E Inno {_tag}"; + string appId = Guid.NewGuid().ToString("B").ToUpperInvariant(); + BuildInno(Path.Combine(_feed, "Inno-1.5.exe"), name, appId, "1.5"); + + var manager = Manager(); + var offered = Assert.Single(manager.ListLatest()); + Assert.Equal((InstallerKind.Inno, "1.5"), (offered.Item.Details.Kind, offered.Item.Version)); + Succeeded(await Perform(manager, Offered(manager, offered.Item.Id), OperationType.Install)); + var entry = Assert.Single(EntriesNamed(name)); + Assert.Equal(($"{appId}_is1", false), (entry.Key, entry.PerMachine)); + Assert.True(File.Exists(Path.Combine(InnoFolder(name), "payload.txt"))); + + BuildInno(Path.Combine(_feed, "Inno-2.0.exe"), name, appId, "2.0"); + manager = Manager(); + Succeeded(await Perform(manager, Upgradable(manager, offered.Item.Id), OperationType.Update)); + Assert.Equal("2.0", Assert.Single(EntriesNamed(name)).DisplayVersion); + manager = Manager(); + var installed = Assert.Single(manager.ListInstalled()); + Assert.Equal(("2.0", false), (installed.Installed, installed.HasUpdate)); + + Succeeded(await Perform(manager, Installed(manager, offered.Item.Id), OperationType.Uninstall)); + Assert.Empty(EntriesNamed(name)); + Assert.False(File.Exists(Path.Combine(InnoFolder(name), "payload.txt"))); + } + + [InstallerEndToEndFact(needsInno: true)] + public async Task AnInstallerRegisteringAnotherVersionIsNotOfferedAgain() + { + string name = $"UniGetUI E2E Build {_tag}"; + string appId = Guid.NewGuid().ToString("B").ToUpperInvariant(); + BuildInno(Path.Combine(_feed, "Build.exe"), name, appId, "4.2", productVersion: "4.2.0.1234"); + + var manager = Manager(); + var item = Assert.Single(manager.ListLatest()).Item; + Assert.Equal("4.2.0.1234", item.Version); + Succeeded(await Perform(manager, Offered(manager, item.Id), OperationType.Install)); + Assert.Equal("4.2", Assert.Single(EntriesNamed(name)).DisplayVersion); + + var installed = Assert.Single(Manager().ListInstalled()); + Assert.Equal(("4.2.0.1234", false), (installed.Installed, installed.HasUpdate)); + + InstallReceipts.TestPathOverride = Path.Combine(_root, "no-receipts.json"); + Assert.True(Assert.Single(Manager().ListInstalled()).HasUpdate); + } + + [InstallerEndToEndFact(needsInno: true)] + public async Task AManifestDrivesAnInstallerUniGetUIDoesNotRecognize() + { + string name = $"UniGetUI E2E Manifest {_tag}"; + string appId = Guid.NewGuid().ToString("B").ToUpperInvariant(); + string exe = BuildInno(Path.Combine(_feed, "Vendor.exe"), name, appId, "7.0"); + Write(Path.ChangeExtension(exe, ".json"), $$""" + { + "id": "Vendor.Manifest", + "name": "Manifest app", + "installer": "exe", + "installArguments": "/VERYSILENT /SUPPRESSMSGBOXES /NORESTART", + "uninstallArguments": "/VERYSILENT /SUPPRESSMSGBOXES /NORESTART", + "uninstallKey": "{{appId}}_is1", + "scope": "user" + } + """); + + var manager = Manager(); + var item = Assert.Single(manager.ListLatest()).Item; + Assert.Equal(("Vendor.Manifest", InstallerKind.Exe), (item.Id, item.Details.Kind)); + Succeeded(await Perform(manager, Offered(manager, item.Id), OperationType.Install)); + manager = Manager(); + Assert.Equal("7.0", Assert.Single(manager.ListInstalled()).Installed); + Succeeded(await Perform(manager, Installed(manager, item.Id), OperationType.Uninstall)); + Assert.Empty(EntriesNamed(name)); + } + + // ── Every user ──────────────────────────────────────────────────────── + + [InstallerEndToEndFact(elevated: true)] + public async Task AnMsiForEveryUserInstallsAndUninstallsBehindUac() + { + string name = $"UniGetUI E2E Machine {_tag}"; + string upgradeCode = Guid.NewGuid().ToString("B").ToUpperInvariant(); + BuildMsi(Path.Combine(_feed, "Machine.msi"), name, "1.0.0", upgradeCode, perMachine: true); + + var manager = Manager(); + Assert.True(Assert.Single(manager.ListLatest()).Item.Details.PerMachine); + Succeeded(await Perform(manager, Offered(manager, upgradeCode), OperationType.Install)); + Assert.True(Assert.Single(EntriesNamed(name)).PerMachine, Log); + manager = Manager(); + Succeeded(await Perform(manager, Installed(manager, upgradeCode), OperationType.Uninstall)); + Assert.Empty(EntriesNamed(name)); + } + + [InstallerEndToEndFact(needsInno: true, elevated: true)] + public async Task AnInnoInstallerForEveryUserElevatesItself() + { + string name = $"UniGetUI E2E Admin {_tag}"; + string appId = Guid.NewGuid().ToString("B").ToUpperInvariant(); + BuildInno(Path.Combine(_feed, "Admin.exe"), name, appId, "1.0", perMachine: true); + + var manager = Manager(); + Succeeded(await Perform(manager, Offered(manager, Assert.Single(manager.ListLatest()).Item.Id), OperationType.Install)); + Assert.True(Assert.Single(EntriesNamed(name)).PerMachine, Log); + manager = Manager(); + Succeeded(await Perform(manager, Installed(manager, Assert.Single(manager.ListInstalled()).Item.Id), OperationType.Uninstall)); + Assert.Empty(EntriesNamed(name)); + } + + // ── Fixtures ────────────────────────────────────────────────────────── + + private string InnoFolder(string name, bool perMachine = false) => Path.Combine( + perMachine ? Environment.GetFolderPath(Environment.SpecialFolder.ProgramFilesX86) + : Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "Programs"), + "UniGetUI E2E", name); + + private string BuildInno(string output, string name, string appId, string version, string? productVersion = null, bool perMachine = false) + { + string build = Path.Combine(_root, "build", Guid.NewGuid().ToString("N")); + Directory.CreateDirectory(build); + File.WriteAllText(Path.Combine(build, "payload.txt"), $"{name} {version}"); + string numeric = string.Join('.', (productVersion ?? version).Split('.').Concat(["0", "0", "0"]).Take(4)); + string script = string.Join(Environment.NewLine, + "[Setup]", + "AppId={" + appId, + "AppName=" + name, + "AppVersion=" + version, + "AppPublisher=" + Vendor, + "VersionInfoVersion=" + numeric, + "VersionInfoProductName=" + name, + "VersionInfoProductTextVersion=" + (productVersion ?? version), + "VersionInfoCompany=" + Vendor, + @"DefaultDirName={" + (perMachine ? "autopf" : "localappdata") + @"}" + (perMachine ? "" : @"\Programs") + @"\UniGetUI E2E\" + name, + "PrivilegesRequired=" + (perMachine ? "admin" : "lowest"), + "DisableProgramGroupPage=yes", + "DisableDirPage=yes", + "Compression=zip", + "OutputDir=" + build, + "OutputBaseFilename=setup", + "", + "[Files]", + "Source: \"" + Path.Combine(build, "payload.txt") + "\"; DestDir: \"{app}\""); + string iss = Path.Combine(build, "setup.iss"); + File.WriteAllText(iss, script); + var compile = Process.Start(new ProcessStartInfo(WinInstallerEndToEndTests.InnoCompiler!, $"/Q \"{iss}\"") + { + UseShellExecute = false, + CreateNoWindow = true, + RedirectStandardOutput = true, + RedirectStandardError = true, + })!; + string errors = compile.StandardError.ReadToEnd() + compile.StandardOutput.ReadToEnd(); + compile.WaitForExit(); + Assert.True(compile.ExitCode == 0, errors); + Directory.CreateDirectory(Path.GetDirectoryName(output)!); + File.Copy(Path.Combine(build, "setup.exe"), output, overwrite: true); + _innoFolders.Add(InnoFolder(name, perMachine)); + return output; + } + + private string? MsiRegistryVersion(string name) + { + using var key = Registry.CurrentUser.OpenSubKey(@"Software\UniGetUI E2E\" + name); + return key?.GetValue("Version") as string; + } + + private string BuildMsi(string path, string name, string version, string upgradeCode, bool perMachine = false) + { + Directory.CreateDirectory(Path.GetDirectoryName(path)!); + string productCode = Guid.NewGuid().ToString("B").ToUpperInvariant(); + _productCodes.Add(productCode); + _registryKeys.Add(@"Software\UniGetUI E2E\" + name); + Check(MsiOpenDatabase(path, 3, out nint database)); + try + { + string[] tables = + [ + "CREATE TABLE `Property` (`Property` CHAR(72) NOT NULL, `Value` LONGCHAR NOT NULL LOCALIZABLE PRIMARY KEY `Property`)", + "CREATE TABLE `Directory` (`Directory` CHAR(72) NOT NULL, `Directory_Parent` CHAR(72), `DefaultDir` CHAR(255) NOT NULL LOCALIZABLE PRIMARY KEY `Directory`)", + "CREATE TABLE `Component` (`Component` CHAR(72) NOT NULL, `ComponentId` CHAR(38), `Directory_` CHAR(72) NOT NULL, `Attributes` SHORT NOT NULL, `Condition` CHAR(255), `KeyPath` CHAR(72) PRIMARY KEY `Component`)", + "CREATE TABLE `Registry` (`Registry` CHAR(72) NOT NULL, `Root` SHORT NOT NULL, `Key` CHAR(255) NOT NULL LOCALIZABLE, `Name` CHAR(255) LOCALIZABLE, `Value` LONGCHAR LOCALIZABLE, `Component_` CHAR(72) NOT NULL PRIMARY KEY `Registry`)", + "CREATE TABLE `Feature` (`Feature` CHAR(38) NOT NULL, `Feature_Parent` CHAR(38), `Title` CHAR(64) LOCALIZABLE, `Description` CHAR(255) LOCALIZABLE, `Display` SHORT, `Level` SHORT NOT NULL, `Directory_` CHAR(72), `Attributes` SHORT NOT NULL PRIMARY KEY `Feature`)", + "CREATE TABLE `FeatureComponents` (`Feature_` CHAR(38) NOT NULL, `Component_` CHAR(72) NOT NULL PRIMARY KEY `Feature_`, `Component_`)", + "CREATE TABLE `Media` (`DiskId` SHORT NOT NULL, `LastSequence` LONG NOT NULL, `DiskPrompt` CHAR(64) LOCALIZABLE, `Cabinet` CHAR(255), `VolumeLabel` CHAR(32), `Source` CHAR(72) PRIMARY KEY `DiskId`)", + "CREATE TABLE `InstallExecuteSequence` (`Action` CHAR(72) NOT NULL, `Condition` CHAR(255), `Sequence` SHORT PRIMARY KEY `Action`)", + "CREATE TABLE `Upgrade` (`UpgradeCode` CHAR(38) NOT NULL, `VersionMin` CHAR(20), `VersionMax` CHAR(20), `Language` CHAR(255), `Attributes` LONG NOT NULL, `Remove` CHAR(255), `ActionProperty` CHAR(72) NOT NULL PRIMARY KEY `UpgradeCode`, `VersionMin`, `VersionMax`, `Language`, `Attributes`)", + ]; + foreach (string table in tables) + Execute(database, table); + var properties = new Dictionary + { + ["ProductCode"] = productCode, + ["UpgradeCode"] = upgradeCode, + ["ProductName"] = name, + ["ProductVersion"] = version, + ["Manufacturer"] = Vendor, + ["ProductLanguage"] = "1033", + ["SecureCustomProperties"] = "OLDERFOUND", + ["ALLUSERS"] = perMachine ? "1" : "2", + }; + if (!perMachine) + properties["MSIINSTALLPERUSER"] = "1"; + foreach (var (key, value) in properties) + Execute(database, $"INSERT INTO `Property` (`Property`, `Value`) VALUES ('{key}', '{value}')"); + Execute(database, "INSERT INTO `Directory` (`Directory`, `DefaultDir`) VALUES ('TARGETDIR', 'SourceDir')"); + Execute(database, "INSERT INTO `Media` (`DiskId`, `LastSequence`) VALUES (1, 0)"); + Execute(database, $"INSERT INTO `Component` (`Component`, `ComponentId`, `Directory_`, `Attributes`, `KeyPath`) VALUES ('Main', '{Guid.NewGuid().ToString("B").ToUpperInvariant()}', 'TARGETDIR', 4, 'Version')"); + Execute(database, $"INSERT INTO `Registry` (`Registry`, `Root`, `Key`, `Name`, `Value`, `Component_`) VALUES ('Version', 1, 'Software\\UniGetUI E2E\\{name}', 'Version', '{version}', 'Main')"); + Execute(database, "INSERT INTO `Feature` (`Feature`, `Title`, `Display`, `Level`, `Attributes`) VALUES ('Main', 'Main', 0, 1, 0)"); + Execute(database, "INSERT INTO `FeatureComponents` (`Feature_`, `Component_`) VALUES ('Main', 'Main')"); + Execute(database, $"INSERT INTO `Upgrade` (`UpgradeCode`, `VersionMin`, `VersionMax`, `Attributes`, `ActionProperty`) VALUES ('{upgradeCode}', '0.0.0', '{version}', 256, 'OLDERFOUND')"); + (string Action, int Sequence, string? Condition)[] sequence = + [ + ("FindRelatedProducts", 25, null), ("CostInitialize", 800, null), ("FileCost", 900, null), + ("CostFinalize", 1000, null), ("InstallValidate", 1400, null), ("InstallInitialize", 1500, null), + ("RemoveExistingProducts", 1510, null), ("ProcessComponents", 1600, null), ("UnpublishFeatures", 1800, null), + ("RemoveRegistryValues", 2600, null), ("WriteRegistryValues", 5000, null), ("RegisterUser", 6000, null), + ("RegisterProduct", 6100, null), ("PublishFeatures", 6300, null), ("PublishProduct", 6400, null), + ("InstallFinalize", 6600, null), + ]; + foreach (var (action, number, condition) in sequence) + Execute(database, condition is null + ? $"INSERT INTO `InstallExecuteSequence` (`Action`, `Sequence`) VALUES ('{action}', {number})" + : $"INSERT INTO `InstallExecuteSequence` (`Action`, `Condition`, `Sequence`) VALUES ('{action}', '{condition}', {number})"); + + Check(MsiGetSummaryInformation(database, null, 6, out nint summary)); + Check(MsiSummaryInfoSetProperty(summary, 7, 30, 0, 0, RuntimeInformation.OSArchitecture == Architecture.X86 ? "Intel;1033" : "x64;1033")); + Check(MsiSummaryInfoSetProperty(summary, 9, 30, 0, 0, Guid.NewGuid().ToString("B").ToUpperInvariant())); + Check(MsiSummaryInfoSetProperty(summary, 14, 3, 200, 0, null)); + Check(MsiSummaryInfoSetProperty(summary, 15, 3, 2, 0, null)); + Check(MsiSummaryInfoSetProperty(summary, 2, 30, 0, 0, name)); + Check(MsiSummaryInfoSetProperty(summary, 4, 30, 0, 0, Vendor)); + Check(MsiSummaryInfoPersist(summary)); + MsiCloseHandle(summary); + Check(MsiDatabaseCommit(database)); + } + finally { MsiCloseHandle(database); } + return path; + + static void Execute(nint database, string query) + { + Check(MsiDatabaseOpenView(database, query, out nint view)); + try { Check(MsiViewExecute(view, 0)); } + finally { MsiCloseHandle(view); } + } + + static void Check(uint result) => Assert.Equal(0u, result); + } + + private sealed class FolderServer : IDisposable + { + private readonly HttpListener _listener = new(); + private readonly string _folder; + + public FolderServer(string folder) + { + _folder = folder; + int port = Random.Shared.Next(40000, 60000); + Url = $"http://localhost:{port}/feed/"; + _listener.Prefixes.Add(Url); + _listener.Start(); + _ = Task.Run(ServeAsync); + } + + public string Url { get; } + + private async Task ServeAsync() + { + while (_listener.IsListening) + { + HttpListenerContext context; + try { context = await _listener.GetContextAsync(); } + catch (Exception) { return; } + string relative = Uri.UnescapeDataString(context.Request.Url!.AbsolutePath["/feed/".Length..]); + string file = Path.Combine(_folder, relative.Replace('/', Path.DirectorySeparatorChar)); + if (File.Exists(file)) + { + byte[] body = File.ReadAllBytes(file); + context.Response.ContentLength64 = body.Length; + await context.Response.OutputStream.WriteAsync(body); + } + else + context.Response.StatusCode = 404; + context.Response.Close(); + } + } + + public void Dispose() => _listener.Close(); + } + + [DllImport("msi.dll", CharSet = CharSet.Unicode, EntryPoint = "MsiOpenDatabaseW")] + private static extern uint MsiOpenDatabase(string path, nint persist, out nint database); + + [DllImport("msi.dll", CharSet = CharSet.Unicode, EntryPoint = "MsiDatabaseOpenViewW")] + private static extern uint MsiDatabaseOpenView(nint database, string query, out nint view); + + [DllImport("msi.dll")] + private static extern uint MsiViewExecute(nint view, nint record); + + [DllImport("msi.dll")] + private static extern uint MsiDatabaseCommit(nint database); + + [DllImport("msi.dll", CharSet = CharSet.Unicode, EntryPoint = "MsiGetSummaryInformationW")] + private static extern uint MsiGetSummaryInformation(nint database, string? path, uint updateCount, out nint summary); + + [DllImport("msi.dll", CharSet = CharSet.Unicode, EntryPoint = "MsiSummaryInfoSetPropertyW")] + private static extern uint MsiSummaryInfoSetProperty(nint summary, uint property, uint dataType, int integer, nint fileTime, string? value); + + [DllImport("msi.dll")] + private static extern uint MsiSummaryInfoPersist(nint summary); + + [DllImport("msi.dll")] + private static extern uint MsiCloseHandle(nint handle); +} +#endif diff --git a/src/UniGetUI.PackageEngine.Tests/WinInstallerTests.cs b/src/UniGetUI.PackageEngine.Tests/WinInstallerTests.cs new file mode 100644 index 0000000000..30c2e295bb --- /dev/null +++ b/src/UniGetUI.PackageEngine.Tests/WinInstallerTests.cs @@ -0,0 +1,802 @@ +#if WINDOWS +using System.Diagnostics; +using System.Runtime.InteropServices; +using System.Text; +using UniGetUI.PackageEngine.Enums; +using Architecture = System.Runtime.InteropServices.Architecture; +using UniGetUI.PackageEngine.Managers.InstallerFeeds; +using UniGetUI.PackageEngine.Managers.MacPkgManager.Internal; +using UniGetUI.PackageEngine.Managers.WinInstallerManager; +using UniGetUI.PackageEngine.Managers.WinInstallerManager.Internal; + +namespace UniGetUI.PackageEngine.Tests; + +[Collection("Installer feeds")] +public sealed class WinInstallerTests : IDisposable +{ + private readonly string _directory = Path.Combine(Path.GetTempPath(), nameof(WinInstallerTests), Guid.NewGuid().ToString("N")); + private readonly List _started = []; + private int _exitCode; + + private static readonly string Cmd = Path.Combine(Environment.SystemDirectory, "cmd.exe"); + private const string UpgradeCode = "{11111111-2222-3333-4444-555555555555}"; + private const string ProductCode = "{AAAAAAAA-BBBB-CCCC-DDDD-EEEEEEEEEEEE}"; + + public WinInstallerTests() + { + Directory.CreateDirectory(_directory); + WinFeed.TestIconDirectoryOverride = Path.Combine(_directory, "icons"); + InstallReceipts.TestPathOverride = Path.Combine(_directory, "receipts.json"); + WinFeed.ForgetReadFiles(); + WinFeed.ForgetCachedIndexes(); + WinInstallerRunner.IsAdministrator = () => false; + WinInstallerRunner.RunProcess = process => + { + _started.Add(process); + return Task.FromResult(_exitCode); + }; + WinInstallerRunner.UninstallPollInterval = TimeSpan.FromMilliseconds(10); + WinInstallerRunner.UninstallSettleTime = TimeSpan.FromMilliseconds(100); + Authenticode.Verify = _ => new SignatureCheck(0, "Example Corp"); + InstalledMatcher.RelatedProducts = _ => []; + ArpEntries.Source = () => []; + } + + public void Dispose() + { + WinFeed.TestIconDirectoryOverride = null; + InstallReceipts.TestPathOverride = null; + CatalogFeeds.TestCatalogPathOverride = null; + WinInstallerRunner.IsAdministrator = UniGetUI.Core.Tools.CoreTools.IsAdministrator; + WinInstallerRunner.RunProcess = WinInstallerRunner.RunProcessAsync; + Authenticode.Verify = Authenticode.VerifyFile; + InstalledMatcher.RelatedProducts = MsiDatabase.RelatedProducts; + ArpEntries.Source = ArpEntries.ReadRegistry; + WinFeed.ForgetReadFiles(); + WinFeed.ForgetCachedIndexes(); + Directory.Delete(_directory, recursive: true); + } + + private string WriteFile(string relative, byte[] contents) + { + string path = Path.Combine(_directory, relative); + Directory.CreateDirectory(Path.GetDirectoryName(path)!); + File.WriteAllBytes(path, contents); + return path; + } + + private static byte[] NsisOverlay() + { + byte[] header = new byte[28]; + BitConverter.GetBytes(0xDEADBEEF).CopyTo(header, 4); + Encoding.ASCII.GetBytes("NullsoftInst").CopyTo(header, 8); + return header; + } + + private string Installer(string relative, byte[]? overlay = null) => + WriteFile(relative, [.. File.ReadAllBytes(Cmd), .. overlay ?? NsisOverlay()]); + + private string Manifest(string installer, string json) => WriteFile(Path.ChangeExtension(Path.GetRelativePath(_directory, installer), ".json"), + Encoding.UTF8.GetBytes(json)); + + private static string CmdVersion() => InstallerFile.CleanVersion(PeReader.Read(Cmd).String("ProductVersion"))!; + + private static ArpEntry Entry(string key, string? name = null, string? version = null, string? publisher = null, + bool perMachine = true, bool msi = false, bool hidden = false, string? uninstall = null, string? quiet = null, + params string[] bundleCodes) => + new(key, perMachine, name, version, publisher, uninstall, quiet, msi, hidden, bundleCodes); + + private static WinFeedItem Item(string id, string name, string version, InstallerDetails details, params string[] architectures) => + new(id, name, version, new Uri(@"C:\feed\" + id + (details.Kind == InstallerKind.Msi ? ".msi" : ".exe")), null, 1, + architectures, null, details); + + [Fact] + public void ThePeReaderAgreesWithWindowsOnVersionResources() + { + foreach (string file in new[] { Environment.ProcessPath!, typeof(object).Assembly.Location, typeof(WinInstallerTests).Assembly.Location }) + { + var pe = PeReader.Read(file); + var expected = FileVersionInfo.GetVersionInfo(file); + Assert.Equal(expected.ProductName, pe.String("ProductName")); + Assert.Equal(expected.CompanyName, pe.String("CompanyName")); + Assert.Equal(expected.ProductVersion, pe.String("ProductVersion")); + Assert.Equal($"{expected.FileMajorPart}.{expected.FileMinorPart}.{expected.FileBuildPart}.{expected.FilePrivatePart}", + pe.FixedFileVersion); + } + } + + [Fact] + public void ThePeReaderReportsTheMachineAndTheLargestIcon() + { + var pe = PeReader.Read(Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Windows), "regedit.exe")); + ushort expected = RuntimeInformation.OSArchitecture == Architecture.Arm64 ? PeReader.MachineArm64 : PeReader.MachineAmd64; + Assert.Equal(expected, pe.Machine); + Assert.NotNull(pe.Icon); + Assert.True(PeReader.IsPng(pe.Icon) || (pe.Icon[2] == 1 && pe.Icon[4] == 1)); + } + + [Fact] + public void ThePeReaderRefusesWhatIsNotAnExecutable() + { + Assert.Throws(() => PeReader.Read(new MemoryStream([0x4D, 0x5A, 1, 2]))); + Assert.Throws(() => PeReader.Read(new MemoryStream(Encoding.ASCII.GetBytes(new string('x', 4096))))); + byte[] truncated = File.ReadAllBytes(Cmd)[..1024]; + Assert.Throws(() => PeReader.Read(new MemoryStream(truncated))); + } + + [Fact] + public void InstallerTechnologiesAreRecognized() + { + var plain = PeReader.Read(Cmd); + Assert.Equal(InstallerKind.Exe, InstallerKinds.Detect(plain)); + Assert.Equal(InstallerKind.Nsis, InstallerKinds.Detect(PeReader.Read(Installer("nsis.exe")))); + Assert.Equal(InstallerKind.Inno, InstallerKinds.Detect(PeReader.Read(Installer("inno.exe", + [.. Encoding.ASCII.GetBytes("rDlPtS\u00cd\u00e6\u00d7{"), .. Encoding.ASCII.GetBytes("Inno Setup Setup Data (6.3.0)")])))); + Assert.Equal(InstallerKind.Inno, InstallerKinds.Detect(plain with + { + Strings = new Dictionary { ["Comments"] = "This installation was built with Inno Setup." }, + })); + Assert.Equal(InstallerKind.Burn, InstallerKinds.Detect(plain with { Sections = [".text", ".wixburn", ".rsrc"] })); + Assert.Equal(InstallerKind.InstallShield, InstallerKinds.Detect(plain with + { + Strings = new Dictionary { ["InternalName"] = "InstallShield Setup.exe" }, + })); + } + + [Fact] + public void AnExeIsIdentifiedByItsVersionResource() + { + var metadata = InstallerFile.Read(Installer("feed/Tool-Setup.exe")); + string product = PeReader.Read(Cmd).String("ProductName")!; + Assert.Equal(InstallerFile.CleanName(product), metadata.Name); + Assert.Equal(InstallerFile.MakeId(metadata.Name), metadata.Id); + Assert.Equal(CmdVersion(), metadata.Version); + Assert.Equal(InstallerKind.Nsis, metadata.Details.Kind); + Assert.Equal("/S", metadata.Details.InstallArguments); + Assert.Equal("Microsoft Corporation", metadata.Details.Publisher); + } + + [Fact] + public void AManifestFillsInWhatTheExeCannotSay() + { + string exe = Installer("Vendor.exe", [1, 2, 3]); + var error = Assert.Throws(() => InstallerFile.Read(exe)); + Assert.Contains("installArguments", error.Message); + Assert.Contains("Vendor.json", error.Message); + + Manifest(exe, """ + { + // the vendor documents these switches + "id": "Vendor.Tool", + "name": "Vendor Tool", + "version": "5.1", + "installArguments": "--silent --no-reboot", + "uninstallArguments": "--uninstall --silent", + "displayName": "Vendor Tool Pro", + "uninstallKey": "VendorTool", + "architectures": ["AMD64"], + "scope": "user", + } + """); + var metadata = InstallerFile.Read(exe); + Assert.Equal(("Vendor.Tool", "Vendor Tool", "5.1"), (metadata.Id, metadata.Name, metadata.Version)); + Assert.Equal(InstallerKind.Exe, metadata.Details.Kind); + Assert.Equal("--silent --no-reboot", metadata.Details.InstallArguments); + Assert.Equal("--uninstall --silent", metadata.Details.UninstallArguments); + Assert.Equal(("Vendor Tool Pro", "VendorTool"), (metadata.Details.DisplayName, metadata.Details.UninstallKey)); + Assert.Equal(["x64"], metadata.Architectures); + Assert.True(metadata.Details.PerUser); + + Manifest(exe, "{ \"version\": "); + Assert.Contains("not valid", Assert.Throws(() => InstallerFile.Read(exe)).Message); + } + + [Fact] + public void AnMsiSaysEverythingItself() + { + string msi = Msi("feed/Example.msi", new() + { + ["ProductCode"] = ProductCode.ToLowerInvariant(), + ["UpgradeCode"] = UpgradeCode, + ["ProductVersion"] = "3.2.1", + ["ProductName"] = "Example App 3.2.1 (x64 edition)", + ["Manufacturer"] = "Example Corp", + ["ALLUSERS"] = "2", + ["MSIINSTALLPERUSER"] = "1", + }, "x64;1033"); + var metadata = InstallerFile.Read(msi); + Assert.Equal((UpgradeCode, "Example App", "3.2.1"), (metadata.Id, metadata.Name, metadata.Version)); + Assert.Equal(InstallerKind.Msi, metadata.Details.Kind); + Assert.Equal((ProductCode, UpgradeCode), (metadata.Details.ProductCode, metadata.Details.UpgradeCode)); + Assert.Equal("Example App 3.2.1 (x64 edition)", metadata.Details.DisplayName); + Assert.Equal(["x64"], metadata.Architectures); + Assert.True(metadata.Details.PerUser); + Assert.Equal("Example Corp", metadata.Details.Publisher); + + string machine = Msi("Machine.msi", new() { ["ProductCode"] = ProductCode, ["ProductVersion"] = "1.0", ["ALLUSERS"] = "1" }, "Intel;1033"); + var machineWide = InstallerFile.Read(machine); + Assert.Equal(ProductCode, machineWide.Id); + Assert.Equal(["x86"], machineWide.Architectures); + Assert.False(machineWide.Details.PerUser); + + Assert.Throws(() => InstallerFile.Read(Msi("NoCode.msi", new() { ["ProductVersion"] = "1.0" }, "x64;0"))); + Assert.Throws(() => InstallerFile.Read(WriteFile("Broken.msi", [1, 2, 3]))); + } + + [Theory] + [InlineData("7-Zip 23.01 (x64 edition)", "7-Zip")] + [InlineData("Notepad++ (64-bit x64)", "Notepad++")] + [InlineData("Zoom Workplace", "Zoom Workplace")] + [InlineData("MyApp version 1.5", "MyApp")] + [InlineData("Python 3.12.1 (64-bit)", "Python")] + [InlineData("Visual Studio Installer", "Visual Studio Installer")] + [InlineData("Node.js", "Node.js")] + public void ProductNamesLoseTheirVersionAndArchitecture(string text, string expected) => + Assert.Equal(expected, InstallerFile.CleanName(text)); + + [Theory] + [InlineData("npp.8.6.2.Installer.x64", "npp")] + [InlineData("Zoom Setup", "Zoom")] + [InlineData("tool-win64-setup", "tool")] + public void FileNamesAlsoLoseTheirSetupWords(string text, string expected) => + Assert.Equal(expected, InstallerFile.CleanName(text, isProductName: false)); + + [Theory] + [InlineData("8.6.2", "8.6.2")] + [InlineData(" v1.2.3 (build 77)", "1.2.3")] + [InlineData("1, 2, 3, 4", "1.2.3.4")] + [InlineData("0.0.0.0", null)] + [InlineData("beta", null)] + [InlineData(null, null)] + public void VersionsAreReadFromTheirLeadingNumbers(string? text, string? expected) => + Assert.Equal(expected, InstallerFile.CleanVersion(text)); + + [Fact] + public void BuildsArePickedForTheMachineTheyRunOn() + { + static int? Compare(string a, string b) => new Version(a).CompareTo(new Version(b)); + var x86 = Item("A", "A", "2.0", new InstallerDetails(InstallerKind.Msi), "x86"); + var x64 = Item("A", "A", "2.0", new InstallerDetails(InstallerKind.Msi), "x64"); + var arm = Item("A", "A", "2.0", new InstallerDetails(InstallerKind.Msi), "arm64"); + var older = Item("A", "A", "1.0", new InstallerDetails(InstallerKind.Msi), "arm64"); + Assert.Same(arm, WinFeed.Latest([x86, x64, arm, older], Compare, Architecture.Arm64).Single()); + Assert.Same(x64, WinFeed.Latest([x86, x64, arm, older], Compare, Architecture.X64).Single()); + Assert.Same(x64, WinFeed.Latest([x86, x64, older], Compare, Architecture.Arm64).Single()); + Assert.Empty(WinFeed.Latest([arm, older], Compare, Architecture.X64)); + } + + [Fact] + public void AnEntryNamedExactlyByThePackageIsIt() + { + var item = Item("A", "Tool", "1.0", new InstallerDetails(InstallerKind.Exe, UninstallKey: "ToolKey")); + var wanted = Entry("toolkey", "Something else"); + Assert.Same(wanted, InstalledMatcher.Find(item, [Entry("Tool", "Tool"), wanted])); + Assert.Null(InstalledMatcher.Find(item, [Entry("Tool", "Tool")])); + } + + [Fact] + public void AnMsiIsFoundByItsUpgradeCodeOrProductCodeButNeverByName() + { + var item = Item(UpgradeCode, "Example", "2.0", new InstallerDetails(InstallerKind.Msi, ProductCode: ProductCode, UpgradeCode: UpgradeCode)); + var older = Entry("{99999999-0000-0000-0000-000000000000}", "Example", "1.0", msi: true); + InstalledMatcher.RelatedProducts = code => code == UpgradeCode ? [older.Key] : []; + Assert.Same(older, InstalledMatcher.Find(item, [Entry("Example", "Example"), older])); + + InstalledMatcher.RelatedProducts = _ => []; + var same = Entry(ProductCode, "Example", "2.0", msi: true); + Assert.Same(same, InstalledMatcher.Find(item, [same])); + Assert.Null(InstalledMatcher.Find(item, [Entry("Example", "Example", "2.0")])); + } + + [Fact] + public void ABundleIsFoundByItsUpgradeCode() + { + var item = Item("B", "Bundle", "2.0", new InstallerDetails(InstallerKind.Burn, UpgradeCode: UpgradeCode)); + var bundle = Entry("{00000000-1111-1111-1111-000000000000}", "Bundle Runtime", "1.0", bundleCodes: [UpgradeCode]); + Assert.Same(bundle, InstalledMatcher.Find(item, [bundle])); + } + + [Fact] + public void AnExeIsFoundByItsNameAndPublisher() + { + var item = Item("Tool", "Tool", "1.7", new InstallerDetails(InstallerKind.Inno, Publisher: "Vendor Inc.")); + var inno = Entry("{1234}_is1", "Tool version 1.5", "1.5", "Vendor"); + var other = Entry("Tool", "Tool 1.5", "1.5", "Vendor"); + Assert.Same(inno, InstalledMatcher.Find(item, [other, inno])); + Assert.Same(other, InstalledMatcher.Find(item, [other, Entry("Tool2", "Tool", "1.0", "Someone Else")])); + Assert.Null(InstalledMatcher.Find(item, [Entry("Hidden", "Tool", "1.0", hidden: true)])); + Assert.Null(InstalledMatcher.Find(item, [Entry("Toolbox", "Toolbox", "1.0", "Vendor")])); + Assert.Null(InstalledMatcher.Find(item, [Entry("Tool", "Tool", "1.0", "Other Vendor")])); + } + + [Fact] + public void ASideBySideProductOfAnotherMajorVersionIsNotIt() + { + var sdk = Item("Microsoft.NET.SDK", "Microsoft .NET SDK", "10.1.326.7603", + new InstallerDetails(InstallerKind.Burn, Publisher: "Microsoft Corporation")); + var eight = Entry("{064a9976-69e7-4aa2-9c9d-474d120ab7f6}", "Microsoft .NET SDK 8.0.133 (arm64)", "8.1.3326.52309", "Microsoft Corporation"); + var ten = Entry("{0aa1d1ed-0000-0000-0000-000000000000}", "Microsoft .NET SDK 10.0.103 (arm64)", "10.1.326.7603", "Microsoft Corporation"); + Assert.Null(InstalledMatcher.Find(sdk, [eight])); + Assert.Same(ten, InstalledMatcher.Find(sdk, [eight, ten])); + Assert.Same(eight, InstalledMatcher.Find(sdk with { Version = "8.1.3400" }, [eight])); + } + + [Fact] + public void TheNewestOfSeveralInstalledVersionsIsTheInstalledOne() + { + var msi = Item(UpgradeCode, "Example", "3.0", new InstallerDetails(InstallerKind.Msi, UpgradeCode: UpgradeCode)); + var older = Entry("{00000000-0000-0000-0000-000000000001}", "Example", "1.0", msi: true); + var newer = Entry("{00000000-0000-0000-0000-000000000002}", "Example", "2.0", msi: true); + InstalledMatcher.RelatedProducts = _ => [older.Key, newer.Key]; + Assert.Same(newer, InstalledMatcher.Find(msi, [older, newer])); + + var exe = Item("Tool", "Tool", "3.0", new InstallerDetails(InstallerKind.Exe, Publisher: "Vendor")); + var first = Entry("A", "Tool", "1.0", "Vendor"); + var second = Entry("B", "Tool", "2.5", "Vendor"); + Assert.Same(second, InstalledMatcher.Find(exe, [first, second])); + } + + [Fact] + public void AnExeWithOnlyAFixedFileVersionIsStillRead() + { + var pe = PeReader.Read(Cmd) with + { + Strings = new Dictionary(), + FixedProductVersion = "0.0.0.0", + FixedFileVersion = "3.4.5.6", + }; + var metadata = InstallerFile.FromExe(pe, new InstallerManifest { InstallArguments = "/q" }, "Tool"); + Assert.Equal("3.4.5.6", metadata.Version); + } + + [Theory] + [InlineData("工具", "工具")] + [InlineData("编辑器", "编辑器")] + [InlineData("Программа", "Программа")] + [InlineData("应用 Pro", "应用.Pro")] + [InlineData("Café Tool", "Café.Tool")] + [InlineData("-Tool", "Tool")] + public void AnIdKeepsTheLettersOfAnyScript(string name, string expected) => + Assert.Equal(expected, InstallerFile.MakeId(name)); + + private const string MediaTable = "CREATE TABLE `Media` (`DiskId` SHORT NOT NULL, `LastSequence` LONG NOT NULL, `DiskPrompt` CHAR(64) LOCALIZABLE, `Cabinet` CHAR(255), `VolumeLabel` CHAR(32), `Source` CHAR(72) PRIMARY KEY `DiskId`)"; + private const string FileTable = "CREATE TABLE `File` (`File` CHAR(72) NOT NULL, `Component_` CHAR(72) NOT NULL, `FileName` CHAR(255) NOT NULL LOCALIZABLE, `FileSize` LONG NOT NULL, `Version` CHAR(72), `Language` CHAR(20), `Attributes` SHORT, `Sequence` SHORT NOT NULL PRIMARY KEY `File`)"; + + [Fact] + public void AnMsiWhoseFilesAreOutsideItIsRefused() + { + static Dictionary Properties() => new() { ["ProductCode"] = ProductCode, ["ProductVersion"] = "1.0" }; + + string external = Msi("External.msi", Properties(), "x64;1033", null, + MediaTable, "INSERT INTO `Media` (`DiskId`, `LastSequence`, `Cabinet`) VALUES (1, 1, 'data1.cab')"); + Assert.Contains("data1.cab", Assert.Throws(() => InstallerFile.Read(external)).Message); + + string loose = Msi("Loose.msi", Properties(), "x64;1033", null, + MediaTable, "INSERT INTO `Media` (`DiskId`, `LastSequence`) VALUES (1, 1)", + FileTable, "INSERT INTO `File` (`File`, `Component_`, `FileName`, `FileSize`, `Sequence`) VALUES ('App', 'Main', 'app.exe', 10, 1)"); + Assert.Contains("uncompressed", Assert.Throws(() => InstallerFile.Read(loose)).Message); + + string embedded = Msi("Embedded.msi", Properties(), "x64;1033", null, + MediaTable, "INSERT INTO `Media` (`DiskId`, `LastSequence`, `Cabinet`) VALUES (1, 1, '#cab1.cab')", + FileTable, "INSERT INTO `File` (`File`, `Component_`, `FileName`, `FileSize`, `Sequence`) VALUES ('App', 'Main', 'app.exe', 10, 1)"); + Assert.Equal("1.0", InstallerFile.Read(embedded).Version); + } + + [Fact] + public void AnMsiWithoutAllUsersInstallsForTheUserWithoutElevation() + { + string msi = Msi("PerUser.msi", new() { ["ProductCode"] = ProductCode, ["ProductVersion"] = "1.0" }, "x64;1033"); + Assert.True(InstallerFile.Read(msi).Details.PerUser); + var item = WinFeed.ScanFolder(_directory, CancellationToken.None).First(i => i.Location.LocalPath.EndsWith("PerUser.msi")); + Assert.False(WinInstallerRunner.InstallProcess(item, "C:\\staged.msi", "C:\\log.txt").Elevated); + } + + [Fact] + public void AFeedPackageHasNoStandaloneCommandLine() + { + var manager = new WinInstaller(); + var source = new UniGetUI.PackageEngine.Classes.Manager.ManagerSource(manager, "feed", new Uri("https://h/")); + var package = new UniGetUI.PackageEngine.PackageClasses.Package("Tool", "Vendor.Tool", "1.0", source, manager); + foreach (var operation in new[] { OperationType.Install, OperationType.Update, OperationType.Uninstall }) + Assert.Throws(() => + manager.OperationHelper.GetStandaloneParameters(package, new UniGetUI.PackageEngine.Serializable.InstallOptions(), operation)); + } + + [Fact] + public void AReceiptKeepsAnInstallFromBeingOfferedAgain() + { + var entry = Entry("Tool", "Tool", "2.0.0.1234"); + Assert.Equal("2.0.0.1234", InstallReceipts.InstalledVersion("Tool", entry)); + InstallReceipts.Record("Tool", entry, "2.0"); + Assert.Equal("2.0", InstallReceipts.InstalledVersion("Tool", entry)); + Assert.Equal("2.1.0.1300", InstallReceipts.InstalledVersion("Tool", entry with { DisplayVersion = "2.1.0.1300" })); + Assert.Equal("2.0.0.1234", InstallReceipts.InstalledVersion("Other", entry)); + InstallReceipts.Record("Tool", entry with { DisplayVersion = "3.0" }, "3.0"); + Assert.Equal("2.0.0.1234", InstallReceipts.InstalledVersion("Tool", entry)); + } + + [Fact] + public void UninstallsRunWhatThePackageRegisteredSilently() + { + string folder = Path.Combine(_directory, "Program Files", "Tool"); + string uninstaller = WriteFile(Path.Combine("Program Files", "Tool", "unins000.exe"), [0]); + + var msi = WinInstallerRunner.UninstallProcess(null, Entry(ProductCode.ToLowerInvariant(), msi: true, uninstall: "MsiExec.exe /X" + ProductCode)); + Assert.Equal((WinInstallerRunner.MsiExecPath, $"/x {ProductCode} /qn /norestart", true), (msi!.FileName, msi.Arguments, msi.Elevated)); + + var inno = WinInstallerRunner.UninstallProcess(null, Entry("{1}_is1", uninstall: $"\"{uninstaller}\"")); + Assert.Equal((uninstaller, "/VERYSILENT /SUPPRESSMSGBOXES /NORESTART"), (inno!.FileName, inno.Arguments)); + + var unquoted = WinInstallerRunner.UninstallProcess(new InstallerDetails(InstallerKind.Nsis), + Entry("Tool", uninstall: $"{uninstaller} /remove", perMachine: false)); + Assert.Equal((uninstaller, "/remove /S", false), (unquoted!.FileName, unquoted.Arguments, unquoted.Elevated)); + + var quiet = WinInstallerRunner.UninstallProcess(null, Entry("Tool", uninstall: $"\"{uninstaller}\"", quiet: $"\"{uninstaller}\" /quiet")); + Assert.Equal((uninstaller, "/quiet", true), (quiet!.FileName, quiet.Arguments, quiet.Elevated)); + + var given = WinInstallerRunner.UninstallProcess(new InstallerDetails(InstallerKind.Exe, UninstallArguments: "--silent"), + Entry("Tool", uninstall: $"\"{uninstaller}\"", quiet: $"\"{uninstaller}\" /quiet")); + Assert.Equal("--silent", given!.Arguments); + + Assert.Null(WinInstallerRunner.UninstallProcess(new InstallerDetails(InstallerKind.Exe), Entry("Tool", uninstall: $"\"{uninstaller}\""))); + Assert.Null(WinInstallerRunner.UninstallProcess(null, Entry("{1}_is1", uninstall: $"\"{Path.Combine(folder, "missing.exe")}\""))); + Assert.Null(WinInstallerRunner.UninstallProcess(null, Entry("{1}_is1"))); + } + + [Fact] + public async Task AnUninstallSucceedsOnceItsEntryIsGone() + { + string uninstaller = WriteFile("uninst.exe", [0]); + var entry = Entry("Tool", "Tool", "1.0", uninstall: $"\"{uninstaller}\""); + var log = new List(); + var entries = new List { entry }; + ArpEntries.Source = entries.ToArray; + WinInstallerRunner.RunProcess = process => + { + _started.Add(process); + entries.Clear(); + return Task.FromResult(0); + }; + Assert.Equal(OperationVeredict.Success, await WinInstallerRunner.UninstallAsync("Tool", new InstallerDetails(InstallerKind.Nsis), entry, log.Add, _ => { })); + Assert.Equal("/S", _started.Single().Arguments); + + entries.Add(entry); + WinInstallerRunner.RunProcess = _ => Task.FromResult(0); + Assert.Equal(OperationVeredict.Failure, await WinInstallerRunner.UninstallAsync("Tool", new InstallerDetails(InstallerKind.Nsis), entry, log.Add, _ => { })); + Assert.Contains(log, l => l.Contains("still listed")); + } + + [Fact] + public async Task AnInstallRunsTheCheckedCopySilently() + { + string exe = Installer("feed/Tool Setup.exe"); + var metadata = InstallerFile.Read(exe); + var item = WinFeed.ScanFolder(Path.Combine(_directory, "feed"), CancellationToken.None).Single(); + var log = new List(); + + Assert.Equal(OperationVeredict.Success, await WinInstallerRunner.InstallAsync(item, false, log.Add, _ => { }, CancellationToken.None)); + var process = _started.Single(); + Assert.Equal("Tool Setup.exe", Path.GetFileName(process.FileName)); + Assert.NotEqual(exe, process.FileName); + Assert.Equal(("/S", false), (process.Arguments, process.Elevated)); + Assert.Contains(log, l => l.Contains("signed by Example Corp")); + Assert.False(Directory.Exists(Path.GetDirectoryName(process.FileName))); + Assert.Equal(metadata.Id, item.Id); + + _exitCode = 3010; + Assert.Equal(OperationVeredict.Success, await WinInstallerRunner.InstallAsync(item, false, log.Add, _ => { }, CancellationToken.None)); + _exitCode = 1; + Assert.Equal(OperationVeredict.Failure, await WinInstallerRunner.InstallAsync(item, false, log.Add, _ => { }, CancellationToken.None)); + } + + [Fact] + public async Task AnUnsignedInstallerNeedsItsCatalogsPermission() + { + Installer("feed/Tool.exe"); + var item = WinFeed.ScanFolder(Path.Combine(_directory, "feed"), CancellationToken.None).Single(); + Authenticode.Verify = _ => new SignatureCheck(unchecked((int)0x800B0100), null); + var error = await Assert.ThrowsAsync(() => + WinInstallerRunner.InstallAsync(item, false, _ => { }, _ => { }, CancellationToken.None)); + Assert.Contains("not signed", error.Message); + Assert.Empty(_started); + + var log = new List(); + Assert.Equal(OperationVeredict.Success, await WinInstallerRunner.InstallAsync(item, true, log.Add, _ => { }, CancellationToken.None)); + Assert.Contains(log, l => l.Contains("allows unsigned")); + } + + [Fact] + public async Task AFileReplacedSinceItWasListedIsRefused() + { + string exe = Installer("feed/Tool.exe"); + var item = WinFeed.ScanFolder(Path.Combine(_directory, "feed"), CancellationToken.None).Single(); + Manifest(exe, "{ \"version\": \"9.9\" }"); + var error = await Assert.ThrowsAsync(() => + WinInstallerRunner.InstallAsync(item, false, _ => { }, _ => { }, CancellationToken.None)); + Assert.Contains("Reload the feed", error.Message); + Assert.Empty(_started); + } + + [Fact] + public async Task AnMsiInstallsThroughMsiexecWithALog() + { + Msi("feed/Example.msi", new() { ["ProductCode"] = ProductCode, ["ProductVersion"] = "1.0", ["ProductName"] = "Example" }, "x64;1033"); + var item = WinFeed.ScanFolder(Path.Combine(_directory, "feed"), CancellationToken.None).Single(); + WinInstallerRunner.IsAdministrator = () => true; + Assert.Equal(OperationVeredict.Success, await WinInstallerRunner.InstallAsync(item, false, _ => { }, _ => { }, CancellationToken.None)); + var process = _started.Single(); + Assert.Equal(WinInstallerRunner.MsiExecPath, process.FileName); + Assert.Matches("^/i \".*Example\\.msi\" /qn /norestart /l\\*v \".*install\\.log\"$", process.Arguments); + Assert.False(process.Elevated); + } + + [Fact] + public void ElevationIsDeclinedAsACancel() + { + Assert.Equal(OperationVeredict.Canceled, WinInstallerRunner.Verdict(1602, _ => { })); + Assert.Equal(OperationVeredict.Success, WinInstallerRunner.Verdict(1641, _ => { })); + } + + [Fact] + public void AnOddVendorFileNameIsNotKeptForTheStagedCopy() + { + var details = new InstallerDetails(InstallerKind.Exe); + Assert.Equal("Tool Setup (x64).exe", WinInstallerRunner.StagedName(Item("a", "a", "1", details) with { Location = new Uri(@"C:\f\Tool Setup (x64).exe") })); + Assert.Equal("setup.exe", WinInstallerRunner.StagedName(Item("a", "a", "1", details) with { Location = new Uri(@"C:\f\a&calc.exe") })); + Assert.Equal("setup.msi", WinInstallerRunner.StagedName(Item("a", "a", "1", new InstallerDetails(InstallerKind.Msi)) with { Location = new Uri(@"C:\f\x.exe") })); + } + + [Fact] + public void TheIndexCommandIndexesWindowsFilesAndKeepsMacOnes() + { + string exe = Installer("Tool.exe"); + WriteFile("Tool.png", [1, 2, 3]); + Msi("sub/Example.msi", new() { ["ProductCode"] = ProductCode, ["UpgradeCode"] = UpgradeCode, ["ProductVersion"] = "2.0" }, "x64;1033"); + Installer("Tool-uninstaller.exe"); + WriteFile("junk.exe", [1, 2, 3]); + string mac = WriteFile("App.pkg", [9, 9, 9]); + string changed = WriteFile("Changed.dmg", [7]); + WriteFile("index.json", Encoding.UTF8.GetBytes($$""" + { + "version": 1, + "packages": [ + { "id": "com.example.app", "name": "App", "version": "1.0", "file": "App.pkg", "sha256": "{{FeedLocation.ComputeSha256(mac)}}", "size": 3 }, + { "id": "com.example.changed", "name": "Changed", "version": "1.0", "file": "Changed.dmg", "sha256": "{{new string('0', 64)}}", "size": 1 }, + { "id": "com.example.gone", "name": "Gone", "version": "1.0", "file": "Gone.zip", "sha256": "{{new string('0', 64)}}", "size": 1 } + ] + } + """)); + + var output = new StringWriter(); + var error = new StringWriter(); + Assert.Equal(3, FeedIndexCommand.Run([_directory], output, error, new WinInstallerIndexer())); + Assert.Contains("junk.exe", error.ToString()); + Assert.Contains("Changed.dmg", error.ToString()); + Assert.Contains("App.pkg: App (com.example.app 1.0) (kept)", output.ToString()); + + byte[] json = File.ReadAllBytes(Path.Combine(_directory, "index.json")); + var written = FeedJson.Deserialize(json, FeedIndexJsonContext.Default.FeedIndex)!; + Assert.Equal(["App.pkg"], written.Packages.Select(e => e.File)); + Assert.Equal(["Tool.exe", "sub/Example.msi"], written.WindowsPackages!.Select(e => e.File)); + var indexUrl = new Uri("https://h/feed/index.json"); + var items = WinFeed.ParseIndex(json, indexUrl); + Assert.Equal(2, items.Count); + var tool = items.Single(i => i.Details.Kind == InstallerKind.Nsis); + Assert.Equal(FeedLocation.ComputeSha256(exe), tool.Sha256); + Assert.Equal("https://h/feed/Tool.png", tool.Icon?.AbsoluteUri); + Assert.Equal("/S", tool.Details.InstallArguments); + var msi = items.Single(i => i.Details.Kind == InstallerKind.Msi); + Assert.Equal((UpgradeCode, "https://h/feed/sub/Example.msi", ProductCode), (msi.Id, msi.Location.AbsoluteUri, msi.Details.ProductCode)); + + var macItems = PkgFeed.ParseIndex(json, indexUrl); + Assert.Equal("com.example.app", macItems.Single().Id); + } + + [Fact] + public void AWebIndexEntryMustSayHowItInstallsAndBeListedForWindows() + { + string sha = new('a', 64); + string json = $$""" + { + "version": 1, + "packages": [ + { "id": "old-style", "name": "Old", "version": "1", "file": "old.exe", "sha256": "{{sha}}", "windows": { "installer": "inno" } } + ], + "windowsPackages": [ + { "id": "ok", "name": "Ok", "version": "1", "file": "ok.exe", "sha256": "{{sha}}", "windows": { "installer": "inno" } }, + { "id": "noblock", "name": "No", "version": "1", "file": "no.exe", "sha256": "{{sha}}" }, + { "id": "unknown", "name": "Unknown", "version": "1", "file": "u.exe", "sha256": "{{sha}}", "windows": { "installer": "exe" } }, + { "id": "given", "name": "Given", "version": "1", "file": "g.exe", "sha256": "{{sha}}", "windows": { "installer": "exe", "installArguments": "-q" } }, + { "id": "mismatch", "name": "M", "version": "1", "file": "m.exe", "sha256": "{{sha}}", "windows": { "installer": "msi" } }, + { "id": "escape", "name": "E", "version": "1", "file": "../e.exe", "sha256": "{{sha}}", "windows": { "installer": "nsis" } } + ] + } + """; + var items = WinFeed.ParseIndex(Encoding.UTF8.GetBytes(json), new Uri("https://h/feed/index.json")); + Assert.Equal(["ok", "given"], items.Select(i => i.Id)); + Assert.Equal("/VERYSILENT /SUPPRESSMSGBOXES /NORESTART /SP-", items[0].Details.InstallArguments); + } + + [Fact] + public void TheManagerListsFeedPackagesAndRecognizesInstalledOnes() + { + string feed = Path.Combine(_directory, "feed"); + Installer("feed/Tool.exe"); + Msi("feed/Example.msi", new() + { + ["ProductCode"] = ProductCode, ["UpgradeCode"] = UpgradeCode, ["ProductVersion"] = "2.0", ["ProductName"] = "Example", + }, "Intel;1033"); + CatalogFeeds.TestCatalogPathOverride = WriteFile("SoftwareCatalog.json", Encoding.UTF8.GetBytes($$""" + { "version": 1, "catalogs": [ { "id": "it-apps", "name": "IT apps", "packages": [], "pkgFeed": "{{feed.Replace(@"\", @"\\")}}" } ] } + """)); + var manager = new WinInstaller(); + var listed = manager.ListLatest(); + Assert.Equal(2, listed.Count); + Assert.All(listed, p => Assert.Equal("it-apps", p.Source.Name)); + + string toolName = listed.Single(p => p.Item.Details.Kind == InstallerKind.Nsis).Item.Name; + InstalledMatcher.RelatedProducts = code => code == UpgradeCode ? ["{12345678-0000-0000-0000-000000000000}"] : []; + ArpEntries.Source = () => + [ + Entry("{12345678-0000-0000-0000-000000000000}", "Example", "1.0", msi: true), + Entry("SomeTool", toolName, CmdVersion(), "Microsoft"), + ]; + var installed = manager.ListInstalled(); + Assert.Equal(2, installed.Count); + var example = installed.Single(p => p.Item.Id == UpgradeCode); + Assert.Equal(("1.0", true), (example.Installed, example.HasUpdate)); + var tool = installed.Single(p => p.Item.Id != UpgradeCode); + Assert.False(tool.HasUpdate); + } + + private static byte[] Ico(params (byte Width, ushort Bits, byte[] Image)[] images) + { + var ico = new List { 0, 0, 1, 0, (byte)images.Length, 0 }; + int offset = 6 + 16 * images.Length; + foreach (var (width, bits, image) in images) + { + ico.AddRange([width, width, 0, 0, 1, 0, (byte)bits, (byte)(bits >> 8)]); + ico.AddRange(BitConverter.GetBytes(image.Length)); + ico.AddRange(BitConverter.GetBytes(offset)); + offset += image.Length; + } + foreach (var (_, _, image) in images) + ico.AddRange(image); + return [.. ico]; + } + + private static readonly byte[] Png = [0x89, (byte)'P', (byte)'N', (byte)'G', 0x0D, 0x0A, 0x1A, 0x0A, 1, 2, 3, 4]; + + [Fact] + public void AnMsiShowsTheLargestImageOfTheIconItGivesAppsAndFeatures() + { + byte[] small = [.. Enumerable.Repeat((byte)7, 40)]; + string msi = Msi("Icon.msi", new() { ["ProductCode"] = ProductCode, ["ProductVersion"] = "1.0", ["ARPPRODUCTICON"] = "Product.ico" }, + "x64;1033", [("Other.ico", Ico((16, 32, small))), ("Product.ico", Ico((16, 32, small), (0, 32, Png), (48, 32, small)))]); + Assert.Equal(Png, InstallerFile.Read(msi).Icon); + + string withoutProductIcon = Msi("Any.msi", new() { ["ProductCode"] = ProductCode, ["ProductVersion"] = "1.0" }, + "x64;1033", [("Only.ico", Ico((32, 32, small)))]); + byte[] icon = InstallerFile.Read(withoutProductIcon).Icon!; + Assert.Equal(Ico((32, 32, small)), icon); + + Assert.Null(InstallerFile.Read(Msi("None.msi", new() { ["ProductCode"] = ProductCode, ["ProductVersion"] = "1.0" }, "x64;1033")).Icon); + } + + [Fact] + public void AnMsiIconStoredAsAnExecutableIsReadFromItsResources() + { + string regedit = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Windows), "regedit.exe"); + string msi = Msi("Exe.msi", new() { ["ProductCode"] = ProductCode, ["ProductVersion"] = "1.0", ["ARPPRODUCTICON"] = "App.exe" }, + "x64;1033", [("App.exe", File.ReadAllBytes(regedit))]); + Assert.Equal(PeReader.Read(regedit).Icon, InstallerFile.Read(msi).Icon); + } + + [Fact] + public void ListingsReadingAFileAtOnceAllSeeItsIcon() + { + string regedit = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Windows), "regedit.exe"); + for (int i = 0; i < 6; i++) + Msi($"busy/App{i}.msi", new() { ["ProductCode"] = Guid.NewGuid().ToString("B"), ["ProductVersion"] = "1.0", ["ARPPRODUCTICON"] = "App.exe" }, + "x64;1033", [("App.exe", File.ReadAllBytes(regedit))]); + string folder = Path.Combine(_directory, "busy"); + var listings = new IReadOnlyList[12]; + Parallel.For(0, listings.Length, new ParallelOptions { MaxDegreeOfParallelism = 12 }, + i => listings[i] = WinFeed.ScanFolder(folder, CancellationToken.None)); + Assert.All(listings, listing => Assert.All(listing, item => Assert.NotNull(item.Icon))); + Assert.All(listings, listing => Assert.Equal(6, listing.Count)); + } + + [Fact] + public void AnIconWhoseImagesLieOutsideItIsIgnored() + { + byte[] ico = Ico((0, 32, Png)); + BitConverter.GetBytes(10_000).CopyTo(ico, 18); + Assert.Null(PeReader.BestIconInIco(ico)); + Assert.Null(PeReader.BestIconInIco([0, 0, 1])); + Assert.Null(PeReader.IconFromFile([(byte)'M', (byte)'Z', 0, 0])); + Assert.Equal(Png, PeReader.IconFromFile(Ico((16, 8, [9, 9]), (0, 32, Png)))); + } + + private string Msi(string relative, Dictionary properties, string template, + (string Name, byte[] Data)[]? icons = null, params string[] statements) + { + string path = Path.Combine(_directory, relative); + Directory.CreateDirectory(Path.GetDirectoryName(path)!); + Check(MsiOpenDatabase(path, 3, out nint database)); + try + { + Execute(database, "CREATE TABLE `Property` (`Property` CHAR(72) NOT NULL, `Value` LONGCHAR NOT NULL PRIMARY KEY `Property`)"); + foreach (var (name, value) in properties) + Execute(database, $"INSERT INTO `Property` (`Property`, `Value`) VALUES ('{name}', '{value}')"); + foreach (string statement in statements) + Execute(database, statement); + if (icons is not null) + { + Execute(database, "CREATE TABLE `Icon` (`Name` CHAR(72) NOT NULL, `Data` OBJECT NOT NULL PRIMARY KEY `Name`)"); + foreach (var (name, data) in icons) + { + string stream = Path.Combine(_directory, Guid.NewGuid().ToString("N")); + File.WriteAllBytes(stream, data); + nint record = MsiCreateRecord(2); + Check(MsiRecordSetString(record, 1, name)); + Check(MsiRecordSetStream(record, 2, stream)); + Check(MsiDatabaseOpenView(database, "INSERT INTO `Icon` (`Name`, `Data`) VALUES (?, ?)", out nint view)); + Check(MsiViewExecute(view, record)); + MsiCloseHandle(view); + MsiCloseHandle(record); + } + } + Check(MsiGetSummaryInformation(database, null, 1, out nint summary)); + Check(MsiSummaryInfoSetProperty(summary, 7, 30, 0, 0, template)); + Check(MsiSummaryInfoPersist(summary)); + MsiCloseHandle(summary); + Check(MsiDatabaseCommit(database)); + } + finally { MsiCloseHandle(database); } + return path; + + static void Execute(nint database, string query) + { + Check(MsiDatabaseOpenView(database, query, out nint view)); + try { Check(MsiViewExecute(view, 0)); } + finally { MsiCloseHandle(view); } + } + + static void Check(uint result) => Assert.Equal(0u, result); + } + + [DllImport("msi.dll", CharSet = CharSet.Unicode, EntryPoint = "MsiOpenDatabaseW")] + private static extern uint MsiOpenDatabase(string path, nint persist, out nint database); + + [DllImport("msi.dll", CharSet = CharSet.Unicode, EntryPoint = "MsiDatabaseOpenViewW")] + private static extern uint MsiDatabaseOpenView(nint database, string query, out nint view); + + [DllImport("msi.dll")] + private static extern uint MsiViewExecute(nint view, nint record); + + [DllImport("msi.dll")] + private static extern uint MsiDatabaseCommit(nint database); + + [DllImport("msi.dll", CharSet = CharSet.Unicode, EntryPoint = "MsiGetSummaryInformationW")] + private static extern uint MsiGetSummaryInformation(nint database, string? path, uint updateCount, out nint summary); + + [DllImport("msi.dll", CharSet = CharSet.Unicode, EntryPoint = "MsiSummaryInfoSetPropertyW")] + private static extern uint MsiSummaryInfoSetProperty(nint summary, uint property, uint dataType, int integer, nint fileTime, string? value); + + [DllImport("msi.dll")] + private static extern uint MsiSummaryInfoPersist(nint summary); + + [DllImport("msi.dll")] + private static extern uint MsiCloseHandle(nint handle); + + [DllImport("msi.dll")] + private static extern nint MsiCreateRecord(uint fields); + + [DllImport("msi.dll", CharSet = CharSet.Unicode, EntryPoint = "MsiRecordSetStringW")] + private static extern uint MsiRecordSetString(nint record, uint field, string value); + + [DllImport("msi.dll", CharSet = CharSet.Unicode, EntryPoint = "MsiRecordSetStreamW")] + private static extern uint MsiRecordSetStream(nint record, uint field, string path); +} +#endif diff --git a/src/UniGetUI.Tests/SoftwareCatalogTests.cs b/src/UniGetUI.Tests/SoftwareCatalogTests.cs index 01be418c36..abd5f7b980 100644 --- a/src/UniGetUI.Tests/SoftwareCatalogTests.cs +++ b/src/UniGetUI.Tests/SoftwareCatalogTests.cs @@ -767,9 +767,9 @@ public void OnlyACatalogWithPackagesCountsAsUsable() File.WriteAllText(file, """{"version":1,"catalogs":[{"id":"a","name":"A","packages":[{"Id":"jq","Name":"jq","Version":"","Source":"Homebrew","ManagerName":"Homebrew"}]}]}"""); Assert.True(SoftwareCatalog.HasUsableCatalog(file)); - // A package feed fills its catalog at run time, but only on macOS, where feeds are read. + // A package feed fills its catalog at run time, but only on macOS and Windows, where feeds are read. File.WriteAllText(file, """{"version":1,"catalogs":[{"id":"a","name":"A","packages":[],"pkgFeed":"https://h/mac/"}]}"""); - Assert.Equal(OperatingSystem.IsMacOS(), SoftwareCatalog.HasUsableCatalog(file)); + Assert.Equal(OperatingSystem.IsMacOS() || OperatingSystem.IsWindows(), SoftwareCatalog.HasUsableCatalog(file)); } [Fact] diff --git a/src/UniGetUI.Windows.slnx b/src/UniGetUI.Windows.slnx index 615846d72c..7752d1895f 100644 --- a/src/UniGetUI.Windows.slnx +++ b/src/UniGetUI.Windows.slnx @@ -224,12 +224,20 @@ + + + + + + + +