From d69886bbba3e6368b75c49bac65aacee866ef317 Mon Sep 17 00:00:00 2001 From: Cyberboss Date: Tue, 2 Oct 2018 11:28:51 -0400 Subject: [PATCH] Add TestWindowsSymlinkFactory --- .../IO/TestWindowsSymlinkFactory.cs | 57 +++++++++++++++++++ 1 file changed, 57 insertions(+) create mode 100644 tests/Tgstation.Server.Host.Tests/IO/TestWindowsSymlinkFactory.cs diff --git a/tests/Tgstation.Server.Host.Tests/IO/TestWindowsSymlinkFactory.cs b/tests/Tgstation.Server.Host.Tests/IO/TestWindowsSymlinkFactory.cs new file mode 100644 index 0000000000..c799ff19f3 --- /dev/null +++ b/tests/Tgstation.Server.Host.Tests/IO/TestWindowsSymlinkFactory.cs @@ -0,0 +1,57 @@ +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); + } + } +}