From 680bbc1253ef165273b0a15e0dc2aadf9b464b5d Mon Sep 17 00:00:00 2001 From: Kevin Wooten Date: Fri, 9 Jun 2023 01:24:45 -0700 Subject: [PATCH] Add tests for random data resillience to ASN1 and CBOR --- Tests/ASN1Tests.swift | 8 +++----- Tests/AnyValueTests.swift | 6 +++--- Tests/CBORTests.swift | 20 ++++++++++++++++++++ 3 files changed, 26 insertions(+), 8 deletions(-) diff --git a/Tests/ASN1Tests.swift b/Tests/ASN1Tests.swift index 32c79e9c7..bd6ec8b84 100644 --- a/Tests/ASN1Tests.swift +++ b/Tests/ASN1Tests.swift @@ -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)) diff --git a/Tests/AnyValueTests.swift b/Tests/AnyValueTests.swift index 39c0da99f..62fb86ec2 100644 --- a/Tests/AnyValueTests.swift +++ b/Tests/AnyValueTests.swift @@ -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( @@ -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), .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), .dictionary([1: 1, 2: "test", 3: true])) // Passthrough diff --git a/Tests/CBORTests.swift b/Tests/CBORTests.swift index 852452f30..53fa63676 100644 --- a/Tests/CBORTests.swift +++ b/Tests/CBORTests.swift @@ -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)) + } + } + }