diff --git a/tests/Tgstation.Server.Host.Tests/IO/TestSymlinkFactory.cs b/tests/Tgstation.Server.Host.Tests/IO/TestSymlinkFactory.cs new file mode 100644 index 0000000000..d21955f066 --- /dev/null +++ b/tests/Tgstation.Server.Host.Tests/IO/TestSymlinkFactory.cs @@ -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(() => symlinkFactory.CreateSymbolicLink(null, null, default)).ConfigureAwait(false); + await Assert.ThrowsExceptionAsync(() => 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(() => symlinkFactory.CreateSymbolicLink(null, null, default)).ConfigureAwait(false); + await Assert.ThrowsExceptionAsync(() => 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!"); + } + } +} diff --git a/tests/Tgstation.Server.Host.Tests/IO/TestWindowsSymlinkFactory.cs b/tests/Tgstation.Server.Host.Tests/IO/TestWindowsSymlinkFactory.cs deleted file mode 100644 index c799ff19f3..0000000000 --- a/tests/Tgstation.Server.Host.Tests/IO/TestWindowsSymlinkFactory.cs +++ /dev/null @@ -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(() => factory.CreateSymbolicLink(null, null, default)).ConfigureAwait(false); - await Assert.ThrowsExceptionAsync(() => 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(() => factory.CreateSymbolicLink(BadPath, BadPath, default)).ConfigureAwait(false); - } - } -}