Skip to content

Commit

Permalink
Add tests for random data resillience to ASN1 and CBOR
Browse files Browse the repository at this point in the history
  • Loading branch information
kdubb committed Jun 9, 2023
1 parent d76fd29 commit 680bbc1
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 8 deletions.
8 changes: 3 additions & 5 deletions Tests/ASN1Tests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -267,11 +267,9 @@ class ASN1Tests: XCTestCase {

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")
}
var random = Data(capacity: encoded.count)
for _ in 0 ..< encoded.count {
random.append(UInt8.random(in: 0 ..< .max))
}

XCTAssertThrowsError(try ASN1.Decoder.decode(TestStruct.self, from: random))
Expand Down
6 changes: 3 additions & 3 deletions Tests/AnyValueTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -270,7 +270,7 @@ class AnyValueTests: XCTestCase {
XCTAssertEqual(try AnyValue.wrapped(uuid), .uuid(uuid))
let date = Date()
XCTAssertEqual(try AnyValue.wrapped(date), .date(date))
XCTAssertEqual(try AnyValue.wrapped([1, "test", true]), .array([1, "test", true]))
XCTAssertEqual(try AnyValue.wrapped([1, "test", true] as [Any]), .array([1, "test", true]))

// Unorderd dictionaries
XCTAssertEqual(
Expand All @@ -283,9 +283,9 @@ class AnyValueTests: XCTestCase {
.dictionaryValue.map { val in Dictionary(uniqueKeysWithValues: val.map { ($0, $1) }) },
[.int(1): .string("a"), .int(2): .string("b"), .int(3): .string("c")]
)
XCTAssertEqual(try AnyValue.wrapped(["a": 1, "b": "test", "c": true] as OrderedDictionary),
XCTAssertEqual(try AnyValue.wrapped(["a": 1, "b": "test", "c": true] as OrderedDictionary<AnyValue, AnyValue>),
.dictionary(["a": 1, "b": "test", "c": true]))
XCTAssertEqual(try AnyValue.wrapped([1: 1, 2: "test", 3: true] as OrderedDictionary),
XCTAssertEqual(try AnyValue.wrapped([1: 1, 2: "test", 3: true] as OrderedDictionary<AnyValue, AnyValue>),
.dictionary([1: 1, 2: "test", 3: true]))

// Passthrough
Expand Down
20 changes: 20 additions & 0 deletions Tests/CBORTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -92,4 +92,24 @@ class CBORTests: XCTestCase {
XCTAssertEqual(map, try CBORSerialization.cbor(from: Data([0xA3, 0x61, 0x63, 0x01, 0x61, 0x61, 0x02, 0x61, 0x62, 0x03])))
}

func testRandomData() throws {

struct TestStruct: Codable {
var id: [Int]
var data: Data
}

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

for _ in 0 ..< 10000 {

var random = Data(capacity: encoded.count)
for _ in 0 ..< encoded.count {
random.append(UInt8.random(in: 0 ..< .max))
}

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

}

0 comments on commit 680bbc1

Please sign in to comment.