diff options
| author | JP Dillingham <jp@dillingham.me> | 2023-07-02 12:22:39 -0500 |
|---|---|---|
| committer | JP Dillingham <jp@dillingham.me> | 2023-07-02 12:22:39 -0500 |
| commit | da070602c8db9ec39b447c34857f2a8434e9cdd0 (patch) | |
| tree | 990bf8ea388ca59323f567585d457061848a6578 /tests/slskd.Tests.Unit/Files/FileServiceTests.cs | |
| parent | 94712ee9915cd46212b589e283a211545ec1141e (diff) | |
| download | slskd-da070602c8db9ec39b447c34857f2a8434e9cdd0.tar.gz | |
add tests to ensure only desirable paths can be accessed
Diffstat (limited to 'tests/slskd.Tests.Unit/Files/FileServiceTests.cs')
| -rw-r--r-- | tests/slskd.Tests.Unit/Files/FileServiceTests.cs | 137 |
1 files changed, 137 insertions, 0 deletions
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<IOptionsSnapshot<Options>>(); + + 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<IOptionsSnapshot<Options>> 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<ArgumentException>(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<UnauthorizedException>(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<NotFoundException>(ex); + } + + [Fact] + public async Task DeleteDirectoriesAsync_Throws_ArgumentException_Given_Relative_Path() + { + var ex = await Record.ExceptionAsync(() => FileService.DeleteDirectoriesAsync("../foo")); + + Assert.NotNull(ex); + Assert.IsType<ArgumentException>(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<UnauthorizedException>(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<ArgumentException>(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<UnauthorizedException>(ex); + } + } +} + |
