more test to core
This commit is contained in:
parent
6d32516aec
commit
8562a3e481
|
@ -65,7 +65,4 @@
|
|||
|
||||
<color name="splashBackground">#2E2E2E</color>
|
||||
|
||||
<color name="importListFileName">#FFFFFF</color>
|
||||
<color name="importListAdditionalInfo">#BBBBBB</color>
|
||||
|
||||
</resources>
|
||||
|
|
|
@ -1,42 +0,0 @@
|
|||
package info.nightscout.androidaps.utils;
|
||||
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.powermock.modules.junit4.PowerMockRunner;
|
||||
|
||||
@RunWith(PowerMockRunner.class)
|
||||
public class DecimalFormatterTest {
|
||||
|
||||
@Test
|
||||
public void to0DecimalTest() {
|
||||
Assert.assertEquals("1", DecimalFormatter.to0Decimal(1.33d).replace(",", "."));
|
||||
Assert.assertEquals("1U", DecimalFormatter.to0Decimal(1.33d, "U").replace(",", "."));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void to1DecimalTest() {
|
||||
Assert.assertEquals("1.3", DecimalFormatter.to1Decimal(1.33d).replace(",", "."));
|
||||
Assert.assertEquals("1.3U", DecimalFormatter.to1Decimal(1.33d, "U").replace(",", "."));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void to2DecimalTest() {
|
||||
Assert.assertEquals("1.33", DecimalFormatter.to2Decimal(1.3333d).replace(",", "."));
|
||||
Assert.assertEquals("1.33U", DecimalFormatter.to2Decimal(1.3333d, "U").replace(",", "."));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void to3DecimalTest() {
|
||||
Assert.assertEquals("1.333", DecimalFormatter.to3Decimal(1.3333d).replace(",", "."));
|
||||
Assert.assertEquals("1.333U", DecimalFormatter.to3Decimal(1.3333d, "U").replace(",", "."));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void toPumpSupportedBolus() {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void pumpSupportedBolusFormat() {
|
||||
}
|
||||
}
|
|
@ -1,93 +0,0 @@
|
|||
package info.nightscout.androidaps.utils;
|
||||
|
||||
import org.json.JSONException;
|
||||
import org.json.JSONObject;
|
||||
import org.junit.Test;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertFalse;
|
||||
import static org.junit.Assert.assertNotEquals;
|
||||
import static org.junit.Assert.assertNull;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
/**
|
||||
* Created by mike on 12.03.2018.
|
||||
*/
|
||||
|
||||
public class JsonHelperTest {
|
||||
|
||||
private String jsonString = "{\"d\":\"3.0\",\"i\":\"4\",\"s\":\"5\",\"b\":\"true\",\"j\":{\"a\": \"1\"}}";
|
||||
|
||||
@Test
|
||||
public void safeGetObjectTest() throws JSONException {
|
||||
JSONObject object = new JSONObject(jsonString);
|
||||
Object o = new Object();
|
||||
assertEquals(o, JsonHelper.safeGetObject(null, "x", o));
|
||||
assertEquals(o, JsonHelper.safeGetObject(object, "x", o));
|
||||
assertNotEquals(o, JsonHelper.safeGetObject(object, "d", o));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void safeGetJSONObjectTest() throws JSONException {
|
||||
JSONObject object = new JSONObject(jsonString);
|
||||
JSONObject o = new JSONObject();
|
||||
assertEquals(o, JsonHelper.safeGetJSONObject(null, "x", o));
|
||||
assertTrue(JsonHelper.safeGetJSONObject(object, "j", o).has("a"));
|
||||
assertEquals(o, JsonHelper.safeGetJSONObject(object, "d", o));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void safeGetStringTest() throws JSONException {
|
||||
JSONObject object = new JSONObject(jsonString);
|
||||
Object o = new Object();
|
||||
assertNull(JsonHelper.safeGetString(null, "s"));
|
||||
assertNull(JsonHelper.safeGetString(object, "notexisting"));
|
||||
assertEquals("5", JsonHelper.safeGetString(object, "s"));
|
||||
|
||||
assertEquals("default", JsonHelper.safeGetString(null, "notexisting", "default"));
|
||||
assertEquals("default", JsonHelper.safeGetString(object, "notexisting", "default"));
|
||||
assertEquals("5", JsonHelper.safeGetString(object, "s", "default"));
|
||||
|
||||
assertEquals("default", JsonHelper.safeGetStringAllowNull(null, "notexisting", "default"));
|
||||
assertEquals("default", JsonHelper.safeGetStringAllowNull(object, "notexisting", "default"));
|
||||
assertNull(JsonHelper.safeGetStringAllowNull(object, "notexisting", null));
|
||||
assertEquals("5", JsonHelper.safeGetStringAllowNull(object, "s", "default"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void safeGetDoubleTest() throws JSONException {
|
||||
JSONObject object = new JSONObject(jsonString);
|
||||
|
||||
assertEquals(0.0d, JsonHelper.safeGetDouble(object, "notexisting"), 0.0d);
|
||||
assertEquals(0.0d, JsonHelper.safeGetDouble(null, "notexisting"), 0.0d);
|
||||
assertEquals(3.0d, JsonHelper.safeGetDouble(object, "d"), 0.000001d);
|
||||
|
||||
assertEquals(6d, JsonHelper.safeGetDouble(null, "notexisting", 6d), 0.0d);
|
||||
assertEquals(6d, JsonHelper.safeGetDouble(object, "notexisting", 6d), 0.0d);
|
||||
assertEquals(3d, JsonHelper.safeGetDouble(object, "d", 6d), 0.0d);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void safeGetLntTest() throws JSONException {
|
||||
JSONObject object = new JSONObject(jsonString);
|
||||
assertEquals(0, JsonHelper.safeGetInt(null, "notexisting"));
|
||||
assertEquals(0, JsonHelper.safeGetInt(object, "notexisting"));
|
||||
assertEquals(4, JsonHelper.safeGetInt(object, "i"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void safeGetLongTest() throws JSONException {
|
||||
JSONObject object = new JSONObject(jsonString);
|
||||
assertEquals(0, JsonHelper.safeGetInt(null, "notexisting"));
|
||||
assertEquals(0, JsonHelper.safeGetInt(object, "notexisting"));
|
||||
assertEquals(4, JsonHelper.safeGetInt(object, "i"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void safeGetBooleanTest() throws JSONException {
|
||||
JSONObject object = new JSONObject(jsonString);
|
||||
assertFalse(JsonHelper.safeGetBoolean(null, "notexisting"));
|
||||
assertFalse(JsonHelper.safeGetBoolean(object, "notexisting"));
|
||||
assertTrue(JsonHelper.safeGetBoolean(object, "b"));
|
||||
}
|
||||
}
|
|
@ -1,22 +0,0 @@
|
|||
package info.nightscout.androidaps.utils;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
public class StringUtilsTest {
|
||||
|
||||
@Test
|
||||
public void removeSurroundingQuotesTest() {
|
||||
String compareString = "test";
|
||||
|
||||
assertEquals(compareString, StringUtils.removeSurroundingQuotes(compareString));
|
||||
assertEquals(compareString, StringUtils.removeSurroundingQuotes("\"" + compareString + "\""));
|
||||
assertEquals("\"" + compareString, StringUtils.removeSurroundingQuotes("\"" + compareString));
|
||||
|
||||
compareString = "te\"st";
|
||||
assertEquals(compareString, StringUtils.removeSurroundingQuotes(compareString));
|
||||
assertEquals(compareString, StringUtils.removeSurroundingQuotes("\"" + compareString + "\""));
|
||||
assertEquals("\"" + compareString, StringUtils.removeSurroundingQuotes("\"" + compareString));
|
||||
}
|
||||
}
|
|
@ -1,49 +0,0 @@
|
|||
package info.nightscout.androidaps.utils;
|
||||
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
/**
|
||||
* Created by mike on 26.03.2018.
|
||||
*/
|
||||
|
||||
//@RunWith(PowerMockRunner.class)
|
||||
public class TTest {
|
||||
|
||||
@Test
|
||||
public void toUnits() {
|
||||
Assert.assertEquals(1, T.msecs(1000).secs());
|
||||
Assert.assertEquals(1, T.secs(60).mins());
|
||||
Assert.assertEquals(1, T.mins(60).hours());
|
||||
Assert.assertEquals(1, T.hours(24).days());
|
||||
Assert.assertEquals(24, T.days(1).hours());
|
||||
Assert.assertEquals(60000, T.mins(1).msecs());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void now() {
|
||||
Assert.assertTrue(Math.abs(T.now().msecs() - System.currentTimeMillis()) < 5000);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void additions() {
|
||||
long nowMsecs = System.currentTimeMillis();
|
||||
T now = T.msecs(nowMsecs);
|
||||
|
||||
Assert.assertEquals(now.plus(T.secs(5)).msecs(), nowMsecs + 5 * 1000);
|
||||
Assert.assertEquals(now.plus(T.mins(5)).msecs(), nowMsecs + 5 * 60 * 1000);
|
||||
Assert.assertEquals(now.plus(T.hours(5)).msecs(), nowMsecs + 5 * 60 * 60 * 1000);
|
||||
Assert.assertEquals(now.plus(T.days(5)).msecs(), nowMsecs + 5 * 24 * 60 * 60 * 1000);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void subtractions() {
|
||||
long nowMsecs = System.currentTimeMillis();
|
||||
T now = T.msecs(nowMsecs);
|
||||
|
||||
Assert.assertEquals(now.minus(T.secs(5)).msecs(), nowMsecs - 5 * 1000);
|
||||
Assert.assertEquals(now.minus(T.mins(5)).msecs(), nowMsecs - 5 * 60 * 1000);
|
||||
Assert.assertEquals(now.minus(T.hours(5)).msecs(), nowMsecs - 5 * 60 * 60 * 1000);
|
||||
Assert.assertEquals(now.minus(T.days(5)).msecs(), nowMsecs - 5 * 24 * 60 * 60 * 1000);
|
||||
}
|
||||
}
|
|
@ -46,6 +46,17 @@ android {
|
|||
sourceCompatibility JavaVersion.VERSION_1_8
|
||||
targetCompatibility JavaVersion.VERSION_1_8
|
||||
}
|
||||
testOptions {
|
||||
unitTests {
|
||||
returnDefaultValues = true
|
||||
includeAndroidResources = true
|
||||
|
||||
all {
|
||||
maxParallelForks = 10
|
||||
forkEvery = 20
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
|
|
@ -109,6 +109,12 @@ dependencies {
|
|||
api 'com.scottyab:rootbeer-lib:0.0.8'
|
||||
|
||||
testImplementation "junit:junit:$junit_version"
|
||||
testImplementation "org.mockito:mockito-core:${mockitoVersion}"
|
||||
testImplementation "org.powermock:powermock-api-mockito2:${powermockVersion}"
|
||||
testImplementation "org.powermock:powermock-module-junit4-rule-agent:${powermockVersion}"
|
||||
testImplementation "org.powermock:powermock-module-junit4-rule:${powermockVersion}"
|
||||
testImplementation "org.powermock:powermock-module-junit4:${powermockVersion}"
|
||||
androidTestImplementation "androidx.test.ext:junit:$androidx_junit"
|
||||
androidTestImplementation "androidx.test:rules:$androidx_rules"
|
||||
testImplementation 'org.json:json:20201115' // Needed for JsonHelperTest
|
||||
}
|
|
@ -1,5 +1,6 @@
|
|||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<androidx.cardview.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
xmlns:app="http://schemas.android.com/apk/res-auto"
|
||||
xmlns:card_view="http://schemas.android.com/apk/res-auto"
|
||||
xmlns:tools="http://schemas.android.com/tools"
|
||||
android:id="@+id/careportal_cardview"
|
||||
|
@ -29,7 +30,7 @@
|
|||
android:layout_marginEnd="6dp"
|
||||
android:layout_marginBottom="1dp"
|
||||
card_view:srcCompat="@drawable/ic_meta_format"
|
||||
android:tint="@color/importListFileName" />
|
||||
app:tint="@color/importListFileName" />
|
||||
|
||||
<TextView
|
||||
android:id="@+id/filelist_name"
|
||||
|
@ -90,7 +91,7 @@
|
|||
android:layout_marginEnd="8dp"
|
||||
android:layout_marginBottom="1dp"
|
||||
card_view:srcCompat="@drawable/ic_meta_name"
|
||||
android:tint="@color/importListAdditionalInfo" />
|
||||
app:tint="@color/importListAdditionalInfo" />
|
||||
|
||||
<TextView
|
||||
android:id="@+id/meta_device_name"
|
||||
|
@ -122,7 +123,7 @@
|
|||
android:layout_marginEnd="8dp"
|
||||
android:layout_marginBottom="1dp"
|
||||
card_view:srcCompat="@drawable/ic_meta_date"
|
||||
android:tint="@color/importListAdditionalInfo" />
|
||||
app:tint="@color/importListAdditionalInfo" />
|
||||
|
||||
<TextView
|
||||
android:id="@+id/meta_date_time"
|
||||
|
|
|
@ -85,5 +85,9 @@
|
|||
<color name="metadataTextWarning">#FF8C00</color>
|
||||
<color name="metadataTextError">#FF5555</color>
|
||||
|
||||
<!-- Import/Export -->
|
||||
<color name="importListAdditionalInfo">#BBBBBB</color>
|
||||
<color name="importListFileName">#FFFFFF</color>
|
||||
|
||||
|
||||
</resources>
|
||||
|
|
40
core/src/test/java/info/nightscout/androidaps/TestBase.kt
Normal file
40
core/src/test/java/info/nightscout/androidaps/TestBase.kt
Normal file
|
@ -0,0 +1,40 @@
|
|||
package info.nightscout.androidaps
|
||||
|
||||
import info.nightscout.androidaps.logging.AAPSLoggerTest
|
||||
import info.nightscout.androidaps.utils.rx.AapsSchedulers
|
||||
import info.nightscout.androidaps.utils.rx.TestAapsSchedulers
|
||||
import org.junit.Before
|
||||
import org.junit.Rule
|
||||
import org.mockito.Mockito
|
||||
import org.mockito.junit.MockitoJUnit
|
||||
import org.mockito.junit.MockitoRule
|
||||
import java.util.*
|
||||
|
||||
@Suppress("SpellCheckingInspection")
|
||||
open class TestBase {
|
||||
|
||||
val aapsLogger = AAPSLoggerTest()
|
||||
val aapsSchedulers: AapsSchedulers = TestAapsSchedulers()
|
||||
|
||||
// Add a JUnit rule that will setup the @Mock annotated vars and log.
|
||||
// Another possibility would be to add `MockitoAnnotations.initMocks(this) to the setup method.
|
||||
@get:Rule
|
||||
val mockitoRule: MockitoRule = MockitoJUnit.rule()
|
||||
|
||||
@Before
|
||||
fun setupLocale() {
|
||||
Locale.setDefault(Locale.ENGLISH)
|
||||
System.setProperty("disableFirebase", "true")
|
||||
}
|
||||
|
||||
// Workaround for Kotlin nullability.
|
||||
// https://medium.com/@elye.project/befriending-kotlin-and-mockito-1c2e7b0ef791
|
||||
// https://stackoverflow.com/questions/30305217/is-it-possible-to-use-mockito-in-kotlin
|
||||
fun <T> anyObject(): T {
|
||||
Mockito.any<T>()
|
||||
return uninitialized()
|
||||
}
|
||||
|
||||
@Suppress("Unchecked_Cast")
|
||||
fun <T> uninitialized(): T = null as T
|
||||
}
|
|
@ -1,97 +1,98 @@
|
|||
package info.nightscout.androidaps.utils
|
||||
|
||||
import info.nightscout.androidaps.TestBase
|
||||
import org.hamcrest.CoreMatchers.containsString
|
||||
import org.hamcrest.CoreMatchers.not
|
||||
import org.junit.Assert
|
||||
import org.junit.Assume.assumeThat
|
||||
import org.junit.Test
|
||||
import org.junit.runner.RunWith
|
||||
import org.powermock.core.classloader.annotations.PowerMockIgnore
|
||||
import org.powermock.modules.junit4.PowerMockRunner
|
||||
|
||||
// https://stackoverflow.com/questions/52344522/joseexception-couldnt-create-aes-gcm-nopadding-cipher-illegal-key-size
|
||||
// https://stackoverflow.com/questions/47708951/can-aes-256-work-on-android-devices-with-api-level-26
|
||||
// Java prior to Oracle Java 8u161 does not have policy for 256 bit AES - but Android support it
|
||||
// when test is run in Vanilla JVM without policy - Invalid key size exception is thrown
|
||||
fun assumeAES256isSupported(cryptoUtil: CryptoUtil) {
|
||||
cryptoUtil.lastException?.message?.let { exceptionMessage ->
|
||||
assumeThat("Upgrade your testing environment Java (OpenJDK or Java 8u161) and JAVA_HOME - AES 256 is supported by Android so this exception should not happen!", exceptionMessage, not(containsString("key size")))
|
||||
}
|
||||
}
|
||||
|
||||
@PowerMockIgnore("javax.crypto.*")
|
||||
@RunWith(PowerMockRunner::class)
|
||||
class CryptoUtilTest: TestBase() {
|
||||
|
||||
var cryptoUtil: CryptoUtil = CryptoUtil(aapsLogger)
|
||||
|
||||
@Test
|
||||
fun testFixedSaltCrypto() {
|
||||
val salt = byteArrayOf(
|
||||
-33, -29, 16, -19, 99, -111, -3, 2, 116, 106, 47, 38, -54, 11, -77, 28,
|
||||
111, -15, -65, -110, 4, -32, -29, -70, -95, -88, -53, 19, 87, -103, 123, -15)
|
||||
|
||||
val password = "thisIsFixedPassword"
|
||||
val payload = "FIXED-PAYLOAD"
|
||||
|
||||
val encrypted = cryptoUtil.encrypt(password, salt, payload)
|
||||
assumeAES256isSupported(cryptoUtil)
|
||||
Assert.assertNotNull(encrypted)
|
||||
|
||||
val decrypted = cryptoUtil.decrypt(password, salt, encrypted!!)
|
||||
assumeAES256isSupported(cryptoUtil)
|
||||
Assert.assertEquals(decrypted, payload)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testStandardCrypto() {
|
||||
val salt = cryptoUtil.mineSalt()
|
||||
|
||||
val password = "topSikret"
|
||||
val payload = "{what:payloadYouWantToProtect}"
|
||||
|
||||
val encrypted = cryptoUtil.encrypt(password, salt, payload)
|
||||
assumeAES256isSupported(cryptoUtil)
|
||||
Assert.assertNotNull(encrypted)
|
||||
|
||||
val decrypted = cryptoUtil.decrypt(password, salt, encrypted!!)
|
||||
assumeAES256isSupported(cryptoUtil)
|
||||
Assert.assertEquals(decrypted, payload)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testHashVector() {
|
||||
val payload = "{what:payloadYouWantToProtect}"
|
||||
val hash = cryptoUtil.sha256(payload)
|
||||
Assert.assertEquals(hash, "a1aafe3ed6cc127e6d102ddbc40a205147230e9cfd178daf108c83543bbdcd13")
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testHmac() {
|
||||
val payload = "{what:payloadYouWantToProtect}"
|
||||
val password = "topSikret"
|
||||
val expectedHmac = "ea2213953d0f2e55047cae2d23fb4f0de1b805d55e6271efa70d6b85fb692bea" // generated using other HMAC tool
|
||||
val hash = cryptoUtil.hmac256(payload, password)
|
||||
Assert.assertEquals(hash, expectedHmac)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testPlainPasswordCheck() {
|
||||
Assert.assertTrue(cryptoUtil.checkPassword("same", "same"))
|
||||
Assert.assertFalse(cryptoUtil.checkPassword("same", "other"))
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testHashedPasswordCheck() {
|
||||
Assert.assertTrue(cryptoUtil.checkPassword("givenSecret", cryptoUtil.hashPassword("givenSecret")))
|
||||
Assert.assertFalse(cryptoUtil.checkPassword("givenSecret", cryptoUtil.hashPassword("otherSecret")))
|
||||
|
||||
Assert.assertTrue(cryptoUtil.checkPassword("givenHashToCheck", "hmac:7fe5f9c7b4b97c5d32d5cfad9d07473543a9938dc07af48a46dbbb49f4f68c12:a0c7cee14312bbe31b51359a67f0d2dfdf46813f319180269796f1f617a64be1"))
|
||||
Assert.assertFalse(cryptoUtil.checkPassword("givenMashToCheck", "hmac:7fe5f9c7b4b97c5d32d5cfad9d07473543a9938dc07af48a46dbbb49f4f68c12:a0c7cee14312bbe31b51359a67f0d2dfdf46813f319180269796f1f617a64be1"))
|
||||
Assert.assertFalse(cryptoUtil.checkPassword("givenHashToCheck", "hmac:0fe5f9c7b4b97c5d32d5cfad9d07473543a9938dc07af48a46dbbb49f4f68c12:a0c7cee14312bbe31b51359a67f0d2dfdf46813f319180269796f1f617a64be1"))
|
||||
Assert.assertFalse(cryptoUtil.checkPassword("givenHashToCheck", "hmac:7fe5f9c7b4b97c5d32d5cfad9d07473543a9938dc07af48a46dbbb49f4f68c12:b0c7cee14312bbe31b51359a67f0d2dfdf46813f319180269796f1f617a64be1"))
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
package info.nightscout.androidaps.utils
|
||||
|
||||
import info.nightscout.androidaps.TestBase
|
||||
import org.hamcrest.CoreMatchers.containsString
|
||||
import org.hamcrest.CoreMatchers.not
|
||||
import org.junit.Assert
|
||||
import org.junit.Assume.assumeThat
|
||||
import org.junit.Test
|
||||
import org.junit.runner.RunWith
|
||||
import org.powermock.core.classloader.annotations.PowerMockIgnore
|
||||
import org.powermock.modules.junit4.PowerMockRunner
|
||||
|
||||
// https://stackoverflow.com/questions/52344522/joseexception-couldnt-create-aes-gcm-nopadding-cipher-illegal-key-size
|
||||
// https://stackoverflow.com/questions/47708951/can-aes-256-work-on-android-devices-with-api-level-26
|
||||
// Java prior to Oracle Java 8u161 does not have policy for 256 bit AES - but Android support it
|
||||
// when test is run in Vanilla JVM without policy - Invalid key size exception is thrown
|
||||
fun assumeAES256isSupported(cryptoUtil: CryptoUtil) {
|
||||
cryptoUtil.lastException?.message?.let { exceptionMessage ->
|
||||
assumeThat("Upgrade your testing environment Java (OpenJDK or Java 8u161) and JAVA_HOME - AES 256 is supported by Android so this exception should not happen!", exceptionMessage, not(containsString("key size")))
|
||||
}
|
||||
}
|
||||
|
||||
@Suppress("SpellCheckingInspection")
|
||||
@PowerMockIgnore("javax.crypto.*")
|
||||
@RunWith(PowerMockRunner::class)
|
||||
class CryptoUtilTest: TestBase() {
|
||||
|
||||
private var cryptoUtil: CryptoUtil = CryptoUtil(aapsLogger)
|
||||
|
||||
@Test
|
||||
fun testFixedSaltCrypto() {
|
||||
val salt = byteArrayOf(
|
||||
-33, -29, 16, -19, 99, -111, -3, 2, 116, 106, 47, 38, -54, 11, -77, 28,
|
||||
111, -15, -65, -110, 4, -32, -29, -70, -95, -88, -53, 19, 87, -103, 123, -15)
|
||||
|
||||
val password = "thisIsFixedPassword"
|
||||
val payload = "FIXED-PAYLOAD"
|
||||
|
||||
val encrypted = cryptoUtil.encrypt(password, salt, payload)
|
||||
assumeAES256isSupported(cryptoUtil)
|
||||
Assert.assertNotNull(encrypted)
|
||||
|
||||
val decrypted = cryptoUtil.decrypt(password, salt, encrypted!!)
|
||||
assumeAES256isSupported(cryptoUtil)
|
||||
Assert.assertEquals(decrypted, payload)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testStandardCrypto() {
|
||||
val salt = cryptoUtil.mineSalt()
|
||||
|
||||
val password = "topSikret"
|
||||
val payload = "{what:payloadYouWantToProtect}"
|
||||
|
||||
val encrypted = cryptoUtil.encrypt(password, salt, payload)
|
||||
assumeAES256isSupported(cryptoUtil)
|
||||
Assert.assertNotNull(encrypted)
|
||||
|
||||
val decrypted = cryptoUtil.decrypt(password, salt, encrypted!!)
|
||||
assumeAES256isSupported(cryptoUtil)
|
||||
Assert.assertEquals(decrypted, payload)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testHashVector() {
|
||||
val payload = "{what:payloadYouWantToProtect}"
|
||||
val hash = cryptoUtil.sha256(payload)
|
||||
Assert.assertEquals(hash, "a1aafe3ed6cc127e6d102ddbc40a205147230e9cfd178daf108c83543bbdcd13")
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testHmac() {
|
||||
val payload = "{what:payloadYouWantToProtect}"
|
||||
val password = "topSikret"
|
||||
val expectedHmac = "ea2213953d0f2e55047cae2d23fb4f0de1b805d55e6271efa70d6b85fb692bea" // generated using other HMAC tool
|
||||
val hash = cryptoUtil.hmac256(payload, password)
|
||||
Assert.assertEquals(hash, expectedHmac)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testPlainPasswordCheck() {
|
||||
Assert.assertTrue(cryptoUtil.checkPassword("same", "same"))
|
||||
Assert.assertFalse(cryptoUtil.checkPassword("same", "other"))
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testHashedPasswordCheck() {
|
||||
Assert.assertTrue(cryptoUtil.checkPassword("givenSecret", cryptoUtil.hashPassword("givenSecret")))
|
||||
Assert.assertFalse(cryptoUtil.checkPassword("givenSecret", cryptoUtil.hashPassword("otherSecret")))
|
||||
|
||||
Assert.assertTrue(cryptoUtil.checkPassword("givenHashToCheck", "hmac:7fe5f9c7b4b97c5d32d5cfad9d07473543a9938dc07af48a46dbbb49f4f68c12:a0c7cee14312bbe31b51359a67f0d2dfdf46813f319180269796f1f617a64be1"))
|
||||
Assert.assertFalse(cryptoUtil.checkPassword("givenMashToCheck", "hmac:7fe5f9c7b4b97c5d32d5cfad9d07473543a9938dc07af48a46dbbb49f4f68c12:a0c7cee14312bbe31b51359a67f0d2dfdf46813f319180269796f1f617a64be1"))
|
||||
Assert.assertFalse(cryptoUtil.checkPassword("givenHashToCheck", "hmac:0fe5f9c7b4b97c5d32d5cfad9d07473543a9938dc07af48a46dbbb49f4f68c12:a0c7cee14312bbe31b51359a67f0d2dfdf46813f319180269796f1f617a64be1"))
|
||||
Assert.assertFalse(cryptoUtil.checkPassword("givenHashToCheck", "hmac:7fe5f9c7b4b97c5d32d5cfad9d07473543a9938dc07af48a46dbbb49f4f68c12:b0c7cee14312bbe31b51359a67f0d2dfdf46813f319180269796f1f617a64be1"))
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -2,7 +2,7 @@ package info.nightscout.androidaps.utils
|
|||
|
||||
import android.content.Context
|
||||
import info.nightscout.androidaps.TestBase
|
||||
import info.nightscout.androidaps.R
|
||||
import info.nightscout.androidaps.core.R
|
||||
import info.nightscout.androidaps.utils.resources.ResourceHelper
|
||||
import org.junit.Assert
|
||||
import org.junit.Test
|
|
@ -0,0 +1,30 @@
|
|||
package info.nightscout.androidaps.utils
|
||||
|
||||
import org.junit.Assert
|
||||
import org.junit.Test
|
||||
import org.junit.runner.RunWith
|
||||
import org.powermock.modules.junit4.PowerMockRunner
|
||||
|
||||
@RunWith(PowerMockRunner::class)
|
||||
class DecimalFormatterTest {
|
||||
|
||||
@Test fun to0DecimalTest() {
|
||||
Assert.assertEquals("1", DecimalFormatter.to0Decimal(1.33).replace(",", "."))
|
||||
Assert.assertEquals("1U", DecimalFormatter.to0Decimal(1.33, "U").replace(",", "."))
|
||||
}
|
||||
|
||||
@Test fun to1DecimalTest() {
|
||||
Assert.assertEquals("1.3", DecimalFormatter.to1Decimal(1.33).replace(",", "."))
|
||||
Assert.assertEquals("1.3U", DecimalFormatter.to1Decimal(1.33, "U").replace(",", "."))
|
||||
}
|
||||
|
||||
@Test fun to2DecimalTest() {
|
||||
Assert.assertEquals("1.33", DecimalFormatter.to2Decimal(1.3333).replace(",", "."))
|
||||
Assert.assertEquals("1.33U", DecimalFormatter.to2Decimal(1.3333, "U").replace(",", "."))
|
||||
}
|
||||
|
||||
@Test fun to3DecimalTest() {
|
||||
Assert.assertEquals("1.333", DecimalFormatter.to3Decimal(1.3333).replace(",", "."))
|
||||
Assert.assertEquals("1.333U", DecimalFormatter.to3Decimal(1.3333, "U").replace(",", "."))
|
||||
}
|
||||
}
|
|
@ -0,0 +1,82 @@
|
|||
package info.nightscout.androidaps.utils
|
||||
|
||||
import org.json.JSONObject
|
||||
import org.junit.Assert
|
||||
import org.junit.Test
|
||||
import org.junit.runner.RunWith
|
||||
import org.powermock.modules.junit4.PowerMockRunner
|
||||
|
||||
@Suppress("SpellCheckingInspection")
|
||||
@RunWith(PowerMockRunner::class)
|
||||
class JsonHelperTest {
|
||||
|
||||
private val jsonString = "{\"d\":\"3.0\",\"i\":\"4\",\"s\":\"5\",\"b\":\"true\",\"j\":{\"a\": \"1\"}}"
|
||||
|
||||
@Test
|
||||
fun safeGetObjectTest() {
|
||||
val json = JSONObject(jsonString)
|
||||
val o = Any()
|
||||
Assert.assertEquals(o, JsonHelper.safeGetObject(null, "x", o))
|
||||
Assert.assertEquals(o, JsonHelper.safeGetObject(json, "x", o))
|
||||
Assert.assertNotEquals(o, JsonHelper.safeGetObject(json, "d", o))
|
||||
}
|
||||
|
||||
@Test
|
||||
fun safeGetJSONObjectTest() {
|
||||
val json = JSONObject(jsonString)
|
||||
val o = JSONObject()
|
||||
Assert.assertEquals(o, JsonHelper.safeGetJSONObject(null, "x", o))
|
||||
Assert.assertTrue(JsonHelper.safeGetJSONObject(json, "j", o)!!.has("a"))
|
||||
Assert.assertEquals(o, JsonHelper.safeGetJSONObject(json, "d", o))
|
||||
}
|
||||
|
||||
@Test
|
||||
fun safeGetStringTest() {
|
||||
val json = JSONObject(jsonString)
|
||||
Assert.assertNull(JsonHelper.safeGetString(null, "s"))
|
||||
Assert.assertNull(JsonHelper.safeGetString(json, "notexisting"))
|
||||
Assert.assertEquals("5", JsonHelper.safeGetString(json, "s"))
|
||||
Assert.assertEquals("default", JsonHelper.safeGetString(null, "notexisting", "default"))
|
||||
Assert.assertEquals("default", JsonHelper.safeGetString(json, "notexisting", "default"))
|
||||
Assert.assertEquals("5", JsonHelper.safeGetString(json, "s", "default"))
|
||||
Assert.assertEquals("default", JsonHelper.safeGetStringAllowNull(null, "notexisting", "default"))
|
||||
Assert.assertEquals("default", JsonHelper.safeGetStringAllowNull(json, "notexisting", "default"))
|
||||
Assert.assertNull(JsonHelper.safeGetStringAllowNull(json, "notexisting", null))
|
||||
Assert.assertEquals("5", JsonHelper.safeGetStringAllowNull(json, "s", "default"))
|
||||
}
|
||||
|
||||
@Test
|
||||
fun safeGetDoubleTest() {
|
||||
val json = JSONObject(jsonString)
|
||||
Assert.assertEquals(0.0, JsonHelper.safeGetDouble(json, "notexisting"), 0.0)
|
||||
Assert.assertEquals(0.0, JsonHelper.safeGetDouble(null, "notexisting"), 0.0)
|
||||
Assert.assertEquals(3.0, JsonHelper.safeGetDouble(json, "d"), 0.000001)
|
||||
Assert.assertEquals(6.0, JsonHelper.safeGetDouble(null, "notexisting", 6.0), 0.0)
|
||||
Assert.assertEquals(6.0, JsonHelper.safeGetDouble(json, "notexisting", 6.0), 0.0)
|
||||
Assert.assertEquals(3.0, JsonHelper.safeGetDouble(json, "d", 6.0), 0.0)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun safeGetLntTest() {
|
||||
val json = JSONObject(jsonString)
|
||||
Assert.assertEquals(0, JsonHelper.safeGetInt(null, "notexisting").toLong())
|
||||
Assert.assertEquals(0, JsonHelper.safeGetInt(json, "notexisting").toLong())
|
||||
Assert.assertEquals(4, JsonHelper.safeGetInt(json, "i").toLong())
|
||||
}
|
||||
|
||||
@Test
|
||||
fun safeGetLongTest() {
|
||||
val json = JSONObject(jsonString)
|
||||
Assert.assertEquals(0, JsonHelper.safeGetInt(null, "notexisting").toLong())
|
||||
Assert.assertEquals(0, JsonHelper.safeGetInt(json, "notexisting").toLong())
|
||||
Assert.assertEquals(4, JsonHelper.safeGetInt(json, "i").toLong())
|
||||
}
|
||||
|
||||
@Test
|
||||
fun safeGetBooleanTest() {
|
||||
val json = JSONObject(jsonString)
|
||||
Assert.assertFalse(JsonHelper.safeGetBoolean(null, "notexisting"))
|
||||
Assert.assertFalse(JsonHelper.safeGetBoolean(json, "notexisting"))
|
||||
Assert.assertTrue(JsonHelper.safeGetBoolean(json, "b"))
|
||||
}
|
||||
}
|
|
@ -32,4 +32,9 @@ class RoundTest {
|
|||
Assert.assertEquals(2.0, Round.ceilTo(1.49999, 1.0), 0.00000001)
|
||||
Assert.assertEquals(0.0, Round.ceilTo(0.0, 1.0), 0.00000001)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun isSameTest() {
|
||||
Assert.assertTrue(Round.isSame(0.54, 0.54))
|
||||
}
|
||||
}
|
|
@ -0,0 +1,18 @@
|
|||
package info.nightscout.androidaps.utils
|
||||
|
||||
import org.junit.Assert
|
||||
import org.junit.Test
|
||||
|
||||
class StringUtilsTest {
|
||||
|
||||
@Test fun removeSurroundingQuotesTest() {
|
||||
var compareString = "test"
|
||||
Assert.assertEquals(compareString, StringUtils.removeSurroundingQuotes(compareString))
|
||||
Assert.assertEquals(compareString, StringUtils.removeSurroundingQuotes("\"" + compareString + "\""))
|
||||
Assert.assertEquals("\"" + compareString, StringUtils.removeSurroundingQuotes("\"" + compareString))
|
||||
compareString = "te\"st"
|
||||
Assert.assertEquals(compareString, StringUtils.removeSurroundingQuotes(compareString))
|
||||
Assert.assertEquals(compareString, StringUtils.removeSurroundingQuotes("\"" + compareString + "\""))
|
||||
Assert.assertEquals("\"" + compareString, StringUtils.removeSurroundingQuotes("\"" + compareString))
|
||||
}
|
||||
}
|
40
core/src/test/java/info/nightscout/androidaps/utils/TTest.kt
Normal file
40
core/src/test/java/info/nightscout/androidaps/utils/TTest.kt
Normal file
|
@ -0,0 +1,40 @@
|
|||
package info.nightscout.androidaps.utils
|
||||
|
||||
import org.junit.Assert
|
||||
import org.junit.Test
|
||||
import kotlin.math.abs
|
||||
|
||||
@Suppress("SpellCheckingInspection")
|
||||
class TTest {
|
||||
|
||||
@Test fun toUnits() {
|
||||
Assert.assertEquals(1, T.msecs(1000).secs())
|
||||
Assert.assertEquals(1, T.secs(60).mins())
|
||||
Assert.assertEquals(1, T.mins(60).hours())
|
||||
Assert.assertEquals(1, T.hours(24).days())
|
||||
Assert.assertEquals(24, T.days(1).hours())
|
||||
Assert.assertEquals(60000, T.mins(1).msecs())
|
||||
}
|
||||
|
||||
@Test fun now() {
|
||||
Assert.assertTrue(abs(T.now().msecs() - System.currentTimeMillis()) < 5000)
|
||||
}
|
||||
|
||||
@Test fun additions() {
|
||||
val nowMsecs = System.currentTimeMillis()
|
||||
val now = T.msecs(nowMsecs)
|
||||
Assert.assertEquals(now.plus(T.secs(5)).msecs(), nowMsecs + 5 * 1000)
|
||||
Assert.assertEquals(now.plus(T.mins(5)).msecs(), nowMsecs + 5 * 60 * 1000)
|
||||
Assert.assertEquals(now.plus(T.hours(5)).msecs(), nowMsecs + 5 * 60 * 60 * 1000)
|
||||
Assert.assertEquals(now.plus(T.days(5)).msecs(), nowMsecs + 5 * 24 * 60 * 60 * 1000)
|
||||
}
|
||||
|
||||
@Test fun subtractions() {
|
||||
val nowMsecs = System.currentTimeMillis()
|
||||
val now = T.msecs(nowMsecs)
|
||||
Assert.assertEquals(now.minus(T.secs(5)).msecs(), nowMsecs - 5 * 1000)
|
||||
Assert.assertEquals(now.minus(T.mins(5)).msecs(), nowMsecs - 5 * 60 * 1000)
|
||||
Assert.assertEquals(now.minus(T.hours(5)).msecs(), nowMsecs - 5 * 60 * 60 * 1000)
|
||||
Assert.assertEquals(now.minus(T.days(5)).msecs(), nowMsecs - 5 * 24 * 60 * 60 * 1000)
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue