Improve symlink factory tests

This commit is contained in:
Cyberboss
2018-10-02 11:41:25 -04:00
parent 7c289d3cb6
commit 68e7932e68
2 changed files with 103 additions and 57 deletions
@@ -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);
}
}
}