mirror of
https://github.com/corda/corda.git
synced 2025-02-21 09:51:57 +00:00
Minor: move some test functions for making zips out of the global namespace.
This commit is contained in:
parent
6ba3ae870c
commit
2cbdb719c6
@ -5,6 +5,7 @@ import com.google.common.hash.HashingInputStream
|
||||
import net.corda.client.rpc.CordaRPCConnection
|
||||
import net.corda.client.rpc.notUsed
|
||||
import net.corda.contracts.asset.Cash
|
||||
import net.corda.core.InputStreamAndHash
|
||||
import net.corda.core.contracts.DOLLARS
|
||||
import net.corda.core.contracts.POUNDS
|
||||
import net.corda.core.contracts.SWISS_FRANCS
|
||||
@ -16,7 +17,6 @@ import net.corda.core.node.services.Vault
|
||||
import net.corda.core.node.services.vault.*
|
||||
import net.corda.core.seconds
|
||||
import net.corda.core.utilities.OpaqueBytes
|
||||
import net.corda.core.sizedInputStreamAndHash
|
||||
import net.corda.core.utilities.loggerFor
|
||||
import net.corda.flows.CashIssueFlow
|
||||
import net.corda.flows.CashPaymentFlow
|
||||
@ -78,7 +78,7 @@ class StandaloneCordaRPClientTest {
|
||||
|
||||
@Test
|
||||
fun `test attachments`() {
|
||||
val attachment = sizedInputStreamAndHash(attachmentSize)
|
||||
val attachment = InputStreamAndHash.createInMemoryTestZip(attachmentSize, 1)
|
||||
assertFalse(rpcProxy.attachmentExists(attachment.sha256))
|
||||
val id = WrapperStream(attachment.inputStream).use { rpcProxy.uploadAttachment(it) }
|
||||
assertEquals(attachment.sha256, id, "Attachment has incorrect SHA256 hash")
|
||||
|
@ -291,37 +291,39 @@ fun extractZipFile(inputStream: InputStream, toDirectory: Path) {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a valid InputStream from an in-memory zip as required for tests.
|
||||
* Note that a slightly bigger than numOfExpectedBytes size is expected.
|
||||
*/
|
||||
@Throws(IllegalArgumentException::class)
|
||||
fun sizedInputStreamAndHash(numOfExpectedBytes: Int): InputStreamAndHash {
|
||||
if (numOfExpectedBytes <= 0) throw IllegalArgumentException("A positive number of numOfExpectedBytes is required.")
|
||||
val baos = ByteArrayOutputStream()
|
||||
ZipOutputStream(baos).use({ zos ->
|
||||
val arraySize = 1024
|
||||
val bytes = ByteArray(arraySize)
|
||||
val n = (numOfExpectedBytes - 1) / arraySize + 1 // same as Math.ceil(numOfExpectedBytes/arraySize).
|
||||
zos.setLevel(Deflater.NO_COMPRESSION)
|
||||
zos.putNextEntry(ZipEntry("z"))
|
||||
for (i in 0 until n) {
|
||||
zos.write(bytes, 0, arraySize)
|
||||
}
|
||||
zos.closeEntry()
|
||||
})
|
||||
return getInputStreamAndHashFromOutputStream(baos)
|
||||
}
|
||||
|
||||
/** Convert a [ByteArrayOutputStream] to [InputStreamAndHash]. */
|
||||
fun getInputStreamAndHashFromOutputStream(baos: ByteArrayOutputStream): InputStreamAndHash {
|
||||
// TODO: Consider converting OutputStream to InputStream without creating a ByteArray, probably using piped streams.
|
||||
fun ByteArrayOutputStream.getInputStreamAndHash(baos: ByteArrayOutputStream): InputStreamAndHash {
|
||||
val bytes = baos.toByteArray()
|
||||
// TODO: Consider calculating sha256 on the fly using a DigestInputStream.
|
||||
return InputStreamAndHash(ByteArrayInputStream(bytes), bytes.sha256())
|
||||
}
|
||||
|
||||
data class InputStreamAndHash(val inputStream: InputStream, val sha256: SecureHash.SHA256)
|
||||
data class InputStreamAndHash(val inputStream: InputStream, val sha256: SecureHash.SHA256) {
|
||||
companion object {
|
||||
/**
|
||||
* Get a valid InputStream from an in-memory zip as required for some tests. The zip consists of a single file
|
||||
* called "z" that contains the given content byte repeated the given number of times.
|
||||
* Note that a slightly bigger than numOfExpectedBytes size is expected.
|
||||
*/
|
||||
@Throws(IllegalArgumentException::class)
|
||||
@JvmStatic
|
||||
fun createInMemoryTestZip(numOfExpectedBytes: Int, content: Byte): InputStreamAndHash {
|
||||
require(numOfExpectedBytes > 0)
|
||||
val baos = ByteArrayOutputStream()
|
||||
ZipOutputStream(baos).use { zos ->
|
||||
val arraySize = 1024
|
||||
val bytes = ByteArray(arraySize) { content }
|
||||
val n = (numOfExpectedBytes - 1) / arraySize + 1 // same as Math.ceil(numOfExpectedBytes/arraySize).
|
||||
zos.setLevel(Deflater.NO_COMPRESSION)
|
||||
zos.putNextEntry(ZipEntry("z"))
|
||||
for (i in 0 until n) {
|
||||
zos.write(bytes, 0, arraySize)
|
||||
}
|
||||
zos.closeEntry()
|
||||
}
|
||||
return baos.getInputStreamAndHash(baos)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// TODO: Generic csv printing utility for clases.
|
||||
|
||||
|
@ -3,6 +3,8 @@ package net.corda.attachmentdemo
|
||||
import co.paralleluniverse.fibers.Suspendable
|
||||
import joptsimple.OptionParser
|
||||
import net.corda.client.rpc.CordaRPCClient
|
||||
import net.corda.core.InputStreamAndHash
|
||||
import net.corda.core.InputStreamAndHash.Companion.createInMemoryTestZip
|
||||
import net.corda.core.contracts.Contract
|
||||
import net.corda.core.contracts.ContractState
|
||||
import net.corda.core.contracts.TransactionForContract
|
||||
@ -16,7 +18,6 @@ import net.corda.core.identity.AbstractParty
|
||||
import net.corda.core.identity.Party
|
||||
import net.corda.core.messaging.CordaRPCOps
|
||||
import net.corda.core.messaging.startTrackedFlow
|
||||
import net.corda.core.sizedInputStreamAndHash
|
||||
import net.corda.core.transactions.SignedTransaction
|
||||
import net.corda.core.utilities.Emoji
|
||||
import net.corda.core.utilities.NetworkHostAndPort
|
||||
@ -72,7 +73,7 @@ fun main(args: Array<String>) {
|
||||
|
||||
/** An in memory test zip attachment of at least numOfClearBytes size, will be used. */
|
||||
fun sender(rpc: CordaRPCOps, numOfClearBytes: Int = 1024) { // default size 1K.
|
||||
val (inputStream, hash) = sizedInputStreamAndHash(numOfClearBytes)
|
||||
val (inputStream, hash) = InputStreamAndHash.createInMemoryTestZip(numOfClearBytes, 0)
|
||||
sender(rpc, inputStream, hash)
|
||||
}
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user