Skip to content

Commit 4515dce

Browse files
committed
CCPackageUnzipper unit tests added.
Password protected archive added for tests. Password is foobar.
1 parent 33c3120 commit 4515dce

File tree

2 files changed

+140
-5
lines changed

2 files changed

+140
-5
lines changed

UnitTests/CCPackageUnzipperTests.m

Lines changed: 140 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,28 +7,163 @@
77
//
88

99
#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"
1018

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;
1228

1329
@end
1430

31+
1532
@implementation CCPackageUnzipperTests
1633

1734
- (void)setUp
1835
{
1936
[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+
}
2173
}
2274

2375
- (void)tearDown
2476
{
25-
// Put teardown code here. This method is called after the invocation of each test method in the class.
77+
[self deleteGeneratedFiles];
78+
2679
[super tearDown];
2780
}
2881

29-
- (void)testExample
82+
- (void)deleteGeneratedFiles
83+
{
84+
NSFileManager *fileManager = [NSFileManager defaultManager];
85+
[fileManager removeItemAtPath:_unzipFolderPath error:nil];
86+
}
87+
88+
- (void)testUnzipping
3089
{
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];
32167
}
33168

34169
@end
Binary file not shown.

0 commit comments

Comments
 (0)