diff --git a/.codecov.yml b/.codecov.yml index da33c0a..fd70e4c 100644 --- a/.codecov.yml +++ b/.codecov.yml @@ -2,5 +2,4 @@ coverage: range: "80...100" ignore: - "Sources/Scanner.swift" - - "Sources/DataURI+String.swift" - "Tests/" diff --git a/Sources/DataURI+String.swift b/Sources/DataURI+String.swift index 63e84d1..9a311dc 100644 --- a/Sources/DataURI+String.swift +++ b/Sources/DataURI+String.swift @@ -6,6 +6,7 @@ extension Sequence where Iterator.Element == Byte { internal var base64Decoded: Bytes { let bytes = [Byte](self) let dataBase64 = Data(bytes: bytes) + guard let data = Data(base64Encoded: dataBase64) else { return [] } diff --git a/Tests/DataURITests/DataURITests.swift b/Tests/DataURITests/DataURITests.swift index e73e0d0..4fa6690 100644 --- a/Tests/DataURITests/DataURITests.swift +++ b/Tests/DataURITests/DataURITests.swift @@ -1,4 +1,7 @@ import XCTest + +import Core + @testable import DataURI class DataURITests: XCTestCase { @@ -8,6 +11,7 @@ class DataURITests: XCTestCase { ("testHTMLText", testHTMLText), ("testHTMLJavascriptText", testHTMLJavascriptText), ("testFailedInvalidScheme", testFailedInvalidScheme), + ("testPublicInterface", testPublicInterface), ("testSpeed", testSpeed) ] @@ -64,6 +68,14 @@ class DataURITests: XCTestCase { } } + func testPublicInterface() { + expectNoThrow() { + let (data, type) = try "data:,Hello%2C%20World!".dataURIDecoded() + XCTAssertEqual(data.string, "Hello, World!") + XCTAssertEqual(type, "text/plain;charset=US-ASCII") + } + } + func testSpeed() { measure { for _ in 0..<10_000 { @@ -73,4 +85,16 @@ class DataURITests: XCTestCase { } } } + + //FIXME(Brett): remove when Core 1.1 includes `base64Decoded` + // required for 100% coverage + func testBase64DecodeFailure() { + var bytes = "SGVsbG8sIFdvcmxkIQ%3D%3D".bytes //Hello World! + bytes.append(0x1E) //invalid control character + let decodedBytes = bytes.base64Decoded + XCTAssertEqual( + decodedBytes, [], + "Invalid character should have caused the base64Decoder to escape." + ) + } }