@capgo/capacitor-updater 8.54.2 → 9.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CapgoCapacitorUpdater.podspec +2 -2
- package/Package.swift +3 -4
- package/README.md +162 -417
- package/android/build.gradle +7 -7
- package/android/consumer-rules.pro +48 -0
- package/android/proguard-rules.pro +1 -23
- package/android/src/main/java/ee/forgr/capacitor_updater/CapacitorUpdaterPlugin.java +1420 -404
- package/android/src/main/java/ee/forgr/capacitor_updater/CapgoUpdater.java +1100 -308
- package/android/src/main/java/ee/forgr/capacitor_updater/CryptoCipher.java +102 -65
- package/android/src/main/java/ee/forgr/capacitor_updater/DownloadService.java +155 -18
- package/android/src/main/java/ee/forgr/capacitor_updater/DownloadWorkerManager.java +22 -2
- package/android/src/main/java/ee/forgr/capacitor_updater/HeadlessUpdateWorker.java +82 -21
- package/android/src/main/java/ee/forgr/capacitor_updater/Http3.java +356 -0
- package/android/src/main/java/ee/forgr/capacitor_updater/HttpEngineTransport.java +553 -0
- package/dist/docs.json +44 -566
- package/dist/esm/definitions.d.ts +97 -215
- package/dist/esm/definitions.js.map +1 -1
- package/ios/Sources/CapacitorUpdaterPlugin/CapacitorUpdaterPlugin.swift +197 -84
- package/ios/Sources/CapacitorUpdaterPlugin/CapgoUpdater.swift +402 -139
- package/ios/Sources/CapacitorUpdaterPlugin/CryptoCipher.swift +81 -55
- package/ios/Sources/CapacitorUpdaterPlugin/RedirectPolicyDelegate.swift +24 -0
- package/ios/Sources/CapacitorUpdaterPlugin/ShakeMenu.swift +2 -1
- package/ios/Sources/CapacitorUpdaterPlugin/WKWebView+CapgoObjcBridge.swift +20 -0
- package/ios/Tests/CapacitorUpdaterPluginTests/BuiltinHashesTests.swift +210 -0
- package/ios/Tests/CapacitorUpdaterPluginTests/BundleHardeningTests.swift +292 -0
- package/ios/Tests/CapacitorUpdaterPluginTests/CapacitorUpdaterPluginTests.swift +3905 -0
- package/ios/Tests/CapacitorUpdaterPluginTests/CapgoSemanticVersionTests.swift +64 -0
- package/ios/Tests/CapacitorUpdaterPluginTests/ChecksumRequiredTests.swift +151 -0
- package/ios/Tests/CapacitorUpdaterPluginTests/CliCryptoFixtureTests.swift +249 -0
- package/ios/Tests/CapacitorUpdaterPluginTests/HttpsOnlyBuiltinMinimumTests.swift +528 -0
- package/ios/Tests/CapacitorUpdaterPluginTests/InstallPerfTests.swift +174 -0
- package/ios/Tests/CapacitorUpdaterPluginTests/ListChannelsTests.swift +74 -0
- package/ios/Tests/CapacitorUpdaterPluginTests/LoggerSecurityTests.swift +149 -0
- package/ios/Tests/CapacitorUpdaterPluginTests/NativeContractTests.swift +160 -0
- package/ios/Tests/CapacitorUpdaterPluginTests/NetworkTransportTests.swift +1095 -0
- package/ios/Tests/CapacitorUpdaterPluginTests/PopulateDeltaCacheTests.swift +573 -0
- package/ios/Tests/CapacitorUpdaterPluginTests/RedirectPolicyTests.swift +63 -0
- package/ios/Tests/CapacitorUpdaterPluginTests/RsaContractTests.swift +281 -0
- package/ios/Tests/CapacitorUpdaterPluginTests/SessionKeyRequiredTests.swift +192 -0
- package/ios/Tests/CapacitorUpdaterPluginTests/TestZipWriter.swift +235 -0
- package/ios/Tests/CapacitorUpdaterPluginTests/UpdateLifecycleTests.swift +454 -0
- package/ios/Tests/CapacitorUpdaterPluginTests/WebKitOverlayGuardTests.swift +32 -0
- package/ios/Tests/CapacitorUpdaterPluginTests/ZipArchiveReaderTests.swift +302 -0
- package/ios/Tests/CapacitorUpdaterPluginTests/ZipBundleExtractionTests.swift +122 -0
- package/package.json +15 -13
|
@@ -0,0 +1,292 @@
|
|
|
1
|
+
import CommonCrypto
|
|
2
|
+
import CryptoKit
|
|
3
|
+
import Foundation
|
|
4
|
+
import XCTest
|
|
5
|
+
@testable import CapacitorUpdaterPlugin
|
|
6
|
+
|
|
7
|
+
/// Regression tests for the bundle install gates: checksum before extraction, zip symlinks, strict AES.
|
|
8
|
+
final class BundleHardeningTests: XCTestCase {
|
|
9
|
+
private var root = URL(fileURLWithPath: NSTemporaryDirectory())
|
|
10
|
+
private let libraryDir = FileManager.default.urls(for: .libraryDirectory, in: .userDomainMask).first!
|
|
11
|
+
|
|
12
|
+
override func setUpWithError() throws {
|
|
13
|
+
root = FileManager.default.temporaryDirectory.appendingPathComponent("capgo-hardening-\(UUID().uuidString)")
|
|
14
|
+
try FileManager.default.createDirectory(at: root, withIntermediateDirectories: true)
|
|
15
|
+
// Zip downloads are staged in Documents, which a fresh simulator without a host app may not have yet.
|
|
16
|
+
if let documents = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first {
|
|
17
|
+
try FileManager.default.createDirectory(at: documents, withIntermediateDirectories: true)
|
|
18
|
+
}
|
|
19
|
+
CryptoCipher.setLogger(Logger(withTag: "hardening-tests", options: Logger.Options(level: .silent)))
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
override func tearDownWithError() throws {
|
|
23
|
+
try? FileManager.default.removeItem(at: root)
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
// MARK: - Checksum before extraction
|
|
27
|
+
|
|
28
|
+
/// Serves `payload` for every download and records whether extraction ran.
|
|
29
|
+
private final class LocalDownloadCapgoUpdater: CapgoUpdater {
|
|
30
|
+
var payload = Data()
|
|
31
|
+
var downloadRequests = 0
|
|
32
|
+
var extractionCalls = 0
|
|
33
|
+
var sentStatsActions: [String] = []
|
|
34
|
+
|
|
35
|
+
override func sendStats(action: String, versionName _: String? = nil, oldVersionName _: String? = "") {
|
|
36
|
+
sentStatsActions.append(action)
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
override func performDownloadRequest(
|
|
40
|
+
_ request: URLRequest,
|
|
41
|
+
label _: String,
|
|
42
|
+
resumeData _: Data? = nil,
|
|
43
|
+
onProgress _: ((Int64, Int64) -> Void)? = nil
|
|
44
|
+
) -> DownloadRequestResult {
|
|
45
|
+
downloadRequests += 1
|
|
46
|
+
let fileURL = FileManager.default.temporaryDirectory.appendingPathComponent("capgo-hardening-\(UUID().uuidString).zip")
|
|
47
|
+
do {
|
|
48
|
+
try payload.write(to: fileURL)
|
|
49
|
+
} catch {
|
|
50
|
+
return DownloadRequestResult(fileURL: nil, response: nil, error: error, timedOut: false)
|
|
51
|
+
}
|
|
52
|
+
let response = HTTPURLResponse(url: request.url ?? fileURL, statusCode: 200, httpVersion: "HTTP/1.1", headerFields: nil)
|
|
53
|
+
return DownloadRequestResult(fileURL: fileURL, response: response, error: nil, timedOut: false)
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
override func saveDownloaded(sourceZip: URL, id: String, base: URL, notify: Bool, bufferSize: Int = CryptoCipher.ioBufferBytes()) throws {
|
|
57
|
+
extractionCalls += 1
|
|
58
|
+
try super.saveDownloaded(sourceZip: sourceZip, id: id, base: base, notify: notify, bufferSize: bufferSize)
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
private func makeDownloadUpdater(payload: Data) -> LocalDownloadCapgoUpdater {
|
|
63
|
+
let updater = LocalDownloadCapgoUpdater()
|
|
64
|
+
updater.setLogger(Logger(withTag: "hardening-tests", options: Logger.Options(level: .silent)))
|
|
65
|
+
// Payloads are served from file:// URLs.
|
|
66
|
+
updater.httpsOnly = false
|
|
67
|
+
updater.payload = payload
|
|
68
|
+
return updater
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
private func bundleZipData() throws -> Data {
|
|
72
|
+
var writer = TestZipWriter()
|
|
73
|
+
writer.addFile("index.html", Data("<html>hardening</html>".utf8))
|
|
74
|
+
return writer.build()
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
private func sha256Hex(_ data: Data) -> String {
|
|
78
|
+
SHA256.hash(data: data).map { String(format: "%02x", $0) }.joined()
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
private func bundleDirectory(_ id: String) -> URL {
|
|
82
|
+
libraryDir.appendingPathComponent("NoCloud/ionic_built_snapshots").appendingPathComponent(id)
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
func testDownloadRejectsChecksumMismatchBeforeExtraction() throws {
|
|
86
|
+
let updater = makeDownloadUpdater(payload: try bundleZipData())
|
|
87
|
+
defer { updater.shutdown() }
|
|
88
|
+
// Never fetched: LocalDownloadCapgoUpdater serves the payload.
|
|
89
|
+
let url = root.appendingPathComponent("update.zip")
|
|
90
|
+
|
|
91
|
+
XCTAssertThrowsError(
|
|
92
|
+
try updater.downloadVerified(url: url, version: "9.9.9-mismatch", sessionKey: "", expectedChecksum: String(repeating: "a", count: 64))
|
|
93
|
+
) { error in
|
|
94
|
+
XCTAssertEqual(error as? ObjectSavableError, .checksum)
|
|
95
|
+
}
|
|
96
|
+
XCTAssertEqual(updater.downloadRequests, 1)
|
|
97
|
+
XCTAssertEqual(updater.extractionCalls, 0, "an archive that fails verification must never be extracted")
|
|
98
|
+
XCTAssertTrue(updater.sentStatsActions.contains("checksum_fail"))
|
|
99
|
+
XCTAssertNil(updater.getBundleInfoByVersionName(version: "9.9.9-mismatch"), "the rejected bundle is dropped so it can be downloaded again")
|
|
100
|
+
let leftovers = try FileManager.default.contentsOfDirectory(atPath: libraryDir.path).filter { $0.hasPrefix("capgo_unzip_") }
|
|
101
|
+
XCTAssertTrue(leftovers.isEmpty)
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
func testDownloadExtractsWhenChecksumMatches() throws {
|
|
105
|
+
let zip = try bundleZipData()
|
|
106
|
+
let updater = makeDownloadUpdater(payload: zip)
|
|
107
|
+
defer { updater.shutdown() }
|
|
108
|
+
// Never fetched: LocalDownloadCapgoUpdater serves the payload.
|
|
109
|
+
let url = root.appendingPathComponent("update.zip")
|
|
110
|
+
|
|
111
|
+
let bundle = try updater.downloadVerified(url: url, version: "9.9.9-match", sessionKey: "", expectedChecksum: sha256Hex(zip))
|
|
112
|
+
defer { _ = updater.delete(id: bundle.getId()) }
|
|
113
|
+
|
|
114
|
+
XCTAssertEqual(updater.extractionCalls, 1)
|
|
115
|
+
XCTAssertEqual(bundle.getChecksum(), sha256Hex(zip))
|
|
116
|
+
XCTAssertFalse(updater.sentStatsActions.contains("checksum_fail"))
|
|
117
|
+
XCTAssertTrue(FileManager.default.fileExists(atPath: bundleDirectory(bundle.getId()).appendingPathComponent("index.html").path))
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
func testDownloadRequiresChecksumWhenExpectedChecksumIsEmpty() throws {
|
|
121
|
+
// The shake menu passes the update response checksum; an empty one must stop before any download.
|
|
122
|
+
let updater = makeDownloadUpdater(payload: try bundleZipData())
|
|
123
|
+
defer { updater.shutdown() }
|
|
124
|
+
// Never fetched: LocalDownloadCapgoUpdater serves the payload.
|
|
125
|
+
let url = root.appendingPathComponent("update.zip")
|
|
126
|
+
|
|
127
|
+
XCTAssertThrowsError(try updater.downloadVerified(url: url, version: "9.9.9-empty", sessionKey: "", expectedChecksum: ""))
|
|
128
|
+
XCTAssertEqual(updater.downloadRequests, 0)
|
|
129
|
+
XCTAssertEqual(updater.extractionCalls, 0)
|
|
130
|
+
XCTAssertTrue(updater.sentStatsActions.contains("checksum_required"))
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
// MARK: - Zip symlinks
|
|
134
|
+
|
|
135
|
+
private func makeUpdater() -> CapgoUpdater {
|
|
136
|
+
let updater = CapgoUpdater()
|
|
137
|
+
updater.setLogger(Logger(withTag: "hardening-tests", options: Logger.Options(level: .silent)))
|
|
138
|
+
return updater
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
func testSaveDownloadedRejectsSymlinkChainEscape() throws {
|
|
142
|
+
// m -> "." is harmless alone, but "l -> m/.." normalizes to the folder while physically pointing at its parent.
|
|
143
|
+
let escapeName = "capgo-escape-\(UUID().uuidString)"
|
|
144
|
+
var writer = TestZipWriter()
|
|
145
|
+
writer.addFile("index.html", Data("<html></html>".utf8))
|
|
146
|
+
writer.addSymlink("m", target: ".")
|
|
147
|
+
writer.addSymlink("l", target: "m/..")
|
|
148
|
+
writer.addFile("l/\(escapeName)", Data("owned".utf8))
|
|
149
|
+
let zipURL = root.appendingPathComponent("chain.zip")
|
|
150
|
+
try writer.write(to: zipURL)
|
|
151
|
+
let base = root.appendingPathComponent("bundles")
|
|
152
|
+
|
|
153
|
+
XCTAssertThrowsError(try makeUpdater().saveDownloaded(sourceZip: zipURL, id: "evil", base: base, notify: false))
|
|
154
|
+
|
|
155
|
+
XCTAssertFalse(FileManager.default.fileExists(atPath: libraryDir.appendingPathComponent(escapeName).path))
|
|
156
|
+
XCTAssertFalse(FileManager.default.fileExists(atPath: base.appendingPathComponent("evil").path))
|
|
157
|
+
let leftovers = try FileManager.default.contentsOfDirectory(atPath: libraryDir.path).filter { $0.hasPrefix("capgo_unzip_") }
|
|
158
|
+
XCTAssertTrue(leftovers.isEmpty, "the temp unzip folder is removed on failure")
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
func testSaveDownloadedRejectsSymlinkTargetsWithParentSegmentsOrAbsolutePaths() throws {
|
|
162
|
+
let targets = ["../index.html", "a/../b", "..", "/", "/tmp", "//etc"]
|
|
163
|
+
for (index, target) in targets.enumerated() {
|
|
164
|
+
var writer = TestZipWriter()
|
|
165
|
+
writer.addFile("index.html", Data("<html></html>".utf8))
|
|
166
|
+
writer.addSymlink("assets/link", target: target)
|
|
167
|
+
let zipURL = root.appendingPathComponent("target-\(index).zip")
|
|
168
|
+
try writer.write(to: zipURL)
|
|
169
|
+
let base = root.appendingPathComponent("target-bundles-\(index)")
|
|
170
|
+
XCTAssertThrowsError(
|
|
171
|
+
try makeUpdater().saveDownloaded(sourceZip: zipURL, id: "evil", base: base, notify: false),
|
|
172
|
+
"symlink target \(target) must be rejected"
|
|
173
|
+
)
|
|
174
|
+
XCTAssertFalse(FileManager.default.fileExists(atPath: base.appendingPathComponent("evil").path))
|
|
175
|
+
}
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
func testSaveDownloadedKeepsSafeRelativeSymlinks() throws {
|
|
179
|
+
var writer = TestZipWriter()
|
|
180
|
+
writer.addFile("index.html", Data("<html></html>".utf8))
|
|
181
|
+
writer.addSymlink("m", target: ".")
|
|
182
|
+
writer.addFile("assets/app.js", Data("app".utf8))
|
|
183
|
+
writer.addSymlink("alias", target: "assets/app.js")
|
|
184
|
+
let zipURL = root.appendingPathComponent("safe.zip")
|
|
185
|
+
try writer.write(to: zipURL)
|
|
186
|
+
let base = root.appendingPathComponent("safe-bundles")
|
|
187
|
+
|
|
188
|
+
try makeUpdater().saveDownloaded(sourceZip: zipURL, id: "ok", base: base, notify: false)
|
|
189
|
+
|
|
190
|
+
let bundle = base.appendingPathComponent("ok")
|
|
191
|
+
XCTAssertEqual(try FileManager.default.destinationOfSymbolicLink(atPath: bundle.appendingPathComponent("alias").path), "assets/app.js")
|
|
192
|
+
XCTAssertEqual(try Data(contentsOf: bundle.appendingPathComponent("alias")), Data("app".utf8))
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
func testPhysicalContainmentCheckFollowsSymlinks() throws {
|
|
196
|
+
let inside = root.appendingPathComponent("inside")
|
|
197
|
+
try FileManager.default.createDirectory(at: inside, withIntermediateDirectories: true)
|
|
198
|
+
try FileManager.default.createSymbolicLink(atPath: inside.appendingPathComponent("up").path, withDestinationPath: "..")
|
|
199
|
+
|
|
200
|
+
XCTAssertNoThrow(try CapgoUpdater.assertPhysicallyInsideDirectory(inside.appendingPathComponent("a/b"), root: inside))
|
|
201
|
+
XCTAssertThrowsError(try CapgoUpdater.assertPhysicallyInsideDirectory(inside.appendingPathComponent("up/x"), root: inside))
|
|
202
|
+
XCTAssertThrowsError(try CapgoUpdater.assertPhysicallyInsideDirectory(inside.appendingPathComponent("up"), root: inside))
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
// MARK: - AES strictness
|
|
206
|
+
|
|
207
|
+
private let key = Data((0..<16).map { UInt8($0 * 7 & 0xff) })
|
|
208
|
+
private let iv = Data((0..<16).map { UInt8($0 + 3) })
|
|
209
|
+
|
|
210
|
+
private var aesKey: AES128Key {
|
|
211
|
+
AES128Key(iv: iv, aes128Key: key, logger: Logger(withTag: "hardening-tests", options: Logger.Options(level: .silent)))
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
/// Reference AES-128-CBC decryption without any padding handling.
|
|
215
|
+
private func rawCbcDecrypt(_ ciphertext: Data, iv chainingValue: Data) -> Data {
|
|
216
|
+
let reference = AES128Key(iv: chainingValue, aes128Key: key, logger: Logger(withTag: "hardening-tests", options: Logger.Options(level: .silent)))
|
|
217
|
+
guard let plain = reference.decryptBlocks(ciphertext) else {
|
|
218
|
+
XCTFail("reference AES decryption failed")
|
|
219
|
+
return Data()
|
|
220
|
+
}
|
|
221
|
+
return plain
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
/// Builds a `blocks`-long CBC ciphertext whose last block decrypts to `lastPlainBlock`, by choosing the
|
|
225
|
+
/// previous ciphertext block (CBC: P[n] = D(C[n]) xor C[n-1]). No encryption is involved.
|
|
226
|
+
private func makeCiphertext(blocks: Int, lastPlainBlock: [UInt8]) -> Data {
|
|
227
|
+
precondition(blocks >= 2 && lastPlainBlock.count == kCCBlockSizeAES128)
|
|
228
|
+
let blockSize = kCCBlockSizeAES128
|
|
229
|
+
var bytes = (0..<(blocks * blockSize)).map { UInt8(($0 * 37 + 11) & 0xff) }
|
|
230
|
+
let lastBlock = Data(bytes.suffix(blockSize))
|
|
231
|
+
let decryptedLast = [UInt8](rawCbcDecrypt(lastBlock, iv: Data(count: blockSize)))
|
|
232
|
+
for index in 0..<blockSize {
|
|
233
|
+
bytes[(blocks - 2) * blockSize + index] = decryptedLast[index] ^ lastPlainBlock[index]
|
|
234
|
+
}
|
|
235
|
+
return Data(bytes)
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
private func block(filler: UInt8 = 0x41, tail: [UInt8]) -> [UInt8] {
|
|
239
|
+
[UInt8](repeating: filler, count: kCCBlockSizeAES128 - tail.count) + tail
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
func testAesFileDecryptionRoundTripsAcrossChunks() throws {
|
|
243
|
+
// Larger than two I/O buffers so the held-back padding block crosses chunk boundaries.
|
|
244
|
+
let blocks = (CryptoCipher.ioBufferBytes() * 2) / kCCBlockSizeAES128 + 3
|
|
245
|
+
let ciphertext = makeCiphertext(blocks: blocks, lastPlainBlock: block(tail: [UInt8](repeating: 5, count: 5)))
|
|
246
|
+
let expected = rawCbcDecrypt(ciphertext, iv: iv).dropLast(5)
|
|
247
|
+
let file = root.appendingPathComponent("round-trip.bin")
|
|
248
|
+
try ciphertext.write(to: file)
|
|
249
|
+
|
|
250
|
+
try aesKey.decrypt(from: file, to: file)
|
|
251
|
+
|
|
252
|
+
XCTAssertEqual(try Data(contentsOf: file), Data(expected))
|
|
253
|
+
let fullPadding = makeCiphertext(blocks: 2, lastPlainBlock: [UInt8](repeating: 16, count: 16))
|
|
254
|
+
XCTAssertEqual(aesKey.decrypt(data: fullPadding), rawCbcDecrypt(fullPadding, iv: iv).prefix(16))
|
|
255
|
+
}
|
|
256
|
+
|
|
257
|
+
func testAesFileDecryptionRejectsCorruptedCiphertext() throws {
|
|
258
|
+
let valid = makeCiphertext(blocks: 2, lastPlainBlock: block(tail: [3, 3, 3]))
|
|
259
|
+
let cases: [(String, Data)] = [
|
|
260
|
+
("bad padding", makeCiphertext(blocks: 2, lastPlainBlock: block(tail: [1, 4, 4, 4]))),
|
|
261
|
+
("zero padding", makeCiphertext(blocks: 2, lastPlainBlock: block(tail: [0]))),
|
|
262
|
+
("padding > block", makeCiphertext(blocks: 2, lastPlainBlock: block(tail: [17]))),
|
|
263
|
+
("not block aligned", valid + Data([1, 2, 3])),
|
|
264
|
+
("truncated", valid.prefix(10))
|
|
265
|
+
]
|
|
266
|
+
XCTAssertNotNil(aesKey.decrypt(data: valid), "the reference ciphertext is valid")
|
|
267
|
+
for (name, ciphertext) in cases {
|
|
268
|
+
let file = root.appendingPathComponent("corrupt-\(UUID().uuidString).bin")
|
|
269
|
+
try ciphertext.write(to: file)
|
|
270
|
+
XCTAssertThrowsError(try aesKey.decrypt(from: file, to: file), name)
|
|
271
|
+
XCTAssertEqual(try Data(contentsOf: file), ciphertext, "\(name): the source is left untouched")
|
|
272
|
+
XCTAssertNil(aesKey.decrypt(data: ciphertext), name)
|
|
273
|
+
}
|
|
274
|
+
}
|
|
275
|
+
|
|
276
|
+
func testDecryptFileFailsWhenPublicKeyIsUnusable() throws {
|
|
277
|
+
let file = root.appendingPathComponent("encrypted.bin")
|
|
278
|
+
let ciphertext = makeCiphertext(blocks: 2, lastPlainBlock: block(tail: [1]))
|
|
279
|
+
try ciphertext.write(to: file)
|
|
280
|
+
|
|
281
|
+
XCTAssertThrowsError(
|
|
282
|
+
try CryptoCipher.decryptFile(
|
|
283
|
+
filePath: file,
|
|
284
|
+
publicKey: "not-a-public-key",
|
|
285
|
+
sessionKey: "aXY=:c2Vzc2lvbg==",
|
|
286
|
+
version: "1.0.0",
|
|
287
|
+
expectedCiphertextSha256: CryptoCipher.calcChecksum(filePath: file)
|
|
288
|
+
)
|
|
289
|
+
)
|
|
290
|
+
XCTAssertEqual(try Data(contentsOf: file), ciphertext)
|
|
291
|
+
}
|
|
292
|
+
}
|