Skip to content
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

Fix file leak when using metadata functions #1359

Merged
merged 2 commits into from
Oct 1, 2023
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Add test for file leak
pablobaxter committed Sep 30, 2023
commit 4cb3c8ad748d8a5adb11c64df0528c398be24c68
97 changes: 97 additions & 0 deletions okio/src/jvmTest/kotlin/okio/FileLeakTest.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
/*
* Copyright (C) 2023 Square, Inc. and others.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package okio

import java.util.zip.ZipEntry
import java.util.zip.ZipOutputStream
import kotlin.test.assertEquals
import kotlin.test.assertNotNull
import kotlin.test.assertTrue
import okio.Path.Companion.toPath
import okio.fakefilesystem.FakeFileSystem
import org.junit.After
import org.junit.Before
import org.junit.Test

class FileLeakTest {

private lateinit var fakeFileSystem: FakeFileSystem
private val fakeZip = "/test.zip".toPath()
private val fakeEntry = "some.file".toPath()

@Before
fun setup() {
fakeFileSystem = FakeFileSystem()
with(fakeFileSystem) {
write(fakeZip) {
writeZip {
putEntry(fakeEntry.name) {
writeUtf8("FooBar")
}
}
}
}
}

@After
fun tearDown() {
fakeFileSystem.checkNoOpenFiles()
}

@Test
fun zipFileSystemExistsTest() {
val zipFileSystem = fakeFileSystem.openZip(fakeZip)
assertTrue(zipFileSystem.exists(fakeEntry))
fakeFileSystem.checkNoOpenFiles()
}

@Test
fun zipFileSystemMetadataTest() {
val zipFileSystem = fakeFileSystem.openZip(fakeZip)
assertNotNull(zipFileSystem.metadataOrNull(fakeEntry))
fakeFileSystem.checkNoOpenFiles()
}

@Test
fun zipFileSystemSourceTest() {
val zipFileSystem = fakeFileSystem.openZip(fakeZip)
zipFileSystem.source(fakeEntry).use { source ->
assertEquals("FooBar", source.buffer().readUtf8())
}
fakeFileSystem.checkNoOpenFiles()
}
}

/**
* Writes a ZIP file to a [BufferedSink].
*/
private inline fun <R> BufferedSink.writeZip(action: ZipOutputStream.() -> R): R {
return ZipOutputStream(outputStream()).use(action)
}

/**
* Adds a new ZIP entry named [name], populates it with [action], and closes the entry.
*/
private inline fun <R> ZipOutputStream.putEntry(name: String, action: BufferedSink.() -> R): R {
putNextEntry(ZipEntry(name).apply { time = 0L })
val sink = sink().buffer()
return try {
sink.action()
} finally {
sink.flush()
closeEntry()
}
}