|
| 1 | +package org.hyperledger.ariesframework.connectionless |
| 2 | + |
| 3 | +import androidx.test.filters.LargeTest |
| 4 | +import androidx.test.platform.app.InstrumentationRegistry |
| 5 | +import kotlinx.coroutines.test.runTest |
| 6 | +import org.hyperledger.ariesframework.TestHelper |
| 7 | +import org.hyperledger.ariesframework.agent.Agent |
| 8 | +import org.hyperledger.ariesframework.agent.SubjectOutboundTransport |
| 9 | +import org.hyperledger.ariesframework.connection.models.ConnectionState |
| 10 | +import org.hyperledger.ariesframework.credentials.models.AutoAcceptCredential |
| 11 | +import org.hyperledger.ariesframework.credentials.models.CreateOfferOptions |
| 12 | +import org.hyperledger.ariesframework.credentials.models.CredentialPreview |
| 13 | +import org.hyperledger.ariesframework.credentials.models.CredentialState |
| 14 | +import org.hyperledger.ariesframework.oob.models.CreateOutOfBandInvitationConfig |
| 15 | +import org.hyperledger.ariesframework.proofs.ProofService |
| 16 | +import org.hyperledger.ariesframework.proofs.models.AttributeFilter |
| 17 | +import org.hyperledger.ariesframework.proofs.models.AutoAcceptProof |
| 18 | +import org.hyperledger.ariesframework.proofs.models.PredicateType |
| 19 | +import org.hyperledger.ariesframework.proofs.models.ProofAttributeInfo |
| 20 | +import org.hyperledger.ariesframework.proofs.models.ProofPredicateInfo |
| 21 | +import org.hyperledger.ariesframework.proofs.models.ProofRequest |
| 22 | +import org.hyperledger.ariesframework.proofs.models.ProofState |
| 23 | +import org.junit.After |
| 24 | +import org.junit.Assert.assertEquals |
| 25 | +import org.junit.Assert.assertNotNull |
| 26 | +import org.junit.Before |
| 27 | +import org.junit.Test |
| 28 | +import kotlin.time.Duration.Companion.seconds |
| 29 | + |
| 30 | +class ConnectionlessExchangeTest { |
| 31 | + lateinit var issuerAgent: Agent |
| 32 | + lateinit var holderAgent: Agent |
| 33 | + lateinit var verifierAgent: Agent |
| 34 | + |
| 35 | + lateinit var credDefId: String |
| 36 | + |
| 37 | + val credentialPreview = CredentialPreview.fromDictionary(mapOf("name" to "John", "age" to "99")) |
| 38 | + val context = InstrumentationRegistry.getInstrumentation().targetContext |
| 39 | + |
| 40 | + @Before |
| 41 | + fun setUp() = runTest(timeout = 30.seconds) { |
| 42 | + val issuerConfig = TestHelper.getBaseConfig("issuer", true) |
| 43 | + issuerConfig.autoAcceptCredential = AutoAcceptCredential.Always |
| 44 | + issuerAgent = Agent(context, issuerConfig) |
| 45 | + |
| 46 | + val holderConfig = TestHelper.getBaseConfig("holder", true) |
| 47 | + holderConfig.autoAcceptCredential = AutoAcceptCredential.Always |
| 48 | + holderConfig.autoAcceptProof = AutoAcceptProof.Always |
| 49 | + holderAgent = Agent(context, holderConfig) |
| 50 | + |
| 51 | + val verifierConfig = TestHelper.getBaseConfig("verifier", true) |
| 52 | + verifierConfig.autoAcceptProof = AutoAcceptProof.Always |
| 53 | + verifierAgent = Agent(context, verifierConfig) |
| 54 | + |
| 55 | + issuerAgent.initialize() |
| 56 | + holderAgent.initialize() |
| 57 | + verifierAgent.initialize() |
| 58 | + |
| 59 | + credDefId = TestHelper.prepareForIssuance(issuerAgent, listOf("name", "age")) |
| 60 | + } |
| 61 | + |
| 62 | + @After |
| 63 | + fun tearDown() = runTest { |
| 64 | + issuerAgent.reset() |
| 65 | + holderAgent.reset() |
| 66 | + verifierAgent.reset() |
| 67 | + } |
| 68 | + |
| 69 | + @Test @LargeTest |
| 70 | + fun testConnectionlessExchange() = runTest { |
| 71 | + issuerAgent.setOutboundTransport(SubjectOutboundTransport(holderAgent)) |
| 72 | + holderAgent.setOutboundTransport(SubjectOutboundTransport(issuerAgent)) |
| 73 | + |
| 74 | + val offerOptions = CreateOfferOptions( |
| 75 | + connection = null, |
| 76 | + credentialDefinitionId = credDefId, |
| 77 | + attributes = credentialPreview.attributes, |
| 78 | + comment = "credential-offer for test", |
| 79 | + ) |
| 80 | + val (message, record) = issuerAgent.credentialService.createOffer(offerOptions) |
| 81 | + validateState(issuerAgent, record.threadId, CredentialState.OfferSent) |
| 82 | + |
| 83 | + val oobConfig = CreateOutOfBandInvitationConfig( |
| 84 | + label = "issuer-to-holder-invitation", |
| 85 | + alias = "issuer-to-holder-invitation", |
| 86 | + handshake = false, |
| 87 | + messages = listOf(message), |
| 88 | + multiUseInvitation = false, |
| 89 | + autoAcceptConnection = true, |
| 90 | + ) |
| 91 | + val oobInvitation = issuerAgent.oob.createInvitation(oobConfig) |
| 92 | + |
| 93 | + val (oob, connection) = holderAgent.oob.receiveInvitation(oobInvitation.outOfBandInvitation) |
| 94 | + assertNotNull(connection) |
| 95 | + assertEquals(connection?.state, ConnectionState.Complete) |
| 96 | + assertNotNull(oob) |
| 97 | + |
| 98 | + validateState(holderAgent, record.threadId, CredentialState.Done) |
| 99 | + validateState(issuerAgent, record.threadId, CredentialState.Done) |
| 100 | + |
| 101 | + // credential exchange done. |
| 102 | + |
| 103 | + holderAgent.setOutboundTransport(SubjectOutboundTransport(verifierAgent)) |
| 104 | + verifierAgent.setOutboundTransport(SubjectOutboundTransport(holderAgent)) |
| 105 | + |
| 106 | + val proofRequest = getProofRequest() |
| 107 | + val (proofRequestMessage, proofExchangeRecord) = verifierAgent.proofService.createRequest( |
| 108 | + proofRequest, |
| 109 | + ) |
| 110 | + validateState(verifierAgent, proofExchangeRecord.threadId, ProofState.RequestSent) |
| 111 | + |
| 112 | + val oobConfigForProofExchange = CreateOutOfBandInvitationConfig( |
| 113 | + label = "verifier-to-holder-invitation", |
| 114 | + alias = "verifier-to-holder-invitation", |
| 115 | + handshake = false, |
| 116 | + messages = listOf(proofRequestMessage), |
| 117 | + multiUseInvitation = false, |
| 118 | + autoAcceptConnection = true, |
| 119 | + ) |
| 120 | + val oobInvitationForProofExchange = |
| 121 | + verifierAgent.oob.createInvitation(oobConfigForProofExchange) |
| 122 | + |
| 123 | + val (oobForProofExchange, connectionForProofExchange) = holderAgent.oob.receiveInvitation( |
| 124 | + oobInvitationForProofExchange.outOfBandInvitation, |
| 125 | + ) |
| 126 | + assertNotNull(connectionForProofExchange) |
| 127 | + assertEquals(connectionForProofExchange?.state, ConnectionState.Complete) |
| 128 | + assertNotNull(oobForProofExchange) |
| 129 | + |
| 130 | + validateState(holderAgent, proofExchangeRecord.threadId, ProofState.Done) |
| 131 | + validateState(verifierAgent, proofExchangeRecord.threadId, ProofState.Done) |
| 132 | + } |
| 133 | + |
| 134 | + private suspend fun validateState(agent: Agent, threadId: String, state: CredentialState) { |
| 135 | + val record = agent.credentialExchangeRepository.getByThreadAndConnectionId(threadId, null) |
| 136 | + assertEquals(record.state, state) |
| 137 | + } |
| 138 | + |
| 139 | + private suspend fun validateState(agent: Agent, threadId: String, state: ProofState) { |
| 140 | + val record = agent.proofRepository.getByThreadAndConnectionId(threadId, null) |
| 141 | + assertEquals(record.state, state) |
| 142 | + } |
| 143 | + |
| 144 | + private suspend fun getProofRequest(): ProofRequest { |
| 145 | + val attributes = mapOf( |
| 146 | + "name" to ProofAttributeInfo( |
| 147 | + name = "name", |
| 148 | + restrictions = listOf(AttributeFilter(credentialDefinitionId = credDefId)), |
| 149 | + ), |
| 150 | + ) |
| 151 | + val predicates = mapOf( |
| 152 | + "age" to ProofPredicateInfo( |
| 153 | + name = "age", |
| 154 | + predicateType = PredicateType.GreaterThanOrEqualTo, |
| 155 | + predicateValue = 50, |
| 156 | + restrictions = listOf(AttributeFilter(credentialDefinitionId = credDefId)), |
| 157 | + ), |
| 158 | + ) |
| 159 | + val nonce = ProofService.generateProofRequestNonce() |
| 160 | + return ProofRequest(nonce = nonce, requestedAttributes = attributes, requestedPredicates = predicates) |
| 161 | + } |
| 162 | +} |
0 commit comments