-
Notifications
You must be signed in to change notification settings - Fork 95
/
Copy pathCheckDataTests.m
209 lines (164 loc) · 9.03 KB
/
CheckDataTests.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
//
// CheckDataTests.m
// UnrarKit
//
// Created by Dov Frankel on 10/6/17.
//
#import "URKArchiveTestCase.h"
@interface CheckDataTests : URKArchiveTestCase @end
@implementation CheckDataTests
#pragma mark - checkDataIntegrity
- (void)testCheckDataIntegrity {
NSArray *testArchives = @[@"Test Archive.rar",
@"Test Archive (Password).rar",
@"Test Archive (Header Password).rar"];
for (NSString *testArchiveName in testArchives) {
NSLog(@"Testing data integrity of archive %@", testArchiveName);
NSURL *testArchiveURL = self.testFileURLs[testArchiveName];
NSString *password = ([testArchiveName rangeOfString:@"Password"].location != NSNotFound
? @"password"
: nil);
URKArchive *archive = [[URKArchive alloc] initWithURL:testArchiveURL password:password error:nil];
BOOL success = [archive checkDataIntegrity];
XCTAssertTrue(success, @"Data integrity check failed for %@", testArchiveName);
}
}
- (void)testCheckDataIntegrity_NotAnArchive {
NSURL *testArchiveURL = self.testFileURLs[@"Test File B.jpg"];
URKArchive *archive = [[URKArchive alloc] initWithURL:testArchiveURL error:nil];
BOOL success = [archive checkDataIntegrity];
XCTAssertFalse(success, @"Data integrity check passed for non-archive");
}
- (void)testCheckDataIntegrity_ModifiedCRC {
NSURL *testArchiveURL = self.testFileURLs[@"Modified CRC Archive.rar"];
URKArchive *archive = [[URKArchive alloc] initWithURL:testArchiveURL error:nil];
BOOL success = [archive checkDataIntegrity];
XCTAssertFalse(success, @"Data integrity check passed for archive with a modified CRC");
}
- (void)testCheckDataIntegrity_ModifiedCRC_IgnoringMismatches {
NSURL *testArchiveURL = self.testFileURLs[@"Modified CRC Archive.rar"];
URKArchive *archive = [[URKArchive alloc] initWithURL:testArchiveURL error:nil];
archive.ignoreCRCMismatches = YES;
// Still expect failure, so a consumer can still tell that data is mismatched
BOOL success = [archive checkDataIntegrity];
XCTAssertFalse(success, @"Data integrity check passed for archive with a modified CRC");
}
#pragma mark - checkDataIntegrityOfFile
- (void)testCheckDataIntegrityForFile {
NSArray *testArchives = @[@"Test Archive.rar",
@"Test Archive (Password).rar",
@"Test Archive (Header Password).rar"];
for (NSString *testArchiveName in testArchives) {
NSLog(@"Testing data integrity of file in archive %@", testArchiveName);
NSURL *testArchiveURL = self.testFileURLs[testArchiveName];
NSString *password = ([testArchiveName rangeOfString:@"Password"].location != NSNotFound
? @"password"
: nil);
URKArchive *archive = [[URKArchive alloc] initWithURL:testArchiveURL password:password error:nil];
NSError *listFilenamesError = nil;
NSArray <NSString*> *filenames = [archive listFilenames:&listFilenamesError];
XCTAssertNotNil(filenames, @"No file info returned for %@", testArchiveName);
XCTAssertNil(listFilenamesError, @"Error returned for %@: %@", testArchiveName, listFilenamesError);
NSString *firstFilename = filenames.firstObject;
BOOL success = [archive checkDataIntegrityOfFile:firstFilename];
XCTAssertTrue(success, @"Data integrity check failed for %@ in %@", firstFilename, testArchiveName);
}
}
- (void)testCheckDataIntegrityForFile_NotAnArchive {
NSURL *testArchiveURL = self.testFileURLs[@"Test File B.jpg"];
URKArchive *archive = [[URKArchive alloc] initWithURL:testArchiveURL error:nil];
BOOL success = [archive checkDataIntegrityOfFile:@"README.md"];
XCTAssertFalse(success, @"Data integrity check passed for non-archive");
}
- (void)testCheckDataIntegrityForFile_ModifiedCRC {
NSURL *testArchiveURL = self.testFileURLs[@"Modified CRC Archive.rar"];
URKArchive *archive = [[URKArchive alloc] initWithURL:testArchiveURL error:nil];
BOOL success = [archive checkDataIntegrityOfFile:@"README.md"];
XCTAssertFalse(success, @"Data integrity check passed for archive with modified CRC");
}
- (void)testCheckDataIntegrityForFile_ModifiedCRC_IgnoringMismatches {
NSURL *testArchiveURL = self.testFileURLs[@"Modified CRC Archive.rar"];
URKArchive *archive = [[URKArchive alloc] initWithURL:testArchiveURL error:nil];
archive.ignoreCRCMismatches = YES;
// Still expect failure, so a consumer can still tell that data is mismatched
BOOL success = [archive checkDataIntegrityOfFile:@"README.md"];
XCTAssertFalse(success, @"Data integrity check passed for archive with modified CRC");
}
- (void)testCheckDataIntegrityIgnoringCRCMismatches {
NSArray *testArchives = @[@"Test Archive.rar",
@"Test Archive (Password).rar",
@"Test Archive (Header Password).rar"];
for (NSString *testArchiveName in testArchives) {
NSLog(@"Testing data integrity of file in archive %@", testArchiveName);
NSURL *testArchiveURL = self.testFileURLs[testArchiveName];
NSString *password = ([testArchiveName rangeOfString:@"Password"].location != NSNotFound
? @"password"
: nil);
URKArchive *archive = [[URKArchive alloc] initWithURL:testArchiveURL password:password error:nil];
__block BOOL blockInvoked = NO;
BOOL success = [archive checkDataIntegrityIgnoringCRCMismatches:^BOOL{
blockInvoked = YES;
return NO;
}];
XCTAssertTrue(success, @"Data integrity check failed for %@", testArchiveName);
XCTAssertFalse(blockInvoked, @"Block prompting whether to ignore CRC mismatches should not have been called");
XCTAssertFalse(archive.ignoreCRCMismatches);
}
}
- (void)testCheckDataIntegrityIgnoringCRCMismatches_InBackground {
NSURL *testArchiveURL = self.testFileURLs[@"Modified CRC Archive.rar"];
URKArchive *archive = [[URKArchive alloc] initWithURL:testArchiveURL error:nil];
__block BOOL blockInvoked = NO;
__block NSThread *blockThread = nil;
XCTestExpectation *exp = [self expectationWithDescription:@"data check block invoked"];
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
BOOL success = [archive checkDataIntegrityIgnoringCRCMismatches:^BOOL{
blockInvoked = YES;
blockThread = [NSThread currentThread];
return YES;
}];
XCTAssertTrue(success, @"Data integrity check failed for archive with modified CRC, when instructed to ignore");
[exp fulfill];
});
[self waitForExpectationsWithTimeout:10 handler:nil];
XCTAssertTrue(blockInvoked, @"Block prompting whether to ignore CRC mismatches should have been called");
XCTAssertEqualObjects(blockThread, [NSThread mainThread]);
XCTAssertTrue(archive.ignoreCRCMismatches);
}
- (void)testCheckDataIntegrityIgnoringCRCMismatches_NotAnArchive {
NSURL *testArchiveURL = self.testFileURLs[@"Test File B.jpg"];
URKArchive *archive = [[URKArchive alloc] initWithURL:testArchiveURL error:nil];
__block BOOL blockInvoked = NO;
BOOL success = [archive checkDataIntegrityIgnoringCRCMismatches:^BOOL{
blockInvoked = YES;
return NO;
}];
XCTAssertFalse(success, @"Data integrity check passed for non-archive");
XCTAssertFalse(blockInvoked, @"Block prompting whether to ignore CRC mismatches should not have been called");
XCTAssertFalse(archive.ignoreCRCMismatches);
}
- (void)testCheckDataIntegrityIgnoringCRCMismatches_ModifiedCRC_Ignore {
NSURL *testArchiveURL = self.testFileURLs[@"Modified CRC Archive.rar"];
URKArchive *archive = [[URKArchive alloc] initWithURL:testArchiveURL error:nil];
__block BOOL blockInvoked = NO;
BOOL success = [archive checkDataIntegrityIgnoringCRCMismatches:^BOOL{
blockInvoked = YES;
return YES;
}];
XCTAssertTrue(success, @"Data integrity check failed for archive with modified CRC, when instructed to ignore");
XCTAssertTrue(blockInvoked, @"Block prompting whether to ignore CRC mismatches should have been called");
XCTAssertTrue(archive.ignoreCRCMismatches);
}
- (void)testCheckDataIntegrityIgnoringCRCMismatches_ModifiedCRC_DontIgnore {
NSURL *testArchiveURL = self.testFileURLs[@"Modified CRC Archive.rar"];
URKArchive *archive = [[URKArchive alloc] initWithURL:testArchiveURL error:nil];
__block BOOL blockInvoked = NO;
BOOL success = [archive checkDataIntegrityIgnoringCRCMismatches:^BOOL{
blockInvoked = YES;
return NO;
}];
XCTAssertFalse(success, @"Data integrity check passed for archive with modified CRC");
XCTAssertTrue(blockInvoked, @"Block prompting whether to ignore CRC mismatches should have been called");
XCTAssertFalse(archive.ignoreCRCMismatches);
}
@end