From da070602c8db9ec39b447c34857f2a8434e9cdd0 Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Sun, 2 Jul 2023 12:22:39 -0500 Subject: add tests to ensure only desirable paths can be accessed --- tests/slskd.Tests.Unit/Files/FileServiceTests.cs | 137 +++++++++++++++++++++++ 1 file changed, 137 insertions(+) create mode 100644 tests/slskd.Tests.Unit/Files/FileServiceTests.cs (limited to 'tests') diff --git a/tests/slskd.Tests.Unit/Files/FileServiceTests.cs b/tests/slskd.Tests.Unit/Files/FileServiceTests.cs new file mode 100644 index 00000000..7c9931ab --- /dev/null +++ b/tests/slskd.Tests.Unit/Files/FileServiceTests.cs @@ -0,0 +1,137 @@ +using Microsoft.Extensions.Options; + +namespace slskd.Tests.Unit.Files +{ + using System; + using System.IO; + using System.Threading.Tasks; + using Moq; + using slskd.Files; + using Xunit; + + public class FileServiceTests : IDisposable + { + public FileServiceTests() + { + OptionsSnapshotMock = new Mock>(); + + Temp = Path.Combine(Path.GetTempPath(), $"slskd.test.{Guid.NewGuid()}"); + Directory.CreateDirectory(Temp); + + FileService = new FileService( + optionsSnapshot: OptionsSnapshotMock.Object); + } + + public void Dispose() + { + Directory.Delete(Temp, recursive: true); + } + + private Mock> OptionsSnapshotMock { get; init; } + private string Temp { get; init; } + private IFileService FileService { get; init; } + + [Fact] + public async Task ListContentsAsync_Throws_ArgumentException_Given_Relative_Path() + { + var ex = await Record.ExceptionAsync(() => FileService.ListContentsAsync(directory: "../")); + + Assert.NotNull(ex); + Assert.IsType(ex); + Assert.Equal("directory", ((ArgumentException)ex).ParamName); + } + + [Fact] + public async Task ListContentsAsync_Throws_UnauthorizedException_Given_Disallowed_Directory() + { + OptionsSnapshotMock.Setup(o => o.Value).Returns(new Options + { + Directories = new Options.DirectoriesOptions + { + Downloads = Path.Combine(Temp, "downloads"), + Incomplete = Path.Combine(Temp, "incomplete"), + } + }); + + var ex = await Record.ExceptionAsync(() => FileService.ListContentsAsync(directory: Path.Combine(Temp, "foo"))); + + Assert.NotNull(ex); + Assert.IsType(ex); + } + + [Fact] + public async Task ListContentsAsync_Throws_NotFoundException_Given_NonExistent_Directory() + { + OptionsSnapshotMock.Setup(o => o.Value).Returns(new Options + { + Directories = new Options.DirectoriesOptions + { + Downloads = Path.Combine(Temp, "downloads"), + Incomplete = Path.Combine(Temp, "incomplete"), + } + }); + + var ex = await Record.ExceptionAsync(() => FileService.ListContentsAsync(directory: Path.Combine(Temp, "downloads", "foo"))); + + Assert.NotNull(ex); + Assert.IsType(ex); + } + + [Fact] + public async Task DeleteDirectoriesAsync_Throws_ArgumentException_Given_Relative_Path() + { + var ex = await Record.ExceptionAsync(() => FileService.DeleteDirectoriesAsync("../foo")); + + Assert.NotNull(ex); + Assert.IsType(ex); + Assert.Equal("directories", ((ArgumentException)ex).ParamName); + } + + [Fact] + public async Task DeleteDirectoriesAsync_Throws_ArgumentException_Given_Disallowed_Path() + { + OptionsSnapshotMock.Setup(o => o.Value).Returns(new Options + { + Directories = new Options.DirectoriesOptions + { + Downloads = Path.Combine(Temp, "downloads"), + Incomplete = Path.Combine(Temp, "incomplete"), + } + }); + + var ex = await Record.ExceptionAsync(() => FileService.DeleteDirectoriesAsync(Path.Combine(Temp, "foo"))); + + Assert.NotNull(ex); + Assert.IsType(ex); + } + + [Fact] + public async Task DeleteFilesAsync_Throws_ArgumentException_Given_Relative_Path() + { + var ex = await Record.ExceptionAsync(() => FileService.DeleteFilesAsync("../foo.bar")); + + Assert.NotNull(ex); + Assert.IsType(ex); + Assert.Equal("files", ((ArgumentException)ex).ParamName); + } + + [Fact] + public async Task DeleteFilesAsync_Throws_ArgumentException_Given_Disallowed_Path() + { + OptionsSnapshotMock.Setup(o => o.Value).Returns(new Options + { + Directories = new Options.DirectoriesOptions + { + Downloads = Path.Combine(Temp, "downloads"), + Incomplete = Path.Combine(Temp, "incomplete"), + } + }); + + var ex = await Record.ExceptionAsync(() => FileService.DeleteFilesAsync(Path.Combine(Temp, "foo"))); + + Assert.NotNull(ex); + Assert.IsType(ex); + } + } +} + -- cgit v1.2.3