File: MockRegistry.swift

package info (click to toggle)
swiftlang 6.0.3-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 2,519,992 kB
  • sloc: cpp: 9,107,863; ansic: 2,040,022; asm: 1,135,751; python: 296,500; objc: 82,456; f90: 60,502; lisp: 34,951; pascal: 19,946; sh: 18,133; perl: 7,482; ml: 4,937; javascript: 4,117; makefile: 3,840; awk: 3,535; xml: 914; fortran: 619; cs: 573; ruby: 573
file content (467 lines) | stat: -rw-r--r-- 17,913 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
//===----------------------------------------------------------------------===//
//
// This source file is part of the Swift open source project
//
// Copyright (c) 2021-2023 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See http://swift.org/LICENSE.txt for license information
// See http://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//

import Basics
import Foundation
import PackageFingerprint
import PackageGraph
import PackageLoading
import PackageModel
import PackageRegistry
import PackageSigning

import protocol TSCBasic.HashAlgorithm

import struct TSCUtility.Version

public class MockRegistry {
    private let baseURL: URL
    private let fileSystem: FileSystem
    private let identityResolver: IdentityResolver
    private let checksumAlgorithm: HashAlgorithm
    public var registryClient: RegistryClient!
    private let jsonEncoder: JSONEncoder

    private var packageVersions = [PackageIdentity: [String: InMemoryRegistryPackageSource]]()
    private var packagesSourceControlURLs = [PackageIdentity: [URL]]()
    private var sourceControlURLs = [URL: PackageIdentity]()
    private let packagesLock = NSLock()

    public init(
        filesystem: FileSystem,
        identityResolver: IdentityResolver,
        checksumAlgorithm: HashAlgorithm,
        fingerprintStorage: PackageFingerprintStorage,
        signingEntityStorage: PackageSigningEntityStorage,
        customBaseURL: URL? = .none
    ) {
        self.fileSystem = filesystem
        self.identityResolver = identityResolver
        self.checksumAlgorithm = checksumAlgorithm
        self.jsonEncoder = JSONEncoder.makeWithDefaults()

        var configuration = RegistryConfiguration()
        if let customBaseURL {
            self.baseURL = customBaseURL

        } else {
            self.baseURL = URL("http://localhost/registry/mock")
        }
        configuration.defaultRegistry = .init(url: self.baseURL, supportsAvailability: false)
        configuration.security = .testDefault

        self.registryClient = RegistryClient(
            configuration: configuration,
            fingerprintStorage: fingerprintStorage,
            fingerprintCheckingMode: .strict,
            skipSignatureValidation: false,
            signingEntityStorage: signingEntityStorage,
            signingEntityCheckingMode: .strict,
            authorizationProvider: .none,
            customHTTPClient: LegacyHTTPClient(handler: self.httpHandler),
            customArchiverProvider: { fileSystem in MockRegistryArchiver(fileSystem: fileSystem) },
            delegate: .none,
            checksumAlgorithm: checksumAlgorithm
        )
    }

    public func addPackage(
        identity: PackageIdentity,
        versions: [Version],
        sourceControlURLs: [URL]? = .none,
        source: InMemoryRegistryPackageSource
    ) {
        self.addPackage(
            identity: identity,
            versions: versions.map(\.description),
            sourceControlURLs: sourceControlURLs,
            source: source
        )
    }

    public func addPackage(
        identity: PackageIdentity,
        versions: [String],
        sourceControlURLs: [URL]? = .none,
        source: InMemoryRegistryPackageSource
    ) {
        self.packagesLock.withLock {
            // versions
            var updatedVersions = self.packageVersions[identity] ?? [:]
            for version in versions {
                updatedVersions[version.description] = source
            }
            self.packageVersions[identity] = updatedVersions
            // source control URLs
            if let sourceControlURLs {
                var packageSourceControlURLs = self.packagesSourceControlURLs[identity] ?? []
                packageSourceControlURLs.append(contentsOf: sourceControlURLs)
                self.packagesSourceControlURLs[identity] = packageSourceControlURLs
                // reverse index
                for sourceControlURL in sourceControlURLs {
                    self.sourceControlURLs[sourceControlURL] = identity
                }
            }
        }
    }

    func httpHandler(
        request: LegacyHTTPClient.Request,
        progress: LegacyHTTPClient.ProgressHandler?,
        completion: @escaping (Result<LegacyHTTPClient.Response, Error>) -> Void
    ) {
        do {
            guard request.url.absoluteString.hasPrefix(self.baseURL.absoluteString) else {
                throw StringError("url outside mock registry \(self.baseURL)")
            }

            switch request.kind {
            case .generic:
                let response = try self.handleRequest(request: request)
                completion(.success(response))
            case .download(let fileSystem, let destination):
                let response = try self.handleDownloadRequest(
                    request: request,
                    progress: progress,
                    fileSystem: fileSystem,
                    destination: destination
                )
                completion(.success(response))
            }
        } catch {
            completion(.failure(error))
        }
    }

    private func handleRequest(request: LegacyHTTPClient.Request) throws -> LegacyHTTPClient.Response {
        let routeComponents = request.url.absoluteString.dropFirst(self.baseURL.absoluteString.count + 1)
            .split(separator: "/")
        switch routeComponents.count {
        case _ where routeComponents[0].hasPrefix("identifiers?url="):
            guard let query = request.url.query else {
                throw StringError("invalid url: \(request.url)")
            }
            guard let sourceControlURL = URL(string: String(query.dropFirst(4))) else {
                throw StringError("invalid url query: \(query)")
            }
            return try self.getIdentifiers(url: sourceControlURL)
        case 2:
            let package = PackageIdentity.plain(routeComponents.joined(separator: "."))
            return try self.getPackageMetadata(packageIdentity: package)
        case 3:
            let package = PackageIdentity.plain(routeComponents[0 ... 1].joined(separator: "."))
            let version = String(routeComponents[2])
            return try self.getVersionMetadata(packageIdentity: package, version: version)
        case 4 where routeComponents[3] == "Package.swift":
            let package = PackageIdentity.plain(routeComponents[0 ... 1].joined(separator: "."))
            let version = String(routeComponents[2])
            guard let components = URLComponents(url: request.url, resolvingAgainstBaseURL: false) else {
                throw StringError("invalid url: \(request.url)")
            }
            let toolsVersion = components.queryItems?.first(where: { $0.name == "swift-version" })?.value
                .flatMap(ToolsVersion.init(string:))
            return try self.getManifest(packageIdentity: package, version: version, toolsVersion: toolsVersion)
        default:
            throw StringError("unknown request \(request.url)")
        }
    }

    private func getPackageMetadata(packageIdentity: PackageIdentity) throws -> HTTPClientResponse {
        guard let registryIdentity = packageIdentity.registry else {
            throw StringError("invalid package identifier '\(packageIdentity)'")
        }

        let versions = self.packageVersions[packageIdentity] ?? [:]
        let metadata = RegistryClient.Serialization.PackageMetadata(
            releases: versions.keys
                .reduce(into: [String: RegistryClient.Serialization.PackageMetadata.Release]()) { partial, item in
                    partial[item] =
                        .init(
                            url: "\(self.baseURL.absoluteString)/\(registryIdentity.scope)/\(registryIdentity.name)/\(item)"
                        )
                }
        )

        /*
         <https://github.com/mona/LinkedList>; rel="canonical",
         <ssh://git@github.com:mona/LinkedList.git>; rel="alternate"
         */
        let sourceControlURLs = self.packagesSourceControlURLs[packageIdentity]
        let links = sourceControlURLs?.map { url in
            "<\(url.absoluteString)>; rel=alternate"
        }.joined(separator: ", ")

        var headers = HTTPClientHeaders()
        headers.add(name: "Content-Version", value: "1")
        headers.add(name: "Content-Type", value: "application/json")
        if let links {
            headers.add(name: "Link", value: links)
        }

        return try HTTPClientResponse(
            statusCode: 200,
            headers: headers,
            body: self.jsonEncoder.encode(metadata)
        )
    }

    private func getVersionMetadata(packageIdentity: PackageIdentity, version: String) throws -> HTTPClientResponse {
        guard let package = self.packageVersions[packageIdentity]?[version] else {
            return .notFound()
        }

        let zipfileContent = try self.zipFileContent(
            packageIdentity: packageIdentity,
            version: version,
            source: package
        )
        let zipfileChecksum = self.checksumAlgorithm.hash(zipfileContent)

        let metadata = RegistryClient.Serialization.VersionMetadata(
            id: packageIdentity.description,
            version: version,
            resources: [
                .init(
                    name: "source-archive",
                    type: "application/zip",
                    checksum: zipfileChecksum.hexadecimalRepresentation,
                    signing: nil
                ),
            ],
            metadata: .init(
                description: "\(packageIdentity) description",
                readmeURL: "http://\(packageIdentity)/readme"
            ),
            publishedAt: Date()
        )

        var headers = HTTPClientHeaders()
        headers.add(name: "Content-Version", value: "1")
        headers.add(name: "Content-Type", value: "application/json")

        return try HTTPClientResponse(
            statusCode: 200,
            headers: headers,
            body: self.jsonEncoder.encode(metadata)
        )
    }

    private func getManifest(
        packageIdentity: PackageIdentity,
        version: String,
        toolsVersion: ToolsVersion? = .none
    ) throws -> HTTPClientResponse {
        guard let package = self.packageVersions[packageIdentity]?[version] else {
            return .notFound()
        }

        let filename: String
        if let toolsVersion {
            filename = Manifest.basename + "@swift-\(toolsVersion).swift"
        } else {
            filename = Manifest.basename + ".swift"
        }

        let content: Data = try package.fileSystem.readFileContents(package.path.appending(component: filename))

        var headers = HTTPClientHeaders()
        headers.add(name: "Content-Version", value: "1")
        headers.add(name: "Content-Type", value: "text/x-swift")

        return HTTPClientResponse(
            statusCode: 200,
            headers: headers,
            body: content
        )
    }

    private func getIdentifiers(url: URL) throws -> HTTPClientResponse {
        let identifiers = self.sourceControlURLs[url].map { [$0.description] } ?? []

        let packageIdentifiers = RegistryClient.Serialization.PackageIdentifiers(
            identifiers: identifiers
        )

        var headers = HTTPClientHeaders()
        headers.add(name: "Content-Version", value: "1")
        headers.add(name: "Content-Type", value: "application/json")

        return HTTPClientResponse(
            statusCode: 200,
            headers: headers,
            body: try self.jsonEncoder.encode(packageIdentifiers)
        )
    }

    private func handleDownloadRequest(
        request: LegacyHTTPClient.Request,
        progress: LegacyHTTPClient.ProgressHandler?,
        fileSystem: FileSystem,
        destination: AbsolutePath
    ) throws -> HTTPClientResponse {
        let routeComponents = request.url.absoluteString.dropFirst(self.baseURL.absoluteString.count + 1)
            .split(separator: "/")
        guard routeComponents.count == 3, routeComponents[2].hasSuffix(".zip") else {
            throw StringError("invalid request \(request.url), expecting zip suffix")
        }

        let packageIdentity = PackageIdentity.plain(routeComponents[0 ... 1].joined(separator: "."))
        let version = String(routeComponents[2].dropLast(4))

        guard let package = self.packageVersions[packageIdentity]?[version] else {
            return .notFound()
        }

        if !fileSystem.exists(destination.parentDirectory) {
            try fileSystem.createDirectory(destination.parentDirectory, recursive: true)
        }

        let zipfileContent = try self.zipFileContent(
            packageIdentity: packageIdentity,
            version: version,
            source: package
        )
        try fileSystem.writeFileContents(destination, string: zipfileContent)

        var headers = HTTPClientHeaders()
        headers.add(name: "Content-Version", value: "1")
        headers.add(name: "Content-Type", value: "application/zip")

        return HTTPClientResponse(
            statusCode: 200,
            headers: headers
        )
    }

    private func zipFileContent(
        packageIdentity: PackageIdentity,
        version: String,
        source: InMemoryRegistryPackageSource
    ) throws -> String {
        var content = "\(packageIdentity)_\(version)\n"
        content += source.path.pathString + "\n"
        for file in try source.listFiles() {
            content += file.pathString + "\n"
        }
        return content
    }
}

public struct InMemoryRegistryPackageSource {
    let fileSystem: FileSystem
    public let path: AbsolutePath

    public init(fileSystem: FileSystem, path: AbsolutePath, writeContent: Bool = true) {
        self.fileSystem = fileSystem
        self.path = path
    }

    public func writePackageContent(targets: [String] = [], toolsVersion: ToolsVersion = .current) throws {
        try self.fileSystem.createDirectory(self.path, recursive: true)
        let sourcesDir = self.path.appending("Sources")
        for target in targets {
            let targetDir = sourcesDir.appending(component: target)
            try self.fileSystem.createDirectory(targetDir, recursive: true)
            try self.fileSystem.writeFileContents(targetDir.appending("file.swift"), bytes: "")
        }
        let manifestPath = self.path.appending(component: Manifest.filename)
        try self.fileSystem.writeFileContents(manifestPath, string: "// swift-tools-version:\(toolsVersion)")
    }

    public func listFiles(root: AbsolutePath? = .none) throws -> [AbsolutePath] {
        var files = [AbsolutePath]()
        let root = root ?? self.path
        let entries = try self.fileSystem.getDirectoryContents(root)
        for entry in entries.map({ root.appending(component: $0) }) {
            if self.fileSystem.isDirectory(entry) {
                let directoryFiles = try self.listFiles(root: entry)
                files.append(contentsOf: directoryFiles)
            } else if self.fileSystem.isFile(entry) {
                files.append(entry)
            } else {
                throw StringError("invalid entry type")
            }
        }
        return files
    }
}

private struct MockRegistryArchiver: Archiver {
    let supportedExtensions = Set<String>(["zip"])
    let fileSystem: FileSystem

    init(fileSystem: FileSystem) {
        self.fileSystem = fileSystem
    }

    func extract(
        from archivePath: AbsolutePath,
        to destinationPath: AbsolutePath,
        completion: @escaping (Result<Void, Error>) -> Void
    ) {
        do {
            let lines = try self.readFileContents(archivePath)
            guard lines.count >= 2 else {
                throw StringError("invalid mock zip format, not enough lines")
            }
            let rootPath = lines[1]
            for path in lines[2 ..< lines.count] {
                let relativePath = String(path.dropFirst(rootPath.count + 1))
                let targetPath = try AbsolutePath(
                    validating: relativePath,
                    relativeTo: destinationPath.appending("package")
                )
                if !self.fileSystem.exists(targetPath.parentDirectory) {
                    try self.fileSystem.createDirectory(targetPath.parentDirectory, recursive: true)
                }
                try self.fileSystem.copy(from: try AbsolutePath(validating: path), to: targetPath)
            }
            completion(.success(()))
        } catch {
            completion(.failure(error))
        }
    }

    func compress(
        directory: AbsolutePath,
        to destinationPath: AbsolutePath,
        completion: @escaping (Result<Void, Error>) -> Void
    ) {
        fatalError("not implemented")
    }

    func validate(path: AbsolutePath, completion: @escaping (Result<Bool, Error>) -> Void) {
        do {
            let lines = try self.readFileContents(path)
            completion(.success(lines.count >= 2))
        } catch {
            completion(.failure(error))
        }
    }

    private func readFileContents(_ path: AbsolutePath) throws -> [String] {
        let content: String = try self.fileSystem.readFileContents(path)
        return content.split(whereSeparator: { $0.isNewline }).map(String.init)
    }
}

extension RegistryConfiguration.Security {
    public static let testDefault: RegistryConfiguration.Security = {
        var signing = RegistryConfiguration.Security.Signing()
        signing.onUnsigned = .silentAllow
        signing.onUntrustedCertificate = .silentAllow

        var security = RegistryConfiguration.Security()
        security.default.signing = signing
        return security
    }()
}