diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 62b5949..2b8f69a 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -60,9 +60,9 @@ jobs: with: distribution: temurin java-version: 17 - - uses: gradle/actions/setup-gradle@v6 + - uses: gradle/actions/setup-gradle@8da46b539cd8fe6ab27ca75128919a18d757604c # v6.4.1 if: github.event_name != 'pull_request' || steps.filter.outputs.code == 'true' - - uses: android-actions/setup-android@v4 + - uses: android-actions/setup-android@be39fa834029ff78f1a44aa3bb0819b8fc2bd8fd # v4.0.4 if: github.event_name != 'pull_request' || steps.filter.outputs.code == 'true' - name: Unit tests and coverage if: github.event_name != 'pull_request' || steps.filter.outputs.code == 'true' @@ -86,7 +86,7 @@ jobs: name: roborazzi-diffs path: app/build/outputs/roborazzi/ if-no-files-found: ignore - - uses: codecov/codecov-action@v7 + - uses: codecov/codecov-action@303a32d7a59b442fa8d48b6a1cc6825c09c847a5 # v7.1.1 if: github.event_name == 'push' with: files: app/build/reports/jacoco/jacocoTestReport/jacocoTestReport.xml diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index 28e1aea..5318604 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -39,10 +39,10 @@ jobs: distribution: 'temurin' - name: Setup Gradle - uses: gradle/actions/setup-gradle@v6 + uses: gradle/actions/setup-gradle@8da46b539cd8fe6ab27ca75128919a18d757604c # v6.4.1 - name: Setup Android SDK - uses: android-actions/setup-android@v4 + uses: android-actions/setup-android@be39fa834029ff78f1a44aa3bb0819b8fc2bd8fd # v4.0.4 - name: Decode keystore run: echo "${{ secrets.KEYSTORE_BASE64 }}" | base64 -d > app/release.keystore @@ -68,7 +68,7 @@ jobs: run: rm -f app/release.keystore - name: Create GitHub Release - uses: softprops/action-gh-release@v3 + uses: softprops/action-gh-release@8a0cd651e5b2365bd640854a3fa5dfc70e4875f3 # v3.0.4 with: tag_name: ${{ steps.version.outputs.tag }} name: ${{ steps.version.outputs.tag }} diff --git a/CLAUDE.md b/CLAUDE.md index 8347d5d..f608742 100644 --- a/CLAUDE.md +++ b/CLAUDE.md @@ -9,7 +9,7 @@ Android monitoring agent for CashPilot that tracks passive income apps (EarnApp, - Ktor HTTP client - Kotlinx Serialization - DataStore Preferences -- Android SDK 26+ (minSdk), compileSdk 36, targetSdk 35 +- Android SDK 26+ (minSdk), compileSdk 37, targetSdk 36 - Gradle with version catalogs (`libs.versions.toml`) - ProGuard (release builds: `isMinifyEnabled = true`, `isShrinkResources = true`) diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index b468235..802adea 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -24,12 +24,14 @@ + android:theme="@style/Theme.CashPilotAndroid" + tools:targetApi="31"> by preferencesDataStore(name = "cashpilot_settings") +private val Context.dataStore: DataStore by preferencesDataStore(name = SettingsStore.FILE_NAME) object SettingsStore { + /** The DataStore name. res/xml/data_extraction_rules.xml keeps its file on this device. */ + internal const val FILE_NAME = "cashpilot_settings" + private val SERVER_URL = stringPreferencesKey("server_url") private val API_KEY = stringPreferencesKey("api_key") private val WORKER_KEY = stringPreferencesKey("worker_key") diff --git a/app/src/main/res/xml/data_extraction_rules.xml b/app/src/main/res/xml/data_extraction_rules.xml new file mode 100644 index 0000000..b4698c5 --- /dev/null +++ b/app/src/main/res/xml/data_extraction_rules.xml @@ -0,0 +1,18 @@ + + + + + + + + + + + + diff --git a/app/src/test/java/com/cashpilot/android/DataBackupRulesTest.kt b/app/src/test/java/com/cashpilot/android/DataBackupRulesTest.kt new file mode 100644 index 0000000..0f3a18a --- /dev/null +++ b/app/src/test/java/com/cashpilot/android/DataBackupRulesTest.kt @@ -0,0 +1,101 @@ +package com.cashpilot.android + +import android.content.Context +import androidx.datastore.preferences.preferencesDataStoreFile +import com.cashpilot.android.service.Heartbeat +import com.cashpilot.android.util.SettingsStore +import org.junit.Assert.assertEquals +import org.junit.Assert.assertTrue +import org.junit.Test +import org.junit.runner.RunWith +import org.robolectric.RobolectricTestRunner +import org.robolectric.RuntimeEnvironment +import org.robolectric.annotation.Config +import org.xmlpull.v1.XmlPullParser + +/** + * The settings DataStore holds the fleet key and this phone's worker key, and + * must not leave the device. + * + * allowBackup="false" stops cloud backup, but Android 12 and later ignore it for + * device-to-device transfer. Only the dataExtractionRules file can keep the + * DataStore off a new phone, so this checks the manifest points at it and that + * the DataStore file and the heartbeat state are excluded in both sections. + */ +// Same SDK as IconGoldenTest: Robolectric 4.17 cannot run this module's targetSdk. +@RunWith(RobolectricTestRunner::class) +@Config(sdk = [34]) +class DataBackupRulesTest { + + private val context: Context = RuntimeEnvironment.getApplication() + + /** domain/path excludes per section, read from the merged resource. */ + private fun excludes(): Map>> { + val result = mutableMapOf>>() + val parser = context.resources.getXml(R.xml.data_extraction_rules) + var section: String? = null + while (parser.next() != XmlPullParser.END_DOCUMENT) { + if (parser.eventType != XmlPullParser.START_TAG) continue + when (parser.name) { + "cloud-backup", "device-transfer" -> { + section = parser.name + result.getOrPut(parser.name) { mutableListOf() } + } + "exclude" -> result.getValue(section!!).add( + parser.getAttributeValue(null, "domain") to + parser.getAttributeValue(null, "path"), + ) + } + } + return result + } + + @Test + fun `the manifest points at the data extraction rules`() { + // Robolectric does not fill ApplicationInfo.dataExtractionRulesRes, so + // read the merged manifest that the Android Gradle plugin hands it. + val props = java.util.Properties() + javaClass.classLoader!!.getResourceAsStream("com/android/tools/test_config.properties")!! + .use { props.load(it) } + val manifest = java.io.File(props.getProperty("android_merged_manifest")) + val doc = javax.xml.parsers.DocumentBuilderFactory.newInstance() + .apply { isNamespaceAware = true }.newDocumentBuilder().parse(manifest) + val app = doc.getElementsByTagName("application").item(0) as org.w3c.dom.Element + val android = "http://schemas.android.com/apk/res/android" + assertEquals("@xml/data_extraction_rules", app.getAttributeNS(android, "dataExtractionRules")) + } + + /** + * The path of the settings DataStore, relative to filesDir, from the same + * function the preferencesDataStore delegate uses. Nothing is written: the + * delegate is one store per process, bound to whichever test touched it + * first, so a file written from here can land in another test's filesDir. + */ + private fun settingsFile(): String = + context.preferencesDataStoreFile(SettingsStore.FILE_NAME).relativeTo(context.filesDir).path + + private fun assertExcluded(domain: String, file: String) { + val rules = excludes() + for (section in listOf("cloud-backup", "device-transfer")) { + val paths = rules[section].orEmpty() + .filter { it.first == domain }.map { it.second.trimEnd('/') } + assertTrue( + "$section does not exclude $domain:$file", + paths.any { it == "." || file == it || file.startsWith("$it/") }, + ) + } + } + + @Test + fun `the settings DataStore is kept out of cloud backup and device transfer`() { + assertEquals("datastore/cashpilot_settings.preferences_pb", settingsFile()) + assertExcluded("file", settingsFile()) + } + + @Test + fun `the heartbeat state is kept out of cloud backup and device transfer`() { + // Last server URL and earnings snapshot: no key, but a phone set up + // again starts clean, as allowBackup="false" intends. + assertExcluded("sharedpref", "${Heartbeat.PREFS}.xml") + } +}