Test PublicKey serializer has not been overridden

This commit is contained in:
Joseph Zuniga-Daly 2020-07-03 17:20:04 +01:00
parent 14d8651817
commit cf2928b7bd
3 changed files with 56 additions and 1 deletions

View File

@ -2,6 +2,7 @@ package net.corda.node.customcheckpointserializer
import com.nhaarman.mockito_kotlin.doReturn import com.nhaarman.mockito_kotlin.doReturn
import com.nhaarman.mockito_kotlin.whenever import com.nhaarman.mockito_kotlin.whenever
import net.corda.core.crypto.generateKeyPair
import net.corda.core.serialization.EncodingWhitelist import net.corda.core.serialization.EncodingWhitelist
import net.corda.core.serialization.internal.CheckpointSerializationContext import net.corda.core.serialization.internal.CheckpointSerializationContext
import net.corda.core.serialization.internal.checkpointDeserialize import net.corda.core.serialization.internal.checkpointDeserialize
@ -46,7 +47,8 @@ class CustomCheckpointSerializerTest(private val compression: CordaSerialization
TestCorDapp.TestAbstractClassSerializer(), TestCorDapp.TestAbstractClassSerializer(),
TestCorDapp.TestClassSerializer(), TestCorDapp.TestClassSerializer(),
TestCorDapp.TestInterfaceSerializer(), TestCorDapp.TestInterfaceSerializer(),
TestCorDapp.TestFinalClassSerializer() TestCorDapp.TestFinalClassSerializer(),
TestCorDapp.BrokenPublicKeySerializer()
)) ))
} }
@ -70,6 +72,20 @@ class CustomCheckpointSerializerTest(private val compression: CordaSerialization
testBrokenMapSerialization(DifficultToSerialize.BrokenMapFinal()) testBrokenMapSerialization(DifficultToSerialize.BrokenMapFinal())
} }
@Test(timeout=300_000)
fun `test PublicKey serializer has not been overridden`() {
val publicKey = generateKeyPair().public
// Serialize/deserialize
val checkpoint = publicKey.checkpointSerialize(context)
val deserializedCheckpoint = checkpoint.checkpointDeserialize(context)
// Check the elements are as expected
Assert.assertArrayEquals(publicKey.encoded, deserializedCheckpoint.encoded)
}
private fun testBrokenMapSerialization(brokenMap : MutableMap<String, String>): MutableMap<String, String> { private fun testBrokenMapSerialization(brokenMap : MutableMap<String, String>): MutableMap<String, String> {
// Add elements to the map // Add elements to the map
brokenMap.putAll(mapOf("key" to "value")) brokenMap.putAll(mapOf("key" to "value"))

View File

@ -64,4 +64,12 @@ class MockNetworkCustomCheckpointSerializerTest {
val result = node.startFlow(TestCorDapp.TestFlowWithDifficultToSerializeLocalVariableAsFinal(5)).get() val result = node.startFlow(TestCorDapp.TestFlowWithDifficultToSerializeLocalVariableAsFinal(5)).get()
Assertions.assertThat(result).isEqualTo(5) Assertions.assertThat(result).isEqualTo(5)
} }
@Test(timeout = 300_000)
@Suspendable
fun `check PublicKey serializer has not been overridden`() {
val node = mockNetwork.createPartyNode()
val result = node.startFlow(TestCorDapp.TestFlowCheckingPublicKeySerializer()).get()
Assertions.assertThat(result.encoded).isEqualTo(node.info.legalIdentities.first().owningKey.encoded)
}
} }

View File

@ -1,12 +1,14 @@
package net.corda.node.customcheckpointserializer package net.corda.node.customcheckpointserializer
import co.paralleluniverse.fibers.Suspendable import co.paralleluniverse.fibers.Suspendable
import net.corda.core.flows.FlowException
import net.corda.core.flows.FlowLogic import net.corda.core.flows.FlowLogic
import net.corda.core.flows.StartableByRPC import net.corda.core.flows.StartableByRPC
import net.corda.core.serialization.CheckpointCustomSerializer import net.corda.core.serialization.CheckpointCustomSerializer
import net.corda.testing.node.internal.CustomCordapp import net.corda.testing.node.internal.CustomCordapp
import net.corda.testing.node.internal.enclosedCordapp import net.corda.testing.node.internal.enclosedCordapp
import org.assertj.core.api.Assertions import org.assertj.core.api.Assertions
import java.security.PublicKey
import java.time.Duration import java.time.Duration
/** /**
@ -109,6 +111,23 @@ class TestCorDapp {
} }
} }
@StartableByRPC
class TestFlowCheckingPublicKeySerializer :
FlowLogic<PublicKey>() {
@Suspendable
override fun call(): PublicKey {
val ref = ourIdentity.owningKey
// Force a checkpoint
sleep(Duration.ofSeconds(0))
// Return deserialized object
return ref
}
}
// Custom serializers // Custom serializers
@Suppress("unused") @Suppress("unused")
@ -166,4 +185,16 @@ class TestCorDapp {
.also { it.putAll(proxy) } .also { it.putAll(proxy) }
} }
} }
@Suppress("unused")
class BrokenPublicKeySerializer :
CheckpointCustomSerializer<PublicKey, String> {
override fun toProxy(obj: PublicKey): String {
throw FlowException("Broken on purpose")
}
override fun fromProxy(proxy: String): PublicKey {
throw FlowException("Broken on purpose")
}
}
} }