-
-
Notifications
You must be signed in to change notification settings - Fork 478
fix(core): Close manifest streams after version detection #6125
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
Closed
+139
โ42
Closed
Changes from all commits
Commits
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
This file contains hidden or 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 hidden or 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
78 changes: 78 additions & 0 deletions
78
sentry/src/test/java/io/sentry/internal/ManifestVersionReaderTest.kt
This file contains hidden or 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,78 @@ | ||
| package io.sentry.internal | ||
|
|
||
| import com.google.common.truth.Truth.assertThat | ||
| import java.io.ByteArrayInputStream | ||
| import java.net.URL | ||
| import java.net.URLConnection | ||
| import java.nio.charset.StandardCharsets | ||
| import java.util.Collections | ||
| import kotlin.test.Test | ||
| import org.mockito.kotlin.inOrder | ||
| import org.mockito.kotlin.mock | ||
| import org.mockito.kotlin.whenever | ||
|
|
||
| class ManifestVersionReaderTest { | ||
| private class CloseTrackingInputStream(content: String) : | ||
| ByteArrayInputStream(content.toByteArray(StandardCharsets.UTF_8)) { | ||
| var isClosed = false | ||
|
|
||
| override fun close() { | ||
| isClosed = true | ||
| super.close() | ||
| } | ||
| } | ||
|
|
||
| private class Fixture(contents: List<String>) { | ||
| val classLoader = mock<ClassLoader>() | ||
| val inputStreams = contents.map(::CloseTrackingInputStream) | ||
| val connections = contents.map { mock<URLConnection>() } | ||
| val urls = contents.map { mock<URL>() } | ||
|
|
||
| init { | ||
| whenever(classLoader.getResources("META-INF/MANIFEST.MF")) | ||
| .thenReturn(Collections.enumeration(urls)) | ||
| urls.indices.forEach { index -> | ||
| whenever(urls[index].openConnection()).thenReturn(connections[index]) | ||
| whenever(connections[index].inputStream).thenReturn(inputStreams[index]) | ||
| } | ||
| } | ||
|
|
||
| val sut = ManifestVersionReader(classLoader) | ||
| } | ||
|
|
||
| @Test | ||
| fun `closes manifest stream and disables connection caching before opening it`() { | ||
| val fixture = Fixture(listOf(validManifest())) | ||
|
|
||
| fixture.sut.readManifestFiles() | ||
|
|
||
| assertThat(fixture.inputStreams.single().isClosed).isTrue() | ||
| inOrder(fixture.connections.single()) { | ||
| verify(fixture.connections.single()).useCaches = false | ||
| verify(fixture.connections.single()).inputStream | ||
| } | ||
| } | ||
|
|
||
| @Test | ||
| fun `closes malformed manifest stream and continues reading manifests`() { | ||
| val fixture = Fixture(listOf("not a manifest\n", validManifest())) | ||
|
|
||
| val versionInfo = fixture.sut.readOpenTelemetryVersion() | ||
|
|
||
| assertThat(fixture.inputStreams.map { it.isClosed }).containsExactly(true, true).inOrder() | ||
| assertThat(versionInfo).isNotNull() | ||
| assertThat(versionInfo!!.sdkName).isEqualTo("sentry.java.opentelemetry.test") | ||
| assertThat(versionInfo.sdkVersion).isEqualTo("1.2.3") | ||
| } | ||
|
|
||
| companion object { | ||
| private fun validManifest() = | ||
| """ | ||
| Manifest-Version: 1.0 | ||
| Sentry-Opentelemetry-SDK-Name: sentry.java.opentelemetry.test | ||
| Implementation-Version: 1.2.3 | ||
|
|
||
| """ | ||
| .trimIndent() | ||
| } | ||
| } |
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.
this constructor allows the creation of mutliple instances of what was previously a lazily instantiated singleton.
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.
This is just for tests, I'll mark it accordingly.