mirror of
https://github.com/tgstation/tgstation-server.git
synced 2026-07-20 20:43:47 +01:00
Improve symlink factory tests
This commit is contained in:
@@ -0,0 +1,103 @@
|
||||
using Microsoft.VisualStudio.TestTools.UnitTesting;
|
||||
using System;
|
||||
using System.ComponentModel;
|
||||
using System.IO;
|
||||
using System.Runtime.InteropServices;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Tgstation.Server.Host.IO.Tests
|
||||
{
|
||||
[TestClass]
|
||||
public sealed class TestSymlinkFactory
|
||||
{
|
||||
ISymlinkFactory symlinkFactory;
|
||||
|
||||
[TestInitialize]
|
||||
public void SelectFactory()
|
||||
{
|
||||
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
|
||||
symlinkFactory = new WindowsSymlinkFactory();
|
||||
else
|
||||
symlinkFactory = new PosixSymlinkFactory();
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public async Task TestFileWorks()
|
||||
{
|
||||
const string Text = "Hello world";
|
||||
string f2 = null;
|
||||
var f1 = Path.GetTempFileName();
|
||||
try
|
||||
{
|
||||
f2 = f1 + ".linked";
|
||||
File.WriteAllText(f1, Text);
|
||||
|
||||
await Assert.ThrowsExceptionAsync<ArgumentNullException>(() => symlinkFactory.CreateSymbolicLink(null, null, default)).ConfigureAwait(false);
|
||||
await Assert.ThrowsExceptionAsync<ArgumentNullException>(() => symlinkFactory.CreateSymbolicLink(f1, null, default)).ConfigureAwait(false);
|
||||
|
||||
await symlinkFactory.CreateSymbolicLink(f1, f2, default).ConfigureAwait(false);
|
||||
Assert.IsTrue(File.Exists(f2));
|
||||
|
||||
var f2Contents = File.ReadAllText(f2);
|
||||
Assert.AreEqual(Text, f2Contents);
|
||||
}
|
||||
finally
|
||||
{
|
||||
File.Delete(f2);
|
||||
File.Delete(f1);
|
||||
}
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public async Task TestDirectoryWorks()
|
||||
{
|
||||
const string FileName = "TestFile.txt";
|
||||
const string Text = "Hello world";
|
||||
string f2 = null;
|
||||
var f1 = Path.GetTempFileName();
|
||||
File.Delete(f1);
|
||||
Directory.CreateDirectory(f1);
|
||||
try
|
||||
{
|
||||
f2 = f1 + ".linked";
|
||||
var p1 = Path.Combine(f1, FileName);
|
||||
File.WriteAllText(p1, Text);
|
||||
|
||||
await Assert.ThrowsExceptionAsync<ArgumentNullException>(() => symlinkFactory.CreateSymbolicLink(null, null, default)).ConfigureAwait(false);
|
||||
await Assert.ThrowsExceptionAsync<ArgumentNullException>(() => symlinkFactory.CreateSymbolicLink(f1, null, default)).ConfigureAwait(false);
|
||||
|
||||
await symlinkFactory.CreateSymbolicLink(f1, f2, default).ConfigureAwait(false);
|
||||
|
||||
var p2 = Path.Combine(f2, FileName);
|
||||
Assert.IsTrue(File.Exists(p2));
|
||||
|
||||
var f2Contents = File.ReadAllText(p2);
|
||||
Assert.AreEqual(Text, f2Contents);
|
||||
|
||||
File.Delete(p2);
|
||||
Assert.IsFalse(File.Exists(p1));
|
||||
}
|
||||
finally
|
||||
{
|
||||
Directory.Delete(f2, true);
|
||||
Directory.Delete(f1, true);
|
||||
}
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public async Task TestFailsProperly()
|
||||
{
|
||||
const string BadPath = "/../../?>O(UF+}P{{??>/////";
|
||||
|
||||
try
|
||||
{
|
||||
await symlinkFactory.CreateSymbolicLink(BadPath, BadPath, default).ConfigureAwait(false);
|
||||
}
|
||||
catch
|
||||
{
|
||||
return;
|
||||
}
|
||||
Assert.Fail("No exception thrown!");
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,57 +0,0 @@
|
||||
using Microsoft.VisualStudio.TestTools.UnitTesting;
|
||||
using System;
|
||||
using System.ComponentModel;
|
||||
using System.IO;
|
||||
using System.Runtime.InteropServices;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Tgstation.Server.Host.IO.Tests
|
||||
{
|
||||
[TestClass]
|
||||
public sealed class TestWindowsSymlinkFactory
|
||||
{
|
||||
[TestMethod]
|
||||
public async Task TestWorks()
|
||||
{
|
||||
if (!RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
|
||||
Assert.Inconclusive("Windows only test");
|
||||
|
||||
var factory = new WindowsSymlinkFactory();
|
||||
|
||||
const string Text = "Hello world";
|
||||
string f2 = null;
|
||||
var f1 = Path.GetTempFileName();
|
||||
try
|
||||
{
|
||||
f2 = f1 + ".linked";
|
||||
File.WriteAllText(f1, Text);
|
||||
|
||||
await Assert.ThrowsExceptionAsync<ArgumentNullException>(() => factory.CreateSymbolicLink(null, null, default)).ConfigureAwait(false);
|
||||
await Assert.ThrowsExceptionAsync<ArgumentNullException>(() => factory.CreateSymbolicLink(f1, null, default)).ConfigureAwait(false);
|
||||
|
||||
await factory.CreateSymbolicLink(f1, f2, default).ConfigureAwait(false);
|
||||
|
||||
var f2Contents = File.ReadAllText(f2);
|
||||
Assert.AreEqual(Text, f2Contents);
|
||||
}
|
||||
finally
|
||||
{
|
||||
File.Delete(f2);
|
||||
File.Delete(f1);
|
||||
}
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public async Task TestFailsProperly()
|
||||
{
|
||||
if (!RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
|
||||
Assert.Inconclusive("Windows only test");
|
||||
|
||||
var factory = new WindowsSymlinkFactory();
|
||||
|
||||
const string BadPath = "C:/?><:{ }";
|
||||
|
||||
await Assert.ThrowsExceptionAsync<Win32Exception>(() => factory.CreateSymbolicLink(BadPath, BadPath, default)).ConfigureAwait(false);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user