This repository has been archived by the owner on Jun 20, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 496
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2344 from corona-warn-app/feature/4321-ppa-main
Privacy Preserving Analytics (main-feature-branch) (EXPOSUREAPP-4321)
- Loading branch information
Showing
104 changed files
with
9,418 additions
and
53 deletions.
There are no files selected for viewing
102 changes: 102 additions & 0 deletions
102
...rc/androidTest/java/de/rki/coronawarnapp/ui/onboarding/OnboardingAnalyticsFragmentTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
package de.rki.coronawarnapp.ui.onboarding | ||
|
||
import androidx.lifecycle.asLiveData | ||
import androidx.test.ext.junit.runners.AndroidJUnit4 | ||
import dagger.Module | ||
import dagger.android.ContributesAndroidInjector | ||
import de.rki.coronawarnapp.datadonation.analytics.Analytics | ||
import de.rki.coronawarnapp.datadonation.analytics.common.Districts | ||
import de.rki.coronawarnapp.datadonation.analytics.storage.AnalyticsSettings | ||
import de.rki.coronawarnapp.server.protocols.internal.ppdd.PpaData | ||
import io.mockk.MockKAnnotations | ||
import io.mockk.coEvery | ||
import io.mockk.every | ||
import io.mockk.impl.annotations.MockK | ||
import io.mockk.spyk | ||
import io.mockk.unmockkAll | ||
import kotlinx.coroutines.flow.flow | ||
import kotlinx.coroutines.flow.flowOf | ||
import org.junit.After | ||
import org.junit.Before | ||
import org.junit.Rule | ||
import org.junit.Test | ||
import org.junit.runner.RunWith | ||
import testhelpers.BaseUITest | ||
import testhelpers.Screenshot | ||
import testhelpers.SystemUIDemoModeRule | ||
import testhelpers.TestDispatcherProvider | ||
import testhelpers.captureScreenshot | ||
import testhelpers.launchFragment2 | ||
import tools.fastlane.screengrab.locale.LocaleTestRule | ||
|
||
@RunWith(AndroidJUnit4::class) | ||
class OnboardingAnalyticsFragmentTest : BaseUITest() { | ||
|
||
@MockK lateinit var settings: AnalyticsSettings | ||
@MockK lateinit var districts: Districts | ||
@MockK lateinit var analytics: Analytics | ||
|
||
private lateinit var viewModel: OnboardingAnalyticsViewModel | ||
|
||
@Rule | ||
@JvmField | ||
val localeTestRule = LocaleTestRule() | ||
|
||
@get:Rule | ||
val systemUIDemoModeRule = SystemUIDemoModeRule() | ||
|
||
@Before | ||
fun setup() { | ||
MockKAnnotations.init(this, relaxed = true) | ||
|
||
coEvery { districts.loadDistricts() } returns listOf(Districts.District( | ||
districtId = 11011004, | ||
districtName = "SK Berlin Charlottenburg-Wilmersdorf" | ||
)) | ||
|
||
viewModel = onboardingAnalyticsViewModelSpy() | ||
with(viewModel) { | ||
every { ageGroup } returns flowOf(PpaData.PPAAgeGroup.AGE_GROUP_0_TO_29).asLiveData() | ||
every { federalState } returns flowOf(PpaData.PPAFederalState.FEDERAL_STATE_BE).asLiveData() | ||
every { district } returns flow { emit(districts.loadDistricts().first()) }.asLiveData() | ||
} | ||
|
||
setupMockViewModel( | ||
object : OnboardingAnalyticsViewModel.Factory { | ||
override fun create(): OnboardingAnalyticsViewModel = viewModel | ||
} | ||
) | ||
} | ||
|
||
private fun onboardingAnalyticsViewModelSpy() = spyk( | ||
OnboardingAnalyticsViewModel( | ||
settings = settings, | ||
districts = districts, | ||
dispatcherProvider = TestDispatcherProvider(), | ||
analytics = analytics | ||
) | ||
) | ||
|
||
@After | ||
fun teardown() { | ||
clearAllViewModels() | ||
unmockkAll() | ||
} | ||
|
||
@Test | ||
fun launch_fragment() { | ||
launchFragment2<OnboardingAnalyticsFragment>() | ||
} | ||
|
||
@Screenshot | ||
@Test | ||
fun capture_screenshot() { | ||
captureScreenshot<OnboardingAnalyticsFragment>() | ||
} | ||
} | ||
|
||
@Module | ||
abstract class OnboardingAnalyticsFragmentTestModule { | ||
@ContributesAndroidInjector | ||
abstract fun onboardingAnalyticsFragment(): OnboardingAnalyticsFragment | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
12 changes: 12 additions & 0 deletions
12
.../rki/coronawarnapp/datadonation/analytics/storage/DefaultLastAnalyticsSubmissionLogger.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
package de.rki.coronawarnapp.datadonation.analytics.storage | ||
|
||
import de.rki.coronawarnapp.server.protocols.internal.ppdd.PpaData | ||
import javax.inject.Inject | ||
|
||
class DefaultLastAnalyticsSubmissionLogger @Inject constructor() : LastAnalyticsSubmissionLogger { | ||
override suspend fun storeAnalyticsData(analyticsProto: PpaData.PPADataAndroid) { | ||
// Do not store past analytics submissions in Production | ||
} | ||
|
||
override suspend fun getLastAnalyticsData(): LastAnalyticsSubmission? = null | ||
} |
92 changes: 92 additions & 0 deletions
92
.../rki/coronawarnapp/datadonation/analytics/storage/DefaultLastAnalyticsSubmissionLogger.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
package de.rki.coronawarnapp.datadonation.analytics.storage | ||
|
||
import android.content.Context | ||
import com.google.gson.Gson | ||
import com.google.gson.JsonParseException | ||
import com.google.gson.TypeAdapter | ||
import com.google.gson.stream.JsonReader | ||
import com.google.gson.stream.JsonWriter | ||
import de.rki.coronawarnapp.server.protocols.internal.ppdd.PpaData | ||
import de.rki.coronawarnapp.util.TimeStamper | ||
import de.rki.coronawarnapp.util.coroutine.DispatcherProvider | ||
import de.rki.coronawarnapp.util.di.AppContext | ||
import de.rki.coronawarnapp.util.serialization.BaseGson | ||
import de.rki.coronawarnapp.util.serialization.adapter.InstantAdapter | ||
import de.rki.coronawarnapp.util.serialization.fromJson | ||
import de.rki.coronawarnapp.util.serialization.toJson | ||
import kotlinx.coroutines.withContext | ||
import okio.ByteString.Companion.decodeBase64 | ||
import okio.ByteString.Companion.toByteString | ||
import org.joda.time.Instant | ||
import org.json.JSONObject | ||
import timber.log.Timber | ||
import java.io.File | ||
import javax.inject.Inject | ||
|
||
class DefaultLastAnalyticsSubmissionLogger @Inject constructor( | ||
@AppContext private val context: Context, | ||
private val dispatcherProvider: DispatcherProvider, | ||
@BaseGson private val baseGson: Gson, | ||
private val timeStamper: TimeStamper | ||
) : LastAnalyticsSubmissionLogger { | ||
private val analyticsDir = File(context.cacheDir, "analytics_storage") | ||
private val analyticsFile = File(analyticsDir, "last_analytics.bin") | ||
|
||
private val gson by lazy { | ||
baseGson.newBuilder() | ||
.registerTypeAdapter(Instant::class.java, InstantAdapter()) | ||
.registerTypeAdapter(PpaData.PPADataAndroid::class.java, PPADataAndroidAdapter()) | ||
.create() | ||
} | ||
|
||
override suspend fun storeAnalyticsData(analyticsProto: PpaData.PPADataAndroid) = | ||
withContext(dispatcherProvider.IO) { | ||
if (!analyticsDir.exists()) { | ||
analyticsDir.mkdirs() | ||
} | ||
|
||
val dataObject = LastAnalyticsSubmission( | ||
timestamp = timeStamper.nowUTC, | ||
ppaDataAndroid = analyticsProto | ||
) | ||
|
||
try { | ||
gson.toJson(dataObject, analyticsFile) | ||
} catch (e: Exception) { | ||
Timber.e(e, "Failed to store analytics data.") | ||
} | ||
} | ||
|
||
override suspend fun getLastAnalyticsData(): LastAnalyticsSubmission? = withContext(dispatcherProvider.IO) { | ||
try { | ||
gson.fromJson<LastAnalyticsSubmission>(analyticsFile)?.also { | ||
requireNotNull(it.ppaDataAndroid) | ||
requireNotNull(it.timestamp) | ||
} | ||
} catch (e: Exception) { | ||
Timber.e(e, "Couldn't load analytics data.") | ||
null | ||
} | ||
} | ||
|
||
companion object { | ||
class PPADataAndroidAdapter : TypeAdapter<PpaData.PPADataAndroid>() { | ||
override fun write(out: JsonWriter, value: PpaData.PPADataAndroid?) { | ||
if (value == null) out.nullValue() | ||
else value.toByteArray()?.toByteString()?.base64().let { out.value(it) } | ||
} | ||
|
||
override fun read(reader: JsonReader): PpaData.PPADataAndroid? = when (reader.peek()) { | ||
JSONObject.NULL -> reader.nextNull().let { null } | ||
else -> { | ||
val raw = reader.nextString().decodeBase64()?.toByteArray() | ||
if (raw == null) { | ||
throw JsonParseException("Can't decode base64 ByteArray") | ||
} else { | ||
PpaData.PPADataAndroid.parseFrom(raw) | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.