-
-
Notifications
You must be signed in to change notification settings - Fork 370
Expand file tree
/
Copy pathZIPFoundationDataSerializationTests.swift
More file actions
131 lines (124 loc) · 5.94 KB
/
Copy pathZIPFoundationDataSerializationTests.swift
File metadata and controls
131 lines (124 loc) · 5.94 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
//
// ZIPFoundationDataSerializationTests.swift
// ZIPFoundation
//
// Copyright © 2017-2021 Thomas Zoechling, https://www.peakstep.com and the ZIP Foundation project authors.
// Released under the MIT License.
//
// See https://github.com/weichsel/ZIPFoundation/blob/master/LICENSE for license information.
//
import XCTest
@testable import ZIPFoundation
extension ZIPFoundationTests {
func testReadStructureErrorConditions() {
let processInfo = ProcessInfo.processInfo
let fileManager = FileManager()
var fileURL = ZIPFoundationTests.tempZipDirectoryURL
fileURL.appendPathComponent(processInfo.globallyUniqueString)
let result = fileManager.createFile(atPath: fileURL.path, contents: Data(),
attributes: nil)
XCTAssert(result == true)
let fileSystemRepresentation = fileManager.fileSystemRepresentation(withPath: fileURL.path)
let file: FILEPointer = fopen(fileSystemRepresentation, "rb")
// Close the file to exercise the error path during readStructure that deals with
// unreadable file data.
fclose(file)
let centralDirectoryStructure: Entry.CentralDirectoryStructure? = Data.readStruct(from: file, at: 0)
XCTAssertNil(centralDirectoryStructure)
}
func testReadChunkErrorConditions() {
let processInfo = ProcessInfo.processInfo
let fileManager = FileManager()
var fileURL = ZIPFoundationTests.tempZipDirectoryURL
fileURL.appendPathComponent(processInfo.globallyUniqueString)
let result = fileManager.createFile(atPath: fileURL.path, contents: Data(),
attributes: nil)
XCTAssert(result == true)
let fileSystemRepresentation = fileManager.fileSystemRepresentation(withPath: fileURL.path)
let file: FILEPointer = fopen(fileSystemRepresentation, "rb")
// Close the file to exercise the error path during readChunk that deals with
// unreadable file data.
fclose(file)
do {
_ = try Data.readChunk(of: 10, from: file)
} catch let error as Data.DataError {
XCTAssert(error == .unreadableFile)
} catch {
XCTFail("Unexpected error while testing to read from a closed file.")
}
}
func testWriteChunkErrorConditions() {
let processInfo = ProcessInfo.processInfo
let fileManager = FileManager()
var fileURL = ZIPFoundationTests.tempZipDirectoryURL
fileURL.appendPathComponent(processInfo.globallyUniqueString)
let result = fileManager.createFile(atPath: fileURL.path, contents: Data(),
attributes: nil)
XCTAssert(result == true)
let fileSystemRepresentation = fileManager.fileSystemRepresentation(withPath: fileURL.path)
let file: FILEPointer = fopen(fileSystemRepresentation, "rb")
// Close the file to exercise the error path during writeChunk that deals with
// unwritable files.
fclose(file)
do {
let dataWritten = try Data.write(chunk: Data(count: 10), to: file)
XCTAssert(dataWritten == 0)
} catch let error as Data.DataError {
XCTAssert(error == .unwritableFile)
} catch {
XCTFail("Unexpected error while testing to write into a closed file.")
}
}
func testCRC32Calculation() {
let dataURL = self.resourceURL(for: #function, pathExtension: "data")
let data = (try? Data.init(contentsOf: dataURL)) ?? Data()
XCTAssertEqual(data.crc32(checksum: 0), 1400077496)
#if canImport(zlib)
XCTAssertEqual(data.crc32(checksum: 0), data.builtInCRC32(checksum: 0))
#endif
}
func testWriteLargeChunk() {
let processInfo = ProcessInfo.processInfo
let fileManager = FileManager()
var fileURL = ZIPFoundationTests.tempZipDirectoryURL
fileURL.appendPathComponent(processInfo.globallyUniqueString)
let result = fileManager.createFile(atPath: fileURL.path, contents: Data(),
attributes: nil)
XCTAssert(result == true)
let fileSystemRepresentation = fileManager.fileSystemRepresentation(withPath: fileURL.path)
let file: FILEPointer = fopen(fileSystemRepresentation, "rb+")
let data = Data.makeRandomData(size: 1024)
do {
let writtenSize = try Data.writeLargeChunk(data, size: 1024, bufferSize: 256, to: file)
XCTAssertEqual(writtenSize, 1024)
fseeko(file, 0, SEEK_SET)
let writtenData = try Data.readChunk(of: Int(writtenSize), from: file)
XCTAssertEqual(writtenData, data)
} catch {
XCTFail("Unexpected error while testing to write into a closed file.")
}
}
func testWriteLargeChunkErrorConditions() {
let processInfo = ProcessInfo.processInfo
let fileManager = FileManager()
var fileURL = ZIPFoundationTests.tempZipDirectoryURL
fileURL.appendPathComponent(processInfo.globallyUniqueString)
let result = fileManager.createFile(atPath: fileURL.path, contents: Data(),
attributes: nil)
XCTAssert(result == true)
let fileSystemRepresentation = fileManager.fileSystemRepresentation(withPath: fileURL.path)
let file: FILEPointer = fopen(fileSystemRepresentation, "rb")
let data = Data.makeRandomData(size: 1024)
// Close the file to exercise the error path during writeChunk that deals with
// unwritable files.
fclose(file)
do {
let dataWritten = try Data.writeLargeChunk(data, size: 1024, bufferSize: 256, to: file)
XCTAssert(dataWritten == 0)
} catch let error as Data.DataError {
XCTAssert(error == .unwritableFile)
} catch {
XCTFail("Unexpected error while testing to write into a closed file.")
}
}
}