@@ -15,6 +15,7 @@ import Commands
15
15
import Foundation
16
16
import PackageLoading
17
17
import PackageModel
18
+ import PackageRegistry
18
19
@testable import PackageRegistryTool
19
20
import PackageSigning
20
21
import SPMTestSupport
@@ -117,6 +118,19 @@ final class PackageRegistryToolTests: CommandsTestCase {
117
118
XCTAssertEqual ( json [ " version " ] , . int( 1 ) )
118
119
}
119
120
121
+ // Set default registry with allow-insecure-http option
122
+ do {
123
+ try execute ( [ " set " , " \( customRegistryBaseURL) " , " --allow-insecure-http " ] , packagePath: packageRoot)
124
+
125
+ let json = try JSON ( data: localFileSystem. readFileContents ( configurationFilePath) )
126
+ XCTAssertEqual ( json [ " registries " ] ? . dictionary? . count, 1 )
127
+ XCTAssertEqual (
128
+ json [ " registries " ] ? . dictionary ? [ " [default] " ] ? . dictionary ? [ " url " ] ? . string,
129
+ " \( customRegistryBaseURL) "
130
+ )
131
+ XCTAssertEqual ( json [ " version " ] , . int( 1 ) )
132
+ }
133
+
120
134
// Unset default registry
121
135
do {
122
136
try execute ( [ " unset " ] , packagePath: packageRoot)
@@ -215,6 +229,40 @@ final class PackageRegistryToolTests: CommandsTestCase {
215
229
}
216
230
}
217
231
232
+ func testSetInsecureURL( ) throws {
233
+ try fixture ( name: " DependencyResolution/External/Simple " ) { fixturePath in
234
+ let packageRoot = fixturePath. appending ( " Bar " )
235
+ let configurationFilePath = AbsolutePath (
236
+ " .swiftpm/configuration/registries.json " ,
237
+ relativeTo: packageRoot
238
+ )
239
+
240
+ XCTAssertFalse ( localFileSystem. exists ( configurationFilePath) )
241
+
242
+ // Set default registry
243
+ XCTAssertThrowsError ( try execute ( [ " set " , " http://package.example.com " ] , packagePath: packageRoot) )
244
+
245
+ XCTAssertFalse ( localFileSystem. exists ( configurationFilePath) )
246
+ }
247
+ }
248
+
249
+ func testSetAllowedInsecureURL( ) throws {
250
+ try fixture ( name: " DependencyResolution/External/Simple " ) { fixturePath in
251
+ let packageRoot = fixturePath. appending ( " Bar " )
252
+ let configurationFilePath = AbsolutePath (
253
+ " .swiftpm/configuration/registries.json " ,
254
+ relativeTo: packageRoot
255
+ )
256
+
257
+ XCTAssertFalse ( localFileSystem. exists ( configurationFilePath) )
258
+
259
+ // Set default registry
260
+ try execute ( [ " set " , " http://package.example.com " , " --allow-insecure-http " ] , packagePath: packageRoot)
261
+
262
+ XCTAssertTrue ( localFileSystem. exists ( configurationFilePath) )
263
+ }
264
+ }
265
+
218
266
func testSetInvalidScope( ) throws {
219
267
try fixture ( name: " DependencyResolution/External/Simple " ) { fixturePath in
220
268
let packageRoot = fixturePath. appending ( " Bar " )
@@ -402,6 +450,133 @@ final class PackageRegistryToolTests: CommandsTestCase {
402
450
}
403
451
}
404
452
453
+ func testPublishingToHTTPRegistry( ) throws {
454
+ #if os(Linux)
455
+ // needed for archiving
456
+ guard SPM_posix_spawn_file_actions_addchdir_np_supported ( ) else {
457
+ throw XCTSkip ( " working directory not supported on this platform " )
458
+ }
459
+ #endif
460
+
461
+ let packageIdentity = " test.my-package "
462
+ let version = " 0.1.0 "
463
+ let registryURL = " http://packages.example.com "
464
+
465
+ _ = try withTemporaryDirectory { temporaryDirectory in
466
+ let packageDirectory = temporaryDirectory. appending ( " MyPackage " )
467
+ try localFileSystem. createDirectory ( packageDirectory)
468
+
469
+ let initPackage = try InitPackage (
470
+ name: " MyPackage " ,
471
+ packageType: . executable,
472
+ destinationPath: packageDirectory,
473
+ fileSystem: localFileSystem
474
+ )
475
+ try initPackage. writePackageStructure ( )
476
+ XCTAssertFileExists ( packageDirectory. appending ( " Package.swift " ) )
477
+
478
+ let workingDirectory = temporaryDirectory. appending ( component: UUID ( ) . uuidString)
479
+ try localFileSystem. createDirectory ( workingDirectory)
480
+
481
+ XCTAssertThrowsError ( try SwiftPM . Registry. execute (
482
+ [
483
+ " publish " ,
484
+ packageIdentity,
485
+ version,
486
+ " --url= \( registryURL) " ,
487
+ " --scratch-directory= \( workingDirectory. pathString) " ,
488
+ " --package-path= \( packageDirectory. pathString) " ,
489
+ " --dry-run " ,
490
+ ]
491
+ ) )
492
+ }
493
+ }
494
+
495
+ func testPublishingToAllowedHTTPRegistry( ) throws {
496
+ #if os(Linux)
497
+ // needed for archiving
498
+ guard SPM_posix_spawn_file_actions_addchdir_np_supported ( ) else {
499
+ throw XCTSkip ( " working directory not supported on this platform " )
500
+ }
501
+ #endif
502
+
503
+ let packageIdentity = " test.my-package "
504
+ let version = " 0.1.0 "
505
+ let registryURL = " http://packages.example.com "
506
+
507
+ // with no authentication configured for registry
508
+ _ = try withTemporaryDirectory { temporaryDirectory in
509
+ let packageDirectory = temporaryDirectory. appending ( " MyPackage " )
510
+ try localFileSystem. createDirectory ( packageDirectory)
511
+
512
+ let initPackage = try InitPackage (
513
+ name: " MyPackage " ,
514
+ packageType: . executable,
515
+ destinationPath: packageDirectory,
516
+ fileSystem: localFileSystem
517
+ )
518
+ try initPackage. writePackageStructure ( )
519
+ XCTAssertFileExists ( packageDirectory. appending ( " Package.swift " ) )
520
+
521
+ let workingDirectory = temporaryDirectory. appending ( component: UUID ( ) . uuidString)
522
+ try localFileSystem. createDirectory ( workingDirectory)
523
+
524
+ try SwiftPM . Registry. execute (
525
+ [
526
+ " publish " ,
527
+ packageIdentity,
528
+ version,
529
+ " --url= \( registryURL) " ,
530
+ " --scratch-directory= \( workingDirectory. pathString) " ,
531
+ " --package-path= \( packageDirectory. pathString) " ,
532
+ " --allow-insecure-http " ,
533
+ " --dry-run " ,
534
+ ]
535
+ )
536
+ }
537
+
538
+ // with authentication configured for registry
539
+ _ = try withTemporaryDirectory { temporaryDirectory in
540
+ let packageDirectory = temporaryDirectory. appending ( " MyPackage " )
541
+ try localFileSystem. createDirectory ( packageDirectory)
542
+
543
+ let initPackage = try InitPackage (
544
+ name: " MyPackage " ,
545
+ packageType: . executable,
546
+ destinationPath: packageDirectory,
547
+ fileSystem: localFileSystem
548
+ )
549
+ try initPackage. writePackageStructure ( )
550
+ XCTAssertFileExists ( packageDirectory. appending ( " Package.swift " ) )
551
+
552
+ let workingDirectory = temporaryDirectory. appending ( component: UUID ( ) . uuidString)
553
+ try localFileSystem. createDirectory ( workingDirectory)
554
+
555
+ let configurationFilePath = AbsolutePath (
556
+ " .swiftpm/configuration/registries.json " ,
557
+ relativeTo: packageDirectory
558
+ )
559
+
560
+ try localFileSystem. createDirectory ( configurationFilePath. parentDirectory, recursive: true )
561
+ var configuration = RegistryConfiguration ( )
562
+ try configuration. add ( authentication: . init( type: . basic) , for: URL ( registryURL) )
563
+ try localFileSystem. writeFileContents ( configurationFilePath, data: JSONEncoder ( ) . encode ( configuration) )
564
+
565
+ XCTAssertThrowsError ( try SwiftPM . Registry. execute (
566
+ [
567
+ " publish " ,
568
+ packageIdentity,
569
+ version,
570
+ " --url= \( registryURL) " ,
571
+ " --scratch-directory= \( workingDirectory. pathString) " ,
572
+ " --package-path= \( packageDirectory. pathString) " ,
573
+ " --allow-insecure-http " ,
574
+ " --dry-run " ,
575
+ ]
576
+ ) )
577
+ }
578
+ }
579
+
405
580
func testPublishingUnsignedPackage( ) throws {
406
581
#if os(Linux)
407
582
// needed for archiving
@@ -903,13 +1078,18 @@ final class PackageRegistryToolTests: CommandsTestCase {
903
1078
}
904
1079
}
905
1080
1081
+ func testLoginRequiresHTTPS( ) {
1082
+ let registryURL = URL ( string: " http://packages.example.com " ) !
1083
+
1084
+ XCTAssertThrowsError ( try SwiftPM . Registry. execute ( [ " login " , " --url " , registryURL. absoluteString] ) )
1085
+ }
1086
+
906
1087
func testCreateLoginURL( ) {
907
1088
let registryURL = URL ( string: " https://packages.example.com " ) !
908
1089
909
1090
XCTAssertEqual ( try SwiftPackageRegistryTool . Login. loginURL ( from: registryURL, loginAPIPath: nil ) . absoluteString, " https://packages.example.com/login " )
910
1091
911
1092
XCTAssertEqual ( try SwiftPackageRegistryTool . Login. loginURL ( from: registryURL, loginAPIPath: " /secret-sign-in " ) . absoluteString, " https://packages.example.com/secret-sign-in " )
912
-
913
1093
}
914
1094
915
1095
func testCreateLoginURLMaintainsPort( ) {
@@ -920,6 +1100,18 @@ final class PackageRegistryToolTests: CommandsTestCase {
920
1100
XCTAssertEqual ( try SwiftPackageRegistryTool . Login. loginURL ( from: registryURL, loginAPIPath: " /secret-sign-in " ) . absoluteString, " https://packages.example.com:8081/secret-sign-in " )
921
1101
}
922
1102
1103
+ func testValidateRegistryURL( ) throws {
1104
+ // Valid
1105
+ try URL ( string: " https://packages.example.com " ) !. validateRegistryURL ( )
1106
+ try URL ( string: " http://packages.example.com " ) !. validateRegistryURL ( allowHTTP: true )
1107
+
1108
+ // Invalid
1109
+ XCTAssertThrowsError ( try URL ( string: " http://packages.example.com " ) !. validateRegistryURL ( ) )
1110
+ XCTAssertThrowsError ( try URL ( string: " http://packages.example.com " ) !. validateRegistryURL ( allowHTTP: false ) )
1111
+ XCTAssertThrowsError ( try URL ( string: " ssh://packages.example.com " ) !. validateRegistryURL ( ) )
1112
+ XCTAssertThrowsError ( try URL ( string: " ftp://packages.example.com " ) !. validateRegistryURL ( allowHTTP: true ) )
1113
+ }
1114
+
923
1115
private func testRoots( ) throws -> [ [ UInt8 ] ] {
924
1116
try fixture ( name: " Signing " , createGitRepo: false ) { fixturePath in
925
1117
let rootCA = try localFileSystem
0 commit comments