Skip to content

Commit

Permalink
ASN1: Add tests for random data resillience
Browse files Browse the repository at this point in the history
  • Loading branch information
kdubb committed Jun 9, 2023
1 parent 8b3304b commit d76fd29
Showing 1 changed file with 29 additions and 0 deletions.
29 changes: 29 additions & 0 deletions Tests/ASN1Tests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -249,4 +249,33 @@ class ASN1Tests: XCTestCase {
XCTAssertEqual(offset(5), -40920)
}

func testRandomData() throws {

struct TestStruct: Codable, SchemaSpecified {
var id: OID
var data: Data

static var asn1Schema: Schema {
.sequence([
"id": .objectIdentifier(),
"data": .octetString(size: .is(16))
])
}
}

let encoded = try ASN1.Encoder.encode(TestStruct(id: [1, 2, 3, 4, 5], data: Data(count: 16)))

for _ in 0 ..< 10000 {

var random = Data(count: encoded.count)
random.withUnsafeMutableBytes { ptr in
if SecRandomCopyBytes(nil, ptr.count, ptr.baseAddress!) != errSecSuccess {
fatalError("SecRandomCopyBytes failed")
}
}

XCTAssertThrowsError(try ASN1.Decoder.decode(TestStruct.self, from: random))
}
}

}

0 comments on commit d76fd29

Please sign in to comment.