From 0f6c93913b7b6e8ba6557192dbbc488dbcdb4039 Mon Sep 17 00:00:00 2001 From: seanksullivan Date: Sun, 25 Jun 2017 12:40:31 -0700 Subject: [PATCH 1/4] Created a TestTools project to provide a means to share the Helpers and Extensions between existing nUnit and MSTest2 tests. Created base MSTest2 class and test. --- ILMerge.Tests/BaselineTests.cs | 1 + ILMerge.Tests/ILMerge.Tests.csproj | 1 + ILMerge.Tests/Integration/ConsoleTests.cs | 2 +- ILMerge.Tests/KeyTests.cs | 3 +- ILMerge.sln | 18 +++- .../ILMergeMsTest.UnitTests.csproj | 81 ++++++++++++++++++ ILMergeMsTest.UnitTests/KeyMSTests.cs | 64 ++++++++++++++ .../Properties/AssemblyInfo.cs | 20 +++++ ILMergeMsTest.UnitTests/TestData/test.snk | Bin 0 -> 596 bytes ILMergeMsTest.UnitTests/packages.config | 5 ++ {ILMerge.Tests => TestTools}/Extensions.cs | 7 +- .../Helpers/CspContainerUtils.cs | 2 +- .../Helpers/ProcessUtils.cs | 2 +- .../Helpers/ShadowCopyUtils.cs | 2 +- .../Helpers/StackEnumerator.cs | 2 +- .../Helpers/TempFile.cs | 2 +- TestTools/Properties/AssemblyInfo.cs | 39 +++++++++ TestTools/TestTools.csproj | 58 +++++++++++++ 18 files changed, 296 insertions(+), 13 deletions(-) create mode 100644 ILMergeMsTest.UnitTests/ILMergeMsTest.UnitTests.csproj create mode 100644 ILMergeMsTest.UnitTests/KeyMSTests.cs create mode 100644 ILMergeMsTest.UnitTests/Properties/AssemblyInfo.cs create mode 100644 ILMergeMsTest.UnitTests/TestData/test.snk create mode 100644 ILMergeMsTest.UnitTests/packages.config rename {ILMerge.Tests => TestTools}/Extensions.cs (89%) rename {ILMerge.Tests => TestTools}/Helpers/CspContainerUtils.cs (97%) rename {ILMerge.Tests => TestTools}/Helpers/ProcessUtils.cs (99%) rename {ILMerge.Tests => TestTools}/Helpers/ShadowCopyUtils.cs (97%) rename {ILMerge.Tests => TestTools}/Helpers/StackEnumerator.cs (99%) rename {ILMerge.Tests => TestTools}/Helpers/TempFile.cs (96%) create mode 100644 TestTools/Properties/AssemblyInfo.cs create mode 100644 TestTools/TestTools.csproj diff --git a/ILMerge.Tests/BaselineTests.cs b/ILMerge.Tests/BaselineTests.cs index 156c3b0..b97fae4 100644 --- a/ILMerge.Tests/BaselineTests.cs +++ b/ILMerge.Tests/BaselineTests.cs @@ -1,5 +1,6 @@ using System.Reflection; using NUnit.Framework; +using TestTools; namespace ILMerging.Tests { diff --git a/ILMerge.Tests/ILMerge.Tests.csproj b/ILMerge.Tests/ILMerge.Tests.csproj index 0b292f2..a3f62b2 100644 --- a/ILMerge.Tests/ILMerge.Tests.csproj +++ b/ILMerge.Tests/ILMerge.Tests.csproj @@ -10,6 +10,7 @@ + diff --git a/ILMerge.Tests/Integration/ConsoleTests.cs b/ILMerge.Tests/Integration/ConsoleTests.cs index d453774..2412e27 100644 --- a/ILMerge.Tests/Integration/ConsoleTests.cs +++ b/ILMerge.Tests/Integration/ConsoleTests.cs @@ -3,7 +3,7 @@ using System.IO; using System.Reflection; using System.Runtime.InteropServices; -using ILMerging.Tests.Helpers; +using TestTools.Helpers; using NUnit.Framework; namespace ILMerging.Tests.Integration diff --git a/ILMerge.Tests/KeyTests.cs b/ILMerge.Tests/KeyTests.cs index dc4e965..78ed8a6 100644 --- a/ILMerge.Tests/KeyTests.cs +++ b/ILMerge.Tests/KeyTests.cs @@ -2,7 +2,8 @@ using System.IO; using System.Reflection; using System.Security.Cryptography; -using ILMerging.Tests.Helpers; +using TestTools.Helpers; +using TestTools; using NUnit.Framework; namespace ILMerging.Tests diff --git a/ILMerge.sln b/ILMerge.sln index 6b839f6..2960ac1 100644 --- a/ILMerge.sln +++ b/ILMerge.sln @@ -1,12 +1,12 @@ Microsoft Visual Studio Solution File, Format Version 12.00 # Visual Studio 15 -VisualStudioVersion = 15.0.26403.7 +VisualStudioVersion = 15.0.26430.14 MinimumVisualStudioVersion = 10.0.40219.1 -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ILMerge", "ILMerge\ILMerge.csproj", "{84B41834-EFDE-4E52-A8CE-B51DFCB1D1B2}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ILMerge", "ILMerge\ILMerge.csproj", "{84B41834-EFDE-4E52-A8CE-B51DFCB1D1B2}" EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "System.Compiler", "System.Compiler\System.Compiler.csproj", "{D7E16B38-3893-4EEF-847F-A3BE807E9546}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ILMerge.Tests", "ILMerge.Tests\ILMerge.Tests.csproj", "{734805E8-B2FB-41E3-A669-80429AA50C98}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ILMerge.Tests", "ILMerge.Tests\ILMerge.Tests.csproj", "{734805E8-B2FB-41E3-A669-80429AA50C98}" EndProject Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution Items", "{B2EC5A7D-1059-4EC6-95FB-B2075609DF4B}" ProjectSection(SolutionItems) = preProject @@ -14,6 +14,10 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution README.md = README.md EndProjectSection EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ILMergeMsTest.UnitTests", "ILMergeMsTest.UnitTests\ILMergeMsTest.UnitTests.csproj", "{E75D1760-E327-4709-892E-8CF522CACADB}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "TestTools", "TestTools\TestTools.csproj", "{E0F0B234-A11A-43DC-9461-3B0184FA5E90}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -32,6 +36,14 @@ Global {734805E8-B2FB-41E3-A669-80429AA50C98}.Debug|Any CPU.Build.0 = Debug|Any CPU {734805E8-B2FB-41E3-A669-80429AA50C98}.Release|Any CPU.ActiveCfg = Release|Any CPU {734805E8-B2FB-41E3-A669-80429AA50C98}.Release|Any CPU.Build.0 = Release|Any CPU + {E75D1760-E327-4709-892E-8CF522CACADB}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {E75D1760-E327-4709-892E-8CF522CACADB}.Debug|Any CPU.Build.0 = Debug|Any CPU + {E75D1760-E327-4709-892E-8CF522CACADB}.Release|Any CPU.ActiveCfg = Release|Any CPU + {E75D1760-E327-4709-892E-8CF522CACADB}.Release|Any CPU.Build.0 = Release|Any CPU + {E0F0B234-A11A-43DC-9461-3B0184FA5E90}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {E0F0B234-A11A-43DC-9461-3B0184FA5E90}.Debug|Any CPU.Build.0 = Debug|Any CPU + {E0F0B234-A11A-43DC-9461-3B0184FA5E90}.Release|Any CPU.ActiveCfg = Release|Any CPU + {E0F0B234-A11A-43DC-9461-3B0184FA5E90}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE diff --git a/ILMergeMsTest.UnitTests/ILMergeMsTest.UnitTests.csproj b/ILMergeMsTest.UnitTests/ILMergeMsTest.UnitTests.csproj new file mode 100644 index 0000000..6e4feeb --- /dev/null +++ b/ILMergeMsTest.UnitTests/ILMergeMsTest.UnitTests.csproj @@ -0,0 +1,81 @@ + + + + + Debug + AnyCPU + {E75D1760-E327-4709-892E-8CF522CACADB} + Library + Properties + ILMergeMsTest.UnitTests + ILMergeMsTest.UnitTests + v4.5.2 + 512 + {3AC096D0-A1C2-E12C-1390-A8335801FDAB};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC} + 15.0 + $(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion) + $(ProgramFiles)\Common Files\microsoft shared\VSTT\$(VisualStudioVersion)\UITestExtensionPackages + False + UnitTest + + + + + true + full + false + bin\Debug\ + DEBUG;TRACE + prompt + 4 + + + pdbonly + true + bin\Release\ + TRACE + prompt + 4 + + + + ..\packages\MSTest.TestFramework.1.1.18\lib\net45\Microsoft.VisualStudio.TestPlatform.TestFramework.dll + + + ..\packages\MSTest.TestFramework.1.1.18\lib\net45\Microsoft.VisualStudio.TestPlatform.TestFramework.Extensions.dll + + + + + + + + + + + + + PreserveNewest + + + + + {84b41834-efde-4e52-a8ce-b51dfcb1d1b2} + ILMerge + + + {e0f0b234-a11a-43dc-9461-3b0184fa5e90} + TestTools + + + + + + + This project references NuGet package(s) that are missing on this computer. Use NuGet Package Restore to download them. For more information, see http://go.microsoft.com/fwlink/?LinkID=322105. The missing file is {0}. + + + + + + \ No newline at end of file diff --git a/ILMergeMsTest.UnitTests/KeyMSTests.cs b/ILMergeMsTest.UnitTests/KeyMSTests.cs new file mode 100644 index 0000000..3954d07 --- /dev/null +++ b/ILMergeMsTest.UnitTests/KeyMSTests.cs @@ -0,0 +1,64 @@ +using System; +using Microsoft.VisualStudio.TestTools.UnitTesting; +using System.IO; +using ILMerging; +using System.Reflection; +using TestTools.Helpers; +using TestTools; + +namespace ILMergeMsTest.UnitTests +{ + [TestClass] + public class KeyMSTests + { + public TestContext TestContext { get; set; } + + [TestMethod] + [DeploymentItem("TestData")] + public void Can_sign_using_keyfile() + { + const string Extension = ".dll"; + const string TestSnk = "test.snk"; + + var outputFile = Path.Combine(TestContext.DeploymentDirectory, Path.ChangeExtension(Path.GetRandomFileName(), Extension)); + + var ilMerge = new ILMerge { KeyFile = "test.snk", OutputFile = outputFile }; + ilMerge.SetUpInputAssemblyForTest(Assembly.GetExecutingAssembly()); + ilMerge.Merge(); + + var expectedKeyBytes = new StrongNameKeyPair(File.ReadAllBytes(TestSnk)).PublicKey; + var outputFileKeyBytes = AssemblyName.GetAssemblyName(outputFile).GetPublicKey(); + + CollectionAssert.AreEqual(expectedKeyBytes, outputFileKeyBytes, "Expected PublicKey bytes do not match-up to the generated assembly PublicKey bytes"); + } + + //[TestMethod] + //[DeploymentItem("TestData")] + //public void Can_sign_using_keycontainer() + //{ + // var keyContainerName = Guid.NewGuid().ToString(); + // CspContainerUtils.ImportBlob(true, keyContainerName, KeyNumber.Signature, File.ReadAllBytes(TestFiles.TestSnk)); + // try + // { + // using (var outputFile = TempFile.WithExtension(".dll")) + // { + // var ilMerge = new ILMerge + // { + // KeyContainer = keyContainerName, + // OutputFile = outputFile + // }; + // ilMerge.SetUpInputAssemblyForTest(Assembly.GetExecutingAssembly()); + // ilMerge.Merge(); + + // Assert.That( + // AssemblyName.GetAssemblyName(outputFile).GetPublicKey(), + // Is.EqualTo(new StrongNameKeyPair(File.ReadAllBytes(TestFiles.TestSnk)).PublicKey)); + // } + // } + // finally + // { + // CspContainerUtils.Delete(true, keyContainerName, KeyNumber.Signature); + // } + //} + } +} diff --git a/ILMergeMsTest.UnitTests/Properties/AssemblyInfo.cs b/ILMergeMsTest.UnitTests/Properties/AssemblyInfo.cs new file mode 100644 index 0000000..4b6b9dd --- /dev/null +++ b/ILMergeMsTest.UnitTests/Properties/AssemblyInfo.cs @@ -0,0 +1,20 @@ +using System.Reflection; +using System.Runtime.CompilerServices; +using System.Runtime.InteropServices; + +[assembly: AssemblyTitle("ILMergeMsTest.UnitTests")] +[assembly: AssemblyDescription("")] +[assembly: AssemblyConfiguration("")] +[assembly: AssemblyCompany("")] +[assembly: AssemblyProduct("ILMergeMsTest.UnitTests")] +[assembly: AssemblyCopyright("Copyright © 2017")] +[assembly: AssemblyTrademark("")] +[assembly: AssemblyCulture("")] + +[assembly: ComVisible(false)] + +[assembly: Guid("e75d1760-e327-4709-892e-8cf522cacadb")] + +// [assembly: AssemblyVersion("1.0.*")] +[assembly: AssemblyVersion("1.0.0.0")] +[assembly: AssemblyFileVersion("1.0.0.0")] diff --git a/ILMergeMsTest.UnitTests/TestData/test.snk b/ILMergeMsTest.UnitTests/TestData/test.snk new file mode 100644 index 0000000000000000000000000000000000000000..482d44ea3a03c4a46f1bbf5e3db29c4ba6daecf9 GIT binary patch literal 596 zcmV-a0;~N80ssI2Bme+XQ$aES1ONa50097n%Arqj46ZLQ3cs=AU9f;xhRR&Jzg{XQ z`A1&=7cVDireAw6StXUG;FqOG6VZ1gQks@lf>bviO9CpCYac zeoBlg)tz%YL_k#$*$kEW&gerO?iVYZ3s^7<>x!3Q9xCOlzmTS?<7OQlH;k40-MfoF)y+cvs*$G`jYtdr z84ttErMfR4uTkxl^zj;$)#$2*H%ehX_v7@~;>fvM&TT=Cjt(4Oa~6+X{B49#V~oZ> z%v$Z|=1=LiOQ~bxF4=TiY=C8%o!2 iF;>X45mr{GvLSp7LnwiVHRW609QhDJQjWsrhP9s+U literal 0 HcmV?d00001 diff --git a/ILMergeMsTest.UnitTests/packages.config b/ILMergeMsTest.UnitTests/packages.config new file mode 100644 index 0000000..5304c05 --- /dev/null +++ b/ILMergeMsTest.UnitTests/packages.config @@ -0,0 +1,5 @@ + + + + + \ No newline at end of file diff --git a/ILMerge.Tests/Extensions.cs b/TestTools/Extensions.cs similarity index 89% rename from ILMerge.Tests/Extensions.cs rename to TestTools/Extensions.cs index 7e2cf8b..a538f5d 100644 --- a/ILMerge.Tests/Extensions.cs +++ b/TestTools/Extensions.cs @@ -1,8 +1,9 @@ -using System.Linq; +using ILMerging; +using System.Linq; using System.Reflection; -using ILMerging.Tests.Helpers; +using TestTools.Helpers; -namespace ILMerging.Tests +namespace TestTools { internal static class Extensions { diff --git a/ILMerge.Tests/Helpers/CspContainerUtils.cs b/TestTools/Helpers/CspContainerUtils.cs similarity index 97% rename from ILMerge.Tests/Helpers/CspContainerUtils.cs rename to TestTools/Helpers/CspContainerUtils.cs index 6fef286..9698757 100644 --- a/ILMerge.Tests/Helpers/CspContainerUtils.cs +++ b/TestTools/Helpers/CspContainerUtils.cs @@ -1,6 +1,6 @@ using System.Security.Cryptography; -namespace ILMerging.Tests.Helpers +namespace TestTools.Helpers { public static class CspContainerUtils { diff --git a/ILMerge.Tests/Helpers/ProcessUtils.cs b/TestTools/Helpers/ProcessUtils.cs similarity index 99% rename from ILMerge.Tests/Helpers/ProcessUtils.cs rename to TestTools/Helpers/ProcessUtils.cs index 3b789be..f6d47e0 100644 --- a/ILMerge.Tests/Helpers/ProcessUtils.cs +++ b/TestTools/Helpers/ProcessUtils.cs @@ -3,7 +3,7 @@ using System.Diagnostics; using System.Text; -namespace ILMerging.Tests.Helpers +namespace TestTools.Helpers { public static class ProcessUtils { diff --git a/ILMerge.Tests/Helpers/ShadowCopyUtils.cs b/TestTools/Helpers/ShadowCopyUtils.cs similarity index 97% rename from ILMerge.Tests/Helpers/ShadowCopyUtils.cs rename to TestTools/Helpers/ShadowCopyUtils.cs index 18769ce..28f45ba 100644 --- a/ILMerge.Tests/Helpers/ShadowCopyUtils.cs +++ b/TestTools/Helpers/ShadowCopyUtils.cs @@ -4,7 +4,7 @@ using System.Linq; using System.Reflection; -namespace ILMerging.Tests.Helpers +namespace TestTools.Helpers { public static class ShadowCopyUtils { diff --git a/ILMerge.Tests/Helpers/StackEnumerator.cs b/TestTools/Helpers/StackEnumerator.cs similarity index 99% rename from ILMerge.Tests/Helpers/StackEnumerator.cs rename to TestTools/Helpers/StackEnumerator.cs index 460822d..8e7df5a 100644 --- a/ILMerge.Tests/Helpers/StackEnumerator.cs +++ b/TestTools/Helpers/StackEnumerator.cs @@ -2,7 +2,7 @@ using System.Collections.Generic; using System.ComponentModel; -namespace ILMerging.Tests.Helpers +namespace TestTools.Helpers { public static class StackEnumerator { diff --git a/ILMerge.Tests/Helpers/TempFile.cs b/TestTools/Helpers/TempFile.cs similarity index 96% rename from ILMerge.Tests/Helpers/TempFile.cs rename to TestTools/Helpers/TempFile.cs index 2df1d48..d5e97a7 100644 --- a/ILMerge.Tests/Helpers/TempFile.cs +++ b/TestTools/Helpers/TempFile.cs @@ -3,7 +3,7 @@ using System.IO; using System.Threading; -namespace ILMerging.Tests.Helpers +namespace TestTools.Helpers { [DebuggerDisplay("{ToString(),nq}")] public sealed class TempFile : IDisposable diff --git a/TestTools/Properties/AssemblyInfo.cs b/TestTools/Properties/AssemblyInfo.cs new file mode 100644 index 0000000..e105234 --- /dev/null +++ b/TestTools/Properties/AssemblyInfo.cs @@ -0,0 +1,39 @@ +using System.Reflection; +using System.Runtime.CompilerServices; +using System.Runtime.InteropServices; + +// General Information about an assembly is controlled through the following +// set of attributes. Change these attribute values to modify the information +// associated with an assembly. +[assembly: AssemblyTitle("TestTools")] +[assembly: AssemblyDescription("")] +[assembly: AssemblyConfiguration("")] +[assembly: AssemblyCompany("")] +[assembly: AssemblyProduct("TestTools")] +[assembly: AssemblyCopyright("Copyright © 2017")] +[assembly: AssemblyTrademark("")] +[assembly: AssemblyCulture("")] + +// Setting ComVisible to false makes the types in this assembly not visible +// to COM components. If you need to access a type in this assembly from +// COM, set the ComVisible attribute to true on that type. +[assembly: ComVisible(false)] + +// The following GUID is for the ID of the typelib if this project is exposed to COM +[assembly: Guid("e0f0b234-a11a-43dc-9461-3b0184fa5e90")] + +// Version information for an assembly consists of the following four values: +// +// Major Version +// Minor Version +// Build Number +// Revision +// +// You can specify all the values or you can default the Build and Revision Numbers +// by using the '*' as shown below: +// [assembly: AssemblyVersion("1.0.*")] +[assembly: AssemblyVersion("1.0.0.0")] +[assembly: AssemblyFileVersion("1.0.0.0")] + +[assembly: InternalsVisibleTo("ILMergeMsTest.UnitTests")] +[assembly: InternalsVisibleTo("ILMerge.Tests")] diff --git a/TestTools/TestTools.csproj b/TestTools/TestTools.csproj new file mode 100644 index 0000000..60c6acd --- /dev/null +++ b/TestTools/TestTools.csproj @@ -0,0 +1,58 @@ + + + + + Debug + AnyCPU + {E0F0B234-A11A-43DC-9461-3B0184FA5E90} + Library + Properties + TestTools + TestTools + v4.5.2 + 512 + + + true + full + false + bin\Debug\ + DEBUG;TRACE + prompt + 4 + + + pdbonly + true + bin\Release\ + TRACE + prompt + 4 + + + + + + + + + + + + + + + + + + + + + + + {84b41834-efde-4e52-a8ce-b51dfcb1d1b2} + ILMerge + + + + \ No newline at end of file From 84eff429a49c987521c01cd7506825c8c4d0c5f9 Mon Sep 17 00:00:00 2001 From: seanksullivan Date: Sun, 25 Jun 2017 13:36:41 -0700 Subject: [PATCH 2/4] Completed creating MSTest versions of the nUnit KeyTests class. --- ILMergeMsTest.UnitTests/TestData/test.pfx | Bin 0 -> 1709 bytes 1 file changed, 0 insertions(+), 0 deletions(-) create mode 100644 ILMergeMsTest.UnitTests/TestData/test.pfx diff --git a/ILMergeMsTest.UnitTests/TestData/test.pfx b/ILMergeMsTest.UnitTests/TestData/test.pfx new file mode 100644 index 0000000000000000000000000000000000000000..99aa269d0f9f9554be3589885425ce71d57c6979 GIT binary patch literal 1709 zcmcJQc{G)29LJw`ajn-fTx7jd93U ze~S28?l$fm6%j=WIh0*Ehq43OEP{ja|FMLX;2f+7+6Az96v;okq$Ciukc09G4tkwX zqe%Q^!;4GfV(uJm=bcsRH7F|Dj&snguO4JaaOpN1jYS`C&N$oI>t)iX{H9N9u{R#1 z)@^hFax%HCJF5DO3?`4h_^yW6 zNHu8FQ!)KLhbJxF{aCerHAh!PP!y3MH@82&dQDi<^y{p3iZgh^ZL!7bwB`$li9?3h z^^q-F^*c&`?VqmgKCIq|&v?pN(TX!v1-k|I?&p15?viuEC^>`K0p$|J?LmM6sx$3M<| zJn1Li+3lpRx>)o>4B1{EMr*U5T4;$+H`I5EXnxwuD3uzKnaT-1;uD!5GSw7suy$-* z<9v*zo@BaG2r}8_tHO&b!`i()KUQ3M$D6S;v+zDNpjKir9EsS7iv0BU%NMU*8GKOQ z_As@6a5gB^a^U-dH(oD;b@FLzD7uL+HKQk|Q~X5*c!^A4x^d=#^ni?Jd+ZswCbdU(B7NOYpjp0Kimm7zUh4u&a*bs84Ss!@u3 zeyYTL=b-rOgWCi8a3sh#X41ft_6X+U1 zS`Ul?0TmP2!dg;WfHh!1Vk~!ALAF_@WWtLgu1`U5gkvIykj@$`-~@tzFNgx+AR1Nx zhyirq0*^~h7zl)94Rr#DfL=J{DBuJ2zU3}Da9%#|03<))529cavsR{G3N^}x(MyJQ z0<73ilZ(McCurKO*6M)+o-~H%F^7E=cS<(P64aGV z5D^hTxRI`e3k7q>JE4QyvXQUK%PqUo zKCLw)koo+`LAAb%>xRsS#NT*={3+twHfHB>Hlo?(-=RYccxyG zgpN?V!2TZ2ye5}q*wvrAU{E+dbS*>j^hLvvMBbvlvQ%`1=@%hWH>NK;{X9H~@^AW` zkzi)B7J{eE@7w0JS_nznzvs!E&?H1DBKp!8s)8$|M!zkYJ0cnMA#z53A@0?%hQ5p= du5eD=$1<^?mz Date: Sun, 25 Jun 2017 13:54:05 -0700 Subject: [PATCH 3/4] The purpose of this MSTest unit test project is to demonstrate that MSTest2 unit tests are very similar to nUnit tests. Both frameworks have their strengths and weaknesses - fyi: my comfor zone is MSTest2, but I appreciate some of the functionality that nUnit has to offer . --- .../ILMergeMsTest.UnitTests.csproj | 6 + ILMergeMsTest.UnitTests/KeyMSTests.cs | 123 +++++++++++++----- ILMergeMsTest.UnitTests/Readme.txt | 17 +++ 3 files changed, 113 insertions(+), 33 deletions(-) create mode 100644 ILMergeMsTest.UnitTests/Readme.txt diff --git a/ILMergeMsTest.UnitTests/ILMergeMsTest.UnitTests.csproj b/ILMergeMsTest.UnitTests/ILMergeMsTest.UnitTests.csproj index 6e4feeb..6150d92 100644 --- a/ILMergeMsTest.UnitTests/ILMergeMsTest.UnitTests.csproj +++ b/ILMergeMsTest.UnitTests/ILMergeMsTest.UnitTests.csproj @@ -54,6 +54,9 @@ + + PreserveNewest + PreserveNewest @@ -68,6 +71,9 @@ TestTools + + + diff --git a/ILMergeMsTest.UnitTests/KeyMSTests.cs b/ILMergeMsTest.UnitTests/KeyMSTests.cs index 3954d07..19aa730 100644 --- a/ILMergeMsTest.UnitTests/KeyMSTests.cs +++ b/ILMergeMsTest.UnitTests/KeyMSTests.cs @@ -5,60 +5,117 @@ using System.Reflection; using TestTools.Helpers; using TestTools; +using System.Security.Cryptography; +using System.Collections.Generic; namespace ILMergeMsTest.UnitTests { + /// + /// The purpose of this class is to demonstrate that MSTest/MSTest2 is just as simple to use as nUnit. + /// MSTest and nUnit both have their own strengths and weeknesses, but for the most part - these tests are simple. + /// [TestClass] public class KeyMSTests { + #region Private Members + private const string Extension = ".dll"; + private const string TestSnk = "test.snk"; + private const string TestPfx = "test.pfx"; + private static List _keyContainerNameList = new List(); + #endregion + + #region Public Properties public TestContext TestContext { get; set; } + #endregion + + #region Init and Cleanup + /// + /// Let's cleanup Csp Containers after all tests have executed (successfully or unsuccessfully). + /// + [ClassCleanup] + public static void ClassCleanup() + { + _keyContainerNameList.ForEach(containerName => CspContainerUtils.Delete(true, containerName, KeyNumber.Signature)); + } + #endregion [TestMethod] [DeploymentItem("TestData")] public void Can_sign_using_keyfile() { - const string Extension = ".dll"; - const string TestSnk = "test.snk"; - - var outputFile = Path.Combine(TestContext.DeploymentDirectory, Path.ChangeExtension(Path.GetRandomFileName(), Extension)); + // ARRANGE + var outputFile = Path.Combine(TestContext.DeploymentDirectory, + Path.ChangeExtension(Path.GetRandomFileName(), Extension)); var ilMerge = new ILMerge { KeyFile = "test.snk", OutputFile = outputFile }; + ilMerge.SetUpInputAssemblyForTest(Assembly.GetExecutingAssembly()); + + // ACT ilMerge.Merge(); + // ASSERT var expectedKeyBytes = new StrongNameKeyPair(File.ReadAllBytes(TestSnk)).PublicKey; - var outputFileKeyBytes = AssemblyName.GetAssemblyName(outputFile).GetPublicKey(); + var outputFileKeyBytes = AssemblyName.GetAssemblyName(outputFile).GetPublicKey(); + CollectionAssert.AreEqual(expectedKeyBytes, outputFileKeyBytes, "Expected PublicKey bytes do not match-up to the generated assembly PublicKey bytes"); + } + + [TestMethod] + [DeploymentItem("TestData")] + public void Can_sign_using_keycontainer() + { + // ARRANGE + var keyContainerName = Guid.NewGuid().ToString(); + _keyContainerNameList.Add(keyContainerName); + CspContainerUtils.ImportBlob(true, keyContainerName, KeyNumber.Signature, File.ReadAllBytes(TestSnk)); + + var outputFile = Path.Combine(TestContext.DeploymentDirectory, + Path.ChangeExtension(Path.GetRandomFileName(), Extension)); + + var ilMerge = new ILMerge + { + KeyContainer = keyContainerName, + OutputFile = outputFile + }; + ilMerge.SetUpInputAssemblyForTest(Assembly.GetExecutingAssembly()); + + // ACT + ilMerge.Merge(); + + // ASSERT + var expectedKeyBytes = new StrongNameKeyPair(File.ReadAllBytes(TestSnk)).PublicKey; + var outputFileKeyBytes = AssemblyName.GetAssemblyName(outputFile).GetPublicKey(); CollectionAssert.AreEqual(expectedKeyBytes, outputFileKeyBytes, "Expected PublicKey bytes do not match-up to the generated assembly PublicKey bytes"); } - //[TestMethod] - //[DeploymentItem("TestData")] - //public void Can_sign_using_keycontainer() - //{ - // var keyContainerName = Guid.NewGuid().ToString(); - // CspContainerUtils.ImportBlob(true, keyContainerName, KeyNumber.Signature, File.ReadAllBytes(TestFiles.TestSnk)); - // try - // { - // using (var outputFile = TempFile.WithExtension(".dll")) - // { - // var ilMerge = new ILMerge - // { - // KeyContainer = keyContainerName, - // OutputFile = outputFile - // }; - // ilMerge.SetUpInputAssemblyForTest(Assembly.GetExecutingAssembly()); - // ilMerge.Merge(); - - // Assert.That( - // AssemblyName.GetAssemblyName(outputFile).GetPublicKey(), - // Is.EqualTo(new StrongNameKeyPair(File.ReadAllBytes(TestFiles.TestSnk)).PublicKey)); - // } - // } - // finally - // { - // CspContainerUtils.Delete(true, keyContainerName, KeyNumber.Signature); - // } - //} + [TestMethod] + [DeploymentItem("TestData")] + public void Bad_keyfile_gives_diagnostic_warning() + { + // ARRANGE + var outputFile = Path.Combine(TestContext.DeploymentDirectory, + Path.ChangeExtension(Path.GetRandomFileName(), Extension)); + + var logFile = Path.Combine(TestContext.DeploymentDirectory, + Path.ChangeExtension(Path.GetRandomFileName(), ".log")); + + var ilMerge = new ILMerge + { + KeyFile = TestPfx, + OutputFile = outputFile, + LogFile = logFile + }; + ilMerge.SetUpInputAssemblyForTest(Assembly.GetExecutingAssembly()); + + //ACT + ilMerge.Merge(); + + //ASSERT + var logText = File.ReadAllText(logFile); + Assert.IsTrue(logText.Contains("Unable to obtain public key for StrongNameKeyPair.")); + Assert.IsTrue(logText.Contains("PFX")); + Assert.IsTrue(logText.Contains("key container")); + } } } diff --git a/ILMergeMsTest.UnitTests/Readme.txt b/ILMergeMsTest.UnitTests/Readme.txt new file mode 100644 index 0000000..2dcabeb --- /dev/null +++ b/ILMergeMsTest.UnitTests/Readme.txt @@ -0,0 +1,17 @@ +The purpose of this unit test project is to demonstrate that MSTest2 unit tests are very similar to nUnit tests. +Both frameworks have their strengths and weaknesses - but I prefer MSTest2 :). + +I do appreciate nUnit's TestFixture, and TestClass functionality - + I think MSTest2 can provide the same benefits; in some cases in a simpler manner, other cases... not so simple. + +With MSTest2 we can use the DataRow attribute, which is similar to nUnit's TestCase attribute - to provide data-driven unit test processing. + +nUnit example: + [TestCase(true, TestName = "{m}(with mscorsn in path)")] + [TestCase(false, TestName = "{m}(without mscorsn in path)")] + public void No_DLL_load_crashes_when_given_PFX(bool withMscorsnInPath) + +MSTest2 example: + [DataRow(true, DisplayName = "(with mscorsn in path)")] + [DataRow(false, DisplayName = "(without mscorsn in path)")] + public void No_DLL_load_crashes_when_given_PFX(bool withMscorsnInPath) \ No newline at end of file From d88ba9597ea112b4148c0b6649616eb4ebd0ec6b Mon Sep 17 00:00:00 2001 From: seanksullivan Date: Sun, 25 Jun 2017 20:00:49 -0700 Subject: [PATCH 4/4] Completed replicating all NUnit tests with MSTest2 tests, for demonstration purposes. Excluded all test and test-tool classes from Code Coverage collection. FYI - you need the VS Enterprise SKU to view code coverage results. --- ILMerge.Tests/BaselineTests.cs | 2 + ILMerge.Tests/Integration/ConsoleTests.cs | 2 + ILMerge.Tests/KeyTests.cs | 2 + ILMerge.Tests/TestFiles.cs | 2 + ILMergeMsTest.UnitTests/BaselineMSTests.cs | 37 +++++++++ ILMergeMsTest.UnitTests/ConsoleMSTests.cs | 80 +++++++++++++++++++ .../ILMergeMsTest.UnitTests.csproj | 2 + ILMergeMsTest.UnitTests/KeyMSTests.cs | 8 +- TestTools/Extensions.cs | 2 + TestTools/Helpers/CspContainerUtils.cs | 4 +- TestTools/Helpers/ProcessUtils.cs | 2 + TestTools/Helpers/ShadowCopyUtils.cs | 2 + TestTools/Helpers/StackEnumerator.cs | 4 + TestTools/Helpers/TempFile.cs | 2 + 14 files changed, 148 insertions(+), 3 deletions(-) create mode 100644 ILMergeMsTest.UnitTests/BaselineMSTests.cs create mode 100644 ILMergeMsTest.UnitTests/ConsoleMSTests.cs diff --git a/ILMerge.Tests/BaselineTests.cs b/ILMerge.Tests/BaselineTests.cs index b97fae4..97e2888 100644 --- a/ILMerge.Tests/BaselineTests.cs +++ b/ILMerge.Tests/BaselineTests.cs @@ -1,9 +1,11 @@ using System.Reflection; using NUnit.Framework; using TestTools; +using System.Diagnostics.CodeAnalysis; namespace ILMerging.Tests { + [ExcludeFromCodeCoverage] [TestFixture] public sealed class BaselineTests { diff --git a/ILMerge.Tests/Integration/ConsoleTests.cs b/ILMerge.Tests/Integration/ConsoleTests.cs index 2412e27..5772ab0 100644 --- a/ILMerge.Tests/Integration/ConsoleTests.cs +++ b/ILMerge.Tests/Integration/ConsoleTests.cs @@ -5,9 +5,11 @@ using System.Runtime.InteropServices; using TestTools.Helpers; using NUnit.Framework; +using System.Diagnostics.CodeAnalysis; namespace ILMerging.Tests.Integration { + [ExcludeFromCodeCoverage] [TestFixture, Category("Integration")] public sealed class ConsoleTests { diff --git a/ILMerge.Tests/KeyTests.cs b/ILMerge.Tests/KeyTests.cs index 78ed8a6..592b0cb 100644 --- a/ILMerge.Tests/KeyTests.cs +++ b/ILMerge.Tests/KeyTests.cs @@ -5,9 +5,11 @@ using TestTools.Helpers; using TestTools; using NUnit.Framework; +using System.Diagnostics.CodeAnalysis; namespace ILMerging.Tests { + [ExcludeFromCodeCoverage] [TestFixture] public sealed class KeyTests { diff --git a/ILMerge.Tests/TestFiles.cs b/ILMerge.Tests/TestFiles.cs index 07d00e8..86eb935 100644 --- a/ILMerge.Tests/TestFiles.cs +++ b/ILMerge.Tests/TestFiles.cs @@ -1,8 +1,10 @@ using System.IO; using NUnit.Framework; +using System.Diagnostics.CodeAnalysis; namespace ILMerging.Tests { + [ExcludeFromCodeCoverage] internal static class TestFiles { private static string FromCurrentDir(string fileName) diff --git a/ILMergeMsTest.UnitTests/BaselineMSTests.cs b/ILMergeMsTest.UnitTests/BaselineMSTests.cs new file mode 100644 index 0000000..1683618 --- /dev/null +++ b/ILMergeMsTest.UnitTests/BaselineMSTests.cs @@ -0,0 +1,37 @@ +using ILMerging; +using Microsoft.VisualStudio.TestTools.UnitTesting; +using System.Diagnostics.CodeAnalysis; +using System.Reflection; +using TestTools; + +namespace ILMergeMsTest.UnitTests +{ + [ExcludeFromCodeCoverage] + [TestClass] + public sealed class BaselineMSTests + { + #region Private Members + private const string Extension = ".dll"; + private const string TestSnk = "test.snk"; + private const string TestPfx = "test.pfx"; + #endregion + + #region Public Properties + public TestContext TestContext { get; set; } + #endregion + + /// + /// Replicating the "Single_Input" NUnit test, using MSTest2. + /// Test output and working directory is within the generated "TestResults" folder - it is cleansed for all "Release" test runs, + /// and partially persists for "Debug" test runs (the NUnit tests are using the system's "Temp" directory, which is not preferable). + /// + /// + [TestMethod] + public void Single_input() + { + var ilMerge = new ILMerge(); + ilMerge.SetUpInputAssemblyForTest(Assembly.GetExecutingAssembly()); + ilMerge.MergeToTempFileForTest(".dll"); + } + } +} diff --git a/ILMergeMsTest.UnitTests/ConsoleMSTests.cs b/ILMergeMsTest.UnitTests/ConsoleMSTests.cs new file mode 100644 index 0000000..d7b3ae6 --- /dev/null +++ b/ILMergeMsTest.UnitTests/ConsoleMSTests.cs @@ -0,0 +1,80 @@ +using System; +using System.Diagnostics; +using System.IO; +using System.Reflection; +using System.Runtime.InteropServices; +using TestTools.Helpers; +using Microsoft.VisualStudio.TestTools.UnitTesting; +using ILMerging; +using System.Collections.Generic; +using System.Diagnostics.CodeAnalysis; + +namespace ILMergeMsTest.UnitTests +{ + [ExcludeFromCodeCoverage] + [TestClass] + public sealed class ConsoleMSTests + { + #region Private Members + private const string Extension = ".dll"; + private const string TestSnk = "test.snk"; + private const string TestPfx = "test.pfx"; + #endregion + + #region Public Properties + public TestContext TestContext { get; set; } + #endregion + + /// + /// Demonstrating MSTest2 DataRowAttribute usage, + /// that partially replicates nUnit TestFixture and TestCase functionality. + /// Test output and working directory is within the generated "TestResults" folder - it is cleansed for all "Release" test runs, + /// and partially persists for "Debug" test runs (the NUnit tests are using the system's "Temp" directory, which is not preferable). + /// + /// + [TestMethod] + [TestCategory("Integration")] + [DeploymentItem("TestData")] + [DataRow(true, DisplayName = "(with mscorsn in path)")] + [DataRow(false, DisplayName = "(without mscorsn in path)")] + public void No_DLL_load_crashes_when_given_PFX(bool withMscorsnInPath) + { + // Inconclusive for non-64 Bit OS' + if (withMscorsnInPath && !Environment.Is64BitOperatingSystem) Assert.Inconclusive("This test can only be run on a 64-bit OS."); + + // ARRANGE + var ilMergeExePath = typeof(ILMerge).Assembly.Location; + var inputAssembly = Assembly.GetExecutingAssembly(); + var fileName = ShadowCopyUtils.GenerateILMergeLibCliSwitches(inputAssembly); + var outputFile = Path.Combine(TestContext.DeploymentDirectory, + Path.ChangeExtension(Path.GetRandomFileName(), Extension)); + + var startInfo = new ProcessStartInfo( + ilMergeExePath, $"{fileName} /keyfile:\"{TestPfx}\" /out:\"{outputFile}\" \"{inputAssembly.Location}\"") + { + WorkingDirectory = Path.GetDirectoryName(inputAssembly.Location) + }; + + if (withMscorsnInPath) + { + startInfo.EnvironmentVariables["PATH"] = $"{Environment.GetEnvironmentVariable("PATH")};{RuntimeEnvironment.GetRuntimeDirectory()}"; + } + + // ACT + // The system runs .NET executables as 64-bit no matter what the architecture of the calling process is. + var result = ProcessUtils.Run(startInfo); + + // ASSERT + Assert.AreEqual(0, result.ExitCode); + Assert.IsFalse(result.ToString().Contains("Unable to load DLL 'mscorsn.dll'")); + Assert.IsFalse(result.ToString().Contains("An attempt was made to load a program with an incorrect format.")); + + // Test failures: + Assert.IsFalse(result.ToString().Contains("Unhandled Exception: System.IO.FileNotFoundException"), + "The test is not being run properly. If you are using ReSharper, disable shadow copy. " + + "If you are using NCrunch, go to NCrunch's configuration for the ILMerge project and " + + "make sure \"Copy referenced assemblies to workspace\" is set to True. " + + "(Both ReSharper and NCrunch settings are saved in the repo, so this should not happen.)"); + } + } +} diff --git a/ILMergeMsTest.UnitTests/ILMergeMsTest.UnitTests.csproj b/ILMergeMsTest.UnitTests/ILMergeMsTest.UnitTests.csproj index 6150d92..7a8a641 100644 --- a/ILMergeMsTest.UnitTests/ILMergeMsTest.UnitTests.csproj +++ b/ILMergeMsTest.UnitTests/ILMergeMsTest.UnitTests.csproj @@ -49,6 +49,8 @@ + + diff --git a/ILMergeMsTest.UnitTests/KeyMSTests.cs b/ILMergeMsTest.UnitTests/KeyMSTests.cs index 19aa730..f0704cf 100644 --- a/ILMergeMsTest.UnitTests/KeyMSTests.cs +++ b/ILMergeMsTest.UnitTests/KeyMSTests.cs @@ -7,15 +7,19 @@ using TestTools; using System.Security.Cryptography; using System.Collections.Generic; +using System.Diagnostics.CodeAnalysis; namespace ILMergeMsTest.UnitTests { /// /// The purpose of this class is to demonstrate that MSTest/MSTest2 is just as simple to use as nUnit. - /// MSTest and nUnit both have their own strengths and weeknesses, but for the most part - these tests are simple. + /// MSTest and NUnit both have their own strengths and weaknesses, but for the most part - these tests are simple. + /// Test output and working directory is within the generated "TestResults" folder - it is cleansed for all "Release" test runs, + /// and partially persists for "Debug" test runs (the NUnit tests are using the system's "Temp" directory, which is not preferable). /// + [ExcludeFromCodeCoverage] [TestClass] - public class KeyMSTests + public sealed class KeyMSTests { #region Private Members private const string Extension = ".dll"; diff --git a/TestTools/Extensions.cs b/TestTools/Extensions.cs index a538f5d..c57b3ad 100644 --- a/TestTools/Extensions.cs +++ b/TestTools/Extensions.cs @@ -1,10 +1,12 @@ using ILMerging; +using System.Diagnostics.CodeAnalysis; using System.Linq; using System.Reflection; using TestTools.Helpers; namespace TestTools { + [ExcludeFromCodeCoverage] internal static class Extensions { public static void SetUpInputAssemblyForTest(this ILMerge ilMerge, Assembly inputAssembly) diff --git a/TestTools/Helpers/CspContainerUtils.cs b/TestTools/Helpers/CspContainerUtils.cs index 9698757..ec86a9c 100644 --- a/TestTools/Helpers/CspContainerUtils.cs +++ b/TestTools/Helpers/CspContainerUtils.cs @@ -1,7 +1,9 @@ -using System.Security.Cryptography; +using System.Diagnostics.CodeAnalysis; +using System.Security.Cryptography; namespace TestTools.Helpers { + [ExcludeFromCodeCoverage] public static class CspContainerUtils { public static void ImportBlob(bool machineLevel, string containerName, KeyNumber keyNumber, byte[] blob) diff --git a/TestTools/Helpers/ProcessUtils.cs b/TestTools/Helpers/ProcessUtils.cs index f6d47e0..fe4f022 100644 --- a/TestTools/Helpers/ProcessUtils.cs +++ b/TestTools/Helpers/ProcessUtils.cs @@ -1,10 +1,12 @@ using System; using System.Collections.Generic; using System.Diagnostics; +using System.Diagnostics.CodeAnalysis; using System.Text; namespace TestTools.Helpers { + [ExcludeFromCodeCoverage] public static class ProcessUtils { public static ProcessResult Run(ProcessStartInfo startInfo) diff --git a/TestTools/Helpers/ShadowCopyUtils.cs b/TestTools/Helpers/ShadowCopyUtils.cs index 28f45ba..5d9a1fb 100644 --- a/TestTools/Helpers/ShadowCopyUtils.cs +++ b/TestTools/Helpers/ShadowCopyUtils.cs @@ -1,11 +1,13 @@ using System; using System.Collections.Generic; +using System.Diagnostics.CodeAnalysis; using System.IO; using System.Linq; using System.Reflection; namespace TestTools.Helpers { + [ExcludeFromCodeCoverage] public static class ShadowCopyUtils { public static IEnumerable GetTransitiveClosure(params Assembly[] assemblies) diff --git a/TestTools/Helpers/StackEnumerator.cs b/TestTools/Helpers/StackEnumerator.cs index 8e7df5a..89323a8 100644 --- a/TestTools/Helpers/StackEnumerator.cs +++ b/TestTools/Helpers/StackEnumerator.cs @@ -1,9 +1,11 @@ using System; using System.Collections.Generic; using System.ComponentModel; +using System.Diagnostics.CodeAnalysis; namespace TestTools.Helpers { + [ExcludeFromCodeCoverage] public static class StackEnumerator { public static StackEnumerator Create(params T[] initial) => new StackEnumerator(initial); @@ -14,6 +16,7 @@ public static class StackEnumerator public static StackEnumerator Create(TContext initialContext, IEnumerator initial) => new StackEnumerator(initialContext, initial); } + [ExcludeFromCodeCoverage] public sealed class StackEnumerator : IDisposable { private readonly Stack> stack = new Stack>(); @@ -76,6 +79,7 @@ public void Dispose() } } + [ExcludeFromCodeCoverage] public sealed class StackEnumerator : IDisposable { public struct ContextCurrent diff --git a/TestTools/Helpers/TempFile.cs b/TestTools/Helpers/TempFile.cs index d5e97a7..cdb0022 100644 --- a/TestTools/Helpers/TempFile.cs +++ b/TestTools/Helpers/TempFile.cs @@ -1,10 +1,12 @@ using System; using System.Diagnostics; +using System.Diagnostics.CodeAnalysis; using System.IO; using System.Threading; namespace TestTools.Helpers { + [ExcludeFromCodeCoverage] [DebuggerDisplay("{ToString(),nq}")] public sealed class TempFile : IDisposable {