|
7 | 7 | //
|
8 | 8 |
|
9 | 9 | #import <XCTest/XCTest.h>
|
| 10 | +#import <MacTypes.h> |
| 11 | +#import "CCPackageUnzipper.h" |
| 12 | +#import "CCPackage.h" |
| 13 | +#import "CCPackageInstallData.h" |
| 14 | +#import "CCPackage+InstallData.h" |
| 15 | +#import "CCPackageConstants.h" |
| 16 | +#import "CCPackageUnzipperDelegate.h" |
| 17 | +#import "CCUnitTestAssertions.h" |
10 | 18 |
|
11 |
| -@interface CCPackageUnzipperTests : XCTestCase |
| 19 | +@interface CCPackageUnzipperTests : XCTestCase <CCPackageUnzipperDelegate> |
| 20 | + |
| 21 | +@property (nonatomic, strong) CCPackage *package; |
| 22 | +@property (nonatomic, strong) CCPackageInstallData *installData; |
| 23 | +@property (nonatomic, copy) NSString *unzipFolderPath; |
| 24 | +@property (nonatomic, strong) NSCondition *condition; |
| 25 | +@property (nonatomic) BOOL unzipperReturned; |
| 26 | +@property (nonatomic) BOOL unzippingSuccessful; |
| 27 | +@property (nonatomic, strong) NSError *unzippingError; |
12 | 28 |
|
13 | 29 | @end
|
14 | 30 |
|
| 31 | + |
15 | 32 | @implementation CCPackageUnzipperTests
|
16 | 33 |
|
17 | 34 | - (void)setUp
|
18 | 35 | {
|
19 | 36 | [super setUp];
|
20 |
| - // Put setup code here. This method is called before the invocation of each test method in the class. |
| 37 | + |
| 38 | + [self deleteGeneratedFiles]; |
| 39 | + self.unzipperReturned = NO; |
| 40 | + self.unzippingError = nil; |
| 41 | + self.unzippingSuccessful = NO; |
| 42 | + |
| 43 | + self.unzipFolderPath = [NSTemporaryDirectory() stringByAppendingPathComponent:PACKAGE_REL_UNZIP_FOLDER]; |
| 44 | + |
| 45 | + [self createUnzipFolder]; |
| 46 | + |
| 47 | + self.package = [[CCPackage alloc] initWithName:@"Foo" |
| 48 | + resolution:@"phonehd" |
| 49 | + os:@"iOS" |
| 50 | + remoteURL:[NSURL URLWithString:@"http://foo.fake/Foo-iOS-phonehd.zip"]]; |
| 51 | + |
| 52 | + self.installData = [[CCPackageInstallData alloc] initWithPackage:_package]; |
| 53 | + [_package setInstallData:_installData]; |
| 54 | + |
| 55 | + NSString *pathToZip = [[NSBundle mainBundle] pathForResource:@"Resources-shared/Packages/testpackage-iOS-phonehd" ofType:@"zip"]; |
| 56 | + _installData.localDownloadURL = [NSURL fileURLWithPath:pathToZip]; |
| 57 | + _installData.unzipURL = [NSURL fileURLWithPath:[_unzipFolderPath stringByAppendingPathComponent:[_package standardIdentifier]]]; |
| 58 | + |
| 59 | + NSLog(@"%@", _installData.unzipURL.path); |
| 60 | +} |
| 61 | + |
| 62 | +- (void)createUnzipFolder |
| 63 | +{ |
| 64 | + NSError *error; |
| 65 | + NSFileManager *fileManager = [NSFileManager defaultManager]; |
| 66 | + if (![fileManager createDirectoryAtURL:[NSURL fileURLWithPath:_unzipFolderPath] |
| 67 | + withIntermediateDirectories:YES |
| 68 | + attributes:nil |
| 69 | + error:&error]) |
| 70 | + { |
| 71 | + NSLog(@"%@", error); |
| 72 | + } |
21 | 73 | }
|
22 | 74 |
|
23 | 75 | - (void)tearDown
|
24 | 76 | {
|
25 |
| - // Put teardown code here. This method is called after the invocation of each test method in the class. |
| 77 | + [self deleteGeneratedFiles]; |
| 78 | + |
26 | 79 | [super tearDown];
|
27 | 80 | }
|
28 | 81 |
|
29 |
| -- (void)testExample |
| 82 | +- (void)deleteGeneratedFiles |
| 83 | +{ |
| 84 | + NSFileManager *fileManager = [NSFileManager defaultManager]; |
| 85 | + [fileManager removeItemAtPath:_unzipFolderPath error:nil]; |
| 86 | +} |
| 87 | + |
| 88 | +- (void)testUnzipping |
30 | 89 | {
|
31 |
| - XCTFail(@"No implementation for \"%s\"", __PRETTY_FUNCTION__); |
| 90 | + [self unzipUntilDelegateMethodsReturn:nil]; |
| 91 | + |
| 92 | + NSFileManager *fileManager = [NSFileManager defaultManager]; |
| 93 | + NSArray *contents = [fileManager contentsOfDirectoryAtURL:[_installData.unzipURL URLByAppendingPathComponent:@"testpackage-iOS-phonehd"] |
| 94 | + includingPropertiesForKeys:@[NSURLNameKey, NSURLIsDirectoryKey] |
| 95 | + options:NSDirectoryEnumerationSkipsSubdirectoryDescendants |
| 96 | + error:nil]; |
| 97 | + |
| 98 | + XCTAssertEqual(contents.count, 4); |
| 99 | + XCTAssertTrue(_unzippingSuccessful); |
| 100 | +} |
| 101 | + |
| 102 | +- (void)testUnzippingOfNonExistingArchive |
| 103 | +{ |
| 104 | + _installData.localDownloadURL = [NSURL fileURLWithPath:@"/foo.zip"]; |
| 105 | + |
| 106 | + [self unzipUntilDelegateMethodsReturn:nil]; |
| 107 | + |
| 108 | + XCTAssertFalse(_unzippingSuccessful); |
| 109 | +} |
| 110 | + |
| 111 | +// This test is not working as SSZipArchive will return succes although file operations fail |
| 112 | +// This requires some modifications of SSZipArchive |
| 113 | +- (void)testUnzippingOfInaccessibleUnzipFolder |
| 114 | +{ |
| 115 | + _installData.unzipURL = [NSURL fileURLWithPath:@"/temp/surelynotexistingfolder"]; |
| 116 | + |
| 117 | + // [self unzipUntilDelegateMethodsReturn:]; |
| 118 | + |
| 119 | + // XCTAssertFalse(_unzippingSuccessful); |
| 120 | +} |
| 121 | + |
| 122 | +- (void)testUnzipOfPasswordProtectedPackage |
| 123 | +{ |
| 124 | + NSString *pathToZip = [[NSBundle mainBundle] pathForResource:@"Resources-shared/Packages/password-iOS-phone" ofType:@"zip"]; |
| 125 | + _installData.localDownloadURL = [NSURL fileURLWithPath:pathToZip]; |
| 126 | + |
| 127 | + [self unzipUntilDelegateMethodsReturn:@"foobar"]; |
| 128 | + |
| 129 | + NSString *secretFilePath = [_installData.unzipURL.path stringByAppendingPathComponent:@"password-iOS-phone/secret.txt"]; |
| 130 | + NSError *error; |
| 131 | + NSString *contentsOfSecretFile = [NSString stringWithContentsOfFile:secretFilePath encoding:NSUTF8StringEncoding error:&error]; |
| 132 | + XCTAssertNil(error); |
| 133 | + CCAssertEqualStrings(contentsOfSecretFile, @"unzipping successful"); |
| 134 | +} |
| 135 | + |
| 136 | +- (void)unzipUntilDelegateMethodsReturn:(NSString *)password |
| 137 | +{ |
| 138 | + self.condition = [[NSCondition alloc] init]; |
| 139 | + [_condition lock]; |
| 140 | + |
| 141 | + CCPackageUnzipper *unzipper = [[CCPackageUnzipper alloc] initWithPackage:_package]; |
| 142 | + unzipper.password = password; |
| 143 | + unzipper.delegate = self; |
| 144 | + [unzipper unpackPackage]; |
| 145 | + |
| 146 | + while(!_unzipperReturned) |
| 147 | + { |
| 148 | + [_condition wait]; |
| 149 | + } |
| 150 | + [_condition unlock]; |
| 151 | +} |
| 152 | + |
| 153 | +- (void)unzipFinished:(CCPackageUnzipper *)packageUnzipper |
| 154 | +{ |
| 155 | + self.unzippingSuccessful = YES; |
| 156 | + self.unzipperReturned = YES; |
| 157 | + [_condition signal]; |
| 158 | +} |
| 159 | + |
| 160 | +- (void)unzipFailed:(CCPackageUnzipper *)packageUnzipper error:(NSError *)error |
| 161 | +{ |
| 162 | + self.unzippingSuccessful = NO; |
| 163 | + self.unzippingError = error; |
| 164 | + |
| 165 | + self.unzipperReturned = YES; |
| 166 | + [_condition signal]; |
32 | 167 | }
|
33 | 168 |
|
34 | 169 | @end
|
0 commit comments