This repository has been archived by the owner on Aug 12, 2022. It is now read-only.
/
ZIPTests.swift
186 lines (162 loc) · 6.76 KB
/
ZIPTests.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
//
// ZIPTests.swift
// r2-shared-swift
//
// Created by Mickaël Menu on 15/04/2020.
//
// Copyright 2020 Readium Foundation. All rights reserved.
// Use of this source code is governed by a BSD-style license which is detailed
// in the LICENSE file present in the project repository where this source code is maintained.
//
import XCTest
@testable import R2Shared
fileprivate let fixtures = Fixtures(path: "ZIP")
struct ZIPTester<Archive: ZIPArchive> {
func testOpenSuccess() {
XCTAssertNoThrow(try Archive(file: fixtures.url(for: "test.zip")))
}
func testOpenNotFound() {
XCTAssertThrowsError(try Archive(file: fixtures.url(for: "unknown.zip"))) { error in
XCTAssertEqual(error as? ZIPError, ZIPError.openFailed)
}
}
func testOpenNotAZIP() {
XCTAssertThrowsError(try Archive(file: fixtures.url(for: "not-a.zip"))) { error in
XCTAssertEqual(error as? ZIPError, ZIPError.openFailed)
}
}
func testGetNonExistingEntry() {
let archive = try! Archive(file: fixtures.url(for: "test.zip"))
let entry = archive.entry(at: "unknown")
XCTAssertNil(entry)
}
func testGetFileEntry() {
let archive = try! Archive(file: fixtures.url(for: "test.zip"))
XCTAssertEqual(
archive.entry(at: "A folder/wasteland-cover.jpg"),
ZIPEntry(
path: "A folder/wasteland-cover.jpg",
isDirectory: false,
length: 103477,
compressedLength: 82374
)
)
}
func testGetUncompressedFileEntry() {
let archive = try! Archive(file: fixtures.url(for: "test.zip"))
XCTAssertEqual(
archive.entry(at: "uncompressed.jpg"),
ZIPEntry(
path: "uncompressed.jpg",
isDirectory: false,
length: 279551,
compressedLength: 279551
)
)
}
func testGetDirectoryEntry() {
let archive = try! Archive(file: fixtures.url(for: "test.zip"))
XCTAssertNil(archive.entry(at: "A folder"))
XCTAssertEqual(
archive.entry(at: "A folder/"),
ZIPEntry(
path: "A folder/",
isDirectory: true,
length: 0,
compressedLength: 0
)
)
}
func testGetEntries() {
let archive = try! Archive(file: fixtures.url(for: "test.zip"))
XCTAssertEqual(archive.entries, [
ZIPEntry(path: ".hidden", isDirectory: false, length: 0, compressedLength: 0),
ZIPEntry(path: "A folder/", isDirectory: true, length: 0, compressedLength: 0),
ZIPEntry(path: "A folder/Sub.folder%/", isDirectory: true, length: 0, compressedLength: 0),
ZIPEntry(path: "A folder/Sub.folder%/file.txt", isDirectory: false, length: 20, compressedLength: 20),
ZIPEntry(path: "A folder/wasteland-cover.jpg", isDirectory: false, length: 103477, compressedLength: 82374),
ZIPEntry(path: "root.txt", isDirectory: false, length: 0, compressedLength: 0),
ZIPEntry(path: "uncompressed.jpg", isDirectory: false, length: 279551, compressedLength: 279551),
ZIPEntry(path: "uncompressed.txt", isDirectory: false, length: 30, compressedLength: 30)
])
}
func testReadCompressedEntry() {
let archive = try! Archive(file: fixtures.url(for: "test.zip"))
let entry = archive.entry(at: "A folder/Sub.folder%/file.txt")!
let data = archive.read(at: entry.path)
XCTAssertNotNil(data)
XCTAssertEqual(
String(data: data!, encoding: .utf8),
"I'm inside\nthe ZIP.\n"
)
}
func testReadUncompressedEntry() {
let archive = try! Archive(file: fixtures.url(for: "test.zip"))
let entry = archive.entry(at: "uncompressed.txt")!
let data = archive.read(at: entry.path)
XCTAssertNotNil(data)
XCTAssertEqual(
String(data: data!, encoding: .utf8),
"This content is uncompressed.\n"
)
}
func testReadRange() {
let archive = try! Archive(file: fixtures.url(for: "test.zip"))
let entry = archive.entry(at: "A folder/Sub.folder%/file.txt")!
let data = archive.read(at: entry.path, range: (entry.length - 6)..<entry.length)
XCTAssertNotNil(data)
XCTAssertEqual(
String(data: data!, encoding: .utf8),
" ZIP.\n"
)
}
}
//class ZIPFoundationTests: XCTestCase {
//
// lazy var tester = ZIPTester<ZIPFoundationArchive>()
//
// func testOpenSuccess() { tester.testOpenSuccess() }
// func testOpenNotFound() { tester.testOpenNotFound() }
// func testOpenNotAZIP() { tester.testOpenNotAZIP() }
// func testGetNonExistingEntry() { tester.testGetNonExistingEntry() }
// func testGetFileEntry() { tester.testGetFileEntry() }
// func testGetUncompressedFileEntry() { tester.testGetUncompressedFileEntry() }
// func testGetDirectoryEntry() { tester.testGetDirectoryEntry() }
// func testGetEntries() { tester.testGetEntries() }
// func testReadCompressedEntry() { tester.testReadCompressedEntry() }
// func testReadUncompressedEntry() { tester.testReadUncompressedEntry() }
// func testReadRange() { tester.testReadRange() }
//
//}
class MinizipTests: XCTestCase {
lazy var tester = ZIPTester<MinizipArchive>()
func testOpenSuccess() { tester.testOpenSuccess() }
func testOpenNotFound() { tester.testOpenNotFound() }
func testOpenNotAZIP() { tester.testOpenNotAZIP() }
func testGetNonExistingEntry() { tester.testGetNonExistingEntry() }
func testGetFileEntry() { tester.testGetFileEntry() }
func testGetUncompressedFileEntry() { tester.testGetUncompressedFileEntry() }
func testGetDirectoryEntry() { tester.testGetDirectoryEntry() }
func testGetEntries() { tester.testGetEntries() }
func testReadCompressedEntry() { tester.testReadCompressedEntry() }
func testReadUncompressedEntry() { tester.testReadUncompressedEntry() }
func testReadRange() { tester.testReadRange() }
}
class ZIPBenchmarkingTests: XCTestCase {
func testCompareRange() {
let archives: [ZIPArchive] = [
try! MinizipArchive(file: fixtures.url(for: "test.zip")),
// try! ZIPFoundationArchive(file: fixtures.url(for: "test.zip"))
]
let path = "A folder/wasteland-cover.jpg"
let length = 103477
measure {
let lower = Int.random(in: 0..<length - 100)
let upper = Int.random(in: lower..<length)
let range = lower..<upper
let datas = archives.map { $0.read(at: path, range: range) }
let data = datas[0]
XCTAssertTrue(datas.allSatisfy { $0 == data })
}
}
}