In iOS10, is it possible to access Keychain from within a static library/framework, for unit testing
Asked Answered
T

1

6

Until Xcode 8/iOS 10, I was able to access the Keychain from within the unit tests of a static library. After upgrading, I get an error, see stack trace:

failed: caught "NSInternalInconsistencyException", "Couldn't add the Keychain Item."
(
    0   CoreFoundation                      0x000000010b2a734b __exceptionPreprocess + 171
    1   libobjc.A.dylib                     0x000000010ad0821e objc_exception_throw + 48
    2   CoreFoundation                      0x000000010b2ab442 +[NSException raise:format:arguments:] + 98
    3   Foundation                          0x000000010a89eedd -[NSAssertionHandler handleFailureInMethod:object:file:lineNumber:description:] + 195
    4   MyLibTests                   0x0000000117366dea -[KeychainItemWrapper writeToKeychain] + 970
    5   MyLibTests                   0x000000011736614a -[KeychainItemWrapper setObject:forKey:] + 218
    6   MyLibTests                   0x000000011736a794 -[MyUserManager(Credentials) save] + 196
    7   PawPointsLibTests                   0x0000000116ef5490 -[MyLibTest tearDown] + 256
    8   XCTest                              0x000000010a6e0547 __24-[XCTestCase invokeTest]_block_invoke_2 + 1304
    9   XCTest                              0x000000010a7189d4 -[XCTestContext performInScope:] + 190
    10  XCTest                              0x000000010a6e001c -[XCTestCase invokeTest] + 255
    11  XCTest                              0x000000010a6e0835 -[XCTestCase performTest:] + 457
    12  XCTest                              0x000000010a6dd8fd -[XCTestSuite performTest:] + 491
    13  XCTest                              0x000000010a6dd8fd -[XCTestSuite performTest:] + 491
    14  XCTest                              0x000000010a6dd8fd -[XCTestSuite performTest:] + 491
    15  XCTest                              0x000000010a6c9b0c __25-[XCTestDriver _runSuite]_block_invoke + 51
    16  XCTest                              0x000000010a6eafc3 -[XCTestObservationCenter _observeTestExecutionForBlock:] + 602
    17  XCTest                              0x000000010a6c99a9 -[XCTestDriver _runSuite] + 436
    18  XCTest                              0x000000010a6ca7a2 -[XCTestDriver _checkForTestManager] + 287
    19  XCTest                              0x000000010a719f5c _XCTestMain + 628
    20  xctest                              0x000000010a64c922 xctest + 6434
    21  libdyld.dylib                       0x000000010e01368d start + 1
    22  ???                                 0x0000000000000005 0x0 + 5

Any ideas how to make this work again. At the app level, enabling keychain sharing and adding entitlements took care of it, but that won't help me b/c I need to be able to test the library independently before shipping, since the app development is done by a different team. I am using Apple's old example for how to add to keychain, but have tried other libraries, all with the same result.

Thievish answered 16/9, 2016 at 18:53 Comment(0)
A
13

You can try this workaround:

  1. Add an app target to your project.

  2. Enable keychain sharing and adding entitlement for the app you added.

  3. Go to the test target of your project. Under General->Testing->Host application, add the application you created.

  4. Run the tests

Antiperspirant answered 14/11, 2016 at 11:11 Comment(2)
For me it's enough to do steps 1, 3 and 4. Thanks!Boomkin
Thanks! This also worked for my XCFramework without needing to enable Keychain sharing. For the benefit of others, the error I was seeing was -34018 ("A required entitlement isn’t present") as the OSStatus when trying to load or save Keychain items from my framework's unit tests.Malorie

© 2022 - 2024 — McMap. All rights reserved.