Skip to content

Commit

Permalink
Merge pull request #96 from lintianzhi/fix/uint32_crcValue
Browse files Browse the repository at this point in the history
crc32
  • Loading branch information
longbai committed Sep 29, 2014
2 parents 91fae46 + 549fba6 commit e6098a7
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 4 deletions.
2 changes: 1 addition & 1 deletion QiniuSDK/QiniuBlkputRet.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
@property (copy, nonatomic) NSString* host;
@property (copy, nonatomic) NSString* ctx;
@property (copy, nonatomic) NSString* checksum;
@property int crc32;
@property UInt32 crc32;
@property int offset;

@end
2 changes: 1 addition & 1 deletion QiniuSDK/QiniuBlockUploader.m
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ - (void) postChunk:(NSData *)chunk
progress.ctx = [putRet objectForKey:@"ctx"];
progress.host = [putRet objectForKey:@"host"];
progress.checksum = [putRet objectForKey:@"checksum"];
progress.crc32 = [[putRet objectForKey:@"crc32"] intValue];
progress.crc32 = [[putRet objectForKey:@"crc32"] unsignedIntValue];
progress.offset = [[putRet objectForKey:@"offset"] intValue];
*error = nil;
} else {
Expand Down
2 changes: 1 addition & 1 deletion QiniuSDK/QiniuSDKTests/QiniuSDKTests.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

@interface QiniuSDKTests : SenTestCase<QiniuUploadDelegate>
{
bool _done;
bool _done, _succeed;
bool _progressReceived;
NSString *_filePath;
NSString *_token;
Expand Down
50 changes: 50 additions & 0 deletions QiniuSDK/QiniuSDKTests/QiniuSDKTests.m
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,14 @@ - (void)setUp
_token = [policy makeToken:QiniuAccessKey secretKey:QiniuSecretKey];

_done = false;
_succeed = false;
_progressReceived = false;
}

- (void)reset
{
_done = false;
_succeed = false;
_progressReceived = false;
}

Expand Down Expand Up @@ -87,6 +95,7 @@ - (void)uploadProgressUpdated:(NSString *)theFilePath percent:(float)percent
- (void)uploadSucceeded:(NSString *)theFilePath ret:(NSDictionary *)ret
{
_done = true;
_succeed = true;
NSLog(@"Upload Succeeded: %@ - Ret: %@", theFilePath, ret);
}

Expand Down Expand Up @@ -118,23 +127,29 @@ - (void) waitFinish {

- (void) testSimpleUpload
{
[self reset];
QiniuSimpleUploader *uploader = [QiniuSimpleUploader uploaderWithToken:_token];
uploader.delegate = self;
[uploader uploadFile:_filePath key:[NSString stringWithFormat:@"test-%@.png", [self timeString]] extra:nil];
[self waitFinish];
STAssertTrue(_succeed, @"upload should succeed");

}

- (void) testSimpleUploadWithUndefinedKey
{
[self reset];
QiniuSimpleUploader *uploader = [QiniuSimpleUploader uploaderWithToken:_token];
uploader.delegate = self;
[uploader uploadFile:_filePath key:kQiniuUndefinedKey extra:nil];
[self waitFinish];
STAssertTrue(_succeed, @"upload should succeed");
}


- (void) testSimpleUploadWithReturnBodyAndUserParams
{
[self reset];
QiniuPutPolicy *policy = [[QiniuPutPolicy new] autorelease];
policy.expires = 3600;
policy.scope = QiniuBucketName;
Expand All @@ -152,10 +167,12 @@ - (void) testSimpleUploadWithReturnBodyAndUserParams
// upload
[uploader uploadFile:_filePath key:[NSString stringWithFormat:@"test-%@.png", [self timeString]] extra:extra];
[self waitFinish];
STAssertTrue(_succeed, @"upload should succeed");
}

- (void) testSimpleUploadWithWrongCrc32
{
[self reset];
QiniuSimpleUploader *uploader = [QiniuSimpleUploader uploaderWithToken:_token];
uploader.delegate = self;

Expand All @@ -167,10 +184,12 @@ - (void) testSimpleUploadWithWrongCrc32
// upload
[uploader uploadFile:_filePath key:[NSString stringWithFormat:@"test-%@.png", [self timeString]] extra:extra];
[self waitFinish];
STAssertFalse(_succeed, @"upload should fail");
}

- (void) testSimpleUploadWithRightCrc32
{
[self reset];
QiniuSimpleUploader *uploader = [QiniuSimpleUploader uploaderWithToken:_token];
uploader.delegate = self;

Expand All @@ -187,10 +206,40 @@ - (void) testSimpleUploadWithRightCrc32
// upload
[uploader uploadFile:_filePath key:[NSString stringWithFormat:@"test-%@.png", [self timeString]] extra:extra];
[self waitFinish];
STAssertTrue(_succeed, @"upload should succeed");
}

- (void) testSimpleUploadWithNegativeCrc32
{
[self reset];
QiniuSimpleUploader *uploader = [QiniuSimpleUploader uploaderWithToken:_token];
uploader.delegate = self;

// calc right crc32 value
// NSData *buffer = [NSData dataWithContentsOfFile:_filePath];

NSData *buffer = [@"Hello, World!" dataUsingEncoding:NSUTF8StringEncoding];
uLong crc = crc32(0L, Z_NULL, 0);
crc = crc32(crc, [buffer bytes], [buffer length]);

// extra argument with right crc32
QiniuPutExtra *extra = [[[QiniuPutExtra alloc] init] autorelease];
extra.crc32 = crc;
extra.checkCrc = 1;
NSLog(@"crc32: %lu", crc);

NSString *crcpath = [NSTemporaryDirectory() stringByAppendingPathComponent:@"testcrc"];
[buffer writeToFile:_filePath atomically:TRUE];

// upload
[uploader uploadFile:crcpath key:[NSString stringWithFormat:@"test-%@.png", [self timeString]] extra:extra];
[self waitFinish];
STAssertTrue(_succeed, @"upload should succeed");
}

- (void) testResumableUpload
{
[self reset];
QiniuResumableUploader *uploader = [QiniuResumableUploader instanceWithToken: _token];
uploader.delegate = self;
NSString *key = [NSString stringWithFormat:@"test-%@.png", [self timeString]];
Expand All @@ -209,6 +258,7 @@ - (void) testResumableUpload
[uploader uploadFile:_filePath key:key params:params];
NSLog(@"key: %@\n", key);
[self waitFinish];
STAssertTrue(_succeed, @"upload should succeed");
}

@end
2 changes: 1 addition & 1 deletion QiniuSDK/QiniuSimpleUploader.m
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ - (void) uploadFile:(NSString *)filePath
if (extra) {
mimeType = extra.mimeType;
if (extra.checkCrc == 1) {
[_request addPostValue: [NSString stringWithFormat:@"%ld", extra.crc32] forKey:@"crc32"];
[_request addPostValue: [NSString stringWithFormat:@"%lu", extra.crc32] forKey:@"crc32"];
}
for (NSString *key in extra.params) {
[_request addPostValue:[extra.params objectForKey:key] forKey:key];
Expand Down

0 comments on commit e6098a7

Please sign in to comment.