forked from weichsel/ZIPFoundation
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathZIPFoundationEntryTests.swift
211 lines (201 loc) · 11 KB
/
ZIPFoundationEntryTests.swift
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
//
// ZIPFoundationEntryTests.swift
// ZIPFoundation
//
// Copyright © 2017-2023 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 testEntryWrongDataLengthErrorConditions() {
let emptyCDS = Entry.CentralDirectoryStructure(data: Data(),
additionalDataProvider: {_ -> Data in
return Data() })
XCTAssertNil(emptyCDS)
let emptyLFH = Entry.LocalFileHeader(data: Data(),
additionalDataProvider: {_ -> Data in
return Data() })
XCTAssertNil(emptyLFH)
let emptyDD = Entry.DefaultDataDescriptor(data: Data(),
additionalDataProvider: {_ -> Data in
return Data() })
XCTAssertNil(emptyDD)
let emptyZIP64DD = Entry.ZIP64DataDescriptor(data: Data(),
additionalDataProvider: {_ -> Data in
return Data() })
XCTAssertNil(emptyZIP64DD)
}
func testEntryInvalidSignatureErrorConditions() {
let invalidCDS = Entry.CentralDirectoryStructure(data: Data(count: Entry.CentralDirectoryStructure.size),
additionalDataProvider: {_ -> Data in
return Data() })
XCTAssertNil(invalidCDS)
let invalidLFH = Entry.LocalFileHeader(data: Data(count: Entry.LocalFileHeader.size),
additionalDataProvider: {_ -> Data in
return Data() })
XCTAssertNil(invalidLFH)
}
func testEntryInvalidAdditionalDataErrorConditions() {
let cdsBytes: [UInt8] = [0x50, 0x4b, 0x01, 0x02, 0x1e, 0x03, 0x14, 0x00,
0x08, 0x00, 0x08, 0x00, 0xab, 0x85, 0x77, 0x47,
0x00, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0xb0, 0x11, 0x00, 0x00, 0x00, 0x00]
let invalidAddtionalDataCDS = Entry.CentralDirectoryStructure(data: Data(cdsBytes)) { _ -> Data in
return Data()
}
XCTAssertNil(invalidAddtionalDataCDS)
let lfhBytes: [UInt8] = [0x50, 0x4b, 0x03, 0x04, 0x14, 0x00, 0x08, 0x00,
0x08, 0x00, 0xab, 0x85, 0x77, 0x47, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x01, 0x00, 0x00, 0x00]
let invalidAddtionalDataLFH = Entry.LocalFileHeader(data: Data(lfhBytes)) { _ -> Data in
return Data()
}
XCTAssertNil(invalidAddtionalDataLFH)
let cds2Bytes: [UInt8] = [0x50, 0x4b, 0x01, 0x02, 0x1e, 0x03, 0x14, 0x00,
0x08, 0x08, 0x08, 0x00, 0xab, 0x85, 0x77, 0x47,
0x00, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0xb0, 0x11, 0x00, 0x00, 0x00, 0x00]
let cds2 = Entry.CentralDirectoryStructure(data: Data(cds2Bytes)) { _ -> Data in
throw AdditionalDataError.encodingError
}
XCTAssertNil(cds2)
let lfhBytes2: [UInt8] = [0x50, 0x4b, 0x03, 0x04, 0x14, 0x00, 0x08, 0x08,
0x08, 0x00, 0xab, 0x85, 0x77, 0x47, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x01, 0x00, 0x00, 0x00]
let lfh2 = Entry.LocalFileHeader(data: Data(lfhBytes2)) { _ -> Data in
throw AdditionalDataError.encodingError
}
XCTAssertNil(lfh2)
}
func testEntryInvalidPathEncodingErrorConditions() {
// Use bytes that are invalid code units in UTF-8 to trigger failed initialization
// of the path String.
let invalidPathBytes: [UInt8] = [0xFF]
let cdsBytes: [UInt8] = [0x50, 0x4b, 0x01, 0x02, 0x1e, 0x03, 0x14, 0x00,
0x08, 0x08, 0x08, 0x00, 0xab, 0x85, 0x77, 0x47,
0x00, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0xb0, 0x11, 0x00, 0x00, 0x00, 0x00]
let cds = Entry.CentralDirectoryStructure(data: Data(cdsBytes)) { _ -> Data in
return Data(invalidPathBytes)
}
let lfhBytes: [UInt8] = [0x50, 0x4b, 0x03, 0x04, 0x14, 0x00, 0x08, 0x08,
0x08, 0x00, 0xab, 0x85, 0x77, 0x47, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x01, 0x00, 0x00, 0x00]
let lfh = Entry.LocalFileHeader(data: Data(lfhBytes)) { _ -> Data in
return Data(invalidPathBytes)
}
guard let central = cds else {
XCTFail("Failed to read central directory structure.")
return
}
guard let local = lfh else {
XCTFail("Failed to read local file header.")
return
}
guard let entry = Entry(centralDirectoryStructure: central, localFileHeader: local) else {
XCTFail("Failed to read entry.")
return
}
XCTAssertTrue(entry.path == "")
}
func testEntryMissingDataDescriptorErrorCondition() {
let cdsBytes: [UInt8] = [0x50, 0x4b, 0x01, 0x02, 0x1e, 0x03, 0x14, 0x00,
0x08, 0x08, 0x08, 0x00, 0xab, 0x85, 0x77, 0x47,
0x00, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0xb0, 0x11, 0x00, 0x00, 0x00, 0x00]
let cds = Entry.CentralDirectoryStructure(data: Data(cdsBytes)) { _ -> Data in
return Data()
}
let lfhBytes: [UInt8] = [0x50, 0x4b, 0x03, 0x04, 0x14, 0x00, 0x08, 0x08,
0x08, 0x00, 0xab, 0x85, 0x77, 0x47, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00]
let lfh = Entry.LocalFileHeader(data: Data(lfhBytes)) { _ -> Data in
return Data()
}
guard let central = cds else {
XCTFail("Failed to read central directory structure.")
return
}
guard let local = lfh else {
XCTFail("Failed to read local file header.")
return
}
guard let entry = Entry(centralDirectoryStructure: central, localFileHeader: local) else {
XCTFail("Failed to read entry.")
return
}
XCTAssertTrue(entry.checksum == 0)
}
func testEntryTypeDetectionHeuristics() {
// Set the upper byte of .versionMadeBy to 0x15.
// This exercises the code path that deals with invalid OSTypes.
let cdsBytes: [UInt8] = [0x50, 0x4b, 0x01, 0x02, 0x1e, 0x15, 0x14, 0x00,
0x08, 0x08, 0x08, 0x00, 0xab, 0x85, 0x77, 0x47,
0x00, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0xb0, 0x11, 0x00, 0x00, 0x00, 0x00]
let cds = Entry.CentralDirectoryStructure(data: Data(cdsBytes)) { _ -> Data in
guard let pathData = "/".data(using: .utf8) else { throw AdditionalDataError.encodingError }
return pathData
}
let lfhBytes: [UInt8] = [0x50, 0x4b, 0x03, 0x04, 0x14, 0x00, 0x08, 0x08,
0x08, 0x00, 0xab, 0x85, 0x77, 0x47, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x01, 0x00, 0x00, 0x00]
let lfh = Entry.LocalFileHeader(data: Data(lfhBytes)) { _ -> Data in
guard let pathData = "/".data(using: .utf8) else { throw AdditionalDataError.encodingError }
return pathData
}
guard let central = cds else {
XCTFail("Failed to read central directory structure.")
return
}
guard let local = lfh else {
XCTFail("Failed to read local file header.")
return
}
guard let entry = Entry(centralDirectoryStructure: central, localFileHeader: local) else {
XCTFail("Failed to read entry.")
return
}
XCTAssertTrue(entry.type == .directory)
}
func testEntryValidDataDescriptor() {
let ddBytes: [UInt8] = [0x50, 0x4b, 0x07, 0x08, 0x00, 0x00, 0x00, 0x00,
0x0a, 0x00, 0x00, 0x00, 0x0a, 0x00, 0x00, 0x00]
let dataDescriptor = Entry.DefaultDataDescriptor(data: Data(ddBytes),
additionalDataProvider: {_ -> Data in
return Data() })
XCTAssertEqual(dataDescriptor?.uncompressedSize, 10)
XCTAssertEqual(dataDescriptor?.compressedSize, 10)
// The DataDescriptor signature is not mandatory.
let ddBytesWithoutSignature: [UInt8] = [0x00, 0x00, 0x00, 0x00, 0x0a, 0x00, 0x00, 0x00,
0x0a, 0x00, 0x00, 0x00, 0x50, 0x4b, 0x07, 0x08]
let dataDescriptorWithoutSignature = Entry.DefaultDataDescriptor(data: Data(ddBytesWithoutSignature),
additionalDataProvider: {_ -> Data in
return Data() })
XCTAssertEqual(dataDescriptorWithoutSignature?.uncompressedSize, 10)
XCTAssertEqual(dataDescriptorWithoutSignature?.compressedSize, 10)
}
func testEntryIsCompressed() throws {
let archive = self.archive(for: #function, mode: .read)
XCTAssert(archive["compressed"]?.isCompressed == true)
XCTAssert(archive["uncompressed"]?.isCompressed == false)
}
}