diff --git a/scripts/Makefile b/scripts/Makefile index 174a68bd0e..00d0874c3d 100644 --- a/scripts/Makefile +++ b/scripts/Makefile @@ -41,16 +41,16 @@ MI_REF_ASSEMBLY=-r:$(MONAD_EXT)/PS/PS_refs_modil/microsoft.management.infrastruc MI_ASSEMBLY=dotnetlibs/Microsoft.Management.Infrastructure.dll MI_REF=-r:$(MI_ASSEMBLY) -PRODUCT_BASE_REFS=${COREREF} -PRODUCT_MI_REFS=${COREREF} ${MI_NATIVE_REF} -PRODUCT_PS_REFS=${COREREF} ${MI_REF} -r:dotnetlibs/$(ASSEMBLY_LOAD_CONTEXT_TARGET) -PRODUCT_COMMANDS_REFS=${COREREF} -r:dotnetlibs/System.Management.Automation.dll +PRODUCT_BASE_REFS=$(COREREF) +PRODUCT_MI_REFS=$(COREREF) $(MI_NATIVE_REF) +PRODUCT_PS_REFS=$(COREREF) $(MI_REF) -r:dotnetlibs/$(ASSEMBLY_LOAD_CONTEXT_TARGET) +PRODUCT_COMMANDS_REFS=$(COREREF) -r:dotnetlibs/System.Management.Automation.dll MCSOPTS_BASE=-unsafe -nostdlib -noconfig -define:CORECLR -define:_CORECLR -MCSOPTS_MI=${MCSOPTS_BASE} -target:library -MCSOPTS_LIB=${MCSOPTS_BASE} -target:library -MCSOPTS_PS=${STRING_RESOURCES_ORIG} ${MCSOPTS_BASE} -target:library -SRCS_ALL=${STRING_RESOURCES} ${SRCS} +MCSOPTS_MI=$(MCSOPTS_BASE) -target:library +MCSOPTS_LIB=$(MCSOPTS_BASE) -target:library +MCSOPTS_PS=$(STRING_RESOURCES_ORIG) $(MCSOPTS_BASE) -target:library +SRCS_ALL=$(STRING_RESOURCES) $(SRCS) # compilers # - Roslyn's csc is used for all the PS code @@ -63,28 +63,28 @@ RUN_TARGETS=$(POWERSHELL_RUN_TARGETS) dotnetlibs/Microsoft.PowerShell.Commands.M all: dotnetlibs/System.Management.Automation.dll $(RUN_TARGETS) dotnetlibs/$(ASSEMBLY_LOAD_CONTEXT_TARGET) # this is the build rule for SMA.dll -dotnetlibs/System.Management.Automation.dll: ${SYS_AUTO_SRCS} dotnetlibs/Microsoft.Management.Infrastructure.dll ../src/assembly-info/System.Management.Automation.assembly-info.cs dotnetlibs/$(ASSEMBLY_LOAD_CONTEXT_TARGET) $(PLATFORM_SRCS) ${SYS_AUTO_RES_SRCS} ${SYS_AUTO_RES_CS_SRCS} - $(CSC) -out:$@ ${MCSOPTS_LIB} ${PRODUCT_PS_REFS} ${SYS_AUTO_SRCS} ${SYS_AUTO_RES_REF} ${SYS_AUTO_RES_CS_SRCS} $(PLATFORM_SRCS) ../src/assembly-info/System.Management.Automation.assembly-info.cs +dotnetlibs/System.Management.Automation.dll: $(SYS_AUTO_SRCS) dotnetlibs/Microsoft.Management.Infrastructure.dll ../src/assembly-info/System.Management.Automation.assembly-info.cs dotnetlibs/$(ASSEMBLY_LOAD_CONTEXT_TARGET) $(PLATFORM_SRCS) $(SYS_AUTO_RES_SRCS) $(SYS_AUTO_RES_CS_SRCS) + $(CSC) -out:$@ $(MCSOPTS_LIB) $(PRODUCT_PS_REFS) $(SYS_AUTO_SRCS) $(SYS_AUTO_RES_REF) $(SYS_AUTO_RES_CS_SRCS) $(PLATFORM_SRCS) ../src/assembly-info/System.Management.Automation.assembly-info.cs # this is the build rule for MMI.dll -dotnetlibs/Microsoft.Management.Infrastructure.dll: ${MAN_INFRA_SRCS} dotnetlibs/Microsoft.Management.Infrastructure.Native.dll ../src/assembly-info/Microsoft.Management.Infrastructure.assembly-info.cs $(MAN_INFRA_RES_SRCS) $(MAN_INFRA_RES_CS_SRCS) +dotnetlibs/Microsoft.Management.Infrastructure.dll: $(MAN_INFRA_SRCS) dotnetlibs/Microsoft.Management.Infrastructure.Native.dll ../src/assembly-info/Microsoft.Management.Infrastructure.assembly-info.cs $(MAN_INFRA_RES_SRCS) $(MAN_INFRA_RES_CS_SRCS) $(CSC) -out:$@ $(MCSOPTS_MI) $(PRODUCT_MI_REFS) $(MAN_INFRA_SRCS) $(MAN_INFRA_RES_REF) $(MAN_INFRA_RES_CS_SRCS) ../src/assembly-info/Microsoft.Management.Infrastructure.assembly-info.cs # Commands -dotnetlibs/Microsoft.PowerShell.Commands.Management.dll: ${COMMANDS_MANAGEMENT_SRCS} dotnetlibs/System.Management.Automation.dll ${COMMANDS_MANAGEMENT_RES_SRCS} ${COMMANDS_MANAGEMENT_RES_CS_SRCS} $(MI_ASSEMBLY) - $(CSC) -out:$@ ${MCSOPTS_LIB} ${PRODUCT_COMMANDS_REFS} ${COMMANDS_MANAGEMENT_SRCS} ${COMMANDS_MANAGEMENT_RES_CS_SRCS} $(COMMANDS_MANAGEMENT_RES_REF) $(MI_REF) +dotnetlibs/Microsoft.PowerShell.Commands.Management.dll: $(COMMANDS_MANAGEMENT_SRCS) dotnetlibs/System.Management.Automation.dll dotnetlibs/Microsoft.PowerShell.Security.dll $(COMMANDS_MANAGEMENT_RES_SRCS) $(COMMANDS_MANAGEMENT_RES_CS_SRCS) $(MI_ASSEMBLY) + $(CSC) -out:$@ $(MCSOPTS_LIB) $(PRODUCT_COMMANDS_REFS) $(COMMANDS_MANAGEMENT_SRCS) $(COMMANDS_MANAGEMENT_RES_CS_SRCS) $(COMMANDS_MANAGEMENT_RES_REF) $(MI_REF) -r:dotnetlibs/Microsoft.PowerShell.Security.dll -dotnetlibs/Microsoft.PowerShell.Commands.Utility.dll: ${COMMANDS_UTILITY_SRCS} dotnetlibs/System.Management.Automation.dll ${COMMANDS_UTILITY_RES_SRCS} ${COMMANDS_UTILITY_RES_CS_SRCS} - $(CSC) -out:$@ ${MCSOPTS_LIB} ${PRODUCT_COMMANDS_REFS} ${COMMANDS_UTILITY_SRCS} ${COMMANDS_UTILITY_RES_CS_SRCS} $(COMMANDS_UTILITY_RES_REF) +dotnetlibs/Microsoft.PowerShell.Commands.Utility.dll: $(COMMANDS_UTILITY_SRCS) dotnetlibs/System.Management.Automation.dll $(COMMANDS_UTILITY_RES_SRCS) $(COMMANDS_UTILITY_RES_CS_SRCS) + $(CSC) -out:$@ $(MCSOPTS_LIB) $(PRODUCT_COMMANDS_REFS) $(COMMANDS_UTILITY_SRCS) $(COMMANDS_UTILITY_RES_CS_SRCS) $(COMMANDS_UTILITY_RES_REF) -dotnetlibs/Microsoft.PowerShell.Security.dll: ${SECURITY_SRCS} dotnetlibs/System.Management.Automation.dll ${SECURITY_RES_SRCS} ${SECURITY_RES_CS_SRCS} - $(CSC) -out:$@ ${MCSOPTS_LIB} ${PRODUCT_COMMANDS_REFS} ${SECURITY_SRCS} ${SECURITY_RES_CS_SRCS} $(SECURITY_RES_REF) +dotnetlibs/Microsoft.PowerShell.Security.dll: $(SECURITY_SRCS) $(SECURITY_RES_SRCS) $(SECURITY_RES_CS_SRCS) + $(CSC) -out:$@ $(MCSOPTS_LIB) $(PRODUCT_COMMANDS_REFS) $(SECURITY_SRCS) $(SECURITY_RES_CS_SRCS) $(SECURITY_RES_REF) # assembly load context dotnetlibs/$(ASSEMBLY_LOAD_CONTEXT_TARGET): $(ASSEMBLY_LOAD_CONTEXT_SRCS) - $(CSC) -out:$@ ${MCSOPTS_LIB} ${PRODUCT_BASE_REFS} $(ASSEMBLY_LOAD_CONTEXT_SRCS) + $(CSC) -out:$@ $(MCSOPTS_LIB) $(PRODUCT_BASE_REFS) $(ASSEMBLY_LOAD_CONTEXT_SRCS) # this will copy whatever the first version of the dll in the globber is buildtemp/System.Reflection.Metadata.dll: buildtemp/System.Reflection.Metadata.*/lib/portable-net45+win8/System.Reflection.Metadata.dll @@ -101,7 +101,7 @@ dotnetlibs/Microsoft.Management.Infrastructure.Native.dll: ../src/stubs/Microsof MPATH=/usr/lib/mono/4.5/Facades buildtemp/TypeCatalogGen.exe: ../src/monad/monad/nttargets/assemblies/core/PSAssemblyLoadContext/TypeCatalogGen/TypeCatalogGen.cs buildtemp/System.Reflection.Metadata.dll buildtemp/System.Collections.Immutable.dll - $(MCS) -out:$@ -target:exe ${NUGETREF} -pkg:dotnet -r:${MPATH}/System.Runtime.dll -r:${MPATH}/System.Reflection.Primitives.dll -r:${MPATH}/System.IO.dll $< + $(MCS) -out:$@ -target:exe $(NUGETREF) -pkg:dotnet -r:$(MPATH)/System.Runtime.dll -r:$(MPATH)/System.Reflection.Primitives.dll -r:$(MPATH)/System.IO.dll $< # generate the Core PS type catalog # this comes from: ../src/monad/monad/nttargets/assemblies/core/PSAssemblyLoadContext/makefile.inc @@ -194,7 +194,7 @@ native-tests: dotnetlibs/monad_native xunit-tests: $(RUN_TARGETS) internal-prepare-exec_env internal-prepare-release-clr $(addprefix $(TESTRUN_FOLDER)/, ps_test.dll xunit.console.netcore.exe xunit.runner.utility.dll xunit.abstractions.dll xunit.execution.dll) # execute the xUnit runner, with XML output - cd exec_env/app_base && PSMODULEPATH=$(shell pwd)/exec_env/app_base/Modules LD_LIBRARY_PATH=../coreclr:. ../coreclr/corerun xunit.console.netcore.exe ps_test.dll -xml ../../xunittests.xml + exec_env/app_base/runps-test.sh ps_test.dll -xml ../../xunittests.xml pester-tests: $(RUN_TARGETS) internal-prepare-exec_env internal-prepare-release-clr # execute the Pester tests, which needs a TEMP environment variable to be set diff --git a/scripts/runps-test.sh b/scripts/runps-test.sh new file mode 100755 index 0000000000..5196f708be --- /dev/null +++ b/scripts/runps-test.sh @@ -0,0 +1,7 @@ +#!/bin/bash + +CWD=$(pwd) +SCRIPTDIR=$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd) + +cd "$SCRIPTDIR" +PSMODULEPATH="$SCRIPTDIR/Modules" LD_LIBRARY_PATH="$SCRIPTDIR" ./host_cmdline -c ../coreclr -alc Microsoft.PowerShell.CoreCLR.AssemblyLoadContext.dll -tpa xunit.console.netcore.exe xunit.console.netcore.exe "$@" diff --git a/scripts/tests.mk b/scripts/tests.mk index 8a7c312a2a..4f9963828a 100644 --- a/scripts/tests.mk +++ b/scripts/tests.mk @@ -6,4 +6,4 @@ $(TESTRUN_FOLDER)/xunit%: $(MONAD_EXT)/xunit/xunit% cp -f $^ $@ $(TESTRUN_FOLDER)/ps_test.dll: $(TEST_SRCS) $(addprefix $(TESTRUN_FOLDER)/, xunit.core.dll xunit.assert.dll) $(addprefix dotnetlibs/, System.Management.Automation.dll Microsoft.PowerShell.Commands.Management.dll $(ASSEMBLY_LOAD_CONTEXT_TARGET)) - $(CSC) -out:$@ -noconfig -nostdlib -target:library $(addprefix -r:$(TESTRUN_FOLDER)/, xunit.core.dll xunit.assert.dll) $(addprefix -r:dotnetlibs/, System.Management.Automation.dll $(ASSEMBLY_LOAD_CONTEXT_TARGET)) ${COREREF} $(TEST_SRCS) + $(CSC) -out:$@ -noconfig -nostdlib -target:library $(addprefix -r:$(TESTRUN_FOLDER)/, xunit.core.dll xunit.assert.dll) $(addprefix -r:dotnetlibs/, System.Management.Automation.dll $(ASSEMBLY_LOAD_CONTEXT_TARGET)) $(COREREF) $(TEST_SRCS) diff --git a/src/monad b/src/monad index d541b07343..9aecfb12f6 160000 --- a/src/monad +++ b/src/monad @@ -1 +1 @@ -Subproject commit d541b073435e55fd96771697e5549cc9a188fa54 +Subproject commit 9aecfb12f69d91908812751e1b69d7c5c8098e07 diff --git a/src/pester-tests/Test-Test-Path.Tests.ps1 b/src/pester-tests/Test-Test-Path.Tests.ps1 new file mode 100644 index 0000000000..83c18d1f4d --- /dev/null +++ b/src/pester-tests/Test-Test-Path.Tests.ps1 @@ -0,0 +1,114 @@ +Describe "Test-Test-Path" { + $testdirectory = "/usr/bin" + $testfilename = "vi" # use /usr/bin/vi since that's bundled with all linux + $testfile = $testdirectory + "/" + $testfilename + It "Should be called on an existing path without error" { + { Test-Path $testdirectory } | Should Not Throw + { Test-Path -Path $testdirectory } | Should Not Throw + { Test-Path -LiteralPath $testdirectory } | Should Not Throw + } + + It "Should allow piping objects to it" { + { $testdirectory | Test-Path } | Should Not Throw + + $testdirectory | Test-Path | Should Be $true + "/usr/bin/totallyFakeDirectory" | Test-Path | Should Be $false + } + + It "Should return a boolean data type" { + { Test-Path -Path $testdirectory } | Should Be $true + } + + It "Should be called on a nonexistant path without error" { + { Test-Path -Path "aNonexistant/path/that/should/error" } | Should Not Throw + } + + It "Should return false for a nonexistant path" { + Test-Path -Path "aNonexistant/path/that/should/error" | Should Be $false + } + + It "Should return true for an existing path" { + Test-Path -Path $testdirectory | Should Be $true + } + + It "Should be able to accept a regular expression" { + { Test-Path -Path "/u*" } | Should Not Throw + { Test-Path -Path "/u[a-z]r" } | Should Not Throw + } + + It "Should be able to return the correct result when a regular expression is used" { + Test-Path -Path "/u*" | Should Be $true + Test-Path -Path "/u[a-z]*" | Should Be $true + + Test-Path -Path "/aoeu*" | Should Be $false + Test-Path -Path "/u[A-Z]" | Should Be $false + } + + It "Should return false when the Leaf pathtype is used on a directory" { + Test-Path -Path $testdirectory -PathType Leaf | Should Be $false + } + + It "Should return true when the Leaf pathtype is used on an existing endpoint" { + Test-Path -Path $testfile -PathType Leaf | Should Be $true + } + + It "Should return false when the Leaf pathtype is used on a nonexistant file" { + Test-Path -Path "aoeu" -PathType Leaf | Should Be $false + } + + It "Should return true when the Leaf pathtype is used on a file using the Type alias instead of PathType" { + Test-Path -Path $testfile -Type Leaf | Should Be $true + } + + It "Should be able to search multiple regular expressions using the include switch" { + Test-Path -Path "/usr/bin/*" -Include vi* | Should Be $true + } + + It "Should be able to exclude a regular expression using the exclude switch" { + Test-Path -Path "/usr/bin/" -Exclude vi* | Should Be $true + } + + It "Should be able to exclude multiple regular expressions using the exclude switch" { + # tests whether there's any files in the /usr directory that don't start with 'd' or 'g' + Test-Path -Path "/usr" -Exclude d*, g* | Should Be $true + } + + It "Should return true if the syntax of the path is correct when using the IsValid switch" { + Test-Path -Path /this/is/a/valid/path -IsValid | Should Be $true + } + + It "Should return false if the syntax of the path is incorrect when using the IsValid switch" { + Test-Path -Path C:/usr/bin -IsValid | Should Be $false + } + + It "Should return true on paths containing spaces when the path is surrounded in quotes" { + Test-Path -Path "/totally a valid/path" -IsValid | Should Be $true + } + + It "Should throw on paths containing spaces when the path is not surrounded in quotes" { + { Test-Path -Path /a path/without quotes/around/it -IsValid } | Should Throw + } + + It "Should return true if a directory leads or trails with a space when surrounded by quotes" { + Test-Path -Path "/a path / with/funkyspaces" -IsValid | Should Be $true + } + + It "Should return true on a valid path when the LiteralPath switch is used" { + Test-Path -LiteralPath "/usr/bin" | Should Be $true + } + + It "Should return false if regular expressions are used with the LiteralPath switch" { + Test-Path -LiteralPath /*sr/bin | Should Be $false + Test-Path -LiteralPath /[usth]sr/bin | Should Be $false + } + + It "Should return false if regular expressions are used with the LiteralPath alias PSPath switch" { + Test-Path -PSPath /*sr/bin | Should Be $false + Test-Path -PSPath /[aoeu]sr/bin | Should Be $false + } + + It "Should return true if used on components other than filesystem objects" { + Test-Path Alias:\gci | Should Be $true + Test-Path Env:\HOSTNAME | Should Be $true + } +} diff --git a/src/ps_test/test_CorePsPlatform.cs b/src/ps_test/test_CorePsPlatform.cs index 57e58bc32f..e11826091e 100644 --- a/src/ps_test/test_CorePsPlatform.cs +++ b/src/ps_test/test_CorePsPlatform.cs @@ -1,5 +1,6 @@ using Xunit; using System; +using System.IO; using System.Diagnostics; using System.Management.Automation; @@ -90,5 +91,48 @@ namespace PSTests } + + [Fact] + public static void TestExistantFileIsHardLink() + { + string path = @"/tmp/MyTest"; + if (!File.Exists(path)) + { + File.Create(path); + } + // Create a file to write to using StreamWriter. + // convert string to stream. On Windows, this appears to be handled, but on *nix, + // we apparently need to convert to UTF8. + byte[] byteArray = System.Text.Encoding.UTF8.GetBytes(path); + MemoryStream stream = new MemoryStream(byteArray); + + // Convert `path` string to FileSystemInfo data type. And now, it should return true + FileSystemInfo fd = new FileInfo(path); + Assert.True(Platform.NonWindowsIsHardLink(fd)); + } + + [Fact] + public static void TestDirectoryIsHardLink() + { + string path = @"/tmp"; + + // 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 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)); + } } } diff --git a/src/ps_test/test_Runspace.cs b/src/ps_test/test_Runspace.cs new file mode 100644 index 0000000000..f9b4745eb9 --- /dev/null +++ b/src/ps_test/test_Runspace.cs @@ -0,0 +1,48 @@ +using Xunit; +using System; +using System.Management.Automation; +using System.Management.Automation.Runspaces; + +namespace PSTests +{ + public static class RunspaceTests + { + [Fact] + public static void TestMethod() + { + 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 rs = RunspaceFactory.CreateRunspace(/*myHost,*/iss)) + { + rs.Open(); + using (PowerShell ps = PowerShell.Create()) + { + ps.Runspace = rs; + + string script = "get-process | select-object -first 3"; + ps.AddScript(script); + + // IMPORTANT NOTE: do not call AddCommand("out-host") here or + // MergeMyResults, otherwise Invoke will not return any objects + + var results = ps.Invoke(); + + // check that there are 3 captured objects + int objCount = 0; + foreach (var result in results) + { + // 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(3,objCount); + ps.Dispose(); + } + } + } + } +} + diff --git a/src/ps_test/test_SecuritySupport.cs b/src/ps_test/test_SecuritySupport.cs index 4c3b4b9b17..962dc66707 100644 --- a/src/ps_test/test_SecuritySupport.cs +++ b/src/ps_test/test_SecuritySupport.cs @@ -15,7 +15,9 @@ namespace PSTests [Fact] public static void TestCurrentDomain_ProcessExit() { - AmsiUtils.CurrentDomain_ProcessExit(null, EventArgs.Empty); + Assert.Throws(delegate { + AmsiUtils.CurrentDomain_ProcessExit(null, EventArgs.Empty); + }); } [Fact]