using VersOne.Epub.Internal; using VersOne.Epub.Options; using VersOne.Epub.Schema; using VersOne.Epub.Test.Comparers; using VersOne.Epub.Test.Unit.Mocks; namespace VersOne.Epub.Test.Unit.Readers { public class PackageReaderTests { private const string CONTAINER_FILE_PATH = "META-INF/container.xml"; private const string OPF_FILE_PATH = "content.opf"; private const string CONTAINER_FILE = $""" """; private const string MINIMAL_EPUB2_OPF_FILE = $""" """; private const string MINIMAL_EPUB3_OPF_FILE = $""" """; private const string MINIMAL_EPUB3_1_OPF_FILE = $""" """; private const string FULL_OPF_FILE = $""" Test title John Doe 9781234567890 en 2021-12-31T00:00:00Z Test title for collection 1 Test title for collection 2 """; private const string OPF_FILE_WITH_NON_SUPPORTED_EPUB_VERSION = $""" """; private const string OPF_FILE_WITHOUT_PACKAGE = $""" """; private const string OPF_FILE_WITHOUT_METADATA = $""" """; private const string OPF_FILE_WITHOUT_MANIFEST = $""" """; private const string OPF_FILE_WITHOUT_SPINE = $""" """; private const string OPF_FILE_WITHOUT_VERSION_IN_PACKAGE = $""" """; private const string OPF_FILE_WITHOUT_ID_IN_MANIFEST_ITEM = $""" """; private const string OPF_FILE_WITH_ESCAPED_HREF_IN_MANIFEST_ITEM = $""" """; private const string OPF_FILE_WITHOUT_HREF_IN_MANIFEST_ITEM = $""" """; private const string OPF_FILE_WITHOUT_MEDIA_TYPE_IN_MANIFEST_ITEM = $""" """; private const string OPF_FILE_WITH_DUPLICATING_MANIFEST_ITEM_IDS = $""" """; private const string OPF_FILE_WITH_DUPLICATING_MANIFEST_ITEM_HREFS = $""" """; private const string EPUB2_OPF_FILE_WITHOUT_SPINE_TOC = $""" """; private const string EPUB2_OPF_FILE_WITH_EMPTY_SPINE_TOC = $""" """; private const string OPF_FILE_WITHOUT_IDREF_IN_SPINE_ITEMREF = $""" """; private const string OPF_FILE_WITHOUT_TYPE_IN_GUIDE_REFERENCE = $""" """; private const string OPF_FILE_WITHOUT_HREF_IN_GUIDE_REFERENCE = $""" """; private const string OPF_FILE_WITHOUT_ROLE_IN_COLLECTION = $""" """; private static EpubPackage MinimalEpub2Package => new ( uniqueIdentifier: null, epubVersion: EpubVersion.EPUB_2, metadata: new EpubMetadata(), manifest: new EpubManifest(), spine: new EpubSpine ( toc: "ncx" ), guide: null ); private static EpubPackage MinimalEpub3Package => new ( uniqueIdentifier: null, epubVersion: EpubVersion.EPUB_3, metadata: new EpubMetadata(), manifest: new EpubManifest(), spine: new EpubSpine(), guide: null ); private static EpubPackage MinimalEpub31Package => new ( uniqueIdentifier: null, epubVersion: EpubVersion.EPUB_3_1, metadata: new EpubMetadata(), manifest: new EpubManifest(), spine: new EpubSpine(), guide: null ); private static EpubPackage FullPackage => new ( uniqueIdentifier: "book-uid", epubVersion: EpubVersion.EPUB_3, metadata: new EpubMetadata ( titles: new List() { new ( title: "Test title" ) }, creators: new List() { new ( creator: "John Doe" ) }, identifiers: new List() { new ( identifier: "9781234567890", id: "book-uid" ) }, languages: new List() { new ( language: "en" ) }, metaItems: new List() { new ( name: null, content: "2021-12-31T00:00:00Z", property: "dcterms:modified" ) } ), manifest: new EpubManifest ( id: "manifest-id", items: new List() { new ( id: "item-front", href: "front.html", mediaType: "application/xhtml+xml" ), new ( id: "cover", href: "cover.html", mediaType: "application/xhtml+xml" ), new ( id: "cover-image", href: "cover.jpg", mediaType: "image/jpeg", properties: new List() { EpubManifestProperty.COVER_IMAGE } ), new ( id: "item-css", href: "styles.css", mediaType: "text/css" ), new ( id: "item-font", href: "font.ttf", mediaType: "application/x-font-truetype" ), new ( id: "item-1", href: "chapter1.html", mediaType: "application/xhtml+xml", mediaOverlay: "item-1-smil", requiredNamespace: null, requiredModules: null, fallback: null, fallbackStyle: null, properties: null ), new ( id: "item-1-smil", href: "chapter1.smil", mediaType: "application/smil+xml" ), new ( id: "item-2", href: "chapter2.html", mediaType: "application/xhtml+xml" ), new ( id: "item-2-fall", href: "chapter2.xml", mediaType: "text/example+xml", mediaOverlay: null, requiredNamespace: "http://example.com/ns/example/", requiredModules: "ruby, server-side-image-map", fallback: "item-2", fallbackStyle: "item-css", properties: null ), new ( id: "item-3", href: "chapter3.html", mediaType: "application/xhtml+xml" ), new ( id: "item-3-fall", href: "chapter3.xml", mediaType: "application/z3998-auth+xml", mediaOverlay: null, requiredNamespace: null, requiredModules: null, fallback: "item-3", fallbackStyle: null, properties: null ), new ( id: "item-3-remote-audio", href: "http://example.com/audio/123/chapter3.mp4", mediaType: "audio/mp4" ), new ( id: "item-image", href: "image.jpg", mediaType: "image/jpeg" ), new ( id: "item-title-audio", href: "title.mp3", mediaType: "audio/mpeg" ), new ( id: "item-atom", href: "book.atom", mediaType: "application/atom+xml" ), new ( id: "item-toc", href: "toc.html", mediaType: "application/xhtml+xml", properties: new List { EpubManifestProperty.NAV } ), new ( id: "ncx", href: "toc.ncx", mediaType: "application/x-dtbncx+xml" ) } ), spine: new EpubSpine ( id: "spine", pageProgressionDirection: EpubPageProgressionDirection.LEFT_TO_RIGHT, toc: "ncx", items: new List() { new ( id: "itemref-1", idRef: "item-front", isLinear: true ), new ( id: "itemref-2", idRef: "item-toc", isLinear: false ), new ( id: "itemref-3", idRef: "item-1", isLinear: true ), new ( id: "itemref-4", idRef: "item-2", isLinear: true, properties: new List() { EpubSpineProperty.PAGE_SPREAD_LEFT } ), new ( id: "itemref-5", idRef: "item-3", isLinear: true, properties: new List() { EpubSpineProperty.PAGE_SPREAD_RIGHT } ) } ), guide: new EpubGuide ( items: new List() { new ( type: "toc", title: "Contents", href: "toc.html" ) } ), collections: new List() { new ( role: "http://example.org/roles/group", metadata: new EpubMetadata ( titles: new List() { new ( title: "Test title for collection 1", id: "collection-1-title", textDirection: EpubTextDirection.LEFT_TO_RIGHT, language: "en" ) } ), nestedCollections: new List() { new ( role: "http://example.org/roles/unit", metadata: new EpubMetadata ( titles: new List() { new ( title: "Test title for collection 2", id: "collection-2-title", textDirection: EpubTextDirection.AUTO, language: "is" ) } ), nestedCollections: new List(), links: new List(), id: "collection-2", textDirection: EpubTextDirection.RIGHT_TO_LEFT, language: "is" ) }, links: new List() { new ( href: "https://example.com/onix/123", id: "collection-1-link", mediaType: "application/xml", properties: new List() { EpubMetadataLinkProperty.ONIX }, relationships: new List() { EpubMetadataLinkRelationship.RECORD, EpubMetadataLinkRelationship.ONIX_RECORD }, hrefLanguage: null ) }, id: "collection-1", textDirection: EpubTextDirection.LEFT_TO_RIGHT, language: "en" ) }, id: "package-id", textDirection: EpubTextDirection.LEFT_TO_RIGHT, prefix: "foaf: http://xmlns.com/foaf/spec/", language: "en" ); private static EpubPackage Epub2PackageWithoutSpineToc => new ( uniqueIdentifier: null, epubVersion: EpubVersion.EPUB_2, metadata: new EpubMetadata(), manifest: new EpubManifest(), spine: new EpubSpine(), guide: null ); public static IEnumerable ReadMinimalPackageAsyncTestData { get { yield return new object[] { MINIMAL_EPUB2_OPF_FILE, MinimalEpub2Package }; yield return new object[] { MINIMAL_EPUB3_OPF_FILE, MinimalEpub3Package }; yield return new object[] { MINIMAL_EPUB3_1_OPF_FILE, MinimalEpub31Package }; } } public static IEnumerable ReadFullPackageAsyncTestData { get { yield return new object[] { FULL_OPF_FILE, FullPackage }; } } [Fact(DisplayName = "Constructing a PackageReader instance with a non-null epubReaderOptions parameter should succeed")] public void ConstructorWithNonNullEpubReaderOptionsTest() { _ = new PackageReader(new EpubReaderOptions()); } [Fact(DisplayName = "Constructing a PackageReader instance with a null epubReaderOptions parameter should succeed")] public void ConstructorWithNullEpubReaderOptionsTest() { _ = new PackageReader(null); } [Theory(DisplayName = "Reading a minimal OPF package should succeed")] [MemberData(nameof(ReadMinimalPackageAsyncTestData))] public async Task ReadMinimalPackageAsyncTest(string opfFileContent, EpubPackage expectedEpubPackage) { await TestSuccessfulReadOperation(opfFileContent, expectedEpubPackage); } [Theory(DisplayName = "Reading a full OPF package should succeed")] [MemberData(nameof(ReadFullPackageAsyncTestData))] public async Task ReadFullPackageAsyncTest(string opfFileContent, EpubPackage expectedEpubPackage) { await TestSuccessfulReadOperation(opfFileContent, expectedEpubPackage); } [Fact(DisplayName = "Trying to read OPF package from the EPUB file with no OPF package should fail with EpubContainerException")] public async Task ReadPackageWithNoOpfFileTest() { TestZipFile testZipFile = new(); testZipFile.AddEntry(CONTAINER_FILE_PATH, CONTAINER_FILE); PackageReader packageReader = new(); await Assert.ThrowsAsync(() => packageReader.ReadPackageAsync(testZipFile, OPF_FILE_PATH)); } [Fact(DisplayName = "Trying to read OPF package with non-supported EPUB version should fail with EpubPackageException")] public async Task ReadPackageWithNonSupportedEpubVersionTest() { await TestFailingReadOperation(OPF_FILE_WITH_NON_SUPPORTED_EPUB_VERSION); } [Fact(DisplayName = "Trying to read OPF package without 'package' XML node should fail with EpubPackageException")] public async Task ReadPackageWithoutPackageNodeTest() { await TestFailingReadOperation(OPF_FILE_WITHOUT_PACKAGE); } [Fact(DisplayName = "Trying to read OPF package without 'metadata' XML node should fail with EpubPackageException")] public async Task ReadPackageWithoutMetadataNodeTest() { await TestFailingReadOperation(OPF_FILE_WITHOUT_METADATA); } [Fact(DisplayName = "Trying to read OPF package without 'manifest' XML node should fail with EpubPackageException")] public async Task ReadPackageWithoutManifestNodeTest() { await TestFailingReadOperation(OPF_FILE_WITHOUT_MANIFEST); } [Fact(DisplayName = "Trying to read OPF package without 'spine' XML node should fail with EpubPackageException")] public async Task ReadPackageWithoutSpineNodeTest() { await TestFailingReadOperation(OPF_FILE_WITHOUT_SPINE); } [Fact(DisplayName = "Trying to read OPF package without 'version' attribute in a package XML node should fail with EpubPackageException")] public async Task ReadPackageWithoutVersionTest() { await TestFailingReadOperation(OPF_FILE_WITHOUT_VERSION_IN_PACKAGE); } [Fact(DisplayName = "Trying to read OPF package without 'id' attribute in a manifest item XML node should fail with EpubPackageException")] public async Task ReadPackageWithoutManifestItemIdTest() { await TestFailingReadOperation(OPF_FILE_WITHOUT_ID_IN_MANIFEST_ITEM); } [Fact(DisplayName = "Trying to read OPF package without 'id' attribute in a manifest item XML node and null PackageReaderOptions should fail with EpubPackageException")] public async Task ReadPackageWithoutManifestItemIdWithNullPackageReaderOptionsTest() { await TestFailingReadOperationWithNullPackageReaderOptions(OPF_FILE_WITHOUT_ID_IN_MANIFEST_ITEM); } [Fact(DisplayName = "Trying to read OPF package without 'id' attribute in a manifest item XML node with SkipInvalidManifestItems = true should succeed")] public async Task ReadPackageWithoutManifestItemIdWithSkippingInvalidManifestItemsTest() { await TestSuccessfulReadOperationWithSkippingInvalidManifestItems(OPF_FILE_WITHOUT_ID_IN_MANIFEST_ITEM, MinimalEpub3Package); } [Fact(DisplayName = "Read an OPF package with a URI-escaped 'href' attribute in a manifest item XML node should succeed")] public async Task ReadPackageWithEscapedManifestItemHrefTest() { EpubPackage expectedPackage = new ( uniqueIdentifier: null, epubVersion: EpubVersion.EPUB_3, metadata: new EpubMetadata(), manifest: new EpubManifest ( id: null, items: new List() { new ( id: "item-1", href: "chapter1.html", mediaType: "application/xhtml+xml" ) } ), spine: new EpubSpine(), guide: null ); await TestSuccessfulReadOperationWithSkippingInvalidManifestItems(OPF_FILE_WITH_ESCAPED_HREF_IN_MANIFEST_ITEM, expectedPackage); } [Fact(DisplayName = "Trying to read OPF package without 'href' attribute in a manifest item XML node should fail with EpubPackageException")] public async Task ReadPackageWithoutManifestItemHrefTest() { await TestFailingReadOperation(OPF_FILE_WITHOUT_HREF_IN_MANIFEST_ITEM); } [Fact(DisplayName = "Trying to read OPF package without 'href' attribute in a manifest item XML node and null PackageReaderOptions should fail with EpubPackageException")] public async Task ReadPackageWithoutManifestItemHrefWithNullPackageReaderOptionsTest() { await TestFailingReadOperationWithNullPackageReaderOptions(OPF_FILE_WITHOUT_HREF_IN_MANIFEST_ITEM); } [Fact(DisplayName = "Trying to read OPF package without 'href' attribute in a manifest item XML node with SkipInvalidManifestItems = true should succeed")] public async Task ReadPackageWithoutManifestItemHrefWithSkippingInvalidManifestItemsTest() { await TestSuccessfulReadOperationWithSkippingInvalidManifestItems(OPF_FILE_WITHOUT_HREF_IN_MANIFEST_ITEM, MinimalEpub3Package); } [Fact(DisplayName = "Trying to read OPF package without 'media-type' attribute in a manifest item XML node should fail with EpubPackageException")] public async Task ReadPackageWithoutManifestItemMediaTypeTest() { await TestFailingReadOperation(OPF_FILE_WITHOUT_MEDIA_TYPE_IN_MANIFEST_ITEM); } [Fact(DisplayName = "Trying to read OPF package without 'media-type' attribute in a manifest item XML node and null PackageReaderOptions should fail with EpubPackageException")] public async Task ReadPackageWithoutManifestItemMediaTypeWithNullPackageReaderOptionsTest() { await TestFailingReadOperationWithNullPackageReaderOptions(OPF_FILE_WITHOUT_MEDIA_TYPE_IN_MANIFEST_ITEM); } [Fact(DisplayName = "Trying to read OPF package without 'media-type' attribute in a manifest item XML node with SkipInvalidManifestItems = true should succeed")] public async Task ReadPackageWithoutManifestItemMediaTypeWithSkippingInvalidManifestItemsTest() { await TestSuccessfulReadOperationWithSkippingInvalidManifestItems(OPF_FILE_WITHOUT_MEDIA_TYPE_IN_MANIFEST_ITEM, MinimalEpub3Package); } [Fact(DisplayName = "Trying to read OPF package with duplicating 'id' attributes in manifest item XML nodes should fail with EpubPackageException")] public async Task ReadPackageWithDuplicatingManifestItemIdsTest() { await TestFailingReadOperationWithNullPackageReaderOptions(OPF_FILE_WITH_DUPLICATING_MANIFEST_ITEM_IDS); } [Fact(DisplayName = "Trying to read OPF package with duplicating 'href' attributes in manifest item XML nodes should fail with EpubPackageException")] public async Task ReadPackageWithDuplicatingManifestItemHrefsTest() { await TestFailingReadOperationWithNullPackageReaderOptions(OPF_FILE_WITH_DUPLICATING_MANIFEST_ITEM_HREFS); } [Fact(DisplayName = "Trying to read EPUB 2 OPF package without 'toc' attribute in the spine XML node should fail with EpubPackageException")] public async Task ReadEpub2PackageWithoutSpineTocTest() { await TestFailingReadOperation(EPUB2_OPF_FILE_WITHOUT_SPINE_TOC); } [Fact(DisplayName = "Trying to read EPUB 2 OPF package without 'toc' attribute in the spine XML node and null PackageReaderOptions should fail with EpubPackageException")] public async Task ReadEpub2PackageWithoutSpineTocWithNullPackageReaderOptionsTest() { await TestFailingReadOperationWithNullPackageReaderOptions(EPUB2_OPF_FILE_WITHOUT_SPINE_TOC); } [Fact(DisplayName = "Trying to read EPUB 2 OPF package with empty 'toc' attribute in the spine XML node should fail with EpubPackageException")] public async Task ReadEpub2PackageWithEmptySpineTocTest() { await TestFailingReadOperation(EPUB2_OPF_FILE_WITH_EMPTY_SPINE_TOC); } [Fact(DisplayName = "Trying to read EPUB 2 OPF package with empty 'toc' attribute in the spine XML node and null PackageReaderOptions should fail with EpubPackageException")] public async Task ReadEpub2PackageWithEmptySpineTocWithNullPackageReaderOptionsTest() { await TestFailingReadOperationWithNullPackageReaderOptions(EPUB2_OPF_FILE_WITH_EMPTY_SPINE_TOC); } [Fact(DisplayName = "Trying to read EPUB 2 OPF package without 'toc' attribute in the spine XML node with IgnoreMissingToc = true should succeed")] public async Task ReadEpub2PackageWithoutSpineTocWithIgnoreMissingTocTest() { EpubReaderOptions epubReaderOptions = new() { PackageReaderOptions = new PackageReaderOptions() { IgnoreMissingToc = true } }; await TestSuccessfulReadOperation(EPUB2_OPF_FILE_WITHOUT_SPINE_TOC, Epub2PackageWithoutSpineToc, epubReaderOptions); } [Fact(DisplayName = "Trying to read OPF package without 'idref' attribute in a spine item ref XML node should fail with EpubPackageException")] public async Task ReadPackageWithoutSpineItemRefIdRefTest() { await TestFailingReadOperation(OPF_FILE_WITHOUT_IDREF_IN_SPINE_ITEMREF); } [Fact(DisplayName = "Trying to read OPF package without 'type' attribute in a guide reference XML node should fail with EpubPackageException")] public async Task ReadPackageWithoutGuideReferenceTypeTest() { await TestFailingReadOperation(OPF_FILE_WITHOUT_TYPE_IN_GUIDE_REFERENCE); } [Fact(DisplayName = "Trying to read OPF package without 'href' attribute in a guide reference XML node should fail with EpubPackageException")] public async Task ReadPackageWithoutGuideReferenceHrefTest() { await TestFailingReadOperation(OPF_FILE_WITHOUT_HREF_IN_GUIDE_REFERENCE); } [Fact(DisplayName = "Trying to read OPF package without 'role' attribute in a collection XML node should fail with EpubPackageException")] public async Task ReadPackageWithoutCollectionRoleTest() { await TestFailingReadOperation(OPF_FILE_WITHOUT_ROLE_IN_COLLECTION); } private static async Task TestSuccessfulReadOperation(string opfFileContent, EpubPackage expectedEpubPackage, EpubReaderOptions? epubReaderOptions = null) { TestZipFile testZipFile = CreateTestZipFileWithOpfFile(opfFileContent); PackageReader packageReader = new(epubReaderOptions ?? new EpubReaderOptions()); EpubPackage actualEpubPackage = await packageReader.ReadPackageAsync(testZipFile, OPF_FILE_PATH); EpubPackageComparer.CompareEpubPackages(expectedEpubPackage, actualEpubPackage); } private static Task TestSuccessfulReadOperationWithSkippingInvalidManifestItems(string opfFileContent, EpubPackage expectedEpubPackage) { EpubReaderOptions epubReaderOptions = new() { PackageReaderOptions = new PackageReaderOptions() { SkipInvalidManifestItems = true } }; return TestSuccessfulReadOperation(opfFileContent, expectedEpubPackage, epubReaderOptions); } private static async Task TestFailingReadOperation(string opfFileContent, EpubReaderOptions? epubReaderOptions = null) { TestZipFile testZipFile = CreateTestZipFileWithOpfFile(opfFileContent); PackageReader packageReader = new(epubReaderOptions ?? new EpubReaderOptions()); await Assert.ThrowsAsync(() => packageReader.ReadPackageAsync(testZipFile, OPF_FILE_PATH)); } private static Task TestFailingReadOperationWithNullPackageReaderOptions(string opfFileContent) { EpubReaderOptions epubReaderOptions = new() { PackageReaderOptions = null! }; return TestFailingReadOperation(opfFileContent, epubReaderOptions); } private static TestZipFile CreateTestZipFileWithOpfFile(string opfFileContent) { TestZipFile testZipFile = new(); testZipFile.AddEntry(CONTAINER_FILE_PATH, CONTAINER_FILE); testZipFile.AddEntry(OPF_FILE_PATH, opfFileContent); return testZipFile; } } }