public async Task ShouldGenerateCorrectContent()
        {
            string         fileName = @"C:\MockFileName.ps1";
            IList <string> commands = new List <string>()
            {
                @"Start-Process 'C:\windows\system32\notepad.exe'",
                @"Start-Process .",
            };

            IOptions options = new Options();
            IPowershellTranslator translator   = new PowershellTranslator();
            UTF8Encoding          uTF8Encoding = new UTF8Encoding();
            string strContent = string.Empty;

            LiteralScript literalScript = new LiteralScript(
                commands,
                translator,
                fileName,
                (path, content, token) =>
            {
                strContent = uTF8Encoding.GetString(content);
                return(Task.CompletedTask);
            });
            await literalScript.Process(options);

            string expected = @"Start-Process 'C:\windows\system32\notepad.exe'
Start-Process .";
            string actual   = strContent;

            Assert.Equal(expected, actual, ignoreLineEndingDifferences: true);
        }
        public async Task ShouldCopyFromCorrectSource()
        {
            string         fileName = @"C:\MockFileName.ps1";
            IList <string> commands = new List <string>()
            {
                @"Start-Process 'C:\windows\system32\notepad.exe'",
                @"Start-Process .",
            };

            IOptions options = new Options();
            IPowershellTranslator translator = new PowershellTranslator();
            string sourceFilePath            = string.Empty;

            LiteralScript literalScript = new LiteralScript(
                commands,
                translator,
                fileName,
                (path, content, token) =>
            {
                sourceFilePath = path;
                return(Task.CompletedTask);
            });
            await literalScript.Process(options);

            string expected = fileName;
            string actual   = sourceFilePath;

            Assert.Equal(expected, actual);
        }
Exemple #3
0
        public static async Task <LiteralScript> ProcessLiteralScriptFactory(IList <string> scripts, IPowershellTranslator translator, string scriptName, IOptions options)
        {
            var literalScript = new LiteralScript(scripts, translator, scriptName);
            await literalScript.Process(options);

            return(literalScript);
        }
Exemple #4
0
        public void ShouldInitializeTranslator()
        {
            IList <string> commands = new List <string>()
            {
                @"Start-Process 'C:\windows\system32\notepad.exe'",
                @"Start-Process .",
            };
            LiteralScript literalScript = new LiteralScript(commands);

            var actual = literalScript.Translator;

            Assert.NotNull(actual);
        }
Exemple #5
0
        public async Task ShouldBuildCorrectMainScriptContentWithSingleScript()
        {
            IList <string> actualScripts = new List <string>();

            IOptions options = new Options();
            ISandboxConfigurationBuilder configurationBuilder = new SandboxConfigurationBuilder(new Options());

            Action <string> createDirectory   = path => { };
            Action <string> deleteFiles       = path => { };
            Action <string> deleteDirectories = path => { };

            Func <string, Task> startProcess = path => Task.CompletedTask;
            Func <string, byte[], CancellationToken, Task> writeAllBytes = (path, content, token) => Task.CompletedTask;
            Func <string, string, CancellationToken, Task> copyFiles     = (source, destination, token) => Task.CompletedTask;
            Func <IList <string>, IPowershellTranslator, string, IOptions, Task <LiteralScript> > literalScriptFactory = async(scripts, translator, name, options) =>
            {
                actualScripts = scripts;
                var literalScript = new LiteralScript(scripts, translator, name, writeAllBytes);
                await literalScript.Process(options);

                return(literalScript);
            };

            var sandbox = new Sandbox(options, configurationBuilder, createDirectory, deleteFiles, deleteDirectories, startProcess, literalScriptFactory);
            await sandbox.Run(
                // Raw script that will be executed
                new LiteralScript(new List <string>()
            {
                @"Start-Process 'C:\windows\system32\notepad.exe'"
            },
                                  new PowershellTranslator(null, () => 1000),
                                  "mockScriptName1.ps1",
                                  writeAllBytes));

            IList <string> expected = new List <string>()
            {
                @"powershell.exe -ExecutionPolicy Bypass -File C:\Users\WDAGUtilityAccount\Desktop\Sandbox\mockScriptName1.ps1 3>&1 2>&1 > ""C:\Users\WDAGUtilityAccount\Desktop\Log_1000.txt""",
            };
            IList <string> actual = actualScripts;

            Assert.Equal(expected, actual);
        }
Exemple #6
0
        public async Task ShouldCallSandboxConfigurationBuilderAndStartProcess()
        {
            int timesCalled = 0;

            IOptions            options           = new Options();
            Action <string>     createDirectory   = path => { };
            Action <string>     deleteFiles       = path => { };
            Action <string>     deleteDirectories = path => { };
            Func <string, Task> startProcess      = path =>
            {
                timesCalled++;
                return(Task.CompletedTask);
            };
            Func <string, byte[], CancellationToken, Task> writeAllBytes = (path, content, token) => Task.CompletedTask;
            Func <string, string, CancellationToken, Task> copyFiles     = (source, destination, token) => Task.CompletedTask;
            Func <IList <string>, IPowershellTranslator, string, IOptions, Task <LiteralScript> > literalScriptFactory = async(scripts, translator, name, options) =>
            {
                var literalScript = new LiteralScript(scripts, translator, name, writeAllBytes);
                await literalScript.Process(options);

                return(literalScript);
            };

            Mock <ISandboxConfigurationBuilder> configurationBuilder = new Mock <ISandboxConfigurationBuilder>();

            var sandbox = new Sandbox(options, configurationBuilder.Object, createDirectory, deleteFiles, deleteDirectories, startProcess, literalScriptFactory);
            await sandbox.Run(new List <IScript>()
            {
                // Raw script that will be executed
                new LiteralScript(new List <string>()
                {
                    @"Start-Process 'C:\windows\system32\notepad.exe'"
                },
                                  new PowershellTranslator(),
                                  "mockScriptName1.ps1",
                                  writeAllBytes),

                // Script that will open the browser
                new ExternalScript(
                    new MockFileSystemInfo(@"C:\Users\OpenBrowserScript.ps1"),
                    new PowershellTranslator(),
                    copyFiles),

                // Script that will open explorer
                new ExternalScript(
                    new MockFileSystemInfo(@"C:\Users\OpenExplorerScript.ps1"),
                    new PowershellTranslator(),
                    copyFiles),

                // Script that will install curl and fiddler using scoop package manager
                new ScoopPackageManagerScript(
                    new List <string>()
                {
                    "curl", "fiddler"
                },
                    new PowershellTranslator(),
                    "mockScriptName2",
                    new StringBuilder(),
                    writeAllBytes),
            });


            configurationBuilder.Verify(c => c.Build(It.IsAny <string>(), It.IsAny <CancellationToken>()), Times.Once());
            Assert.Equal(1, timesCalled);
        }