-
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
Add a StrictMode noteSlowCall for SSL init #8339
Merged
Merged
Changes from 8 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
6595d59
StrictMode warning
yschimke e114794
Fix test
yschimke 467aa82
Add strict mode checks on slow calls
yschimke 2446e01
Add strict mode checks on slow calls
yschimke f4805b0
dont fail
yschimke 3389def
Update StrictModeTest.kt
yschimke 6f39d9a
Cleanup
yschimke a0fdb92
Merge branch 'master' into OkHttp-warning
yschimke 402fa73
Run tests
yschimke File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
69 changes: 69 additions & 0 deletions
69
android-test/src/androidTest/java/okhttp/android/test/StrictModeTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
package okhttp.android.test | ||
|
||
import android.os.StrictMode | ||
import android.os.StrictMode.ThreadPolicy | ||
import android.os.strictmode.Violation | ||
import assertk.assertThat | ||
import assertk.assertions.hasSize | ||
import assertk.assertions.isEmpty | ||
import assertk.assertions.isEqualTo | ||
import okhttp3.HttpUrl.Companion.toHttpUrl | ||
import okhttp3.OkHttpClient | ||
import okhttp3.Request | ||
import okhttp3.internal.platform.Platform | ||
import org.junit.jupiter.api.AfterEach | ||
import org.junit.jupiter.api.Test | ||
import org.junit.jupiter.api.parallel.Isolated | ||
|
||
@Isolated | ||
class StrictModeTest { | ||
private val violations = mutableListOf<Violation>() | ||
|
||
@AfterEach | ||
fun cleanup() { | ||
StrictMode.setThreadPolicy( | ||
ThreadPolicy.Builder() | ||
.permitAll() | ||
.build(), | ||
) | ||
} | ||
|
||
@Test | ||
fun testInit() { | ||
Platform.resetForTests() | ||
|
||
applyStrictMode() | ||
|
||
// Not currently safe | ||
// See https://github.com/square/okhttp/pull/8248 | ||
OkHttpClient() | ||
|
||
assertThat(violations).hasSize(1) | ||
assertThat(violations[0].message).isEqualTo("newSSLContext") | ||
} | ||
|
||
@Test | ||
fun testNewCall() { | ||
Platform.resetForTests() | ||
|
||
val client = OkHttpClient() | ||
|
||
applyStrictMode() | ||
|
||
// Safe on main | ||
client.newCall(Request("https://google.com/robots.txt".toHttpUrl())) | ||
|
||
assertThat(violations).isEmpty() | ||
} | ||
|
||
private fun applyStrictMode() { | ||
StrictMode.setThreadPolicy( | ||
ThreadPolicy.Builder() | ||
.detectCustomSlowCalls() | ||
.penaltyListener({ it.run() }) { | ||
violations.add(it) | ||
} | ||
.build(), | ||
) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hmmm. When was this added? I would assume after our minSdk, so it should really have an
@SdkSuppress(minSdk=something)
on the class.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'll check, also why it's not failing if so.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ahhh, just the tests. And not running because of mannodermaus/android-junit5#225 and a minSdk 26 requirement for tests.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah. JUnit 5 continues to prove that it's the wrong choice for any project.