package com.maenle.bump import androidx.test.platform.app.InstrumentationRegistry import androidx.test.ext.junit.runners.AndroidJUnit4 import com.maenle.bump.util.Message import com.maenle.bump.util.MessageProcessor import com.maenle.bump.util.RestSingleton import org.json.JSONArray 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 NotificationTest { @Test fun showNotification() { } } 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: JSONObject) { val message = MessageProcessor(code) val messageData: String = messageEncrypted.get("data").toString() assertEquals(messageData, encrypted) val data = message.decrypt(messageData) assertEquals(data, testMessage) val m = Message(code, data, messageEncrypted.get("timestamp").toString()) 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) } @Test fun pushListAndDecrypt() { 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 listTester(messages: JSONArray) { val message = MessageProcessor(code) var messageEncoded = messages[0] as JSONObject var messageData = messageEncoded.get("data").toString() var data = message.decrypt(messageData) assertEquals(data, "hello") messageEncoded = messages[messages.length()-1] as JSONObject messageData = messageEncoded.get("data").toString() 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).list(message.sender) { i -> listTester(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("") }