From c56186278c966fd6229f5269d1178dcce7994ebc Mon Sep 17 00:00:00 2001 From: Robert Dima Date: Mon, 14 Dec 2020 17:41:19 +0200 Subject: [PATCH 1/3] Fix AssemblyInstaller failing to RemoveBindingRedirect when action="UnRegister" and dll is missing. --- .../Installer/Installers/AssemblyInstaller.cs | 9 ++- .../DotNetNuke.Tests.Core.csproj | 1 + .../Installer/AssemblyInstallerTests.cs | 71 +++++++++++++++++++ 3 files changed, 80 insertions(+), 1 deletion(-) create mode 100644 DNN Platform/Tests/DotNetNuke.Tests.Core/Services/Installer/AssemblyInstallerTests.cs diff --git a/DNN Platform/Library/Services/Installer/Installers/AssemblyInstaller.cs b/DNN Platform/Library/Services/Installer/Installers/AssemblyInstaller.cs index 61d5c80d04b..064cbd422f7 100644 --- a/DNN Platform/Library/Services/Installer/Installers/AssemblyInstaller.cs +++ b/DNN Platform/Library/Services/Installer/Installers/AssemblyInstaller.cs @@ -289,7 +289,14 @@ private void RemoveBindingRedirect(InstallFile file) /// true if the XML Merge was applied successfully, false if the file was not a strong-named assembly or could not be read. private bool ApplyXmlMerge(InstallFile file, string xmlMergeFile) { - var assemblyName = ReadAssemblyName(Path.Combine(this.PhysicalBasePath, file.FullName)); + var assemblyFileFullPath = Path.Combine(this.PhysicalBasePath, file.FullName); + if (!File.Exists(assemblyFileFullPath)) + { + return false; + } + + var assemblyName = ReadAssemblyName(assemblyFileFullPath); + var publicKeyToken = ReadPublicKey(assemblyName); if (string.IsNullOrEmpty(publicKeyToken)) { diff --git a/DNN Platform/Tests/DotNetNuke.Tests.Core/DotNetNuke.Tests.Core.csproj b/DNN Platform/Tests/DotNetNuke.Tests.Core/DotNetNuke.Tests.Core.csproj index 456f8610a26..bd2731b0e89 100644 --- a/DNN Platform/Tests/DotNetNuke.Tests.Core/DotNetNuke.Tests.Core.csproj +++ b/DNN Platform/Tests/DotNetNuke.Tests.Core/DotNetNuke.Tests.Core.csproj @@ -198,6 +198,7 @@ + diff --git a/DNN Platform/Tests/DotNetNuke.Tests.Core/Services/Installer/AssemblyInstallerTests.cs b/DNN Platform/Tests/DotNetNuke.Tests.Core/Services/Installer/AssemblyInstallerTests.cs new file mode 100644 index 00000000000..b143a827054 --- /dev/null +++ b/DNN Platform/Tests/DotNetNuke.Tests.Core/Services/Installer/AssemblyInstallerTests.cs @@ -0,0 +1,71 @@ +namespace DotNetNuke.Tests.Core.Services.Installer +{ + using System; + using System.Collections.Generic; + using System.IO; + using System.Linq; + using System.Text; + using System.Threading.Tasks; + using System.Xml.XPath; + using DotNetNuke.Abstractions; + using DotNetNuke.Abstractions.Application; + using DotNetNuke.Common; + using DotNetNuke.Services.Installer; + using DotNetNuke.Services.Installer.Installers; + using DotNetNuke.Services.Installer.Packages; + using DotNetNuke.Services.Localization; + using DotNetNuke.Tests.Utilities; + using DotNetNuke.Tests.Utilities.Mocks; + using Microsoft.Extensions.DependencyInjection; + using Moq; + using NUnit.Framework; + + public class AssemblyInstallerTests : DnnUnitTest + { + [SetUp] + public void SetUp() + { + var serviceCollection = new ServiceCollection(); + var appInfo = new Mock(); + appInfo.SetupGet(app => app.Status).Returns(UpgradeStatus.None); + serviceCollection.AddTransient(container => appInfo.Object); + serviceCollection.AddTransient(container => Mock.Of()); + Globals.DependencyProvider = serviceCollection.BuildServiceProvider(); + + var dp = MockComponentProvider.CreateDataProvider(); + dp.Setup(p => p.UnRegisterAssembly(It.IsAny(), It.IsAny())).Returns(true); + + LocalizationProvider.SetTestableInstance(new Mock().Object); + } + + [Test] + public void Install_UnRegisterAssembly_ShouldSucceed_WhenFileIsMissing() + { + var nav = GetAssembliesXmlNavigator(); + + var sut = new AssemblyInstaller() + { + DeleteFiles = true, + Package = new PackageInfo(new InstallerInfo(Directory.GetCurrentDirectory(), InstallMode.Install)), + }; + sut.ReadManifest(nav); + + sut.Install(); + } + + private XPathNavigator GetAssembliesXmlNavigator() + { + var doc = new XPathDocument(new StringReader(@" + + + bin\Providers + FakeAssembly.dll + FakeAssembly.dll + 01.02.03 + + +")); + return doc.CreateNavigator(); + } + } +} From 6f5cb093bf14f4940da13d8b4238c5416e3a50b4 Mon Sep 17 00:00:00 2001 From: Brian Dukes Date: Mon, 14 Dec 2020 12:10:44 -0600 Subject: [PATCH 2/3] Add assertion to test --- .../Services/Installer/AssemblyInstallerTests.cs | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/DNN Platform/Tests/DotNetNuke.Tests.Core/Services/Installer/AssemblyInstallerTests.cs b/DNN Platform/Tests/DotNetNuke.Tests.Core/Services/Installer/AssemblyInstallerTests.cs index b143a827054..81fd3156304 100644 --- a/DNN Platform/Tests/DotNetNuke.Tests.Core/Services/Installer/AssemblyInstallerTests.cs +++ b/DNN Platform/Tests/DotNetNuke.Tests.Core/Services/Installer/AssemblyInstallerTests.cs @@ -50,7 +50,9 @@ public void Install_UnRegisterAssembly_ShouldSucceed_WhenFileIsMissing() }; sut.ReadManifest(nav); - sut.Install(); + sut.Install(); + + Assert.IsTrue(sut.Log.Valid); } private XPathNavigator GetAssembliesXmlNavigator() From e6dad3069bbdd8f857a840fe898ca30aaa7e27e8 Mon Sep 17 00:00:00 2001 From: Brian Dukes Date: Mon, 14 Dec 2020 12:15:10 -0600 Subject: [PATCH 3/3] Clean up warnings Also adjust naming --- .../Installer/Installers/AssemblyInstaller.cs | 1 - .../Installer/AssemblyInstallerTests.cs | 26 +++++++++---------- 2 files changed, 13 insertions(+), 14 deletions(-) diff --git a/DNN Platform/Library/Services/Installer/Installers/AssemblyInstaller.cs b/DNN Platform/Library/Services/Installer/Installers/AssemblyInstaller.cs index 064cbd422f7..353387e54f9 100644 --- a/DNN Platform/Library/Services/Installer/Installers/AssemblyInstaller.cs +++ b/DNN Platform/Library/Services/Installer/Installers/AssemblyInstaller.cs @@ -296,7 +296,6 @@ private bool ApplyXmlMerge(InstallFile file, string xmlMergeFile) } var assemblyName = ReadAssemblyName(assemblyFileFullPath); - var publicKeyToken = ReadPublicKey(assemblyName); if (string.IsNullOrEmpty(publicKeyToken)) { diff --git a/DNN Platform/Tests/DotNetNuke.Tests.Core/Services/Installer/AssemblyInstallerTests.cs b/DNN Platform/Tests/DotNetNuke.Tests.Core/Services/Installer/AssemblyInstallerTests.cs index 81fd3156304..e6c47f496b9 100644 --- a/DNN Platform/Tests/DotNetNuke.Tests.Core/Services/Installer/AssemblyInstallerTests.cs +++ b/DNN Platform/Tests/DotNetNuke.Tests.Core/Services/Installer/AssemblyInstallerTests.cs @@ -1,12 +1,13 @@ -namespace DotNetNuke.Tests.Core.Services.Installer +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information +namespace DotNetNuke.Tests.Core.Services.Installer { using System; - using System.Collections.Generic; using System.IO; using System.Linq; - using System.Text; - using System.Threading.Tasks; using System.Xml.XPath; + using DotNetNuke.Abstractions; using DotNetNuke.Abstractions.Application; using DotNetNuke.Common; @@ -32,8 +33,8 @@ public void SetUp() serviceCollection.AddTransient(container => Mock.Of()); Globals.DependencyProvider = serviceCollection.BuildServiceProvider(); - var dp = MockComponentProvider.CreateDataProvider(); - dp.Setup(p => p.UnRegisterAssembly(It.IsAny(), It.IsAny())).Returns(true); + var dataProvider = MockComponentProvider.CreateDataProvider(); + dataProvider.Setup(p => p.UnRegisterAssembly(It.IsAny(), It.IsAny())).Returns(true); LocalizationProvider.SetTestableInstance(new Mock().Object); } @@ -41,21 +42,20 @@ public void SetUp() [Test] public void Install_UnRegisterAssembly_ShouldSucceed_WhenFileIsMissing() { - var nav = GetAssembliesXmlNavigator(); - - var sut = new AssemblyInstaller() + var installer = new AssemblyInstaller { DeleteFiles = true, Package = new PackageInfo(new InstallerInfo(Directory.GetCurrentDirectory(), InstallMode.Install)), }; - sut.ReadManifest(nav); + var manifestNav = GetUnRegisterAssemblyManifest(); + installer.ReadManifest(manifestNav); - sut.Install(); + installer.Install(); - Assert.IsTrue(sut.Log.Valid); + Assert.IsTrue(installer.Log.Valid); } - private XPathNavigator GetAssembliesXmlNavigator() + private static XPathNavigator GetUnRegisterAssemblyManifest() { var doc = new XPathDocument(new StringReader(@"