Files
RobustToolbox/Robust.UnitTesting/Shared/Resources/WritableDirProviderTest.cs
T
AcruidandGitHub 2183cd7ca1 Massive Namespace Cleanup (#1544)
* Removed the Interfaces folder.
* All objects inside the GameObjects subfolders are now in the GameObjects namespace.
* Added a Resharper DotSettings file to mark the GameObjects subfolders as not providing namespaces.
* Simplified Robust.client.Graphics namespace.
* Automated remove redundant using statements.
2021-02-10 23:27:19 -08:00

66 lines
2.0 KiB
C#

using System;
using System.IO;
using NUnit.Framework;
using Robust.Shared.ContentPack;
using Robust.Shared.Utility;
namespace Robust.UnitTesting.Shared.Resources
{
[TestFixture]
[TestOf(typeof(WritableDirProvider))]
public class WritableDirProviderTest
{
private string _testDirPath = default!;
private DirectoryInfo _testDir = default!;
private WritableDirProvider _dirProvider = default!;
[OneTimeSetUp]
public void Setup()
{
var tmpPath = Path.GetTempPath();
var guid = Guid.NewGuid();
_testDirPath = Path.Combine(tmpPath, guid.ToString());
_testDir = Directory.CreateDirectory(_testDirPath);
var subDir = Path.Combine(_testDirPath, "writable");
_dirProvider = new WritableDirProvider(Directory.CreateDirectory(subDir));
}
[OneTimeTearDown]
public void TearDown()
{
_testDir.Delete(true);
}
[Test]
public void TestNoParentAccess()
{
File.WriteAllText(Path.Combine(_testDirPath, "dummy"), "foobar");
// No, ../ does not work to read stuff in the parent dir.
Assert.That(() => _dirProvider.ReadAllText(new ResourcePath("/../dummy")),
Throws.InstanceOf<FileNotFoundException>());
}
[Test]
public void TestNotRooted()
{
// Path must be rooted.
Assert.That(() => _dirProvider.OpenRead(new ResourcePath("foo.bar")),
Throws.InstanceOf<ArgumentException>());
}
[Test]
public void TestParentAccessClamped()
{
File.WriteAllText(Path.Combine(_testDirPath, "dummy"), "foobar");
_dirProvider.WriteAllText(new ResourcePath("/dummy"), "pranked");
// ../ should get clamped to /.
Assert.That(_dirProvider.ReadAllText(new ResourcePath("/../dummy")), Is.EqualTo("pranked"));
}
}
}