|
| 1 | +package org.readium.r2.streamer.parser |
| 2 | + |
| 3 | +import kotlinx.coroutines.runBlocking |
| 4 | +import org.junit.Assert.* |
| 5 | +import org.junit.Test |
| 6 | +import org.junit.runner.RunWith |
| 7 | +import org.readium.r2.shared.fetcher.FailureResource |
| 8 | +import org.readium.r2.shared.fetcher.Fetcher |
| 9 | +import org.readium.r2.shared.fetcher.Resource |
| 10 | +import org.readium.r2.shared.fetcher.StringResource |
| 11 | +import org.readium.r2.shared.publication.ContentProtection.Scheme |
| 12 | +import org.readium.r2.shared.publication.Link |
| 13 | +import org.readium.r2.shared.util.mediatype.MediaType |
| 14 | +import org.robolectric.RobolectricTestRunner |
| 15 | + |
| 16 | +@RunWith(RobolectricTestRunner::class) |
| 17 | +class FallbackContentProtectionTest { |
| 18 | + |
| 19 | + @Test |
| 20 | + fun `Sniff no content protection`() { |
| 21 | + assertNull(sniff(mediaType = MediaType.EPUB, resources = emptyMap())) |
| 22 | + } |
| 23 | + |
| 24 | + @Test |
| 25 | + fun `Sniff EPUB with empty encryption xml`() { |
| 26 | + assertNull(sniff(mediaType = MediaType.EPUB, resources = mapOf( |
| 27 | + "/META-INF/encryption.xml" to """<?xml version='1.0' encoding='utf-8'?><encryption xmlns="urn:oasis:names:tc:opendocument:xmlns:container" xmlns:enc="http://www.w3.org/2001/04/xmlenc#"></encryption>""" |
| 28 | + ))) |
| 29 | + } |
| 30 | + |
| 31 | + @Test |
| 32 | + fun `Sniff LCP protected package`() { |
| 33 | + assertEquals(Scheme.Lcp, sniff( |
| 34 | + mediaType = MediaType.ZIP, |
| 35 | + resources = mapOf( |
| 36 | + "/license.lcpl" to "{}" |
| 37 | + ) |
| 38 | + )) |
| 39 | + } |
| 40 | + |
| 41 | + @Test |
| 42 | + fun `Sniff LCP protected EPUB`() { |
| 43 | + assertEquals(Scheme.Lcp, sniff( |
| 44 | + mediaType = MediaType.EPUB, |
| 45 | + resources = mapOf( |
| 46 | + "/META-INF/license.lcpl" to "{}" |
| 47 | + ) |
| 48 | + )) |
| 49 | + } |
| 50 | + |
| 51 | + @Test |
| 52 | + fun `Sniff LCP protected EPUB missing the license`() { |
| 53 | + assertEquals(Scheme.Lcp, sniff( |
| 54 | + mediaType = MediaType.EPUB, |
| 55 | + resources = mapOf( |
| 56 | + "/META-INF/encryption.xml" to """<?xml version="1.0" encoding="UTF-8"?> |
| 57 | +<encryption xmlns="urn:oasis:names:tc:opendocument:xmlns:container"> |
| 58 | + <EncryptedData xmlns="http://www.w3.org/2001/04/xmlenc#"> |
| 59 | + <EncryptionMethod xmlns="http://www.w3.org/2001/04/xmlenc#" Algorithm="http://www.w3.org/2001/04/xmlenc#aes256-cbc"></EncryptionMethod> |
| 60 | + <KeyInfo xmlns="http://www.w3.org/2000/09/xmldsig#"> |
| 61 | + <RetrievalMethod xmlns="http://www.w3.org/2000/09/xmldsig#" URI="license.lcpl#/encryption/content_key" Type="http://readium.org/2014/01/lcp#EncryptedContentKey"></RetrievalMethod> |
| 62 | + </KeyInfo> |
| 63 | + <CipherData xmlns="http://www.w3.org/2001/04/xmlenc#"> |
| 64 | + <CipherReference xmlns="http://www.w3.org/2001/04/xmlenc#" URI="OPS/chapter_001.xhtml"></CipherReference> |
| 65 | + </CipherData> |
| 66 | + <EncryptionProperties xmlns="http://www.w3.org/2001/04/xmlenc#"> |
| 67 | + <EncryptionProperty xmlns="http://www.w3.org/2001/04/xmlenc#"> |
| 68 | + <Compression xmlns="http://www.idpf.org/2016/encryption#compression" Method="8" OriginalLength="13877"></Compression> |
| 69 | + </EncryptionProperty> |
| 70 | + </EncryptionProperties> |
| 71 | + </EncryptedData> |
| 72 | +</encryption>""" |
| 73 | + ) |
| 74 | + )) |
| 75 | + } |
| 76 | + |
| 77 | + @Test |
| 78 | + fun `Sniff Adobe ADEPT`() { |
| 79 | + assertEquals(Scheme.Adept, sniff( |
| 80 | + mediaType = MediaType.EPUB, |
| 81 | + resources = mapOf( |
| 82 | + "/META-INF/encryption.xml" to """<?xml version="1.0"?><encryption xmlns="urn:oasis:names:tc:opendocument:xmlns:container"> |
| 83 | + <EncryptedData xmlns="http://www.w3.org/2001/04/xmlenc#"> |
| 84 | + <EncryptionMethod Algorithm="http://www.w3.org/2001/04/xmlenc#aes128-cbc"></EncryptionMethod> |
| 85 | + <KeyInfo xmlns="http://www.w3.org/2000/09/xmldsig#"> |
| 86 | + <resource xmlns="http://ns.adobe.com/adept">urn:uuid:2c43729c-b985-4531-8e86-ae75ce5e5da9</resource> |
| 87 | + </KeyInfo> |
| 88 | + <CipherData> |
| 89 | + <CipherReference URI="OEBPS/stylesheet.css"></CipherReference> |
| 90 | + </CipherData> |
| 91 | + </EncryptedData> |
| 92 | + </encryption>""" |
| 93 | + ) |
| 94 | + )) |
| 95 | + } |
| 96 | + |
| 97 | + @Test |
| 98 | + fun `Sniff Adobe ADEPT from rights xml`() { |
| 99 | + assertEquals(Scheme.Adept, sniff( |
| 100 | + mediaType = MediaType.EPUB, |
| 101 | + resources = mapOf( |
| 102 | + "/META-INF/encryption.xml" to """<?xml version='1.0' encoding='utf-8'?><encryption xmlns="urn:oasis:names:tc:opendocument:xmlns:container" xmlns:enc="http://www.w3.org/2001/04/xmlenc#"></encryption>""", |
| 103 | + "/META-INF/rights.xml" to """<?xml version="1.0"?><adept:rights xmlns:adept="http://ns.adobe.com/adept"></adept:rights>""" |
| 104 | + ) |
| 105 | + )) |
| 106 | + } |
| 107 | + |
| 108 | + private fun sniff(mediaType: MediaType, resources: Map<String, String>): Scheme? = runBlocking { |
| 109 | + FallbackContentProtection().sniffScheme( |
| 110 | + fetcher = TestFetcher(resources), |
| 111 | + mediaType = mediaType |
| 112 | + ) |
| 113 | + } |
| 114 | +} |
| 115 | + |
| 116 | +class TestFetcher(private val resources: Map<String, String> = emptyMap()) : Fetcher { |
| 117 | + |
| 118 | + override suspend fun links(): List<Link> = resources.map { Link(href = it.key) } |
| 119 | + |
| 120 | + override fun get(link: Link): Resource = |
| 121 | + resources[link.href]?.let { StringResource(link, it) } |
| 122 | + ?: FailureResource(link, Resource.Exception.NotFound()) |
| 123 | + |
| 124 | + override suspend fun close() {} |
| 125 | +} |
0 commit comments