CORDA-3651: addManifest now uses separate files for reading and writing. (#6026)

* CORDA-3651: addManifest now uses separate files for reading and writing.

* CORDA-3651: The jar scanning loader now closes itsself.

Co-authored-by: Adel El-Beik <adelel-beik@19LDN-MAC108.local>
This commit is contained in:
Adel El-Beik 2020-03-06 09:14:36 +00:00 committed by GitHub
parent e5a8888232
commit 5b50ef49bc
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 15 additions and 5 deletions

View File

@ -18,6 +18,7 @@ import java.io.FileOutputStream
import java.lang.IllegalStateException
import java.net.URL
import java.nio.file.Files
import java.util.Arrays.asList
import java.util.jar.JarOutputStream
import java.util.zip.Deflater.NO_COMPRESSION
import java.util.zip.ZipEntry
@ -200,8 +201,11 @@ class CordappProviderImplTests {
val duplicateJarPath = signedJarPath.parent.resolve("duplicate-" + signedJarPath.fileName)
Files.copy(signedJarPath, duplicateJarPath)
val urls = asList(signedJarPath.toUri().toURL(), duplicateJarPath.toUri().toURL())
JarScanningCordappLoader.fromJarUrls(urls, VersionInfo.UNKNOWN).use {
assertFailsWith<IllegalStateException> {
newCordappProvider(signedJarPath.toUri().toURL(), duplicateJarPath.toUri().toURL())
CordappProviderImpl(it, stubConfigProvider, attachmentStore).apply { start() }
}
}
}
}
@ -212,8 +216,11 @@ class CordappProviderImplTests {
SelfCleaningDir().use { file ->
val jarA = ContractJarTestUtils.makeTestContractJar(file.path, listOf("com.example.MyContract", "com.example.AnotherContractForA"), generateManifest = false, jarFileName = "sampleA.jar")
val jarB = ContractJarTestUtils.makeTestContractJar(file.path, listOf("com.example.MyContract", "com.example.AnotherContractForB"), generateManifest = false, jarFileName = "sampleB.jar")
val urls = asList(jarA.toUri().toURL(), jarB.toUri().toURL())
JarScanningCordappLoader.fromJarUrls(urls, VersionInfo.UNKNOWN).use {
assertFailsWith<IllegalStateException> {
newCordappProvider(jarA.toUri().toURL(), jarB.toUri().toURL())
CordappProviderImpl(it, stubConfigProvider, attachmentStore).apply { start() }
}
}
}
}

View File

@ -12,6 +12,7 @@ import java.nio.file.Files
import java.nio.file.NoSuchFileException
import java.nio.file.Path
import java.nio.file.Paths
import java.nio.file.StandardCopyOption.REPLACE_EXISTING
import java.security.PublicKey
import java.util.jar.Attributes
import java.util.jar.JarInputStream
@ -88,12 +89,13 @@ object JarSignatureTestUtils {
JarInputStream(FileInputStream((this / fileName).toFile())).use(JarSignatureCollector::collectSigners)
fun Path.addManifest(fileName: String, vararg entries: Pair<Attributes.Name, String>) {
val outputFile = this / (fileName + "Output")
JarInputStream(FileInputStream((this / fileName).toFile())).use { input ->
val manifest = input.manifest ?: Manifest()
entries.forEach { (attributeName, value) ->
manifest.mainAttributes[attributeName] = value
}
val output = JarOutputStream(FileOutputStream((this / fileName).toFile()), manifest)
val output = JarOutputStream(FileOutputStream(outputFile.toFile()), manifest)
var entry = input.nextEntry
val buffer = ByteArray(1 shl 14)
while (true) {
@ -108,5 +110,6 @@ object JarSignatureTestUtils {
}
output.close()
}
Files.copy(outputFile, this / fileName, REPLACE_EXISTING)
}
}