restful push and pop with cryptography

- integration test pushing and poping
  a message and comparing decrypted
  strings passes
- integration test comparing encrypted
  and decrypted string passes
This commit is contained in:
2021-12-17 12:15:54 +01:00
parent 10ba4cf31b
commit c46c664cdb
4 changed files with 179 additions and 136 deletions

View File

@ -1,108 +0,0 @@
package com.maenle.bump
import android.util.Log
import androidx.test.platform.app.InstrumentationRegistry
import androidx.test.ext.junit.runners.AndroidJUnit4
import com.example.bump.MessageProcessor
import com.example.bump.RestSingleton
import org.junit.Test
import org.junit.runner.RunWith
import org.junit.Assert.*
import java.util.concurrent.CountDownLatch
import java.util.concurrent.TimeUnit
/**
* Instrumented test, which will execute on an Android device.
*
* See [testing documentation](http://d.android.com/tools/testing).
*/
@RunWith(AndroidJUnit4::class)
class ExampleInstrumentedTest {
@Test
fun useAppContext() {
// Context of the app under test.
val appContext = InstrumentationRegistry.getInstrumentation().targetContext
assertEquals("com.maenle.bump", appContext.packageName)
}
}
@RunWith(AndroidJUnit4::class)
class MessageProcessorTest {
@Test
fun decryptMessages() {
val code = "dydoes-unknowledgeable-indiscretion-househusbands-pot-walloper-indiscretion-discophorous-transcriptions-dydoes-poodle-faker-transcriptions-budlike"
val messageRaw = "M1dEAxKZ5HUHCJoRkgGOvAABhqCAAAAAAGG2eKTSlKXWLDQx5B_wssZsNwsanzQID2UyUm4KKuKYKgfwH5MG2N-qzt6K4mg3pfZmWPaiDB9PiqlX236k6zo9Yvvq"
val data = decryptMessage(code, messageRaw)
assertEquals(data, "hello")
}
companion object {
@JvmStatic
fun decryptMessage(code: String, messageRaw: String): String {
val message = MessageProcessor(code)
val data = message.decrypt(messageRaw)
return data
}
}
}
@RunWith(AndroidJUnit4::class)
class RestTest{
@Test
fun getPeek() {
val context = InstrumentationRegistry.getInstrumentation().targetContext
val sender = "dydoes-unknowledgeable-indiscretion-househusbands"
RestSingleton.getInstance(context).peek(sender) { i -> assertEquals(i, "hello") }
}
}
class IntegrationTest{
@Test
fun pushPopAndDecrypt() {
val testMessage = "Hi There Sir"
val sender = "dydoes-unknowledgeable-indiscretion-househusbands"
val context = InstrumentationRegistry.getInstrumentation().targetContext
val lock = CountDownLatch(1);
fun messageTester(encrypted: String) {
val code = "dydoes-unknowledgeable-indiscretion-househusbands-pot-walloper-indiscretion-discophorous-transcriptions-dydoes-poodle-faker-transcriptions-budlike"
Log.d("TEST", encrypted)
val message = MessageProcessor(code)
val data = message.decrypt(encrypted)
Log.d("TEST", "data")
assertEquals(data, testMessage)
lock.countDown()
}
RestSingleton.getInstance(context).push(sender, testMessage) {
RestSingleton.getInstance(context).pop(sender) { i -> messageTester(i) }
}
Log.d("TEST", "waiting")
lock.await(20000, TimeUnit.MILLISECONDS)
Log.d("TEST", "done")
}
@Test
fun popAndDectypt() {
val testMessage = "Hi There Sir"
val sender = "raphael"
val context = InstrumentationRegistry.getInstrumentation().targetContext
val lock = CountDownLatch(1);
fun messageTester(encrypted: String) {
val code = "dydoes-unknowledgeable-indiscretion-househusbands-pot-walloper-indiscretion-discophorous-transcriptions-dydoes-poodle-faker-transcriptions-budlike"
val message = MessageProcessor(code)
val data = message.decrypt(encrypted)
assertEquals(data, testMessage)
lock.countDown()
}
RestSingleton.getInstance(context).pop(sender) { i -> messageTester(i) }
lock.await(20000, TimeUnit.MILLISECONDS)
}
}

View File

@ -0,0 +1,39 @@
package com.example.bump
import androidx.test.ext.junit.runners.AndroidJUnit4
import org.junit.Assert
import org.junit.Test
import org.junit.runner.RunWith
@RunWith(AndroidJUnit4::class)
class MessageProcessorTest {
@Test
fun decryptMessage() {
val code = "dydoes-unknowledgeable-indiscretion-househusbands-pot-walloper-indiscretion-discophorous-transcriptions-dydoes-poodle-faker-transcriptions-budlike"
// val messageRaw = "M1dEAxKZ5HUHCJoRkgGOvAABhqCAAAAAAGG2eKTSlKXWLDQx5B_wssZsNwsanzQID2UyUm4KKuKYKgfwH5MG2N-qzt6K4mg3pfZmWPaiDB9PiqlX236k6zo9Yvvq"
val messageRaw = "M1dEAxKZ5HUHCJoRkgGOvAABhqCAAAAAAGG8afPPk380EzwcbGzNoTr_I4y6YT8hnUYcToinlgsVkaUx5K-JicdS5epZenOX4u8vVhhMvR0ebeWm_mgp6LZvTw8S"
val data = decryptMessage(code, messageRaw)
Assert.assertEquals(data, "hello")
}
@Test
fun encryptAndDecryptMessage() {
val code = "dydoes-unknowledgeable-indiscretion-househusbands-pot-walloper-indiscretion-discophorous-transcriptions-dydoes-poodle-faker-transcriptions-budlike"
val message = MessageProcessor(code)
val test = getRandomString(32)
val encrypted = message.encrypt(test)
val decrypted = decryptMessage(code, encrypted)
Assert.assertEquals(test, decrypted)
}
companion object {
@JvmStatic
fun decryptMessage(code: String, messageData: String): String {
val message = MessageProcessor(code)
val data = message.decrypt(messageData)
return data
}
}
}

View File

@ -0,0 +1,69 @@
package com.example.bump
import androidx.test.platform.app.InstrumentationRegistry
import androidx.test.ext.junit.runners.AndroidJUnit4
import org.json.JSONObject
import org.junit.Test
import org.junit.runner.RunWith
import org.junit.Assert.*
import java.util.concurrent.CountDownLatch
import java.util.concurrent.TimeUnit
/**
* Instrumented test, which will execute on an Android device.
*
* See [testing documentation](http://d.android.com/tools/testing).
*/
@RunWith(AndroidJUnit4::class)
class ExampleInstrumentedTest {
@Test
fun useAppContext() {
// Context of the app under test.
val appContext = InstrumentationRegistry.getInstrumentation().targetContext
assertEquals("com.maenle.bump", appContext.packageName)
}
}
class RestCryptTest{
@Test
fun pushPopAndDecrypt() {
val code = "dydoes-unknowledgeable-indiscretion-househusbands-pot-walloper-indiscretion-discophorous-transcriptions-dydoes-poodle-faker-transcriptions-budlike"
val testMessage = getRandomString(32)
val context = InstrumentationRegistry.getInstrumentation().targetContext
var encrypted = ""
val lock = CountDownLatch(1)
fun messageTester(messageEncrypted: String) {
val message = MessageProcessor(code)
val messageData: String = JSONObject(messageEncrypted).get("data").toString()
assertEquals(messageData, encrypted)
val data = message.decrypt(messageData)
assertEquals(data, testMessage)
lock.countDown()
}
val message = MessageProcessor(code)
encrypted = message.encrypt(testMessage)
RestSingleton.getInstance(context).push(message.sender, encrypted) {
RestSingleton.getInstance(context).pop(message.sender) { i -> messageTester(i) }
}
lock.await(200000, TimeUnit.MILLISECONDS)
}
}
fun getRandomString(length: Int) : String {
val allowedChars = ('A'..'Z') + ('a'..'z') + ('0'..'9') + (' ') + ('_') + ('-')
return (1..length)
.map { allowedChars.random() }
.joinToString("")
}