Skip to content

Commit

Permalink
Eoin/plt 796 move evervault multiplatform into evervault android (#27)
Browse files Browse the repository at this point in the history
* port evervault-multiplatform to evervault-android

* Minor fixes

* Allow running of e2e tests in debug mode
  • Loading branch information
boilsquid committed Oct 27, 2023
1 parent ede14e2 commit a8eb7f7
Show file tree
Hide file tree
Showing 79 changed files with 2,357 additions and 508 deletions.
28 changes: 23 additions & 5 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,36 @@ env:
CARGO_TERM_COLOR: always

jobs:
ci:
unit-tests:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: actions/setup-java@v3
with:
distribution: 'corretto'
java-version: '17'
- name: build project
run: ./gradlew clean build
- name: unit tests
run: ./gradlew test --info
- name: build and test project
run: ./gradlew clean build --info
- name: Upload unit test report on failure
if: ${{ failure() }}
uses: actions/upload-artifact@v2
with:
name: failed_unit_tests
path: ${{ github.workspace }}/lib/build/reports/tests/test
e2e-tests:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: actions/setup-java@v3
with:
distribution: 'corretto'
java-version: '17'
- name: e2e tests
env:
EV_API_KEY: ${{ secrets.EV_API_KEY }}
EV_TEAM_UUID: ${{ secrets.EV_TEAM_UUID }}
EV_APP_UUID: ${{ secrets.EV_APP_UUID }}
run: ./gradlew clean :evervault-common-e2e:build --info
- name: Upload unit test report on failure
if: ${{ failure() }}
uses: actions/upload-artifact@v2
Expand Down
1 change: 1 addition & 0 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ plugins {
id("org.jetbrains.kotlin.android") version "1.8.20" apply false
id("io.github.gradle-nexus.publish-plugin") version "1.1.0"
id("com.android.application") version "7.4.2" apply false
id("com.android.library") version "7.4.2" apply false
}

allprojects {
Expand Down
1 change: 1 addition & 0 deletions evervault-common-e2e/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/build
54 changes: 54 additions & 0 deletions evervault-common-e2e/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
plugins {
id("com.android.library")
id("org.jetbrains.kotlin.android")
kotlin("plugin.serialization") version "1.8.21"
}

java {
sourceCompatibility = JavaVersion.VERSION_11
targetCompatibility = JavaVersion.VERSION_11
}

android {
namespace = "com.evervault.sdk.e2e"
compileSdk = 33

defaultConfig {
minSdk = 33

testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
consumerProguardFiles("consumer-rules.pro")
}

buildTypes {
release {
isMinifyEnabled = false
proguardFiles(
getDefaultProguardFile("proguard-android-optimize.txt"),
"proguard-rules.pro"
)
}
}
compileOptions {
sourceCompatibility = JavaVersion.VERSION_11
targetCompatibility = JavaVersion.VERSION_11
}
kotlinOptions {
jvmTarget = "11"
}
}

val ktorVersion = "2.3.1"

dependencies {
implementation(project(":evervault-common"))
implementation(kotlin("stdlib-common"))
implementation("org.jetbrains.kotlin:kotlin-stdlib-common")
implementation("org.jetbrains.kotlinx:kotlinx-serialization-json:1.5.1")
implementation("org.jetbrains.kotlinx:kotlinx-coroutines-core:1.5.2")
implementation("io.ktor:ktor-client-core:$ktorVersion")
implementation("io.ktor:ktor-client-okhttp:$ktorVersion")
implementation("org.jetbrains.kotlinx:kotlinx-serialization-json:1.5.1")
implementation("com.google.code.gson:gson:2.8.7")
testImplementation("junit:junit:4.13.2")
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,197 @@
package com.evervault.sdk.e2e

import com.evervault.sdk.common.ConfigUrls
import com.evervault.sdk.common.CustomConfig
import org.junit.Test
import com.evervault.sdk.common.Evervault
import com.google.gson.Gson
import io.ktor.client.HttpClient
import io.ktor.client.plugins.defaultRequest
import io.ktor.client.request.header
import io.ktor.client.request.headers
import io.ktor.client.request.post
import io.ktor.client.request.setBody
import io.ktor.client.statement.HttpResponse
import io.ktor.client.statement.bodyAsText
import io.ktor.http.HttpHeaders
import kotlinx.coroutines.async
import kotlinx.coroutines.coroutineScope
import kotlinx.coroutines.runBlocking
import kotlinx.serialization.Serializable
import kotlinx.serialization.encodeToString
import kotlinx.serialization.json.Json
import org.junit.Assert.assertEquals
import org.junit.Assert.assertNotNull
import org.junit.Before
import java.util.Base64

class EvervaultTest {
private val encryptedStringRegex = Regex("((ev(:|%3A))(debug(:|%3A))?(([A-z0-9+/=%]+)(:|%3A))?((number|boolean|string)(:|%3A))?(([A-z0-9+/=%]+)(:|%3A)){3}(\\$|%24))|(((eyJ[A-z0-9+=.]+){2})([\\w]{8}(-[\\w]{4}){3}-[\\w]{12}))")

private lateinit var apiKey: String
private lateinit var appUuid: String
private lateinit var teamUuid: String
private val isDebugMode = setDebugMode()

private var httpClient = HttpClient {
defaultRequest {
header(HttpHeaders.ContentType, "application/json")
}
}

private val json = Json { ignoreUnknownKeys = true }

@Before
fun setup() {
Evervault.shared.configure(
teamId = teamUuid,
appId = appUuid,
customConfig = CustomConfig(
isDebugMode = isDebugMode
)
)
}

@Test
fun testEncryptString() = runBlocking {
val encryptedString = Evervault.shared.encrypt("dummy string to encrypt")
println(encryptedString)
assertNotNull(encryptedString)
val matches = encryptedStringRegex.findAll(encryptedString as String).count()
assertEquals(1, matches)
}

@Test
fun testDecryptData() = runBlocking {
// Test requires an Evervault API key so don't run if its not set
if (!isDebugMode) {
val data = RawData(
stringData = "Bob",
numberData = 1,
floatData = 1.5,
booleanData = true,
arrayData = arrayListOf("hello", "world"),
)
// Encrypt some data
val encrypted = encryptData(ConfigUrls().apiUrl, data)
val clientToken = createClientSideToken(ConfigUrls().apiUrl, encrypted)
assertNotNull(clientToken)
val decrypted = Evervault.shared.decrypt(clientToken.token, encrypted) as Map<*, *>
println(decrypted)
assertEquals(
decrypted["stringData"],
"Bob"
)

assertEquals(
decrypted["numberData"],
1.0
)

assertEquals(
decrypted["floatData"],
1.5
)

assertEquals(
decrypted["booleanData"],
true
)

assertEquals(
decrypted["arrayData"],
arrayListOf<String>("hello", "world")
)
}
}

@Serializable
data class TokenData(
val token: String,
)

@Serializable
data class RawData(
var stringData: String?,
var numberData: Int?,
var floatData: Double?,
var booleanData: Boolean?,
var arrayData: ArrayList<String>?,
)
@Serializable
data class EncryptedTestData(
var stringData: String?,
var numberData: String?,
var floatData: String?,
var booleanData: String?,
var arrayData: ArrayList<String>?,
)

private suspend fun createClientSideToken(url: Any, data: Any): TokenData {
val task = coroutineScope {
async {
try {
val headerValue =
"${appUuid}:${apiKey}"
val b64HeaderValue =
Base64.getEncoder().encodeToString(headerValue.toByteArray());
val body = Gson().toJson(mapOf("action" to "api:decrypt", "payload" to data))
val response: HttpResponse =
httpClient.post("${url}/client-side-tokens") {
setBody(body)
headers {
append("Authorization", "Basic $b64HeaderValue")
}
}

val responseBody = response.bodyAsText()
return@async json.decodeFromString<TokenData>(responseBody)
} catch (error: Error) {
throw error
}
}
}

return task.await()
}

private suspend fun encryptData(url: Any, data: RawData): EncryptedTestData {
val task = coroutineScope {
async {
try {
val headerValue = "${appUuid}:${apiKey}"
val b64HeaderValue =
Base64.getEncoder().encodeToString(headerValue.toByteArray());
val body = json.encodeToString(data)
val response: HttpResponse =
httpClient.post("${url}/encrypt") {
setBody(body)
headers{
append("Authorization", "Basic $b64HeaderValue")
}
}
val responseBody = response.bodyAsText()
return@async json.decodeFromString<EncryptedTestData>(responseBody)
} catch (error: Error) {
throw error
}
}
}

return task.await()
}

private fun setDebugMode(): Boolean {
try {
apiKey = System.getenv("EV_API_KEY")
appUuid = System.getenv("EV_APP_UUID")
teamUuid = System.getenv("EV_TEAM_UUID")
} catch (e: RuntimeException) {
apiKey = "test-api-key"
appUuid = "test-app-uuid"
teamUuid = "test-team-uuid"
return true
}
return false
}
}
1 change: 1 addition & 0 deletions evervault-common/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/build
51 changes: 51 additions & 0 deletions evervault-common/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
plugins {
id("com.android.library")
id("org.jetbrains.kotlin.android")
kotlin("plugin.serialization") version "1.8.21"
}

android {
namespace = "com.evervault.sdk.common"
compileSdk = 33

defaultConfig {
minSdk = 26

testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
consumerProguardFiles("consumer-rules.pro")
}

buildTypes {
release {
isMinifyEnabled = false
proguardFiles(
getDefaultProguardFile("proguard-android-optimize.txt"),
"proguard-rules.pro"
)
}
}
compileOptions {
sourceCompatibility = JavaVersion.VERSION_11
targetCompatibility = JavaVersion.VERSION_11
}
kotlinOptions {
jvmTarget = "11"
}
}

val ktorVersion = "2.3.1"

dependencies {
implementation("androidx.core:core-ktx:1.9.0")
implementation("androidx.appcompat:appcompat:1.6.1")
implementation("com.google.android.material:material:1.8.0")
implementation("io.ktor:ktor-client-core:$ktorVersion")
implementation("io.ktor:ktor-client-okhttp:$ktorVersion")
implementation("org.jetbrains.kotlinx:kotlinx-serialization-json:1.5.1")
implementation("com.google.code.gson:gson:2.8.9")
implementation("org.bouncycastle:bcprov-jdk15on:1.70")
testImplementation("junit:junit:4.13.2")
implementation("org.mockito.kotlin:mockito-kotlin:5.0.0")
androidTestImplementation("androidx.test.ext:junit:1.1.5")
androidTestImplementation("androidx.test.espresso:espresso-core:3.5.1")
}
Empty file.
21 changes: 21 additions & 0 deletions evervault-common/proguard-rules.pro
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# Add project specific ProGuard rules here.
# You can control the set of applied configuration files using the
# proguardFiles setting in build.gradle.
#
# For more details, see
# http://developer.android.com/guide/developing/tools/proguard.html

# If your project uses WebView with JS, uncomment the following
# and specify the fully qualified class name to the JavaScript interface
# class:
#-keepclassmembers class fqcn.of.javascript.interface.for.webview {
# public *;
#}

# Uncomment this to preserve the line number information for
# debugging stack traces.
#-keepattributes SourceFile,LineNumberTable

# If you keep the line number information, uncomment this to
# hide the original source file name.
#-renamesourcefileattribute SourceFile
Loading

0 comments on commit a8eb7f7

Please sign in to comment.