File size: 8,187 Bytes
8c763fb | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 | // Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
using System;
using System.IO;
using System.Management.Automation;
using System.Security;
using Microsoft.Management.Infrastructure;
using Microsoft.PowerShell;
using Xunit;
namespace PowerShell.Hosting.SDK.Tests
{
public static class HostingTests
{
[Fact]
public static void TestCommandFromUtility()
{
using (System.Management.Automation.PowerShell ps = System.Management.Automation.PowerShell.Create())
{
var results = ps.AddScript("Get-Verb -Verb get").Invoke();
foreach (dynamic item in results)
{
Assert.Equal("Get", item.Verb);
}
}
}
[Fact]
public static void TestCommandFromManagement()
{
using (System.Management.Automation.PowerShell ps = System.Management.Automation.PowerShell.Create())
{
var path = Environment.CurrentDirectory;
var results = ps.AddCommand("Test-Path").AddParameter("Path", path).Invoke<bool>();
foreach (dynamic item in results)
{
Assert.True(item);
}
}
}
[Fact]
public static void TestCommandFromCore()
{
using (System.Management.Automation.PowerShell ps = System.Management.Automation.PowerShell.Create())
{
var results = ps.AddScript(@"$i = 0 ; 1..3 | ForEach-Object { $i += $_} ; $i").Invoke<int>();
foreach (dynamic item in results)
{
Assert.Equal(6, item);
}
}
}
[SkippableFact]
public static void TestCommandFromMMI()
{
// Test is disabled since we do not have a CimCmdlets module released in the SDK.
Skip.IfNot(Platform.IsWindows);
using (System.Management.Automation.PowerShell ps = System.Management.Automation.PowerShell.Create())
{
var results = ps.AddScript("[Microsoft.Management.Infrastructure.CimInstance]::new('Win32_Process')").Invoke();
Assert.True(results.Count > 0);
}
}
[SkippableFact]
public static void TestCommandFromDiagnostics()
{
Skip.IfNot(Platform.IsWindows);
using (System.Management.Automation.PowerShell ps = System.Management.Automation.PowerShell.Create())
{
var results = ps.AddScript("Get-WinEvent -ListLog Application").Invoke();
foreach (dynamic item in results)
{
Assert.Equal("Application", item.LogName);
}
}
}
[SkippableFact]
public static void TestCommandFromSecurity()
{
Skip.IfNot(Platform.IsWindows);
using (System.Management.Automation.PowerShell ps = System.Management.Automation.PowerShell.Create())
{
var results = ps.AddScript("ConvertTo-SecureString -String test -AsPlainText -Force").Invoke<SecureString>();
Assert.IsType<SecureString>(results[0]);
}
}
[SkippableFact]
public static void TestCommandFromWSMan()
{
Skip.IfNot(Platform.IsWindows);
using (System.Management.Automation.PowerShell ps = System.Management.Automation.PowerShell.Create())
{
var results = ps.AddScript("Test-WSMan").Invoke();
foreach (dynamic item in results)
{
Assert.Equal("Microsoft Corporation", item.ProductVendor);
}
}
}
[Fact]
public static void TestCommandFromNative()
{
var fs = File.Create(Path.GetTempFileName());
fs.Close();
string target = fs.Name;
string path = Path.GetTempFileName();
using (System.Management.Automation.PowerShell ps = System.Management.Automation.PowerShell.Create())
{
// New-Item -ItemType SymbolicLink uses libpsl-native, hence using it for validating native dependencies.
string command = $"New-Item -ItemType SymbolicLink -Path {path} -Target {target}";
var results = ps.AddScript(command).Invoke<FileInfo>();
foreach (var item in results)
{
Assert.Equal(path, item.FullName);
}
}
if (File.Exists(path))
{
File.Delete(path);
}
if (File.Exists(target))
{
File.Delete(target);
}
}
/// <summary>
/// Reference assemblies should be handled correctly so that Add-Type works in the hosting scenario.
/// </summary>
[Fact]
public static void TestAddTypeCmdletInHostScenario()
{
string code = @"
using System;
public class Foo
{
public Foo(string name, string path)
{
this.Name = name;
this.Path = path;
}
public string Name;
public string Path;
}
";
using (System.Management.Automation.PowerShell ps = System.Management.Automation.PowerShell.Create())
{
ps.AddCommand("Add-Type").AddParameter("TypeDefinition", code).Invoke();
ps.Commands.Clear();
var results = ps.AddScript("[Foo]::new('Joe', 'Unknown')").Invoke();
Assert.Single(results);
dynamic foo = results[0];
Assert.Equal("Joe", foo.Name);
Assert.Equal("Unknown", foo.Path);
}
}
[Fact]
public static void TestConsoleShellScenario()
{
int ret = ConsoleShell.Start("Hello", string.Empty, new string[] { "-noprofile", "-c", "exit 42" });
Assert.Equal(42, ret);
}
/* Test disabled because CommandLineParser is static and can only be initialized once (above in TestConsoleShellScenario)
/// <summary>
/// ConsoleShell cannot start with both InitialSessionState and -ConfigurationFile argument configurations specified.
/// </summary>
[Fact]
public static void TestConsoleShellConfigConflictError()
{
var iss = System.Management.Automation.Runspaces.InitialSessionState.CreateDefault2();
int ret = ConsoleShell.Start(iss, "BannerText", string.Empty, new string[] { @"-ConfigurationFile ""noneSuch""" });
Assert.Equal(70, ret); // ExitCodeInitFailure.
}
*/
[Fact]
public static void TestBuiltInModules()
{
var iss = System.Management.Automation.Runspaces.InitialSessionState.CreateDefault2();
if (System.Management.Automation.Platform.IsWindows)
{
iss.ExecutionPolicy = Microsoft.PowerShell.ExecutionPolicy.RemoteSigned;
}
using var runspace = System.Management.Automation.Runspaces.RunspaceFactory.CreateRunspace(iss);
runspace.Open();
using var ps = System.Management.Automation.PowerShell.Create(runspace);
var results_1 = ps.AddScript("Write-Output Hello > $null; Get-Module").Invoke<System.Management.Automation.PSModuleInfo>();
Assert.Single(results_1);
var module = results_1[0];
Assert.Equal("Microsoft.PowerShell.Utility", module.Name);
ps.Commands.Clear();
var results_2 = ps.AddScript("Join-Path $PSHOME 'Modules' 'Microsoft.PowerShell.Utility' 'Microsoft.PowerShell.Utility.psd1'").Invoke<string>();
var moduleManifestPath = results_2[0];
Assert.Equal(moduleManifestPath, module.Path, ignoreCase: true);
}
}
}
|