Conform AuthorityInformationAccess to RangeReplaceableCollection (#105)

This commit is contained in:
David Nadoba authored and GitHub committed 2023-06-29 06:22:43 -07:00
1 parent 1beb546810
commit e8f5dadf79
3 files changed
+40 -1

No files matched your search

@@ -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<Int>, with newElements: some Collection<AccessDescription>) {
self.descriptions.replaceSubrange(subrange, with: newElements)
}
}
extension AuthorityInformationAccess {
/// Describes the location and format of additional information provided
/// by the issuer of a given certificate.
@@ -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
+27
View File
@@ -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(