-
Notifications
You must be signed in to change notification settings - Fork 95
/
Copy pathIterateFileInfoTests.m
248 lines (190 loc) · 9.94 KB
/
IterateFileInfoTests.m
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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
//
// IterateFileInfoTests.m
// UnrarKit
//
// Created by Dov Frankel on 5/22/18.
//
//
#import "URKArchiveTestCase.h"
@interface IterateFileInfoTests : URKArchiveTestCase
@end
@implementation IterateFileInfoTests
- (void)testIterateFileInfo
{
NSArray *testArchives = @[@"Test Archive.rar", @"Test Archive (Password).rar"];
NSSet *expectedFileSet = [self.testFileURLs keysOfEntriesPassingTest:^BOOL(NSString *key, id obj, BOOL *stop) {
return ![key hasSuffix:@"rar"] && ![key hasSuffix:@"md"];
}];
NSArray *expectedFiles = [[expectedFileSet allObjects] sortedArrayUsingSelector:@selector(compare:)];
for (NSString *testArchiveName in testArchives) {
NSLog(@"Testing list files of archive %@", testArchiveName);
NSURL *testArchiveURL = self.testFileURLs[testArchiveName];
URKArchive *archive = [[URKArchive alloc] initWithURL:testArchiveURL error:nil];
NSError *error = nil;
NSMutableArray *iteratedFiles = [NSMutableArray array];
BOOL success = [archive iterateFileInfo:^(URKFileInfo * _Nonnull fileInfo, BOOL * _Nonnull stop) {
[iteratedFiles addObject:fileInfo.filename];
}
error:&error];
XCTAssertTrue(success, @"Error returned by iterateFileInfo");
XCTAssertNil(error, @"Error returned by iterateFileInfo");
XCTAssertEqual(iteratedFiles.count, expectedFileSet.count,
@"Incorrect number of files listed in archive");
for (NSInteger i = 0; i < iteratedFiles.count; i++) {
NSString *archiveFilename = iteratedFiles[i];
NSString *expectedFilename = expectedFiles[i];
NSLog(@"Testing for file %@", expectedFilename);
XCTAssertEqualObjects(archiveFilename, expectedFilename, @"Incorrect filename listed");
}
}
}
- (void)testIterateFileInfo_Unicode
{
NSSet *expectedFileSet = [self.unicodeFileURLs keysOfEntriesPassingTest:^BOOL(NSString *key, id obj, BOOL *stop) {
return ![key hasSuffix:@"rar"] && ![key hasSuffix:@"md"];
}];
NSArray *expectedFiles = [[expectedFileSet allObjects] sortedArrayUsingSelector:@selector(compare:)];
NSURL *testArchiveURL = self.unicodeFileURLs[@"Ⓣest Ⓐrchive.rar"];
URKArchive *archive = [[URKArchive alloc] initWithURL:testArchiveURL error:nil];
NSError *error = nil;
NSMutableArray *iteratedFiles = [NSMutableArray array];
BOOL success = [archive iterateFileInfo:^(URKFileInfo * _Nonnull fileInfo, BOOL * _Nonnull stop) {
[iteratedFiles addObject:fileInfo.filename];
}
error:&error];
XCTAssertTrue(success, @"Error returned by iterateFileInfo");
XCTAssertNil(error, @"Error returned by iterateFileInfo");
XCTAssertEqual(iteratedFiles.count, expectedFileSet.count,
@"Incorrect number of files listed in archive");
for (NSInteger i = 0; i < iteratedFiles.count; i++) {
NSString *archiveFilename = iteratedFiles[i];
NSString *expectedFilename = expectedFiles[i];
XCTAssertEqualObjects(archiveFilename, expectedFilename, @"Incorrect filename listed");
}
}
- (void)testIterateFileInfo_RAR5
{
NSArray *expectedFiles = @[@"yohoho_ws.txt",
@"nopw.txt"];
NSURL *testArchiveURL = self.testFileURLs[@"Test Archive (RAR5).rar"];
URKArchive *archive = [[URKArchive alloc] initWithURL:testArchiveURL error:nil];
NSError *error = nil;
NSMutableArray *iteratedFiles = [NSMutableArray array];
BOOL success = [archive iterateFileInfo:^(URKFileInfo * _Nonnull fileInfo, BOOL * _Nonnull stop) {
[iteratedFiles addObject:fileInfo.filename];
}
error:&error];
XCTAssertTrue(success, @"Error returned by iterateFileInfo");
XCTAssertNil(error, @"Error returned by iterateFileInfo");
XCTAssertEqual(iteratedFiles.count, expectedFiles.count,
@"Incorrect number of files listed in archive");
for (NSInteger i = 0; i < iteratedFiles.count; i++) {
NSString *archiveFilename = iteratedFiles[i];
NSString *expectedFilename = expectedFiles[i];
XCTAssertEqualObjects(archiveFilename, expectedFilename, @"Incorrect filename listed");
}
}
- (void)testIterateFileInfo_HeaderPassword
{
NSArray *testArchives = @[@"Test Archive (Header Password).rar"];
NSSet *expectedFileSet = [self.testFileURLs keysOfEntriesPassingTest:^BOOL(NSString *key, id obj, BOOL *stop) {
return ![key hasSuffix:@"rar"] && ![key hasSuffix:@"md"];
}];
NSArray *expectedFiles = [[expectedFileSet allObjects] sortedArrayUsingSelector:@selector(compare:)];
for (NSString *testArchiveName in testArchives) {
NSLog(@"Testing list files of archive %@", testArchiveName);
NSURL *testArchiveURL = self.testFileURLs[testArchiveName];
URKArchive *archiveNoPassword = [[URKArchive alloc] initWithURL:testArchiveURL error:nil];
NSError *passwordError = nil;
NSArray *filesInArchive = [archiveNoPassword listFilenames:&passwordError];
XCTAssertNotNil(passwordError, @"No error returned by listFilenames (no password given)");
XCTAssertNil(filesInArchive, @"List of files returned (no password given)");
URKArchive *archive = [[URKArchive alloc] initWithURL:testArchiveURL password:@"password" error:nil];
NSError *error = nil;
NSMutableArray *iteratedFiles = [NSMutableArray array];
BOOL success = [archive iterateFileInfo:^(URKFileInfo * _Nonnull fileInfo, BOOL * _Nonnull stop) {
[iteratedFiles addObject:fileInfo.filename];
}
error:&error];
XCTAssertTrue(success, @"Error returned by iterateFileInfo");
XCTAssertNil(error, @"Error returned by iterateFileInfo");
XCTAssertEqual(iteratedFiles.count, expectedFiles.count,
@"Incorrect number of files listed in archive");
for (NSInteger i = 0; i < iteratedFiles.count; i++) {
NSString *archiveFilename = iteratedFiles[i];
NSString *expectedFilename = expectedFiles[i];
NSLog(@"Testing for file %@", expectedFilename);
XCTAssertEqualObjects(archiveFilename, expectedFilename, @"Incorrect filename listed");
}
}
}
- (void)testIterateFileInfo_NoHeaderPasswordGiven
{
URKArchive *archive = [[URKArchive alloc] initWithURL:self.testFileURLs[@"Test Archive (Header Password).rar"] error:nil];
NSError *error = nil;
__block BOOL called = NO;
BOOL success = [archive iterateFileInfo:^(URKFileInfo * _Nonnull fileInfo, BOOL * _Nonnull stop) {
called = YES;
}
error:&error];
XCTAssertNotNil(error, @"Iteration without password returned no error");
XCTAssertFalse(success, @"Iteration without password succeeded");
XCTAssertFalse(called, @"Iteration without password called action block");
XCTAssertEqual(error.code, URKErrorCodeMissingPassword, @"Unexpected error code returned");
}
- (void)testIterateFileInfo_NoFilePasswordGiven
{
URKArchive *archive = [[URKArchive alloc] initWithURL:self.testFileURLs[@"Test Archive (Password).rar"] error:nil];
NSError *error = nil;
__block BOOL called = NO;
BOOL success = [archive iterateFileInfo:^(URKFileInfo * _Nonnull fileInfo, BOOL * _Nonnull stop) {
called = YES;
}
error:&error];
XCTAssertNil(error, @"Iteration without file password failed");
XCTAssertTrue(success, @"Iteration without file password failed");
XCTAssertTrue(called, @"Iteration without file password didn't call action block");
}
- (void)testIterateFileInfo_InvalidArchive
{
URKArchive *archive = [[URKArchive alloc] initWithURL:self.testFileURLs[@"Test File A.txt"] error:nil];
NSError *error = nil;
__block BOOL called = NO;
BOOL success = [archive iterateFileInfo:^(URKFileInfo * _Nonnull fileInfo, BOOL * _Nonnull stop) {
called = YES;
}
error:&error];
XCTAssertNotNil(error, @"Iteration of invalid archive succeeded");
XCTAssertFalse(success, @"Iteration for invalid archive succeeded");
XCTAssertFalse(called, @"Iteration for invalid archive called action block");
XCTAssertEqual(error.code, URKErrorCodeBadArchive, @"Unexpected error code returned");
}
- (void)testIterateFileInfo_ModifiedCRC
{
URKArchive *archive = [[URKArchive alloc] initWithURL:self.testFileURLs[@"Modified CRC Archive.rar"] error:nil];
NSError *error = nil;
__block BOOL called = NO;
BOOL success = [archive iterateFileInfo:^(URKFileInfo * _Nonnull fileInfo, BOOL * _Nonnull stop) {
called = YES;
}
error:&error];
XCTAssertNotNil(error, @"Iteration of invalid archive succeeded");
XCTAssertFalse(success, @"Iteration for invalid archive succeeded");
XCTAssertTrue(called, @"Iteration for invalid archive called action block");
XCTAssertEqual(error.code, URKErrorCodeBadData, @"Unexpected error code returned");
}
- (void)testIterateFileInfo_ModifiedCRC_IgnoringMismatches
{
URKArchive *archive = [[URKArchive alloc] initWithURL:self.testFileURLs[@"Modified CRC Archive.rar"] error:nil];
archive.ignoreCRCMismatches = YES;
NSError *error = nil;
__block int calledCount = 0;
BOOL iterateSuccess = [archive iterateFileInfo:^(URKFileInfo * _Nonnull fileInfo, BOOL * _Nonnull stop) {
calledCount = 1;
}
error:&error];
XCTAssertNil(error, @"Modified CRC not ignored");
XCTAssertTrue(iterateSuccess, @"Iteration for invalid archive succeeded");
XCTAssertEqual(1, calledCount, @"Iterated too many times for archive with modified CRC");
}
@end