Skip to content

Commit

Permalink
shared-tests #6
Browse files Browse the repository at this point in the history
  • Loading branch information
MilosKozak committed Sep 3, 2023
1 parent d1694e9 commit 53beed4
Show file tree
Hide file tree
Showing 13 changed files with 120 additions and 201 deletions.
3 changes: 3 additions & 0 deletions plugins/configuration/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ dependencies {
implementation project(':core:utils')
implementation project(':core:ui')
implementation project(':core:validators')

testImplementation project(':app-wear-shared:shared-tests')

//WorkManager
api "androidx.work:work-runtime-ktx:$work_version"
// Maintenance
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package info.nightscout.configuration.maintenance.formats

import info.nightscout.androidaps.TestBase
import info.nightscout.core.utils.CryptoUtil
import info.nightscout.interfaces.maintenance.PrefFormatError
import info.nightscout.interfaces.maintenance.PrefMetadata
Expand All @@ -9,10 +8,10 @@ import info.nightscout.interfaces.maintenance.PrefsFormat
import info.nightscout.interfaces.maintenance.PrefsMetadataKey
import info.nightscout.interfaces.maintenance.PrefsStatus
import info.nightscout.shared.interfaces.ResourceHelper
import info.nightscout.sharedtests.TestBase
import org.hamcrest.CoreMatchers
import org.junit.Assert
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.Test
import org.mockito.ArgumentMatchers
Expand Down Expand Up @@ -63,13 +62,13 @@ open class EncryptedPrefsFormatTest : TestBase() {

assumeAES256isSupported(cryptoUtil)

Assert.assertEquals(prefs.values.size, 2)
Assert.assertEquals(prefs.values["key1"], "A")
Assert.assertEquals(prefs.values["keyB"], "2")
Assertions.assertEquals(prefs.values.size, 2)
Assertions.assertEquals(prefs.values["key1"], "A")
Assertions.assertEquals(prefs.values["keyB"], "2")

Assert.assertEquals(prefs.metadata[PrefsMetadataKey.FILE_FORMAT]?.status, PrefsStatus.OK)
Assert.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.FILE_FORMAT]?.status, PrefsStatus.OK)
Assertions.assertEquals(prefs.metadata[PrefsMetadataKey.FILE_FORMAT]?.value, PrefsFormat.FORMAT_KEY_ENC)
Assertions.assertEquals(prefs.metadata[PrefsMetadataKey.ENCRYPTION]?.status, PrefsStatus.OK)
}

@Test
Expand Down Expand Up @@ -107,13 +106,13 @@ open class EncryptedPrefsFormatTest : TestBase() {

assumeAES256isSupported(cryptoUtil)

Assert.assertEquals(prefsOut.values.size, 2)
Assert.assertEquals(prefsOut.values["testpref1"], "--1--")
Assert.assertEquals(prefsOut.values["testpref2"], "another")
Assertions.assertEquals(prefsOut.values.size, 2)
Assertions.assertEquals(prefsOut.values["testpref1"], "--1--")
Assertions.assertEquals(prefsOut.values["testpref2"], "another")

Assert.assertEquals(prefsOut.metadata[PrefsMetadataKey.FILE_FORMAT]?.status, PrefsStatus.OK)
Assert.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.FILE_FORMAT]?.status, PrefsStatus.OK)
Assertions.assertEquals(prefsOut.metadata[PrefsMetadataKey.FILE_FORMAT]?.value, PrefsFormat.FORMAT_KEY_ENC)
Assertions.assertEquals(prefsOut.metadata[PrefsMetadataKey.ENCRYPTION]?.status, PrefsStatus.OK)
}

@Test
Expand All @@ -134,11 +133,11 @@ open class EncryptedPrefsFormatTest : TestBase() {
val encryptedFormat = EncryptedPrefsFormat(rh, cryptoUtil, storage)
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)
Assert.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.FILE_FORMAT]?.status, PrefsStatus.OK)
Assertions.assertEquals(prefs.metadata[PrefsMetadataKey.FILE_FORMAT]?.value, PrefsFormat.FORMAT_KEY_ENC)
Assertions.assertEquals(prefs.metadata[PrefsMetadataKey.ENCRYPTION]?.status, PrefsStatus.ERROR)
}

@Test
Expand All @@ -164,10 +163,10 @@ open class EncryptedPrefsFormatTest : TestBase() {
assumeAES256isSupported(cryptoUtil)

// 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
Assert.assertEquals(prefs.metadata[PrefsMetadataKey.ENCRYPTION]?.status, PrefsStatus.ERROR)
Assertions.assertEquals(prefs.metadata[PrefsMetadataKey.ENCRYPTION]?.status, PrefsStatus.ERROR)
}

@Test
Expand All @@ -188,8 +187,8 @@ open class EncryptedPrefsFormatTest : TestBase() {
val encryptedFormat = EncryptedPrefsFormat(rh, cryptoUtil, storage)
val prefs = encryptedFormat.loadPreferences(getMockedFile(), "sikret")

Assert.assertEquals(prefs.values.size, 0)
Assert.assertEquals(prefs.metadata[PrefsMetadataKey.ENCRYPTION]?.status, PrefsStatus.ERROR)
Assertions.assertEquals(prefs.values.size, 0)
Assertions.assertEquals(prefs.metadata[PrefsMetadataKey.ENCRYPTION]?.status, PrefsStatus.ERROR)
}

@Test
Expand All @@ -203,13 +202,13 @@ open class EncryptedPrefsFormatTest : TestBase() {
val encryptedFormat = EncryptedPrefsFormat(rh, cryptoUtil, storage)
val prefs = encryptedFormat.loadPreferences(getMockedFile(), "sikret")

Assert.assertEquals(prefs.values.size, 0)
Assert.assertEquals(prefs.metadata[PrefsMetadataKey.FILE_FORMAT]?.status, PrefsStatus.ERROR)
Assertions.assertEquals(prefs.values.size, 0)
Assertions.assertEquals(prefs.metadata[PrefsMetadataKey.FILE_FORMAT]?.status, PrefsStatus.ERROR)
}

@Test
fun garbageInputTest() {
Assert.assertThrows(PrefFormatError::class.java) {
Assertions.assertThrows(PrefFormatError::class.java) {
val frozenPrefs = "whatever man, i duno care"

val storage = SingleStringStorage(frozenPrefs)
Expand All @@ -220,7 +219,7 @@ open class EncryptedPrefsFormatTest : TestBase() {

@Test
fun unknownFormatTest() {
Assert.assertThrows(PrefFormatError::class.java) {
Assertions.assertThrows(PrefFormatError::class.java) {
val frozenPrefs = "{\n" +
" \"metadata\": {},\n" +
" \"security\": {\n" +
Expand Down
2 changes: 2 additions & 0 deletions plugins/constraints/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ dependencies {
implementation project(':core:validators')
implementation project(':database:entities')

testImplementation project(':app-wear-shared:shared-tests')

// Phone checker
api 'com.scottyab:rootbeer-lib:0.1.0'
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package info.nightscout.plugins.constraints.bgQualityCheck

import dagger.android.AndroidInjector
import dagger.android.HasAndroidInjector
import info.nightscout.androidaps.TestBase
import info.nightscout.core.utils.fabric.FabricPrivacy
import info.nightscout.database.entities.GlucoseValue
import info.nightscout.interfaces.aps.AutosensDataStore
Expand All @@ -16,6 +15,7 @@ import info.nightscout.rx.bus.RxBus
import info.nightscout.shared.interfaces.ResourceHelper
import info.nightscout.shared.utils.DateUtil
import info.nightscout.shared.utils.T
import info.nightscout.sharedtests.TestBase
import org.junit.jupiter.api.Assertions
import org.junit.jupiter.api.BeforeEach
import org.junit.jupiter.api.Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@ package info.nightscout.plugins.constraints.dstHelper

import dagger.android.AndroidInjector
import dagger.android.HasAndroidInjector
import info.nightscout.androidaps.TestBase
import info.nightscout.interfaces.aps.Loop
import info.nightscout.interfaces.plugin.ActivePlugin
import info.nightscout.shared.interfaces.ResourceHelper
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.Test
import org.mockito.Mock
Expand All @@ -34,38 +34,38 @@ class DstHelperPluginTest : TestBase() {
}

@Test
private fun runTest() {
fun runTest() {
val tz = TimeZone.getTimeZone("Europe/Rome")
TimeZone.setDefault(tz)
var cal = Calendar.getInstance(tz, Locale.ITALIAN)
val df: DateFormat = SimpleDateFormat("yyyy-MM-dd HH:mm", Locale.ITALIAN)
var dateBeforeDST = df.parse("2018-03-25 01:55")
cal.time = dateBeforeDST!!
Assert.assertEquals(false, plugin.wasDST(cal))
Assert.assertEquals(true, plugin.willBeDST(cal))
Assertions.assertEquals(false, plugin.wasDST(cal))
Assertions.assertEquals(true, plugin.willBeDST(cal))
TimeZone.setDefault(tz)
cal = Calendar.getInstance(tz, Locale.ITALIAN)
dateBeforeDST = df.parse("2018-03-25 03:05")
cal.time = dateBeforeDST!!
Assert.assertEquals(true, plugin.wasDST(cal))
Assert.assertEquals(false, plugin.willBeDST(cal))
Assertions.assertEquals(true, plugin.wasDST(cal))
Assertions.assertEquals(false, plugin.willBeDST(cal))
TimeZone.setDefault(tz)
cal = Calendar.getInstance(tz, Locale.ITALIAN)
dateBeforeDST = df.parse("2018-03-25 02:05") //Cannot happen!!!
cal.time = dateBeforeDST!!
Assert.assertEquals(true, plugin.wasDST(cal))
Assert.assertEquals(false, plugin.willBeDST(cal))
Assertions.assertEquals(true, plugin.wasDST(cal))
Assertions.assertEquals(false, plugin.willBeDST(cal))
TimeZone.setDefault(tz)
cal = Calendar.getInstance(tz, Locale.ITALIAN)
dateBeforeDST = df.parse("2018-03-25 05:55") //Cannot happen!!!
cal.time = dateBeforeDST!!
Assert.assertEquals(true, plugin.wasDST(cal))
Assert.assertEquals(false, plugin.willBeDST(cal))
Assertions.assertEquals(true, plugin.wasDST(cal))
Assertions.assertEquals(false, plugin.willBeDST(cal))
TimeZone.setDefault(tz)
cal = Calendar.getInstance(tz, Locale.ITALIAN)
dateBeforeDST = df.parse("2018-03-25 06:05") //Cannot happen!!!
cal.time = dateBeforeDST!!
Assert.assertEquals(false, plugin.wasDST(cal))
Assert.assertEquals(false, plugin.willBeDST(cal))
Assertions.assertEquals(false, plugin.wasDST(cal))
Assertions.assertEquals(false, plugin.willBeDST(cal))
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,16 @@ package info.nightscout.plugins.constraints.objectives

import dagger.android.AndroidInjector
import dagger.android.HasAndroidInjector
import info.nightscout.androidaps.TestBase
import info.nightscout.interfaces.Config
import info.nightscout.interfaces.constraints.Constraint
import info.nightscout.interfaces.constraints.Objectives
import info.nightscout.interfaces.plugin.ActivePlugin
import info.nightscout.plugins.constraints.R
import info.nightscout.plugins.constraints.objectives.objectives.Objective
import info.nightscout.shared.interfaces.ResourceHelper
import info.nightscout.shared.sharedPreferences.SP
import info.nightscout.shared.utils.DateUtil
import info.nightscout.sharedtests.TestBase
import org.junit.jupiter.api.Assertions
import org.junit.jupiter.api.BeforeEach
import org.junit.jupiter.api.Test
Expand All @@ -27,9 +28,9 @@ class ObjectivesPluginTest : TestBase() {

private lateinit var objectivesPlugin: ObjectivesPlugin

val injector = HasAndroidInjector {
private val injector = HasAndroidInjector {
AndroidInjector {
if (it is info.nightscout.plugins.constraints.objectives.objectives.Objective) {
if (it is Objective) {
it.sp = sp
it.rh = rh
it.dateUtil = dateUtil
Expand Down
Original file line number Diff line number Diff line change
@@ -1,30 +1,31 @@
package info.nightscout.plugins.constraints.objectives

import info.nightscout.androidaps.TestBase
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.mockito.Mock
import kotlin.math.abs

class SntpClientTest : TestBase() {

@Mock lateinit var dateUtil: DateUtil

@Test
fun ntpTimeTest() {
// 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() {
Assert.assertFalse(networkConnected)
Assert.assertFalse(success)
Assert.assertEquals(0L, time)
Assertions.assertFalse(networkConnected)
Assertions.assertFalse(success)
Assertions.assertEquals(0L, time)
}
}, false)
// 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() {
Assert.assertTrue(success)
Assert.assertTrue(abs(time - System.currentTimeMillis()) < 60000)
Assertions.assertTrue(success)
Assertions.assertTrue(abs(time - System.currentTimeMillis()) < 60000)
}
})
}
Expand Down
Loading

0 comments on commit 53beed4

Please sign in to comment.