@@ -58,9 +58,8 @@ class XZTests: XCTestCase {
5858 func testMultiStreamNoPadding( ) throws {
5959 // Doesn't contain any padding.
6060 let testData = try Constants . data ( forTest: " test_multi " , withType: XZTests . testType)
61-
62- let decompressedData = try XZArchive . unarchive ( archive: testData)
6361 let splitDecompressedData = try XZArchive . splitUnarchive ( archive: testData)
62+ XCTAssertEqual ( splitDecompressedData. count, 4 )
6463
6564 var answerData = Data ( )
6665 for i in 1 ... 4 {
@@ -69,6 +68,7 @@ class XZTests: XCTestCase {
6968 XCTAssertEqual ( splitDecompressedData [ i - 1 ] , currentAnswerData)
7069 }
7170
71+ let decompressedData = try XZArchive . unarchive ( archive: testData)
7272 XCTAssertEqual ( decompressedData, answerData)
7373 }
7474
@@ -77,11 +77,9 @@ class XZTests: XCTestCase {
7777 // After second - 4 bytes of padding.
7878 // Third - 8 bytes.
7979 // At the end - 4 bytes.
80-
8180 let testData = try Constants . data ( forTest: " test_multi_pad " , withType: XZTests . testType)
82-
83- let decompressedData = try XZArchive . unarchive ( archive: testData)
8481 let splitDecompressedData = try XZArchive . splitUnarchive ( archive: testData)
82+ XCTAssertEqual ( splitDecompressedData. count, 4 )
8583
8684 var answerData = Data ( )
8785 for i in 1 ... 4 {
@@ -91,6 +89,7 @@ class XZTests: XCTestCase {
9189 XCTAssertEqual ( splitDecompressedData [ i - 1 ] , currentAnswerData)
9290 }
9391
92+ let decompressedData = try XZArchive . unarchive ( archive: testData)
9493 XCTAssertEqual ( decompressedData, answerData)
9594 }
9695
@@ -120,4 +119,43 @@ class XZTests: XCTestCase {
120119 XCTAssertThrowsError ( try XZArchive . unarchive ( archive: testData) )
121120 }
122121
122+ func testChecksumMismatch( ) throws {
123+ // Here we test that an error for checksum mismatch is thrown correctly and its associated value contains
124+ // expected data. We do this by programmatically adjusting the input: we change one of the bytes for the checkum,
125+ // which makes it incorrect.
126+ var testData = try Constants . data ( forTest: " test1 " , withType: XZTests . testType)
127+ // Here we modify the stored value of crc64.
128+ testData [ 46 ] &+= 1
129+ var thrownError : Error ? = nil
130+ XCTAssertThrowsError ( try XZArchive . unarchive ( archive: testData) ) { thrownError = $0 }
131+ XCTAssertTrue ( thrownError is XZError , " Unexpected error type: \( type ( of: thrownError) ) " )
132+ if case let . some( . wrongCheck( decompressedData) ) = thrownError as? XZError {
133+ XCTAssertEqual ( decompressedData. count, 1 )
134+ let answerData = try Constants . data ( forAnswer: " test1 " )
135+ XCTAssertEqual ( decompressedData. first, answerData)
136+ } else {
137+ XCTFail ( " Unexpected error: \( String ( describing: thrownError) ) " )
138+ }
139+ }
140+
141+ func testMultiStreamChecksumMismatch( ) throws {
142+ // Here we test that an error for checksum mismatch is thrown correctly and its associated value contains
143+ // expected data. We do this by programmatically adjusting the input: we change one of the bytes for the checkum,
144+ // which makes it incorrect.
145+ var testData = try Constants . data ( forTest: " test_multi " , withType: XZTests . testType)
146+ // Here we modify the stored value of crc64.
147+ testData [ 2346 ] &+= 1
148+ var thrownError : Error ? = nil
149+ XCTAssertThrowsError ( try XZArchive . splitUnarchive ( archive: testData) ) { thrownError = $0 }
150+ XCTAssertTrue ( thrownError is XZError , " Unexpected error type: \( type ( of: thrownError) ) " )
151+ if case let . some( . wrongCheck( decompressedData) ) = thrownError as? XZError {
152+ XCTAssertEqual ( decompressedData. count, 2 )
153+ var answerData = [ try Constants . data ( forAnswer: " test1 " ) ]
154+ answerData. append ( try Constants . data ( forAnswer: " test2 " ) )
155+ XCTAssertEqual ( decompressedData, answerData)
156+ } else {
157+ XCTFail ( " Unexpected error: \( String ( describing: thrownError) ) " )
158+ }
159+ }
160+
123161}
0 commit comments