Permalink
Browse files

Check for bad inputs.

  • Loading branch information...
precipice committed Jul 25, 2011
1 parent 727f08c commit 145186057d7a99f28f89a7134d214d649edeb199
Showing with 70 additions and 6 deletions.
  1. +30 −4 HAKeychain/HAKeychain.m
  2. +40 −2 Tests/HAKeychainTest.m
View
@@ -8,14 +8,42 @@
#import "HAKeychain.h"
+@interface HAKeychain (PrivateMethods)
++ (BOOL)validService:(NSString *)service account:(NSString *)account;
++ (void)setCode:(NSInteger)code error:(NSError **)error;
+@end
+
@implementation HAKeychain
++ (BOOL)validService:(NSString *)service account:(NSString *)account {
+ if (service == NULL || [service length] == 0 ||
+ account == NULL || [account length] == 0) {
+ return NO;
+ }
+ return YES;
+}
+
+
++ (void)setCode:(NSInteger)code error:(NSError **)error {
+ if (error != NULL) {
+ *error = [NSError errorWithDomain:NSCocoaErrorDomain
+ code:code
+ userInfo:nil];
+ }
+}
+
+
+ (BOOL)createPassword:(NSString *)password
forService:(NSString *)service
account:(NSString *)account
keychain:(SecKeychainRef)keychain
error:(NSError **)error {
+
+ if (password == NULL || ![HAKeychain validService:service account:account]) {
+ [HAKeychain setCode:errSecParam error:error];
+ return NO;
+ }
const char *passwordUTF8 = [password UTF8String];
const char *serviceUTF8 = [service UTF8String];
@@ -32,10 +60,8 @@ + (BOOL)createPassword:(NSString *)password
&item);
if (item) CFRelease(item);
- if (status != noErr && error != NULL) {
- *error = [NSError errorWithDomain:NSCocoaErrorDomain
- code:status
- userInfo:nil];
+ if (status != noErr) {
+ [HAKeychain setCode:status error:error];
}
return status == noErr;
View
@@ -19,7 +19,6 @@ @interface HAKeychainTest : GHTestCase {
@implementation HAKeychainTest
- (void)setUpClass {
- GHTestLog(@"Creating private keychain for test suite to use.");
const char *pathName = "/tmp/HAKeychain-Test.keychain";
void *password = "hakeychaintest";
@@ -34,9 +33,14 @@ - (void)setUpClass {
- (void)tearDownClass {
- GHTestLog(@"Deleting test suite keychain.");
+ // Comment out this line and the GHAssertNoErr below, run the suite, and
+ // open up '/Applications/Utilities/Keychain Access.app' to see what the
+ // test cases look like in Keychain (or to debug a test).
OSStatus err = SecKeychainDelete(testKeychain);
+
+ // Run this even if the above is commented-out.
CFRelease(testKeychain);
+
GHAssertNoErr(err, @"Failed to delete test keychain");
}
@@ -105,4 +109,38 @@ - (void)testIgnoreError {
"but should have failed.");
}
+
+- (void)testNilPassword {
+ NSError *error = nil;
+ BOOL success = [HAKeychain createPassword:nil
+ forService:@"nilpassservice"
+ account:@"nilpassaccount"
+ keychain:testKeychain
+ error:&error];
+ GHAssertFalse(success, @"Password creation succeeded but should have failed.");
+ GHAssertNotNil(error, @"Should have an error, but there wasn't one.");
+}
+
+
+- (void)testZeroLengthService {
+ NSError *error = nil;
+ BOOL success = [HAKeychain createPassword:@"zeroservicepass"
+ forService:@""
+ account:@"zeroserviceaccount"
+ keychain:testKeychain
+ error:&error];
+ GHAssertFalse(success, @"Password creation succeeded but should have failed.");
+ GHAssertNotNil(error, @"Should have an error, but there wasn't one.");
+}
+
+
+- (void)testNilAccountAndError {
+ BOOL success = [HAKeychain createPassword:@"nilacctpass"
+ forService:@"nilacctservice"
+ account:nil
+ keychain:testKeychain
+ error:nil];
+ GHAssertFalse(success, @"Password creation succeeded but should have failed.");
+}
+
@end

0 comments on commit 1451860

Please sign in to comment.