mirror of
https://github.com/aphrody-labs/PowerShell.git
synced 2026-10-17 04:36:39 +00:00
Reorganize tests
- Pester source code moved to `test/Pester`, deleted `ext-src`. - Pester tests (.ps1 files) moved to `test/powershell` - xUnit tests (.cs files) moved to `test/csharp` - Third-party script test moved to `test/shebang`
This commit is contained in:
1 parent
ec08d9f46c
commit
bbebf2f76a
105 files changed
+1
-1
No files matched your search
@@ -0,0 +1,166 @@
|
||||
namespace PSTests
|
||||
{
|
||||
using System;
|
||||
using System.Globalization;
|
||||
using System.Management.Automation.Host;
|
||||
|
||||
/// <summary>
|
||||
/// This is a sample implementation of the PSHost abstract class for
|
||||
/// console applications. Not all members are implemented. Those that
|
||||
/// are not implemented throw a NotImplementedException exception or
|
||||
/// return nothing.
|
||||
/// </summary>
|
||||
internal class MyHost : PSHost
|
||||
{
|
||||
/// <summary>
|
||||
/// A reference to the PSHost implementation.
|
||||
/// </summary>
|
||||
private RunspaceTests program;
|
||||
|
||||
/// <summary>
|
||||
/// The culture information of the thread that created
|
||||
/// this object.
|
||||
/// </summary>
|
||||
private CultureInfo originalCultureInfo =
|
||||
CultureInfo.CurrentCulture;
|
||||
// System.Threading.Thread.CurrentThread.CurrentCulture;
|
||||
|
||||
/// <summary>
|
||||
/// The UI culture information of the thread that created
|
||||
/// this object.
|
||||
/// </summary>
|
||||
private CultureInfo originalUICultureInfo =
|
||||
CultureInfo.CurrentCulture;
|
||||
// System.Threading.Thread.CurrentThread.CurrentUICulture;
|
||||
|
||||
/// <summary>
|
||||
/// The identifier of this PSHost implementation.
|
||||
/// </summary>
|
||||
private Guid myId = Guid.NewGuid();
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the MyHost class. Keep
|
||||
/// a reference to the host application object so that it
|
||||
/// can be informed of when to exit.
|
||||
/// </summary>
|
||||
/// <param name="program">
|
||||
/// A reference to the host application object.
|
||||
/// </param>
|
||||
public MyHost(RunspaceTests program)
|
||||
{
|
||||
this.program = program;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Return the culture information to use. This implementation
|
||||
/// returns a snapshot of the culture information of the thread
|
||||
/// that created this object.
|
||||
/// </summary>
|
||||
public override System.Globalization.CultureInfo CurrentCulture
|
||||
{
|
||||
get { return this.originalCultureInfo; }
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Return the UI culture information to use. This implementation
|
||||
/// returns a snapshot of the UI culture information of the thread
|
||||
/// that created this object.
|
||||
/// </summary>
|
||||
public override System.Globalization.CultureInfo CurrentUICulture
|
||||
{
|
||||
get { return this.originalUICultureInfo; }
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// This implementation always returns the GUID allocated at
|
||||
/// instantiation time.
|
||||
/// </summary>
|
||||
public override Guid InstanceId
|
||||
{
|
||||
get { return this.myId; }
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Return a string that contains the name of the host implementation.
|
||||
/// Keep in mind that this string may be used by script writers to
|
||||
/// identify when your host is being used.
|
||||
/// </summary>
|
||||
public override string Name
|
||||
{
|
||||
get { return "MySampleConsoleHostImplementation"; }
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// This sample does not implement a PSHostUserInterface component so
|
||||
/// this property simply returns null.
|
||||
/// </summary>
|
||||
public override PSHostUserInterface UI
|
||||
{
|
||||
get { return null; }
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Return the version object for this application. Typically this
|
||||
/// should match the version resource in the application.
|
||||
/// </summary>
|
||||
public override Version Version
|
||||
{
|
||||
get { return new Version(1, 0, 0, 0); }
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Not implemented by this example class. The call fails with
|
||||
/// a NotImplementedException exception.
|
||||
/// </summary>
|
||||
public override void EnterNestedPrompt()
|
||||
{
|
||||
throw new NotImplementedException(
|
||||
"The method or operation is not implemented.");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Not implemented by this example class. The call fails
|
||||
/// with a NotImplementedException exception.
|
||||
/// </summary>
|
||||
public override void ExitNestedPrompt()
|
||||
{
|
||||
throw new NotImplementedException(
|
||||
"The method or operation is not implemented.");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// This API is called before an external application process is
|
||||
/// started. Typically it is used to save state so the parent can
|
||||
/// restore state that has been modified by a child process (after
|
||||
/// the child exits). In this example, this functionality is not
|
||||
/// needed so the method returns nothing.
|
||||
/// </summary>
|
||||
public override void NotifyBeginApplication()
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// This API is called after an external application process finishes.
|
||||
/// Typically it is used to restore state that a child process may
|
||||
/// have altered. In this example, this functionality is not
|
||||
/// needed so the method returns nothing.
|
||||
/// </summary>
|
||||
public override void NotifyEndApplication()
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Indicate to the host application that exit has
|
||||
/// been requested. Pass the exit code that the host
|
||||
/// application should use when exiting the process.
|
||||
/// </summary>
|
||||
/// <param name="exitCode">The exit code to use.</param>
|
||||
public override void SetShouldExit(int exitCode)
|
||||
{
|
||||
// this.program.ShouldExit = true;
|
||||
// this.program.ExitCode = exitCode;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
{
|
||||
"version": "1.0.0-*",
|
||||
"description": "PowerShell On Linux xUnit Tests",
|
||||
"authors": [ "andschwa" ],
|
||||
|
||||
"dependencies": {
|
||||
"NETStandard.Library": "1.0.0-rc2-23712",
|
||||
"Microsoft.PowerShell.Linux.Host": {
|
||||
"type": "build",
|
||||
"version": "1.0.0-*"
|
||||
},
|
||||
"xunit": "2.1.0-*",
|
||||
"xunit.runner.dnx": "2.1.0-*"
|
||||
},
|
||||
|
||||
"frameworks": { "dnxcore50": { } }
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
using Xunit;
|
||||
using System;
|
||||
using System.Management.Automation.Language;
|
||||
|
||||
namespace PSTests
|
||||
{
|
||||
public static class PSEnumerableBinderTests
|
||||
{
|
||||
[Fact]
|
||||
public static void TestIsComObject()
|
||||
{
|
||||
// It just needs an arbitrary object
|
||||
Assert.False(PSEnumerableBinder.IsComObject(42));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
using Xunit;
|
||||
using System;
|
||||
using System.Management.Automation;
|
||||
|
||||
namespace PSTests
|
||||
{
|
||||
public static class CorePsExtensionsTests
|
||||
{
|
||||
[Fact]
|
||||
public static void TestEnvironmentOSVersion()
|
||||
{
|
||||
// this test primarily checks that there is no exception thrown
|
||||
// NOTE: the type name is explicitly written here to make clear what
|
||||
// type and function is tested
|
||||
System.Management.Automation.Environment.OperatingSystem os = System.Management.Automation.Environment.OSVersion;
|
||||
Assert.NotNull(os);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,395 @@
|
||||
using Xunit;
|
||||
using System;
|
||||
using System.IO;
|
||||
using System.Diagnostics;
|
||||
using System.Management.Automation;
|
||||
|
||||
namespace PSTests
|
||||
{
|
||||
public static class PlatformTests
|
||||
{
|
||||
[Fact]
|
||||
public static void TestIsLinux()
|
||||
{
|
||||
Assert.True(Platform.IsLinux());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public static void TestHasCom()
|
||||
{
|
||||
Assert.False(Platform.HasCom());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public static void TestHasAmsi()
|
||||
{
|
||||
Assert.False(Platform.HasAmsi());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public static void TestUsesCodeSignedAssemblies()
|
||||
{
|
||||
Assert.False(Platform.UsesCodeSignedAssemblies());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public static void TestHasDriveAutoMounting()
|
||||
{
|
||||
Assert.False(Platform.HasDriveAutoMounting());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public static void TestHasRegistrySupport()
|
||||
{
|
||||
Assert.False(Platform.HasRegistrySupport());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public static void TestGetUserName()
|
||||
{
|
||||
var startInfo = new ProcessStartInfo
|
||||
{
|
||||
FileName = @"/usr/bin/env",
|
||||
Arguments = "whoami",
|
||||
RedirectStandardOutput = true,
|
||||
UseShellExecute = false
|
||||
};
|
||||
using (Process process = Process.Start(startInfo))
|
||||
{
|
||||
// Get output of call to whoami without trailing newline
|
||||
string username = process.StandardOutput.ReadToEnd().Trim();
|
||||
process.WaitForExit();
|
||||
|
||||
// The process should return an exit code of 0 on success
|
||||
Assert.Equal(0, process.ExitCode);
|
||||
// It should be the same as what our platform code returns
|
||||
Assert.Equal(username, Platform.NonWindowsGetUserName());
|
||||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public static void TestGetMachineName()
|
||||
{
|
||||
var startInfo = new ProcessStartInfo
|
||||
{
|
||||
FileName = @"/usr/bin/env",
|
||||
Arguments = "hostname",
|
||||
RedirectStandardOutput = true,
|
||||
UseShellExecute = false
|
||||
};
|
||||
using (Process process = Process.Start(startInfo))
|
||||
{
|
||||
// Get output of call to hostname without trailing newline
|
||||
string hostname = process.StandardOutput.ReadToEnd().Trim();
|
||||
process.WaitForExit();
|
||||
|
||||
// The process should return an exit code of 0 on success
|
||||
Assert.Equal(0, process.ExitCode);
|
||||
// It should be the same as what our platform code returns
|
||||
Assert.Equal(hostname, Platform.NonWindowsGetMachineName());
|
||||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public static void TestGetFQDN()
|
||||
{
|
||||
var startInfo = new ProcessStartInfo
|
||||
{
|
||||
FileName = @"/usr/bin/env",
|
||||
Arguments = "hostname --fqdn",
|
||||
RedirectStandardOutput = true,
|
||||
UseShellExecute = false
|
||||
};
|
||||
using (Process process = Process.Start(startInfo))
|
||||
{
|
||||
// Get output of call to hostname without trailing newline
|
||||
string hostname = process.StandardOutput.ReadToEnd().Trim();
|
||||
process.WaitForExit();
|
||||
|
||||
// The process should return an exit code of 0 on success
|
||||
Assert.Equal(0, process.ExitCode);
|
||||
// It should be the same as what our platform code returns
|
||||
Assert.Equal(hostname, Platform.NonWindowsGetHostName());
|
||||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public static void TestGetDomainName()
|
||||
{
|
||||
var startInfo = new ProcessStartInfo
|
||||
{
|
||||
FileName = @"/usr/bin/env",
|
||||
Arguments = "dnsdomainname",
|
||||
RedirectStandardOutput = true,
|
||||
UseShellExecute = false
|
||||
};
|
||||
using (Process process = Process.Start(startInfo))
|
||||
{
|
||||
// Get output of call to hostname without trailing newline
|
||||
string domainName = process.StandardOutput.ReadToEnd().Trim();
|
||||
process.WaitForExit();
|
||||
|
||||
// The process should return an exit code of 0 on success
|
||||
Assert.Equal(0, process.ExitCode);
|
||||
// It should be the same as what our platform code returns
|
||||
Assert.Equal(domainName, Platform.NonWindowsGetDomainName());
|
||||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public static void TestIsExecutable()
|
||||
{
|
||||
Assert.True(Platform.NonWindowsIsExecutable("/bin/ls"));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public static void TestIsNotExecutable()
|
||||
{
|
||||
Assert.False(Platform.NonWindowsIsExecutable("/etc/hosts"));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public static void TestDirectoryIsNotExecutable()
|
||||
{
|
||||
Assert.False(Platform.NonWindowsIsExecutable("/etc"));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public static void TestFileIsNotHardLink()
|
||||
{
|
||||
string path = @"/tmp/nothardlink";
|
||||
if (File.Exists(path))
|
||||
{
|
||||
File.Delete(path);
|
||||
}
|
||||
|
||||
File.Create(path);
|
||||
|
||||
FileSystemInfo fd = new FileInfo(path);
|
||||
|
||||
// Since this is the only reference to the file, it is not considered a
|
||||
// hardlink by our API (though all files are hardlinks on Linux)
|
||||
Assert.False(Platform.NonWindowsIsHardLink(fd));
|
||||
|
||||
File.Delete(path);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public static void TestFileIsHardLink()
|
||||
{
|
||||
string path = @"/tmp/originallink";
|
||||
if (File.Exists(path))
|
||||
{
|
||||
File.Delete(path);
|
||||
}
|
||||
|
||||
File.Create(path);
|
||||
|
||||
string link = "/tmp/newlink";
|
||||
|
||||
if (File.Exists(link))
|
||||
{
|
||||
File.Delete(link);
|
||||
}
|
||||
|
||||
var startInfo = new ProcessStartInfo
|
||||
{
|
||||
FileName = @"/usr/bin/env",
|
||||
Arguments = "ln " + path + " " + link,
|
||||
RedirectStandardOutput = true,
|
||||
UseShellExecute = false
|
||||
};
|
||||
|
||||
using (Process process = Process.Start(startInfo))
|
||||
{
|
||||
process.WaitForExit();
|
||||
Assert.Equal(0, process.ExitCode);
|
||||
}
|
||||
|
||||
|
||||
// Since there are now two references to the file, both are considered
|
||||
// hardlinks by our API (though all files are hardlinks on Linux)
|
||||
FileSystemInfo fd = new FileInfo(path);
|
||||
Assert.True(Platform.NonWindowsIsHardLink(fd));
|
||||
|
||||
fd = new FileInfo(link);
|
||||
Assert.True(Platform.NonWindowsIsHardLink(fd));
|
||||
|
||||
File.Delete(path);
|
||||
File.Delete(link);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public static void TestDirectoryIsNotHardLink()
|
||||
{
|
||||
string path = @"/tmp";
|
||||
|
||||
FileSystemInfo fd = new FileInfo(path);
|
||||
|
||||
Assert.False(Platform.NonWindowsIsHardLink(fd));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public static void TestNonExistantIsHardLink()
|
||||
{
|
||||
// A file that should *never* exist on a test machine:
|
||||
string path = @"/tmp/ThisFileShouldNotExistOnTestMachines";
|
||||
|
||||
// If the file exists, then there's a larger issue that needs to be looked at
|
||||
Assert.False(File.Exists(path));
|
||||
|
||||
// Convert `path` string to FileSystemInfo data type. And now, it should return true
|
||||
FileSystemInfo fd = new FileInfo(path);
|
||||
Assert.False(Platform.NonWindowsIsHardLink(fd));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public static void TestFileIsSymLink()
|
||||
{
|
||||
string path = @"/tmp/originallink";
|
||||
if (File.Exists(path))
|
||||
{
|
||||
File.Delete(path);
|
||||
}
|
||||
|
||||
File.Create(path);
|
||||
|
||||
string link = "/tmp/newlink";
|
||||
|
||||
if (File.Exists(link))
|
||||
{
|
||||
File.Delete(link);
|
||||
}
|
||||
|
||||
var startInfo = new ProcessStartInfo
|
||||
{
|
||||
FileName = @"/usr/bin/env",
|
||||
Arguments = "ln -s " + path + " " + link,
|
||||
RedirectStandardOutput = true,
|
||||
UseShellExecute = false
|
||||
};
|
||||
|
||||
using (Process process = Process.Start(startInfo))
|
||||
{
|
||||
process.WaitForExit();
|
||||
Assert.Equal(0, process.ExitCode);
|
||||
}
|
||||
|
||||
FileSystemInfo fd = new FileInfo(path);
|
||||
Assert.False(Platform.NonWindowsIsSymLink(fd));
|
||||
|
||||
fd = new FileInfo(link);
|
||||
Assert.True(Platform.NonWindowsIsSymLink(fd));
|
||||
|
||||
File.Delete(path);
|
||||
File.Delete(link);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public static void TestCommandLineArgvReturnsAnElement()
|
||||
{
|
||||
string testCommand= "Today";
|
||||
|
||||
string[] retval = Platform.CommandLineToArgv(testCommand);
|
||||
|
||||
Assert.Equal(1, retval.Length);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public static void TestCommandLineArgvWWithSpaces()
|
||||
{
|
||||
string testCommand= "Today is a good day or is it";
|
||||
|
||||
string[] retval = Platform.CommandLineToArgv(testCommand);
|
||||
|
||||
Assert.Equal("Today", retval[0]);
|
||||
Assert.Equal("is", retval[1]);
|
||||
Assert.Equal("a", retval[2]);
|
||||
Assert.Equal("good", retval[3]);
|
||||
Assert.Equal("day", retval[4]);
|
||||
Assert.Equal("or", retval[5]);
|
||||
Assert.Equal("is", retval[6]);
|
||||
Assert.Equal("it", retval[7]);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public static void TestCommandLineToArgvWTabsAreTreatedAsSpaces()
|
||||
{
|
||||
string testCommand = "Today \t is a good day";
|
||||
|
||||
string[] retval = Platform.CommandLineToArgv(testCommand);
|
||||
|
||||
Assert.Equal("Today", retval[0]);
|
||||
Assert.Equal("is", retval[1]);
|
||||
Assert.Equal("a", retval[2]);
|
||||
Assert.Equal("good", retval[3]);
|
||||
Assert.Equal("day", retval[4]);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public static void TestCommandLineToArgvWQuotesAreArgs()
|
||||
{
|
||||
string testCommand = "Today is \"a good\" day";
|
||||
|
||||
string[] retval = Platform.CommandLineToArgv(testCommand);
|
||||
|
||||
Assert.Equal(4, retval.Length);
|
||||
Assert.Equal("Today", retval[0]);
|
||||
Assert.Equal("is", retval[1]);
|
||||
Assert.Equal("\"a good\"", retval[2]);
|
||||
Assert.Equal("day", retval[3]);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public static void TestCommandLineToArgvWEvenNumberBackSlashes()
|
||||
{
|
||||
string test1 = "a\\b c d";
|
||||
string test2 = "a\\b\\c d";
|
||||
string test3 = "a \\b\\c\\d";
|
||||
string test4 = "a\\\\\\b";
|
||||
string[] retval;
|
||||
|
||||
retval = Platform.CommandLineToArgv(test1);
|
||||
|
||||
Assert.Equal(3, retval.Length);
|
||||
Assert.Equal("a\\b", retval[0]);
|
||||
Assert.Equal("c", retval[1]);
|
||||
Assert.Equal("d", retval[2]);
|
||||
|
||||
retval = Platform.CommandLineToArgv(test2);
|
||||
|
||||
Assert.Equal(2, retval.Length);
|
||||
Assert.Equal("a\\b\\c", retval[0]);
|
||||
Assert.Equal("d", retval[1]);
|
||||
|
||||
retval = Platform.CommandLineToArgv(test3);
|
||||
|
||||
Assert.Equal(2, retval.Length);
|
||||
Assert.Equal("a", retval[0]);
|
||||
Assert.Equal("\\b\\c\\d", retval[1]);
|
||||
|
||||
retval = Platform.CommandLineToArgv(test4);
|
||||
|
||||
Assert.Equal(1, retval.Length);
|
||||
Assert.Equal("a\\\\\\b", retval[0]);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public static void TestCommandLineToArgvwOddNumberWithBackSlashes()
|
||||
{
|
||||
string test1 = "a\\\"b";
|
||||
string test2 = "a\\\\\\\"b";
|
||||
string[] retval;
|
||||
|
||||
retval = Platform.CommandLineToArgv(test1);
|
||||
|
||||
Assert.Equal(1, retval.Length);
|
||||
Assert.Equal("a\\\"b", retval[0]);
|
||||
|
||||
retval = Platform.CommandLineToArgv(test2);
|
||||
|
||||
Assert.Equal(1, retval.Length);
|
||||
Assert.Equal("a\\\\\\\"b", retval[0]);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
using Xunit;
|
||||
using System;
|
||||
using System.Management.Automation;
|
||||
|
||||
namespace PSTests
|
||||
{
|
||||
public static class PSTypeExtensionsTests
|
||||
{
|
||||
[Fact]
|
||||
public static void TestIsComObject()
|
||||
{
|
||||
// It just needs an arbitrary type
|
||||
Assert.False(PSTypeExtensions.IsComObject(42.GetType()));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
using Xunit;
|
||||
using System;
|
||||
using System.Management.Automation;
|
||||
using Microsoft.PowerShell.Commands;
|
||||
|
||||
namespace PSTests
|
||||
{
|
||||
public static class FileSystemProviderTests
|
||||
{
|
||||
[Fact]
|
||||
public static void TestCreateJunctionFails()
|
||||
{
|
||||
Assert.False(InternalSymbolicLinkLinkCodeMethods.CreateJunction("",""));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
using Xunit;
|
||||
using System;
|
||||
using System.Management.Automation;
|
||||
|
||||
namespace PSTests
|
||||
{
|
||||
// Not static because a test requires non-const variables
|
||||
public class MshSnapinInfoTests
|
||||
{
|
||||
// Test that it does not throw an exception
|
||||
[Fact]
|
||||
public void TestReadRegistryInfo()
|
||||
{
|
||||
Version someVersion = null;
|
||||
string someString = null;
|
||||
PSSnapInReader.ReadRegistryInfo(out someVersion, out someString, out someString, out someString, out someString, out someVersion);
|
||||
}
|
||||
|
||||
// PublicKeyToken is null on Linux
|
||||
[Fact]
|
||||
public void TestReadCoreEngineSnapIn()
|
||||
{
|
||||
PSSnapInInfo pSSnapInInfo = PSSnapInReader.ReadCoreEngineSnapIn();
|
||||
Assert.Contains("PublicKeyToken=null", pSSnapInInfo.AssemblyName);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,71 @@
|
||||
using Xunit;
|
||||
using System;
|
||||
using System.Management.Automation;
|
||||
using System.Diagnostics;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Xml.Linq;
|
||||
|
||||
namespace PSTests
|
||||
{
|
||||
public static class OmiInterfaceTests
|
||||
{
|
||||
[Fact]
|
||||
public static void TestHostName()
|
||||
{
|
||||
const string ns = "root/omi";
|
||||
const string cn = "OMI_Identify";
|
||||
const string property = "SystemName";
|
||||
|
||||
string expected = null;
|
||||
|
||||
var startInfo = new ProcessStartInfo
|
||||
{
|
||||
FileName = @"/usr/bin/env",
|
||||
Arguments = "hostname",
|
||||
RedirectStandardOutput = true,
|
||||
UseShellExecute = false
|
||||
};
|
||||
using (Process process = Process.Start(startInfo))
|
||||
{
|
||||
// Get output of call to hostname without trailing newline
|
||||
expected = process.StandardOutput.ReadToEnd().Trim();
|
||||
process.WaitForExit();
|
||||
|
||||
// The process should return an exit code of 0 on success
|
||||
Assert.Equal(0, process.ExitCode);
|
||||
}
|
||||
|
||||
string value = null;
|
||||
OmiInterface oi = new OmiInterface();
|
||||
oi.GetOmiValue(ns, cn, property, out value);
|
||||
Assert.Equal(expected, value);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public static void TestXHugeNumberEnumerable()
|
||||
{
|
||||
const string ns = "interop";
|
||||
const string cn = "X_HugeNumber";
|
||||
|
||||
OmiInterface oi = new OmiInterface();
|
||||
IEnumerable<XElement> elements;
|
||||
oi.GetOmiValues(ns, cn, out elements);
|
||||
Assert.True(elements.FirstOrDefault() != null);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public static void TestXHugeNumberOmiData()
|
||||
{
|
||||
const string ns = "interop";
|
||||
const string cn = "X_HugeNumber";
|
||||
|
||||
OmiInterface oi = new OmiInterface();
|
||||
|
||||
OmiData data;
|
||||
oi.GetOmiValues(ns, cn, out data);
|
||||
Assert.True(data != null);
|
||||
Assert.Equal(data.Values.Count(), 22);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
using Xunit;
|
||||
using System;
|
||||
using System.Management.Automation;
|
||||
|
||||
namespace PSTests
|
||||
{
|
||||
public static class PSVersionInfoTests
|
||||
{
|
||||
[Fact]
|
||||
public static void TestVersions()
|
||||
{
|
||||
// test that a non-null version table is returned, and
|
||||
// that it does not throw
|
||||
Assert.NotNull(PSVersionInfo.GetPSVersionTable());
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,141 @@
|
||||
using Xunit;
|
||||
using System;
|
||||
using System.Management.Automation;
|
||||
using System.Management.Automation.Runspaces;
|
||||
|
||||
namespace PSTests
|
||||
{
|
||||
// NOTE: do not call AddCommand("out-host") after invoking or MergeMyResults,
|
||||
// otherwise Invoke will not return any objects
|
||||
|
||||
public class RunspaceTests
|
||||
{
|
||||
private static int count = 3;
|
||||
private static string script = String.Format($"get-process | select-object -first {count}");
|
||||
|
||||
[Fact]
|
||||
public void TestRunspaceWithPipeline()
|
||||
{
|
||||
using (Runspace runspace = RunspaceFactory.CreateRunspace())
|
||||
{
|
||||
runspace.Open();
|
||||
|
||||
using (var pipeline = runspace.CreatePipeline(script))
|
||||
{
|
||||
int objCount = 0;
|
||||
foreach (var result in pipeline.Invoke())
|
||||
{
|
||||
++objCount;
|
||||
Assert.NotNull(result);
|
||||
}
|
||||
Assert.Equal(count, objCount);
|
||||
}
|
||||
|
||||
runspace.Close();
|
||||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void TestRunspaceWithPowerShell()
|
||||
{
|
||||
using (var runspace = RunspaceFactory.CreateRunspace())
|
||||
{
|
||||
runspace.Open();
|
||||
|
||||
using (PowerShell powerShell = PowerShell.Create())
|
||||
{
|
||||
powerShell.Runspace = runspace;
|
||||
|
||||
powerShell.AddScript(script);
|
||||
|
||||
int objCount = 0;
|
||||
foreach (var result in powerShell.Invoke())
|
||||
{
|
||||
++objCount;
|
||||
Assert.NotNull(result);
|
||||
}
|
||||
Assert.Equal(count, objCount);
|
||||
}
|
||||
|
||||
runspace.Close();
|
||||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void TestRunspaceWithPowerShellAndHost()
|
||||
{
|
||||
MyHost myHost = new MyHost(this);
|
||||
using (var runspace = RunspaceFactory.CreateRunspace(myHost))
|
||||
{
|
||||
runspace.Open();
|
||||
|
||||
using (PowerShell powerShell = PowerShell.Create())
|
||||
{
|
||||
powerShell.Runspace = runspace;
|
||||
|
||||
powerShell.AddScript(script);
|
||||
|
||||
int objCount = 0;
|
||||
foreach (var result in powerShell.Invoke())
|
||||
{
|
||||
++objCount;
|
||||
Assert.NotNull(result);
|
||||
}
|
||||
Assert.Equal(count, objCount);
|
||||
}
|
||||
|
||||
runspace.Close();
|
||||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void TestRunspaceWithFunction()
|
||||
{
|
||||
MyHost myHost = new MyHost(this);
|
||||
using (var runspace = RunspaceFactory.CreateRunspace(myHost))
|
||||
{
|
||||
runspace.Open();
|
||||
|
||||
using (PowerShell powerShell = PowerShell.Create())
|
||||
{
|
||||
powerShell.Runspace = runspace;
|
||||
|
||||
powerShell.AddScript("{1}.Invoke()");
|
||||
powerShell.Invoke();
|
||||
}
|
||||
|
||||
runspace.Close();
|
||||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void TestRunspaceWithPowerShellAndInitialSessionState()
|
||||
{
|
||||
InitialSessionState iss = InitialSessionState.CreateDefault2();
|
||||
|
||||
// NOTE: instantiate custom host myHost for the next line to capture stdout and stderr output
|
||||
// in addition to just the PSObjects
|
||||
using (Runspace runspace = RunspaceFactory.CreateRunspace(/*myHost,*/iss))
|
||||
{
|
||||
runspace.Open();
|
||||
using (PowerShell powerShell = PowerShell.Create())
|
||||
{
|
||||
powerShell.Runspace = runspace;
|
||||
|
||||
powerShell.AddScript(script);
|
||||
|
||||
int objCount = 0;
|
||||
foreach (var result in powerShell.Invoke())
|
||||
{
|
||||
// this is how an object would be captured here and looked at,
|
||||
// each result is a PSObject with the data from the pipeline
|
||||
++objCount;
|
||||
Assert.NotNull(result);
|
||||
}
|
||||
Assert.Equal(count, objCount);
|
||||
powerShell.Dispose();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,35 @@
|
||||
using Xunit;
|
||||
using System;
|
||||
using System.Management.Automation;
|
||||
|
||||
namespace PSTests
|
||||
{
|
||||
public static class SecuritySupportTests
|
||||
{
|
||||
[Fact]
|
||||
public static void TestScanContent()
|
||||
{
|
||||
Assert.Equal(AmsiUtils.ScanContent("", ""), AmsiUtils.AmsiNativeMethods.AMSI_RESULT.AMSI_RESULT_NOT_DETECTED);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public static void TestCurrentDomain_ProcessExit()
|
||||
{
|
||||
Assert.Throws<PlatformNotSupportedException>(delegate {
|
||||
AmsiUtils.CurrentDomain_ProcessExit(null, EventArgs.Empty);
|
||||
});
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public static void TestCloseSession()
|
||||
{
|
||||
AmsiUtils.CloseSession();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public static void TestUninitialize()
|
||||
{
|
||||
AmsiUtils.Uninitialize();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
using Xunit;
|
||||
using System;
|
||||
using System.Management.Automation;
|
||||
|
||||
namespace PSTests
|
||||
{
|
||||
public static class UtilsTests
|
||||
{
|
||||
[Fact]
|
||||
public static void TestIsWinPEHost()
|
||||
{
|
||||
Assert.False(Utils.IsWinPEHost());
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user