|
| 1 | +// Copyright (c) Microsoft Corporation. |
| 2 | +// Licensed under the MIT License. |
| 3 | + |
| 4 | +using System.IO; |
| 5 | +using System.Linq; |
| 6 | +using BuildXL.FrontEnd.Sdk; |
| 7 | +using BuildXL.FrontEnd.Utilities; |
| 8 | +using BuildXL.Processes; |
| 9 | +using BuildXL.ProcessPipExecutor; |
| 10 | +using BuildXL.Utilities.Configuration; |
| 11 | +using BuildXL.Utilities.Configuration.Mutable; |
| 12 | +using BuildXL.Utilities.Core; |
| 13 | +using Test.BuildXL.Processes; |
| 14 | +using Test.BuildXL.TestUtilities.Xunit; |
| 15 | +using Xunit; |
| 16 | +using Xunit.Abstractions; |
| 17 | + |
| 18 | +namespace Test.BuildXL.FrontEnd.Utilities |
| 19 | +{ |
| 20 | + public class FrontEndUtilitiesTest : TemporaryStorageTestBase |
| 21 | + { |
| 22 | + protected BuildXLContext Context { get; } |
| 23 | + protected FrontEndContext FrontEndContext { get; } |
| 24 | + protected IConfiguration Configuration { get; } |
| 25 | + protected PipEnvironment PipEnvironment { get; } |
| 26 | + |
| 27 | + public FrontEndUtilitiesTest(ITestOutputHelper output) : base(output) |
| 28 | + { |
| 29 | + Configuration = new ConfigurationImpl(); |
| 30 | + Context = BuildXLContext.CreateInstanceForTesting(); |
| 31 | + FrontEndContext = FrontEndContext.CreateInstanceForTesting( |
| 32 | + pathTable: Context.PathTable, symbolTable: Context.SymbolTable, qualifierTable: Context.QualifierTable, frontEndConfig: Configuration.FrontEnd, loggingContext: LoggingContext); |
| 33 | + PipEnvironment = new PipEnvironment(LoggingContext); |
| 34 | + } |
| 35 | + |
| 36 | + [Fact] |
| 37 | + public void RunSandboxedToolHasAllExpectedAccesses() |
| 38 | + { |
| 39 | + var input = GetFullPath("input"); |
| 40 | + var inputPath = AbsolutePath.Create(Context.PathTable, input); |
| 41 | + File.WriteAllText(input, "some input"); |
| 42 | + |
| 43 | + var output = GetFullPath("output"); |
| 44 | + var outputPath = AbsolutePath.Create(Context.PathTable, output); |
| 45 | + |
| 46 | + var temporaryDirectoryPath = AbsolutePath.Create(Context.PathTable, TemporaryDirectory); |
| 47 | + var nonExistentPath = AbsolutePath.Create(Context.PathTable, GetFullPath("non-existent")); |
| 48 | + |
| 49 | + string toolArguments; |
| 50 | + |
| 51 | + // Let's test a read, an enumeration, a write and an absent probe |
| 52 | + if (OperatingSystemHelper.IsWindowsOS) |
| 53 | + { |
| 54 | + toolArguments = "/C \"type input & dir & copy input output & if exist non-existent echo hi\""; |
| 55 | + } |
| 56 | + else |
| 57 | + { |
| 58 | + toolArguments = "-c \"cat input; ls; cp input output; if [ -e non-existent ]; then echo hi; fi\""; |
| 59 | + } |
| 60 | + |
| 61 | + var toolDirectory = AbsolutePath.Create(Context.PathTable, CmdHelper.OsShellExe).GetParent(Context.PathTable); |
| 62 | + |
| 63 | + var result = FrontEndUtilities.RunSandboxedToolAsync( |
| 64 | + FrontEndContext, |
| 65 | + CmdHelper.OsShellExe, |
| 66 | + buildStorageDirectory: TemporaryDirectory, |
| 67 | + fileAccessManifest: FrontEndUtilities.GenerateToolFileAccessManifest(FrontEndContext, toolDirectory), |
| 68 | + arguments: toolArguments, |
| 69 | + workingDirectory: TemporaryDirectory, |
| 70 | + description: $"Test sandboxed tool", |
| 71 | + PipEnvironment.GetBaseEnvironmentVariables()).GetAwaiter().GetResult(); |
| 72 | + |
| 73 | + XAssert.AreEqual(0, result.ExitCode); |
| 74 | + var allAccesses = result.AllUnexpectedFileAccesses.Union(result.ExplicitlyReportedFileAccesses); |
| 75 | + |
| 76 | + // type input |
| 77 | + XAssert.IsTrue(allAccesses.Any(a => a.RequestedAccess == RequestedAccess.Read && GetAbsolutePath(a) == inputPath)); |
| 78 | + // dir |
| 79 | + XAssert.IsTrue(allAccesses.Any(a => a.RequestedAccess == RequestedAccess.Enumerate && GetAbsolutePath(a) == temporaryDirectoryPath)); |
| 80 | + // copy input output |
| 81 | + XAssert.IsTrue(allAccesses.Any(a => a.RequestedAccess == RequestedAccess.Write && GetAbsolutePath(a) == outputPath)); |
| 82 | + // if exist non-existent echo hi |
| 83 | + XAssert.IsTrue(allAccesses.Any(a => a.RequestedAccess == RequestedAccess.Probe && GetAbsolutePath(a) == nonExistentPath)); |
| 84 | + } |
| 85 | + |
| 86 | + private AbsolutePath GetAbsolutePath(ReportedFileAccess access) => AbsolutePath.Create(Context.PathTable, access.GetPath(Context.PathTable)); |
| 87 | + } |
| 88 | +} |
0 commit comments