From 1a53f1a22a9ff128991d19135b9368ad5c1ef66c Mon Sep 17 00:00:00 2001 From: Chris Rankin Date: Tue, 10 Sep 2019 11:50:02 +0100 Subject: [PATCH] CORDA-2871: Ensure ClassLoader.loadClass() throws ClassNotFoundException for all cases where the class cannot be found. (#64) --- .../DeserializeKotlinAliasTest.kt | 69 +++++++++++++++++++ 1 file changed, 69 insertions(+) create mode 100644 src/test/kotlin/net/corda/djvm/serialization/DeserializeKotlinAliasTest.kt diff --git a/src/test/kotlin/net/corda/djvm/serialization/DeserializeKotlinAliasTest.kt b/src/test/kotlin/net/corda/djvm/serialization/DeserializeKotlinAliasTest.kt new file mode 100644 index 0000000000..aecdd740d3 --- /dev/null +++ b/src/test/kotlin/net/corda/djvm/serialization/DeserializeKotlinAliasTest.kt @@ -0,0 +1,69 @@ +package net.corda.djvm.serialization + +import net.corda.core.crypto.SecureHash +import net.corda.core.node.services.AttachmentId +import net.corda.core.serialization.CordaSerializable +import net.corda.core.serialization.internal._contextSerializationEnv +import net.corda.core.serialization.serialize +import net.corda.djvm.serialization.SandboxType.* +import org.junit.jupiter.api.Assertions.assertEquals +import org.junit.jupiter.api.Test +import org.junit.jupiter.api.extension.ExtendWith +import org.junit.jupiter.api.fail +import java.util.function.Function + +@ExtendWith(LocalSerialization::class) +class DeserializeKotlinAliasTest : TestBase(KOTLIN) { + @Test + fun `test deserializing kotlin alias`() { + val attachmentId = SecureHash.allOnesHash + val data = attachmentId.serialize() + + sandbox { + _contextSerializationEnv.set(createSandboxSerializationEnv(classLoader)) + + val sandboxAttachmentId = data.deserializeFor(classLoader) + + val taskFactory = classLoader.createRawTaskFactory() + val showAlias = classLoader.createTaskFor(taskFactory, ShowAlias::class.java) + val result = showAlias.apply(sandboxAttachmentId) ?: fail("Result cannot be null") + + assertEquals(ShowAlias().apply(attachmentId), result.toString()) + assertEquals(SANDBOX_STRING, result::class.java.name) + } + } + + class ShowAlias : Function { + override fun apply(id: AttachmentId): String { + return id.toString() + } + } + + @Test + fun `test deserializing data with kotlin alias`() { + val attachment = AttachmentData(SecureHash.allOnesHash) + val data = attachment.serialize() + + sandbox { + _contextSerializationEnv.set(createSandboxSerializationEnv(classLoader)) + + val sandboxAttachment = data.deserializeFor(classLoader) + + val taskFactory = classLoader.createRawTaskFactory() + val showAliasData = classLoader.createTaskFor(taskFactory, ShowAliasData::class.java) + val result = showAliasData.apply(sandboxAttachment) ?: fail("Result cannot be null") + + assertEquals(ShowAliasData().apply(attachment), result.toString()) + assertEquals(SANDBOX_STRING, result::class.java.name) + } + } + + class ShowAliasData: Function { + override fun apply(data: AttachmentData): String { + return data.toString() + } + } +} + +@CordaSerializable +data class AttachmentData(val id: AttachmentId)