Repository navigation
Expand file tree
/
Copy pathtest_HostingBasic.cs
More file actions
143 lines (124 loc) · 4.74 KB
/
Copy pathtest_HostingBasic.cs
File metadata and controls
143 lines (124 loc) · 4.74 KB
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
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
using System;
using System.IO;
using System.Management.Automation;
using System.Security;
using Microsoft.Management.Infrastructure;
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);
}
}
}
}