From 0b5290319a13c8f73315ea0e0407d88bfd79c944 Mon Sep 17 00:00:00 2001 From: Sean Hall Date: Wed, 17 Feb 2021 15:47:46 -0600 Subject: Add test for explicitly elevating between detect and plan. --- src/TestBA/MessagePump.cs | 39 ++++++++++++++++++++++ src/TestBA/TestBA.cs | 22 ++++++++++++ .../ElevationTests/BundleA/BundleA.wixproj | 18 ++++++++++ src/TestData/ElevationTests/BundleA/BundleA.wxs | 10 ++++++ .../ElevationTests/PackageA/PackageA.wixproj | 9 +++++ src/WixToolsetTest.BurnE2E/ElevationTests.cs | 30 +++++++++++++++++ src/WixToolsetTest.BurnE2E/TestBAController.cs | 5 +++ 7 files changed, 133 insertions(+) create mode 100644 src/TestBA/MessagePump.cs create mode 100644 src/TestData/ElevationTests/BundleA/BundleA.wixproj create mode 100644 src/TestData/ElevationTests/BundleA/BundleA.wxs create mode 100644 src/TestData/ElevationTests/PackageA/PackageA.wixproj create mode 100644 src/WixToolsetTest.BurnE2E/ElevationTests.cs diff --git a/src/TestBA/MessagePump.cs b/src/TestBA/MessagePump.cs new file mode 100644 index 00000000..21a00349 --- /dev/null +++ b/src/TestBA/MessagePump.cs @@ -0,0 +1,39 @@ +// Copyright (c) .NET Foundation and contributors. All rights reserved. Licensed under the Microsoft Reciprocal License. See LICENSE.TXT file in the project root for full license information. + +namespace WixToolset.Test.BA +{ + using System; + using System.Runtime.InteropServices; + using System.Windows.Forms; + + public class MessagePump + { + const uint PM_REMOVE = 1; + + [DllImport("user32.dll", ExactSpelling = true)] + [return: MarshalAs(UnmanagedType.Bool)] + public static extern bool PeekMessageW(ref Message pMsg, IntPtr hWnd, uint wMsgFilterMin, uint wMsgFilterMax, uint wRemoveMsg); + + [DllImport("user32.dll", ExactSpelling = true)] + [return: MarshalAs(UnmanagedType.Bool)] + public static extern bool TranslateMessage(ref Message pMsg); + + [DllImport("user32.dll", ExactSpelling = true)] + public static extern IntPtr DispatchMessageW(ref Message pMsg); + + public static void ProcessMessages(int maxMessages) + { + for (int i = 0; i < maxMessages; i++) + { + Message message = new Message(); + if (!PeekMessageW(ref message, IntPtr.Zero, 0, 0, PM_REMOVE)) + { + break; + } + + TranslateMessage(ref message); + DispatchMessageW(ref message); + } + } + } +} diff --git a/src/TestBA/TestBA.cs b/src/TestBA/TestBA.cs index 1348ce98..b9f869a6 100644 --- a/src/TestBA/TestBA.cs +++ b/src/TestBA/TestBA.cs @@ -28,6 +28,7 @@ namespace WixToolset.Test.BA private bool immediatelyQuit; private bool quitAfterDetect; + private bool explicitlyElevateAndPlanFromOnElevateBegin; private int redetectRemaining; private int sleepDuringCache; private int cancelCacheAtProgress; @@ -122,6 +123,12 @@ namespace WixToolset.Test.BA redetectCount = 0; } + string explicitlyElevateAndPlanFromOnElevateBegin = this.ReadPackageAction(null, "ExplicitlyElevateAndPlanFromOnElevateBegin"); + if (String.IsNullOrEmpty(explicitlyElevateAndPlanFromOnElevateBegin) || !Boolean.TryParse(explicitlyElevateAndPlanFromOnElevateBegin, out this.explicitlyElevateAndPlanFromOnElevateBegin)) + { + this.explicitlyElevateAndPlanFromOnElevateBegin = false; + } + string quitAfterDetect = this.ReadPackageAction(null, "QuitAfterDetect"); if (String.IsNullOrEmpty(quitAfterDetect) || !Boolean.TryParse(quitAfterDetect, out this.quitAfterDetect)) { @@ -214,6 +221,10 @@ namespace WixToolset.Test.BA { this.ShutdownUiThread(); } + else if (this.explicitlyElevateAndPlanFromOnElevateBegin) + { + this.Engine.Elevate(this.windowHandle); + } else { this.Engine.Plan(this.action); @@ -225,6 +236,17 @@ namespace WixToolset.Test.BA } } + protected override void OnElevateBegin(ElevateBeginEventArgs args) + { + if (this.explicitlyElevateAndPlanFromOnElevateBegin) + { + this.Engine.Plan(this.action); + + // Simulate showing some UI since these tests won't actually show the UAC prompt. + MessagePump.ProcessMessages(10); + } + } + protected override void OnPlanPackageBegin(PlanPackageBeginEventArgs args) { RequestState state; diff --git a/src/TestData/ElevationTests/BundleA/BundleA.wixproj b/src/TestData/ElevationTests/BundleA/BundleA.wixproj new file mode 100644 index 00000000..414535b1 --- /dev/null +++ b/src/TestData/ElevationTests/BundleA/BundleA.wixproj @@ -0,0 +1,18 @@ + + + + Bundle + {5DDF6D9F-FF04-40E8-919C-8DD1DCE4B592} + + + + + + + + + + + + + \ No newline at end of file diff --git a/src/TestData/ElevationTests/BundleA/BundleA.wxs b/src/TestData/ElevationTests/BundleA/BundleA.wxs new file mode 100644 index 00000000..bd164a29 --- /dev/null +++ b/src/TestData/ElevationTests/BundleA/BundleA.wxs @@ -0,0 +1,10 @@ + + + + + + + + + + diff --git a/src/TestData/ElevationTests/PackageA/PackageA.wixproj b/src/TestData/ElevationTests/PackageA/PackageA.wixproj new file mode 100644 index 00000000..87f99513 --- /dev/null +++ b/src/TestData/ElevationTests/PackageA/PackageA.wixproj @@ -0,0 +1,9 @@ + + + + {D803BB11-5B94-42EA-8289-7A17E55699A3} + + + + + \ No newline at end of file diff --git a/src/WixToolsetTest.BurnE2E/ElevationTests.cs b/src/WixToolsetTest.BurnE2E/ElevationTests.cs new file mode 100644 index 00000000..54a89469 --- /dev/null +++ b/src/WixToolsetTest.BurnE2E/ElevationTests.cs @@ -0,0 +1,30 @@ +// Copyright (c) .NET Foundation and contributors. All rights reserved. Licensed under the Microsoft Reciprocal License. See LICENSE.TXT file in the project root for full license information. + +namespace WixToolsetTest.BurnE2E +{ + using Xunit; + using Xunit.Abstractions; + + public class ElevationTests : BurnE2ETests + { + public ElevationTests(ITestOutputHelper testOutputHelper) : base(testOutputHelper) { } + + /// + /// This test calls Elevate after Detect, and then calls Plan in OnElevateBegin. + /// After calling Plan, it pumps some messages to simulate UI like the UAC callback. + /// + [Fact(Skip = "https://github.com/wixtoolset/issues/issues/6349")] // CAUTION: this test currently hangs because the Plan request gets dropped. + public void CanExplicitlyElevateAndPlanFromOnElevateBegin() + { + var packageA = this.CreatePackageInstaller("PackageA"); + var bundleA = this.CreateBundleInstaller("BundleA"); + var testBAController = this.CreateTestBAController(); + + testBAController.SetExplicitlyElevateAndPlanFromOnElevateBegin(); + + bundleA.Install(); + bundleA.VerifyRegisteredAndInPackageCache(); + packageA.VerifyInstalled(true); + } + } +} diff --git a/src/WixToolsetTest.BurnE2E/TestBAController.cs b/src/WixToolsetTest.BurnE2E/TestBAController.cs index 1b254656..8e3053b3 100644 --- a/src/WixToolsetTest.BurnE2E/TestBAController.cs +++ b/src/WixToolsetTest.BurnE2E/TestBAController.cs @@ -41,6 +41,11 @@ namespace WixToolsetTest.BurnE2E } } + public void SetExplicitlyElevateAndPlanFromOnElevateBegin(string value = "true") + { + this.SetBurnTestValue("ExplicitlyElevateAndPlanFromOnElevateBegin", value); + } + public void SetImmediatelyQuit(string value = "true") { this.SetBurnTestValue("ImmediatelyQuit", value); -- cgit v1.2.3-55-g6feb