This repository has been archived by the owner on Aug 12, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #94 from readium/fix/lcp-decryptor
Fix LCP decryption of ranges
- Loading branch information
Showing
10 changed files
with
381 additions
and
91 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
83 changes: 83 additions & 0 deletions
83
readium-lcp-swiftTests/Content Protection/LCPDecryptorTests.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
// | ||
// Copyright 2020 Readium Foundation. All rights reserved. | ||
// Use of this source code is governed by the BSD-style license | ||
// available in the top-level LICENSE file of the project. | ||
// | ||
|
||
import XCTest | ||
import R2Shared | ||
import PDFKit | ||
@testable import ReadiumLCP | ||
|
||
class LCPDecryptorTests: XCTestCase { | ||
|
||
let fixtures = Fixtures() | ||
var service: LCPService! | ||
var encryptedResource: Resource! | ||
var clearData: Data! | ||
|
||
override func setUpWithError() throws { | ||
service = R2MakeLCPService() | ||
|
||
let fetcher = ArchiveFetcher(archive: try DefaultArchiveFactory().open(url: self.fixtures.url(for: "daisy.lcpdf"), password: nil)) | ||
encryptedResource = fetcher.get(Link( | ||
href: "/publication.pdf", | ||
properties: Properties([ | ||
"encrypted": [ | ||
"scheme": "http://readium.org/2014/01/lcp", | ||
"profile": "http://readium.org/lcp/basic-profile", | ||
"algorithm": "http://www.w3.org/2001/04/xmlenc#aes256-cbc" | ||
] | ||
]) | ||
)) | ||
|
||
clearData = try Data(contentsOf: fixtures.url(for: "daisy.pdf")) | ||
} | ||
|
||
/// Checks that we can decrypt the full content successfully. | ||
func testDecryptFull() throws { | ||
retrieveLicense(path: "daisy.lcpdf", passphrase: "test") { license in | ||
let decryptedResource = LCPDecryptor(license: license).decrypt(resource: self.encryptedResource) | ||
|
||
XCTAssertEqual(try decryptedResource.read().get(), self.clearData) | ||
} | ||
} | ||
|
||
/// Checks that we can decrypt various ranges successfully. | ||
func testDecryptRanges() throws { | ||
retrieveLicense(path: "daisy.lcpdf", passphrase: "test") { license in | ||
let decryptedResource = LCPDecryptor(license: license).decrypt(resource: self.encryptedResource) | ||
|
||
// These ranges seem arbirtrary, but some of them were failing before the fix in the | ||
// same commit. | ||
let ranges: [Range<UInt64>] = [ | ||
0..<2048, // 2048 | ||
817152..<819200, // 2048 | ||
819200..<819856, // 656 | ||
0..<16384, // 16384 | ||
819792..<819856, // 64 | ||
819565..<819856 // 291 | ||
] | ||
|
||
for range in ranges { | ||
let intRange = Int(range.lowerBound)..<Int(range.upperBound) | ||
let decrypted = try decryptedResource.read(range: range).get() | ||
let clear = self.clearData[intRange] | ||
XCTAssertEqual(decrypted, clear, "Failed to decrypt range \(intRange)") | ||
} | ||
} | ||
} | ||
|
||
private func retrieveLicense(path: String, passphrase: String, completion: @escaping (LCPLicense) throws -> Void) { | ||
let completionExpectation = expectation(description: "License opened") | ||
|
||
let url = fixtures.url(for: path) | ||
service.retrieveLicense(from: url, authentication: LCPPassphrase(passphrase)) { result in | ||
try! completion((try! result.get())!) | ||
completionExpectation.fulfill() | ||
} | ||
|
||
waitForExpectations(timeout: 10, handler: nil) | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
// | ||
// Copyright 2020 Readium Foundation. All rights reserved. | ||
// Use of this source code is governed by the BSD-style license | ||
// available in the top-level LICENSE file of the project. | ||
// | ||
|
||
import Foundation | ||
import XCTest | ||
|
||
class Fixtures { | ||
|
||
let path: String? | ||
|
||
init(path: String? = nil) { | ||
self.path = path | ||
} | ||
|
||
private lazy var bundle = Bundle(for: type(of: self)) | ||
|
||
func url(for filepath: String) -> URL { | ||
return try! XCTUnwrap(bundle.resourceURL?.appendingPathComponent("Fixtures/\(path ?? "")/\(filepath)")) | ||
} | ||
|
||
func data(at filepath: String) -> Data { | ||
return try! XCTUnwrap(try? Data(contentsOf: url(for: filepath))) | ||
} | ||
|
||
func json<T>(at filepath: String) -> T { | ||
return try! XCTUnwrap(JSONSerialization.jsonObject(with: data(at: filepath)) as? T) | ||
} | ||
|
||
} |
Binary file not shown.
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 changes: 4 additions & 0 deletions
4
readium-lcp-swiftTests/readium-lcp-swiftTests-Bridging-Header.h
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
// | ||
// Use this file to import your target's public headers that you would like to expose to Swift. | ||
// | ||
|
This file was deleted.
Oops, something went wrong.