Repository navigation
feat: Add testing Block Store #870
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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
42 changes: 42 additions & 0 deletions
42
Auth/src/standard/kotlin/com/infomaniak/core/auth/backup/BlockStore.kt
This file contains hidden or 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,42 @@ | ||
| /* | ||
| * Infomaniak Core - Android | ||
| * Copyright (C) 2026 Infomaniak Network SA | ||
| * | ||
| * This program is free software: you can redistribute it and/or modify | ||
| * it under the terms of the GNU General Public License as published by | ||
| * the Free Software Foundation, either version 3 of the License, or | ||
| * (at your option) any later version. | ||
| * | ||
| * This program is distributed in the hope that it will be useful, | ||
| * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| * GNU General Public License for more details. | ||
| * | ||
| * You should have received a copy of the GNU General Public License | ||
| * along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
| */ | ||
| package com.infomaniak.core.auth.backup | ||
|
|
||
| import android.app.backup.FullBackupDataOutput | ||
| import android.content.pm.ApplicationInfo | ||
| import com.infomaniak.core.common.backup.FullBackupAgent | ||
| import splitties.bitflags.hasFlag | ||
| import splitties.init.appCtx | ||
|
|
||
| sealed class BlockStore { | ||
|
|
||
| abstract suspend fun storeBytes(key: String, shouldBackupToCloud: Boolean, bytes: ByteArray) | ||
| abstract suspend fun retrieveBytes(keys: List<String>? = null): Map<String, ByteArray> | ||
| abstract suspend fun deleteBytes(keys: List<String>): Boolean | ||
| abstract suspend fun isE2eeAvailable(): Boolean | ||
|
|
||
| context(agent: FullBackupAgent) | ||
| abstract fun backupTestBlockStoreIfNeeded(data: FullBackupDataOutput) | ||
|
|
||
| companion object { | ||
| val instance: BlockStore = when { | ||
| appCtx.applicationInfo.flags.hasFlag(ApplicationInfo.FLAG_DEBUGGABLE) -> BlockStoreTestingImpl(e2eeAvailable = true) | ||
| else -> BlockStorePlayServicesImpl() | ||
| } | ||
| } | ||
| } |
This file contains hidden or 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
62 changes: 62 additions & 0 deletions
62
Auth/src/standard/kotlin/com/infomaniak/core/auth/backup/BlockStorePlayServicesImpl.kt
This file contains hidden or 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,62 @@ | ||
| /* | ||
| * Infomaniak Core - Android | ||
| * Copyright (C) 2026 Infomaniak Network SA | ||
| * | ||
| * This program is free software: you can redistribute it and/or modify | ||
| * it under the terms of the GNU General Public License as published by | ||
| * the Free Software Foundation, either version 3 of the License, or | ||
| * (at your option) any later version. | ||
| * | ||
| * This program is distributed in the hope that it will be useful, | ||
| * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| * GNU General Public License for more details. | ||
| * | ||
| * You should have received a copy of the GNU General Public License | ||
| * along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
| */ | ||
| package com.infomaniak.core.auth.backup | ||
|
|
||
| import android.app.backup.FullBackupDataOutput | ||
| import com.google.android.gms.auth.blockstore.Blockstore | ||
| import com.google.android.gms.auth.blockstore.DeleteBytesRequest | ||
| import com.google.android.gms.auth.blockstore.RetrieveBytesRequest | ||
| import com.google.android.gms.auth.blockstore.StoreBytesData | ||
| import com.infomaniak.core.common.backup.FullBackupAgent | ||
| import kotlinx.coroutines.tasks.await | ||
| import splitties.init.appCtx | ||
|
|
||
| internal class BlockStorePlayServicesImpl : BlockStore() { | ||
| private val client = Blockstore.getClient(appCtx) | ||
|
|
||
| override suspend fun storeBytes( | ||
| key: String, | ||
| shouldBackupToCloud: Boolean, | ||
| bytes: ByteArray | ||
| ) { | ||
| val storeRequest = StoreBytesData.Builder() | ||
| .setKey(key) | ||
| .setShouldBackupToCloud(shouldBackupToCloud) | ||
| .setBytes(bytes) | ||
| .build() | ||
| client.storeBytes(storeRequest).await() | ||
| } | ||
|
|
||
| override suspend fun retrieveBytes(keys: List<String>?): Map<String, ByteArray> { | ||
| val retrieveRequest = RetrieveBytesRequest.Builder() | ||
| .setKeys(keys ?: emptyList()) | ||
| .setRetrieveAll(keys == null) | ||
| .build() | ||
| return client.retrieveBytes(retrieveRequest).await().blockstoreDataMap.mapValues { it.value.bytes } | ||
| } | ||
|
|
||
| override suspend fun deleteBytes(keys: List<String>): Boolean { | ||
| val deleteRequest = DeleteBytesRequest.Builder().setKeys(keys).build() | ||
| return client.deleteBytes(deleteRequest).await() | ||
| } | ||
|
|
||
| override suspend fun isE2eeAvailable(): Boolean = client.isEndToEndEncryptionAvailable.await() | ||
|
|
||
| context(agent: FullBackupAgent) | ||
| override fun backupTestBlockStoreIfNeeded(data: FullBackupDataOutput) = Unit | ||
| } |
84 changes: 84 additions & 0 deletions
84
Auth/src/standard/kotlin/com/infomaniak/core/auth/backup/BlockStoreTestingImpl.kt
This file contains hidden or 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,84 @@ | ||
| /* | ||
| * Infomaniak Core - Android | ||
| * Copyright (C) 2026 Infomaniak Network SA | ||
| * | ||
| * This program is free software: you can redistribute it and/or modify | ||
| * it under the terms of the GNU General Public License as published by | ||
| * the Free Software Foundation, either version 3 of the License, or | ||
| * (at your option) any later version. | ||
| * | ||
| * This program is distributed in the hope that it will be useful, | ||
| * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| * GNU General Public License for more details. | ||
| * | ||
| * You should have received a copy of the GNU General Public License | ||
| * along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
| */ | ||
| @file:OptIn(ExperimentalAtomicApi::class) | ||
|
|
||
| package com.infomaniak.core.auth.backup | ||
|
|
||
| import android.app.backup.FullBackupDataOutput | ||
| import com.infomaniak.core.common.backup.FullBackupAgent | ||
| import com.infomaniak.core.common.backup.isDeviceToDeviceTransfer | ||
| import kotlinx.coroutines.Dispatchers | ||
| import kotlinx.coroutines.invoke | ||
| import splitties.init.appCtx | ||
| import java.io.File | ||
| import kotlin.concurrent.atomics.ExperimentalAtomicApi | ||
|
|
||
| internal class BlockStoreTestingImpl(private val e2eeAvailable: Boolean) : BlockStore() { | ||
|
|
||
| private val dir by lazy { appCtx.filesDir.resolve("debugBlockStore").also { it.mkdir() } } | ||
|
LouisCAD marked this conversation as resolved.
Resolved
|
||
| private val shouldBackupToCloudSuffix = "-cloud-ok" | ||
|
|
||
| override suspend fun storeBytes(key: String, shouldBackupToCloud: Boolean, bytes: ByteArray) { | ||
| Dispatchers.IO { | ||
| val cloudOkFile = dir.resolve(key + shouldBackupToCloudSuffix) | ||
| val deviceToDeviceOnlyFile = dir.resolve(key) | ||
|
LouisCAD marked this conversation as resolved.
Resolved
|
||
| val file = if (shouldBackupToCloud) cloudOkFile else deviceToDeviceOnlyFile | ||
| file.writeBytes(bytes) | ||
| val fileToDelete = if (shouldBackupToCloud) deviceToDeviceOnlyFile else cloudOkFile | ||
| fileToDelete.delete() | ||
| } | ||
| } | ||
|
|
||
| override suspend fun retrieveBytes(keys: List<String>?): Map<String, ByteArray> = Dispatchers.IO { | ||
| val targetKeys = keys ?: getStoredKeys() | ||
| buildMap(capacity = targetKeys.size) { | ||
| targetKeys.forEach { key -> | ||
| val bytes = fileForKey(key)?.readBytes() ?: return@forEach | ||
| this[key] = bytes | ||
| } | ||
| } | ||
| } | ||
|
|
||
| override suspend fun deleteBytes(keys: List<String>): Boolean = Dispatchers.IO { | ||
| var deleted = false | ||
| keys.forEach { key -> | ||
| deleted = dir.resolve(key).delete() || deleted | ||
| deleted = dir.resolve(key + shouldBackupToCloudSuffix).delete() || deleted | ||
| } | ||
| deleted | ||
| } | ||
|
|
||
| override suspend fun isE2eeAvailable(): Boolean = e2eeAvailable | ||
|
|
||
| context(agent: FullBackupAgent) | ||
| override fun backupTestBlockStoreIfNeeded(data: FullBackupDataOutput) { | ||
| dir.listFiles()?.forEach { file -> | ||
| if (data.isDeviceToDeviceTransfer || file.name.endsWith(shouldBackupToCloudSuffix)) { | ||
| agent.fullBackupFile(file, data) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| private fun fileForKey(key: String): File? { | ||
| return dir.resolve(key).takeIf { it.exists() } ?: dir.resolve(key + shouldBackupToCloudSuffix).takeIf { it.exists() } | ||
| } | ||
|
|
||
| private fun getStoredKeys(): List<String> { | ||
| return (dir.list()?.asList() ?: emptyList<String>()).map { it.removeSuffix(shouldBackupToCloudSuffix) } | ||
| } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.