diff --git a/Sources/X509/Extension Types/AuthorityInformationAccess.swift b/Sources/X509/Extension Types/AuthorityInformationAccess.swift index 8e55311..faf6dd5 100644 --- a/Sources/X509/Extension Types/AuthorityInformationAccess.swift +++ b/Sources/X509/Extension Types/AuthorityInformationAccess.swift @@ -22,6 +22,12 @@ import SwiftASN1 public struct AuthorityInformationAccess { @usableFromInline var descriptions: [AccessDescription] + + /// Create a new empty ``AuthorityInformationAccess/`` object + /// containing no access descriptions. + public init() { + self.descriptions = [] + } /// Create a new ``AuthorityInformationAccess/`` object /// containing specific access descriptions. @@ -82,6 +88,13 @@ extension AuthorityInformationAccess: RandomAccessCollection { } } +extension AuthorityInformationAccess: RangeReplaceableCollection { + @inlinable + public mutating func replaceSubrange(_ subrange: Range, with newElements: some Collection) { + self.descriptions.replaceSubrange(subrange, with: newElements) + } +} + extension AuthorityInformationAccess { /// Describes the location and format of additional information provided /// by the issuer of a given certificate. diff --git a/Sources/X509/Extension Types/ExtendedKeyUsage.swift b/Sources/X509/Extension Types/ExtendedKeyUsage.swift index 0e71ddd..3bd6d37 100644 --- a/Sources/X509/Extension Types/ExtendedKeyUsage.swift +++ b/Sources/X509/Extension Types/ExtendedKeyUsage.swift @@ -103,7 +103,6 @@ extension ExtendedKeyUsage: CustomStringConvertible { } } -// TODO(cory): Probably also RangeReplaceableCollection, even though it's kinda crap. extension ExtendedKeyUsage: RandomAccessCollection { public var startIndex: Int { self.usages.startIndex diff --git a/Tests/X509Tests/CertificateTests.swift b/Tests/X509Tests/CertificateTests.swift index 8127b2c..6c42e04 100644 --- a/Tests/X509Tests/CertificateTests.swift +++ b/Tests/X509Tests/CertificateTests.swift @@ -85,6 +85,33 @@ final class CertificateTests: XCTestCase { "Issuer: uri: https://example.com/ca, OCSP Server: uri: http://example.com/ocsp, 1.2.3.4: rfc822Name: mail@example.com" ) } + + func testRangeReplaceableCollectionConformance() throws { + var ext = AuthorityInformationAccess([ + .init(method: .issuingCA, location: .uniformResourceIdentifier("https://example.com/ca")), + .init(method: .ocspServer, location: .uniformResourceIdentifier("http://example.com/ocsp")), + .init(method: .init(.unknownType([1, 2, 3, 4])), location: .rfc822Name("mail@example.com")), + ]) + + ext.replaceSubrange(1..<2, with: [ + .init(method: .ocspServer, location: .uniformResourceIdentifier("http://example.com/ocsp/a")), + .init(method: .ocspServer, location: .uniformResourceIdentifier("http://example.com/ocsp/b")), + ]) + + XCTAssertEqual(Array(ext), [ + .init(method: .issuingCA, location: .uniformResourceIdentifier("https://example.com/ca")), + .init(method: .ocspServer, location: .uniformResourceIdentifier("http://example.com/ocsp/a")), + .init(method: .ocspServer, location: .uniformResourceIdentifier("http://example.com/ocsp/b")), + .init(method: .init(.unknownType([1, 2, 3, 4])), location: .rfc822Name("mail@example.com")), + ]) + + func conformsToRangeReplaceableCollection(_ value: some Any) -> Bool { + value is any RangeReplaceableCollection + } + // writing out `ext is any RangeReplaceableCollection` will produce a warning that this is always true + // therefore we go through this indirection to silence this warning + XCTAssertTrue(conformsToRangeReplaceableCollection(ext)) + } func testPrintingAKIExtension() throws { var ext = AuthorityKeyIdentifier(