|
| 1 | +// Licensed to the .NET Foundation under one or more agreements. |
| 2 | +// The .NET Foundation licenses this file to you under the MIT license. |
| 3 | + |
| 4 | +using System.IO; |
| 5 | +using Xunit; |
| 6 | +using Xunit.Abstractions; |
| 7 | +using Wasm.Build.Tests; |
| 8 | + |
| 9 | +#nullable enable |
| 10 | + |
| 11 | +namespace Wasi.Build.Tests; |
| 12 | + |
| 13 | +public class ILStripTests : BuildTestBase |
| 14 | +{ |
| 15 | + public ILStripTests(ITestOutputHelper output, SharedBuildPerTestClassFixture buildContext) |
| 16 | + : base(output, buildContext) |
| 17 | + { |
| 18 | + } |
| 19 | + |
| 20 | + [Theory] |
| 21 | + [InlineData("", /*expectILStripping*/ true, /*singleFileBundle*/false)] // Default case |
| 22 | + [InlineData("", /*expectILStripping*/ true, /*singleFileBundle*/true)] // Default case |
| 23 | + [InlineData("false", /*expectILStripping*/ false, /*singleFileBundle*/false)] // the opposite of the default case |
| 24 | + [InlineData("false", /*expectILStripping*/ false, /*singleFileBundle*/true)] // the opposite of the default case |
| 25 | + public void WasmStripILAfterAOT_TestDefaultAndOverride(string stripILAfterAOT, bool expectILStripping, bool singleFileBundle) |
| 26 | + { |
| 27 | + string config = "Release"; |
| 28 | + string id = $"{config}_{GetRandomId()}"; |
| 29 | + string projectFile = CreateWasmTemplateProject(id, "wasiconsole"); |
| 30 | + string projectName = Path.GetFileNameWithoutExtension(projectFile); |
| 31 | + |
| 32 | + string extraProperties = "<RunAOTCompilation>true</RunAOTCompilation>"; |
| 33 | + if (singleFileBundle) |
| 34 | + extraProperties += "<WasmSingleFileBundle>true</WasmSingleFileBundle>"; |
| 35 | + if (!string.IsNullOrEmpty(stripILAfterAOT)) |
| 36 | + extraProperties += $"<WasmStripILAfterAOT>{stripILAfterAOT}</WasmStripILAfterAOT>"; |
| 37 | + AddItemsPropertiesToProject(projectFile, extraProperties); |
| 38 | + |
| 39 | + var buildArgs = new BuildArgs(projectName, config, AOT: true, ProjectFileContents: id, ExtraBuildArgs: null); |
| 40 | + buildArgs = ExpandBuildArgs(buildArgs); |
| 41 | + |
| 42 | + BuildProject(buildArgs, |
| 43 | + id: id, |
| 44 | + new BuildProjectOptions( |
| 45 | + DotnetWasmFromRuntimePack: false, |
| 46 | + CreateProject: false, |
| 47 | + Publish: true, |
| 48 | + TargetFramework: BuildTestBase.DefaultTargetFramework, |
| 49 | + UseCache: false)); |
| 50 | + |
| 51 | + string runArgs = $"run --no-silent --no-build -c {config}"; |
| 52 | + new RunCommand(s_buildEnv, _testOutput, label: id) |
| 53 | + .WithWorkingDirectory(_projectDir!) |
| 54 | + .ExecuteWithCapturedOutput(runArgs) |
| 55 | + .EnsureSuccessful(); |
| 56 | + |
| 57 | + string frameworkDir = singleFileBundle ? "" : Path.Combine(_projectDir!, "bin", config, DefaultTargetFramework, "wasi-wasm", "AppBundle", "managed"); |
| 58 | + string objBuildDir = Path.Combine(_projectDir!, "obj", config, DefaultTargetFramework, "wasi-wasm", "wasm", "for-publish"); |
| 59 | + TestWasmStripILAfterAOTOutput(objBuildDir, frameworkDir, expectILStripping, singleFileBundle, _testOutput); |
| 60 | + } |
| 61 | + |
| 62 | + private static void TestWasmStripILAfterAOTOutput(string objBuildDir, string appBundleFrameworkDir, bool expectILStripping, bool singleFileBundle, ITestOutputHelper testOutput) |
| 63 | + { |
| 64 | + string origAssemblyDir = Path.Combine(objBuildDir, "aot-in"); |
| 65 | + string strippedAssemblyDir = Path.Combine(objBuildDir, "stripped"); |
| 66 | + Assert.True(Directory.Exists(origAssemblyDir), $"Could not find the original AOT input assemblies dir: {origAssemblyDir}"); |
| 67 | + if (expectILStripping) |
| 68 | + Assert.True(Directory.Exists(strippedAssemblyDir), $"Could not find the stripped assemblies dir: {strippedAssemblyDir}"); |
| 69 | + else |
| 70 | + Assert.False(Directory.Exists(strippedAssemblyDir), $"Expected {strippedAssemblyDir} to not exist"); |
| 71 | + |
| 72 | + string assemblyToExamine = "System.Private.CoreLib.dll"; |
| 73 | + string originalAssembly = Path.Combine(objBuildDir, origAssemblyDir, assemblyToExamine); |
| 74 | + string strippedAssembly = Path.Combine(objBuildDir, strippedAssemblyDir, assemblyToExamine); |
| 75 | + string includedAssembly = Path.Combine(appBundleFrameworkDir, assemblyToExamine); |
| 76 | + |
| 77 | + Assert.True(File.Exists(originalAssembly), $"Expected {nameof(originalAssembly)} {originalAssembly} to exist"); |
| 78 | + if (!singleFileBundle) |
| 79 | + Assert.True(File.Exists(includedAssembly), $"Expected {nameof(includedAssembly)} {includedAssembly} to exist"); |
| 80 | + if (expectILStripping) |
| 81 | + Assert.True(File.Exists(strippedAssembly), $"Expected {nameof(strippedAssembly)} {strippedAssembly} to exist"); |
| 82 | + else |
| 83 | + Assert.False(File.Exists(strippedAssembly), $"Expected {strippedAssembly} to not exist"); |
| 84 | + |
| 85 | + string compressedOriginalAssembly = Utils.GZipCompress(originalAssembly); |
| 86 | + string? compressedIncludedAssembly = null; |
| 87 | + FileInfo compressedOriginalAssembly_fi = new FileInfo(compressedOriginalAssembly); |
| 88 | + FileInfo? compressedincludedAssembly_fi = null; |
| 89 | + |
| 90 | + testOutput.WriteLine ($"compressedOriginalAssembly_fi: {compressedOriginalAssembly_fi.Length}, {compressedOriginalAssembly}"); |
| 91 | + if (!singleFileBundle) |
| 92 | + { |
| 93 | + compressedIncludedAssembly = Utils.GZipCompress(includedAssembly)!; |
| 94 | + compressedincludedAssembly_fi = new FileInfo(compressedIncludedAssembly); |
| 95 | + testOutput.WriteLine ($"compressedincludedAssembly_fi: {compressedincludedAssembly_fi.Length}, {compressedIncludedAssembly}"); |
| 96 | + } |
| 97 | + |
| 98 | + if (expectILStripping) |
| 99 | + { |
| 100 | + string compressedStrippedAssembly = Utils.GZipCompress(strippedAssembly); |
| 101 | + FileInfo compressedStrippedAssembly_fi = new FileInfo(compressedStrippedAssembly); |
| 102 | + testOutput.WriteLine ($"compressedStrippedAssembly_fi: {compressedStrippedAssembly_fi.Length}, {compressedStrippedAssembly}"); |
| 103 | + |
| 104 | + // original > stripped assembly |
| 105 | + Assert.True(compressedOriginalAssembly_fi.Length > compressedStrippedAssembly_fi.Length, |
| 106 | + $"Expected original assembly ({compressedOriginalAssembly}) size ({compressedOriginalAssembly_fi.Length}) " + |
| 107 | + $"to be bigger than the stripped assembly ({compressedStrippedAssembly}) size ({compressedStrippedAssembly_fi.Length})"); |
| 108 | + |
| 109 | + if (!singleFileBundle) |
| 110 | + { |
| 111 | + // included == stripped assembly |
| 112 | + Assert.True(compressedincludedAssembly_fi!.Length == compressedStrippedAssembly_fi.Length, |
| 113 | + $"Expected included assembly ({compressedIncludedAssembly}) size ({compressedincludedAssembly_fi.Length}) " + |
| 114 | + $"to be the same as the stripped assembly ({compressedStrippedAssembly}) size ({compressedStrippedAssembly_fi.Length})"); |
| 115 | + } |
| 116 | + } |
| 117 | + else |
| 118 | + { |
| 119 | + if (!singleFileBundle) |
| 120 | + { |
| 121 | + // original == included assembly |
| 122 | + Assert.True(compressedincludedAssembly_fi!.Length == compressedOriginalAssembly_fi.Length, |
| 123 | + $"Expected included assembly ({compressedIncludedAssembly}) size ({compressedincludedAssembly_fi.Length}) " + |
| 124 | + $"to be the same as the original assembly ({compressedOriginalAssembly}) size ({compressedOriginalAssembly_fi.Length})"); |
| 125 | + } |
| 126 | + } |
| 127 | + } |
| 128 | +} |
0 commit comments