Pass some key constants into MockServices. (#2173)

This commit is contained in:
Andrzej Cichocki
2017-12-05 14:50:56 +00:00
committed by GitHub
parent 9adf4bfc57
commit e4d76204c1
14 changed files with 72 additions and 75 deletions

View File

@ -13,6 +13,7 @@ import net.corda.node.services.identity.InMemoryIdentityService
import net.corda.nodeapi.NodeInfoFilesCopier
import net.corda.testing.*
import net.corda.testing.node.MockKeyManagementService
import net.corda.testing.node.makeTestIdentityService
import org.assertj.core.api.Assertions.assertThat
import org.assertj.core.api.Assertions.contentOf
import org.junit.Before
@ -47,7 +48,7 @@ class NodeInfoWatcherTest {
@Before
fun start() {
val identityService = InMemoryIdentityService(trustRoot = DEV_TRUST_ROOT)
val identityService = makeTestIdentityService()
keyManagementService = MockKeyManagementService(identityService, ALICE_KEY)
nodeInfoWatcher = NodeInfoWatcher(tempFolder.root.toPath(), scheduler)
nodeInfoPath = tempFolder.root.toPath() / CordformNode.NODE_INFO_DIRECTORY

View File

@ -24,14 +24,8 @@ import javax.annotation.concurrent.ThreadSafe
* @param identities initial set of identities for the service, typically only used for unit tests.
*/
@ThreadSafe
class InMemoryIdentityService(identities: Iterable<PartyAndCertificate> = emptySet(),
confidentialIdentities: Iterable<PartyAndCertificate> = emptySet(),
override val trustRoot: X509Certificate,
vararg caCertificates: X509Certificate) : SingletonSerializeAsToken(), IdentityService {
constructor(wellKnownIdentities: Iterable<PartyAndCertificate> = emptySet(),
confidentialIdentities: Iterable<PartyAndCertificate> = emptySet(),
trustRoot: X509CertificateHolder) : this(wellKnownIdentities, confidentialIdentities, trustRoot.cert)
class InMemoryIdentityService(identities: Iterable<PartyAndCertificate>,
trustRoot: X509CertificateHolder) : SingletonSerializeAsToken(), IdentityService {
companion object {
private val log = contextLogger()
}
@ -40,18 +34,15 @@ class InMemoryIdentityService(identities: Iterable<PartyAndCertificate> = emptyS
* Certificate store for certificate authority and intermediary certificates.
*/
override val caCertStore: CertStore
override val trustAnchor: TrustAnchor = TrustAnchor(trustRoot, null)
override val trustRoot = trustRoot.cert
override val trustAnchor: TrustAnchor = TrustAnchor(this.trustRoot, null)
private val keyToParties = ConcurrentHashMap<PublicKey, PartyAndCertificate>()
private val principalToParties = ConcurrentHashMap<CordaX500Name, PartyAndCertificate>()
init {
val caCertificatesWithRoot: Set<X509Certificate> = caCertificates.toSet() + trustRoot
caCertStore = CertStore.getInstance("Collection", CollectionCertStoreParameters(caCertificatesWithRoot))
caCertStore = CertStore.getInstance("Collection", CollectionCertStoreParameters(setOf(this.trustRoot)))
keyToParties.putAll(identities.associateBy { it.owningKey })
principalToParties.putAll(identities.associateBy { it.name })
confidentialIdentities.forEach { identity ->
principalToParties.computeIfAbsent(identity.name) { identity }
}
}
// TODO: Check the certificate validation logic

View File

@ -19,7 +19,6 @@ import net.corda.core.utilities.OpaqueBytes;
import net.corda.finance.contracts.DealState;
import net.corda.finance.contracts.asset.Cash;
import net.corda.finance.schemas.CashSchemaV1;
import net.corda.node.services.identity.InMemoryIdentityService;
import net.corda.nodeapi.internal.persistence.CordaPersistence;
import net.corda.nodeapi.internal.persistence.DatabaseTransaction;
import net.corda.testing.SerializationEnvironmentRule;
@ -48,6 +47,7 @@ import static net.corda.finance.contracts.asset.CashUtilities.*;
import static net.corda.testing.CoreTestUtils.*;
import static net.corda.testing.TestConstants.*;
import static net.corda.testing.node.MockServices.makeTestDatabaseAndMockServices;
import static net.corda.testing.node.MockServicesKt.makeTestIdentityService;
import static org.assertj.core.api.Assertions.assertThat;
public class VaultQueryJavaTests {
@ -61,14 +61,12 @@ public class VaultQueryJavaTests {
@Before
public void setUp() throws CertificateException, InvalidAlgorithmParameterException {
List<String> cordappPackages = Arrays.asList("net.corda.testing.contracts", "net.corda.finance.contracts.asset", CashSchemaV1.class.getPackage().getName());
IdentityService identitySvc = new InMemoryIdentityService(
Arrays.asList(getMEGA_CORP_IDENTITY(), getDUMMY_CASH_ISSUER_IDENTITY(), getDUMMY_NOTARY_IDENTITY()),
Collections.emptySet(),
getDEV_TRUST_ROOT());
IdentityService identitySvc = makeTestIdentityService(Arrays.asList(getMEGA_CORP_IDENTITY(), getDUMMY_CASH_ISSUER_IDENTITY(), getDUMMY_NOTARY_IDENTITY()));
Pair<CordaPersistence, MockServices> databaseAndServices = makeTestDatabaseAndMockServices(
Arrays.asList(getMEGA_CORP_KEY(), getDUMMY_NOTARY_KEY()),
identitySvc,
cordappPackages);
cordappPackages,
getMEGA_CORP().getName());
issuerServices = new MockServices(cordappPackages, getDUMMY_CASH_ISSUER_NAME(), getDUMMY_CASH_ISSUER_KEY(), getBOC_KEY());
database = databaseAndServices.getFirst();
MockServices services = databaseAndServices.getSecond();

View File

@ -11,13 +11,12 @@ import net.corda.core.internal.concurrent.openFuture
import net.corda.core.messaging.FlowProgressHandleImpl
import net.corda.core.utilities.ProgressTracker
import net.corda.nodeapi.internal.persistence.DatabaseConfig
import net.corda.node.services.identity.InMemoryIdentityService
import net.corda.node.shell.InteractiveShell
import net.corda.node.internal.configureDatabase
import net.corda.testing.DEV_TRUST_ROOT
import net.corda.testing.MEGA_CORP
import net.corda.testing.MEGA_CORP_IDENTITY
import net.corda.testing.node.MockServices
import net.corda.testing.node.makeTestIdentityService
import net.corda.testing.rigorousMock
import org.junit.After
import org.junit.Before
@ -49,7 +48,7 @@ class InteractiveShellTest {
override fun call() = a
}
private val ids = InMemoryIdentityService(listOf(MEGA_CORP_IDENTITY), trustRoot = DEV_TRUST_ROOT)
private val ids = makeTestIdentityService(listOf(MEGA_CORP_IDENTITY))
private val om = JacksonSupport.createInMemoryMapper(ids, YAMLFactory())
private fun check(input: String, expected: String) {

View File

@ -91,7 +91,7 @@ class NodeSchedulerServiceTest : SingletonSerializeAsToken() {
calls = 0
val dataSourceProps = makeTestDataSourceProperties()
database = configureDatabase(dataSourceProps, DatabaseConfig(), rigorousMock())
val identityService = InMemoryIdentityService(trustRoot = DEV_TRUST_ROOT)
val identityService = makeTestIdentityService()
kms = MockKeyManagementService(identityService, ALICE_KEY)
val configuration = testNodeConfiguration(Paths.get("."), CordaX500Name("Alice", "London", "GB"))
val validatedTransactions = MockTransactionStorage()

View File

@ -23,9 +23,13 @@ import kotlin.test.assertNull
* Tests for the in memory identity service.
*/
class InMemoryIdentityServiceTests {
companion object {
private fun createService(vararg identities: PartyAndCertificate) = InMemoryIdentityService(identities.toSet(), DEV_TRUST_ROOT)
}
@Test
fun `get all identities`() {
val service = InMemoryIdentityService(trustRoot = DEV_TRUST_ROOT)
val service = createService()
// Nothing registered, so empty set
assertNull(service.getAllIdentities().firstOrNull())
@ -43,7 +47,7 @@ class InMemoryIdentityServiceTests {
@Test
fun `get identity by key`() {
val service = InMemoryIdentityService(trustRoot = DEV_TRUST_ROOT)
val service = createService()
assertNull(service.partyFromKey(ALICE_PUBKEY))
service.verifyAndRegisterIdentity(ALICE_IDENTITY)
assertEquals(ALICE, service.partyFromKey(ALICE_PUBKEY))
@ -52,13 +56,13 @@ class InMemoryIdentityServiceTests {
@Test
fun `get identity by name with no registered identities`() {
val service = InMemoryIdentityService(trustRoot = DEV_TRUST_ROOT)
val service = createService()
assertNull(service.wellKnownPartyFromX500Name(ALICE.name))
}
@Test
fun `get identity by substring match`() {
val service = InMemoryIdentityService(trustRoot = DEV_TRUST_ROOT)
val service = createService()
service.verifyAndRegisterIdentity(ALICE_IDENTITY)
service.verifyAndRegisterIdentity(BOB_IDENTITY)
val alicente = getTestPartyAndCertificate(CordaX500Name(organisation = "Alicente Worldwide", locality = "London", country = "GB"), generateKeyPair().public)
@ -70,7 +74,7 @@ class InMemoryIdentityServiceTests {
@Test
fun `get identity by name`() {
val service = InMemoryIdentityService(trustRoot = DEV_TRUST_ROOT)
val service = createService()
val identities = listOf("Org A", "Org B", "Org C")
.map { getTestPartyAndCertificate(CordaX500Name(organisation = it, locality = "London", country = "GB"), generateKeyPair().public) }
assertNull(service.wellKnownPartyFromX500Name(identities.first().name))
@ -87,7 +91,7 @@ class InMemoryIdentityServiceTests {
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 = InMemoryIdentityService(trustRoot = DEV_TRUST_ROOT)
val service = createService()
// TODO: Generate certificate with an EdDSA key rather than ECDSA
val identity = Party(rootCert.cert)
val txIdentity = AnonymousParty(txKey.public)
@ -108,7 +112,7 @@ class InMemoryIdentityServiceTests {
val (_, bobTxIdentity) = createParty(ALICE.name, DEV_CA)
// Now we have identities, construct the service and let it know about both
val service = InMemoryIdentityService(setOf(alice), emptySet(), DEV_TRUST_ROOT)
val service = createService(alice)
service.verifyAndRegisterIdentity(aliceTxIdentity)
var actual = service.certificateFromKey(aliceTxIdentity.party.owningKey)
@ -131,8 +135,7 @@ class InMemoryIdentityServiceTests {
val (bob, anonymousBob) = createParty(BOB.name, DEV_CA)
// Now we have identities, construct the service and let it know about both
val service = InMemoryIdentityService(setOf(alice, bob), emptySet(), DEV_TRUST_ROOT)
val service = createService(alice, bob)
service.verifyAndRegisterIdentity(anonymousAlice)
service.verifyAndRegisterIdentity(anonymousBob)
@ -168,7 +171,7 @@ class InMemoryIdentityServiceTests {
*/
@Test
fun `deanonymising a well known identity should return the identity`() {
val service = InMemoryIdentityService(trustRoot = DEV_TRUST_ROOT)
val service = createService()
val expected = ALICE
service.verifyAndRegisterIdentity(ALICE_IDENTITY)
val actual = service.wellKnownPartyFromAnonymous(expected)
@ -180,7 +183,7 @@ class InMemoryIdentityServiceTests {
*/
@Test
fun `deanonymising a false well known identity should return null`() {
val service = InMemoryIdentityService(trustRoot = DEV_TRUST_ROOT)
val service = createService()
val notAlice = Party(ALICE.name, generateKeyPair().public)
service.verifyAndRegisterIdentity(ALICE_IDENTITY)
val actual = service.wellKnownPartyFromAnonymous(notAlice)

View File

@ -17,6 +17,7 @@ import net.corda.nodeapi.internal.crypto.X509Utilities
import net.corda.nodeapi.internal.persistence.CordaPersistence
import net.corda.testing.*
import net.corda.testing.node.MockServices
import net.corda.testing.node.makeTestIdentityService
import org.junit.After
import org.junit.Before
import org.junit.Test
@ -34,7 +35,7 @@ class PersistentIdentityServiceTests {
@Before
fun setup() {
val databaseAndServices = MockServices.makeTestDatabaseAndMockServices(keys = emptyList(), identityService = PersistentIdentityService(DEV_TRUST_ROOT))
val databaseAndServices = MockServices.makeTestDatabaseAndMockServices(emptyList(), PersistentIdentityService(DEV_TRUST_ROOT), initialIdentityName = MEGA_CORP.name)
database = databaseAndServices.first
services = databaseAndServices.second
identityService = services.identityService
@ -266,7 +267,7 @@ class PersistentIdentityServiceTests {
*/
@Test
fun `deanonymising a well known identity should return the identity`() {
val service = InMemoryIdentityService(trustRoot = DEV_TRUST_ROOT)
val service = makeTestIdentityService()
val expected = ALICE
service.verifyAndRegisterIdentity(ALICE_IDENTITY)
val actual = service.wellKnownPartyFromAnonymous(expected)
@ -278,7 +279,7 @@ class PersistentIdentityServiceTests {
*/
@Test
fun `deanonymising a false well known identity should return null`() {
val service = InMemoryIdentityService(trustRoot = DEV_TRUST_ROOT)
val service = makeTestIdentityService()
val notAlice = Party(ALICE.name, generateKeyPair().public)
service.verifyAndRegisterIdentity(ALICE_IDENTITY)
val actual = service.wellKnownPartyFromAnonymous(notAlice)

View File

@ -38,6 +38,7 @@ import net.corda.nodeapi.internal.persistence.CordaPersistence
import net.corda.testing.*
import net.corda.testing.contracts.VaultFiller
import net.corda.testing.node.MockServices
import net.corda.testing.node.makeTestIdentityService
import org.assertj.core.api.Assertions.assertThat
import org.assertj.core.api.Assertions.assertThatExceptionOfType
import org.junit.After
@ -72,7 +73,11 @@ class NodeVaultServiceTest {
@Before
fun setUp() {
LogHelper.setLevel(NodeVaultService::class)
val databaseAndServices = MockServices.makeTestDatabaseAndMockServices(cordappPackages = cordappPackages)
val databaseAndServices = MockServices.makeTestDatabaseAndMockServices(
listOf(MEGA_CORP_KEY),
makeTestIdentityService(listOf(MEGA_CORP_IDENTITY, MINI_CORP_IDENTITY, DUMMY_CASH_ISSUER_IDENTITY, DUMMY_NOTARY_IDENTITY)),
cordappPackages,
MEGA_CORP.name)
database = databaseAndServices.first
services = databaseAndServices.second
vaultFiller = VaultFiller(services, DUMMY_NOTARY, DUMMY_NOTARY_KEY)

View File

@ -35,6 +35,7 @@ import net.corda.testing.*
import net.corda.testing.contracts.*
import net.corda.testing.node.MockServices
import net.corda.testing.node.MockServices.Companion.makeTestDatabaseAndMockServices
import net.corda.testing.node.makeTestIdentityService
import net.corda.testing.schemas.DummyLinearStateSchemaV1
import org.assertj.core.api.Assertions.assertThat
import org.assertj.core.api.Assertions.assertThatThrownBy
@ -79,8 +80,11 @@ class VaultQueryTests {
@Before
fun setUp() {
// register additional identities
val databaseAndServices = makeTestDatabaseAndMockServices(keys = listOf(MEGA_CORP_KEY, DUMMY_NOTARY_KEY),
cordappPackages = cordappPackages)
val databaseAndServices = makeTestDatabaseAndMockServices(
listOf(MEGA_CORP_KEY, DUMMY_NOTARY_KEY),
makeTestIdentityService(listOf(MEGA_CORP_IDENTITY, MINI_CORP_IDENTITY, DUMMY_CASH_ISSUER_IDENTITY, DUMMY_NOTARY_IDENTITY)),
cordappPackages,
MEGA_CORP.name)
database = databaseAndServices.first
services = databaseAndServices.second
vaultFiller = VaultFiller(services, DUMMY_NOTARY, DUMMY_NOTARY_KEY)

View File

@ -29,6 +29,7 @@ import net.corda.testing.*
import net.corda.testing.contracts.*
import net.corda.testing.node.MockServices
import net.corda.testing.node.MockServices.Companion.makeTestDatabaseAndMockServices
import net.corda.testing.node.makeTestIdentityService
import org.assertj.core.api.Assertions.assertThat
import org.assertj.core.api.Assertions.assertThatThrownBy
import org.junit.After
@ -60,7 +61,11 @@ class VaultWithCashTest {
@Before
fun setUp() {
LogHelper.setLevel(VaultWithCashTest::class)
val databaseAndServices = makeTestDatabaseAndMockServices(cordappPackages = cordappPackages, keys = listOf(generateKeyPair(), DUMMY_NOTARY_KEY))
val databaseAndServices = makeTestDatabaseAndMockServices(
listOf(generateKeyPair(), DUMMY_NOTARY_KEY),
makeTestIdentityService(listOf(MEGA_CORP_IDENTITY, MINI_CORP_IDENTITY, DUMMY_CASH_ISSUER_IDENTITY, DUMMY_NOTARY_IDENTITY)),
cordappPackages,
MEGA_CORP.name)
database = databaseAndServices.first
services = databaseAndServices.second
vaultFiller = VaultFiller(services, DUMMY_NOTARY, DUMMY_NOTARY_KEY)