Skip to content

Instantly share code, notes, and snippets.

@kgleong
Last active February 9, 2018 22:09
Show Gist options
  • Save kgleong/dca2761da414b26e447c246f00d71946 to your computer and use it in GitHub Desktop.
Save kgleong/dca2761da414b26e447c246f00d71946 to your computer and use it in GitHub Desktop.
Valet Test for migrateObjectsMatchingQuery:removeOnCompletion:
- (void)test_migrateObjectsMatchingQueryRemoveOnCompletion_withNSDataAsAccount_doesNotRaiseException;
{
    NSString *identifier = @"my_identifier";
    NSData *dataBlob = [@"foo" dataUsingEncoding:NSUTF8StringEncoding];

    // kSecAttrAccount entry is expected to be a CFString, but a CFDataRef can also be stored as a value.
    NSDictionary *keychainData = @{ 
        (__bridge id)kSecAttrService : identifier, 
        (__bridge id)kSecClass : (__bridge id)kSecClassGenericPassword, 
        (__bridge id)kSecAttrAccount : dataBlob, // NSData object instead of NSString
        (__bridge id)kSecValueData : dataBlob
    };

    OSStatus status = SecItemAdd((__bridge CFDictionaryRef)keychainData, NULL);
    XCTAssertEqual(status, errSecSuccess); // Insert Succeeded

    NSDictionary *query = @{
        (__bridge id)kSecClass : (__bridge id)kSecClassGenericPassword, 
        (__bridge id)kSecAttrService : identifier 
    };

    NSError *error = [self.valet migrateObjectsMatchingQuery:query removeOnCompletion:NO];
    XCTAssertNil(error); // Passes for iOS, fails for macOS
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment