From 05bc05a936c1bf7e7fdb5df67613b586ac726abc Mon Sep 17 00:00:00 2001 From: Andrzej Cichocki Date: Thu, 14 Dec 2017 12:42:18 +0000 Subject: [PATCH] CORDA-716 Retire withTestSerialization (#2240) --- .../corda/core/utilities/NonEmptySetTest.kt | 15 ++-- .../amqp/SerializationOutputTests.kt | 9 ++- .../node/services/AttachmentLoadingTests.kt | 14 ++-- .../identity/InMemoryIdentityServiceTests.kt | 65 ++++++++--------- .../PersistentIdentityServiceTests.kt | 72 +++++++++---------- .../corda/irs/api/NodeInterestRatesTest.kt | 1 + .../corda/testing/SerializationTestHelpers.kt | 19 ----- .../InternalSerializationTestHelpers.kt | 19 +++++ 8 files changed, 115 insertions(+), 99 deletions(-) create mode 100644 testing/test-utils/src/main/kotlin/net/corda/testing/internal/InternalSerializationTestHelpers.kt diff --git a/core/src/test/kotlin/net/corda/core/utilities/NonEmptySetTest.kt b/core/src/test/kotlin/net/corda/core/utilities/NonEmptySetTest.kt index 8d7cb3e12f..0a26639ae4 100644 --- a/core/src/test/kotlin/net/corda/core/utilities/NonEmptySetTest.kt +++ b/core/src/test/kotlin/net/corda/core/utilities/NonEmptySetTest.kt @@ -7,9 +7,10 @@ import com.google.common.collect.testing.features.CollectionSize import junit.framework.TestSuite import net.corda.core.serialization.deserialize 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.assertThatThrownBy +import org.junit.Rule import org.junit.Test import org.junit.runner.RunWith import org.junit.runners.Suite @@ -36,6 +37,10 @@ class NonEmptySetTest { } class General { + @Rule + @JvmField + val testSerialization = SerializationEnvironmentRule() + @Test fun `copyOf - empty source`() { assertThatThrownBy { NonEmptySet.copyOf(HashSet()) }.isInstanceOf(IllegalArgumentException::class.java) @@ -48,11 +53,9 @@ class NonEmptySetTest { @Test fun `serialize deserialize`() { - withTestSerialization { - val original = NonEmptySet.of(-17, 22, 17) - val copy = original.serialize().deserialize() - assertThat(copy).isEqualTo(original).isNotSameAs(original) - } + val original = NonEmptySet.of(-17, 22, 17) + val copy = original.serialize().deserialize() + assertThat(copy).isEqualTo(original).isNotSameAs(original) } } diff --git a/node-api/src/test/kotlin/net/corda/nodeapi/internal/serialization/amqp/SerializationOutputTests.kt b/node-api/src/test/kotlin/net/corda/nodeapi/internal/serialization/amqp/SerializationOutputTests.kt index a3af4c26ce..9871bff1c6 100644 --- a/node-api/src/test/kotlin/net/corda/nodeapi/internal/serialization/amqp/SerializationOutputTests.kt +++ b/node-api/src/test/kotlin/net/corda/nodeapi/internal/serialization/amqp/SerializationOutputTests.kt @@ -29,6 +29,7 @@ import org.apache.qpid.proton.codec.EncoderImpl import org.assertj.core.api.Assertions.* import org.junit.Assert.* import org.junit.Ignore +import org.junit.Rule import org.junit.Test import java.io.ByteArrayInputStream import java.io.IOException @@ -54,6 +55,10 @@ class SerializationOutputTests { val MINI_CORP_PUBKEY get() = miniCorp.publicKey } + @Rule + @JvmField + val testSerialization = SerializationEnvironmentRule() + data class Foo(val bar: String, val pub: Int) data class testFloat(val f: Float) @@ -473,9 +478,9 @@ class SerializationOutputTests { 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) - SerializationFactory.defaultFactory.asCurrent { withCurrentContext(newContext) { serdes(t, factory, factory2, expectedEqual) } } + return SerializationFactory.defaultFactory.asCurrent { withCurrentContext(newContext) { serdes(t, factory, factory2, expectedEqual) } } } @Test diff --git a/node/src/integration-test/kotlin/net/corda/node/services/AttachmentLoadingTests.kt b/node/src/integration-test/kotlin/net/corda/node/services/AttachmentLoadingTests.kt index 2fc3f11770..60e781e9c4 100644 --- a/node/src/integration-test/kotlin/net/corda/node/services/AttachmentLoadingTests.kt +++ b/node/src/integration-test/kotlin/net/corda/node/services/AttachmentLoadingTests.kt @@ -24,16 +24,20 @@ import net.corda.testing.* import net.corda.testing.driver.DriverDSL import net.corda.testing.driver.NodeHandle import net.corda.testing.driver.driver +import net.corda.testing.internal.withoutTestSerialization import net.corda.testing.services.MockAttachmentStorage import net.corda.testing.rigorousMock -import net.corda.testing.withTestSerialization import org.junit.Assert.assertEquals +import org.junit.Rule import org.junit.Test import java.net.URLClassLoader import java.nio.file.Files import kotlin.test.assertFailsWith class AttachmentLoadingTests { + @Rule + @JvmField + val testSerialization = SerializationEnvironmentRule() private val attachments = MockAttachmentStorage() private val provider = CordappProviderImpl(CordappLoader.createDevMode(listOf(isolatedJAR)), attachments) private val cordapp get() = provider.cordapps.first() @@ -80,7 +84,7 @@ class AttachmentLoadingTests { } @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 contractClass = appClassLoader.loadClass(ISOLATED_CONTRACT_ID).asSubclass(Contract::class.java) val generateInitialMethod = contractClass.getDeclaredMethod("generateInitial", PartyAndReference::class.java, Integer.TYPE, Party::class.java) @@ -96,7 +100,7 @@ class AttachmentLoadingTests { } @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 { installIsolatedCordappTo(bankAName) val (bankA, bankB) = createTwoNodes() @@ -104,15 +108,17 @@ class AttachmentLoadingTests { bankA.rpc.startFlowDynamic(flowInitiatorClass, bankB.nodeInfo.legalIdentities.first()).returnValue.getOrThrow() } } + Unit } @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 { installIsolatedCordappTo(bankAName) installIsolatedCordappTo(bankBName) val (bankA, bankB) = createTwoNodes() bankA.rpc.startFlowDynamic(flowInitiatorClass, bankB.nodeInfo.legalIdentities.first()).returnValue.getOrThrow() } + Unit } } diff --git a/node/src/test/kotlin/net/corda/node/services/identity/InMemoryIdentityServiceTests.kt b/node/src/test/kotlin/net/corda/node/services/identity/InMemoryIdentityServiceTests.kt index d7c4fa8bbc..21c9330758 100644 --- a/node/src/test/kotlin/net/corda/node/services/identity/InMemoryIdentityServiceTests.kt +++ b/node/src/test/kotlin/net/corda/node/services/identity/InMemoryIdentityServiceTests.kt @@ -13,6 +13,7 @@ import net.corda.nodeapi.internal.crypto.CertificateType import net.corda.nodeapi.internal.crypto.X509CertificateFactory import net.corda.nodeapi.internal.crypto.X509Utilities import net.corda.testing.* +import org.junit.Rule import org.junit.Test import kotlin.test.assertEquals import kotlin.test.assertFailsWith @@ -34,6 +35,10 @@ class InMemoryIdentityServiceTests { fun createService(vararg identities: PartyAndCertificate) = InMemoryIdentityService(identities.toSet(), DEV_TRUST_ROOT) } + @Rule + @JvmField + val testSerialization = SerializationEnvironmentRule() + @Test fun `get all identities`() { val service = createService() @@ -94,18 +99,16 @@ class InMemoryIdentityServiceTests { */ @Test fun `assert unknown anonymous key is unrecognised`() { - withTestSerialization { - val rootKey = Crypto.generateKeyPair(X509Utilities.DEFAULT_TLS_SIGNATURE_SCHEME) - val rootCert = X509Utilities.createSelfSignedCACertificate(ALICE.name, rootKey) - val txKey = Crypto.generateKeyPair(X509Utilities.DEFAULT_TLS_SIGNATURE_SCHEME) - val service = createService() - // TODO: Generate certificate with an EdDSA key rather than ECDSA - val identity = Party(rootCert.cert) - val txIdentity = AnonymousParty(txKey.public) + val rootKey = Crypto.generateKeyPair(X509Utilities.DEFAULT_TLS_SIGNATURE_SCHEME) + val rootCert = X509Utilities.createSelfSignedCACertificate(ALICE.name, rootKey) + val txKey = Crypto.generateKeyPair(X509Utilities.DEFAULT_TLS_SIGNATURE_SCHEME) + val service = createService() + // TODO: Generate certificate with an EdDSA key rather than ECDSA + val identity = Party(rootCert.cert) + val txIdentity = AnonymousParty(txKey.public) - assertFailsWith { - service.assertOwnership(identity, txIdentity) - } + assertFailsWith { + service.assertOwnership(identity, txIdentity) } } @@ -137,30 +140,28 @@ class InMemoryIdentityServiceTests { */ @Test fun `assert ownership`() { - withTestSerialization { - val (alice, anonymousAlice) = createConfidentialIdentity(ALICE.name) - val (bob, anonymousBob) = createConfidentialIdentity(BOB.name) + val (alice, anonymousAlice) = createConfidentialIdentity(ALICE.name) + val (bob, anonymousBob) = createConfidentialIdentity(BOB.name) - // Now we have identities, construct the service and let it know about both - val service = createService(alice, bob) - service.verifyAndRegisterIdentity(anonymousAlice) - service.verifyAndRegisterIdentity(anonymousBob) + // Now we have identities, construct the service and let it know about both + val service = createService(alice, bob) + service.verifyAndRegisterIdentity(anonymousAlice) + service.verifyAndRegisterIdentity(anonymousBob) - // Verify that paths are verified - service.assertOwnership(alice.party, anonymousAlice.party.anonymise()) - service.assertOwnership(bob.party, anonymousBob.party.anonymise()) - assertFailsWith { - service.assertOwnership(alice.party, anonymousBob.party.anonymise()) - } - assertFailsWith { - service.assertOwnership(bob.party, anonymousAlice.party.anonymise()) - } + // Verify that paths are verified + service.assertOwnership(alice.party, anonymousAlice.party.anonymise()) + service.assertOwnership(bob.party, anonymousBob.party.anonymise()) + assertFailsWith { + service.assertOwnership(alice.party, anonymousBob.party.anonymise()) + } + assertFailsWith { + service.assertOwnership(bob.party, anonymousAlice.party.anonymise()) + } - assertFailsWith { - val owningKey = Crypto.decodePublicKey(DEV_CA.certificate.subjectPublicKeyInfo.encoded) - val subject = CordaX500Name.build(DEV_CA.certificate.cert.subjectX500Principal) - service.assertOwnership(Party(subject, owningKey), anonymousAlice.party.anonymise()) - } + assertFailsWith { + val owningKey = Crypto.decodePublicKey(DEV_CA.certificate.subjectPublicKeyInfo.encoded) + val subject = CordaX500Name.build(DEV_CA.certificate.cert.subjectX500Principal) + service.assertOwnership(Party(subject, owningKey), anonymousAlice.party.anonymise()) } } diff --git a/node/src/test/kotlin/net/corda/node/services/identity/PersistentIdentityServiceTests.kt b/node/src/test/kotlin/net/corda/node/services/identity/PersistentIdentityServiceTests.kt index 46dc9b5f50..956bbbb4cf 100644 --- a/node/src/test/kotlin/net/corda/node/services/identity/PersistentIdentityServiceTests.kt +++ b/node/src/test/kotlin/net/corda/node/services/identity/PersistentIdentityServiceTests.kt @@ -21,6 +21,7 @@ import net.corda.testing.node.MockServices.Companion.makeTestDataSourcePropertie import net.corda.testing.node.makeTestIdentityService import org.junit.After import org.junit.Before +import org.junit.Rule import org.junit.Test import kotlin.test.assertEquals import kotlin.test.assertFailsWith @@ -41,6 +42,9 @@ class PersistentIdentityServiceTests { val BOB_PUBKEY get() = bob.publicKey } + @Rule + @JvmField + val testSerialization = SerializationEnvironmentRule() private lateinit var database: CordaPersistence private lateinit var identityService: IdentityService @@ -138,17 +142,15 @@ class PersistentIdentityServiceTests { */ @Test fun `assert unknown anonymous key is unrecognised`() { - withTestSerialization { - val rootKey = Crypto.generateKeyPair(X509Utilities.DEFAULT_TLS_SIGNATURE_SCHEME) - val rootCert = X509Utilities.createSelfSignedCACertificate(ALICE.name, rootKey) - val txKey = Crypto.generateKeyPair(X509Utilities.DEFAULT_IDENTITY_SIGNATURE_SCHEME) - val identity = Party(rootCert.cert) - val txIdentity = AnonymousParty(txKey.public) + val rootKey = Crypto.generateKeyPair(X509Utilities.DEFAULT_TLS_SIGNATURE_SCHEME) + val rootCert = X509Utilities.createSelfSignedCACertificate(ALICE.name, rootKey) + val txKey = Crypto.generateKeyPair(X509Utilities.DEFAULT_IDENTITY_SIGNATURE_SCHEME) + val identity = Party(rootCert.cert) + val txIdentity = AnonymousParty(txKey.public) - assertFailsWith { - database.transaction { - identityService.assertOwnership(identity, txIdentity) - } + assertFailsWith { + database.transaction { + identityService.assertOwnership(identity, txIdentity) } } } @@ -191,38 +193,36 @@ class PersistentIdentityServiceTests { */ @Test fun `assert ownership`() { - withTestSerialization { - val (alice, anonymousAlice) = createConfidentialIdentity(ALICE.name) - val (bob, anonymousBob) = createConfidentialIdentity(BOB.name) + val (alice, anonymousAlice) = createConfidentialIdentity(ALICE.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 { database.transaction { - // Now we have identities, construct the service and let it know about both - identityService.verifyAndRegisterIdentity(anonymousAlice) - identityService.verifyAndRegisterIdentity(anonymousBob) + identityService.assertOwnership(alice.party, anonymousBob.party.anonymise()) } - - // Verify that paths are verified + } + assertFailsWith { database.transaction { - identityService.assertOwnership(alice.party, anonymousAlice.party.anonymise()) - identityService.assertOwnership(bob.party, anonymousBob.party.anonymise()) - } - assertFailsWith { - database.transaction { - identityService.assertOwnership(alice.party, anonymousBob.party.anonymise()) - } - } - assertFailsWith { - database.transaction { - identityService.assertOwnership(bob.party, anonymousAlice.party.anonymise()) - } + identityService.assertOwnership(bob.party, anonymousAlice.party.anonymise()) } + } - assertFailsWith { - val owningKey = Crypto.decodePublicKey(DEV_CA.certificate.subjectPublicKeyInfo.encoded) - database.transaction { - val subject = CordaX500Name.build(DEV_CA.certificate.cert.subjectX500Principal) - identityService.assertOwnership(Party(subject, owningKey), anonymousAlice.party.anonymise()) - } + assertFailsWith { + val owningKey = Crypto.decodePublicKey(DEV_CA.certificate.subjectPublicKeyInfo.encoded) + database.transaction { + val subject = CordaX500Name.build(DEV_CA.certificate.cert.subjectX500Principal) + identityService.assertOwnership(Party(subject, owningKey), anonymousAlice.party.anonymise()) } } } diff --git a/samples/irs-demo/cordapp/src/test/kotlin/net/corda/irs/api/NodeInterestRatesTest.kt b/samples/irs-demo/cordapp/src/test/kotlin/net/corda/irs/api/NodeInterestRatesTest.kt index 3c8b0192f3..019b91c128 100644 --- a/samples/irs-demo/cordapp/src/test/kotlin/net/corda/irs/api/NodeInterestRatesTest.kt +++ b/samples/irs-demo/cordapp/src/test/kotlin/net/corda/irs/api/NodeInterestRatesTest.kt @@ -19,6 +19,7 @@ import net.corda.node.internal.configureDatabase import net.corda.nodeapi.internal.persistence.CordaPersistence import net.corda.nodeapi.internal.persistence.DatabaseConfig import net.corda.testing.* +import net.corda.testing.internal.withoutTestSerialization import net.corda.testing.node.* import net.corda.testing.node.MockServices.Companion.makeTestDataSourceProperties import org.junit.After diff --git a/testing/test-utils/src/main/kotlin/net/corda/testing/SerializationTestHelpers.kt b/testing/test-utils/src/main/kotlin/net/corda/testing/SerializationTestHelpers.kt index 608db48d82..675e82eda0 100644 --- a/testing/test-utils/src/main/kotlin/net/corda/testing/SerializationTestHelpers.kt +++ b/testing/test-utils/src/main/kotlin/net/corda/testing/SerializationTestHelpers.kt @@ -55,25 +55,6 @@ interface GlobalSerializationEnvironment : SerializationEnvironment { fun unset() } -/** @param inheritable whether new threads inherit the environment, use sparingly. */ -fun withTestSerialization(inheritable: Boolean = false, callable: (SerializationEnvironment) -> T): T { - return createTestSerializationEnv("").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 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. * @param armed true to install, false to do nothing and return a dummy env. diff --git a/testing/test-utils/src/main/kotlin/net/corda/testing/internal/InternalSerializationTestHelpers.kt b/testing/test-utils/src/main/kotlin/net/corda/testing/internal/InternalSerializationTestHelpers.kt new file mode 100644 index 0000000000..659c889087 --- /dev/null +++ b/testing/test-utils/src/main/kotlin/net/corda/testing/internal/InternalSerializationTestHelpers.kt @@ -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 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) + } +}