Skip to content

Commit d0bf886

Browse files
committed
platform check
1 parent 207ba6f commit d0bf886

File tree

5 files changed

+195
-28
lines changed

5 files changed

+195
-28
lines changed

Sources/PackageCollections/API.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,4 +182,6 @@ public enum PackageCollectionError: Equatable, Error {
182182
case invalidSignature
183183

184184
case missingSignature
185+
186+
case unsupportedPlatform
185187
}

Sources/PackageCollections/PackageCollections.swift

Lines changed: 68 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,13 @@ import TSCBasic
1414

1515
// TODO: is there a better name? this conflicts with the module name which is okay in this case but not ideal in Swift
1616
public struct PackageCollections: PackageCollectionsProtocol {
17+
// Check JSONPackageCollectionProvider.isSignatureCheckSupported before updating or removing this
18+
#if os(macOS) || os(Linux) || os(Windows)
19+
static let isSupportedPlatform = true
20+
#else
21+
static let isSupportedPlatform = false
22+
#endif
23+
1724
private let configuration: Configuration
1825
private let diagnosticsEngine: DiagnosticsEngine?
1926
private let storageContainer: (storage: Storage, owned: Bool)
@@ -64,6 +71,10 @@ public struct PackageCollections: PackageCollectionsProtocol {
6471

6572
public func listCollections(identifiers: Set<PackageCollectionsModel.CollectionIdentifier>? = nil,
6673
callback: @escaping (Result<[PackageCollectionsModel.Collection], Error>) -> Void) {
74+
guard Self.isSupportedPlatform else {
75+
return callback(.failure(PackageCollectionError.unsupportedPlatform))
76+
}
77+
6778
self.storage.sources.list { result in
6879
switch result {
6980
case .failure(let error):
@@ -93,6 +104,10 @@ public struct PackageCollections: PackageCollectionsProtocol {
93104
}
94105

95106
public func refreshCollections(callback: @escaping (Result<[PackageCollectionsModel.CollectionSource], Error>) -> Void) {
107+
guard Self.isSupportedPlatform else {
108+
return callback(.failure(PackageCollectionError.unsupportedPlatform))
109+
}
110+
96111
self.storage.sources.list { result in
97112
switch result {
98113
case .failure(let error):
@@ -115,17 +130,23 @@ public struct PackageCollections: PackageCollectionsProtocol {
115130
}
116131
}
117132

118-
public func refreshCollection(
119-
_ source: PackageCollectionsModel.CollectionSource,
120-
callback: @escaping (Result<PackageCollectionsModel.Collection, Error>) -> Void
121-
) {
133+
public func refreshCollection(_ source: PackageCollectionsModel.CollectionSource,
134+
callback: @escaping (Result<PackageCollectionsModel.Collection, Error>) -> Void) {
135+
guard Self.isSupportedPlatform else {
136+
return callback(.failure(PackageCollectionError.unsupportedPlatform))
137+
}
138+
122139
self.refreshCollectionFromSource(source: source, trustConfirmationProvider: nil, callback: callback)
123140
}
124141

125142
public func addCollection(_ source: PackageCollectionsModel.CollectionSource,
126143
order: Int? = nil,
127144
trustConfirmationProvider: ((PackageCollectionsModel.Collection, @escaping (Bool) -> Void) -> Void)? = nil,
128145
callback: @escaping (Result<PackageCollectionsModel.Collection, Error>) -> Void) {
146+
guard Self.isSupportedPlatform else {
147+
return callback(.failure(PackageCollectionError.unsupportedPlatform))
148+
}
149+
129150
if let errors = source.validate()?.errors() {
130151
return callback(.failure(MultipleErrors(errors)))
131152
}
@@ -160,6 +181,10 @@ public struct PackageCollections: PackageCollectionsProtocol {
160181

161182
public func removeCollection(_ source: PackageCollectionsModel.CollectionSource,
162183
callback: @escaping (Result<Void, Error>) -> Void) {
184+
guard Self.isSupportedPlatform else {
185+
return callback(.failure(PackageCollectionError.unsupportedPlatform))
186+
}
187+
163188
self.storage.sources.remove(source: source) { result in
164189
switch result {
165190
case .failure(let error):
@@ -173,11 +198,19 @@ public struct PackageCollections: PackageCollectionsProtocol {
173198
public func moveCollection(_ source: PackageCollectionsModel.CollectionSource,
174199
to order: Int,
175200
callback: @escaping (Result<Void, Error>) -> Void) {
201+
guard Self.isSupportedPlatform else {
202+
return callback(.failure(PackageCollectionError.unsupportedPlatform))
203+
}
204+
176205
self.storage.sources.move(source: source, to: order, callback: callback)
177206
}
178207

179208
public func updateCollection(_ source: PackageCollectionsModel.CollectionSource,
180209
callback: @escaping (Result<PackageCollectionsModel.Collection, Error>) -> Void) {
210+
guard Self.isSupportedPlatform else {
211+
return callback(.failure(PackageCollectionError.unsupportedPlatform))
212+
}
213+
181214
self.storage.sources.update(source: source) { result in
182215
switch result {
183216
case .failure(let error):
@@ -193,6 +226,10 @@ public struct PackageCollections: PackageCollectionsProtocol {
193226
// If not found locally (storage), the collection will be fetched from the source.
194227
public func getCollection(_ source: PackageCollectionsModel.CollectionSource,
195228
callback: @escaping (Result<PackageCollectionsModel.Collection, Error>) -> Void) {
229+
guard Self.isSupportedPlatform else {
230+
return callback(.failure(PackageCollectionError.unsupportedPlatform))
231+
}
232+
196233
if let errors = source.validate()?.errors() {
197234
return callback(.failure(MultipleErrors(errors)))
198235
}
@@ -212,11 +249,13 @@ public struct PackageCollections: PackageCollectionsProtocol {
212249

213250
// MARK: - Packages
214251

215-
public func findPackages(
216-
_ query: String,
217-
collections: Set<PackageCollectionsModel.CollectionIdentifier>? = nil,
218-
callback: @escaping (Result<PackageCollectionsModel.PackageSearchResult, Error>) -> Void
219-
) {
252+
public func findPackages(_ query: String,
253+
collections: Set<PackageCollectionsModel.CollectionIdentifier>? = nil,
254+
callback: @escaping (Result<PackageCollectionsModel.PackageSearchResult, Error>) -> Void) {
255+
guard Self.isSupportedPlatform else {
256+
return callback(.failure(PackageCollectionError.unsupportedPlatform))
257+
}
258+
220259
self.storage.sources.list { result in
221260
switch result {
222261
case .failure(let error):
@@ -235,6 +274,10 @@ public struct PackageCollections: PackageCollectionsProtocol {
235274

236275
public func getPackageMetadata(_ reference: PackageReference,
237276
callback: @escaping (Result<PackageCollectionsModel.PackageMetadata, Error>) -> Void) {
277+
guard Self.isSupportedPlatform else {
278+
return callback(.failure(PackageCollectionError.unsupportedPlatform))
279+
}
280+
238281
// first find in storage
239282
self.findPackage(identifier: reference.identity) { result in
240283
switch result {
@@ -266,10 +309,12 @@ public struct PackageCollections: PackageCollectionsProtocol {
266309

267310
// MARK: - Targets
268311

269-
public func listTargets(
270-
collections: Set<PackageCollectionsModel.CollectionIdentifier>? = nil,
271-
callback: @escaping (Result<PackageCollectionsModel.TargetListResult, Error>) -> Void
272-
) {
312+
public func listTargets(collections: Set<PackageCollectionsModel.CollectionIdentifier>? = nil,
313+
callback: @escaping (Result<PackageCollectionsModel.TargetListResult, Error>) -> Void) {
314+
guard Self.isSupportedPlatform else {
315+
return callback(.failure(PackageCollectionError.unsupportedPlatform))
316+
}
317+
273318
self.listCollections(identifiers: collections) { result in
274319
switch result {
275320
case .failure(let error):
@@ -281,12 +326,14 @@ public struct PackageCollections: PackageCollectionsProtocol {
281326
}
282327
}
283328

284-
public func findTargets(
285-
_ query: String,
286-
searchType: PackageCollectionsModel.TargetSearchType? = nil,
287-
collections: Set<PackageCollectionsModel.CollectionIdentifier>? = nil,
288-
callback: @escaping (Result<PackageCollectionsModel.TargetSearchResult, Error>) -> Void
289-
) {
329+
public func findTargets(_ query: String,
330+
searchType: PackageCollectionsModel.TargetSearchType? = nil,
331+
collections: Set<PackageCollectionsModel.CollectionIdentifier>? = nil,
332+
callback: @escaping (Result<PackageCollectionsModel.TargetSearchResult, Error>) -> Void) {
333+
guard Self.isSupportedPlatform else {
334+
return callback(.failure(PackageCollectionError.unsupportedPlatform))
335+
}
336+
290337
let searchType = searchType ?? .exactMatch
291338

292339
self.storage.sources.list { result in
@@ -371,10 +418,8 @@ public struct PackageCollections: PackageCollectionsProtocol {
371418
}
372419
}
373420

374-
func findPackage(
375-
identifier: PackageIdentity,
376-
callback: @escaping (Result<PackageCollectionsModel.PackageSearchResult.Item, Error>) -> Void
377-
) {
421+
func findPackage(identifier: PackageIdentity,
422+
callback: @escaping (Result<PackageCollectionsModel.PackageSearchResult.Item, Error>) -> Void) {
378423
self.storage.sources.list { result in
379424
switch result {
380425
case .failure(let error):

Sources/PackageCollections/Providers/JSONPackageCollectionProvider.swift

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,14 @@ struct JSONPackageCollectionProvider: PackageCollectionProvider {
2828
// FIXME: remove
2929
static let enableSignatureCheck = ProcessInfo.processInfo.environment["ENABLE_COLLECTION_SIGNATURE_CHECK"] != nil
3030

31+
// TODO: This can be removed when the `Security` framework APIs that the `PackageCollectionsSigning`
32+
// module depends on are available on all Apple platforms.
33+
#if os(macOS) || os(Linux) || os(Windows)
34+
static let isSignatureCheckSupported = true
35+
#else
36+
static let isSignatureCheckSupported = false
37+
#endif
38+
3139
private let configuration: Configuration
3240
private let diagnosticsEngine: DiagnosticsEngine
3341
private let httpClient: HTTPClient
@@ -136,6 +144,10 @@ struct JSONPackageCollectionProvider: PackageCollectionProvider {
136144
// Don't validate signature; set isVerified=false
137145
callback(self.makeCollection(from: signedCollection.collection, source: source, signature: Model.SignatureData(from: signedCollection.signature, isVerified: false)))
138146
} else {
147+
if !Self.isSignatureCheckSupported {
148+
fatalError("Unsupported platform")
149+
}
150+
139151
// Check the signature
140152
self.signatureValidator.validate(signedCollection: signedCollection, certPolicyKey: certPolicyKey) { result in
141153
switch result {

Tests/PackageCollectionsTests/JSONPackageCollectionProviderTests.swift

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -335,7 +335,7 @@ class JSONPackageCollectionProviderTests: XCTestCase {
335335
}
336336

337337
func testSignedGood() throws {
338-
if !enableSignatureCheck {
338+
if !enableSignatureCheck || !JSONPackageCollectionProvider.isSignatureCheckSupported {
339339
try XCTSkipIf(true)
340340
}
341341

@@ -471,7 +471,7 @@ class JSONPackageCollectionProviderTests: XCTestCase {
471471
}
472472

473473
func testSigned_noTrustedRootCertsConfigured() throws {
474-
if !enableSignatureCheck {
474+
if !enableSignatureCheck || !JSONPackageCollectionProvider.isSignatureCheckSupported {
475475
try XCTSkipIf(true)
476476
}
477477

@@ -515,7 +515,7 @@ class JSONPackageCollectionProviderTests: XCTestCase {
515515
}
516516

517517
func testSignedBad() throws {
518-
if !enableSignatureCheck {
518+
if !enableSignatureCheck || !JSONPackageCollectionProvider.isSignatureCheckSupported {
519519
try XCTSkipIf(true)
520520
}
521521

@@ -560,7 +560,7 @@ class JSONPackageCollectionProviderTests: XCTestCase {
560560
}
561561

562562
func testSignedLocalFile() throws {
563-
if !enableSignatureCheck {
563+
if !enableSignatureCheck || !JSONPackageCollectionProvider.isSignatureCheckSupported {
564564
try XCTSkipIf(true)
565565
}
566566

0 commit comments

Comments
 (0)