|
| 1 | +package epub |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "errors" |
| 6 | + "testing" |
| 7 | + |
| 8 | + "github.com/readium/go-toolkit/pkg/asset" |
| 9 | + "github.com/readium/go-toolkit/pkg/fetcher" |
| 10 | + "github.com/readium/go-toolkit/pkg/mediatype" |
| 11 | + "github.com/readium/go-toolkit/pkg/protection" |
| 12 | + "github.com/stretchr/testify/assert" |
| 13 | + "github.com/stretchr/testify/require" |
| 14 | +) |
| 15 | + |
| 16 | +// fakeEPUBAsset is a minimal [asset.PublicationAsset] for parser tests. |
| 17 | +// CreateFetcher is never invoked because the tests construct the fetcher |
| 18 | +// themselves and pass it to [Parser.Parse] directly. |
| 19 | +type fakeEPUBAsset struct{ name string } |
| 20 | + |
| 21 | +func (a fakeEPUBAsset) Name() string { return a.name } |
| 22 | +func (a fakeEPUBAsset) MediaType(context.Context) mediatype.MediaType { return mediatype.EPUB } |
| 23 | +func (a fakeEPUBAsset) CreateFetcher(context.Context, asset.Dependencies, string) (fetcher.Fetcher, error) { |
| 24 | + return nil, errors.New("unused in tests") |
| 25 | +} |
| 26 | + |
| 27 | +func openProtectionFixture(t *testing.T, file string) *fetcher.ArchiveFetcher { |
| 28 | + t.Helper() |
| 29 | + f, err := fetcher.NewArchiveFetcherFromPath(t.Context(), "../../protection/testdata/"+file) |
| 30 | + require.NoError(t, err) |
| 31 | + t.Cleanup(f.Close) |
| 32 | + return f |
| 33 | +} |
| 34 | + |
| 35 | +// TestParserEndToEnd exercises [Parser.Parse] against every DRM fixture in |
| 36 | +// pkg/protection/testdata, asserting the parse succeeds and surfaces the |
| 37 | +// expected publication metadata. expectedScheme is the manifest.Encryption |
| 38 | +// scheme URI that every encrypted resource should carry — empty means either |
| 39 | +// no encryption.xml is present (so no resource should be tagged) or the |
| 40 | +// encryption is generic (no DRM scheme attached). |
| 41 | +func TestParserEndToEnd(t *testing.T) { |
| 42 | + for _, tt := range []struct { |
| 43 | + name string |
| 44 | + file string |
| 45 | + title string |
| 46 | + readingOrderSize int |
| 47 | + hasEncryptionXML bool |
| 48 | + expectedScheme string |
| 49 | + }{ |
| 50 | + {"Adobe ADEPT", "fake-adept.epub", "Fake Adept DRM", 1, false, ""}, |
| 51 | + {"Barnes & Noble", "fake-bn.epub", "Fake B&N DRM", 1, false, ""}, |
| 52 | + {"Apple FairPlay", "fake-fairplay.epub", "Fake Fairplay DRM", 1, false, ""}, |
| 53 | + {"Kobo", "fake-kobo.epub", "Fake Kobo DRM", 1, false, ""}, |
| 54 | + {"Readium LCP", "fake-lcp.epub", "The Level 999 Villager Chapter 3", 35, true, protection.SchemeLCP}, |
| 55 | + {"Generic encryption (Yahoo)", "yahoo.ypub", "週刊少年マガジン 2019年8号[2019年1月23日発売]", 540, true, ""}, |
| 56 | + } { |
| 57 | + t.Run(tt.name, func(t *testing.T) { |
| 58 | + f := openProtectionFixture(t, tt.file) |
| 59 | + builder, err := NewParser(nil).Parse(t.Context(), fakeEPUBAsset{name: tt.file}, f) |
| 60 | + require.NoError(t, err) |
| 61 | + require.NotNil(t, builder) |
| 62 | + |
| 63 | + m := builder.Manifest |
| 64 | + assert.Equal(t, tt.title, m.Metadata.Title()) |
| 65 | + assert.Lenf(t, m.ReadingOrder, tt.readingOrderSize, |
| 66 | + "expected reading order size %d, got %d", tt.readingOrderSize, len(m.ReadingOrder)) |
| 67 | + |
| 68 | + encryptedCount := 0 |
| 69 | + for _, link := range append(m.ReadingOrder, m.Resources...) { |
| 70 | + enc, ok := link.Properties["encrypted"].(map[string]interface{}) |
| 71 | + if !ok { |
| 72 | + continue |
| 73 | + } |
| 74 | + encryptedCount++ |
| 75 | + if tt.expectedScheme == "" { |
| 76 | + _, hasScheme := enc["scheme"] |
| 77 | + assert.Falsef(t, hasScheme, "%s should not have a scheme", link.Href.String()) |
| 78 | + } else { |
| 79 | + assert.Equalf(t, tt.expectedScheme, enc["scheme"], "scheme mismatch for %s", link.Href.String()) |
| 80 | + } |
| 81 | + } |
| 82 | + if tt.hasEncryptionXML { |
| 83 | + assert.Greater(t, encryptedCount, 0, "expected at least one resource to carry encryption properties") |
| 84 | + } else { |
| 85 | + assert.Zero(t, encryptedCount, "no resource should carry encryption properties") |
| 86 | + } |
| 87 | + }) |
| 88 | + } |
| 89 | +} |
| 90 | + |
| 91 | +// TestParseEncryptionDataScheme exercises the same two-step the Parser uses: |
| 92 | +// [protection.IdentifyEPUBProtection] followed by parseEncryptionData, and |
| 93 | +// verifies the detected scheme reaches every encryption.xml entry. |
| 94 | +func TestParseEncryptionDataScheme(t *testing.T) { |
| 95 | + for _, tt := range []struct { |
| 96 | + name string |
| 97 | + file string |
| 98 | + expectScheme string |
| 99 | + expectEntries bool |
| 100 | + }{ |
| 101 | + {"Readium LCP", "fake-lcp.epub", protection.SchemeLCP, true}, |
| 102 | + {"Generic encryption (Yahoo)", "yahoo.ypub", "", true}, |
| 103 | + // EPUBs without META-INF/encryption.xml yield no entries at all. |
| 104 | + {"Adobe ADEPT (no encryption.xml)", "fake-adept.epub", "", false}, |
| 105 | + {"Kobo (no encryption.xml)", "fake-kobo.epub", "", false}, |
| 106 | + } { |
| 107 | + t.Run(tt.name, func(t *testing.T) { |
| 108 | + f := openProtectionFixture(t, tt.file) |
| 109 | + |
| 110 | + scheme, err := protection.IdentifyEPUBProtection(t.Context(), f) |
| 111 | + require.NoError(t, err) |
| 112 | + |
| 113 | + enc, err := parseEncryptionData(t.Context(), f, scheme.URI()) |
| 114 | + require.NoError(t, err) |
| 115 | + if !tt.expectEntries { |
| 116 | + assert.Empty(t, enc) |
| 117 | + return |
| 118 | + } |
| 119 | + require.NotEmpty(t, enc) |
| 120 | + for u, e := range enc { |
| 121 | + assert.Equalf(t, tt.expectScheme, e.Scheme, "scheme mismatch for %s", u) |
| 122 | + } |
| 123 | + }) |
| 124 | + } |
| 125 | +} |
0 commit comments