QuietUnrar/Carthage/Checkouts/UnzipKit/Tests/ExtractDataTests.m

128 lines
5.1 KiB
Mathematica
Raw Permalink Normal View History

//
// ExtractDataTests.m
// UnzipKit
//
// Created by Dov Frankel on 7/16/15.
// Copyright (c) 2015 Abbey Code. All rights reserved.
//
#import "UZKArchiveTestCase.h"
#import "UnzipKit.h"
#import "UnzipKitMacros.h"
@interface ExtractDataTests : UZKArchiveTestCase
@end
@implementation ExtractDataTests
- (void)testExtractData
{
NSArray *testArchives = @[@"Test Archive.zip",
@"Test Archive (Password).zip"];
NSSet *expectedFileSet = self.nonZipTestFilePaths;
NSArray *expectedFiles = [expectedFileSet.allObjects sortedArrayUsingSelector:@selector(compare:)];
for (NSString *testArchiveName in testArchives) {
NSURL *testArchiveURL = self.testFileURLs[testArchiveName];
NSString *password = ([testArchiveName rangeOfString:@"Password"].location != NSNotFound
? @"password"
: nil);
UZKArchive *archive = [[UZKArchive alloc] initWithURL:testArchiveURL password:password error:nil];
NSError *error = nil;
NSArray *fileInfos = [archive listFileInfo:&error];
XCTAssertNil(error, @"Error reading file info");
for (NSUInteger i = 0; i < expectedFiles.count; i++) {
NSString *expectedFilename = expectedFiles[i];
NSError *error = nil;
NSData *extractedData = [archive extractDataFromFile:expectedFilename
error:&error];
XCTAssertNil(error, @"Error in extractData:error:");
NSData *expectedFileData = [NSData dataWithContentsOfURL:self.testFileURLs[expectedFilename]];
XCTAssertNotNil(extractedData, @"No data extracted (%@)", testArchiveName);
XCTAssertTrue([expectedFileData isEqualToData:extractedData], @"Extracted data doesn't match original file (%@)", testArchiveName);
error = nil;
NSData *dataFromFileInfo = [archive extractData:fileInfos[i]
error:&error];
XCTAssertNil(error, @"Error extracting data by file info (%@)", testArchiveName);
XCTAssertTrue([expectedFileData isEqualToData:dataFromFileInfo], @"Extracted data from file info doesn't match original file (%@)", testArchiveName);
}
}
}
- (void)testExtractData_Unicode
{
NSSet *expectedFileSet = self.nonZipUnicodeFilePaths;
NSArray *expectedFiles = [expectedFileSet.allObjects sortedArrayUsingSelector:@selector(compare:)];
NSURL *testArchiveURL = self.unicodeFileURLs[@"Ⓣest Ⓐrchive.zip"];
UZKArchive *archive = [[UZKArchive alloc] initWithURL:testArchiveURL error:nil];
NSError *error = nil;
NSArray *fileInfos = [archive listFileInfo:&error];
XCTAssertNil(error, @"Error reading file info");
for (NSUInteger i = 0; i < expectedFiles.count; i++) {
NSString *expectedFilename = expectedFiles[i];
NSError *error = nil;
NSData *extractedData = [archive extractDataFromFile:expectedFilename
error:&error];
XCTAssertNil(error, @"Error in extractData:error:");
NSData *expectedFileData = [NSData dataWithContentsOfURL:self.unicodeFileURLs[expectedFilename]];
XCTAssertNotNil(extractedData, @"No data extracted");
XCTAssertTrue([expectedFileData isEqualToData:extractedData], @"Extracted data doesn't match original file (%@)", expectedFilename);
error = nil;
NSData *dataFromFileInfo = [archive extractData:fileInfos[i]
error:&error];
XCTAssertNil(error, @"Error extracting data by file info");
XCTAssertTrue([expectedFileData isEqualToData:dataFromFileInfo], @"Extracted data from file info doesn't match original file (%@)", expectedFilename);
}
}
- (void)testExtractData_NoPassword
{
NSArray *testArchives = @[@"Test Archive (Password).zip"];
for (NSString *testArchiveName in testArchives) {
UZKArchive *archive = [[UZKArchive alloc] initWithURL:self.testFileURLs[testArchiveName] error:nil];
NSError *error = nil;
NSData *data = [archive extractDataFromFile:@"Test File A.txt"
error:&error];
XCTAssertNotNil(error, @"Extract data without password succeeded");
XCTAssertNil(data, @"Data returned without password");
XCTAssertEqual(error.code, UZKErrorCodeInvalidPassword, @"Unexpected error code returned");
}
}
- (void)testExtractData_InvalidArchive
{
UZKArchive *archive = [[UZKArchive alloc] initWithURL:self.testFileURLs[@"Test File A.txt"] error:nil];
NSError *error = nil;
NSData *data = [archive extractDataFromFile:@"Any file.txt"
error:&error];
XCTAssertNotNil(error, @"Extract data for invalid archive succeeded");
XCTAssertNil(data, @"Data returned for invalid archive");
XCTAssertEqual(error.code, UZKErrorCodeBadZipFile, @"Unexpected error code returned");
}
@end