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); } } }