forked from weichsel/ZIPFoundation
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathZIPFoundationEntryTests+ZIP64.swift
174 lines (162 loc) · 11.3 KB
/
ZIPFoundationEntryTests+ZIP64.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
//
// ZIPFoundationEntryTests+ZIP64.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 {
typealias ZIP64ExtendedInformation = Entry.ZIP64ExtendedInformation
func testEntryZIP64ExtraField() {
let extraFieldBytesIncludingSizeFields: [UInt8] = [0x01, 0x00, 0x10, 0x00, 0x0a, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x0a, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00]
let zip64ExtraField1 = ZIP64ExtendedInformation(data: Data(extraFieldBytesIncludingSizeFields),
fields: [.compressedSize, .uncompressedSize])
XCTAssertNotNil(zip64ExtraField1)
let extraFieldBytesIncludingOtherFields: [UInt8] = [0x01, 0x00, 0x0c, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x0a, 0x0, 0x00, 0x00, 0x0a]
let zip64ExtraField2 = ZIP64ExtendedInformation(data: Data(extraFieldBytesIncludingOtherFields),
fields: [.relativeOffsetOfLocalHeader, .diskNumberStart])
XCTAssertNotNil(zip64ExtraField2)
let updatedZIP64ExtraField1 = ZIP64ExtendedInformation(zip64ExtendedInformation: zip64ExtraField1, offset: 10)
XCTAssertEqual(updatedZIP64ExtraField1?.relativeOffsetOfLocalHeader,
zip64ExtraField1?.relativeOffsetOfLocalHeader)
XCTAssertEqual(updatedZIP64ExtraField1?.dataSize, zip64ExtraField1?.dataSize)
let updatedZIP64ExtraField2 = ZIP64ExtendedInformation(zip64ExtendedInformation: zip64ExtraField2,
offset: UInt64(UInt32.max) + 1)
XCTAssertEqual(updatedZIP64ExtraField2?.relativeOffsetOfLocalHeader, UInt64(UInt32.max) + 1)
XCTAssertEqual(updatedZIP64ExtraField2?.dataSize, zip64ExtraField2?.dataSize)
}
func testEntryZIP64FieldOnlyHasUncompressedSize() {
// Including both original and compressed file size fields. (at least in the Local header)
let expectedZIP64ExtraFieldBytes: [UInt8] = [0x01, 0x00, 0x10, 0x00, 0x0a, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00]
let zip64Field = ZIP64ExtendedInformation(dataSize: 16,
uncompressedSize: 10,
compressedSize: 0,
relativeOffsetOfLocalHeader: 0,
diskNumberStart: 0)
XCTAssertEqual(zip64Field.data, Data(expectedZIP64ExtraFieldBytes))
}
func testEntryZIP64FieldIncludingDiskNumberStart() {
let expectedZIP64ExtraFieldBytes: [UInt8] = [0x01, 0x00, 0x04, 0x00, 0x0a, 0x00, 0x00, 0x00]
let zip64Field = ZIP64ExtendedInformation(dataSize: 4,
uncompressedSize: 0,
compressedSize: 0,
relativeOffsetOfLocalHeader: 0,
diskNumberStart: 10)
XCTAssertEqual(zip64Field.data, Data(expectedZIP64ExtraFieldBytes))
}
func testEntryValidZIP64DataDescriptor() {
let zip64DDBytes: [UInt8] = [0x50, 0x4b, 0x07, 0x08, 0x00, 0x00, 0x00, 0x00,
0x0a, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x0a, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00]
let zip64DataDescriptor = Entry.ZIP64DataDescriptor(data: Data(zip64DDBytes),
additionalDataProvider: {_ -> Data in
return Data() })
XCTAssertEqual(zip64DataDescriptor?.uncompressedSize, 10)
XCTAssertEqual(zip64DataDescriptor?.compressedSize, 10)
}
func testEntryWithZIP64ExtraField() {
// Central Directory
let extraFieldBytesIncludingSizeFields: [UInt8] = [0x01, 0x00, 0x10, 0x00, 0x0a, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x0a, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00]
let cdsBytes: [UInt8] = [0x50, 0x4b, 0x01, 0x02, 0x1e, 0x15, 0x2d, 0x00,
0x08, 0x08, 0x08, 0x00, 0xab, 0x85, 0x77, 0x47,
0x00, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0x01, 0x00, 0x14, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0xb0, 0x11, 0x00, 0x00, 0x00, 0x00]
guard let cds = Entry.CentralDirectoryStructure(data: Data(cdsBytes),
additionalDataProvider: { count -> Data in
guard let name = "/".data(using: .utf8) else {
throw AdditionalDataError.encodingError
}
let extra = name + Data(extraFieldBytesIncludingSizeFields)
XCTAssert(count == extra.count)
return extra
}) else {
XCTFail("Failed to read central directory structure."); return
}
XCTAssertNotNil(cds.extraFields)
XCTAssertEqual(cds.effectiveCompressedSize, 10)
XCTAssertEqual(cds.effectiveUncompressedSize, 10)
// Entry
let lfhBytes: [UInt8] = [0x50, 0x4b, 0x03, 0x04, 0x14, 0x00, 0x08, 0x08,
0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x0a, 0x00, 0x00, 0x00, 0x0a, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00]
guard let lfh = Entry.LocalFileHeader(data: Data(lfhBytes),
additionalDataProvider: { _ -> Data in
return Data()
}) else {
XCTFail("Failed to read local file header."); return
}
guard let entry = Entry(centralDirectoryStructure: cds, localFileHeader: lfh) else {
XCTFail("Failed to create test entry."); return
}
XCTAssertNotNil(entry.zip64ExtendedInformation)
XCTAssertEqual(entry.compressedSize, 10)
XCTAssertEqual(entry.uncompressedSize, 10)
}
func testEntryInvalidZIP64ExtraFieldErrorConditions() {
let emptyExtraField = ZIP64ExtendedInformation(data: Data(),
fields: [.compressedSize])
XCTAssertNil(emptyExtraField)
let extraFieldBytesIncludingExtraByte: [UInt8] = [0x01, 0x00, 0x10, 0x00, 0x0a, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x0a, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00]
let invalidExtraField1 = ZIP64ExtendedInformation(data: Data(extraFieldBytesIncludingExtraByte),
fields: [.compressedSize, .uncompressedSize])
XCTAssertNil(invalidExtraField1)
let extraFieldBytesMissingByte: [UInt8] = [0x01, 0x00, 0x10, 0x00, 0x0a, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x0a, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00]
let invalidExtraField2 = ZIP64ExtendedInformation(data: Data(extraFieldBytesMissingByte),
fields: [.compressedSize, .uncompressedSize])
XCTAssertNil(invalidExtraField2)
let extraFieldBytesWithWrongFields: [UInt8] = [0x01, 0x00, 0x10, 0x00, 0x0a, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x0a, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00]
let invalidExtraField3 = ZIP64ExtendedInformation(data: Data(extraFieldBytesWithWrongFields),
fields: [.compressedSize])
XCTAssertNil(invalidExtraField3)
let extraFieldBytesWithWrongFieldLength: [UInt8] = [0x01, 0x00, 0x10, 0x00, 0x0a, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00]
let invalidExtraField4 = ZIP64ExtendedInformation(data: Data(extraFieldBytesWithWrongFieldLength),
fields: [.diskNumberStart])
XCTAssertNil(invalidExtraField4)
}
func testEntryScanForZIP64Field() {
let extraFieldBytesWithZIP64Field: [UInt8] = [0x01, 0x00, 0x10, 0x00, 0x0a, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x0a, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00]
let zip64Field1 = ZIP64ExtendedInformation.scanForZIP64Field(in: Data(extraFieldBytesWithZIP64Field),
fields: [.uncompressedSize, .compressedSize])
XCTAssertNotNil(zip64Field1)
let extraFieldBytesWithZIP64Field2: [UInt8] = [0x09, 0x00, 0x04, 0x00, 0x0a, 0x00, 0x00, 0x00,
0x01, 0x00, 0x10, 0x00, 0x0a, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x0a, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00]
let zip64Field2 = ZIP64ExtendedInformation.scanForZIP64Field(in: Data(extraFieldBytesWithZIP64Field2),
fields: [.uncompressedSize, .compressedSize])
XCTAssertNotNil(zip64Field2)
}
func testEntryScanForZIP64FieldErrorConditions() {
let emptyExtraField = ZIP64ExtendedInformation.scanForZIP64Field(in: Data(), fields: [])
XCTAssertNil(emptyExtraField)
let extraFieldBytesWithoutZIP64Field: [UInt8] = [0x09, 0x00, 0x10, 0x00, 0x0a, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x0a, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00]
let noZIP64Field = ZIP64ExtendedInformation.scanForZIP64Field(in: Data(extraFieldBytesWithoutZIP64Field),
fields: [])
XCTAssertNil(noZIP64Field)
}
}