Skip to content
Snippets Groups Projects
Unverified Commit 1b9e37c9 authored by Matthias Urhahn's avatar Matthias Urhahn Committed by GitHub
Browse files

Prevent accidental use of protobuf default value for app features (DEV) (#1716)


* Make use of `hasAppFeatures` to prevent accidental use of default values.

* Swap order in if statement to remove unnecessary negation

* Reduce visibility

Co-authored-by: default avatarralfgehrer <mail@ralfgehrer.com>
Co-authored-by: default avatarKolya Opahle <k.opahle@sap.com>
Co-authored-by: default avatarharambasicluka <64483219+harambasicluka@users.noreply.github.com>
parent 3be0e1a7
No related branches found
No related tags found
No related merge requests found
......@@ -11,7 +11,7 @@ interface CWAConfig {
val supportedCountries: List<String>
val appFeatures: AppFeaturesOuterClass.AppFeatures
val appFeatures: List<AppFeaturesOuterClass.AppFeature>
interface Mapper : ConfigMapper<CWAConfig>
}
package de.rki.coronawarnapp.appconfig.mapping
import androidx.annotation.VisibleForTesting
import dagger.Reusable
import de.rki.coronawarnapp.appconfig.CWAConfig
import de.rki.coronawarnapp.server.protocols.internal.v2.AppConfigAndroid
import de.rki.coronawarnapp.server.protocols.internal.v2.AppConfigAndroid.ApplicationConfigurationAndroid
import de.rki.coronawarnapp.server.protocols.internal.v2.AppFeaturesOuterClass
import timber.log.Timber
import javax.inject.Inject
@Reusable
class CWAConfigMapper @Inject constructor() : CWAConfig.Mapper {
override fun map(rawConfig: AppConfigAndroid.ApplicationConfigurationAndroid): CWAConfig {
override fun map(rawConfig: ApplicationConfigurationAndroid): CWAConfig {
return CWAConfigContainer(
latestVersionCode = rawConfig.latestVersionCode,
minVersionCode = rawConfig.minVersionCode,
supportedCountries = rawConfig.getMappedSupportedCountries(),
appFeatures = rawConfig.appFeatures
appFeatures = rawConfig.mapAppFeatures()
)
}
@VisibleForTesting(otherwise = VisibleForTesting.PRIVATE)
internal fun AppConfigAndroid.ApplicationConfigurationAndroid.getMappedSupportedCountries(): List<String> =
private fun ApplicationConfigurationAndroid.getMappedSupportedCountries(): List<String> =
when {
supportedCountriesList == null -> emptyList()
supportedCountriesList.size == 1 && !VALID_CC.matches(supportedCountriesList.single()) -> {
......@@ -30,11 +28,22 @@ class CWAConfigMapper @Inject constructor() : CWAConfig.Mapper {
else -> supportedCountriesList
}
private fun ApplicationConfigurationAndroid.mapAppFeatures(): List<AppFeaturesOuterClass.AppFeature> =
if (hasAppFeatures()) {
val parsedFeatures = mutableListOf<AppFeaturesOuterClass.AppFeature>()
for (index in 0 until appFeatures.appFeaturesCount) {
parsedFeatures.add(appFeatures.getAppFeatures(index))
}
parsedFeatures
} else {
emptyList()
}
data class CWAConfigContainer(
override val latestVersionCode: Long,
override val minVersionCode: Long,
override val supportedCountries: List<String>,
override val appFeatures: AppFeaturesOuterClass.AppFeatures
override val appFeatures: List<AppFeaturesOuterClass.AppFeature>
) : CWAConfig
companion object {
......
package de.rki.coronawarnapp.appconfig.mapping
import de.rki.coronawarnapp.server.protocols.internal.v2.AppConfigAndroid
import de.rki.coronawarnapp.server.protocols.internal.v2.AppFeaturesOuterClass
import io.kotest.matchers.shouldBe
import org.junit.jupiter.api.Test
import testhelpers.BaseTest
......@@ -45,4 +46,27 @@ class CWAConfigMapperTest : BaseTest() {
this.supportedCountries shouldBe emptyList()
}
}
@Test
fun `app features are mapped`() {
val rawConfig = AppConfigAndroid.ApplicationConfigurationAndroid.newBuilder()
.setAppFeatures(
AppFeaturesOuterClass.AppFeatures.newBuilder().apply {
addAppFeatures(AppFeaturesOuterClass.AppFeature.newBuilder().apply { }.build())
}
)
.build()
createInstance().map(rawConfig).apply {
appFeatures.size shouldBe 1
}
}
@Test
fun `app features being empty are handled`() {
val rawConfig = AppConfigAndroid.ApplicationConfigurationAndroid.newBuilder()
.build()
createInstance().map(rawConfig).apply {
appFeatures shouldBe emptyList()
}
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment