Skip to content
This repository has been archived by the owner on Feb 2, 2021. It is now read-only.

iOS keychain cannot be accessed in unit tests #454

Open
odrobnik opened this issue Dec 3, 2014 · 22 comments
Open

iOS keychain cannot be accessed in unit tests #454

odrobnik opened this issue Dec 3, 2014 · 22 comments
Labels

Comments

@odrobnik
Copy link

odrobnik commented Dec 3, 2014

A unit test accessing the iOS keychain fails when run via xctool but works when run via xcodebuild.

This issue has first been described here

Here are my own tests, look at the bottom of the build log:

@LegNeato
Copy link
Contributor

LegNeato commented Dec 5, 2014

Yeah, this is likely because we don't run unit tests in the simulator context. Apple has been moving this direction for a bit now and we plan to go that way too.

@odrobnik
Copy link
Author

odrobnik commented Dec 5, 2014

Pray tell how to do that

@LegNeato
Copy link
Contributor

LegNeato commented Dec 5, 2014

It will require some xctool changes unfortunately. This was a change Apple did in the underlying system in Xcode 5. We have it on the todo to run unit tests in the simulator context, which will solve issues like this.

As a workaround, you can either swizzle or turn the test into an integration test (which runs in the simulator).

@odrobnik
Copy link
Author

odrobnik commented Dec 6, 2014

Please explain how to "turn the test into an integration test".

@grp
Copy link
Contributor

grp commented Dec 7, 2014

I think @LegNeato means an application test, which means you'd set the TEST_HOST and BUNDLE_LOADER build settings to an app to run the test inside of. If it's just for this, you can make an empty app for that.

@LegNeato
Copy link
Contributor

LegNeato commented Dec 8, 2014

Yeah, sorry I used the wrong terminology!

@ExtremeMan
Copy link
Contributor

Fixed since we have switched to use simctl to run logic tests (6b59d20). I've checked against the https://github.com/danielalves/NitroKeychain project. All tests are passed.

@lordkev
Copy link

lordkev commented Oct 26, 2015

I'm still getting "No keychain is available. You may need to restart your computer" with 0.2.7. Tests run fine using xcodebuild.

@ExtremeMan Is there something else I need to do to get it working with the switch to simctl?

@ExtremeMan
Copy link
Contributor

Hm. Are your tests application tests, @lordkev?

@lordkev
Copy link

lordkev commented Oct 26, 2015

@ExtremeMan: No, unit tests.

@ExtremeMan
Copy link
Contributor

@lordkev, can you try moving the test to application tests? (xcodebuild is launching tests in Simulator.app context, but xctool for performance is not doing that for logic tests).

@lordkev
Copy link

lordkev commented Oct 26, 2015

@ExtremeMan I'll give it a shot, but I'm kind of confused about your comment from Aug 13 about simctl fixing the issue and the tests on the NitroKeychain passing. Was that only when changing them to run as application tests?

@ExtremeMan
Copy link
Contributor

Hm, my comment is about logic tests, true. I will need a sample project which I can use to test. Can you help me with that?

@lordkev
Copy link

lordkev commented Oct 26, 2015

Just realized - in our case it's tests for a framework, so there isn't really any application to host it. NitroKeychain is still a good test project. I just verified that with xctool 0.2.7 three of 16 tests fail, but with xcodebuild all tests pass.

@ExtremeMan
Copy link
Contributor

Ok, I will try it out.

P.S. You can always create an empty test host app for your framework.

@ExtremeMan ExtremeMan reopened this Oct 26, 2015
@ExtremeMan
Copy link
Contributor

I looked into it. Yes, NitroKeychain tests are failing. Even if I checkout the xctool revision dated when I closed this issue. I think it is related to Yosemite security changes. Nevertheless the easy fix exists - run those test within application context. I have created an empty test host application for the NitroKeychain test bundle and it just works now.

I will probably need to keep this issue open since Xcode and xcodebuild apparently can run those tests somehow. Most likely they are forwarding keychain requests to simulator app? I am not sure and I am not sure if it is worth investigating since it could be easily fixed.

@ExtremeMan
Copy link
Contributor

Ah, that is interesting. After I reset my changes to NitroKeychain project (converted back to logic tests) they are passing. If I kill simulator app - they are failing. It might be related to some services triggered by Simulator app on launch.

@ExtremeMan
Copy link
Contributor

And if the simulator device is changed to the one that doesn't match the one used by xctool (for example, I open "iPhone 5" device in Simulator.app and specify "iPhone 4" when running tests via xctool) - tests fails again. So it is definitely related to Simulator.app running at the same moment. And simulated device should match.

@diogoguimaraes
Copy link

I'm facing the same problem and the workaround I'm using is described in the following steps:

  • Check which device travis is using during tests, you can find it in the log right when travis begins the Xcode tests;
  • Run xcrun simctl listto check the device's UDID;
  • Open the simulator with the device's UDID before running xctool (before_script):
    - open /Applications/Xcode.app/Contents/Developer/Applications/Simulator.app --args -CurrentDeviceUDID <UDID>;
  • Call xctool with your default configurations.

It would be great to have xctool handling this in a seamless way.

@ExtremeMan
Copy link
Contributor

@diogoguimaraes, thanks for the notes! I will have a look if xctool could launch a simulator app before running logic tests. Should be doable =)

@ExtremeMan ExtremeMan added the bug label Nov 16, 2015
s-aska added a commit to s-aska/KeyClip that referenced this issue Apr 30, 2016
huminzhi added a commit to forcedotcom/SalesforceMobileSDK-iOS that referenced this issue May 18, 2016
@emimh
Copy link

emimh commented May 26, 2016

Any update on this issue? Still having this problem in 0.2.9 ...

@ExtremeMan
Copy link
Contributor

As a workaround just convert tests to application tests or launch simulator manually.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

No branches or pull requests

7 participants