#region ========================================================================= USING =====================================================================================
using Lumina.Contracts.DTO.Common;
using Lumina.Contracts.DTO.MediaLibrary.WrittenContentLibrary.BookLibrary;
using Lumina.Contracts.Fixtures.Core.DTO.MediaLibrary.WrittenContentLibrary.BookLibrary;
using Lumina.Plugins.Calibre.Core;
using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.IO;
using System.Threading;
using System.Threading.Tasks;
#endregion
namespace Lumina.Plugins.Calibre.IntegrationTests.Core;
///
/// Contains integration tests for the class.
///
[ExcludeFromCodeCoverage]
public class CalibreArtworkProviderTests
{
private readonly BookMetadataLookupDtoFixture _bookMetadataLookupDtoFixture = new();
private readonly CalibreArtworkProvider _sut = new();
[Fact]
public async Task GetArtworkAsync_WhenCoverHrefIsABareFileName_ShouldReturnTheLocalCoverPath()
{
// Arrange
string bookDirectory = CreateTempDirectory();
try
{
string bookPath = Path.Combine(bookDirectory, "book.epub");
WriteOpf(bookDirectory, coverHref: "cover.jpg");
File.WriteAllText(Path.Combine(bookDirectory, "cover.jpg"), "image-bytes");
BookMetadataLookupDto lookup = _bookMetadataLookupDtoFixture.Create(path: bookPath);
// Act
IReadOnlyList result = await _sut.GetArtworkAsync(lookup, CancellationToken.None);
// Assert
ArtworkDto artwork = Assert.Single(result);
Assert.Equal(Path.Combine(bookDirectory, "cover.jpg"), artwork.LocalPath);
Assert.Null(artwork.RemoteUrl);
}
finally
{
Directory.Delete(bookDirectory, true);
}
}
[Fact]
public async Task GetArtworkAsync_WhenCoverHrefIsAnAbsolutePath_ShouldReturnNull()
{
// Arrange
string bookDirectory = CreateTempDirectory();
try
{
string bookPath = Path.Combine(bookDirectory, "book.epub");
WriteOpf(bookDirectory, coverHref: @"C:\Windows\System32\drivers\etc\hosts");
BookMetadataLookupDto lookup = _bookMetadataLookupDtoFixture.Create(path: bookPath);
// Act
IReadOnlyList result = await _sut.GetArtworkAsync(lookup, CancellationToken.None);
// Assert
Assert.Empty(result);
}
finally
{
Directory.Delete(bookDirectory, true);
}
}
[Theory]
[InlineData("../cover.jpg")] // Parent directory escape.
[InlineData("sub/cover.jpg")] // Path with a separator.
[InlineData("sub\\cover.jpg")] // Path with a backslash separator.
public async Task GetArtworkAsync_WhenCoverHrefContainsPathTraversalOrSeparators_ShouldReturnNull(string coverHref)
{
// Arrange
string bookDirectory = CreateTempDirectory();
try
{
string bookPath = Path.Combine(bookDirectory, "book.epub");
WriteOpf(bookDirectory, coverHref);
BookMetadataLookupDto lookup = _bookMetadataLookupDtoFixture.Create(path: bookPath);
// Act
IReadOnlyList result = await _sut.GetArtworkAsync(lookup, CancellationToken.None);
// Assert
Assert.Empty(result);
}
finally
{
Directory.Delete(bookDirectory, true);
}
}
[Fact]
public async Task GetArtworkAsync_WhenCoverFileIsMissing_ShouldReturnNull()
{
// Arrange
string bookDirectory = CreateTempDirectory();
try
{
string bookPath = Path.Combine(bookDirectory, "book.epub");
WriteOpf(bookDirectory, coverHref: "cover.jpg");
BookMetadataLookupDto lookup = _bookMetadataLookupDtoFixture.Create(path: bookPath);
// Act
IReadOnlyList result = await _sut.GetArtworkAsync(lookup, CancellationToken.None);
// Assert
Assert.Empty(result);
}
finally
{
Directory.Delete(bookDirectory, true);
}
}
[Fact]
public async Task GetArtworkAsync_WhenOpfFileDoesNotExist_ShouldReturnNull()
{
// Arrange
string bookDirectory = CreateTempDirectory();
try
{
string bookPath = Path.Combine(bookDirectory, "book.epub");
BookMetadataLookupDto lookup = _bookMetadataLookupDtoFixture.Create(path: bookPath);
// Act
IReadOnlyList result = await _sut.GetArtworkAsync(lookup, CancellationToken.None);
// Assert
Assert.Empty(result);
}
finally
{
Directory.Delete(bookDirectory, true);
}
}
[Fact]
public async Task GetArtworkAsync_WhenOpfHasNoCoverReference_ShouldReturnNull()
{
// Arrange
string bookDirectory = CreateTempDirectory();
try
{
string bookPath = Path.Combine(bookDirectory, "book.epub");
File.WriteAllText(Path.Combine(bookDirectory, "metadata.opf"), """Test""");
BookMetadataLookupDto lookup = _bookMetadataLookupDtoFixture.Create(path: bookPath);
// Act
IReadOnlyList result = await _sut.GetArtworkAsync(lookup, CancellationToken.None);
// Assert
Assert.Empty(result);
}
finally
{
Directory.Delete(bookDirectory, true);
}
}
///
/// Writes an OPF file that references the given cover into the book directory.
///
/// The file system path of the directory the OPF file is written into.
/// The href of the cover the OPF file references.
private static void WriteOpf(string bookDirectory, string coverHref)
{
string opf = $$"""Test""";
File.WriteAllText(Path.Combine(bookDirectory, "metadata.opf"), opf);
}
///
/// Creates a unique temporary directory for the book.
///
/// The file system path of the created temporary directory.
private static string CreateTempDirectory()
{
string directoryPath = Path.Combine(Path.GetTempPath(), $"lumina-calibre-artwork-{Guid.NewGuid():N}");
Directory.CreateDirectory(directoryPath);
return directoryPath;
}
}