-
Notifications
You must be signed in to change notification settings - Fork 9.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
isCleartextTrafficPermitted()
fails on OpenJDK 8 + Robolectric
#2533
Comments
This is when running tests with Roboelectric, BTW. So maybe the code determines, the Platform is android, when it's actually not. |
I tried to write up a simple testcase that reproduces this, but apparently it truly is the combination of the following things that make the code fail:
|
Okay, so because the setup is kinda contrived: Here is a sample that reproduces this... https://github.com/kungfoo/okhttp-on-jdk-with-robolectric |
Does it throw or return the wrong result? |
It crashes/throws in the static initializer which results in a Does it throw or return the wrong result? — |
@kungfoo could you paste the stacktrace? |
Will do later when I get my laptop back online...
|
If you want to debug the code under the described circumstances, you can simply use gradle to run the sample project I created for this. Here's the stacktrace:
|
isCleartextTrafficPermitted()
fails on OpenJDK 8 (1.8.0_92-b14)isCleartextTrafficPermitted()
fails on OpenJDK 8 + Robolectric
Wanna submit a fix? |
Might be able to do that, when I'm back out of the woods... :)
|
Here's the message for the target
|
Sounds like you're not using Robolectric since that message comes from the On Thu, May 26, 2016 at 12:56 AM Eliezer Graber notifications@github.com
|
I'm definitely using Robolectric (verified on the call stack), which is why I was confused. Could it be a classloader issue where |
Maybe, but that would be a robolectric bug in its ParallelUniverse then. If On Thu, May 26, 2016 at 1:09 AM Eliezer Graber notifications@github.com
|
Could it be happening because I'm on Robolectric 2.4 (which I believe only supports up to API 18), and |
Possibly. Try the latest. On Thu, May 26, 2016 at 1:21 AM Eliezer Graber notifications@github.com
|
We're pretty stuck on 2.4, so I used the project referenced above and changed the robolectric properties to use API 23. I was greeted by this when running the test:
|
Seems like a clear deficiency on their side then. Not sure what action On Thu, May 26, 2016 at 1:34 AM Eliezer Graber notifications@github.com
|
Would wrapping it in a version check help? |
It's already behind one. You're compiling with API 23 (or newer) and thus On Thu, May 26, 2016 at 2:11 AM Eliezer Graber notifications@github.com
|
Would that affect a runtime check? Something like:
When I try that running in Robolectric it returns 21. |
I'm getting
with okhttp 3.3.1 and both robolectric 3.0 and 3.1-rc1 |
I was able to get the test green. Here's what I did:
@RunWith(RobolectricGradleTestRunner.class)
@Config(sdk = 23)
public class CreateOkHttpClientTest { As for why it works, I don't know. It appears the default SDK is 21. The bit I don't understand is why it isn't throwing a ClassNotFoundException given this class is only in SDK 23. The test project itself is depending on API 23, which may have something to do with it. |
I have a vague idea of why this works now, and I opened up an issue on robolectric project. Let's see what they say, robolectric/robolectric#2478 |
Robolectric 3.1-rc1 is pretty much unusable right now, as running tests take ~10x longer. Any other workarounds? |
Oh, It works if you add a @Implements(NetworkSecurityPolicy.class)
public class NetworkSecurityPolicyWorkaround {
@Implementation
public static NetworkSecurityPolicy getInstance() {
//noinspection OverlyBroadCatchBlock
try {
Class<?> shadow = Class.forName("android.security.NetworkSecurityPolicy");
return (NetworkSecurityPolicy) shadow.newInstance();
} catch (Exception e) {
throw new AssertionError();
}
}
@Implementation
public boolean isCleartextTrafficPermitted(String hostname) {
return true;
}
} |
Summary: - 3.3.1 wasn't compatible with Robolectric 3.0, however 3.4.0 [fixes](square/okhttp#2533 (comment)) this issue. - Few other fixes to OkHttp mentioned in the [CHANGELOG.md](https://github.com/square/okhttp/blob/master/CHANGELOG.md) > Fixes: #7743 > - Android apps can recover from a `REFUSED_STREAM` in HTTP/2. - A few other fixes mentioned in the [CHANGELOG.md](https://github.com/square/okhttp/blob/master/CHANGELOG.md) - CircleCi - Test with `/Examples` ✅ Closes #8672 Reviewed By: alsutton Differential Revision: D3541293 Pulled By: bestander fbshipit-source-id: 76429861b4f4df15cb9c18ab0f177daee3e1459d
Summary: - 3.3.1 wasn't compatible with Robolectric 3.0, however 3.4.0 [fixes](square/okhttp#2533 (comment)) this issue. - Few other fixes to OkHttp mentioned in the [CHANGELOG.md](https://github.com/square/okhttp/blob/master/CHANGELOG.md) > Fixes: facebook#7743 > - Android apps can recover from a `REFUSED_STREAM` in HTTP/2. - A few other fixes mentioned in the [CHANGELOG.md](https://github.com/square/okhttp/blob/master/CHANGELOG.md) - CircleCi - Test with `/Examples` ✅ Closes facebook#8672 Reviewed By: alsutton Differential Revision: D3541293 Pulled By: bestander fbshipit-source-id: 76429861b4f4df15cb9c18ab0f177daee3e1459d
Summary: - 3.3.1 wasn't compatible with Robolectric 3.0, however 3.4.0 [fixes](square/okhttp#2533 (comment)) this issue. - Few other fixes to OkHttp mentioned in the [CHANGELOG.md](https://github.com/square/okhttp/blob/master/CHANGELOG.md) > Fixes: facebook#7743 > - Android apps can recover from a `REFUSED_STREAM` in HTTP/2. - A few other fixes mentioned in the [CHANGELOG.md](https://github.com/square/okhttp/blob/master/CHANGELOG.md) - CircleCi - Test with `/Examples` ✅ Closes facebook#8672 Reviewed By: alsutton Differential Revision: D3541293 Pulled By: bestander fbshipit-source-id: 76429861b4f4df15cb9c18ab0f177daee3e1459d
Summary: - 3.3.1 wasn't compatible with Robolectric 3.0, however 3.4.0 [fixes](square/okhttp#2533 (comment)) this issue. - Few other fixes to OkHttp mentioned in the [CHANGELOG.md](https://github.com/square/okhttp/blob/master/CHANGELOG.md) > Fixes: facebook#7743 > - Android apps can recover from a `REFUSED_STREAM` in HTTP/2. - A few other fixes mentioned in the [CHANGELOG.md](https://github.com/square/okhttp/blob/master/CHANGELOG.md) - CircleCi - Test with `/Examples` ✅ Closes facebook#8672 Reviewed By: alsutton Differential Revision: D3541293 Pulled By: bestander fbshipit-source-id: 76429861b4f4df15cb9c18ab0f177daee3e1459d
I'm not using Roboeletric, but i'm in pointing towards API 24, with okhttp version 3.4.1: @Override public boolean isCleartextTrafficPermitted(String hostname) {
try {
Class<?> networkPolicyClass = Class.forName("android.security.NetworkSecurityPolicy");
Method getInstanceMethod = networkPolicyClass.getMethod("getInstance");
Object networkSecurityPolicy = getInstanceMethod.invoke(null);
Method isCleartextTrafficPermittedMethod = networkPolicyClass
.getMethod("isCleartextTrafficPermitted", String.class);
return (boolean) isCleartextTrafficPermittedMethod.invoke(networkSecurityPolicy, hostname);
} catch (ClassNotFoundException | NoSuchMethodException e) {
return super.isCleartextTrafficPermitted(hostname);
} catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException e) {
throw new AssertionError();
}
} which comes from |
@felipecsl solution (for those who can't upgrade to Robolectric 3+) works in this case too. But I really wouldn't like to use that :( |
Happens with OkHttp 3.5.0 and robolectric 3.1.4 |
We're running into the same issue with OkHttp 3.5.0. Instead of using Robolectric directly we use unmock (which relies on Robolectric's android-all jar). |
I'm so glad I found this ticket, was going crazy trying to understand why okhttp suddenly had an exception when running with robolectric |
I am also still seeing this, but have resolved it with same fix as @eygraber.
add to unit-tests source-set only, eg:
lastly note that this may break with OkHttp 3.7.x |
Is there anything to do in OkHttp for this? |
Reading this, seems like it is fixed in Robolectric 3.3 |
Summary: - 3.3.1 wasn't compatible with Robolectric 3.0, however 3.4.0 [fixes](square/okhttp#2533 (comment)) this issue. - Few other fixes to OkHttp mentioned in the [CHANGELOG.md](https://github.com/square/okhttp/blob/master/CHANGELOG.md) > Fixes: #7743 > - Android apps can recover from a `REFUSED_STREAM` in HTTP/2. - A few other fixes mentioned in the [CHANGELOG.md](https://github.com/square/okhttp/blob/master/CHANGELOG.md) - CircleCi - Test with `/Examples` ✅ Closes facebook/react-native#8672 Reviewed By: alsutton Differential Revision: D3541293 Pulled By: bestander fbshipit-source-id: 76429861b4f4df15cb9c18ab0f177daee3e1459d
How to reproduce:
new OkHttpClient()
: The code fails in the static initializer, when trying to figure out the platform.The code that breaks was introduced here: #2513
Either the detection of the runtime is broken, or the code is wrong there. I'll try to figure out an automatic test for this.
The text was updated successfully, but these errors were encountered: