shared-tests #6

This commit is contained in:
Milos Kozak 2023-09-03 21:26:22 +02:00
parent d1694e9763
commit 53beed4e14
13 changed files with 120 additions and 201 deletions

View file

@ -25,6 +25,9 @@ dependencies {
implementation project(':core:utils') implementation project(':core:utils')
implementation project(':core:ui') implementation project(':core:ui')
implementation project(':core:validators') implementation project(':core:validators')
testImplementation project(':app-wear-shared:shared-tests')
//WorkManager //WorkManager
api "androidx.work:work-runtime-ktx:$work_version" api "androidx.work:work-runtime-ktx:$work_version"
// Maintenance // Maintenance

View file

@ -1,37 +0,0 @@
package info.nightscout.androidaps
import info.nightscout.rx.AapsSchedulers
import info.nightscout.rx.TestAapsSchedulers
import info.nightscout.rx.logging.AAPSLoggerTest
import org.junit.Before
import org.junit.jupiter.api.extension.ExtendWith
import org.mockito.Mockito
import org.mockito.junit.jupiter.MockitoExtension
import org.mockito.junit.jupiter.MockitoSettings
import org.mockito.quality.Strictness
import java.util.Locale
@ExtendWith(MockitoExtension::class)
@MockitoSettings(strictness = Strictness.LENIENT)
open class TestBase {
val aapsLogger = AAPSLoggerTest()
val aapsSchedulers: AapsSchedulers = TestAapsSchedulers()
@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
}

View file

@ -1,6 +1,5 @@
package info.nightscout.configuration.maintenance.formats package info.nightscout.configuration.maintenance.formats
import info.nightscout.androidaps.TestBase
import info.nightscout.core.utils.CryptoUtil import info.nightscout.core.utils.CryptoUtil
import info.nightscout.interfaces.maintenance.PrefFormatError import info.nightscout.interfaces.maintenance.PrefFormatError
import info.nightscout.interfaces.maintenance.PrefMetadata import info.nightscout.interfaces.maintenance.PrefMetadata
@ -9,10 +8,10 @@ import info.nightscout.interfaces.maintenance.PrefsFormat
import info.nightscout.interfaces.maintenance.PrefsMetadataKey import info.nightscout.interfaces.maintenance.PrefsMetadataKey
import info.nightscout.interfaces.maintenance.PrefsStatus import info.nightscout.interfaces.maintenance.PrefsStatus
import info.nightscout.shared.interfaces.ResourceHelper import info.nightscout.shared.interfaces.ResourceHelper
import info.nightscout.sharedtests.TestBase
import org.hamcrest.CoreMatchers import org.hamcrest.CoreMatchers
import org.junit.Assert
import org.junit.Assume import org.junit.Assume
import org.junit.Before import org.junit.jupiter.api.Assertions
import org.junit.jupiter.api.BeforeEach import org.junit.jupiter.api.BeforeEach
import org.junit.jupiter.api.Test import org.junit.jupiter.api.Test
import org.mockito.ArgumentMatchers import org.mockito.ArgumentMatchers
@ -63,13 +62,13 @@ open class EncryptedPrefsFormatTest : TestBase() {
assumeAES256isSupported(cryptoUtil) assumeAES256isSupported(cryptoUtil)
Assert.assertEquals(prefs.values.size, 2) Assertions.assertEquals(prefs.values.size, 2)
Assert.assertEquals(prefs.values["key1"], "A") Assertions.assertEquals(prefs.values["key1"], "A")
Assert.assertEquals(prefs.values["keyB"], "2") Assertions.assertEquals(prefs.values["keyB"], "2")
Assert.assertEquals(prefs.metadata[PrefsMetadataKey.FILE_FORMAT]?.status, PrefsStatus.OK) Assertions.assertEquals(prefs.metadata[PrefsMetadataKey.FILE_FORMAT]?.status, PrefsStatus.OK)
Assert.assertEquals(prefs.metadata[PrefsMetadataKey.FILE_FORMAT]?.value, PrefsFormat.FORMAT_KEY_ENC) Assertions.assertEquals(prefs.metadata[PrefsMetadataKey.FILE_FORMAT]?.value, PrefsFormat.FORMAT_KEY_ENC)
Assert.assertEquals(prefs.metadata[PrefsMetadataKey.ENCRYPTION]?.status, PrefsStatus.OK) Assertions.assertEquals(prefs.metadata[PrefsMetadataKey.ENCRYPTION]?.status, PrefsStatus.OK)
} }
@Test @Test
@ -107,13 +106,13 @@ open class EncryptedPrefsFormatTest : TestBase() {
assumeAES256isSupported(cryptoUtil) assumeAES256isSupported(cryptoUtil)
Assert.assertEquals(prefsOut.values.size, 2) Assertions.assertEquals(prefsOut.values.size, 2)
Assert.assertEquals(prefsOut.values["testpref1"], "--1--") Assertions.assertEquals(prefsOut.values["testpref1"], "--1--")
Assert.assertEquals(prefsOut.values["testpref2"], "another") Assertions.assertEquals(prefsOut.values["testpref2"], "another")
Assert.assertEquals(prefsOut.metadata[PrefsMetadataKey.FILE_FORMAT]?.status, PrefsStatus.OK) Assertions.assertEquals(prefsOut.metadata[PrefsMetadataKey.FILE_FORMAT]?.status, PrefsStatus.OK)
Assert.assertEquals(prefsOut.metadata[PrefsMetadataKey.FILE_FORMAT]?.value, PrefsFormat.FORMAT_KEY_ENC) Assertions.assertEquals(prefsOut.metadata[PrefsMetadataKey.FILE_FORMAT]?.value, PrefsFormat.FORMAT_KEY_ENC)
Assert.assertEquals(prefsOut.metadata[PrefsMetadataKey.ENCRYPTION]?.status, PrefsStatus.OK) Assertions.assertEquals(prefsOut.metadata[PrefsMetadataKey.ENCRYPTION]?.status, PrefsStatus.OK)
} }
@Test @Test
@ -134,11 +133,11 @@ open class EncryptedPrefsFormatTest : TestBase() {
val encryptedFormat = EncryptedPrefsFormat(rh, cryptoUtil, storage) val encryptedFormat = EncryptedPrefsFormat(rh, cryptoUtil, storage)
val prefs = encryptedFormat.loadPreferences(getMockedFile(), "it-is-NOT-right-secret") val prefs = encryptedFormat.loadPreferences(getMockedFile(), "it-is-NOT-right-secret")
Assert.assertEquals(prefs.values.size, 0) Assertions.assertEquals(prefs.values.size, 0)
Assert.assertEquals(prefs.metadata[PrefsMetadataKey.FILE_FORMAT]?.status, PrefsStatus.OK) Assertions.assertEquals(prefs.metadata[PrefsMetadataKey.FILE_FORMAT]?.status, PrefsStatus.OK)
Assert.assertEquals(prefs.metadata[PrefsMetadataKey.FILE_FORMAT]?.value, PrefsFormat.FORMAT_KEY_ENC) Assertions.assertEquals(prefs.metadata[PrefsMetadataKey.FILE_FORMAT]?.value, PrefsFormat.FORMAT_KEY_ENC)
Assert.assertEquals(prefs.metadata[PrefsMetadataKey.ENCRYPTION]?.status, PrefsStatus.ERROR) Assertions.assertEquals(prefs.metadata[PrefsMetadataKey.ENCRYPTION]?.status, PrefsStatus.ERROR)
} }
@Test @Test
@ -164,10 +163,10 @@ open class EncryptedPrefsFormatTest : TestBase() {
assumeAES256isSupported(cryptoUtil) assumeAES256isSupported(cryptoUtil)
// contents were not tampered and we can decrypt them // contents were not tampered and we can decrypt them
Assert.assertEquals(prefs.values.size, 2) Assertions.assertEquals(prefs.values.size, 2)
// but checksum fails on metadata, so overall security fails // but checksum fails on metadata, so overall security fails
Assert.assertEquals(prefs.metadata[PrefsMetadataKey.ENCRYPTION]?.status, PrefsStatus.ERROR) Assertions.assertEquals(prefs.metadata[PrefsMetadataKey.ENCRYPTION]?.status, PrefsStatus.ERROR)
} }
@Test @Test
@ -188,8 +187,8 @@ open class EncryptedPrefsFormatTest : TestBase() {
val encryptedFormat = EncryptedPrefsFormat(rh, cryptoUtil, storage) val encryptedFormat = EncryptedPrefsFormat(rh, cryptoUtil, storage)
val prefs = encryptedFormat.loadPreferences(getMockedFile(), "sikret") val prefs = encryptedFormat.loadPreferences(getMockedFile(), "sikret")
Assert.assertEquals(prefs.values.size, 0) Assertions.assertEquals(prefs.values.size, 0)
Assert.assertEquals(prefs.metadata[PrefsMetadataKey.ENCRYPTION]?.status, PrefsStatus.ERROR) Assertions.assertEquals(prefs.metadata[PrefsMetadataKey.ENCRYPTION]?.status, PrefsStatus.ERROR)
} }
@Test @Test
@ -203,13 +202,13 @@ open class EncryptedPrefsFormatTest : TestBase() {
val encryptedFormat = EncryptedPrefsFormat(rh, cryptoUtil, storage) val encryptedFormat = EncryptedPrefsFormat(rh, cryptoUtil, storage)
val prefs = encryptedFormat.loadPreferences(getMockedFile(), "sikret") val prefs = encryptedFormat.loadPreferences(getMockedFile(), "sikret")
Assert.assertEquals(prefs.values.size, 0) Assertions.assertEquals(prefs.values.size, 0)
Assert.assertEquals(prefs.metadata[PrefsMetadataKey.FILE_FORMAT]?.status, PrefsStatus.ERROR) Assertions.assertEquals(prefs.metadata[PrefsMetadataKey.FILE_FORMAT]?.status, PrefsStatus.ERROR)
} }
@Test @Test
fun garbageInputTest() { fun garbageInputTest() {
Assert.assertThrows(PrefFormatError::class.java) { Assertions.assertThrows(PrefFormatError::class.java) {
val frozenPrefs = "whatever man, i duno care" val frozenPrefs = "whatever man, i duno care"
val storage = SingleStringStorage(frozenPrefs) val storage = SingleStringStorage(frozenPrefs)
@ -220,7 +219,7 @@ open class EncryptedPrefsFormatTest : TestBase() {
@Test @Test
fun unknownFormatTest() { fun unknownFormatTest() {
Assert.assertThrows(PrefFormatError::class.java) { Assertions.assertThrows(PrefFormatError::class.java) {
val frozenPrefs = "{\n" + val frozenPrefs = "{\n" +
" \"metadata\": {},\n" + " \"metadata\": {},\n" +
" \"security\": {\n" + " \"security\": {\n" +

View file

@ -23,6 +23,8 @@ dependencies {
implementation project(':core:validators') implementation project(':core:validators')
implementation project(':database:entities') implementation project(':database:entities')
testImplementation project(':app-wear-shared:shared-tests')
// Phone checker // Phone checker
api 'com.scottyab:rootbeer-lib:0.1.0' api 'com.scottyab:rootbeer-lib:0.1.0'
} }

View file

@ -1,37 +0,0 @@
package info.nightscout.androidaps
import info.nightscout.rx.AapsSchedulers
import info.nightscout.rx.TestAapsSchedulers
import info.nightscout.rx.logging.AAPSLoggerTest
import org.junit.Before
import org.junit.jupiter.api.extension.ExtendWith
import org.mockito.Mockito
import org.mockito.junit.jupiter.MockitoExtension
import org.mockito.junit.jupiter.MockitoSettings
import org.mockito.quality.Strictness
import java.util.Locale
@ExtendWith(MockitoExtension::class)
@MockitoSettings(strictness = Strictness.LENIENT)
open class TestBase {
val aapsLogger = AAPSLoggerTest()
val aapsSchedulers: AapsSchedulers = TestAapsSchedulers()
@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
}

View file

@ -2,7 +2,6 @@ package info.nightscout.plugins.constraints.bgQualityCheck
import dagger.android.AndroidInjector import dagger.android.AndroidInjector
import dagger.android.HasAndroidInjector import dagger.android.HasAndroidInjector
import info.nightscout.androidaps.TestBase
import info.nightscout.core.utils.fabric.FabricPrivacy import info.nightscout.core.utils.fabric.FabricPrivacy
import info.nightscout.database.entities.GlucoseValue import info.nightscout.database.entities.GlucoseValue
import info.nightscout.interfaces.aps.AutosensDataStore import info.nightscout.interfaces.aps.AutosensDataStore
@ -16,6 +15,7 @@ import info.nightscout.rx.bus.RxBus
import info.nightscout.shared.interfaces.ResourceHelper import info.nightscout.shared.interfaces.ResourceHelper
import info.nightscout.shared.utils.DateUtil import info.nightscout.shared.utils.DateUtil
import info.nightscout.shared.utils.T import info.nightscout.shared.utils.T
import info.nightscout.sharedtests.TestBase
import org.junit.jupiter.api.Assertions import org.junit.jupiter.api.Assertions
import org.junit.jupiter.api.BeforeEach import org.junit.jupiter.api.BeforeEach
import org.junit.jupiter.api.Test import org.junit.jupiter.api.Test

View file

@ -2,12 +2,12 @@ package info.nightscout.plugins.constraints.dstHelper
import dagger.android.AndroidInjector import dagger.android.AndroidInjector
import dagger.android.HasAndroidInjector import dagger.android.HasAndroidInjector
import info.nightscout.androidaps.TestBase
import info.nightscout.interfaces.aps.Loop import info.nightscout.interfaces.aps.Loop
import info.nightscout.interfaces.plugin.ActivePlugin import info.nightscout.interfaces.plugin.ActivePlugin
import info.nightscout.shared.interfaces.ResourceHelper import info.nightscout.shared.interfaces.ResourceHelper
import info.nightscout.shared.sharedPreferences.SP import info.nightscout.shared.sharedPreferences.SP
import org.junit.Assert import info.nightscout.sharedtests.TestBase
import org.junit.jupiter.api.Assertions
import org.junit.jupiter.api.BeforeEach import org.junit.jupiter.api.BeforeEach
import org.junit.jupiter.api.Test import org.junit.jupiter.api.Test
import org.mockito.Mock import org.mockito.Mock
@ -34,38 +34,38 @@ class DstHelperPluginTest : TestBase() {
} }
@Test @Test
private fun runTest() { fun runTest() {
val tz = TimeZone.getTimeZone("Europe/Rome") val tz = TimeZone.getTimeZone("Europe/Rome")
TimeZone.setDefault(tz) TimeZone.setDefault(tz)
var cal = Calendar.getInstance(tz, Locale.ITALIAN) var cal = Calendar.getInstance(tz, Locale.ITALIAN)
val df: DateFormat = SimpleDateFormat("yyyy-MM-dd HH:mm", Locale.ITALIAN) val df: DateFormat = SimpleDateFormat("yyyy-MM-dd HH:mm", Locale.ITALIAN)
var dateBeforeDST = df.parse("2018-03-25 01:55") var dateBeforeDST = df.parse("2018-03-25 01:55")
cal.time = dateBeforeDST!! cal.time = dateBeforeDST!!
Assert.assertEquals(false, plugin.wasDST(cal)) Assertions.assertEquals(false, plugin.wasDST(cal))
Assert.assertEquals(true, plugin.willBeDST(cal)) Assertions.assertEquals(true, plugin.willBeDST(cal))
TimeZone.setDefault(tz) TimeZone.setDefault(tz)
cal = Calendar.getInstance(tz, Locale.ITALIAN) cal = Calendar.getInstance(tz, Locale.ITALIAN)
dateBeforeDST = df.parse("2018-03-25 03:05") dateBeforeDST = df.parse("2018-03-25 03:05")
cal.time = dateBeforeDST!! cal.time = dateBeforeDST!!
Assert.assertEquals(true, plugin.wasDST(cal)) Assertions.assertEquals(true, plugin.wasDST(cal))
Assert.assertEquals(false, plugin.willBeDST(cal)) Assertions.assertEquals(false, plugin.willBeDST(cal))
TimeZone.setDefault(tz) TimeZone.setDefault(tz)
cal = Calendar.getInstance(tz, Locale.ITALIAN) cal = Calendar.getInstance(tz, Locale.ITALIAN)
dateBeforeDST = df.parse("2018-03-25 02:05") //Cannot happen!!! dateBeforeDST = df.parse("2018-03-25 02:05") //Cannot happen!!!
cal.time = dateBeforeDST!! cal.time = dateBeforeDST!!
Assert.assertEquals(true, plugin.wasDST(cal)) Assertions.assertEquals(true, plugin.wasDST(cal))
Assert.assertEquals(false, plugin.willBeDST(cal)) Assertions.assertEquals(false, plugin.willBeDST(cal))
TimeZone.setDefault(tz) TimeZone.setDefault(tz)
cal = Calendar.getInstance(tz, Locale.ITALIAN) cal = Calendar.getInstance(tz, Locale.ITALIAN)
dateBeforeDST = df.parse("2018-03-25 05:55") //Cannot happen!!! dateBeforeDST = df.parse("2018-03-25 05:55") //Cannot happen!!!
cal.time = dateBeforeDST!! cal.time = dateBeforeDST!!
Assert.assertEquals(true, plugin.wasDST(cal)) Assertions.assertEquals(true, plugin.wasDST(cal))
Assert.assertEquals(false, plugin.willBeDST(cal)) Assertions.assertEquals(false, plugin.willBeDST(cal))
TimeZone.setDefault(tz) TimeZone.setDefault(tz)
cal = Calendar.getInstance(tz, Locale.ITALIAN) cal = Calendar.getInstance(tz, Locale.ITALIAN)
dateBeforeDST = df.parse("2018-03-25 06:05") //Cannot happen!!! dateBeforeDST = df.parse("2018-03-25 06:05") //Cannot happen!!!
cal.time = dateBeforeDST!! cal.time = dateBeforeDST!!
Assert.assertEquals(false, plugin.wasDST(cal)) Assertions.assertEquals(false, plugin.wasDST(cal))
Assert.assertEquals(false, plugin.willBeDST(cal)) Assertions.assertEquals(false, plugin.willBeDST(cal))
} }
} }

View file

@ -2,15 +2,16 @@ package info.nightscout.plugins.constraints.objectives
import dagger.android.AndroidInjector import dagger.android.AndroidInjector
import dagger.android.HasAndroidInjector import dagger.android.HasAndroidInjector
import info.nightscout.androidaps.TestBase
import info.nightscout.interfaces.Config import info.nightscout.interfaces.Config
import info.nightscout.interfaces.constraints.Constraint import info.nightscout.interfaces.constraints.Constraint
import info.nightscout.interfaces.constraints.Objectives import info.nightscout.interfaces.constraints.Objectives
import info.nightscout.interfaces.plugin.ActivePlugin import info.nightscout.interfaces.plugin.ActivePlugin
import info.nightscout.plugins.constraints.R import info.nightscout.plugins.constraints.R
import info.nightscout.plugins.constraints.objectives.objectives.Objective
import info.nightscout.shared.interfaces.ResourceHelper import info.nightscout.shared.interfaces.ResourceHelper
import info.nightscout.shared.sharedPreferences.SP import info.nightscout.shared.sharedPreferences.SP
import info.nightscout.shared.utils.DateUtil import info.nightscout.shared.utils.DateUtil
import info.nightscout.sharedtests.TestBase
import org.junit.jupiter.api.Assertions import org.junit.jupiter.api.Assertions
import org.junit.jupiter.api.BeforeEach import org.junit.jupiter.api.BeforeEach
import org.junit.jupiter.api.Test import org.junit.jupiter.api.Test
@ -27,9 +28,9 @@ class ObjectivesPluginTest : TestBase() {
private lateinit var objectivesPlugin: ObjectivesPlugin private lateinit var objectivesPlugin: ObjectivesPlugin
val injector = HasAndroidInjector { private val injector = HasAndroidInjector {
AndroidInjector { AndroidInjector {
if (it is info.nightscout.plugins.constraints.objectives.objectives.Objective) { if (it is Objective) {
it.sp = sp it.sp = sp
it.rh = rh it.rh = rh
it.dateUtil = dateUtil it.dateUtil = dateUtil

View file

@ -1,30 +1,31 @@
package info.nightscout.plugins.constraints.objectives package info.nightscout.plugins.constraints.objectives
import info.nightscout.androidaps.TestBase
import info.nightscout.shared.utils.DateUtil import info.nightscout.shared.utils.DateUtil
import org.junit.Assert import info.nightscout.sharedtests.TestBase
import org.junit.jupiter.api.Assertions
import org.junit.jupiter.api.Test import org.junit.jupiter.api.Test
import org.mockito.Mock import org.mockito.Mock
import kotlin.math.abs import kotlin.math.abs
class SntpClientTest : TestBase() { class SntpClientTest : TestBase() {
@Mock lateinit var dateUtil: DateUtil @Mock lateinit var dateUtil: DateUtil
@Test @Test
fun ntpTimeTest() { fun ntpTimeTest() {
// no internet // no internet
info.nightscout.plugins.constraints.objectives.SntpClient(aapsLogger, dateUtil).ntpTime(object : info.nightscout.plugins.constraints.objectives.SntpClient.Callback() { SntpClient(aapsLogger, dateUtil).ntpTime(object : SntpClient.Callback() {
override fun run() { override fun run() {
Assert.assertFalse(networkConnected) Assertions.assertFalse(networkConnected)
Assert.assertFalse(success) Assertions.assertFalse(success)
Assert.assertEquals(0L, time) Assertions.assertEquals(0L, time)
} }
}, false) }, false)
// internet // internet
info.nightscout.plugins.constraints.objectives.SntpClient(aapsLogger, dateUtil).doNtpTime(object : info.nightscout.plugins.constraints.objectives.SntpClient.Callback() { SntpClient(aapsLogger, dateUtil).doNtpTime(object : SntpClient.Callback() {
override fun run() { override fun run() {
Assert.assertTrue(success) Assertions.assertTrue(success)
Assert.assertTrue(abs(time - System.currentTimeMillis()) < 60000) Assertions.assertTrue(abs(time - System.currentTimeMillis()) < 60000)
} }
}) })
} }

View file

@ -1,26 +1,13 @@
package info.nightscout.plugins.constraints.signatureVerifier package info.nightscout.plugins.constraints.signatureVerifier
import android.content.Context import info.nightscout.sharedtests.TestBase
import dagger.android.AndroidInjector import org.junit.jupiter.api.Assertions
import dagger.android.HasAndroidInjector
import info.nightscout.androidaps.TestBase
import info.nightscout.interfaces.ui.UiInteraction
import info.nightscout.shared.interfaces.ResourceHelper
import info.nightscout.shared.sharedPreferences.SP
import org.junit.Assert
import org.junit.jupiter.api.Test import org.junit.jupiter.api.Test
import org.mockito.Mock
class SignatureVerifierPluginTest : TestBase() { class SignatureVerifierPluginTest : TestBase() {
@Mock lateinit var rh: ResourceHelper @Suppress("SameParameterValue")
@Mock lateinit var sp: SP private fun singleCharUnMap(shortHash: String): String {
@Mock lateinit var context: Context
@Mock lateinit var uiInteraction: UiInteraction
val injector = HasAndroidInjector { AndroidInjector { } }
fun singleCharUnMap(shortHash: String): String {
val array = ByteArray(shortHash.length) val array = ByteArray(shortHash.length)
val sb = StringBuilder() val sb = StringBuilder()
for (i in array.indices) { for (i in array.indices) {
@ -30,13 +17,14 @@ class SignatureVerifierPluginTest : TestBase() {
return sb.toString() return sb.toString()
} }
var map = @Suppress("SpellCheckingInspection")
private var map =
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789!\"§$%&/()=?,.-;:_<>|°^`´\\@€*'#+~{}[]¿¡áéíóúàèìòùöäü`ÁÉÍÓÚÀÈÌÒÙÖÄÜßÆÇÊËÎÏԌ۟æçêëîïôœûÿĆČĐŠŽćđšžñΑΒΓΔΕΖΗΘΙΚΛΜΝΞΟΠΡ\u03A2ΣΤΥΦΧΨΩαβγδεζηθικλμνξοπρςστυφχψωϨϩϪϫϬϭϮϯϰϱϲϳϴϵ϶ϷϸϹϺϻϼϽϾϿЀЁЂЃЄЅІЇЈЉЊЋЌЍЎЏАБВГДЕЖЗ" "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789!\"§$%&/()=?,.-;:_<>|°^`´\\@€*'#+~{}[]¿¡áéíóúàèìòùöäü`ÁÉÍÓÚÀÈÌÒÙÖÄÜßÆÇÊËÎÏԌ۟æçêëîïôœûÿĆČĐŠŽćđšžñΑΒΓΔΕΖΗΘΙΚΛΜΝΞΟΠΡ\u03A2ΣΤΥΦΧΨΩαβγδεζηθικλμνξοπρςστυφχψωϨϩϪϫϬϭϮϯϰϱϲϳϴϵ϶ϷϸϹϺϻϼϽϾϿЀЁЂЃЄЅІЇЈЉЊЋЌЍЎЏАБВГДЕЖЗ"
@Test @Test
fun singleCharUnMapTest() { fun singleCharUnMapTest() {
val key = "2ΙšÄΠΒϨÒÇeЄtЄЗž-*Ж*ZcHijЊÄœ<|x\"Ε" @Suppress("SpellCheckingInspection") val key = "2ΙšÄΠΒϨÒÇeЄtЄЗž-*Ж*ZcHijЊÄœ<|x\"Ε"
val unmapped = singleCharUnMap(key) val unmapped = singleCharUnMap(key)
Assert.assertEquals("32:99:61:C4:A0:92:E8:D2:C7:65:04:74:04:17:7E:2D:2A:16:2A:5A:63:48:69:6A:0A:C4:53:3C:7C:78:22:95", unmapped) Assertions.assertEquals("32:99:61:C4:A0:92:E8:D2:C7:65:04:74:04:17:7E:2D:2A:16:2A:5A:63:48:69:6A:0A:C4:53:3C:7C:78:22:95", unmapped)
} }
} }

View file

@ -1,7 +1,6 @@
package info.nightscout.plugins.constraints.signatureVerifier package info.nightscout.plugins.constraints.signatureVerifier
import dagger.Lazy import dagger.Lazy
import info.nightscout.androidaps.TestBase
import info.nightscout.interfaces.Config import info.nightscout.interfaces.Config
import info.nightscout.interfaces.receivers.ReceiverStatusStore import info.nightscout.interfaces.receivers.ReceiverStatusStore
import info.nightscout.interfaces.ui.UiInteraction import info.nightscout.interfaces.ui.UiInteraction
@ -11,8 +10,8 @@ import info.nightscout.plugins.constraints.versionChecker.numericVersionPart
import info.nightscout.shared.interfaces.ResourceHelper import info.nightscout.shared.interfaces.ResourceHelper
import info.nightscout.shared.sharedPreferences.SP import info.nightscout.shared.sharedPreferences.SP
import info.nightscout.shared.utils.DateUtil import info.nightscout.shared.utils.DateUtil
import org.junit.Assert.assertArrayEquals import info.nightscout.sharedtests.TestBase
import org.junit.Assert.assertEquals import org.junit.jupiter.api.Assertions
import org.junit.jupiter.api.BeforeEach import org.junit.jupiter.api.BeforeEach
import org.junit.jupiter.api.Test import org.junit.jupiter.api.Test
import org.mockito.ArgumentMatchers.anyInt import org.mockito.ArgumentMatchers.anyInt
@ -22,7 +21,8 @@ import org.mockito.Mockito.times
import org.mockito.Mockito.verify import org.mockito.Mockito.verify
import org.mockito.Mockito.`when` import org.mockito.Mockito.`when`
@Suppress("SpellCheckingInspection") class VersionCheckerUtilsKtTest : TestBase() { @Suppress("SpellCheckingInspection")
class VersionCheckerUtilsKtTest : TestBase() {
private lateinit var versionCheckerUtils: VersionCheckerUtils private lateinit var versionCheckerUtils: VersionCheckerUtils
@ -39,97 +39,97 @@ import org.mockito.Mockito.`when`
@Test @Test
fun `should handle invalid version`() { fun `should handle invalid version`() {
assertArrayEquals(intArrayOf(), versionCheckerUtils.versionDigits("definitely not version string")) Assertions.assertArrayEquals(intArrayOf(), versionCheckerUtils.versionDigits("definitely not version string"))
} }
@Test @Test
fun `should handle empty version`() { fun `should handle empty version`() {
assertArrayEquals(intArrayOf(), versionCheckerUtils.versionDigits("")) Assertions.assertArrayEquals(intArrayOf(), versionCheckerUtils.versionDigits(""))
} }
@Test @Test
fun `should parse 2 digit version`() { fun `should parse 2 digit version`() {
assertArrayEquals(intArrayOf(0, 999), versionCheckerUtils.versionDigits("0.999-beta")) Assertions.assertArrayEquals(intArrayOf(0, 999), versionCheckerUtils.versionDigits("0.999-beta"))
} }
@Test @Test
fun `should parse 3 digit version`() { fun `should parse 3 digit version`() {
assertArrayEquals(intArrayOf(6, 83, 93), versionCheckerUtils.versionDigits("6.83.93")) Assertions.assertArrayEquals(intArrayOf(6, 83, 93), versionCheckerUtils.versionDigits("6.83.93"))
} }
@Test @Test
fun `should parse 4 digit version`() { fun `should parse 4 digit version`() {
assertArrayEquals(intArrayOf(42, 7, 13, 101), versionCheckerUtils.versionDigits("42.7.13.101")) Assertions.assertArrayEquals(intArrayOf(42, 7, 13, 101), versionCheckerUtils.versionDigits("42.7.13.101"))
} }
@Test @Test
fun `should parse 4 digit version with extra`() { fun `should parse 4 digit version with extra`() {
assertArrayEquals(intArrayOf(1, 2, 3, 4), versionCheckerUtils.versionDigits("1.2.3.4-RC5")) Assertions.assertArrayEquals(intArrayOf(1, 2, 3, 4), versionCheckerUtils.versionDigits("1.2.3.4-RC5"))
} }
@Test @Test
fun `should parse version but only 4 digits are taken`() { fun `should parse version but only 4 digits are taken`() {
assertArrayEquals(intArrayOf(67, 8, 31, 5), versionCheckerUtils.versionDigits("67.8.31.5.153.4.2")) Assertions.assertArrayEquals(intArrayOf(67, 8, 31, 5), versionCheckerUtils.versionDigits("67.8.31.5.153.4.2"))
} }
@Test @Test
fun `should keep 2 digit version`() { fun `should keep 2 digit version`() {
assertEquals("1.2", "1.2".numericVersionPart()) Assertions.assertEquals("1.2", "1.2".numericVersionPart())
} }
@Test @Test
fun `should keep 3 digit version`() { fun `should keep 3 digit version`() {
assertEquals("1.2.3", "1.2.3".numericVersionPart()) Assertions.assertEquals("1.2.3", "1.2.3".numericVersionPart())
} }
@Test @Test
fun `should keep 4 digit version`() { fun `should keep 4 digit version`() {
assertEquals("1.2.3.4", "1.2.3.4".numericVersionPart()) Assertions.assertEquals("1.2.3.4", "1.2.3.4".numericVersionPart())
} }
@Test @Test
fun `should strip 2 digit version RC`() { fun `should strip 2 digit version RC`() {
assertEquals("1.2", "1.2-RC1".numericVersionPart()) Assertions.assertEquals("1.2", "1.2-RC1".numericVersionPart())
} }
@Test @Test
fun `should strip 2 digit version RC old format`() { fun `should strip 2 digit version RC old format`() {
assertEquals("1.2", "1.2RC1".numericVersionPart()) Assertions.assertEquals("1.2", "1.2RC1".numericVersionPart())
} }
@Test @Test
fun `should strip 2 digit version RC without digit`() { fun `should strip 2 digit version RC without digit`() {
assertEquals("1.2", "1.2-RC".numericVersionPart()) Assertions.assertEquals("1.2", "1.2-RC".numericVersionPart())
} }
@Test @Test
fun `should strip 2 digit version dev`() { fun `should strip 2 digit version dev`() {
assertEquals("1.2", "1.2-dev".numericVersionPart()) Assertions.assertEquals("1.2", "1.2-dev".numericVersionPart())
} }
@Test @Test
fun `should strip 2 digit version dev old format 1`() { fun `should strip 2 digit version dev old format 1`() {
assertEquals("1.2", "1.2dev".numericVersionPart()) Assertions.assertEquals("1.2", "1.2dev".numericVersionPart())
} }
@Test @Test
fun `should strip 2 digit version dev old format 2`() { fun `should strip 2 digit version dev old format 2`() {
assertEquals("1.2", "1.2dev-a3".numericVersionPart()) Assertions.assertEquals("1.2", "1.2dev-a3".numericVersionPart())
} }
@Test @Test
fun `should strip 3 digit version RC`() { fun `should strip 3 digit version RC`() {
assertEquals("1.2.3", "1.2.3-RC1".numericVersionPart()) Assertions.assertEquals("1.2.3", "1.2.3-RC1".numericVersionPart())
} }
@Test @Test
fun `should strip 4 digit version RC`() { fun `should strip 4 digit version RC`() {
assertEquals("1.2.3.4", "1.2.3.4-RC5".numericVersionPart()) Assertions.assertEquals("1.2.3.4", "1.2.3.4-RC5".numericVersionPart())
} }
@Test @Test
fun `should strip even with dot`() { fun `should strip even with dot`() {
assertEquals("1.2", "1.2.RC5".numericVersionPart()) Assertions.assertEquals("1.2", "1.2.RC5".numericVersionPart())
} }
@Suppress("SpellCheckingInspection") @Suppress("SpellCheckingInspection")
@ -143,7 +143,7 @@ import org.mockito.Mockito.`when`
| appName = "Aaoeu" | appName = "Aaoeu"
""".trimMargin() """.trimMargin()
val detectedVersion: String? = versionCheckerUtils.findVersion(buildGradle) val detectedVersion: String? = versionCheckerUtils.findVersion(buildGradle)
assertEquals("2.2.2", detectedVersion) Assertions.assertEquals("2.2.2", detectedVersion)
} }
// In case we merge a "x.x.x-dev" into master, don't see it as update. // In case we merge a "x.x.x-dev" into master, don't see it as update.
@ -157,14 +157,14 @@ import org.mockito.Mockito.`when`
| appName = "Aaoeu" | appName = "Aaoeu"
""".trimMargin() """.trimMargin()
val detectedVersion: String? = versionCheckerUtils.findVersion(buildGradle) val detectedVersion: String? = versionCheckerUtils.findVersion(buildGradle)
assertEquals(null, detectedVersion) Assertions.assertEquals(null, detectedVersion)
} }
@Test @Test
fun findVersionMatchesDoesNotMatchErrorResponse() { fun findVersionMatchesDoesNotMatchErrorResponse() {
val buildGradle = """<html><body>Balls! No build.gradle here. Move along</body><html>""" val buildGradle = """<html><body>Balls! No build.gradle here. Move along</body><html>"""
val detectedVersion: String? = versionCheckerUtils.findVersion(buildGradle) val detectedVersion: String? = versionCheckerUtils.findVersion(buildGradle)
assertEquals(null, detectedVersion) Assertions.assertEquals(null, detectedVersion)
} }
@Test @Test
@ -228,6 +228,7 @@ import org.mockito.Mockito.`when`
verify(uiInteraction, times(0)).addNotification(anyInt(), anyString(), anyInt()) verify(uiInteraction, times(0)).addNotification(anyInt(), anyString(), anyInt())
} }
@Test @Test
fun `find higher version`() { fun `find higher version`() {
val buildGradle = """blabla val buildGradle = """blabla
@ -374,7 +375,7 @@ import org.mockito.Mockito.`when`
@BeforeEach @BeforeEach
fun `set time`() { fun `set time`() {
`when`(dateUtil.now()).thenReturn(10000000000L) `when`(dateUtil.now()).thenReturn(10000000000L)
assertEquals(10000000000L, dateUtil.now()) Assertions.assertEquals(10000000000L, dateUtil.now())
`when`(rh.gs(anyInt(), anyString())).thenReturn("") `when`(rh.gs(anyInt(), anyString())).thenReturn("")
} }

View file

@ -2,12 +2,12 @@ package info.nightscout.plugins.constraints.storage
import dagger.android.AndroidInjector import dagger.android.AndroidInjector
import dagger.android.HasAndroidInjector import dagger.android.HasAndroidInjector
import info.nightscout.androidaps.TestBase
import info.nightscout.interfaces.constraints.Constraint import info.nightscout.interfaces.constraints.Constraint
import info.nightscout.interfaces.ui.UiInteraction import info.nightscout.interfaces.ui.UiInteraction
import info.nightscout.rx.logging.AAPSLogger import info.nightscout.rx.logging.AAPSLogger
import info.nightscout.shared.interfaces.ResourceHelper import info.nightscout.shared.interfaces.ResourceHelper
import org.junit.Assert import info.nightscout.sharedtests.TestBase
import org.junit.jupiter.api.Assertions
import org.junit.jupiter.api.BeforeEach import org.junit.jupiter.api.BeforeEach
import org.junit.jupiter.api.Test import org.junit.jupiter.api.Test
import org.mockito.Mock import org.mockito.Mock
@ -42,9 +42,9 @@ class StorageConstraintPluginTest : TestBase() {
val mocked = MockedStorageConstraintPlugin({ AndroidInjector { } }, aapsLogger, rh, uiInteraction) val mocked = MockedStorageConstraintPlugin({ AndroidInjector { } }, aapsLogger, rh, uiInteraction)
// Set free space under 200(Mb) to disable loop // Set free space under 200(Mb) to disable loop
mocked.memSize = 150L mocked.memSize = 150L
Assert.assertEquals(false, mocked.isClosedLoopAllowed(Constraint(true)).value()) Assertions.assertEquals(false, mocked.isClosedLoopAllowed(Constraint(true)).value())
// Set free space over 200(Mb) to enable loop // Set free space over 200(Mb) to enable loop
mocked.memSize = 300L mocked.memSize = 300L
Assert.assertEquals(true, mocked.isClosedLoopAllowed(Constraint(true)).value()) Assertions.assertEquals(true, mocked.isClosedLoopAllowed(Constraint(true)).value())
} }
} }

View file

@ -4,10 +4,7 @@ import info.nightscout.interfaces.constraints.versionChecker.AllowedVersions
import org.joda.time.LocalDate import org.joda.time.LocalDate
import org.json.JSONArray import org.json.JSONArray
import org.json.JSONObject import org.json.JSONObject
import org.junit.Assert.assertEquals import org.junit.jupiter.api.Assertions
import org.junit.Assert.assertFalse
import org.junit.Assert.assertNull
import org.junit.Assert.assertTrue
import org.junit.jupiter.api.Test import org.junit.jupiter.api.Test
class AllowedVersionsTest { class AllowedVersionsTest {
@ -48,14 +45,14 @@ class AllowedVersionsTest {
@Test @Test
fun generateSupportedVersionsTest() { fun generateSupportedVersionsTest() {
val definition = generateSupportedVersions() val definition = generateSupportedVersions()
assertNull(AllowedVersions().findByApi(definition, 0)) Assertions.assertNull(AllowedVersions().findByApi(definition, 0))
assertFalse(AllowedVersions().findByApi(definition, 1)?.has("supported") ?: true) Assertions.assertFalse(AllowedVersions().findByApi(definition, 1)?.has("supported") ?: true)
assertFalse(AllowedVersions().findByApi(definition, 23)?.has("supported") ?: true) Assertions.assertFalse(AllowedVersions().findByApi(definition, 23)?.has("supported") ?: true)
assertEquals("2.6.2", AllowedVersions().findByApi(definition, 24)?.getString("supported")) Assertions.assertEquals("2.6.2", AllowedVersions().findByApi(definition, 24)?.getString("supported"))
assertEquals("2.6.2", AllowedVersions().findByApi(definition, 25)?.getString("supported")) Assertions.assertEquals("2.6.2", AllowedVersions().findByApi(definition, 25)?.getString("supported"))
assertEquals("2.8.2", AllowedVersions().findByApi(definition, 26)?.getString("supported")) Assertions.assertEquals("2.8.2", AllowedVersions().findByApi(definition, 26)?.getString("supported"))
assertEquals("2.8.2", AllowedVersions().findByApi(definition, 27)?.getString("supported")) Assertions.assertEquals("2.8.2", AllowedVersions().findByApi(definition, 27)?.getString("supported"))
assertEquals("2.8.2", AllowedVersions().findByApi(definition, 28)?.getString("supported")) Assertions.assertEquals("2.8.2", AllowedVersions().findByApi(definition, 28)?.getString("supported"))
} }
@Test @Test
@ -65,20 +62,21 @@ class AllowedVersionsTest {
"[{\"minAndroid\":1,\"maxAndroid\":23},{\"minAndroid\":24,\"maxAndroid\":25,\"supported\":\"2.6.2\"},{\"minAndroid\":26,\"maxAndroid\":27,\"supported\":\"2.8.2\"},{\"minAndroid\":28,\"maxAndroid\":99,\"supported\":\"2.8.2\"},{\"endDate\":\"2021-11-07\",\"version\":\"2.9.0-beta1\"},{\"endDate\":\"2021-11-02\",\"version\":\"3.0-beta1\"},{\"endDate\":\"2021-11-04\",\"version\":\"3.0-beta2\"},{\"endDate\":\"2021-11-10\",\"version\":\"3.0-beta3\"},{\"endDate\":\"2021-11-14\",\"version\":\"3.0-beta4\"}\n" + "[{\"minAndroid\":1,\"maxAndroid\":23},{\"minAndroid\":24,\"maxAndroid\":25,\"supported\":\"2.6.2\"},{\"minAndroid\":26,\"maxAndroid\":27,\"supported\":\"2.8.2\"},{\"minAndroid\":28,\"maxAndroid\":99,\"supported\":\"2.8.2\"},{\"endDate\":\"2021-11-07\",\"version\":\"2.9.0-beta1\"},{\"endDate\":\"2021-11-02\",\"version\":\"3.0-beta1\"},{\"endDate\":\"2021-11-04\",\"version\":\"3.0-beta2\"},{\"endDate\":\"2021-11-10\",\"version\":\"3.0-beta3\"},{\"endDate\":\"2021-11-14\",\"version\":\"3.0-beta4\"}\n" +
" ,{\"endDate\":\"2021-11-16\",\"version\":\"3.0-beta5\"}\n" + " ,{\"endDate\":\"2021-11-16\",\"version\":\"3.0-beta5\"}\n" +
"]" "]"
assertNull(AllowedVersions().findByVersion(definition, "2.6.0")) Assertions.assertNull(AllowedVersions().findByVersion(definition, "2.6.0"))
assertTrue(AllowedVersions().findByVersion(definition, "2.9.0-beta1")?.has("endDate") ?: false) Assertions.assertTrue(AllowedVersions().findByVersion(definition, "2.9.0-beta1")?.has("endDate") ?: false)
assertEquals("2021-11-07", AllowedVersions().findByVersion(definition, "2.9.0-beta1")?.getString("endDate")) Assertions.assertEquals("2021-11-07", AllowedVersions().findByVersion(definition, "2.9.0-beta1")?.getString("endDate"))
} }
@Suppress("SpellCheckingInspection")
@Test @Test
fun endDateToMilliseconds() { fun endDateToMilliseconds() {
val definition = generateSupportedVersions() val definition = generateSupportedVersions()
val endDate = AllowedVersions().endDateToMilliseconds(AllowedVersions().findByVersion(definition, "2.9.0-beta1")?.getString("endDate") ?: "1000/01/01") val endDate = AllowedVersions().endDateToMilliseconds(AllowedVersions().findByVersion(definition, "2.9.0-beta1")?.getString("endDate") ?: "1000/01/01")
val dateTime = LocalDate(endDate) val dateTime = LocalDate(endDate)
assertEquals(2021, dateTime.year) Assertions.assertEquals(2021, dateTime.year)
assertEquals(11, dateTime.monthOfYear) Assertions.assertEquals(11, dateTime.monthOfYear)
assertEquals(7, dateTime.dayOfMonth) Assertions.assertEquals(7, dateTime.dayOfMonth)
assertNull(AllowedVersions().endDateToMilliseconds("abdef")) Assertions.assertNull(AllowedVersions().endDateToMilliseconds("abdef"))
} }
} }