2020-03-24 11:08:37 -07:00
|
|
|
// Copyright (c) Microsoft Corporation.
|
2018-02-13 09:23:53 -08:00
|
|
|
// Licensed under the MIT License.
|
2018-12-18 21:11:21 +05:00
|
|
|
|
2015-08-06 16:24:18 +02:00
|
|
|
using System;
|
2016-03-24 01:11:11 -07:00
|
|
|
using System.Collections.Generic;
|
2016-03-17 01:34:48 -07:00
|
|
|
using System.Collections.ObjectModel;
|
2016-03-22 20:13:57 -07:00
|
|
|
using System.Globalization;
|
2016-03-17 01:34:48 -07:00
|
|
|
using System.IO;
|
2016-03-22 20:13:57 -07:00
|
|
|
using System.Linq;
|
2015-08-06 16:24:18 +02:00
|
|
|
using System.Management.Automation;
|
2016-03-22 20:13:57 -07:00
|
|
|
using System.Management.Automation.Host;
|
|
|
|
|
using System.Management.Automation.Internal;
|
|
|
|
|
using System.Management.Automation.Internal.Host;
|
2016-03-17 01:34:48 -07:00
|
|
|
using System.Management.Automation.Provider;
|
2016-03-22 20:13:57 -07:00
|
|
|
using System.Management.Automation.Runspaces;
|
2018-12-18 21:11:21 +05:00
|
|
|
using System.Reflection;
|
2019-03-12 23:49:16 +01:00
|
|
|
using System.Reflection.Metadata;
|
|
|
|
|
using Microsoft.CodeAnalysis.CSharp.Syntax;
|
2016-03-22 20:13:57 -07:00
|
|
|
using Microsoft.PowerShell;
|
2015-08-06 16:24:18 +02:00
|
|
|
using Microsoft.PowerShell.Commands;
|
2018-12-18 21:11:21 +05:00
|
|
|
using Xunit;
|
2015-08-06 16:24:18 +02:00
|
|
|
|
2018-01-03 12:43:28 -08:00
|
|
|
namespace PSTests.Parallel
|
2015-08-06 16:24:18 +02:00
|
|
|
{
|
2018-12-18 21:11:21 +05:00
|
|
|
public class FileSystemProviderTests : IDisposable
|
2015-08-06 16:24:18 +02:00
|
|
|
{
|
2020-10-30 07:20:06 +00:00
|
|
|
private readonly string testPath;
|
|
|
|
|
private readonly string testContent;
|
2018-12-18 21:11:21 +05:00
|
|
|
|
2017-04-14 00:45:46 +04:00
|
|
|
public FileSystemProviderTests()
|
|
|
|
|
{
|
2018-05-31 11:03:14 +05:00
|
|
|
testPath = Path.GetTempFileName();
|
2017-04-14 00:45:46 +04:00
|
|
|
testContent = "test content!";
|
2018-12-18 21:11:21 +05:00
|
|
|
if (File.Exists(testPath))
|
|
|
|
|
{
|
|
|
|
|
File.Delete(testPath);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
File.AppendAllText(testPath, testContent);
|
2017-04-14 00:45:46 +04:00
|
|
|
}
|
2018-12-18 21:11:21 +05:00
|
|
|
|
2020-11-24 04:29:15 +00:00
|
|
|
public void Dispose()
|
2017-04-14 00:45:46 +04:00
|
|
|
{
|
2020-11-24 04:29:15 +00:00
|
|
|
Dispose(true);
|
|
|
|
|
GC.SuppressFinalize(this);
|
2025-10-16 08:58:28 +01:00
|
|
|
}
|
2020-11-24 04:29:15 +00:00
|
|
|
|
|
|
|
|
protected virtual void Dispose(bool disposing)
|
|
|
|
|
{
|
|
|
|
|
if (disposing)
|
|
|
|
|
{
|
|
|
|
|
File.Delete(testPath);
|
|
|
|
|
}
|
2017-04-14 00:45:46 +04:00
|
|
|
}
|
2017-01-16 13:31:14 -08:00
|
|
|
|
2020-12-09 19:04:23 +00:00
|
|
|
private static ExecutionContext GetExecutionContext()
|
2017-04-14 00:45:46 +04:00
|
|
|
{
|
|
|
|
|
CultureInfo currentCulture = CultureInfo.CurrentCulture;
|
2018-12-18 21:11:21 +05:00
|
|
|
PSHost hostInterface = new DefaultHost(currentCulture, currentCulture);
|
2016-03-22 20:13:57 -07:00
|
|
|
InitialSessionState iss = InitialSessionState.CreateDefault2();
|
2017-11-29 18:38:04 +01:00
|
|
|
AutomationEngine engine = new AutomationEngine(hostInterface, iss);
|
2016-03-22 20:13:57 -07:00
|
|
|
ExecutionContext executionContext = new ExecutionContext(engine, hostInterface, iss);
|
2017-04-14 00:45:46 +04:00
|
|
|
return executionContext;
|
|
|
|
|
}
|
2018-12-18 21:11:21 +05:00
|
|
|
|
2020-12-09 19:04:23 +00:00
|
|
|
private static ProviderInfo GetProvider()
|
2016-03-24 01:11:11 -07:00
|
|
|
{
|
|
|
|
|
ExecutionContext executionContext = GetExecutionContext();
|
2016-03-22 20:13:57 -07:00
|
|
|
SessionStateInternal sessionState = new SessionStateInternal(executionContext);
|
2017-01-16 13:31:14 -08:00
|
|
|
|
2018-12-18 21:11:21 +05:00
|
|
|
SessionStateProviderEntry providerEntry = new SessionStateProviderEntry("FileSystem", typeof(FileSystemProvider), null);
|
2017-04-14 00:45:46 +04:00
|
|
|
sessionState.AddSessionStateEntry(providerEntry);
|
2016-03-22 20:13:57 -07:00
|
|
|
ProviderInfo matchingProvider = sessionState.ProviderList.ToList()[0];
|
|
|
|
|
|
|
|
|
|
return matchingProvider;
|
|
|
|
|
}
|
2017-01-16 13:31:14 -08:00
|
|
|
|
2015-08-06 16:24:18 +02:00
|
|
|
[Fact]
|
2016-03-24 01:11:11 -07:00
|
|
|
public void TestCreateJunctionFails()
|
2015-08-06 16:24:18 +02:00
|
|
|
{
|
2018-12-18 21:11:21 +05:00
|
|
|
if (!Platform.IsWindows)
|
2017-11-30 14:44:41 -08:00
|
|
|
{
|
2018-06-13 00:30:10 +05:00
|
|
|
Assert.False(InternalSymbolicLinkLinkCodeMethods.CreateJunction(string.Empty, string.Empty));
|
2017-11-30 14:44:41 -08:00
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2023-02-27 23:42:07 +01:00
|
|
|
Assert.Throws<System.ArgumentException>(delegate { InternalSymbolicLinkLinkCodeMethods.CreateJunction(string.Empty, string.Empty); });
|
2017-11-30 14:44:41 -08:00
|
|
|
}
|
2016-03-17 01:34:48 -07:00
|
|
|
}
|
2017-01-16 13:31:14 -08:00
|
|
|
|
2017-04-14 00:45:46 +04:00
|
|
|
[Fact]
|
2016-03-24 01:11:11 -07:00
|
|
|
public void TestGetHelpMaml()
|
2016-03-17 01:34:48 -07:00
|
|
|
{
|
2017-04-14 00:45:46 +04:00
|
|
|
FileSystemProvider fileSystemProvider = new FileSystemProvider();
|
2018-12-18 21:11:21 +05:00
|
|
|
Assert.Equal(fileSystemProvider.GetHelpMaml(string.Empty, string.Empty), string.Empty);
|
|
|
|
|
Assert.Equal(fileSystemProvider.GetHelpMaml("helpItemName", string.Empty), string.Empty);
|
|
|
|
|
Assert.Equal(fileSystemProvider.GetHelpMaml(string.Empty, "path"), string.Empty);
|
2016-03-17 01:34:48 -07:00
|
|
|
}
|
2017-01-16 13:31:14 -08:00
|
|
|
|
2017-04-14 00:45:46 +04:00
|
|
|
[Fact]
|
2016-03-24 01:11:11 -07:00
|
|
|
public void TestMode()
|
2016-03-17 01:34:48 -07:00
|
|
|
{
|
2018-12-18 21:11:21 +05:00
|
|
|
Assert.Equal(FileSystemProvider.Mode(null), string.Empty);
|
2017-11-30 14:44:41 -08:00
|
|
|
FileSystemInfo directoryObject = null;
|
|
|
|
|
FileSystemInfo fileObject = null;
|
|
|
|
|
FileSystemInfo executableObject = null;
|
|
|
|
|
|
2018-12-18 21:11:21 +05:00
|
|
|
if (!Platform.IsWindows)
|
2017-11-30 14:44:41 -08:00
|
|
|
{
|
|
|
|
|
directoryObject = new DirectoryInfo(@"/");
|
|
|
|
|
fileObject = new FileInfo(@"/etc/hosts");
|
|
|
|
|
executableObject = new FileInfo(@"/bin/echo");
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
directoryObject = new DirectoryInfo(System.Environment.CurrentDirectory);
|
|
|
|
|
fileObject = new FileInfo(System.Reflection.Assembly.GetEntryAssembly().Location);
|
2021-06-25 04:56:55 +01:00
|
|
|
executableObject = new FileInfo(Environment.ProcessPath);
|
2017-11-30 14:44:41 -08:00
|
|
|
}
|
|
|
|
|
|
2019-03-12 23:49:16 +01:00
|
|
|
Assert.Equal("d----", FileSystemProvider.Mode(PSObject.AsPSObject(directoryObject)).Replace("r", "-"));
|
|
|
|
|
Assert.Equal("-----", FileSystemProvider.Mode(PSObject.AsPSObject(fileObject)).Replace("r", "-").Replace("a", "-"));
|
|
|
|
|
Assert.Equal("-----", FileSystemProvider.Mode(PSObject.AsPSObject(executableObject)).Replace("r", "-").Replace("a", "-"));
|
2016-03-17 01:34:48 -07:00
|
|
|
}
|
2017-01-16 13:31:14 -08:00
|
|
|
|
2017-04-14 00:45:46 +04:00
|
|
|
[Fact]
|
2016-03-24 01:11:11 -07:00
|
|
|
public void TestGetProperty()
|
2016-03-17 01:34:48 -07:00
|
|
|
{
|
2017-04-14 00:45:46 +04:00
|
|
|
FileSystemProvider fileSystemProvider = new FileSystemProvider();
|
|
|
|
|
ProviderInfo providerInfoToSet = GetProvider();
|
|
|
|
|
fileSystemProvider.SetProviderInformation(providerInfoToSet);
|
|
|
|
|
fileSystemProvider.Context = new CmdletProviderContext(GetExecutionContext());
|
2018-12-18 21:11:21 +05:00
|
|
|
PSObject pso = new PSObject();
|
|
|
|
|
pso.AddOrSetProperty("IsReadOnly", false);
|
2017-04-14 00:45:46 +04:00
|
|
|
fileSystemProvider.SetProperty(testPath, pso);
|
2020-01-31 22:56:22 +00:00
|
|
|
fileSystemProvider.GetProperty(testPath, new Collection<string>() { "IsReadOnly" });
|
2017-04-14 00:45:46 +04:00
|
|
|
FileInfo fileSystemObject1 = new FileInfo(testPath);
|
2018-12-18 21:11:21 +05:00
|
|
|
PSObject psobject1 = PSObject.AsPSObject(fileSystemObject1);
|
2019-03-12 23:49:16 +01:00
|
|
|
PSPropertyInfo property = psobject1.Properties["IsReadOnly"];
|
|
|
|
|
Assert.False((bool)property.Value);
|
2016-03-17 01:34:48 -07:00
|
|
|
}
|
2017-01-16 13:31:14 -08:00
|
|
|
|
2017-04-14 00:45:46 +04:00
|
|
|
[Fact]
|
2016-03-24 01:11:11 -07:00
|
|
|
public void TestSetProperty()
|
2016-03-17 01:34:48 -07:00
|
|
|
{
|
2017-04-14 00:45:46 +04:00
|
|
|
FileSystemProvider fileSystemProvider = new FileSystemProvider();
|
|
|
|
|
ProviderInfo providerInfoToSet = GetProvider();
|
|
|
|
|
fileSystemProvider.SetProviderInformation(providerInfoToSet);
|
|
|
|
|
fileSystemProvider.Context = new CmdletProviderContext(GetExecutionContext());
|
2020-01-31 22:56:22 +00:00
|
|
|
fileSystemProvider.GetProperty(testPath, new Collection<string>() { "Name" });
|
2017-04-14 00:45:46 +04:00
|
|
|
FileInfo fileSystemObject1 = new FileInfo(testPath);
|
2018-12-18 21:11:21 +05:00
|
|
|
PSObject psobject1 = PSObject.AsPSObject(fileSystemObject1);
|
2019-03-12 23:49:16 +01:00
|
|
|
PSPropertyInfo property = psobject1.Properties["FullName"];
|
|
|
|
|
|
|
|
|
|
Assert.Equal(testPath, property.Value);
|
2016-03-17 01:34:48 -07:00
|
|
|
}
|
2017-01-16 13:31:14 -08:00
|
|
|
|
2017-04-14 00:45:46 +04:00
|
|
|
[Fact]
|
2016-03-24 01:11:11 -07:00
|
|
|
public void TestClearProperty()
|
2016-03-17 01:34:48 -07:00
|
|
|
{
|
2017-04-14 00:45:46 +04:00
|
|
|
FileSystemProvider fileSystemProvider = new FileSystemProvider();
|
|
|
|
|
ProviderInfo providerInfoToSet = GetProvider();
|
|
|
|
|
fileSystemProvider.SetProviderInformation(providerInfoToSet);
|
|
|
|
|
fileSystemProvider.Context = new CmdletProviderContext(GetExecutionContext());
|
2020-01-31 22:56:22 +00:00
|
|
|
fileSystemProvider.ClearProperty(testPath, new Collection<string>() { "Attributes" });
|
2016-03-17 01:34:48 -07:00
|
|
|
}
|
2017-01-16 13:31:14 -08:00
|
|
|
|
2017-04-14 00:45:46 +04:00
|
|
|
[Fact]
|
2016-03-24 01:11:11 -07:00
|
|
|
public void TestGetContentReader()
|
2016-03-17 01:34:48 -07:00
|
|
|
{
|
2017-04-14 00:45:46 +04:00
|
|
|
FileSystemProvider fileSystemProvider = new FileSystemProvider();
|
|
|
|
|
ProviderInfo providerInfoToSet = GetProvider();
|
|
|
|
|
fileSystemProvider.SetProviderInformation(providerInfoToSet);
|
|
|
|
|
fileSystemProvider.Context = new CmdletProviderContext(GetExecutionContext());
|
2017-01-16 13:31:14 -08:00
|
|
|
|
2017-04-14 00:45:46 +04:00
|
|
|
IContentReader contentReader = fileSystemProvider.GetContentReader(testPath);
|
2018-12-18 21:11:21 +05:00
|
|
|
Assert.Equal(contentReader.Read(1)[0], testContent);
|
2017-04-14 00:45:46 +04:00
|
|
|
contentReader.Close();
|
2016-03-17 01:34:48 -07:00
|
|
|
}
|
2017-01-16 13:31:14 -08:00
|
|
|
|
2017-04-14 00:45:46 +04:00
|
|
|
[Fact]
|
2016-03-24 01:11:11 -07:00
|
|
|
public void TestGetContentWriter()
|
2016-03-17 01:34:48 -07:00
|
|
|
{
|
2017-04-14 00:45:46 +04:00
|
|
|
FileSystemProvider fileSystemProvider = new FileSystemProvider();
|
|
|
|
|
ProviderInfo providerInfoToSet = GetProvider();
|
|
|
|
|
fileSystemProvider.SetProviderInformation(providerInfoToSet);
|
|
|
|
|
fileSystemProvider.Context = new CmdletProviderContext(GetExecutionContext());
|
2017-01-16 13:31:14 -08:00
|
|
|
|
2017-04-14 00:45:46 +04:00
|
|
|
IContentWriter contentWriter = fileSystemProvider.GetContentWriter(testPath);
|
2020-01-31 22:56:22 +00:00
|
|
|
contentWriter.Write(new List<string>() { "contentWriterTestContent" });
|
2017-04-14 00:45:46 +04:00
|
|
|
contentWriter.Close();
|
2018-12-18 21:11:21 +05:00
|
|
|
Assert.Equal(File.ReadAllText(testPath), testContent + @"contentWriterTestContent" + System.Environment.NewLine);
|
2016-03-17 01:34:48 -07:00
|
|
|
}
|
2017-01-16 13:31:14 -08:00
|
|
|
|
2017-04-14 00:45:46 +04:00
|
|
|
[Fact]
|
2016-03-24 01:11:11 -07:00
|
|
|
public void TestClearContent()
|
2016-03-17 01:34:48 -07:00
|
|
|
{
|
2017-04-14 00:45:46 +04:00
|
|
|
FileSystemProvider fileSystemProvider = new FileSystemProvider();
|
|
|
|
|
ProviderInfo providerInfoToSet = GetProvider();
|
|
|
|
|
fileSystemProvider.SetProviderInformation(providerInfoToSet);
|
|
|
|
|
fileSystemProvider.Context = new CmdletProviderContext(GetExecutionContext());
|
|
|
|
|
fileSystemProvider.ClearContent(testPath);
|
|
|
|
|
Assert.Empty(File.ReadAllText(testPath));
|
2015-08-06 16:24:18 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|