CORDA-716 Retire withTestSerialization (#2240)

This commit is contained in:
Andrzej Cichocki 2017-12-14 12:42:18 +00:00 committed by GitHub
parent 5b33db93fc
commit 05bc05a936
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 115 additions and 99 deletions

View File

@ -7,9 +7,10 @@ import com.google.common.collect.testing.features.CollectionSize
import junit.framework.TestSuite import junit.framework.TestSuite
import net.corda.core.serialization.deserialize import net.corda.core.serialization.deserialize
import net.corda.core.serialization.serialize import net.corda.core.serialization.serialize
import net.corda.testing.withTestSerialization import net.corda.testing.SerializationEnvironmentRule
import org.assertj.core.api.Assertions.assertThat import org.assertj.core.api.Assertions.assertThat
import org.assertj.core.api.Assertions.assertThatThrownBy import org.assertj.core.api.Assertions.assertThatThrownBy
import org.junit.Rule
import org.junit.Test import org.junit.Test
import org.junit.runner.RunWith import org.junit.runner.RunWith
import org.junit.runners.Suite import org.junit.runners.Suite
@ -36,6 +37,10 @@ class NonEmptySetTest {
} }
class General { class General {
@Rule
@JvmField
val testSerialization = SerializationEnvironmentRule()
@Test @Test
fun `copyOf - empty source`() { fun `copyOf - empty source`() {
assertThatThrownBy { NonEmptySet.copyOf(HashSet<Int>()) }.isInstanceOf(IllegalArgumentException::class.java) assertThatThrownBy { NonEmptySet.copyOf(HashSet<Int>()) }.isInstanceOf(IllegalArgumentException::class.java)
@ -48,11 +53,9 @@ class NonEmptySetTest {
@Test @Test
fun `serialize deserialize`() { fun `serialize deserialize`() {
withTestSerialization { val original = NonEmptySet.of(-17, 22, 17)
val original = NonEmptySet.of(-17, 22, 17) val copy = original.serialize().deserialize()
val copy = original.serialize().deserialize() assertThat(copy).isEqualTo(original).isNotSameAs(original)
assertThat(copy).isEqualTo(original).isNotSameAs(original)
}
} }
} }

View File

@ -29,6 +29,7 @@ import org.apache.qpid.proton.codec.EncoderImpl
import org.assertj.core.api.Assertions.* import org.assertj.core.api.Assertions.*
import org.junit.Assert.* import org.junit.Assert.*
import org.junit.Ignore import org.junit.Ignore
import org.junit.Rule
import org.junit.Test import org.junit.Test
import java.io.ByteArrayInputStream import java.io.ByteArrayInputStream
import java.io.IOException import java.io.IOException
@ -54,6 +55,10 @@ class SerializationOutputTests {
val MINI_CORP_PUBKEY get() = miniCorp.publicKey val MINI_CORP_PUBKEY get() = miniCorp.publicKey
} }
@Rule
@JvmField
val testSerialization = SerializationEnvironmentRule()
data class Foo(val bar: String, val pub: Int) data class Foo(val bar: String, val pub: Int)
data class testFloat(val f: Float) data class testFloat(val f: Float)
@ -473,9 +478,9 @@ class SerializationOutputTests {
assertSerializedThrowableEquivalent(t, desThrowable) assertSerializedThrowableEquivalent(t, desThrowable)
} }
private fun serdesThrowableWithInternalInfo(t: Throwable, factory: SerializerFactory, factory2: SerializerFactory, expectedEqual: Boolean = true): Throwable = withTestSerialization { private fun serdesThrowableWithInternalInfo(t: Throwable, factory: SerializerFactory, factory2: SerializerFactory, expectedEqual: Boolean = true): Throwable {
val newContext = SerializationFactory.defaultFactory.defaultContext.withProperty(CommonPropertyNames.IncludeInternalInfo, true) val newContext = SerializationFactory.defaultFactory.defaultContext.withProperty(CommonPropertyNames.IncludeInternalInfo, true)
SerializationFactory.defaultFactory.asCurrent { withCurrentContext(newContext) { serdes(t, factory, factory2, expectedEqual) } } return SerializationFactory.defaultFactory.asCurrent { withCurrentContext(newContext) { serdes(t, factory, factory2, expectedEqual) } }
} }
@Test @Test

View File

@ -24,16 +24,20 @@ import net.corda.testing.*
import net.corda.testing.driver.DriverDSL import net.corda.testing.driver.DriverDSL
import net.corda.testing.driver.NodeHandle import net.corda.testing.driver.NodeHandle
import net.corda.testing.driver.driver import net.corda.testing.driver.driver
import net.corda.testing.internal.withoutTestSerialization
import net.corda.testing.services.MockAttachmentStorage import net.corda.testing.services.MockAttachmentStorage
import net.corda.testing.rigorousMock import net.corda.testing.rigorousMock
import net.corda.testing.withTestSerialization
import org.junit.Assert.assertEquals import org.junit.Assert.assertEquals
import org.junit.Rule
import org.junit.Test import org.junit.Test
import java.net.URLClassLoader import java.net.URLClassLoader
import java.nio.file.Files import java.nio.file.Files
import kotlin.test.assertFailsWith import kotlin.test.assertFailsWith
class AttachmentLoadingTests { class AttachmentLoadingTests {
@Rule
@JvmField
val testSerialization = SerializationEnvironmentRule()
private val attachments = MockAttachmentStorage() private val attachments = MockAttachmentStorage()
private val provider = CordappProviderImpl(CordappLoader.createDevMode(listOf(isolatedJAR)), attachments) private val provider = CordappProviderImpl(CordappLoader.createDevMode(listOf(isolatedJAR)), attachments)
private val cordapp get() = provider.cordapps.first() private val cordapp get() = provider.cordapps.first()
@ -80,7 +84,7 @@ class AttachmentLoadingTests {
} }
@Test @Test
fun `test a wire transaction has loaded the correct attachment`() = withTestSerialization { fun `test a wire transaction has loaded the correct attachment`() {
val appClassLoader = appContext.classLoader val appClassLoader = appContext.classLoader
val contractClass = appClassLoader.loadClass(ISOLATED_CONTRACT_ID).asSubclass(Contract::class.java) val contractClass = appClassLoader.loadClass(ISOLATED_CONTRACT_ID).asSubclass(Contract::class.java)
val generateInitialMethod = contractClass.getDeclaredMethod("generateInitial", PartyAndReference::class.java, Integer.TYPE, Party::class.java) val generateInitialMethod = contractClass.getDeclaredMethod("generateInitial", PartyAndReference::class.java, Integer.TYPE, Party::class.java)
@ -96,7 +100,7 @@ class AttachmentLoadingTests {
} }
@Test @Test
fun `test that attachments retrieved over the network are not used for code`() { fun `test that attachments retrieved over the network are not used for code`() = withoutTestSerialization {
driver { driver {
installIsolatedCordappTo(bankAName) installIsolatedCordappTo(bankAName)
val (bankA, bankB) = createTwoNodes() val (bankA, bankB) = createTwoNodes()
@ -104,15 +108,17 @@ class AttachmentLoadingTests {
bankA.rpc.startFlowDynamic(flowInitiatorClass, bankB.nodeInfo.legalIdentities.first()).returnValue.getOrThrow() bankA.rpc.startFlowDynamic(flowInitiatorClass, bankB.nodeInfo.legalIdentities.first()).returnValue.getOrThrow()
} }
} }
Unit
} }
@Test @Test
fun `tests that if the attachment is loaded on both sides already that a flow can run`() { fun `tests that if the attachment is loaded on both sides already that a flow can run`() = withoutTestSerialization {
driver { driver {
installIsolatedCordappTo(bankAName) installIsolatedCordappTo(bankAName)
installIsolatedCordappTo(bankBName) installIsolatedCordappTo(bankBName)
val (bankA, bankB) = createTwoNodes() val (bankA, bankB) = createTwoNodes()
bankA.rpc.startFlowDynamic(flowInitiatorClass, bankB.nodeInfo.legalIdentities.first()).returnValue.getOrThrow() bankA.rpc.startFlowDynamic(flowInitiatorClass, bankB.nodeInfo.legalIdentities.first()).returnValue.getOrThrow()
} }
Unit
} }
} }

View File

@ -13,6 +13,7 @@ import net.corda.nodeapi.internal.crypto.CertificateType
import net.corda.nodeapi.internal.crypto.X509CertificateFactory import net.corda.nodeapi.internal.crypto.X509CertificateFactory
import net.corda.nodeapi.internal.crypto.X509Utilities import net.corda.nodeapi.internal.crypto.X509Utilities
import net.corda.testing.* import net.corda.testing.*
import org.junit.Rule
import org.junit.Test import org.junit.Test
import kotlin.test.assertEquals import kotlin.test.assertEquals
import kotlin.test.assertFailsWith import kotlin.test.assertFailsWith
@ -34,6 +35,10 @@ class InMemoryIdentityServiceTests {
fun createService(vararg identities: PartyAndCertificate) = InMemoryIdentityService(identities.toSet(), DEV_TRUST_ROOT) fun createService(vararg identities: PartyAndCertificate) = InMemoryIdentityService(identities.toSet(), DEV_TRUST_ROOT)
} }
@Rule
@JvmField
val testSerialization = SerializationEnvironmentRule()
@Test @Test
fun `get all identities`() { fun `get all identities`() {
val service = createService() val service = createService()
@ -94,18 +99,16 @@ class InMemoryIdentityServiceTests {
*/ */
@Test @Test
fun `assert unknown anonymous key is unrecognised`() { fun `assert unknown anonymous key is unrecognised`() {
withTestSerialization { val rootKey = Crypto.generateKeyPair(X509Utilities.DEFAULT_TLS_SIGNATURE_SCHEME)
val rootKey = Crypto.generateKeyPair(X509Utilities.DEFAULT_TLS_SIGNATURE_SCHEME) val rootCert = X509Utilities.createSelfSignedCACertificate(ALICE.name, rootKey)
val rootCert = X509Utilities.createSelfSignedCACertificate(ALICE.name, rootKey) val txKey = Crypto.generateKeyPair(X509Utilities.DEFAULT_TLS_SIGNATURE_SCHEME)
val txKey = Crypto.generateKeyPair(X509Utilities.DEFAULT_TLS_SIGNATURE_SCHEME) val service = createService()
val service = createService() // TODO: Generate certificate with an EdDSA key rather than ECDSA
// TODO: Generate certificate with an EdDSA key rather than ECDSA val identity = Party(rootCert.cert)
val identity = Party(rootCert.cert) val txIdentity = AnonymousParty(txKey.public)
val txIdentity = AnonymousParty(txKey.public)
assertFailsWith<UnknownAnonymousPartyException> { assertFailsWith<UnknownAnonymousPartyException> {
service.assertOwnership(identity, txIdentity) service.assertOwnership(identity, txIdentity)
}
} }
} }
@ -137,30 +140,28 @@ class InMemoryIdentityServiceTests {
*/ */
@Test @Test
fun `assert ownership`() { fun `assert ownership`() {
withTestSerialization { val (alice, anonymousAlice) = createConfidentialIdentity(ALICE.name)
val (alice, anonymousAlice) = createConfidentialIdentity(ALICE.name) val (bob, anonymousBob) = createConfidentialIdentity(BOB.name)
val (bob, anonymousBob) = createConfidentialIdentity(BOB.name)
// Now we have identities, construct the service and let it know about both // Now we have identities, construct the service and let it know about both
val service = createService(alice, bob) val service = createService(alice, bob)
service.verifyAndRegisterIdentity(anonymousAlice) service.verifyAndRegisterIdentity(anonymousAlice)
service.verifyAndRegisterIdentity(anonymousBob) service.verifyAndRegisterIdentity(anonymousBob)
// Verify that paths are verified // Verify that paths are verified
service.assertOwnership(alice.party, anonymousAlice.party.anonymise()) service.assertOwnership(alice.party, anonymousAlice.party.anonymise())
service.assertOwnership(bob.party, anonymousBob.party.anonymise()) service.assertOwnership(bob.party, anonymousBob.party.anonymise())
assertFailsWith<IllegalArgumentException> { assertFailsWith<IllegalArgumentException> {
service.assertOwnership(alice.party, anonymousBob.party.anonymise()) service.assertOwnership(alice.party, anonymousBob.party.anonymise())
} }
assertFailsWith<IllegalArgumentException> { assertFailsWith<IllegalArgumentException> {
service.assertOwnership(bob.party, anonymousAlice.party.anonymise()) service.assertOwnership(bob.party, anonymousAlice.party.anonymise())
} }
assertFailsWith<IllegalArgumentException> { assertFailsWith<IllegalArgumentException> {
val owningKey = Crypto.decodePublicKey(DEV_CA.certificate.subjectPublicKeyInfo.encoded) val owningKey = Crypto.decodePublicKey(DEV_CA.certificate.subjectPublicKeyInfo.encoded)
val subject = CordaX500Name.build(DEV_CA.certificate.cert.subjectX500Principal) val subject = CordaX500Name.build(DEV_CA.certificate.cert.subjectX500Principal)
service.assertOwnership(Party(subject, owningKey), anonymousAlice.party.anonymise()) service.assertOwnership(Party(subject, owningKey), anonymousAlice.party.anonymise())
}
} }
} }

View File

@ -21,6 +21,7 @@ import net.corda.testing.node.MockServices.Companion.makeTestDataSourcePropertie
import net.corda.testing.node.makeTestIdentityService import net.corda.testing.node.makeTestIdentityService
import org.junit.After import org.junit.After
import org.junit.Before import org.junit.Before
import org.junit.Rule
import org.junit.Test import org.junit.Test
import kotlin.test.assertEquals import kotlin.test.assertEquals
import kotlin.test.assertFailsWith import kotlin.test.assertFailsWith
@ -41,6 +42,9 @@ class PersistentIdentityServiceTests {
val BOB_PUBKEY get() = bob.publicKey val BOB_PUBKEY get() = bob.publicKey
} }
@Rule
@JvmField
val testSerialization = SerializationEnvironmentRule()
private lateinit var database: CordaPersistence private lateinit var database: CordaPersistence
private lateinit var identityService: IdentityService private lateinit var identityService: IdentityService
@ -138,17 +142,15 @@ class PersistentIdentityServiceTests {
*/ */
@Test @Test
fun `assert unknown anonymous key is unrecognised`() { fun `assert unknown anonymous key is unrecognised`() {
withTestSerialization { val rootKey = Crypto.generateKeyPair(X509Utilities.DEFAULT_TLS_SIGNATURE_SCHEME)
val rootKey = Crypto.generateKeyPair(X509Utilities.DEFAULT_TLS_SIGNATURE_SCHEME) val rootCert = X509Utilities.createSelfSignedCACertificate(ALICE.name, rootKey)
val rootCert = X509Utilities.createSelfSignedCACertificate(ALICE.name, rootKey) val txKey = Crypto.generateKeyPair(X509Utilities.DEFAULT_IDENTITY_SIGNATURE_SCHEME)
val txKey = Crypto.generateKeyPair(X509Utilities.DEFAULT_IDENTITY_SIGNATURE_SCHEME) val identity = Party(rootCert.cert)
val identity = Party(rootCert.cert) val txIdentity = AnonymousParty(txKey.public)
val txIdentity = AnonymousParty(txKey.public)
assertFailsWith<UnknownAnonymousPartyException> { assertFailsWith<UnknownAnonymousPartyException> {
database.transaction { database.transaction {
identityService.assertOwnership(identity, txIdentity) identityService.assertOwnership(identity, txIdentity)
}
} }
} }
} }
@ -191,38 +193,36 @@ class PersistentIdentityServiceTests {
*/ */
@Test @Test
fun `assert ownership`() { fun `assert ownership`() {
withTestSerialization { val (alice, anonymousAlice) = createConfidentialIdentity(ALICE.name)
val (alice, anonymousAlice) = createConfidentialIdentity(ALICE.name) val (bob, anonymousBob) = createConfidentialIdentity(BOB.name)
val (bob, anonymousBob) = createConfidentialIdentity(BOB.name)
database.transaction {
// Now we have identities, construct the service and let it know about both
identityService.verifyAndRegisterIdentity(anonymousAlice)
identityService.verifyAndRegisterIdentity(anonymousBob)
}
// Verify that paths are verified
database.transaction {
identityService.assertOwnership(alice.party, anonymousAlice.party.anonymise())
identityService.assertOwnership(bob.party, anonymousBob.party.anonymise())
}
assertFailsWith<IllegalArgumentException> {
database.transaction { database.transaction {
// Now we have identities, construct the service and let it know about both identityService.assertOwnership(alice.party, anonymousBob.party.anonymise())
identityService.verifyAndRegisterIdentity(anonymousAlice)
identityService.verifyAndRegisterIdentity(anonymousBob)
} }
}
// Verify that paths are verified assertFailsWith<IllegalArgumentException> {
database.transaction { database.transaction {
identityService.assertOwnership(alice.party, anonymousAlice.party.anonymise()) identityService.assertOwnership(bob.party, anonymousAlice.party.anonymise())
identityService.assertOwnership(bob.party, anonymousBob.party.anonymise())
}
assertFailsWith<IllegalArgumentException> {
database.transaction {
identityService.assertOwnership(alice.party, anonymousBob.party.anonymise())
}
}
assertFailsWith<IllegalArgumentException> {
database.transaction {
identityService.assertOwnership(bob.party, anonymousAlice.party.anonymise())
}
} }
}
assertFailsWith<IllegalArgumentException> { assertFailsWith<IllegalArgumentException> {
val owningKey = Crypto.decodePublicKey(DEV_CA.certificate.subjectPublicKeyInfo.encoded) val owningKey = Crypto.decodePublicKey(DEV_CA.certificate.subjectPublicKeyInfo.encoded)
database.transaction { database.transaction {
val subject = CordaX500Name.build(DEV_CA.certificate.cert.subjectX500Principal) val subject = CordaX500Name.build(DEV_CA.certificate.cert.subjectX500Principal)
identityService.assertOwnership(Party(subject, owningKey), anonymousAlice.party.anonymise()) identityService.assertOwnership(Party(subject, owningKey), anonymousAlice.party.anonymise())
}
} }
} }
} }

View File

@ -19,6 +19,7 @@ import net.corda.node.internal.configureDatabase
import net.corda.nodeapi.internal.persistence.CordaPersistence import net.corda.nodeapi.internal.persistence.CordaPersistence
import net.corda.nodeapi.internal.persistence.DatabaseConfig import net.corda.nodeapi.internal.persistence.DatabaseConfig
import net.corda.testing.* import net.corda.testing.*
import net.corda.testing.internal.withoutTestSerialization
import net.corda.testing.node.* import net.corda.testing.node.*
import net.corda.testing.node.MockServices.Companion.makeTestDataSourceProperties import net.corda.testing.node.MockServices.Companion.makeTestDataSourceProperties
import org.junit.After import org.junit.After

View File

@ -55,25 +55,6 @@ interface GlobalSerializationEnvironment : SerializationEnvironment {
fun unset() fun unset()
} }
/** @param inheritable whether new threads inherit the environment, use sparingly. */
fun <T> withTestSerialization(inheritable: Boolean = false, callable: (SerializationEnvironment) -> T): T {
return createTestSerializationEnv("<context>").asContextEnv(inheritable, callable)
}
/**
* For example your test class uses [SerializationEnvironmentRule] but you want to turn it off for one method.
* Use sparingly, ideally a test class shouldn't mix serializers init mechanisms.
*/
fun <T> withoutTestSerialization(callable: () -> T): T {
val (property, env) = listOf(_contextSerializationEnv, _inheritableContextSerializationEnv).map { Pair(it, it.get()) }.single { it.second != null }
property.set(null)
try {
return callable()
} finally {
property.set(env)
}
}
/** /**
* Should only be used by Driver and MockNode. * Should only be used by Driver and MockNode.
* @param armed true to install, false to do nothing and return a dummy env. * @param armed true to install, false to do nothing and return a dummy env.

View File

@ -0,0 +1,19 @@
package net.corda.testing.internal
import net.corda.core.serialization.internal._contextSerializationEnv
import net.corda.core.serialization.internal._inheritableContextSerializationEnv
import net.corda.testing.SerializationEnvironmentRule
/**
* For example your test class uses [SerializationEnvironmentRule] but you want to turn it off for one method.
* Use sparingly, ideally a test class shouldn't mix serializers init mechanisms.
*/
fun <T> withoutTestSerialization(callable: () -> T): T { // TODO: Delete this, see CORDA-858.
val (property, env) = listOf(_contextSerializationEnv, _inheritableContextSerializationEnv).map { Pair(it, it.get()) }.single { it.second != null }
property.set(null)
try {
return callable()
} finally {
property.set(env)
}
}