@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,573 @@
|
|
|
1
|
+
import Foundation
|
|
2
|
+
import XCTest
|
|
3
|
+
@testable import CapacitorUpdaterPlugin
|
|
4
|
+
|
|
5
|
+
/// Overrides builtinFolderURL() with a writable temp directory — the real app
|
|
6
|
+
/// bundle (Bundle.main) is read-only at test-runtime, so tests can't write
|
|
7
|
+
/// fixture files there directly.
|
|
8
|
+
private final class TestableCapgoUpdater: CapgoUpdater {
|
|
9
|
+
var builtinFolderOverride = FileManager.default.temporaryDirectory.appendingPathComponent("populate-delta-cache-builtin-\(UUID().uuidString)")
|
|
10
|
+
|
|
11
|
+
override func builtinFolderURL() -> URL {
|
|
12
|
+
builtinFolderOverride
|
|
13
|
+
}
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
final class PopulateDeltaCacheTests: XCTestCase {
|
|
17
|
+
private var implementation: TestableCapgoUpdater!
|
|
18
|
+
private var bundleId: String!
|
|
19
|
+
private var bundleDir: URL!
|
|
20
|
+
private var registeredCacheFiles: [URL] = []
|
|
21
|
+
private let cacheFolder = FileManager.default.urls(for: .cachesDirectory, in: .userDomainMask).first!.appendingPathComponent("capgo_downloads")
|
|
22
|
+
private var builtinFolder: URL {
|
|
23
|
+
implementation.builtinFolderOverride
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
// Loaded from the same RSA fixture native-contract-tests/crypto-rsa.json uses
|
|
27
|
+
// (see RsaContractTests.swift), so the encrypted-manifest path is exercised
|
|
28
|
+
// against real contract data rather than an invented key/ciphertext pair.
|
|
29
|
+
private enum Fixture {
|
|
30
|
+
static let contract: [String: Any] = {
|
|
31
|
+
guard let url = try? locateContractFile(),
|
|
32
|
+
let data = try? Data(contentsOf: url),
|
|
33
|
+
let value = try? JSONSerialization.jsonObject(with: data),
|
|
34
|
+
let dict = value as? [String: Any] else {
|
|
35
|
+
XCTFail("Unable to load RSA contract fixture")
|
|
36
|
+
return [:]
|
|
37
|
+
}
|
|
38
|
+
return dict
|
|
39
|
+
}()
|
|
40
|
+
|
|
41
|
+
static var publicKeyPem: String {
|
|
42
|
+
contract["publicKeyPem"] as? String ?? ""
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
static var firstDecryptChecksumCase: (checksumHex: String, decryptedHex: String) {
|
|
46
|
+
guard let cases = contract["decryptChecksum"] as? [[String: Any]],
|
|
47
|
+
let first = cases.first,
|
|
48
|
+
let input = first["input"] as? [String: Any],
|
|
49
|
+
let expect = first["expect"] as? [String: Any],
|
|
50
|
+
let checksumHex = input["checksumHex"] as? String,
|
|
51
|
+
let decryptedHex = expect["decryptedHex"] as? String else {
|
|
52
|
+
XCTFail("Missing decryptChecksum fixture case")
|
|
53
|
+
return ("", "")
|
|
54
|
+
}
|
|
55
|
+
return (checksumHex, decryptedHex)
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
private static func locateContractFile() throws -> URL {
|
|
59
|
+
let fileManager = FileManager.default
|
|
60
|
+
let roots = [
|
|
61
|
+
URL(fileURLWithPath: fileManager.currentDirectoryPath),
|
|
62
|
+
URL(fileURLWithPath: #filePath)
|
|
63
|
+
]
|
|
64
|
+
for root in roots {
|
|
65
|
+
var current = root
|
|
66
|
+
while current.path != "/" {
|
|
67
|
+
let candidate = current
|
|
68
|
+
.appendingPathComponent("native-contract-tests")
|
|
69
|
+
.appendingPathComponent("crypto-rsa.json")
|
|
70
|
+
if fileManager.fileExists(atPath: candidate.path) {
|
|
71
|
+
return candidate
|
|
72
|
+
}
|
|
73
|
+
current.deleteLastPathComponent()
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
throw NSError(domain: "PopulateDeltaCacheTests", code: 1, userInfo: [NSLocalizedDescriptionKey: "crypto-rsa.json not found"])
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
override func setUpWithError() throws {
|
|
81
|
+
try super.setUpWithError()
|
|
82
|
+
CryptoCipher.setLogger(Logger(withTag: "PopulateDeltaCacheTests", options: Logger.Options(level: .silent)))
|
|
83
|
+
implementation = TestableCapgoUpdater()
|
|
84
|
+
bundleId = "delta-cache-\(UUID().uuidString)"
|
|
85
|
+
bundleDir = try implementation.getBundleDirectory(id: bundleId)
|
|
86
|
+
try FileManager.default.createDirectory(at: bundleDir, withIntermediateDirectories: true)
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
override func tearDown() {
|
|
90
|
+
if implementation != nil {
|
|
91
|
+
try? FileManager.default.removeItem(at: bundleDir)
|
|
92
|
+
}
|
|
93
|
+
try? FileManager.default.removeItem(at: builtinFolder)
|
|
94
|
+
for file in registeredCacheFiles {
|
|
95
|
+
try? FileManager.default.removeItem(at: file)
|
|
96
|
+
}
|
|
97
|
+
registeredCacheFiles = []
|
|
98
|
+
implementation = nil
|
|
99
|
+
// CryptoCipher's logger is shared/static with no getter to snapshot the prior
|
|
100
|
+
// value, so restore a normal (non-silent) default rather than leaving other
|
|
101
|
+
// test suites silenced by this one.
|
|
102
|
+
CryptoCipher.setLogger(Logger(withTag: "TestLogger"))
|
|
103
|
+
super.tearDown()
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
private enum FixtureError: Error {
|
|
107
|
+
case writeFailed(String)
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
@discardableResult
|
|
111
|
+
private func write(_ content: String, named name: String, in directory: URL) throws -> URL {
|
|
112
|
+
try FileManager.default.createDirectory(at: directory, withIntermediateDirectories: true)
|
|
113
|
+
let url = directory.appendingPathComponent(name)
|
|
114
|
+
guard let data = content.data(using: .utf8) else {
|
|
115
|
+
throw FixtureError.writeFailed("Could not encode fixture content for \(name)")
|
|
116
|
+
}
|
|
117
|
+
try data.write(to: url)
|
|
118
|
+
return url
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
/// Computes the cache path for (hash, name), clears any stale entry left over from
|
|
122
|
+
/// an unrelated run (content/hash is deterministic, so paths can collide across
|
|
123
|
+
/// runs), and registers it for teardown cleanup regardless of test outcome.
|
|
124
|
+
private func expectedCacheFile(hash: String, name: String) -> URL {
|
|
125
|
+
let file = cacheFolder.appendingPathComponent("\(hash)_\(name)")
|
|
126
|
+
try? FileManager.default.removeItem(at: file)
|
|
127
|
+
registeredCacheFiles.append(file)
|
|
128
|
+
return file
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
func testManifestProgressIsMonotonicAndDoesNotRepeatPercentages() throws {
|
|
132
|
+
let updater = try XCTUnwrap(implementation)
|
|
133
|
+
updater.setLogger(Logger(withTag: "manifest-progress-test", options: Logger.Options(level: .silent)))
|
|
134
|
+
try FileManager.default.createDirectory(at: cacheFolder, withIntermediateDirectories: true)
|
|
135
|
+
let content = "<html>progress-\(UUID().uuidString)</html>"
|
|
136
|
+
let source = try write(content, named: "source.html", in: bundleDir)
|
|
137
|
+
let hash = CryptoCipher.calcChecksum(filePath: source)
|
|
138
|
+
var entries: [ManifestEntry] = []
|
|
139
|
+
for index in 0..<120 {
|
|
140
|
+
let name = index == 0 ? "index.html" : "file-\(index).txt"
|
|
141
|
+
try Data(content.utf8).write(to: expectedCacheFile(hash: hash, name: name))
|
|
142
|
+
entries.append(ManifestEntry(file_name: name, file_hash: hash, download_url: "https://cached-fixture.invalid/\(name)"))
|
|
143
|
+
}
|
|
144
|
+
let completed = expectation(description: "manifest completed")
|
|
145
|
+
var percents: [Int] = []
|
|
146
|
+
var installedId: String?
|
|
147
|
+
updater.notifyDownloadRaw = { _, percent, _, bundle in
|
|
148
|
+
percents.append(percent)
|
|
149
|
+
if percent == 100 {
|
|
150
|
+
installedId = bundle?.getId()
|
|
151
|
+
completed.fulfill()
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
let manifest = entries
|
|
155
|
+
DispatchQueue.global().async {
|
|
156
|
+
do {
|
|
157
|
+
_ = try updater.downloadManifest(manifest: manifest, version: "progress-test", sessionKey: "")
|
|
158
|
+
} catch {
|
|
159
|
+
DispatchQueue.main.async {
|
|
160
|
+
XCTFail("Manifest failed: \(error)")
|
|
161
|
+
completed.fulfill()
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
wait(for: [completed], timeout: 10)
|
|
166
|
+
if let installedId { _ = updater.delete(id: installedId) }
|
|
167
|
+
XCTAssertEqual(percents.first, 0)
|
|
168
|
+
XCTAssertEqual(percents.last, 100)
|
|
169
|
+
XCTAssertEqual(percents, percents.sorted())
|
|
170
|
+
XCTAssertEqual(Set(percents).count, percents.count)
|
|
171
|
+
XCTAssertLessThanOrEqual(percents.count, 63)
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
// MARK: - manifestHashLookup
|
|
175
|
+
|
|
176
|
+
func testManifestHashLookupStripsBrotliSuffixAndReturnsPlainHash() {
|
|
177
|
+
let manifest = [
|
|
178
|
+
ManifestEntry(file_name: "assets/app.js.br", file_hash: "plainhash", download_url: nil)
|
|
179
|
+
]
|
|
180
|
+
|
|
181
|
+
let lookup = implementation.manifestHashLookup(manifest: manifest, sessionKey: "")
|
|
182
|
+
|
|
183
|
+
XCTAssertEqual(lookup["assets/app.js"]?.hash, "plainhash")
|
|
184
|
+
XCTAssertEqual(lookup["assets/app.js"]?.originalFileName, "assets/app.js.br")
|
|
185
|
+
XCTAssertNil(lookup["assets/app.js.br"])
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
func testManifestHashLookupDecryptsEncryptedManifestHashes() {
|
|
189
|
+
let (checksumHex, decryptedHex) = Fixture.firstDecryptChecksumCase
|
|
190
|
+
implementation.setPublicKey(Fixture.publicKeyPem)
|
|
191
|
+
let manifest = [
|
|
192
|
+
ManifestEntry(file_name: "index.html", file_hash: checksumHex, download_url: nil)
|
|
193
|
+
]
|
|
194
|
+
|
|
195
|
+
let lookup = implementation.manifestHashLookup(manifest: manifest, sessionKey: "a:b")
|
|
196
|
+
|
|
197
|
+
XCTAssertEqual(lookup["index.html"]?.hash, decryptedHex)
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
func testManifestHashLookupSkipsEntriesMissingFileNameOrHash() {
|
|
201
|
+
let manifest = [
|
|
202
|
+
ManifestEntry(file_name: nil, file_hash: "hash", download_url: nil),
|
|
203
|
+
ManifestEntry(file_name: "no-hash.js", file_hash: nil, download_url: nil)
|
|
204
|
+
]
|
|
205
|
+
|
|
206
|
+
XCTAssertTrue(implementation.manifestHashLookup(manifest: manifest, sessionKey: "").isEmpty)
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
// MARK: - populateDeltaCache: (a) reuse manifest hashes instead of re-hashing
|
|
210
|
+
|
|
211
|
+
func testPopulateDeltaCacheReusesManifestHashInsteadOfRecomputing() throws {
|
|
212
|
+
let fileURL = try write("hello world \(bundleId!)", named: "app.js", in: bundleDir)
|
|
213
|
+
let realHash = CryptoCipher.calcChecksum(filePath: fileURL)
|
|
214
|
+
let manifestHash = "deliberately-different-\(realHash)"
|
|
215
|
+
let manifest = [
|
|
216
|
+
ManifestEntry(file_name: "app.js", file_hash: manifestHash, download_url: nil)
|
|
217
|
+
]
|
|
218
|
+
let manifestCacheFile = expectedCacheFile(hash: manifestHash, name: "app.js")
|
|
219
|
+
let realCacheFile = expectedCacheFile(hash: realHash, name: "app.js")
|
|
220
|
+
|
|
221
|
+
implementation.populateDeltaCache(for: bundleId, manifest: manifest, sessionKey: "")
|
|
222
|
+
|
|
223
|
+
XCTAssertTrue(FileManager.default.fileExists(atPath: manifestCacheFile.path))
|
|
224
|
+
XCTAssertFalse(FileManager.default.fileExists(atPath: realCacheFile.path))
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
func testPopulateDeltaCacheFallsBackToRealHashWhenNoManifestEntry() throws {
|
|
228
|
+
let fileURL = try write("hello world \(bundleId!)", named: "app.js", in: bundleDir)
|
|
229
|
+
let realHash = CryptoCipher.calcChecksum(filePath: fileURL)
|
|
230
|
+
let realCacheFile = expectedCacheFile(hash: realHash, name: "app.js")
|
|
231
|
+
|
|
232
|
+
implementation.populateDeltaCache(for: bundleId)
|
|
233
|
+
|
|
234
|
+
XCTAssertTrue(FileManager.default.fileExists(atPath: realCacheFile.path))
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
// MARK: - populateDeltaCache: (b) skip caching builtin-origin files
|
|
238
|
+
|
|
239
|
+
func testPopulateDeltaCacheSkipsFilesAlreadyAvailableFromBuiltin() throws {
|
|
240
|
+
let content = "shared builtin content \(bundleId!)"
|
|
241
|
+
let fileURL = try write(content, named: "shared.js", in: bundleDir)
|
|
242
|
+
let realHash = CryptoCipher.calcChecksum(filePath: fileURL)
|
|
243
|
+
try write(content, named: "shared.js", in: builtinFolder)
|
|
244
|
+
let cacheFile = expectedCacheFile(hash: realHash, name: "shared.js")
|
|
245
|
+
|
|
246
|
+
implementation.populateDeltaCache(for: bundleId)
|
|
247
|
+
|
|
248
|
+
XCTAssertFalse(FileManager.default.fileExists(atPath: cacheFile.path))
|
|
249
|
+
}
|
|
250
|
+
|
|
251
|
+
func testPopulateDeltaCacheStillCachesFilesNotPresentInBuiltin() throws {
|
|
252
|
+
let fileURL = try write("only in this bundle \(bundleId!)", named: "new.js", in: bundleDir)
|
|
253
|
+
let realHash = CryptoCipher.calcChecksum(filePath: fileURL)
|
|
254
|
+
let cacheFile = expectedCacheFile(hash: realHash, name: "new.js")
|
|
255
|
+
|
|
256
|
+
implementation.populateDeltaCache(for: bundleId)
|
|
257
|
+
|
|
258
|
+
XCTAssertTrue(FileManager.default.fileExists(atPath: cacheFile.path))
|
|
259
|
+
}
|
|
260
|
+
|
|
261
|
+
/// Regression test: brotli manifest entries are extracted without the `.br`
|
|
262
|
+
/// suffix, while builtin assets are stored uncompressed. populateDeltaCache must
|
|
263
|
+
/// resolve the builtin path the same way as isManifestEntryAvailableLocally.
|
|
264
|
+
func testPopulateDeltaCacheSkipsBrotliFilesAlreadyAvailableFromBuiltin() throws {
|
|
265
|
+
let content = "shared builtin content \(bundleId!)"
|
|
266
|
+
let extractedFileURL = try write(content, named: "app.js", in: bundleDir.appendingPathComponent("assets"))
|
|
267
|
+
let realHash = CryptoCipher.calcChecksum(filePath: extractedFileURL)
|
|
268
|
+
try write(content, named: "app.js", in: builtinFolder.appendingPathComponent("assets"))
|
|
269
|
+
let manifest = [
|
|
270
|
+
ManifestEntry(file_name: "assets/app.js.br", file_hash: realHash, download_url: nil)
|
|
271
|
+
]
|
|
272
|
+
let cacheFile = expectedCacheFile(hash: realHash, name: "app.js")
|
|
273
|
+
|
|
274
|
+
implementation.populateDeltaCache(for: bundleId, manifest: manifest, sessionKey: "")
|
|
275
|
+
|
|
276
|
+
XCTAssertFalse(FileManager.default.fileExists(atPath: cacheFile.path))
|
|
277
|
+
}
|
|
278
|
+
|
|
279
|
+
// MARK: - getMissingBundleFiles: trust hash-named cache without re-reading
|
|
280
|
+
|
|
281
|
+
func testGetMissingBundleFilesTreatsHashNamedCacheAsReusable() throws {
|
|
282
|
+
let hash = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
|
|
283
|
+
let cacheFile = expectedCacheFile(hash: hash, name: "app.js")
|
|
284
|
+
try FileManager.default.createDirectory(at: cacheFolder, withIntermediateDirectories: true)
|
|
285
|
+
// Content does not need to match the hash: the filename is the trust source
|
|
286
|
+
// (checksum was verified when the cache entry was written).
|
|
287
|
+
try "not-the-hashed-bytes".write(to: cacheFile, atomically: true, encoding: .utf8)
|
|
288
|
+
let manifest = [
|
|
289
|
+
ManifestEntry(file_name: "app.js", file_hash: hash, download_url: nil)
|
|
290
|
+
]
|
|
291
|
+
|
|
292
|
+
let missing = implementation.getMissingBundleFiles(manifest: manifest, sessionKey: "")
|
|
293
|
+
|
|
294
|
+
XCTAssertTrue(missing.isEmpty)
|
|
295
|
+
}
|
|
296
|
+
|
|
297
|
+
func testGetMissingBundleFilesIgnoresEmptyHashNamedCache() throws {
|
|
298
|
+
let hash = "bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb"
|
|
299
|
+
let cacheFile = expectedCacheFile(hash: hash, name: "app.js")
|
|
300
|
+
try FileManager.default.createDirectory(at: cacheFolder, withIntermediateDirectories: true)
|
|
301
|
+
try Data().write(to: cacheFile)
|
|
302
|
+
let manifest = [
|
|
303
|
+
ManifestEntry(file_name: "app.js", file_hash: hash, download_url: nil)
|
|
304
|
+
]
|
|
305
|
+
|
|
306
|
+
let missing = implementation.getMissingBundleFiles(manifest: manifest, sessionKey: "")
|
|
307
|
+
|
|
308
|
+
XCTAssertEqual(missing.count, 1)
|
|
309
|
+
XCTAssertEqual(missing.first?.file_name, "app.js")
|
|
310
|
+
}
|
|
311
|
+
|
|
312
|
+
func testGetMissingBundleFilesReusesEmptyFileForEmptySha256() throws {
|
|
313
|
+
let hash = "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855"
|
|
314
|
+
let cacheFile = expectedCacheFile(hash: hash, name: "empty.txt")
|
|
315
|
+
try FileManager.default.createDirectory(at: cacheFolder, withIntermediateDirectories: true)
|
|
316
|
+
try Data().write(to: cacheFile)
|
|
317
|
+
let manifest = [
|
|
318
|
+
ManifestEntry(file_name: "empty.txt", file_hash: hash, download_url: nil)
|
|
319
|
+
]
|
|
320
|
+
|
|
321
|
+
let missing = implementation.getMissingBundleFiles(manifest: manifest, sessionKey: "")
|
|
322
|
+
|
|
323
|
+
XCTAssertTrue(missing.isEmpty)
|
|
324
|
+
}
|
|
325
|
+
|
|
326
|
+
func testGetMissingBundleFilesRejectsUnsafeCacheHash() throws {
|
|
327
|
+
let hash = "../evil"
|
|
328
|
+
let cacheFile = expectedCacheFile(hash: hash, name: "app.js")
|
|
329
|
+
try FileManager.default.createDirectory(at: cacheFolder, withIntermediateDirectories: true)
|
|
330
|
+
try "payload".write(to: cacheFile, atomically: true, encoding: .utf8)
|
|
331
|
+
let manifest = [
|
|
332
|
+
ManifestEntry(file_name: "app.js", file_hash: hash, download_url: nil)
|
|
333
|
+
]
|
|
334
|
+
|
|
335
|
+
let missing = implementation.getMissingBundleFiles(manifest: manifest, sessionKey: "")
|
|
336
|
+
|
|
337
|
+
XCTAssertEqual(missing.count, 1)
|
|
338
|
+
}
|
|
339
|
+
|
|
340
|
+
func testGetMissingBundleFilesDoesNotTrustCrc32CacheHash() throws {
|
|
341
|
+
let hash = "deadbeef"
|
|
342
|
+
let cacheFile = expectedCacheFile(hash: hash, name: "app.js")
|
|
343
|
+
try FileManager.default.createDirectory(at: cacheFolder, withIntermediateDirectories: true)
|
|
344
|
+
try "payload".write(to: cacheFile, atomically: true, encoding: .utf8)
|
|
345
|
+
let manifest = [
|
|
346
|
+
ManifestEntry(file_name: "app.js", file_hash: hash, download_url: nil)
|
|
347
|
+
]
|
|
348
|
+
|
|
349
|
+
let missing = implementation.getMissingBundleFiles(manifest: manifest, sessionKey: "")
|
|
350
|
+
|
|
351
|
+
XCTAssertEqual(missing.count, 1)
|
|
352
|
+
}
|
|
353
|
+
|
|
354
|
+
func testGetMissingBundleFilesTreatsLegacyBrotliCacheNameAsReusable() throws {
|
|
355
|
+
let hash = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
|
|
356
|
+
let cacheFile = expectedCacheFile(hash: hash, name: "app.js.br")
|
|
357
|
+
try FileManager.default.createDirectory(at: cacheFolder, withIntermediateDirectories: true)
|
|
358
|
+
try "legacy-brotli-cache".write(to: cacheFile, atomically: true, encoding: .utf8)
|
|
359
|
+
let manifest = [
|
|
360
|
+
ManifestEntry(file_name: "app.js.br", file_hash: hash, download_url: nil)
|
|
361
|
+
]
|
|
362
|
+
|
|
363
|
+
let missing = implementation.getMissingBundleFiles(manifest: manifest, sessionKey: "")
|
|
364
|
+
|
|
365
|
+
XCTAssertTrue(missing.isEmpty)
|
|
366
|
+
}
|
|
367
|
+
}
|
|
368
|
+
|
|
369
|
+
extension PopulateDeltaCacheTests {
|
|
370
|
+
func testGetMissingBundleFilesReusesUncompressedBuiltinForBrotliEntry() throws {
|
|
371
|
+
let testId = try XCTUnwrap(bundleId)
|
|
372
|
+
let name = "\(testId).js"
|
|
373
|
+
let source = try write("builtin \(testId)", named: name, in: builtinFolder.appendingPathComponent("assets"))
|
|
374
|
+
let hash = CryptoCipher.calcChecksum(filePath: source)
|
|
375
|
+
let manifest = [ManifestEntry(file_name: "assets/\(name).br", file_hash: hash, download_url: nil)]
|
|
376
|
+
|
|
377
|
+
XCTAssertTrue(implementation.getMissingBundleFiles(manifest: manifest, sessionKey: "").isEmpty)
|
|
378
|
+
|
|
379
|
+
// A decoded filename match alone must not bypass checksum verification.
|
|
380
|
+
try "different content".write(to: source, atomically: true, encoding: .utf8)
|
|
381
|
+
XCTAssertEqual(implementation.getMissingBundleFiles(manifest: manifest, sessionKey: "").count, 1)
|
|
382
|
+
}
|
|
383
|
+
|
|
384
|
+
func testDownloadManifestReusesSignedUncompressedBuiltinForBrotliEntry() throws {
|
|
385
|
+
implementation.setLogger(Logger(withTag: "BrotliBuiltinTest", options: Logger.Options(level: .silent)))
|
|
386
|
+
implementation.setPublicKey(Fixture.publicKeyPem)
|
|
387
|
+
let name = "\(try XCTUnwrap(bundleId)).js"
|
|
388
|
+
let source = try write("", named: name, in: builtinFolder.appendingPathComponent("assets"))
|
|
389
|
+
let (signedHash, plainHash) = Fixture.firstDecryptChecksumCase
|
|
390
|
+
XCTAssertEqual(CryptoCipher.calcChecksum(filePath: source), plainHash)
|
|
391
|
+
let manifest = [ManifestEntry(
|
|
392
|
+
file_name: "assets/\(name).br",
|
|
393
|
+
file_hash: signedHash,
|
|
394
|
+
// Invalid URL makes any attempted download fail immediately: this must reuse builtin.
|
|
395
|
+
download_url: "http://["
|
|
396
|
+
)]
|
|
397
|
+
|
|
398
|
+
// Only checksum recovery is needed when builtin matches; no file/session decryption occurs.
|
|
399
|
+
let result = try implementation.downloadManifest(manifest: manifest, version: "1.0.0", sessionKey: "a:b")
|
|
400
|
+
defer {
|
|
401
|
+
_ = implementation.delete(id: result.getId(), removeInfo: true)
|
|
402
|
+
implementation.shutdown()
|
|
403
|
+
}
|
|
404
|
+
let destination = try implementation.getBundleDirectory(id: result.getId())
|
|
405
|
+
XCTAssertEqual(try Data(contentsOf: destination.appendingPathComponent("assets/\(name)")), Data())
|
|
406
|
+
XCTAssertFalse(FileManager.default.fileExists(atPath: destination.appendingPathComponent("assets/\(name).br").path))
|
|
407
|
+
}
|
|
408
|
+
|
|
409
|
+
func testMissingBrotliEntryCannotReuseFileOutsideBuiltin() throws {
|
|
410
|
+
let name = "\(try XCTUnwrap(bundleId))-outside.js"
|
|
411
|
+
let source = try write("outside", named: name, in: builtinFolder.deletingLastPathComponent())
|
|
412
|
+
defer { try? FileManager.default.removeItem(at: source) }
|
|
413
|
+
let hash = CryptoCipher.calcChecksum(filePath: source)
|
|
414
|
+
let manifest = [ManifestEntry(file_name: "../\(name).br", file_hash: hash, download_url: nil)]
|
|
415
|
+
|
|
416
|
+
XCTAssertEqual(implementation.getMissingBundleFiles(manifest: manifest, sessionKey: "").count, 1)
|
|
417
|
+
}
|
|
418
|
+
}
|
|
419
|
+
|
|
420
|
+
final class IoBufferSizeTests: XCTestCase {
|
|
421
|
+
func testIoBuffersAre256KiBForChecksumAndCopy() {
|
|
422
|
+
XCTAssertEqual(CryptoCipher.ioBufferBytes(), 256 * 1024)
|
|
423
|
+
XCTAssertEqual(CryptoCipher.checksumBufferBytes(), 256 * 1024)
|
|
424
|
+
XCTAssertEqual(CryptoCipher.copyBufferBytes(), 256 * 1024)
|
|
425
|
+
}
|
|
426
|
+
}
|
|
427
|
+
|
|
428
|
+
final class ManifestConcurrencyTests: XCTestCase {
|
|
429
|
+
func testManifestConcurrencyIsFixedAt32() {
|
|
430
|
+
XCTAssertEqual(CapgoUpdater.manifestMaxConcurrentFiles, 32)
|
|
431
|
+
}
|
|
432
|
+
}
|
|
433
|
+
|
|
434
|
+
final class StreamDecodeTests: XCTestCase {
|
|
435
|
+
private var updater: CapgoUpdater!
|
|
436
|
+
private var tempDir: URL!
|
|
437
|
+
|
|
438
|
+
override func setUp() {
|
|
439
|
+
super.setUp()
|
|
440
|
+
updater = CapgoUpdater()
|
|
441
|
+
updater.setLogger(Logger(withTag: "StreamDecodeTests", options: Logger.Options(level: .silent)))
|
|
442
|
+
CryptoCipher.setLogger(Logger(withTag: "StreamDecodeTests", options: Logger.Options(level: .silent)))
|
|
443
|
+
tempDir = FileManager.default.temporaryDirectory.appendingPathComponent("capgo-brotli-\(UUID().uuidString)")
|
|
444
|
+
try? FileManager.default.createDirectory(at: tempDir, withIntermediateDirectories: true)
|
|
445
|
+
}
|
|
446
|
+
|
|
447
|
+
override func tearDown() {
|
|
448
|
+
if let tempDir {
|
|
449
|
+
try? FileManager.default.removeItem(at: tempDir)
|
|
450
|
+
}
|
|
451
|
+
super.tearDown()
|
|
452
|
+
}
|
|
453
|
+
|
|
454
|
+
func testDecompressBrotliStreamsEmptyWrapperAndRealPayload() throws {
|
|
455
|
+
let dir = tempDir!
|
|
456
|
+
|
|
457
|
+
let emptyIn = dir.appendingPathComponent("empty.br")
|
|
458
|
+
let emptyOut = dir.appendingPathComponent("empty.txt")
|
|
459
|
+
try Data([0x1B, 0x00, 0x06]).write(to: emptyIn)
|
|
460
|
+
try updater.decompressBrotli(from: emptyIn, to: emptyOut, fileName: "empty.br")
|
|
461
|
+
XCTAssertEqual(try Data(contentsOf: emptyOut).count, 0)
|
|
462
|
+
|
|
463
|
+
let wrappedIn = dir.appendingPathComponent("hello.br")
|
|
464
|
+
let wrappedOut = dir.appendingPathComponent("hello.txt")
|
|
465
|
+
try Data([0x0b, 0x02, 0x80, 0x68, 0x65, 0x6c, 0x6c, 0x6f, 0x03]).write(to: wrappedIn)
|
|
466
|
+
try updater.decompressBrotli(from: wrappedIn, to: wrappedOut, fileName: "hello.br")
|
|
467
|
+
XCTAssertEqual(try String(contentsOf: wrappedOut, encoding: .utf8), "hello")
|
|
468
|
+
|
|
469
|
+
let realIn = dir.appendingPathComponent("payload.br")
|
|
470
|
+
let realOut = dir.appendingPathComponent("payload.txt")
|
|
471
|
+
try dataFromHex("1ba702f88d94abed6831a46e4b75213df69d8b08871d5db158a9b062f007672bc101c92bf781d73386bfc50a00").write(to: realIn)
|
|
472
|
+
let expected = String(repeating: "Capgo stream brotli test payload. ", count: 20)
|
|
473
|
+
let hashed = try updater.decompressBrotli(from: realIn, to: realOut, fileName: "payload.br")
|
|
474
|
+
XCTAssertEqual(try String(contentsOf: realOut, encoding: .utf8), expected)
|
|
475
|
+
XCTAssertEqual(hashed, CryptoCipher.calcChecksum(filePath: realOut))
|
|
476
|
+
}
|
|
477
|
+
|
|
478
|
+
func testShouldAppendHttpBodyOnlyForPartialContent() {
|
|
479
|
+
XCTAssertFalse(CapgoUpdater.shouldAppendHttpBody(statusCode: 200, existingBytes: 100))
|
|
480
|
+
XCTAssertTrue(CapgoUpdater.shouldAppendHttpBody(statusCode: 206, existingBytes: 100))
|
|
481
|
+
XCTAssertFalse(CapgoUpdater.shouldAppendHttpBody(statusCode: 206, existingBytes: 0))
|
|
482
|
+
}
|
|
483
|
+
|
|
484
|
+
func testManifestPartialURLIsStableForSha256AndPath() {
|
|
485
|
+
let cache = FileManager.default.temporaryDirectory.appendingPathComponent("capgo-partial-\(UUID().uuidString)")
|
|
486
|
+
let hash = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
|
|
487
|
+
let nested = CapgoUpdater.manifestPartialURL(cacheFolder: cache, downloadId: "bundle-a", hash: hash, fileName: "nested/app.js")
|
|
488
|
+
let root = CapgoUpdater.manifestPartialURL(cacheFolder: cache, downloadId: "bundle-a", hash: hash, fileName: "app.js")
|
|
489
|
+
let vendor = CapgoUpdater.manifestPartialURL(cacheFolder: cache, downloadId: "bundle-a", hash: hash, fileName: "vendor/app.js")
|
|
490
|
+
XCTAssertNotEqual(nested.lastPathComponent, root.lastPathComponent)
|
|
491
|
+
XCTAssertNotEqual(nested.lastPathComponent, vendor.lastPathComponent)
|
|
492
|
+
XCTAssertEqual(nested.lastPathComponent, CapgoUpdater.manifestPartialURL(cacheFolder: cache, downloadId: "bundle-a", hash: hash, fileName: "nested/app.js").lastPathComponent)
|
|
493
|
+
// Concurrent downloads of the same file never share a partial.
|
|
494
|
+
XCTAssertNotEqual(nested.lastPathComponent, CapgoUpdater.manifestPartialURL(cacheFolder: cache, downloadId: "bundle-b", hash: hash, fileName: "nested/app.js").lastPathComponent)
|
|
495
|
+
XCTAssertTrue(nested.lastPathComponent.hasPrefix("partial_"))
|
|
496
|
+
XCTAssertTrue(nested.lastPathComponent.contains("_\(hash)_"))
|
|
497
|
+
let escaping = CapgoUpdater.manifestPartialURL(cacheFolder: cache, downloadId: "../../escape", hash: hash, fileName: "app.js")
|
|
498
|
+
XCTAssertTrue(escaping.standardizedFileURL.path.hasPrefix(cache.standardizedFileURL.path + "/"))
|
|
499
|
+
XCTAssertLessThan(nested.lastPathComponent.count, 255)
|
|
500
|
+
XCTAssertTrue(nested.lastPathComponent.hasSuffix(".tmp"))
|
|
501
|
+
let unsafe = CapgoUpdater.manifestPartialURL(cacheFolder: cache, downloadId: "bundle-a", hash: "../evil", fileName: "app.js")
|
|
502
|
+
XCTAssertTrue(unsafe.lastPathComponent.hasPrefix("partial_"))
|
|
503
|
+
XCTAssertTrue(unsafe.standardizedFileURL.path.hasPrefix(cache.standardizedFileURL.path + "/"))
|
|
504
|
+
XCTAssertFalse(unsafe.lastPathComponent.contains(".."))
|
|
505
|
+
}
|
|
506
|
+
|
|
507
|
+
func testStoreDownloadedFileReplacesOn200AndAppendsOn206() throws {
|
|
508
|
+
let updater = TestableCapgoUpdater()
|
|
509
|
+
updater.setLogger(Logger(withTag: "StreamDecodeTests", options: Logger.Options(level: .silent)))
|
|
510
|
+
let dir = FileManager.default.temporaryDirectory.appendingPathComponent("capgo-range-\(UUID().uuidString)")
|
|
511
|
+
try FileManager.default.createDirectory(at: dir, withIntermediateDirectories: true)
|
|
512
|
+
defer { try? FileManager.default.removeItem(at: dir) }
|
|
513
|
+
|
|
514
|
+
var full = Data(count: 4096)
|
|
515
|
+
for i in 0..<full.count {
|
|
516
|
+
full[i] = UInt8(i % 256)
|
|
517
|
+
}
|
|
518
|
+
let first = full.prefix(1500)
|
|
519
|
+
let second = full.suffix(from: 1500)
|
|
520
|
+
let dest = dir.appendingPathComponent("partial.bin")
|
|
521
|
+
try Data(first).write(to: dest)
|
|
522
|
+
|
|
523
|
+
let chunk206 = dir.appendingPathComponent("chunk206.bin")
|
|
524
|
+
try Data(second).write(to: chunk206)
|
|
525
|
+
let url = URL(string: "https://example.invalid/file.bin")!
|
|
526
|
+
let partialResponse = HTTPURLResponse(url: url, statusCode: 206, httpVersion: "HTTP/1.1", headerFields: nil)
|
|
527
|
+
try updater.storeDownloadedFile(chunk206, at: dest, existingBytes: Int64(first.count), response: partialResponse)
|
|
528
|
+
XCTAssertEqual(try Data(contentsOf: dest), full)
|
|
529
|
+
|
|
530
|
+
let replaceChunk = dir.appendingPathComponent("chunk200.bin")
|
|
531
|
+
let other = Data("replaced-full-body".utf8)
|
|
532
|
+
try other.write(to: replaceChunk)
|
|
533
|
+
let okResponse = HTTPURLResponse(url: url, statusCode: 200, httpVersion: "HTTP/1.1", headerFields: nil)
|
|
534
|
+
try updater.storeDownloadedFile(replaceChunk, at: dest, existingBytes: Int64(full.count), response: okResponse)
|
|
535
|
+
XCTAssertEqual(try Data(contentsOf: dest), other)
|
|
536
|
+
}
|
|
537
|
+
|
|
538
|
+
private func dataFromHex(_ hex: String) -> Data {
|
|
539
|
+
var data = Data()
|
|
540
|
+
var index = hex.startIndex
|
|
541
|
+
while index < hex.endIndex {
|
|
542
|
+
let next = hex.index(index, offsetBy: 2)
|
|
543
|
+
data.append(UInt8(hex[index..<next], radix: 16)!)
|
|
544
|
+
index = next
|
|
545
|
+
}
|
|
546
|
+
return data
|
|
547
|
+
}
|
|
548
|
+
}
|
|
549
|
+
|
|
550
|
+
final class ImmutableCacheLinkTests: XCTestCase {
|
|
551
|
+
func testLinkedCacheSurvivesDeletingBundleAndAtomicReplacement() throws {
|
|
552
|
+
let manager = FileManager.default
|
|
553
|
+
let root = manager.temporaryDirectory.appendingPathComponent(UUID().uuidString)
|
|
554
|
+
try manager.createDirectory(at: root, withIntermediateDirectories: true)
|
|
555
|
+
defer { try? manager.removeItem(at: root) }
|
|
556
|
+
let source = root.appendingPathComponent("bundle.js")
|
|
557
|
+
let cache = root.appendingPathComponent("cache/app.js")
|
|
558
|
+
let restored = root.appendingPathComponent("restored.js")
|
|
559
|
+
let original = Data("verified content".utf8)
|
|
560
|
+
try original.write(to: source)
|
|
561
|
+
let updater = CapgoUpdater()
|
|
562
|
+
updater.setLogger(Logger(withTag: "cache-link-test", options: Logger.Options(level: .silent)))
|
|
563
|
+
let alias = root.appendingPathComponent("alias.js")
|
|
564
|
+
try manager.createSymbolicLink(at: alias, withDestinationURL: source)
|
|
565
|
+
try updater.linkOrCopyImmutableFile(from: alias, to: cache)
|
|
566
|
+
try manager.removeItem(at: source)
|
|
567
|
+
XCTAssertEqual(try Data(contentsOf: cache), original)
|
|
568
|
+
try updater.linkOrCopyImmutableFile(from: cache, to: restored)
|
|
569
|
+
try Data("replacement".utf8).write(to: restored, options: .atomic)
|
|
570
|
+
XCTAssertEqual(try Data(contentsOf: cache), original)
|
|
571
|
+
XCTAssertEqual(try manager.contentsOfDirectory(atPath: root.path).filter { $0.hasPrefix("capgo-link-") }.count, 0)
|
|
572
|
+
}
|
|
573
|
+
}
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
import Foundation
|
|
2
|
+
import XCTest
|
|
3
|
+
@testable import CapacitorUpdaterPlugin
|
|
4
|
+
|
|
5
|
+
final class RedirectPolicyTests: XCTestCase {
|
|
6
|
+
private func redirect(
|
|
7
|
+
_ delegate: RedirectPolicyDelegate,
|
|
8
|
+
from source: String,
|
|
9
|
+
to target: String
|
|
10
|
+
) -> (URLRequest?, URLSessionTask) {
|
|
11
|
+
let session = URLSession(configuration: .ephemeral)
|
|
12
|
+
defer { session.invalidateAndCancel() }
|
|
13
|
+
let task = session.dataTask(with: URL(string: source)!)
|
|
14
|
+
let response = HTTPURLResponse(url: URL(string: source)!, statusCode: 302, httpVersion: "HTTP/1.1", headerFields: ["Location": target])!
|
|
15
|
+
var result: URLRequest?
|
|
16
|
+
delegate.urlSession(session, task: task, willPerformHTTPRedirection: response, newRequest: URLRequest(url: URL(string: target)!)) { request in
|
|
17
|
+
result = request
|
|
18
|
+
}
|
|
19
|
+
return (result, task)
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
func testDowngradeDetection() {
|
|
23
|
+
XCTAssertTrue(RedirectPolicyDelegate.isHttpsToHttpDowngrade(from: URL(string: "https://a.com/x"), to: URL(string: "http://b.com/y")))
|
|
24
|
+
XCTAssertTrue(RedirectPolicyDelegate.isHttpsToHttpDowngrade(from: URL(string: "HTTPS://a.com/x"), to: URL(string: "HTTP://a.com/x")))
|
|
25
|
+
XCTAssertFalse(RedirectPolicyDelegate.isHttpsToHttpDowngrade(from: URL(string: "https://a.com/x"), to: URL(string: "https://b.com/y")))
|
|
26
|
+
XCTAssertFalse(RedirectPolicyDelegate.isHttpsToHttpDowngrade(from: URL(string: "http://a.com/x"), to: URL(string: "http://b.com/y")))
|
|
27
|
+
XCTAssertFalse(RedirectPolicyDelegate.isHttpsToHttpDowngrade(from: URL(string: "http://a.com/x"), to: URL(string: "https://b.com/y")))
|
|
28
|
+
XCTAssertFalse(RedirectPolicyDelegate.isHttpsToHttpDowngrade(from: nil, to: URL(string: "http://b.com/y")))
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
func testBlocksHttpsToHttpRedirectByDefault() {
|
|
32
|
+
let delegate = RedirectPolicyDelegate()
|
|
33
|
+
var blocked = false
|
|
34
|
+
delegate.onBlockedRedirect = { _, _ in blocked = true }
|
|
35
|
+
let (request, task) = redirect(delegate, from: "https://api.capgo.app/updates", to: "http://evil.example/bundle.zip")
|
|
36
|
+
XCTAssertNil(request)
|
|
37
|
+
XCTAssertTrue(blocked)
|
|
38
|
+
XCTAssertNotEqual(task.state, .suspended)
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
func testAllowsHttpsToHttpRedirectWhenEnabled() {
|
|
42
|
+
let delegate = RedirectPolicyDelegate()
|
|
43
|
+
delegate.httpsOnly = false
|
|
44
|
+
delegate.allowHttpsToHttpRedirect = true
|
|
45
|
+
let (request, _) = redirect(delegate, from: "https://api.capgo.app/updates", to: "http://example.com/bundle.zip")
|
|
46
|
+
XCTAssertEqual(request?.url?.absoluteString, "http://example.com/bundle.zip")
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
func testAllowsSafeRedirects() {
|
|
50
|
+
let delegate = RedirectPolicyDelegate()
|
|
51
|
+
XCTAssertNotNil(redirect(delegate, from: "https://a.com/x", to: "https://b.com/y").0)
|
|
52
|
+
delegate.httpsOnly = false
|
|
53
|
+
XCTAssertNotNil(redirect(delegate, from: "http://a.com/x", to: "https://b.com/y").0)
|
|
54
|
+
XCTAssertNotNil(redirect(delegate, from: "http://a.com/x", to: "http://b.com/y").0)
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
func testUpdaterBlocksDowngradeByDefault() {
|
|
58
|
+
let updater = CapgoUpdater()
|
|
59
|
+
XCTAssertFalse(updater.allowHttpsToHttpRedirect)
|
|
60
|
+
updater.allowHttpsToHttpRedirect = true
|
|
61
|
+
XCTAssertTrue(updater.allowHttpsToHttpRedirect)
|
|
62
|
+
}
|
|
63
|
+
}
|