Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Unit test for base64, which uncovers some bugs

  • Loading branch information...
commit f65969822d940e3a8db1dee6654d26412f008817 1 parent 2f8af57
@adamjernst authored
View
4 AEURLConnection/AEURLRequestFactory.h
@@ -49,6 +49,8 @@ typedef id (^AEURLParameterProcessor)(NSDictionary *parameters, NSMutableURLRequ
// x-www-form-urlencoded format, like a browser's POST form encoding.
+ (AEURLParameterProcessor)formURLEncodedProcessor;
+@end
+
// Utility functions to url-encode and -decode values.
NSString *AEURLEncodedStringFromString(NSString *string);
NSString *AEStringFromURLEncodedString(NSString *formEncodedString);
@@ -62,5 +64,3 @@ NSString *AEBase64EncodedStringFromData(NSData *data);
NSData *AEDataFromBase64EncodedString(NSString *base64);
// See AEJSONProcessor for a parameter processing block that creates JSON.
-
-@end
View
28 AEURLConnection/AEURLRequestFactory.m
@@ -98,6 +98,8 @@ + (AEURLParameterProcessor)formURLEncodedProcessor {
return formURLEncodedProcessor;
}
+@end
+
#pragma mark - URLEncoding
// These functions are based on AFNetworking's equivalents (substituting AE for
@@ -192,8 +194,7 @@ + (AEURLParameterProcessor)formURLEncodedProcessor {
// http://cocoawithlove.com/2009/06/base64-encoding-options-on-mac-and.html
const char *input = [base64 cStringUsingEncoding:NSUTF8StringEncoding];
unsigned long length = strlen(input);
- NSMutableData *mutableData = [NSMutableData dataWithLength:((length + 3) / 4) * 3];
- uint8_t *output = [mutableData mutableBytes];
+ NSMutableData *output = [NSMutableData dataWithCapacity:((length + 3) / 4) * 3];
//
// Definition for "masked-out" areas of the base64DecodeLookup mapping
@@ -244,25 +245,12 @@ + (AEURLParameterProcessor)formURLEncodedProcessor {
}
}
- //
- // Store the 6 bits from each of the 4 characters as 3 bytes
- //
- // (Uses improved bounds checking suggested by Alexandre Colucci)
- //
- if(accumulateIndex >= 2)
- output[j] = (accumulated[0] << 2) | (accumulated[1] >> 4);
- if(accumulateIndex >= 3)
- output[j + 1] = (accumulated[1] << 4) | (accumulated[2] >> 2);
- if(accumulateIndex >= 4)
- output[j + 2] = (accumulated[2] << 6) | accumulated[3];
+ for (int k = 0; k < accumulateIndex - 1; k++) {
+ uint8_t byte = (accumulated[k] << (k + 1) * 2) | (accumulated[k + 1] >> (2 - k) * 2);
+ [output appendBytes:&byte length:1];
+ }
j += accumulateIndex - 1;
}
- if (output)
- {
- *output = j;
- }
- return mutableData;
+ return output;
}
-
-@end
View
5 Example/AEURLExample.xcodeproj/project.pbxproj
@@ -377,7 +377,7 @@
"$(inherited)",
);
GCC_SYMBOLS_PRIVATE_EXTERN = NO;
- GCC_VERSION = com.apple.compilers.llvm.clang.1_0;
+ GCC_VERSION = "";
GCC_WARN_ABOUT_MISSING_PROTOTYPES = YES;
GCC_WARN_ABOUT_RETURN_TYPE = YES;
GCC_WARN_UNUSED_VARIABLE = YES;
@@ -394,7 +394,8 @@
"CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "iPhone Developer";
COPY_PHASE_STRIP = YES;
GCC_C_LANGUAGE_STANDARD = gnu99;
- GCC_VERSION = com.apple.compilers.llvm.clang.1_0;
+ GCC_OPTIMIZATION_LEVEL = 3;
+ GCC_VERSION = "";
GCC_WARN_ABOUT_MISSING_PROTOTYPES = YES;
GCC_WARN_ABOUT_RETURN_TYPE = YES;
GCC_WARN_UNUSED_VARIABLE = YES;
View
14 Example/AEURLExampleTests/AEURLExampleTests.m
@@ -74,6 +74,20 @@ - (void)testQueryStringEncode {
STAssertEqualObjects(d, d2, @"Query string encoding or decoding failed");
}
+- (void)testBase64 {
+ static const int kTestLength = 2043;
+ NSMutableData *testData = [NSMutableData dataWithCapacity:kTestLength];
+ srand(494020102);
+ for (int i = 0; i < kTestLength / 4; i++) {
+ u_int32_t randomBits = rand();
+ [testData appendBytes:(void*)&randomBits length:4];
+ }
+
+ NSString *base64Value = AEBase64EncodedStringFromData(testData);
+ NSData *decodedValue = AEDataFromBase64EncodedString(base64Value);
+ STAssertEqualObjects(testData, decodedValue, @"Base 64 encoding or decoding failed");
+}
+
@end
Please sign in to comment.
Something went wrong with that request. Please try again.