Permalink
Browse files

Fix a potential crasher (wrap readStream access in main loop in the c…

…ancelledLock)

Tests now build on the device
Fix file paths in tests so they pass on the device
Removed some old cruft
  • Loading branch information...
1 parent d2d7213 commit c5b610b53d9d9231bdd75d3bd849c828d22d216f @pokeb committed Jul 26, 2009
View
@@ -602,10 +602,14 @@ - (void)loadRequest
}
// Find out how much data we've uploaded so far
+ [[self cancelledLock] lock];
[self setTotalBytesSent:[[(NSNumber *)CFReadStreamCopyProperty(readStream, kCFStreamPropertyHTTPRequestBytesWrittenCount) autorelease] unsignedLongLongValue]];
-
+ [[self cancelledLock] unlock];
+
[self updateProgressIndicators];
+
+
// This thread should wait for 1/4 second for the stream to do something. We'll stop early if it does.
CFRunLoopRunInMode(ASIHTTPRequestRunMode,0.25,YES);
}
@@ -1397,9 +1401,11 @@ - (void)handleStreamComplete
return;
}
[progressLock lock];
+
[self setComplete:YES];
[self updateProgressIndicators];
+ [[self cancelledLock] lock];
if (readStream) {
CFReadStreamClose(readStream);
CFReadStreamSetClient(readStream, kCFStreamEventNone, NULL, NULL);
@@ -1449,7 +1455,9 @@ - (void)handleStreamComplete
}
}
}
+ [[self cancelledLock] unlock];
[progressLock unlock];
+
if (fileError) {
[self failWithError:fileError];
} else {
@@ -6,13 +6,9 @@
// Copyright 2008 All-Seeing Interactive. All rights reserved.
//
-#if TARGET_OS_IPHONE
- #import "GHUnit.h"
-#else
- #import <GHUnit/GHUnit.h>
-#endif
+#import "ASITestCase.h"
-@interface ASIFormDataRequestTests : GHTestCase {
+@interface ASIFormDataRequestTests : ASITestCase {
float progress;
}
@@ -24,7 +24,7 @@ - (void)testPostWithFileUpload
//Create a 32kb file
unsigned int size = 1024*32;
NSMutableData *data = [NSMutableData dataWithLength:size];
- NSString *path = [[[[NSBundle mainBundle] bundlePath] stringByDeletingLastPathComponent] stringByAppendingPathComponent:@"bigfile"];
+ NSString *path = [[self filePathForTemporaryTestFiles] stringByAppendingPathComponent:@"bigfile"];
[data writeToFile:path atomically:NO];
ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url];
@@ -39,7 +39,6 @@ - (void)testPostWithFileUpload
[request setPostValue:d forKey:@"post_var2"];
[request setPostValue:v forKey:@"post_var3"];
[request setFile:path forKey:@"file"];
- [request setPostBodyFilePath:@"/Users/ben/Desktop/111.txt"];
[request start];
BOOL success = ([[request responseString] isEqualToString:[NSString stringWithFormat:@"post_var: %@\r\npost_var2: %@\r\npost_var3: %@\r\nfile_name: %@\r\nfile_size: %hu",@"foo",d,v,@"bigfile",size]]);
@@ -6,13 +6,9 @@
// Copyright 2008 All-Seeing Interactive. All rights reserved.
//
-#if TARGET_OS_IPHONE
- #import "GHUnit.h"
-#else
- #import <GHUnit/GHUnit.h>
-#endif
+#import "ASITestCase.h"
-@interface ASIHTTPRequestTests : GHTestCase {
+@interface ASIHTTPRequestTests : ASITestCase {
float progress;
}
@@ -20,7 +20,6 @@ @implementation ASIHTTPRequestSubclass;
@implementation ASIHTTPRequestTests
-
- (void)testBasicDownload
{
NSURL *url = [NSURL URLWithString:@"http://allseeing-i.com"];
@@ -204,7 +203,7 @@ - (void)testDownloadContentLength
- (void)testFileDownload
{
- NSString *path = [[[[NSBundle mainBundle] bundlePath] stringByDeletingLastPathComponent] stringByAppendingPathComponent:@"testimage.png"];
+ NSString *path = [[self filePathForTemporaryTestFiles] stringByAppendingPathComponent:@"testimage.png"];
NSURL *url = [[[NSURL alloc] initWithString:@"http://allseeing-i.com/i/logo.png"] autorelease];
ASIHTTPRequest *request = [[[ASIHTTPRequest alloc] initWithURL:url] autorelease];
@@ -228,7 +227,7 @@ - (void)testFileDownload
- (void)testCompressedResponseDownloadToFile
{
- NSString *path = [[[[NSBundle mainBundle] bundlePath] stringByDeletingLastPathComponent] stringByAppendingPathComponent:@"testfile"];
+ NSString *path = [[self filePathForTemporaryTestFiles] stringByAppendingPathComponent:@"testfile"];
NSURL *url = [[[NSURL alloc] initWithString:@"http://allseeing-i.com/ASIHTTPRequest/tests/first"] autorelease];
ASIHTTPRequest *request = [[[ASIHTTPRequest alloc] initWithURL:url] autorelease];
@@ -290,7 +289,7 @@ - (void)testPostBodyStreamedFromDisk
{
NSURL *url = [NSURL URLWithString:@"http://allseeing-i.com/ASIHTTPRequest/tests/print_request_body"];
NSString *requestBody = @"This is the request body";
- NSString *requestContentPath = [[[[NSBundle mainBundle] bundlePath] stringByDeletingLastPathComponent] stringByAppendingPathComponent:@"testfile.txt"];
+ NSString *requestContentPath = [[self filePathForTemporaryTestFiles] stringByAppendingPathComponent:@"testfile.txt"];
[[requestBody dataUsingEncoding:NSUTF8StringEncoding] writeToFile:requestContentPath atomically:NO];
@@ -630,8 +629,8 @@ - (void)testCompressedResponse
- (void)testPartialFetch
{
- NSString *downloadPath = [[[[NSBundle mainBundle] bundlePath] stringByDeletingLastPathComponent] stringByAppendingPathComponent:@"testfile.txt"];
- NSString *tempPath = [[[[NSBundle mainBundle] bundlePath] stringByDeletingLastPathComponent] stringByAppendingPathComponent:@"tempfile.txt"];
+ NSString *downloadPath = [[self filePathForTemporaryTestFiles] stringByAppendingPathComponent:@"testfile.txt"];
+ NSString *tempPath = [[self filePathForTemporaryTestFiles] stringByAppendingPathComponent:@"tempfile.txt"];
NSString *partialContent = @"This file should be exactly 163 bytes long when encoded as UTF8, Unix line breaks with no BOM.\n";
[partialContent writeToFile:tempPath atomically:NO encoding:NSASCIIStringEncoding error:nil];
@@ -737,7 +736,8 @@ - (void)testCompression
GHAssertTrue(success,@"Failed compress or decompress the correct data");
// Test file to file compression / decompression
- NSString *basePath = [[[NSBundle mainBundle] bundlePath] stringByDeletingLastPathComponent];
+
+ NSString *basePath = [self filePathForTemporaryTestFiles];
NSString *sourcePath = [basePath stringByAppendingPathComponent:@"text.txt"];
NSString *destPath = [basePath stringByAppendingPathComponent:@"text.txt.compressed"];
NSString *newPath = [basePath stringByAppendingPathComponent:@"text2.txt"];
@@ -6,11 +6,7 @@
// Copyright 2008 All-Seeing Interactive. All rights reserved.
//
-#if TARGET_OS_IPHONE
- #import "GHUnit.h"
-#else
- #import <GHUnit/GHUnit.h>
-#endif
+#import "ASITestCase.h"
/*
IMPORTANT
@@ -23,7 +19,7 @@ IMPORTANT
@class ASIHTTPRequest;
@class ASINetworkQueue;
-@interface ASINetworkQueueTests : GHTestCase {
+@interface ASINetworkQueueTests : ASITestCase {
ASIHTTPRequest *requestThatShouldFail;
BOOL complete;
BOOL request_didfail;
@@ -22,12 +22,11 @@
// Used for subclass test
@interface ASINetworkQueueSubclass : ASINetworkQueue {}
@end
-@implementation ASINetworkQueueSubclass;
+@implementation ASINetworkQueueSubclass
@end
@implementation ASINetworkQueueTests
-
- (void)testProgress
{
complete = NO;
@@ -106,7 +105,7 @@ - (void)testUploadProgress
int i;
for (i=0; i<3; i++) {
NSData *data = [[[NSMutableData alloc] initWithLength:fileSizes[i]*1024] autorelease];
- NSString *path = [[[[NSBundle mainBundle] bundlePath] stringByDeletingLastPathComponent] stringByAppendingPathComponent:[NSString stringWithFormat:@"file%hi",i]];
+ NSString *path = [[self filePathForTemporaryTestFiles] stringByAppendingPathComponent:[NSString stringWithFormat:@"file%hi",i]];
[data writeToFile:path atomically:NO];
ASIFormDataRequest *request = [[[ASIFormDataRequest alloc] initWithURL:url] autorelease];
[request setFile:path forKey:@"file"];
@@ -130,7 +129,7 @@ - (void)testUploadProgress
for (i=0; i<3; i++) {
NSData *data = [[[NSMutableData alloc] initWithLength:fileSizes[i]*1024] autorelease];
- NSString *path = [[[[NSBundle mainBundle] bundlePath] stringByDeletingLastPathComponent] stringByAppendingPathComponent:[NSString stringWithFormat:@"file%hi",i]];
+ NSString *path = [[self filePathForTemporaryTestFiles] stringByAppendingPathComponent:[NSString stringWithFormat:@"file%hi",i]];
[data writeToFile:path atomically:NO];
ASIFormDataRequest *request = [[[ASIFormDataRequest alloc] initWithURL:url] autorelease];
[request setFile:path forKey:@"file"];
@@ -381,12 +380,12 @@ - (void)testPartialResume
complete = NO;
progress = 0;
- NSString *temporaryPath = [[[[NSBundle mainBundle] bundlePath] stringByDeletingLastPathComponent] stringByAppendingPathComponent:@"MemexTrails_1.0b1.zip.download"];
+ NSString *temporaryPath = [[self filePathForTemporaryTestFiles] stringByAppendingPathComponent:@"MemexTrails_1.0b1.zip.download"];
if ([[NSFileManager defaultManager] fileExistsAtPath:temporaryPath]) {
[[NSFileManager defaultManager] removeItemAtPath:temporaryPath error:nil];
}
- NSString *downloadPath = [[[[NSBundle mainBundle] bundlePath] stringByDeletingLastPathComponent] stringByAppendingPathComponent:@"MemexTrails_1.0b1.zip"];
+ NSString *downloadPath = [[self filePathForTemporaryTestFiles] stringByAppendingPathComponent:@"MemexTrails_1.0b1.zip"];
if ([[NSFileManager defaultManager] fileExistsAtPath:downloadPath]) {
[[NSFileManager defaultManager] removeItemAtPath:downloadPath error:nil];
}
@@ -6,13 +6,9 @@
// Copyright 2009 All-Seeing Interactive. All rights reserved.
//
-#if TARGET_OS_IPHONE
-#import "GHUnit.h"
-#else
-#import <GHUnit/GHUnit.h>
-#endif
+#import "ASITestCase.h"
-@interface ASIS3RequestTests : GHTestCase {
+@interface ASIS3RequestTests : ASITestCase {
}
- (void)testAuthenticationHeaderGeneration;
@@ -35,7 +35,6 @@ @implementation ASIS3BucketObjectSubclass;
@implementation ASIS3RequestTests
-
// All these tests are based on Amazon's examples at: http://docs.amazonwebservices.com/AmazonS3/2006-03-01/
- (void)testAuthenticationHeaderGeneration
{
@@ -141,7 +140,7 @@ - (void)testREST
// Create the file
NSString *text = @"This is my content";
- NSString *filePath = [[[[NSBundle mainBundle] bundlePath] stringByDeletingLastPathComponent] stringByAppendingPathComponent:@"testfile.txt"];
+ NSString *filePath = [[self filePathForTemporaryTestFiles] stringByAppendingPathComponent:@"testfile.txt"];
[[text dataUsingEncoding:NSUTF8StringEncoding] writeToFile:filePath atomically:NO];
// PUT the file
@@ -231,7 +230,7 @@ - (void)testGZippedContent
{
// Create the file
NSString *text = @"This is my content This is my content This is my content This is my content This is my content This is my content";
- NSString *filePath = [[[[NSBundle mainBundle] bundlePath] stringByDeletingLastPathComponent] stringByAppendingPathComponent:@"testfile.txt"];
+ NSString *filePath = [[self filePathForTemporaryTestFiles] stringByAppendingPathComponent:@"testfile.txt"];
[[text dataUsingEncoding:NSUTF8StringEncoding] writeToFile:filePath atomically:NO];
NSString *path = @"/gzipped-data";
@@ -274,7 +273,7 @@ - (void)testListRequest
int i;
for (i=0; i<5; i++) {
NSString *text = [NSString stringWithFormat:@"This is the content of file #%hi",i];
- NSString *filePath = [[[[NSBundle mainBundle] bundlePath] stringByDeletingLastPathComponent] stringByAppendingPathComponent:[NSString stringWithFormat:@"%hi.txt",i]];
+ NSString *filePath = [[self filePathForTemporaryTestFiles] stringByAppendingPathComponent:[NSString stringWithFormat:@"%hi.txt",i]];
[[text dataUsingEncoding:NSUTF8StringEncoding] writeToFile:filePath atomically:NO];
NSString *path = [NSString stringWithFormat:@"/test-file/%hi",i];
ASIS3Request *request = [ASIS3Request PUTRequestForFile:filePath withBucket:bucket path:path];
@@ -319,7 +318,7 @@ - (void)testListRequest
i=0;
// For each one, we'll just upload the same content again
for (ASIS3BucketObject *object in [listRequest bucketObjects]) {
- NSString *oldFilePath = [[[[NSBundle mainBundle] bundlePath] stringByDeletingLastPathComponent] stringByAppendingPathComponent:[NSString stringWithFormat:@"%hi.txt",i]];;
+ NSString *oldFilePath = [[self filePathForTemporaryTestFiles] stringByAppendingPathComponent:[NSString stringWithFormat:@"%hi.txt",i]];;
ASIS3Request *request = [object PUTRequestWithFile:oldFilePath];
[request setAccessKey:accessKey];
[request setSecretAccessKey:secretAccessKey];
@@ -0,0 +1,20 @@
+//
+// ASITestCase.h
+// iPhone
+//
+// Created by Ben Copsey on 26/07/2009.
+// Copyright 2009 All-Seeing Interactive. All rights reserved.
+//
+
+#import <Foundation/Foundation.h>
+
+#if TARGET_OS_IPHONE
+#import "GHUnit.h"
+#else
+#import <GHUnit/GHUnit.h>
+#endif
+
+@interface ASITestCase : GHTestCase {
+}
+- (NSString *)filePathForTemporaryTestFiles;
+@end
@@ -0,0 +1,23 @@
+//
+// ASITestCase.m
+// iPhone
+//
+// Created by Ben Copsey on 26/07/2009.
+// Copyright 2009 All-Seeing Interactive. All rights reserved.
+//
+
+#import "ASITestCase.h"
+
+
+@implementation ASITestCase
+
+- (NSString *)filePathForTemporaryTestFiles
+{
+ NSString *path = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0] stringByAppendingPathComponent:@"ASIHTTPRequest Test Files"];
+ if (![[NSFileManager defaultManager] fileExistsAtPath:path isDirectory:NULL]) {
+ [[NSFileManager defaultManager] createDirectoryAtPath:path attributes:nil];
+ }
+ return path;
+}
+
+@end
@@ -30,6 +30,7 @@
B55B60140F7659A30064029C /* libz.1.2.3.dylib in Frameworks */ = {isa = PBXBuildFile; fileRef = B55B5EB70F7658200064029C /* libz.1.2.3.dylib */; };
B564D0520FEE677F00A2098A /* ASINSStringAdditions.m in Sources */ = {isa = PBXBuildFile; fileRef = B564D0500FEE677F00A2098A /* ASINSStringAdditions.m */; };
B564D0530FEE678C00A2098A /* ASINSStringAdditions.m in Sources */ = {isa = PBXBuildFile; fileRef = B564D0500FEE677F00A2098A /* ASINSStringAdditions.m */; };
+ B565297E101C8D01000499CF /* ASITestCase.m in Sources */ = {isa = PBXBuildFile; fileRef = B565297C101C8D01000499CF /* ASITestCase.m */; };
B5B513680FBEE435002C74D0 /* GHUnit.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = B5B513670FBEE435002C74D0 /* GHUnit.framework */; };
B5B513830FBEE490002C74D0 /* GHUnit.framework in CopyFiles */ = {isa = PBXBuildFile; fileRef = B5B513670FBEE435002C74D0 /* GHUnit.framework */; };
B5B513860FBEE515002C74D0 /* GHUnitTestMain.m in Sources */ = {isa = PBXBuildFile; fileRef = B5B513850FBEE515002C74D0 /* GHUnitTestMain.m */; };
@@ -85,6 +86,8 @@
B55B5EDF0F7658C70064029C /* Unit Tests (GHUnit).app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = "Unit Tests (GHUnit).app"; sourceTree = BUILT_PRODUCTS_DIR; };
B564D0500FEE677F00A2098A /* ASINSStringAdditions.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = ASINSStringAdditions.m; sourceTree = "<group>"; };
B564D0510FEE677F00A2098A /* ASINSStringAdditions.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = ASINSStringAdditions.h; sourceTree = "<group>"; };
+ B565297C101C8D01000499CF /* ASITestCase.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = ASITestCase.m; sourceTree = "<group>"; };
+ B565297D101C8D01000499CF /* ASITestCase.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = ASITestCase.h; sourceTree = "<group>"; };
B5B513670FBEE435002C74D0 /* GHUnit.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = GHUnit.framework; path = Frameworks/GHUnit.framework; sourceTree = "<group>"; };
B5B513850FBEE515002C74D0 /* GHUnitTestMain.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = GHUnitTestMain.m; sourceTree = "<group>"; };
B5C66394100A4C24004F3C96 /* ASIS3Request.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = ASIS3Request.m; sourceTree = "<group>"; };
@@ -225,6 +228,8 @@
isa = PBXGroup;
children = (
B5B513850FBEE515002C74D0 /* GHUnitTestMain.m */,
+ B565297D101C8D01000499CF /* ASITestCase.h */,
+ B565297C101C8D01000499CF /* ASITestCase.m */,
B55B5D1A0F76568E0064029C /* ASIFormDataRequestTests.h */,
B55B5D1B0F76568E0064029C /* ASIFormDataRequestTests.m */,
B55B5D1C0F76568E0064029C /* ASIHTTPRequestTests.h */,
@@ -345,6 +350,7 @@
B5C664BC100A6220004F3C96 /* ASIS3RequestTests.m in Sources */,
B51E5F3B100B829D004A300D /* ASIS3BucketObject.m in Sources */,
B51E5F56100B83F7004A300D /* ASIS3ListRequest.m in Sources */,
+ B565297E101C8D01000499CF /* ASITestCase.m in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
View
@@ -9,7 +9,7 @@
<key>CFBundleIconFile</key>
<string>iphone-icon.png</string>
<key>CFBundleIdentifier</key>
- <string>com.allseeinginteractive.trailsnetwork.asidemo</string>
+ <string>com.yaddy.yadda.blah</string>
<key>CFBundleInfoDictionaryVersion</key>
<string>6.0</string>
<key>CFBundlePackageType</key>
@@ -4,10 +4,14 @@
<dict>
<key>CFBundleDevelopmentRegion</key>
<string>English</string>
+ <key>CFBundleDisplayName</key>
+ <string>ASIHTTPRequest Unit Tests</string>
<key>CFBundleExecutable</key>
<string>${EXECUTABLE_NAME}</string>
+ <key>CFBundleIconFile</key>
+ <string>iphone-tests-icon.png</string>
<key>CFBundleIdentifier</key>
- <string>com.allseeinginteractive.trailsnetwork.tests</string>
+ <string>com.your.company.blah</string>
<key>CFBundleInfoDictionaryVersion</key>
<string>6.0</string>
<key>CFBundlePackageType</key>
@@ -18,9 +22,5 @@
<string>1.0</string>
<key>NSMainNibFile</key>
<string>GHUnitIPhone</string>
- <key>CFBundleDisplayName</key>
- <string>ASIHTTPRequest Unit Tests</string>
- <key>CFBundleIconFile</key>
- <string>iphone-icon.png</string>
</dict>
</plist>
@@ -1,10 +0,0 @@
-//
-// Prefix header for all source files of the 'asi-http-request' target in the 'asi-http-request' project
-//
-
-#ifdef __OBJC__
-#import <CoreFoundation/CoreFoundation.h>
-#import <Foundation/Foundation.h>
-#import <CFNetwork/CFNetwork.h>
-#import <UIKit/UIKit.h>
-#endif
Oops, something went wrong.

0 comments on commit c5b610b

Please sign in to comment.