From 5bb655e3f6d3acc6963054f9517c8cd5257c522b Mon Sep 17 00:00:00 2001 From: Jordan Dominion Date: Sat, 21 Oct 2023 18:48:14 -0400 Subject: [PATCH] Add test for hard links --- .../IO/TestFilesystemLinkFactory.cs | 57 ++++++++++++++++++- 1 file changed, 55 insertions(+), 2 deletions(-) diff --git a/tests/Tgstation.Server.Host.Tests/IO/TestFilesystemLinkFactory.cs b/tests/Tgstation.Server.Host.Tests/IO/TestFilesystemLinkFactory.cs index e735c48541..dbf2aaf2a8 100644 --- a/tests/Tgstation.Server.Host.Tests/IO/TestFilesystemLinkFactory.cs +++ b/tests/Tgstation.Server.Host.Tests/IO/TestFilesystemLinkFactory.cs @@ -1,10 +1,12 @@ -using Microsoft.VisualStudio.TestTools.UnitTesting; -using System; +using System; using System.IO; using System.Runtime.InteropServices; using System.Security.Principal; +using System.Threading; using System.Threading.Tasks; +using Microsoft.VisualStudio.TestTools.UnitTesting; + namespace Tgstation.Server.Host.IO.Tests { [TestClass] @@ -30,6 +32,57 @@ namespace Tgstation.Server.Host.IO.Tests return principal.IsInRole(WindowsBuiltInRole.Administrator); } + [TestMethod] + public async Task TestHardLinkWorks() + { + string f2 = null; + var f1 = Path.GetTempFileName(); + try + { + f2 = f1 + ".linked"; + const string Text = "Hello world"; + File.WriteAllText(f1, Text); + + if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) + { + await Assert.ThrowsExceptionAsync(() => linkFactory.CreateHardLink(f1, f2, CancellationToken.None)); + Assert.Inconclusive("Windows does not support hardlinks"); + } + + await Assert.ThrowsExceptionAsync(() => linkFactory.CreateHardLink(null, null, CancellationToken.None)); + await Assert.ThrowsExceptionAsync(() => linkFactory.CreateHardLink(f1, null, CancellationToken.None)); + + await linkFactory.CreateHardLink(f1, f2, default); + Assert.IsTrue(File.Exists(f2)); + + var f2Contents = File.ReadAllText(f2); + Assert.AreEqual(Text, f2Contents); + + const string NewText = "asdf"; + File.WriteAllText(f1, NewText); + + f2Contents = File.ReadAllText(f2); + Assert.AreEqual(NewText, f2Contents); + + const string NewText2 = "fdsa"; + File.WriteAllText(f2, NewText2); + + var f1Contents = File.ReadAllText(f1); + Assert.AreEqual(NewText2, f1Contents); + + File.Delete(f1); + Assert.IsFalse(File.Exists(f1)); + Assert.IsTrue(File.Exists(f2)); + f2Contents = File.ReadAllText(f2); + Assert.AreEqual(NewText2, f2Contents); + } + finally + { + File.Delete(f2); + File.Delete(f1); + } + } + [TestMethod] public async Task TestFileWorks() {