Skip to content
This repository has been archived by the owner on Mar 15, 2024. It is now read-only.

Commit

Permalink
Merge pull request #92 from iden3/feature/android-gomobile-integratio…
Browse files Browse the repository at this point in the history
…n-tests

Integration test for the generated Go code in Android
  • Loading branch information
ed255 authored Apr 7, 2020
2 parents 4de0291 + 303f013 commit 4838c36
Show file tree
Hide file tree
Showing 5 changed files with 229 additions and 13 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ unlinked_spec.ds
**/android/**/GeneratedPluginRegistrant.java
**/android/key.properties
**/android/iden3coreapi/github.properties
**/android/iden3coreapi/infura.properties
*.jks

# iOS/XCode related
Expand Down
8 changes: 7 additions & 1 deletion android/iden3coreapi/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,12 @@ android {
versionName "1.0"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
consumerProguardFiles 'consumer-rules.pro'
// Infura config
Properties infuraProperties = new Properties()
InputStream ins = new FileInputStream("iden3coreapi/infura.properties")
infuraProperties.load(ins)
ins.close()
buildConfigField("String", "INFURA_URL", infuraProperties['url'])
}

testOptions {
Expand Down Expand Up @@ -46,7 +52,7 @@ dependencies {
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
implementation 'androidx.appcompat:appcompat:1.1.0'
implementation 'androidx.core:core-ktx:1.2.0'
implementation 'com.iden3:iden3mobile:v0.0.1-beta4'
implementation 'com.iden3:iden3mobile:v0.0.1-beta8'
// Required -- JUnit 4 framework
testImplementation 'junit:junit:4.13'
// Optional -- Robolectric environment
Expand Down
Original file line number Diff line number Diff line change
@@ -1,24 +1,231 @@
package com.iden3.iden3coreapi

import android.support.test.InstrumentationRegistry
import android.support.test.runner.AndroidJUnit4

import android.util.Log
import androidx.test.ext.junit.runners.AndroidJUnit4
import androidx.test.platform.app.InstrumentationRegistry
import iden3mobile.*
import org.junit.Assert.assertEquals
import org.junit.Assert.assertNotEquals
import org.junit.Test
import org.junit.runner.RunWith

import org.junit.Assert.*
import java.io.File
import java.lang.Exception
import java.time.Instant

/**
* Instrumented test, which will execute on an Android device.
*
* See [testing documentation](http://d.android.com/tools/testing).
*/
@RunWith(AndroidJUnit4::class)
class ExampleInstrumentedTest {
class GomobileIntegrationTest {
@Test
fun useAppContext() {
// Context of the app under test.
fun fullFlow() {
// Test config
val nIdentities = 2
val nClaimsPerId = 5
val web3Url = BuildConfig.INFURA_URL
val issuerUrl = "http://167.172.104.160:6100/api/unstable"
val verifierUrl = "http://167.172.104.160:6200/api/unstable"

// Create a new directory for each identity
val appContext = InstrumentationRegistry.getInstrumentation().targetContext
assertEquals("com.iden3.iden3coreapi.test", appContext.packageName)
val storePath = appContext.filesDir.absolutePath
for (i in 0 until nIdentities){
// Remove directory in case last test did't finish
File("$storePath/$i").deleteRecursively()
// Create directory
File("$storePath/$i/").mkdirs()
}

// 1. Create nIdentities
var eventCounter = 0
Log.i("fullFlow","CREATING $nIdentities IDENTITIES")
var ids = List(nIdentities) { i ->
try {
Iden3mobile.newIdentity(
"$storePath/$i",
"password",
web3Url,
1000,
null,
object : Sender {
override fun send(event: Event) {
eventCounter++
Log.i("fullFlow","EVENT RECEIVED: ${event.ticketId}. $eventCounter EVENTS RECEIVED SO FAR")
assertEquals(null, event.err)
}
}
)
} catch (e: Exception) {
assertEquals(null, e)
null
}
}

// 2. Request claims
Log.i("fullFlow", "REQUESTING $nClaimsPerId CLAIMS FOR EACH IDENTITY")
var ticketCounter = 0
for (i in 0 until nClaimsPerId){
var idCount = 0
for (id in ids){
id?.requestClaimWithCb(issuerUrl, "$idCount/$i/${Instant.now()}", object: CallbackRequestClaim{
override fun fn(ticket: Ticket?, e: Exception?) {
assertNotEquals(null, ticket)
assertEquals(null, e)
ticketCounter++
Log.i("fullFlow","REQUEST CLAIM TICKET RECEIVED: ${ticket?.id}. $ticketCounter TICKETS RECEIVED SO FAR}")
}
})
idCount++
}
}
// Wait for callbacks
while (ticketCounter < nIdentities*nClaimsPerId){
Log.i("fullFlow","WAITING FOR REQUEST CLAIM TICKETS")
Thread.sleep(100)
}

// Restart identities
ids = restartIdentities(ids, storePath, web3Url, fun (event: Event) {
eventCounter++
Log.i("fullFlow","EVENT RECEIVED: ${event.ticketId}. $eventCounter EVENTS RECEIVED SO FAR")
assertEquals(null, event.err)
})

// Wait to receive claims
Log.i("fullFlow","WAITING TO RECEIVE CLAIMS")
while (eventCounter < nIdentities*nClaimsPerId){
Log.i("fullFlow","WAITING FOR REQUEST CLAIM EVENTS.")
Thread.sleep(1000)
}
// Check claims on DB
assertEquals(nIdentities*nClaimsPerId, countClaims(ids))

// 3. Prove claims
// Since the claims have been issued instants ago, they may not be on chain yet
// so it's normal to receive error in the upcoming seconds
var provedClaims = 0
var attempts = 10
while (provedClaims < nIdentities*nClaimsPerId && attempts >= 0){
provedClaims = 0
for (id in ids){
id?.getClaimDB()?.iterateClaimsJSON(object: ClaimDBIterFner{
override fun fn(key: String, claim: String): Boolean{
id.proveClaimWithCb(verifierUrl, key, object: CallbackProveClaim {
override fun fn(success: Boolean, e: Exception?) {
Log.i("fullFlow", "Verify claim: $key. Success? $success. Error? $e")
if(e == null){
assertEquals(true, success)
provedClaims++
}
}
})
return true
}
})
}
attempts--
Log.i("fullFlow","WAITING FOR CLAIMS TO BE PROVED.")
Thread.sleep(2_000)
}
assertEquals(nClaimsPerId*nIdentities, provedClaims)

// Restart identities
ids = restartIdentities(ids, storePath, web3Url, fun (event: Event) {
eventCounter++
Log.i("fullFlow","UNEXPECTED EVENT RECEIVED: ${event.ticketId}.")
assertEquals(null, event)
})

// Check claims on DB
assertEquals(nIdentities*nClaimsPerId, countClaims(ids))

// Test cancel ticket
// Request a claim to generate a ticket
ticketCounter = 0
for (id in ids){
id?.requestClaimWithCb(issuerUrl, "${Instant.now()}", object: CallbackRequestClaim{
override fun fn(ticket: Ticket?, e: Exception?) {
assertNotEquals(null, ticket)
assertEquals(null, e)
Log.i("fullFlow","REQUEST CLAIM TICKET RECEIVED.")
// Cancel ticket
id?.tickets.cancelTicket(ticket?.id)
ticketCounter++
}
})
}
while (ticketCounter < nIdentities){
Log.i("fullFlow","WAITING FOR TICKETS TO BE GENERATED.")
Thread.sleep(1000)
}
// Check tickets
Log.i("fullFlow","CHECK TICKETS AFTER RESTART")
// Give time for cancellation to take effect
Thread.sleep(1000)
ticketCounter = 0
var cancelledTicketCounter = 0
for (id in ids){
id?.tickets?.iterate(object: TicketOperator{
override fun iterate(ticket: Ticket?): Boolean {
ticketCounter++
if(ticket?.status == Iden3mobile.TicketStatusCancel){
cancelledTicketCounter++
}
return true
}
})
}
assertEquals(nIdentities*nClaimsPerId + nIdentities, ticketCounter)
assertEquals(nIdentities, cancelledTicketCounter)

// Stop identities
for(id in ids){
id?.stop()
}

// Remove identity directories
for (i in 0 until nIdentities){
File("$storePath/$i").deleteRecursively()
}
}

fun restartIdentities(ids: List<Identity?>, storePath: String, web3Url: String, fn: (e:Event)->Unit): List<Identity?>{
Log.i("fullFlow","RESTARTING IDENTITIES")
for (id in ids){
id?.stop()
}
return List(ids.size) { i ->
try {
Iden3mobile.newIdentityLoad(
"$storePath/$i",
"password",
web3Url,
1000,
object : Sender {
override fun send(event: Event) {
fn(event)
}
}
)
} catch (e: Exception) {
assertEquals(null, e)
null
}
}
}

fun countClaims(ids: List<Identity?>): Int {
var claimCounter = 0
for (id in ids){
id?.getClaimDB()?.iterateClaimsJSON(object: ClaimDBIterFner{
override fun fn(claim: String, key: String): Boolean{
claimCounter++
return true
}
})
}
return claimCounter
}
}
4 changes: 3 additions & 1 deletion android/iden3coreapi/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.iden3.iden3coreapi" />
package="com.iden3.iden3coreapi" >
<uses-permission android:name="android.permission.INTERNET"/>
</manifest>
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import androidx.test.core.app.ApplicationProvider
* See [testing documentation](http://d.android.com/tools/testing).
*/
class ExampleUnitTest {
val context = ApplicationProvider.getApplicationContext<Context>()
//val context = ApplicationProvider.getApplicationContext<Context>()

@Test
fun addition_isCorrect() {
Expand All @@ -20,7 +20,7 @@ class ExampleUnitTest {

@Test
fun testIden3CoreAPI() {
val iden3CoreAPI = Iden3CoreAPI.instance
val iden3CoreAPI = Iden3CoreAPI()
assertTrue(iden3CoreAPI != null)
}
}

0 comments on commit 4838c36

Please sign in to comment.