OCUnit tests exit the command line, but work in Xcode when using Keychain services

I am using SFHFKeychainUtils to use Keychain services in my application. I have written several OCUnit tests that test the functionality of this code. Everything works fine when I run unit tests from Xcode on an iOS simulator or on my device. However, now I am trying to configure the CI server, and the test does not work when it is run through the command line with error code -25291. A look at the Apple documentation tells me: there are no trust results available (errSecNotAvailable). I linked Security.framework with my unit test project, it seems from what I read on the Internet, that’s all I need for this to work. Here is the command I invoke in the console:

/usr/bin/xcodebuild -target [Test_Target] -sdk /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator5.1.sdk/ -configuration Debug

Does anyone have any experience or suggestions for getting unit testing and Keychain Services to play well from the command line?

+7
ios unit-testing continuous-integration ocunit sfhfkeychainutils
Apr 03 2018-12-12T00:
source share
3 answers

I ran into the same problem and the solution for me was to make sure that the simulator was running before any test started. I did this using AppleScript during the build phase of Script in Xcode and essentially the same thing on the CI server. Here is the Script shell that will open the simulator:

exec osascript <<EOF

tell application "iOS Simulator"

activate

end tell

The security / keychain issue that causes this is apparently a known issue, although I don't yet have a radar that tracks it. If you are using Jenkins, put the above Script in the Execute Shell phase before the Xcode build phase. If you control this through Xcode itself, put it in the Script build phase before the RunUnitTests Script build phase. Hope that solves your problem!

+8
May 31 '12 at 19:27
source share
β€” -

I was not able to figure out why access to keychain fails when OCUnit tests are run from the command line.

To continue testing, I added the hacktastic category for my unit test purpose:

 // // SFHFKeychainUtils+UnitTestHacks.m // CB30 // // GRRR!!! http://stackoverflow.com/questions/9996578/ocunit-tests-fail-from-the-command-line-but-work-in-xcode-when-using-keychain-se // // Created by Joshua Vickery on 5/14/12. // #import "SFHFKeychainUtils+UnitTestHacks.h" static NSMutableDictionary *fakeKeyChainHolder; @implementation SFHFKeychainUtils (UnitTestHacks) + (NSMutableDictionary *)fakeKeyChainForServiceName:(NSString *)serviceName { if (nil == fakeKeyChainHolder) { fakeKeyChainHolder = [NSMutableDictionary dictionary]; } NSMutableDictionary *fakeKeyChain = [fakeKeyChainHolder objectForKey:serviceName]; if (nil == fakeKeyChain) { fakeKeyChain = [NSMutableDictionary dictionary]; [fakeKeyChainHolder setObject:fakeKeyChain forKey:serviceName]; } return fakeKeyChain; } #pragma clang diagnostic push #pragma clang diagnostic ignored "-Wobjc-protocol-method-implementation" + (BOOL) deleteItemForUsername: (NSString *) username andServiceName: (NSString *) serviceName error: (NSError **) error { [[self fakeKeyChainForServiceName:serviceName] removeObjectForKey:username]; return YES; } + (BOOL) storeUsername: (NSString *) username andPassword: (NSString *) password forServiceName: (NSString *) serviceName updateExisting: (BOOL) updateExisting error: (NSError **) error { [[self fakeKeyChainForServiceName:serviceName] setObject:password forKey:username]; return YES; } + (NSString *) getPasswordForUsername: (NSString *) username andServiceName: (NSString *) serviceName error: (NSError **) error { return [[self fakeKeyChainForServiceName:serviceName] objectForKey:username]; } #pragma clang diagnostic pop @end 

Note that this is not a good solution, but a workaround is to get unit tests until a better solution is found.

+1
May 14 '12 at 19:28
source share

I have the same problem. From my research, this can lead to a simulator version that works when running tests. Group testing of keychain with the simulator iphone 6.0 up will always lead to the failure of my tests when launched from the command line. Change it to any other version (4.3, 5.0, 5.1), and they will pass. Always good from Xcode with any version.

Perhaps this is a problem with the command line tools, which does not set the necessary flags before running the tests.

I opened a new question here with a small test case: Keychain Services API with errSecNotAvailable error in iphonesimulator 6.0

0
Oct 29
source share



All Articles