mirror of
https://github.com/BinaryAlley/Nemera.git
synced 2026-12-03 12:11:10 +00:00
Adds artwork provider system and Calibre plugin
This commit is contained in:
1 parent
c82fadc7a2
commit
6693ff3f60
124 files changed
+9715
-74
No files matched your search
@@ -0,0 +1,252 @@
|
||||
#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.Domain.SharedKernel.Common.Enums.MediaLibrary;
|
||||
using Lumina.Plugins.Calibre.Core;
|
||||
using Lumina.Plugins.Calibre.Fixtures.Core.Metadata;
|
||||
using Lumina.Plugins.Contracts.Core.Metadata;
|
||||
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.UnitTests.Core;
|
||||
|
||||
/// <summary>
|
||||
/// Contains unit tests for the <see cref="CalibreArtworkProvider"/> class.
|
||||
/// </summary>
|
||||
[ExcludeFromCodeCoverage]
|
||||
public class CalibreArtworkProviderTests
|
||||
{
|
||||
private readonly BookMetadataLookupDtoFixture _bookMetadataLookupDtoFixture = new();
|
||||
private readonly TestMetadataLookupFixture _testMetadataLookupFixture = new();
|
||||
private readonly CalibreArtworkProvider _sut = new();
|
||||
|
||||
[Fact]
|
||||
public void Name_WhenCalled_ShouldReturnTheProviderDisplayName()
|
||||
{
|
||||
// Act
|
||||
string result = _sut.Name;
|
||||
|
||||
// Assert
|
||||
Assert.Equal("Calibre", result);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void SupportedLibraryTypes_WhenCalled_ShouldReturnBookAndEBook()
|
||||
{
|
||||
// Act
|
||||
IReadOnlyList<LibraryType> result = _sut.SupportedLibraryTypes;
|
||||
|
||||
// Assert
|
||||
Assert.Equal(2, result.Count);
|
||||
Assert.Equal(LibraryType.Book, result[0]);
|
||||
Assert.Equal(LibraryType.EBook, result[1]);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void RequiresWebAccess_WhenCalled_ShouldReturnFalse()
|
||||
{
|
||||
// Act
|
||||
bool result = _sut.RequiresWebAccess;
|
||||
|
||||
// Assert
|
||||
Assert.False(result);
|
||||
}
|
||||
|
||||
[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
|
||||
ArtworkDto? result = await _sut.GetArtworkAsync(lookup, CancellationToken.None);
|
||||
|
||||
// Assert
|
||||
Assert.NotNull(result);
|
||||
Assert.Equal(Path.Combine(bookDirectory, "cover.jpg"), result!.LocalPath);
|
||||
Assert.Null(result.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
|
||||
ArtworkDto? result = await _sut.GetArtworkAsync(lookup, CancellationToken.None);
|
||||
|
||||
// Assert
|
||||
Assert.Null(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
|
||||
ArtworkDto? result = await _sut.GetArtworkAsync(lookup, CancellationToken.None);
|
||||
|
||||
// Assert
|
||||
Assert.Null(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
|
||||
ArtworkDto? result = await _sut.GetArtworkAsync(lookup, CancellationToken.None);
|
||||
|
||||
// Assert
|
||||
Assert.Null(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
|
||||
ArtworkDto? result = await _sut.GetArtworkAsync(lookup, CancellationToken.None);
|
||||
|
||||
// Assert
|
||||
Assert.Null(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"), """<?xml version="1.0" encoding="utf-8"?><package xmlns="http://www.idpf.org/2007/opf"><metadata xmlns:dc="http://purl.org/dc/elements/1.1/"><dc:title>Test</dc:title></metadata></package>""");
|
||||
BookMetadataLookupDto lookup = _bookMetadataLookupDtoFixture.Create(path: bookPath);
|
||||
|
||||
// Act
|
||||
ArtworkDto? result = await _sut.GetArtworkAsync(lookup, CancellationToken.None);
|
||||
|
||||
// Assert
|
||||
Assert.Null(result);
|
||||
}
|
||||
finally
|
||||
{
|
||||
Directory.Delete(bookDirectory, true);
|
||||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task GetArtworkAsync_WhenLookupIsNotABookLookup_ShouldReturnNull()
|
||||
{
|
||||
// Arrange
|
||||
MetadataLookupDto lookup = _testMetadataLookupFixture.Create();
|
||||
IArtworkProvider baseProvider = _sut;
|
||||
|
||||
// Act
|
||||
ArtworkDto? result = await baseProvider.GetArtworkAsync(lookup, CancellationToken.None);
|
||||
|
||||
// Assert
|
||||
Assert.Null(result);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task GetArtworkAsync_WhenLookupPathIsEmpty_ShouldReturnNull()
|
||||
{
|
||||
// Arrange
|
||||
BookMetadataLookupDto lookup = _bookMetadataLookupDtoFixture.Create(path: " ");
|
||||
|
||||
// Act
|
||||
ArtworkDto? result = await _sut.GetArtworkAsync(lookup, CancellationToken.None);
|
||||
|
||||
// Assert
|
||||
Assert.Null(result);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Writes an OPF file that references the given cover into the book directory.
|
||||
/// </summary>
|
||||
/// <param name="bookDirectory">The file system path of the directory the OPF file is written into.</param>
|
||||
/// <param name="coverHref">The href of the cover the OPF file references.</param>
|
||||
private static void WriteOpf(string bookDirectory, string coverHref)
|
||||
{
|
||||
string opf = $$"""<?xml version="1.0" encoding="utf-8"?><package xmlns="http://www.idpf.org/2007/opf"><metadata xmlns:dc="http://purl.org/dc/elements/1.1/"><dc:title>Test</dc:title></metadata><guide><reference type="cover" href="{{coverHref}}"/></guide></package>""";
|
||||
File.WriteAllText(Path.Combine(bookDirectory, "metadata.opf"), opf);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Creates a unique temporary directory for the book.
|
||||
/// </summary>
|
||||
/// <returns>The file system path of the created temporary directory.</returns>
|
||||
private static string CreateTempDirectory()
|
||||
{
|
||||
string directoryPath = Path.Combine(Path.GetTempPath(), $"lumina-calibre-artwork-{Guid.NewGuid():N}");
|
||||
Directory.CreateDirectory(directoryPath);
|
||||
return directoryPath;
|
||||
}
|
||||
}
|
||||
+174
@@ -0,0 +1,174 @@
|
||||
#region ========================================================================= USING =====================================================================================
|
||||
using Lumina.Contracts.DTO.MediaLibrary.WrittenContentLibrary.BookLibrary;
|
||||
using Lumina.Contracts.Fixtures.Core.DTO.MediaLibrary.WrittenContentLibrary.BookLibrary;
|
||||
using Lumina.Domain.SharedKernel.Common.Enums.MediaLibrary;
|
||||
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.UnitTests.Core;
|
||||
|
||||
/// <summary>
|
||||
/// Contains unit tests for the <see cref="CalibreBookMetadataProvider"/> class.
|
||||
/// </summary>
|
||||
[ExcludeFromCodeCoverage]
|
||||
public class CalibreBookMetadataProviderTests
|
||||
{
|
||||
private readonly BookMetadataLookupDtoFixture _bookMetadataLookupDtoFixture = new();
|
||||
private readonly CalibreBookMetadataProvider _sut = new();
|
||||
|
||||
private const string OPF_DOCUMENT = """<?xml version="1.0" encoding="utf-8"?><package xmlns="http://www.idpf.org/2007/opf" version="2.0"><metadata xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:opf="http://www.idpf.org/2007/opf"><dc:title>Test Book Title</dc:title><dc:creator opf:role="aut">Test Author</dc:creator></metadata></package>""";
|
||||
|
||||
[Fact]
|
||||
public void Name_WhenCalled_ShouldReturnTheProviderDisplayName()
|
||||
{
|
||||
// Act
|
||||
string result = _sut.Name;
|
||||
|
||||
// Assert
|
||||
Assert.Equal("Calibre", result);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void SupportedLibraryTypes_WhenCalled_ShouldReturnBookAndEBook()
|
||||
{
|
||||
// Act
|
||||
IReadOnlyList<LibraryType> result = _sut.SupportedLibraryTypes;
|
||||
|
||||
// Assert
|
||||
Assert.Equal(2, result.Count);
|
||||
Assert.Equal(LibraryType.Book, result[0]);
|
||||
Assert.Equal(LibraryType.EBook, result[1]);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void RequiresWebAccess_WhenCalled_ShouldReturnFalse()
|
||||
{
|
||||
// Act
|
||||
bool result = _sut.RequiresWebAccess;
|
||||
|
||||
// Assert
|
||||
Assert.False(result);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task GetMetadataAsync_WhenOpfFileExistsNextToTheBook_ShouldReturnTheMappedMetadata()
|
||||
{
|
||||
// Arrange
|
||||
string bookDirectory = CreateTempDirectory();
|
||||
try
|
||||
{
|
||||
string bookPath = Path.Combine(bookDirectory, "book.epub");
|
||||
File.WriteAllText(Path.Combine(bookDirectory, "metadata.opf"), OPF_DOCUMENT);
|
||||
BookMetadataLookupDto lookup = _bookMetadataLookupDtoFixture.Create(path: bookPath);
|
||||
|
||||
// Act
|
||||
BookMetadataDto? result = await _sut.GetMetadataAsync(lookup, CancellationToken.None);
|
||||
|
||||
// Assert
|
||||
Assert.NotNull(result);
|
||||
Assert.Equal("Test Book Title", result!.Title);
|
||||
}
|
||||
finally
|
||||
{
|
||||
Directory.Delete(bookDirectory, true);
|
||||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task GetMetadataAsync_WhenOpfFileDoesNotExist_ShouldReturnNull()
|
||||
{
|
||||
// Arrange
|
||||
string bookDirectory = CreateTempDirectory();
|
||||
try
|
||||
{
|
||||
string bookPath = Path.Combine(bookDirectory, "book.epub");
|
||||
BookMetadataLookupDto lookup = _bookMetadataLookupDtoFixture.Create(path: bookPath);
|
||||
|
||||
// Act
|
||||
BookMetadataDto? result = await _sut.GetMetadataAsync(lookup, CancellationToken.None);
|
||||
|
||||
// Assert
|
||||
Assert.Null(result);
|
||||
}
|
||||
finally
|
||||
{
|
||||
Directory.Delete(bookDirectory, true);
|
||||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task GetMetadataAsync_WhenLookupPathIsEmpty_ShouldReturnNull()
|
||||
{
|
||||
// Arrange
|
||||
BookMetadataLookupDto lookup = _bookMetadataLookupDtoFixture.Create(path: " ");
|
||||
|
||||
// Act
|
||||
BookMetadataDto? result = await _sut.GetMetadataAsync(lookup, CancellationToken.None);
|
||||
|
||||
// Assert
|
||||
Assert.Null(result);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task GetSearchResultsAsync_WhenOpfFileExistsNextToTheBook_ShouldReturnASingleMetadataCandidate()
|
||||
{
|
||||
// Arrange
|
||||
string bookDirectory = CreateTempDirectory();
|
||||
try
|
||||
{
|
||||
string bookPath = Path.Combine(bookDirectory, "book.epub");
|
||||
File.WriteAllText(Path.Combine(bookDirectory, "metadata.opf"), OPF_DOCUMENT);
|
||||
BookMetadataLookupDto lookup = _bookMetadataLookupDtoFixture.Create(path: bookPath);
|
||||
|
||||
// Act
|
||||
IReadOnlyList<BookMetadataDto> result = await _sut.GetSearchResultsAsync(lookup, CancellationToken.None);
|
||||
|
||||
// Assert
|
||||
BookMetadataDto metadata = Assert.IsType<BookMetadataDto>(Assert.Single(result));
|
||||
Assert.Equal("Test Book Title", metadata.Title);
|
||||
}
|
||||
finally
|
||||
{
|
||||
Directory.Delete(bookDirectory, true);
|
||||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task GetSearchResultsAsync_WhenOpfFileDoesNotExist_ShouldReturnAnEmptyList()
|
||||
{
|
||||
// Arrange
|
||||
string bookDirectory = CreateTempDirectory();
|
||||
try
|
||||
{
|
||||
string bookPath = Path.Combine(bookDirectory, "book.epub");
|
||||
BookMetadataLookupDto lookup = _bookMetadataLookupDtoFixture.Create(path: bookPath);
|
||||
|
||||
// Act
|
||||
IReadOnlyList<BookMetadataDto> result = await _sut.GetSearchResultsAsync(lookup, CancellationToken.None);
|
||||
|
||||
// Assert
|
||||
Assert.Empty(result);
|
||||
}
|
||||
finally
|
||||
{
|
||||
Directory.Delete(bookDirectory, true);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Creates a unique temporary directory for the book.
|
||||
/// </summary>
|
||||
/// <returns>The file system path of the created temporary directory.</returns>
|
||||
private static string CreateTempDirectory()
|
||||
{
|
||||
string directoryPath = Path.Combine(Path.GetTempPath(), $"lumina-calibre-metadata-{Guid.NewGuid():N}");
|
||||
Directory.CreateDirectory(directoryPath);
|
||||
return directoryPath;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,98 @@
|
||||
#region ========================================================================= USING =====================================================================================
|
||||
using Lumina.Plugins.Calibre.Core;
|
||||
using Lumina.Plugins.Contracts.Common.Models.DTO.Settings;
|
||||
using Lumina.Plugins.Contracts.Core.Metadata;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
#endregion
|
||||
|
||||
namespace Lumina.Plugins.Calibre.UnitTests.Core;
|
||||
|
||||
/// <summary>
|
||||
/// Contains unit tests for the <see cref="CalibrePlugin"/> class.
|
||||
/// </summary>
|
||||
[ExcludeFromCodeCoverage]
|
||||
public class CalibrePluginTests
|
||||
{
|
||||
private readonly CalibrePlugin _sut = new();
|
||||
|
||||
[Fact]
|
||||
public void Id_WhenCalled_ShouldReturnStablePluginIdentifier()
|
||||
{
|
||||
// Act
|
||||
Guid result = _sut.Id;
|
||||
|
||||
// Assert
|
||||
Assert.Equal(CalibrePlugin.s_pluginId, result);
|
||||
Assert.Equal(new Guid("a1b2c3d4-5e6f-4a7b-8c9d-0e1f2a3b4c5d"), result);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Name_WhenCalled_ShouldReturnPluginDisplayName()
|
||||
{
|
||||
// Act
|
||||
string result = _sut.Name;
|
||||
|
||||
// Assert
|
||||
Assert.Equal("Calibre Metadata", result);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Author_WhenCalled_ShouldReturnPluginAuthor()
|
||||
{
|
||||
// Act
|
||||
string result = _sut.Author;
|
||||
|
||||
// Assert
|
||||
Assert.Equal("Lumina", result);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Version_WhenCalled_ShouldReturnPluginVersion()
|
||||
{
|
||||
// Act
|
||||
Version result = _sut.Version;
|
||||
|
||||
// Assert
|
||||
Assert.Equal(new Version(1, 0, 0), result);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Description_WhenCalled_ShouldReturnPluginDescription()
|
||||
{
|
||||
// Act
|
||||
string result = _sut.Description;
|
||||
|
||||
// Assert
|
||||
Assert.Equal("Reads book metadata and covers from the OPF files of a Calibre library.", result);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void GetSettingsSchema_WhenCalled_ShouldReturnEmptySettingsSchema()
|
||||
{
|
||||
// Act
|
||||
IReadOnlyList<PluginSettingDescriptorDto> result = _sut.GetSettingsSchema();
|
||||
|
||||
// Assert
|
||||
Assert.Empty(result);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void RegisterServices_WhenCalled_ShouldRegisterTheKeyedMetadataAndArtworkProviders()
|
||||
{
|
||||
// Arrange
|
||||
ServiceCollection services = new();
|
||||
|
||||
// Act
|
||||
_sut.RegisterServices(services);
|
||||
ServiceProvider serviceProvider = services.BuildServiceProvider();
|
||||
|
||||
// Assert
|
||||
Assert.NotNull(serviceProvider.GetKeyedService<IMetadataProvider>(CalibrePlugin.s_pluginId));
|
||||
Assert.NotNull(serviceProvider.GetKeyedService<IArtworkProvider>(CalibrePlugin.s_pluginId));
|
||||
Assert.Null(serviceProvider.GetKeyedService<IMetadataProvider>(Guid.NewGuid()));
|
||||
Assert.Null(serviceProvider.GetKeyedService<IArtworkProvider>(Guid.NewGuid()));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,367 @@
|
||||
#region ========================================================================= USING =====================================================================================
|
||||
using Lumina.Contracts.DTO.MediaContributors;
|
||||
using Lumina.Contracts.DTO.MediaLibrary.WrittenContentLibrary.BookLibrary;
|
||||
using Lumina.Domain.SharedKernel.Common.Enums.BookLibrary;
|
||||
using Lumina.Plugins.Calibre.Common.Models.DTO.Opf;
|
||||
using Lumina.Plugins.Calibre.Core.Mapping;
|
||||
using Lumina.Plugins.Calibre.Fixtures.Common.Models.DTO.Opf;
|
||||
using System;
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
#endregion
|
||||
|
||||
namespace Lumina.Plugins.Calibre.UnitTests.Core.Mapping;
|
||||
|
||||
/// <summary>
|
||||
/// Contains unit tests for the <see cref="CalibreMapper"/> class.
|
||||
/// </summary>
|
||||
[ExcludeFromCodeCoverage]
|
||||
public class CalibreMapperTests
|
||||
{
|
||||
private readonly OpfDocumentDtoFixture _opfDocumentDtoFixture = new();
|
||||
private readonly OpfCreatorDtoFixture _opfCreatorDtoFixture = new();
|
||||
private readonly OpfContributorDtoFixture _opfContributorDtoFixture = new();
|
||||
private readonly OpfIdentifierDtoFixture _opfIdentifierDtoFixture = new();
|
||||
|
||||
[Fact]
|
||||
public void Map_WhenDocumentHasAllFields_ShouldMapThemToTheBookMetadataDto()
|
||||
{
|
||||
// Arrange
|
||||
OpfDocumentDto document = _opfDocumentDtoFixture.Create(
|
||||
title: "Test Book Title",
|
||||
description: "<p>A test description with <b>markup</b>. </p>",
|
||||
publisher: "Test Publisher",
|
||||
publishDate: new DateTimeOffset(2015, 6, 15, 0, 0, 0, TimeSpan.Zero),
|
||||
languageCode: "en",
|
||||
series: "The Series",
|
||||
seriesIndex: 2.5,
|
||||
rating: 8,
|
||||
subjects: ["Science fiction", "Space opera"],
|
||||
creators: [_opfCreatorDtoFixture.Create("Test Author", "aut"), _opfCreatorDtoFixture.Create("Co Author", "oth")],
|
||||
contributors:
|
||||
[
|
||||
_opfContributorDtoFixture.Create("calibre (3.48.0) [https://calibre-ebook.com]", "bkp"),
|
||||
_opfContributorDtoFixture.Create("Test Translator", "trl")
|
||||
],
|
||||
identifiers:
|
||||
[
|
||||
_opfIdentifierDtoFixture.Create("ISBN", "978-0-306-40615-7"),
|
||||
_opfIdentifierDtoFixture.Create("goodreads", "123456"),
|
||||
_opfIdentifierDtoFixture.Create("ASIN", "B00TEST1"),
|
||||
_opfIdentifierDtoFixture.Create("lccn", "test-lccn"),
|
||||
_opfIdentifierDtoFixture.Create("oclc", "ocm12345678"),
|
||||
_opfIdentifierDtoFixture.Create("openlibrary", "OL100M"),
|
||||
_opfIdentifierDtoFixture.Create("google", "google-id")
|
||||
]);
|
||||
|
||||
// Act
|
||||
BookMetadataDto result = CalibreMapper.Map(document);
|
||||
|
||||
// Assert
|
||||
Assert.Equal("Test Book Title", result.Title);
|
||||
Assert.Equal("A test description with markup .", result.Description);
|
||||
Assert.Equal("Test Publisher", result.Publisher);
|
||||
Assert.NotNull(result.ReleaseInfo);
|
||||
Assert.Equal(new DateOnly(2015, 6, 15), result.ReleaseInfo!.OriginalReleaseDate);
|
||||
Assert.Equal(2015, result.ReleaseInfo.OriginalReleaseYear);
|
||||
Assert.Equal("The Series", result.Series!.Title);
|
||||
Assert.Equal(2.5f, result.VolumeNumber);
|
||||
Assert.Equal("B00TEST1", result.ASIN);
|
||||
Assert.Equal("123456", result.GoodreadsId);
|
||||
Assert.Equal("test-lccn", result.LCCN);
|
||||
Assert.Equal("ocm12345678", result.OCLCNumber);
|
||||
Assert.Equal("OL100M", result.OpenLibraryId);
|
||||
Assert.Equal("google-id", result.GoogleBooksId);
|
||||
Assert.Null(result.Genres);
|
||||
Assert.Null(result.CoverImagePath);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Map_WhenDescriptionContainsHtmlEntities_ShouldDecodeAndStripThem()
|
||||
{
|
||||
// Arrange
|
||||
OpfDocumentDto document = _opfDocumentDtoFixture.Create(description: "A & B <tag>description</tag> with multiple spaces");
|
||||
|
||||
// Act
|
||||
BookMetadataDto result = CalibreMapper.Map(document);
|
||||
|
||||
// Assert
|
||||
Assert.Equal("A & B description with multiple spaces", result.Description);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Map_WhenDescriptionIsEmpty_ShouldReturnNullDescription()
|
||||
{
|
||||
// Arrange
|
||||
OpfDocumentDto document = _opfDocumentDtoFixture.Create(description: " ");
|
||||
|
||||
// Act
|
||||
BookMetadataDto result = CalibreMapper.Map(document);
|
||||
|
||||
// Assert
|
||||
Assert.Null(result.Description);
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[InlineData(0)] // a zero series index is not a meaningful volume number
|
||||
public void Map_WhenSeriesIndexIsZero_ShouldReturnNullVolumeNumber(double seriesIndex)
|
||||
{
|
||||
// Arrange
|
||||
OpfDocumentDto document = _opfDocumentDtoFixture.Create(seriesIndex: seriesIndex);
|
||||
|
||||
// Act
|
||||
BookMetadataDto result = CalibreMapper.Map(document);
|
||||
|
||||
// Assert
|
||||
Assert.Null(result.VolumeNumber);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Map_WhenSeriesIndexIsNull_ShouldReturnNullVolumeNumber()
|
||||
{
|
||||
// Arrange
|
||||
OpfDocumentDto document = _opfDocumentDtoFixture.Create();
|
||||
|
||||
// Act
|
||||
BookMetadataDto result = CalibreMapper.Map(document);
|
||||
|
||||
// Assert
|
||||
Assert.Null(result.VolumeNumber);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Map_WhenSeriesIsNull_ShouldReturnNullSeries()
|
||||
{
|
||||
// Arrange
|
||||
OpfDocumentDto document = _opfDocumentDtoFixture.Create();
|
||||
|
||||
// Act
|
||||
BookMetadataDto result = CalibreMapper.Map(document);
|
||||
|
||||
// Assert
|
||||
Assert.Null(result.Series);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Map_WhenRatingIsPresent_ShouldMapItToACalibreRatingWithMaxValueTen()
|
||||
{
|
||||
// Arrange
|
||||
OpfDocumentDto document = _opfDocumentDtoFixture.Create(rating: 8);
|
||||
|
||||
// Act
|
||||
BookMetadataDto result = CalibreMapper.Map(document);
|
||||
|
||||
// Assert
|
||||
BookRatingDto rating = Assert.Single(result.Ratings!);
|
||||
Assert.Equal(8m, rating.Value);
|
||||
Assert.Equal(10m, rating.MaxValue);
|
||||
Assert.Equal(BookRatingSource.Calibre, rating.Source);
|
||||
Assert.Null(rating.VoteCount);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Map_WhenRatingIsMissing_ShouldReturnNullRatings()
|
||||
{
|
||||
// Arrange
|
||||
OpfDocumentDto document = _opfDocumentDtoFixture.Create();
|
||||
|
||||
// Act
|
||||
BookMetadataDto result = CalibreMapper.Map(document);
|
||||
|
||||
// Assert
|
||||
Assert.Null(result.Ratings);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Map_WhenSubjectsArePresent_ShouldMapThemToTagsWithNullGenres()
|
||||
{
|
||||
// Arrange
|
||||
OpfDocumentDto document = _opfDocumentDtoFixture.Create(subjects: ["Science fiction", "Space opera"]);
|
||||
|
||||
// Act
|
||||
BookMetadataDto result = CalibreMapper.Map(document);
|
||||
|
||||
// Assert
|
||||
Assert.Null(result.Genres);
|
||||
Assert.Equal(2, result.Tags!.Count);
|
||||
Assert.Equal("Science fiction", result.Tags[0].Name);
|
||||
Assert.Equal("Space opera", result.Tags[1].Name);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Map_WhenCreatorsAndContributorsArePresent_ShouldMapRolesAndCategories()
|
||||
{
|
||||
// Arrange
|
||||
OpfDocumentDto document = _opfDocumentDtoFixture.Create(
|
||||
creators: [_opfCreatorDtoFixture.Create("Test Author", "aut"), _opfCreatorDtoFixture.Create("Co Author", null)],
|
||||
contributors:
|
||||
[
|
||||
_opfContributorDtoFixture.Create("Test Translator", "trl"),
|
||||
_opfContributorDtoFixture.Create("Test Illustrator", "ill"),
|
||||
_opfContributorDtoFixture.Create("calibre (3.48.0) [https://calibre-ebook.com]", "bkp"),
|
||||
_opfContributorDtoFixture.Create("Misc Contributor", null)
|
||||
]);
|
||||
|
||||
// Act
|
||||
BookMetadataDto result = CalibreMapper.Map(document);
|
||||
|
||||
// Assert
|
||||
Assert.Equal(6, result.Contributors!.Count);
|
||||
|
||||
MediaContributorDto author = result.Contributors[0];
|
||||
Assert.Equal("Test Author", author.Name!.DisplayName);
|
||||
Assert.Equal("Author", author.Role!.Name);
|
||||
Assert.Equal("Writing", author.Role.Category);
|
||||
|
||||
MediaContributorDto coAuthor = result.Contributors[1];
|
||||
Assert.Equal("Co Author", coAuthor.Name!.DisplayName);
|
||||
Assert.Equal("Author", coAuthor.Role!.Name);
|
||||
Assert.Equal("Writing", coAuthor.Role.Category);
|
||||
|
||||
MediaContributorDto translator = result.Contributors[2];
|
||||
Assert.Equal("Test Translator", translator.Name!.DisplayName);
|
||||
Assert.Equal("trl", translator.Role!.Name);
|
||||
Assert.Equal("Translation", translator.Role.Category);
|
||||
|
||||
MediaContributorDto illustrator = result.Contributors[3];
|
||||
Assert.Equal("Test Illustrator", illustrator.Name!.DisplayName);
|
||||
Assert.Equal("ill", illustrator.Role!.Name);
|
||||
Assert.Equal("Art", illustrator.Role.Category);
|
||||
|
||||
MediaContributorDto production = result.Contributors[4];
|
||||
Assert.Equal("calibre (3.48.0) [https://calibre-ebook.com]", production.Name!.DisplayName);
|
||||
Assert.Equal("bkp", production.Role!.Name);
|
||||
Assert.Equal("Production", production.Role.Category);
|
||||
|
||||
MediaContributorDto misc = result.Contributors[5];
|
||||
Assert.Equal("Misc Contributor", misc.Name!.DisplayName);
|
||||
Assert.Equal("Contributor", misc.Role!.Name);
|
||||
Assert.Equal("Other", misc.Role.Category);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Map_WhenCreatorsAreDuplicatedWithTheSameRole_ShouldDeDuplicateThem()
|
||||
{
|
||||
// Arrange
|
||||
OpfDocumentDto document = _opfDocumentDtoFixture.Create(creators: [_opfCreatorDtoFixture.Create("Test Author", "aut"), _opfCreatorDtoFixture.Create("Test Author", "aut")]);
|
||||
|
||||
// Act
|
||||
BookMetadataDto result = CalibreMapper.Map(document);
|
||||
|
||||
// Assert
|
||||
MediaContributorDto author = Assert.Single(result.Contributors!);
|
||||
Assert.Equal("Test Author", author.Name!.DisplayName);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Map_WhenIsbnIdentifiersArePresent_ShouldNormalizeAndMapThem()
|
||||
{
|
||||
// Arrange
|
||||
OpfDocumentDto document = _opfDocumentDtoFixture.Create(identifiers:
|
||||
[
|
||||
_opfIdentifierDtoFixture.Create("ISBN", "978-0-306-40615-7"),
|
||||
_opfIdentifierDtoFixture.Create("ISBN", "0306406152"),
|
||||
_opfIdentifierDtoFixture.Create("ISBN", "978-0-306-40615-7"),
|
||||
_opfIdentifierDtoFixture.Create("ISBN", "not-an-isbn")
|
||||
]);
|
||||
|
||||
// Act
|
||||
BookMetadataDto result = CalibreMapper.Map(document);
|
||||
|
||||
// Assert
|
||||
Assert.Equal(2, result.Isbns!.Count);
|
||||
Assert.Contains(result.Isbns, isbn => isbn.Value == "9780306406157" && isbn.Format == IsbnFormat.Isbn13);
|
||||
Assert.Contains(result.Isbns, isbn => isbn.Value == "0306406152" && isbn.Format == IsbnFormat.Isbn10);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Map_WhenNoIdentifiersArePresent_ShouldReturnEmptyIsbnList()
|
||||
{
|
||||
// Arrange
|
||||
OpfDocumentDto document = _opfDocumentDtoFixture.Create();
|
||||
|
||||
// Act
|
||||
BookMetadataDto result = CalibreMapper.Map(document);
|
||||
|
||||
// Assert
|
||||
Assert.Empty(result.Isbns!);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Map_WhenAmazonIdentifiersArePresent_ShouldMapAsin()
|
||||
{
|
||||
// Arrange
|
||||
OpfDocumentDto document = _opfDocumentDtoFixture.Create(identifiers: [_opfIdentifierDtoFixture.Create("amazon", "B00TEST1")]);
|
||||
|
||||
// Act
|
||||
BookMetadataDto result = CalibreMapper.Map(document);
|
||||
|
||||
// Assert
|
||||
Assert.Equal("B00TEST1", result.ASIN);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Map_WhenLanguageCodeIsUnknown_ShouldReturnTheRawCode()
|
||||
{
|
||||
// Arrange
|
||||
OpfDocumentDto document = _opfDocumentDtoFixture.Create(languageCode: "zz");
|
||||
|
||||
// Act
|
||||
BookMetadataDto result = CalibreMapper.Map(document);
|
||||
|
||||
// Assert
|
||||
Assert.Equal("zz", result.Language!.LanguageCode);
|
||||
Assert.Equal("zz", result.Language.LanguageName);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Map_WhenLanguageCodeIsEmpty_ShouldReturnNullLanguage()
|
||||
{
|
||||
// Arrange
|
||||
OpfDocumentDto document = _opfDocumentDtoFixture.Create(languageCode: " ");
|
||||
|
||||
// Act
|
||||
BookMetadataDto result = CalibreMapper.Map(document);
|
||||
|
||||
// Assert
|
||||
Assert.Null(result.Language);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Map_WhenDocumentHasNoMetadata_ShouldReturnEmptyDto()
|
||||
{
|
||||
// Arrange
|
||||
OpfDocumentDto document = _opfDocumentDtoFixture.Create();
|
||||
|
||||
// Act
|
||||
BookMetadataDto result = CalibreMapper.Map(document);
|
||||
|
||||
// Assert
|
||||
Assert.Null(result.Title);
|
||||
Assert.Null(result.Description);
|
||||
Assert.NotNull(result.ReleaseInfo);
|
||||
Assert.Null(result.Publisher);
|
||||
Assert.Null(result.Series);
|
||||
Assert.Null(result.VolumeNumber);
|
||||
Assert.Null(result.Ratings);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Map_WhenPublishDateIsMissing_ShouldReturnEmptyReleaseInfo()
|
||||
{
|
||||
// Arrange
|
||||
OpfDocumentDto document = _opfDocumentDtoFixture.Create(publishDate: null);
|
||||
|
||||
// Act
|
||||
BookMetadataDto result = CalibreMapper.Map(document);
|
||||
|
||||
// Assert
|
||||
// a book without a publication date must still be enrichable, so the release info is present with empty fields
|
||||
Assert.NotNull(result.ReleaseInfo);
|
||||
Assert.Null(result.ReleaseInfo!.OriginalReleaseDate);
|
||||
Assert.Null(result.ReleaseInfo.OriginalReleaseYear);
|
||||
Assert.Null(result.ReleaseInfo.ReReleaseDate);
|
||||
Assert.Null(result.ReleaseInfo.ReReleaseYear);
|
||||
Assert.Null(result.ReleaseInfo.ReleaseCountry);
|
||||
Assert.Null(result.ReleaseInfo.ReleaseVersion);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,212 @@
|
||||
#region ========================================================================= USING =====================================================================================
|
||||
using Lumina.Plugins.Calibre.Common.Models.DTO.Opf;
|
||||
using Lumina.Plugins.Calibre.Core.Opf;
|
||||
using System;
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
using System.IO;
|
||||
#endregion
|
||||
|
||||
namespace Lumina.Plugins.Calibre.UnitTests.Core.Opf;
|
||||
|
||||
/// <summary>
|
||||
/// Contains unit tests for the <see cref="OpfReader"/> class.
|
||||
/// </summary>
|
||||
[ExcludeFromCodeCoverage]
|
||||
public class OpfReaderTests
|
||||
{
|
||||
[Fact]
|
||||
public void Read_WhenOpfFileContainsAllSupportedFields_ShouldParseThemAll()
|
||||
{
|
||||
// Arrange
|
||||
string opfFilePath = CreateOpfFile("""<?xml version="1.0" encoding="utf-8"?><package xmlns="http://www.idpf.org/2007/opf" version="2.0" unique-identifier="uuid_id"><metadata xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:opf="http://www.idpf.org/2007/opf"><dc:title>Test Book Title</dc:title><dc:creator opf:role="aut">Test Author</dc:creator><dc:creator opf:role="oth">Second Creator</dc:creator><dc:contributor opf:role="bkp">calibre (3.48.0) [https://calibre-ebook.com]</dc:contributor><dc:contributor opf:role="trl">Test Translator</dc:contributor><dc:description><p>A test description with <b>markup</b>.</p></dc:description><dc:publisher>Test Publisher</dc:publisher><dc:date>2015-06-15</dc:date><dc:language>en</dc:language><dc:identifier opf:scheme="ISBN">978-0-306-40615-7</dc:identifier><dc:identifier opf:scheme="goodreads">123456</dc:identifier><dc:identifier opf:scheme="ASIN">B00TEST1</dc:identifier><dc:identifier opf:scheme="LCCN">test-lccn</dc:identifier><dc:identifier opf:scheme="oclc">ocm12345678</dc:identifier><dc:identifier opf:scheme="openlibrary">OL100M</dc:identifier><dc:identifier opf:scheme="google">google-id</dc:identifier><dc:subject>Science fiction</dc:subject><dc:subject>Space opera</dc:subject><meta name="calibre:series" content="The Series"/><meta name="calibre:series_index" content="2.5"/><meta name="calibre:rating" content="8"/></metadata><guide><reference type="cover" href="cover.jpg"/><reference type="toc" href="toc.xhtml"/></guide></package>""");
|
||||
|
||||
try
|
||||
{
|
||||
// Act
|
||||
OpfDocumentDto result = OpfReader.Read(opfFilePath);
|
||||
|
||||
// Assert
|
||||
Assert.Equal("Test Book Title", result.Title);
|
||||
Assert.Equal("<p>A test description with <b>markup</b>.</p>", result.Description);
|
||||
Assert.Equal("Test Publisher", result.Publisher);
|
||||
Assert.Equal(2015, result.PublishDate!.Value.Year);
|
||||
Assert.Equal(6, result.PublishDate.Value.Month);
|
||||
Assert.Equal(15, result.PublishDate.Value.Day);
|
||||
Assert.Equal("en", result.LanguageCode);
|
||||
Assert.Equal("The Series", result.Series);
|
||||
Assert.Equal(2.5, result.SeriesIndex);
|
||||
Assert.Equal(8, result.Rating);
|
||||
Assert.Equal("cover.jpg", result.CoverHref);
|
||||
Assert.Equal(2, result.Creators.Count);
|
||||
Assert.Equal("Test Author", result.Creators[0].Name);
|
||||
Assert.Equal("aut", result.Creators[0].Role);
|
||||
Assert.Equal(2, result.Contributors.Count);
|
||||
Assert.Equal("calibre (3.48.0) [https://calibre-ebook.com]", result.Contributors[0].Name);
|
||||
Assert.Equal("bkp", result.Contributors[0].Role);
|
||||
Assert.Equal(7, result.Identifiers.Count);
|
||||
Assert.Contains(result.Identifiers, identifier => identifier.Scheme == "ISBN" && identifier.Value == "978-0-306-40615-7");
|
||||
Assert.Contains(result.Identifiers, identifier => identifier.Scheme == "goodreads" && identifier.Value == "123456");
|
||||
Assert.Contains(result.Identifiers, identifier => identifier.Scheme == "ASIN" && identifier.Value == "B00TEST1");
|
||||
Assert.Equal(2, result.Subjects.Count);
|
||||
Assert.Equal("Science fiction", result.Subjects[0]);
|
||||
Assert.Equal("Space opera", result.Subjects[1]);
|
||||
}
|
||||
finally
|
||||
{
|
||||
File.Delete(opfFilePath);
|
||||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Read_WhenOpfFileDoesNotExist_ShouldReturnEmptyDocument()
|
||||
{
|
||||
// Act
|
||||
OpfDocumentDto result = OpfReader.Read(Path.Combine(Path.GetTempPath(), $"missing-{Guid.NewGuid():N}.opf"));
|
||||
|
||||
// Assert
|
||||
Assert.Null(result.Title);
|
||||
Assert.Empty(result.Identifiers);
|
||||
Assert.Empty(result.Creators);
|
||||
Assert.Empty(result.Subjects);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Read_WhenMetadataContainsWhitespaceOnlyValues_ShouldSkipThem()
|
||||
{
|
||||
// Arrange
|
||||
string opfFilePath = CreateOpfFile("""<?xml version="1.0" encoding="utf-8"?><package xmlns="http://www.idpf.org/2007/opf"><metadata xmlns:dc="http://purl.org/dc/elements/1.1/"><dc:title> </dc:title><dc:creator opf:role="aut"> </dc:creator><dc:subject></dc:subject></metadata></package>""");
|
||||
|
||||
try
|
||||
{
|
||||
// Act
|
||||
OpfDocumentDto result = OpfReader.Read(opfFilePath);
|
||||
|
||||
// Assert
|
||||
Assert.Null(result.Title);
|
||||
Assert.Empty(result.Creators);
|
||||
Assert.Empty(result.Subjects);
|
||||
}
|
||||
finally
|
||||
{
|
||||
File.Delete(opfFilePath);
|
||||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Read_WhenMetadataElementIsMissing_ShouldReturnEmptyDocument()
|
||||
{
|
||||
// Arrange
|
||||
string opfFilePath = CreateOpfFile("""<?xml version="1.0" encoding="utf-8"?><package xmlns="http://www.idpf.org/2007/opf"></package>""");
|
||||
|
||||
try
|
||||
{
|
||||
// Act
|
||||
OpfDocumentDto result = OpfReader.Read(opfFilePath);
|
||||
|
||||
// Assert
|
||||
Assert.Null(result.Title);
|
||||
Assert.Empty(result.Identifiers);
|
||||
}
|
||||
finally
|
||||
{
|
||||
File.Delete(opfFilePath);
|
||||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Read_WhenOpfFileDeclaresADoctypeWithEntities_ShouldNotExpandThem()
|
||||
{
|
||||
// Arrange
|
||||
// the DTD is prohibited, so the document must not be parsed and the entity must not be expanded into the title
|
||||
string opfFilePath = CreateOpfFile("""<?xml version="1.0" encoding="utf-8"?><!DOCTYPE package [<!ENTITY xxe SYSTEM "file:///etc/passwd">]><package xmlns="http://www.idpf.org/2007/opf"><metadata xmlns:dc="http://purl.org/dc/elements/1.1/"><dc:title>&xxe;</dc:title></metadata></package>""");
|
||||
|
||||
try
|
||||
{
|
||||
// Act
|
||||
OpfDocumentDto result = OpfReader.Read(opfFilePath);
|
||||
|
||||
// Assert
|
||||
Assert.Null(result.Title);
|
||||
}
|
||||
finally
|
||||
{
|
||||
File.Delete(opfFilePath);
|
||||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Read_WhenOpfFileContainsBillionLaughsStyleEntityExpansion_ShouldNotExpandThem()
|
||||
{
|
||||
// Arrange
|
||||
string opfFilePath = CreateOpfFile("""<?xml version="1.0" encoding="utf-8"?><!DOCTYPE lolz [<!ENTITY lol "lol"><!ENTITY lol2 "&lol;&lol;&lol;&lol;&lol;&lol;&lol;&lol;&lol;&lol;"><!ENTITY lol3 "&lol2;&lol2;&lol2;&lol2;&lol2;&lol2;&lol2;&lol2;&lol2;&lol2;">]><package xmlns="http://www.idpf.org/2007/opf"><metadata xmlns:dc="http://purl.org/dc/elements/1.1/"><dc:title>&lol3;</dc:title></metadata></package>""");
|
||||
|
||||
try
|
||||
{
|
||||
// Act
|
||||
OpfDocumentDto result = OpfReader.Read(opfFilePath);
|
||||
|
||||
// Assert
|
||||
Assert.Null(result.Title);
|
||||
}
|
||||
finally
|
||||
{
|
||||
File.Delete(opfFilePath);
|
||||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Read_WhenOpfFileIsOversized_ShouldReturnEmptyDocument()
|
||||
{
|
||||
// Arrange
|
||||
string opfFilePath = Path.Combine(Path.GetTempPath(), $"oversized-{Guid.NewGuid():N}.opf");
|
||||
string oversizedContent = $"""<?xml version="1.0" encoding="utf-8"?><package xmlns="http://www.idpf.org/2007/opf"><metadata xmlns:dc="http://purl.org/dc/elements/1.1/"><dc:title>{new string('a', (5 * 1024 * 1024) + 100)}</dc:title></metadata></package>""";
|
||||
File.WriteAllText(opfFilePath, oversizedContent);
|
||||
|
||||
try
|
||||
{
|
||||
// Act
|
||||
OpfDocumentDto result = OpfReader.Read(opfFilePath);
|
||||
|
||||
// Assert
|
||||
Assert.Null(result.Title);
|
||||
Assert.Empty(result.Identifiers);
|
||||
}
|
||||
finally
|
||||
{
|
||||
File.Delete(opfFilePath);
|
||||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Read_WhenOpfFileIsMalformedXml_ShouldReturnEmptyDocument()
|
||||
{
|
||||
// Arrange
|
||||
string opfFilePath = CreateOpfFile("""<package xmlns="http://www.idpf.org/2007/opf"><metadata><dc:title>Unclosed""");
|
||||
|
||||
try
|
||||
{
|
||||
// Act
|
||||
OpfDocumentDto result = OpfReader.Read(opfFilePath);
|
||||
|
||||
// Assert
|
||||
Assert.Null(result.Title);
|
||||
Assert.Empty(result.Identifiers);
|
||||
}
|
||||
finally
|
||||
{
|
||||
File.Delete(opfFilePath);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Creates a temporary OPF file with the given content and returns its path.
|
||||
/// </summary>
|
||||
/// <param name="content">The content of the OPF file to create.</param>
|
||||
/// <returns>The file system path of the created OPF file.</returns>
|
||||
private static string CreateOpfFile(string content)
|
||||
{
|
||||
string opfFilePath = Path.Combine(Path.GetTempPath(), $"opf-{Guid.NewGuid():N}.opf");
|
||||
File.WriteAllText(opfFilePath, content);
|
||||
return opfFilePath;
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user