Files
Libation/Source/_Tests/LibationFileManager.Tests/ApplicationControlPolicyTests.cs
T
Cursor Agentandrmcrackan a0ca574974 Detect Application Control enforcement and stop upgrading into a broken install
An in-app upgrade overlays files Windows has never seen. Smart App Control
blocks unsigned files it does not recognise, so upgrading in place under
enforcement is precisely how a working install becomes one that cannot start,
which is what #1873, #1876 and #1967 all describe.

Read VerifiedAndReputablePolicyState under HKLM\SYSTEM\CurrentControlSet\Control\CI\Policy
to find out. The read needs no elevation and cannot raise a UAC prompt: UAC
prompts only on an explicit elevation request, and HKLM is readable by standard
users. Only the value 1 counts as enforcing. A missing key, a missing value, or
anything unrecognised counts as not enforcing, because the cost of guessing
wrong is telling someone to disable Smart App Control, which cannot be undone,
on a PC that was never blocking anything.

When enforcing, the upgrade notification becomes a notice with the download
link instead of an update prompt, and the flow stops before downloading even if
a UI ignores that. Classic honoured no such flag at all, so its dialog now takes
one; its two prompt labels had to be promoted from designer locals to fields to
carry the explanation.

The blocked-file dialog now states the setting it found rather than asking the
user to go and look, and startup logs the state, the install folder, and any
cloud sync root containing it, so a report answers these without a round trip.

Co-authored-by: rmcrackan <rmcrackan@gmail.com>
2026-08-18 16:26:56 +00:00

65 lines
2.7 KiB
C#

using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
namespace LibationFileManager.Tests;
[TestClass]
public class ApplicationControlPolicyTests
{
[TestMethod]
[DataRow(0, ApplicationControlState.Off)]
[DataRow(1, ApplicationControlState.Enforcing)]
[DataRow(2, ApplicationControlState.Evaluation)]
public void FromPolicyValue_maps_the_documented_values(int policyValue, ApplicationControlState expected)
=> Assert.AreEqual(expected, ApplicationControlPolicy.FromPolicyValue(policyValue));
// The CI\Policy key exists without this value on Windows builds that never shipped Smart App
// Control. Reading that as enforcement tells people to make an irreversible change to a PC that
// was never blocking anything, which is how ludus shipped a false positive (ludus issue #410).
[TestMethod]
[DataRow(null)]
[DataRow(3)]
[DataRow(-1)]
public void FromPolicyValue_treats_anything_unrecognised_as_unknown(int? policyValue)
{
Assert.AreEqual(ApplicationControlState.Unknown, ApplicationControlPolicy.FromPolicyValue(policyValue));
Assert.AreNotEqual(ApplicationControlState.Enforcing, ApplicationControlPolicy.FromPolicyValue(policyValue));
}
[TestMethod]
public void GetState_is_unknown_and_not_enforcing_off_windows()
{
if (OperatingSystem.IsWindows())
Assert.Inconclusive("Skipped because the OS is Windows, where a real policy state is expected.");
Assert.AreEqual(ApplicationControlState.Unknown, ApplicationControlPolicy.GetState());
Assert.IsFalse(ApplicationControlPolicy.IsEnforcing);
}
[TestMethod]
public void Only_the_enforcing_message_tells_the_user_to_turn_smart_app_control_off()
{
var enforcing = StartupAssemblyBootstrap.DescribeApplicationControlState(ApplicationControlState.Enforcing);
StringAssert.Contains(enforcing, "On for this PC");
StringAssert.Contains(enforcing, "cannot turn it back on");
// Turning Smart App Control off cannot be undone, so a state that is not blocking must never
// be answered by suggesting it.
foreach (var state in new[] { ApplicationControlState.Off, ApplicationControlState.Evaluation })
{
var message = StartupAssemblyBootstrap.DescribeApplicationControlState(state);
StringAssert.Contains(message, "another Application Control policy");
Assert.IsFalse(message.Contains("turn", StringComparison.OrdinalIgnoreCase), $"The {state} message must not suggest changing the setting: {message}");
}
}
[TestMethod]
public void The_unknown_message_explains_how_to_look_the_setting_up()
{
var message = StartupAssemblyBootstrap.DescribeApplicationControlState(ApplicationControlState.Unknown);
StringAssert.Contains(message, "Smart App Control settings");
StringAssert.Contains(message, "Evaluation observes without blocking");
}
}