2018-02-13 18:23:53 +01:00
|
|
|
// Copyright (c) Microsoft Corporation. All rights reserved.
|
|
|
|
// Licensed under the MIT License.
|
2018-12-18 17:11:21 +01:00
|
|
|
|
2015-08-06 16:24:18 +02:00
|
|
|
using System;
|
2016-03-24 09:11:11 +01:00
|
|
|
using System.Collections.Generic;
|
2016-03-17 09:34:48 +01:00
|
|
|
using System.Collections.ObjectModel;
|
2016-03-23 04:13:57 +01:00
|
|
|
using System.Globalization;
|
2016-03-17 09:34:48 +01:00
|
|
|
using System.IO;
|
2016-03-23 04:13:57 +01:00
|
|
|
using System.Linq;
|
2015-08-06 16:24:18 +02:00
|
|
|
using System.Management.Automation;
|
2016-03-23 04:13:57 +01:00
|
|
|
using System.Management.Automation.Host;
|
|
|
|
using System.Management.Automation.Internal;
|
|
|
|
using System.Management.Automation.Internal.Host;
|
2016-03-17 09:34:48 +01:00
|
|
|
using System.Management.Automation.Provider;
|
2016-03-23 04:13:57 +01:00
|
|
|
using System.Management.Automation.Runspaces;
|
2018-12-18 17:11:21 +01:00
|
|
|
using System.Reflection;
|
2019-03-12 23:49:16 +01:00
|
|
|
using System.Reflection.Metadata;
|
|
|
|
using Microsoft.CodeAnalysis.CSharp.Syntax;
|
2016-03-23 04:13:57 +01:00
|
|
|
using Microsoft.PowerShell;
|
2015-08-06 16:24:18 +02:00
|
|
|
using Microsoft.PowerShell.Commands;
|
2018-12-18 17:11:21 +01:00
|
|
|
using Xunit;
|
2015-08-06 16:24:18 +02:00
|
|
|
|
2018-01-03 21:43:28 +01:00
|
|
|
namespace PSTests.Parallel
|
2015-08-06 16:24:18 +02:00
|
|
|
{
|
2018-12-18 17:11:21 +01:00
|
|
|
public class FileSystemProviderTests : IDisposable
|
2015-08-06 16:24:18 +02:00
|
|
|
{
|
2017-04-13 22:45:46 +02:00
|
|
|
private string testPath;
|
|
|
|
private string testContent;
|
2018-12-18 17:11:21 +01:00
|
|
|
|
2017-04-13 22:45:46 +02:00
|
|
|
public FileSystemProviderTests()
|
|
|
|
{
|
2018-05-31 08:03:14 +02:00
|
|
|
testPath = Path.GetTempFileName();
|
2017-04-13 22:45:46 +02:00
|
|
|
testContent = "test content!";
|
2018-12-18 17:11:21 +01:00
|
|
|
if (File.Exists(testPath))
|
|
|
|
{
|
|
|
|
File.Delete(testPath);
|
|
|
|
}
|
|
|
|
|
|
|
|
File.AppendAllText(testPath, testContent);
|
2017-04-13 22:45:46 +02:00
|
|
|
}
|
2018-12-18 17:11:21 +01:00
|
|
|
|
2017-04-13 22:45:46 +02:00
|
|
|
void IDisposable.Dispose()
|
|
|
|
{
|
|
|
|
File.Delete(testPath);
|
|
|
|
}
|
2017-01-16 22:31:14 +01:00
|
|
|
|
2017-04-13 22:45:46 +02:00
|
|
|
private ExecutionContext GetExecutionContext()
|
|
|
|
{
|
|
|
|
CultureInfo currentCulture = CultureInfo.CurrentCulture;
|
2018-12-18 17:11:21 +01:00
|
|
|
PSHost hostInterface = new DefaultHost(currentCulture, currentCulture);
|
2016-03-23 04:13:57 +01:00
|
|
|
InitialSessionState iss = InitialSessionState.CreateDefault2();
|
2017-11-29 18:38:04 +01:00
|
|
|
AutomationEngine engine = new AutomationEngine(hostInterface, iss);
|
2016-03-23 04:13:57 +01:00
|
|
|
ExecutionContext executionContext = new ExecutionContext(engine, hostInterface, iss);
|
2017-04-13 22:45:46 +02:00
|
|
|
return executionContext;
|
|
|
|
}
|
2018-12-18 17:11:21 +01:00
|
|
|
|
2017-04-13 22:45:46 +02:00
|
|
|
private ProviderInfo GetProvider()
|
2016-03-24 09:11:11 +01:00
|
|
|
{
|
|
|
|
ExecutionContext executionContext = GetExecutionContext();
|
2016-03-23 04:13:57 +01:00
|
|
|
SessionStateInternal sessionState = new SessionStateInternal(executionContext);
|
2017-01-16 22:31:14 +01:00
|
|
|
|
2018-12-18 17:11:21 +01:00
|
|
|
SessionStateProviderEntry providerEntry = new SessionStateProviderEntry("FileSystem", typeof(FileSystemProvider), null);
|
2017-04-13 22:45:46 +02:00
|
|
|
sessionState.AddSessionStateEntry(providerEntry);
|
2016-03-23 04:13:57 +01:00
|
|
|
ProviderInfo matchingProvider = sessionState.ProviderList.ToList()[0];
|
|
|
|
|
|
|
|
return matchingProvider;
|
|
|
|
}
|
2017-01-16 22:31:14 +01:00
|
|
|
|
2015-08-06 16:24:18 +02:00
|
|
|
[Fact]
|
2016-03-24 09:11:11 +01:00
|
|
|
public void TestCreateJunctionFails()
|
2015-08-06 16:24:18 +02:00
|
|
|
{
|
2018-12-18 17:11:21 +01:00
|
|
|
if (!Platform.IsWindows)
|
2017-11-30 23:44:41 +01:00
|
|
|
{
|
2018-06-12 21:30:10 +02:00
|
|
|
Assert.False(InternalSymbolicLinkLinkCodeMethods.CreateJunction(string.Empty, string.Empty));
|
2017-11-30 23:44:41 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2018-06-12 21:30:10 +02:00
|
|
|
Assert.Throws<System.ArgumentNullException>(delegate { InternalSymbolicLinkLinkCodeMethods.CreateJunction(string.Empty, string.Empty); });
|
2017-11-30 23:44:41 +01:00
|
|
|
}
|
2016-03-17 09:34:48 +01:00
|
|
|
}
|
2017-01-16 22:31:14 +01:00
|
|
|
|
2017-04-13 22:45:46 +02:00
|
|
|
[Fact]
|
2016-03-24 09:11:11 +01:00
|
|
|
public void TestGetHelpMaml()
|
2016-03-17 09:34:48 +01:00
|
|
|
{
|
2017-04-13 22:45:46 +02:00
|
|
|
FileSystemProvider fileSystemProvider = new FileSystemProvider();
|
2018-12-18 17:11:21 +01: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 09:34:48 +01:00
|
|
|
}
|
2017-01-16 22:31:14 +01:00
|
|
|
|
2017-04-13 22:45:46 +02:00
|
|
|
[Fact]
|
2016-03-24 09:11:11 +01:00
|
|
|
public void TestMode()
|
2016-03-17 09:34:48 +01:00
|
|
|
{
|
2018-12-18 17:11:21 +01:00
|
|
|
Assert.Equal(FileSystemProvider.Mode(null), string.Empty);
|
2017-11-30 23:44:41 +01:00
|
|
|
FileSystemInfo directoryObject = null;
|
|
|
|
FileSystemInfo fileObject = null;
|
|
|
|
FileSystemInfo executableObject = null;
|
|
|
|
|
2018-12-18 17:11:21 +01:00
|
|
|
if (!Platform.IsWindows)
|
2017-11-30 23:44:41 +01: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);
|
|
|
|
executableObject = new FileInfo(System.Diagnostics.Process.GetCurrentProcess().MainModule.FileName);
|
|
|
|
}
|
|
|
|
|
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 09:34:48 +01:00
|
|
|
}
|
2017-01-16 22:31:14 +01:00
|
|
|
|
2017-04-13 22:45:46 +02:00
|
|
|
[Fact]
|
2016-03-24 09:11:11 +01:00
|
|
|
public void TestGetProperty()
|
2016-03-17 09:34:48 +01:00
|
|
|
{
|
2017-04-13 22:45:46 +02:00
|
|
|
FileSystemProvider fileSystemProvider = new FileSystemProvider();
|
|
|
|
ProviderInfo providerInfoToSet = GetProvider();
|
|
|
|
fileSystemProvider.SetProviderInformation(providerInfoToSet);
|
|
|
|
fileSystemProvider.Context = new CmdletProviderContext(GetExecutionContext());
|
2018-12-18 17:11:21 +01:00
|
|
|
PSObject pso = new PSObject();
|
|
|
|
pso.AddOrSetProperty("IsReadOnly", false);
|
2017-04-13 22:45:46 +02:00
|
|
|
fileSystemProvider.SetProperty(testPath, pso);
|
2018-12-18 17:11:21 +01:00
|
|
|
fileSystemProvider.GetProperty(testPath, new Collection<string>(){ "IsReadOnly" });
|
2017-04-13 22:45:46 +02:00
|
|
|
FileInfo fileSystemObject1 = new FileInfo(testPath);
|
2018-12-18 17:11:21 +01: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 09:34:48 +01:00
|
|
|
}
|
2017-01-16 22:31:14 +01:00
|
|
|
|
2017-04-13 22:45:46 +02:00
|
|
|
[Fact]
|
2016-03-24 09:11:11 +01:00
|
|
|
public void TestSetProperty()
|
2016-03-17 09:34:48 +01:00
|
|
|
{
|
2017-04-13 22:45:46 +02:00
|
|
|
FileSystemProvider fileSystemProvider = new FileSystemProvider();
|
|
|
|
ProviderInfo providerInfoToSet = GetProvider();
|
|
|
|
fileSystemProvider.SetProviderInformation(providerInfoToSet);
|
|
|
|
fileSystemProvider.Context = new CmdletProviderContext(GetExecutionContext());
|
2018-12-18 17:11:21 +01:00
|
|
|
fileSystemProvider.GetProperty(testPath, new Collection<string>(){ "Name" });
|
2017-04-13 22:45:46 +02:00
|
|
|
FileInfo fileSystemObject1 = new FileInfo(testPath);
|
2018-12-18 17:11:21 +01: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 09:34:48 +01:00
|
|
|
}
|
2017-01-16 22:31:14 +01:00
|
|
|
|
2017-04-13 22:45:46 +02:00
|
|
|
[Fact]
|
2016-03-24 09:11:11 +01:00
|
|
|
public void TestClearProperty()
|
2016-03-17 09:34:48 +01:00
|
|
|
{
|
2017-04-13 22:45:46 +02:00
|
|
|
FileSystemProvider fileSystemProvider = new FileSystemProvider();
|
|
|
|
ProviderInfo providerInfoToSet = GetProvider();
|
|
|
|
fileSystemProvider.SetProviderInformation(providerInfoToSet);
|
|
|
|
fileSystemProvider.Context = new CmdletProviderContext(GetExecutionContext());
|
2018-12-18 17:11:21 +01:00
|
|
|
fileSystemProvider.ClearProperty(testPath, new Collection<string>(){ "Attributes" });
|
2016-03-17 09:34:48 +01:00
|
|
|
}
|
2017-01-16 22:31:14 +01:00
|
|
|
|
2017-04-13 22:45:46 +02:00
|
|
|
[Fact]
|
2016-03-24 09:11:11 +01:00
|
|
|
public void TestGetContentReader()
|
2016-03-17 09:34:48 +01:00
|
|
|
{
|
2017-04-13 22:45:46 +02:00
|
|
|
FileSystemProvider fileSystemProvider = new FileSystemProvider();
|
|
|
|
ProviderInfo providerInfoToSet = GetProvider();
|
|
|
|
fileSystemProvider.SetProviderInformation(providerInfoToSet);
|
|
|
|
fileSystemProvider.Context = new CmdletProviderContext(GetExecutionContext());
|
2017-01-16 22:31:14 +01:00
|
|
|
|
2017-04-13 22:45:46 +02:00
|
|
|
IContentReader contentReader = fileSystemProvider.GetContentReader(testPath);
|
2018-12-18 17:11:21 +01:00
|
|
|
Assert.Equal(contentReader.Read(1)[0], testContent);
|
2017-04-13 22:45:46 +02:00
|
|
|
contentReader.Close();
|
2016-03-17 09:34:48 +01:00
|
|
|
}
|
2017-01-16 22:31:14 +01:00
|
|
|
|
2017-04-13 22:45:46 +02:00
|
|
|
[Fact]
|
2016-03-24 09:11:11 +01:00
|
|
|
public void TestGetContentWriter()
|
2016-03-17 09:34:48 +01:00
|
|
|
{
|
2017-04-13 22:45:46 +02:00
|
|
|
FileSystemProvider fileSystemProvider = new FileSystemProvider();
|
|
|
|
ProviderInfo providerInfoToSet = GetProvider();
|
|
|
|
fileSystemProvider.SetProviderInformation(providerInfoToSet);
|
|
|
|
fileSystemProvider.Context = new CmdletProviderContext(GetExecutionContext());
|
2017-01-16 22:31:14 +01:00
|
|
|
|
2017-04-13 22:45:46 +02:00
|
|
|
IContentWriter contentWriter = fileSystemProvider.GetContentWriter(testPath);
|
2018-12-18 17:11:21 +01:00
|
|
|
contentWriter.Write(new List<string>(){ "contentWriterTestContent" });
|
2017-04-13 22:45:46 +02:00
|
|
|
contentWriter.Close();
|
2018-12-18 17:11:21 +01:00
|
|
|
Assert.Equal(File.ReadAllText(testPath), testContent + @"contentWriterTestContent" + System.Environment.NewLine);
|
2016-03-17 09:34:48 +01:00
|
|
|
}
|
2017-01-16 22:31:14 +01:00
|
|
|
|
2017-04-13 22:45:46 +02:00
|
|
|
[Fact]
|
2016-03-24 09:11:11 +01:00
|
|
|
public void TestClearContent()
|
2016-03-17 09:34:48 +01:00
|
|
|
{
|
2017-04-13 22:45:46 +02: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
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|