-
Notifications
You must be signed in to change notification settings - Fork 95
/
Copy pathListFileInfoTests.m
213 lines (157 loc) · 9.11 KB
/
ListFileInfoTests.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
//
// ListFileInfoTests.m
// UnrarKit
//
//
#import "URKArchiveTestCase.h"
@interface ListFileInfoTests : URKArchiveTestCase
@end
@implementation ListFileInfoTests
- (void)testListFileInfo {
URKArchive *archive = [[URKArchive alloc] initWithURL:self.testFileURLs[@"Test Archive.rar"] error:nil];
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:)];
static NSDateFormatter *testFileInfoDateFormatter;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
testFileInfoDateFormatter = [[NSDateFormatter alloc] init];
testFileInfoDateFormatter.dateFormat = @"M/dd/yyyy h:mm a";
testFileInfoDateFormatter.locale = [NSLocale localeWithLocaleIdentifier:@"en_US"];
});
NSDictionary *expectedTimestamps = @{@"Test File A.txt": [testFileInfoDateFormatter dateFromString:@"3/13/2014 8:02 PM"],
@"Test File B.jpg": [testFileInfoDateFormatter dateFromString:@"3/13/2014 8:04 PM"],
@"Test File C.m4a": [testFileInfoDateFormatter dateFromString:@"3/13/2014 8:05 PM"],};
NSError *error = nil;
NSArray *filesInArchive = [archive listFileInfo:&error];
XCTAssertNil(error, @"Error returned by listFileInfo");
XCTAssertNotNil(filesInArchive, @"No list of files returned");
XCTAssertEqual(filesInArchive.count, expectedFileSet.count, @"Incorrect number of files listed in archive");
NSFileManager *fm = [NSFileManager defaultManager];
for (NSInteger i = 0; i < filesInArchive.count; i++) {
URKFileInfo *fileInfo = filesInArchive[i];
// Test Archive Name
NSString *expectedArchiveName = archive.filename;
XCTAssertEqualObjects(fileInfo.archiveName, expectedArchiveName, @"Incorrect archive name");
// Test Filename
NSString *expectedFilename = expectedFiles[i];
XCTAssertEqualObjects(fileInfo.filename, expectedFilename, @"Incorrect filename");
// Test CRC
NSUInteger expectedFileCRC = [self crcOfTestFile:expectedFilename];
XCTAssertEqual(fileInfo.CRC, expectedFileCRC, @"Incorrect CRC checksum");
// Test Last Modify Date
NSTimeInterval archiveFileTimeInterval = [fileInfo.timestamp timeIntervalSinceReferenceDate];
NSTimeInterval expectedFileTimeInterval = [expectedTimestamps[fileInfo.filename] timeIntervalSinceReferenceDate];
XCTAssertEqualWithAccuracy(archiveFileTimeInterval, expectedFileTimeInterval, 60, @"Incorrect file timestamp (more than 60 seconds off)");
// Test Uncompressed Size
NSError *attributesError = nil;
NSString *expectedFilePath = [[self urlOfTestFile:expectedFilename] path];
NSDictionary *expectedFileAttributes = [fm attributesOfItemAtPath:expectedFilePath
error:&attributesError];
XCTAssertNil(attributesError, @"Error getting file attributes of %@", expectedFilename);
long long expectedFileSize = expectedFileAttributes.fileSize;
XCTAssertEqual(fileInfo.uncompressedSize, expectedFileSize, @"Incorrect uncompressed file size");
// Test Compression method
XCTAssertEqual(fileInfo.compressionMethod, URKCompressionMethodNormal, @"Incorrect compression method");
// Test Host OS
XCTAssertEqual(fileInfo.hostOS, URKHostOSUnix, @"Incorrect host OS");
}
}
- (void)testListFileInfo_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;
NSArray *filesInArchive = [archive listFileInfo:&error];
XCTAssertNil(error, @"Error returned by listFileInfo");
XCTAssertNotNil(filesInArchive, @"No list of files returned");
XCTAssertEqual(filesInArchive.count, expectedFileSet.count,
@"Incorrect number of files listed in archive");
for (NSInteger i = 0; i < filesInArchive.count; i++) {
URKFileInfo *fileInfo = (URKFileInfo *)filesInArchive[i];
XCTAssertEqualObjects(fileInfo.filename, expectedFiles[i], @"Incorrect filename listed");
XCTAssertEqualObjects(fileInfo.archiveName, archive.filename, @"Incorrect archiveName listed");
}
}
#if !TARGET_OS_IPHONE
- (void)testListFileInfo_MultivolumeArchive {
NSArray<NSURL*> *generatedVolumeURLs = [self multiPartArchiveWithName:@"ListFileInfoTests_MultivolumeArchive.rar"];
URKArchive *archive = [[URKArchive alloc] initWithURL:generatedVolumeURLs.firstObject error:nil];
NSError *error = nil;
NSArray *files = [archive listFileInfo:&error];
XCTAssertNil(error, @"Error returned when listing file info for multivolume archive");
XCTAssertEqual(files.count, 1, @"Incorrect number of file info items returned for multivolume archive");
}
#endif
- (void)testListFileInfo_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) {
NSURL *testArchiveURL = self.testFileURLs[testArchiveName];
URKArchive *archiveNoPassword = [[URKArchive alloc] initWithURL:testArchiveURL error:nil];
NSError *error = nil;
NSArray *filesInArchive = [archiveNoPassword listFileInfo:&error];
XCTAssertNotNil(error, @"No error returned by listFileInfo (no password given)");
XCTAssertNil(filesInArchive, @"List of files returned (no password given)");
URKArchive *archive = [[URKArchive alloc] initWithURL:testArchiveURL password:@"password" error:nil];
filesInArchive = nil;
error = nil;
filesInArchive = [archive listFileInfo:&error];
XCTAssertNil(error, @"Error returned by listFileInfo");
XCTAssertEqual(filesInArchive.count, expectedFileSet.count,
@"Incorrect number of files listed in archive");
for (NSInteger i = 0; i < filesInArchive.count; i++) {
URKFileInfo *archiveFileInfo = filesInArchive[i];
NSString *archiveFilename = archiveFileInfo.filename;
NSString *expectedFilename = expectedFiles[i];
XCTAssertEqualObjects(archiveFilename, expectedFilename, @"Incorrect filename listed");
}
}
}
- (void)testListFileInfo_NoHeaderPasswordGiven {
URKArchive *archive = [[URKArchive alloc] initWithURL:self.testFileURLs[@"Test Archive (Header Password).rar"] error:nil];
NSError *error = nil;
NSArray *files = [archive listFileInfo:&error];
XCTAssertNotNil(error, @"List without password succeeded");
XCTAssertNil(files, @"List returned without password");
XCTAssertEqual(error.code, URKErrorCodeMissingPassword, @"Unexpected error code returned");
}
- (void)testListFileInfo_InvalidArchive
{
URKArchive *archive = [[URKArchive alloc] initWithURL:self.testFileURLs[@"Test File A.txt"] error:nil];
NSError *error = nil;
NSArray *files = [archive listFileInfo:&error];
XCTAssertNotNil(error, @"List files of invalid archive succeeded");
XCTAssertNil(files, @"List returned for invalid archive");
XCTAssertEqual(error.code, URKErrorCodeBadArchive, @"Unexpected error code returned");
}
- (void)testListFileInfo_ModifiedCRC
{
URKArchive *archive = [[URKArchive alloc] initWithURL:self.testFileURLs[@"Modified CRC Archive.rar"] error:nil];
NSError *error = nil;
NSArray *files = [archive listFileInfo:&error];
XCTAssertNotNil(error, @"List files of invalid archive succeeded");
XCTAssertNil(files, @"List returned for invalid archive");
XCTAssertEqual(error.code, URKErrorCodeBadData, @"Unexpected error code returned");
}
- (void)testListFileInfo_ModifiedCRC_IgnoringMismatches
{
URKArchive *archive = [[URKArchive alloc] initWithURL:self.testFileURLs[@"Modified CRC Archive.rar"] error:nil];
archive.ignoreCRCMismatches = YES;
NSError *error = nil;
NSArray<URKFileInfo*> *files = [archive listFileInfo:&error];
XCTAssertNil(error, @"List files of invalid archive succeeded");
XCTAssertNotNil(files, @"List returned for invalid archive");
XCTAssertEqual(files.count, 1);
XCTAssertEqualObjects(files[0].filename, @"README.md");
}
@end