From 09cf8aabf6f37005406681b1a9bd192cfa6605ee Mon Sep 17 00:00:00 2001 From: J-Jamet Date: Sun, 14 May 2023 21:56:11 +0200 Subject: [PATCH] Revert "fix: Base64 encoder as external library" This reverts commit 13f2003fed88daf653e9ea0655eb01b6c1ed198c. --- .../database/action/LoadDatabaseRunnable.kt | 4 +-- .../database/action/MergeDatabaseRunnable.kt | 4 +-- .../database/action/ReloadDatabaseRunnable.kt | 4 +-- .../keepass/database/helper/MemoryHelper.kt | 19 ------------ .../assets}/test_image.png | Bin .../assets}/test_text.txt | 0 .../com/kunzisoft/keepass/tests/.gitignore | 1 + .../keepass/tests/stream/BinaryDataTest.kt | 27 +++++++++--------- .../database/element/binary/BinaryByte.kt | 11 +++---- .../database/element/binary/BinaryData.kt | 13 +++++++++ .../database/element/binary/BinaryFile.kt | 9 +++--- 11 files changed, 44 insertions(+), 48 deletions(-) delete mode 100644 app/src/main/java/com/kunzisoft/keepass/database/helper/MemoryHelper.kt rename database/src/{test/resources => androidTest/assets}/test_image.png (100%) rename database/src/{test/resources => androidTest/assets}/test_text.txt (100%) create mode 100644 database/src/androidTest/java/com/kunzisoft/keepass/tests/.gitignore rename database/src/{test => androidTest}/java/com/kunzisoft/keepass/tests/stream/BinaryDataTest.kt (90%) diff --git a/app/src/main/java/com/kunzisoft/keepass/database/action/LoadDatabaseRunnable.kt b/app/src/main/java/com/kunzisoft/keepass/database/action/LoadDatabaseRunnable.kt index daa5773e1..7d2278c99 100644 --- a/app/src/main/java/com/kunzisoft/keepass/database/action/LoadDatabaseRunnable.kt +++ b/app/src/main/java/com/kunzisoft/keepass/database/action/LoadDatabaseRunnable.kt @@ -25,9 +25,9 @@ import com.kunzisoft.keepass.app.database.CipherDatabaseAction import com.kunzisoft.keepass.app.database.FileDatabaseHistoryAction import com.kunzisoft.keepass.database.ContextualDatabase import com.kunzisoft.keepass.database.MainCredential +import com.kunzisoft.keepass.database.element.binary.BinaryData import com.kunzisoft.keepass.database.exception.DatabaseInputException import com.kunzisoft.keepass.database.exception.UnknownDatabaseLocationException -import com.kunzisoft.keepass.database.helper.MemoryHelper.canMemoryBeAllocatedInRAM import com.kunzisoft.keepass.hardware.HardwareKey import com.kunzisoft.keepass.model.CipherEncryptDatabase import com.kunzisoft.keepass.settings.PreferencesUtil @@ -69,7 +69,7 @@ class LoadDatabaseRunnable( mReadonly, binaryDir, { memoryWanted -> - canMemoryBeAllocatedInRAM(context, memoryWanted) + BinaryData.canMemoryBeAllocatedInRAM(context, memoryWanted) }, mFixDuplicateUUID, progressTaskUpdater diff --git a/app/src/main/java/com/kunzisoft/keepass/database/action/MergeDatabaseRunnable.kt b/app/src/main/java/com/kunzisoft/keepass/database/action/MergeDatabaseRunnable.kt index 8315152d2..723d313d1 100644 --- a/app/src/main/java/com/kunzisoft/keepass/database/action/MergeDatabaseRunnable.kt +++ b/app/src/main/java/com/kunzisoft/keepass/database/action/MergeDatabaseRunnable.kt @@ -23,9 +23,9 @@ import android.content.Context import android.net.Uri import com.kunzisoft.keepass.database.ContextualDatabase import com.kunzisoft.keepass.database.MainCredential +import com.kunzisoft.keepass.database.element.binary.BinaryData import com.kunzisoft.keepass.database.exception.DatabaseException import com.kunzisoft.keepass.database.exception.UnknownDatabaseLocationException -import com.kunzisoft.keepass.database.helper.MemoryHelper.canMemoryBeAllocatedInRAM import com.kunzisoft.keepass.hardware.HardwareKey import com.kunzisoft.keepass.settings.PreferencesUtil import com.kunzisoft.keepass.tasks.ProgressTaskUpdater @@ -58,7 +58,7 @@ class MergeDatabaseRunnable( mDatabaseToMergeMainCredential?.toMasterCredential(contentResolver), mDatabaseToMergeChallengeResponseRetriever, { memoryWanted -> - canMemoryBeAllocatedInRAM(context, memoryWanted) + BinaryData.canMemoryBeAllocatedInRAM(context, memoryWanted) }, progressTaskUpdater ) diff --git a/app/src/main/java/com/kunzisoft/keepass/database/action/ReloadDatabaseRunnable.kt b/app/src/main/java/com/kunzisoft/keepass/database/action/ReloadDatabaseRunnable.kt index b2cee356a..6dc5b902f 100644 --- a/app/src/main/java/com/kunzisoft/keepass/database/action/ReloadDatabaseRunnable.kt +++ b/app/src/main/java/com/kunzisoft/keepass/database/action/ReloadDatabaseRunnable.kt @@ -21,9 +21,9 @@ package com.kunzisoft.keepass.database.action import android.content.Context import com.kunzisoft.keepass.database.ContextualDatabase +import com.kunzisoft.keepass.database.element.binary.BinaryData import com.kunzisoft.keepass.database.exception.DatabaseException import com.kunzisoft.keepass.database.exception.UnknownDatabaseLocationException -import com.kunzisoft.keepass.database.helper.MemoryHelper.canMemoryBeAllocatedInRAM import com.kunzisoft.keepass.settings.PreferencesUtil import com.kunzisoft.keepass.tasks.ActionRunnable import com.kunzisoft.keepass.tasks.ProgressTaskUpdater @@ -51,7 +51,7 @@ class ReloadDatabaseRunnable( context.contentResolver.getUriInputStream(mDatabase.fileUri) ?: throw UnknownDatabaseLocationException(), { memoryWanted -> - canMemoryBeAllocatedInRAM(context, memoryWanted) + BinaryData.canMemoryBeAllocatedInRAM(context, memoryWanted) }, progressTaskUpdater) } catch (e: DatabaseException) { diff --git a/app/src/main/java/com/kunzisoft/keepass/database/helper/MemoryHelper.kt b/app/src/main/java/com/kunzisoft/keepass/database/helper/MemoryHelper.kt deleted file mode 100644 index 6aa3630bc..000000000 --- a/app/src/main/java/com/kunzisoft/keepass/database/helper/MemoryHelper.kt +++ /dev/null @@ -1,19 +0,0 @@ -package com.kunzisoft.keepass.database.helper - -import android.app.ActivityManager -import android.content.Context - -object MemoryHelper { - - private const val MAX_BINARY_BYTE = 10485760 // 10 MB - - fun canMemoryBeAllocatedInRAM(context: Context, memoryWanted: Long): Boolean { - if (memoryWanted > MAX_BINARY_BYTE) - return false - val memoryInfo = ActivityManager.MemoryInfo() - (context.getSystemService(Context.ACTIVITY_SERVICE) - as? ActivityManager?)?.getMemoryInfo(memoryInfo) - val availableMemory = memoryInfo.availMem - return availableMemory > (memoryWanted * 5) - } -} \ No newline at end of file diff --git a/database/src/test/resources/test_image.png b/database/src/androidTest/assets/test_image.png similarity index 100% rename from database/src/test/resources/test_image.png rename to database/src/androidTest/assets/test_image.png diff --git a/database/src/test/resources/test_text.txt b/database/src/androidTest/assets/test_text.txt similarity index 100% rename from database/src/test/resources/test_text.txt rename to database/src/androidTest/assets/test_text.txt diff --git a/database/src/androidTest/java/com/kunzisoft/keepass/tests/.gitignore b/database/src/androidTest/java/com/kunzisoft/keepass/tests/.gitignore new file mode 100644 index 000000000..48c029696 --- /dev/null +++ b/database/src/androidTest/java/com/kunzisoft/keepass/tests/.gitignore @@ -0,0 +1 @@ +R.java diff --git a/database/src/test/java/com/kunzisoft/keepass/tests/stream/BinaryDataTest.kt b/database/src/androidTest/java/com/kunzisoft/keepass/tests/stream/BinaryDataTest.kt similarity index 90% rename from database/src/test/java/com/kunzisoft/keepass/tests/stream/BinaryDataTest.kt rename to database/src/androidTest/java/com/kunzisoft/keepass/tests/stream/BinaryDataTest.kt index 13a5da1a9..12ab302f4 100644 --- a/database/src/test/java/com/kunzisoft/keepass/tests/stream/BinaryDataTest.kt +++ b/database/src/androidTest/java/com/kunzisoft/keepass/tests/stream/BinaryDataTest.kt @@ -1,5 +1,7 @@ package com.kunzisoft.keepass.tests.stream +import android.content.Context +import androidx.test.platform.app.InstrumentationRegistry import com.kunzisoft.keepass.database.element.binary.BinaryCache import com.kunzisoft.keepass.database.element.binary.BinaryFile import com.kunzisoft.keepass.utils.readAllBytes @@ -12,22 +14,19 @@ import kotlin.random.Random class BinaryDataTest { - private val fileA = File.createTempFile(TEST_FILE_CACHE_A, null) - private val fileB = File.createTempFile(TEST_FILE_CACHE_B, null) - private val fileC = File.createTempFile(TEST_FILE_CACHE_C, null) + private val context: Context by lazy { + InstrumentationRegistry.getInstrumentation().context + } + + private val cacheDirectory = context.filesDir + private val fileA = File(cacheDirectory, TEST_FILE_CACHE_A) + private val fileB = File(cacheDirectory, TEST_FILE_CACHE_B) + private val fileC = File(cacheDirectory, TEST_FILE_CACHE_C) private val binaryCache = BinaryCache() - private fun buildFile(stringFile: String): File { - return File(javaClass.getResource("/$stringFile")!!.path) - } - - private fun buildFileStream(stringFile: String): InputStream { - return javaClass.getResourceAsStream("/$stringFile")!! - } - private fun saveBinary(asset: String, binaryData: BinaryFile) { - buildFileStream(asset).use { assetInputStream -> + context.assets.open(asset).use { assetInputStream -> binaryData.getOutputDataStream(binaryCache).use { binaryOutputStream -> assetInputStream.readAllBytes(DEFAULT_BUFFER_SIZE) { buffer -> binaryOutputStream.write(buffer) @@ -130,7 +129,7 @@ class BinaryDataTest { fun testReadText() { val binaryA = BinaryFile(fileA) saveBinary(TEST_TEXT_ASSET, binaryA) - assert(streamAreEquals(buildFileStream(TEST_TEXT_ASSET), + assert(streamAreEquals(context.assets.open(TEST_TEXT_ASSET), binaryA.getInputDataStream(binaryCache))) } @@ -138,7 +137,7 @@ class BinaryDataTest { fun testReadImage() { val binaryA = BinaryFile(fileA) saveBinary(TEST_IMAGE_ASSET, binaryA) - assert(streamAreEquals(buildFileStream(TEST_IMAGE_ASSET), + assert(streamAreEquals(context.assets.open(TEST_IMAGE_ASSET), binaryA.getInputDataStream(binaryCache))) } diff --git a/database/src/main/java/com/kunzisoft/keepass/database/element/binary/BinaryByte.kt b/database/src/main/java/com/kunzisoft/keepass/database/element/binary/BinaryByte.kt index 9a9abf34c..6f70c70dc 100644 --- a/database/src/main/java/com/kunzisoft/keepass/database/element/binary/BinaryByte.kt +++ b/database/src/main/java/com/kunzisoft/keepass/database/element/binary/BinaryByte.kt @@ -21,10 +21,11 @@ package com.kunzisoft.keepass.database.element.binary import android.os.Parcel import android.os.Parcelable -import com.kunzisoft.keepass.database.element.binary.BinaryCache.Companion.UNKNOWN +import android.util.Base64 +import android.util.Base64InputStream +import android.util.Base64OutputStream import com.kunzisoft.keepass.utils.readAllBytes -import org.apache.commons.codec.binary.Base64InputStream -import org.apache.commons.codec.binary.Base64OutputStream +import com.kunzisoft.keepass.database.element.binary.BinaryCache.Companion.UNKNOWN import java.io.* import java.util.zip.GZIPOutputStream @@ -59,12 +60,12 @@ class BinaryByte : BinaryData { @Throws(IOException::class) override fun getInputDataStream(binaryCache: BinaryCache): InputStream { - return Base64InputStream(ByteArrayInputStream(getByteArray(binaryCache)), false) + return Base64InputStream(ByteArrayInputStream(getByteArray(binaryCache)), Base64.NO_WRAP) } @Throws(IOException::class) override fun getOutputDataStream(binaryCache: BinaryCache): OutputStream { - return BinaryCountingOutputStream(Base64OutputStream(ByteOutputStream(binaryCache), true)) + return BinaryCountingOutputStream(Base64OutputStream(ByteOutputStream(binaryCache), Base64.NO_WRAP)) } @Throws(IOException::class) diff --git a/database/src/main/java/com/kunzisoft/keepass/database/element/binary/BinaryData.kt b/database/src/main/java/com/kunzisoft/keepass/database/element/binary/BinaryData.kt index a0a154fe4..e1ab900b8 100644 --- a/database/src/main/java/com/kunzisoft/keepass/database/element/binary/BinaryData.kt +++ b/database/src/main/java/com/kunzisoft/keepass/database/element/binary/BinaryData.kt @@ -19,6 +19,8 @@ */ package com.kunzisoft.keepass.database.element.binary +import android.app.ActivityManager +import android.content.Context import android.os.Parcel import android.os.Parcelable import org.apache.commons.io.output.CountingOutputStream @@ -178,6 +180,17 @@ abstract class BinaryData : Parcelable { companion object { private val TAG = BinaryData::class.java.name + private const val MAX_BINARY_BYTE = 10485760 // 10 MB + + fun canMemoryBeAllocatedInRAM(context: Context, memoryWanted: Long): Boolean { + if (memoryWanted > MAX_BINARY_BYTE) + return false + val memoryInfo = ActivityManager.MemoryInfo() + (context.getSystemService(Context.ACTIVITY_SERVICE) + as? ActivityManager?)?.getMemoryInfo(memoryInfo) + val availableMemory = memoryInfo.availMem + return availableMemory > (memoryWanted * 5) + } } } diff --git a/database/src/main/java/com/kunzisoft/keepass/database/element/binary/BinaryFile.kt b/database/src/main/java/com/kunzisoft/keepass/database/element/binary/BinaryFile.kt index d550f6b97..1def0948e 100644 --- a/database/src/main/java/com/kunzisoft/keepass/database/element/binary/BinaryFile.kt +++ b/database/src/main/java/com/kunzisoft/keepass/database/element/binary/BinaryFile.kt @@ -21,9 +21,10 @@ package com.kunzisoft.keepass.database.element.binary import android.os.Parcel import android.os.Parcelable +import android.util.Base64 +import android.util.Base64InputStream +import android.util.Base64OutputStream import com.kunzisoft.keepass.utils.readAllBytes -import org.apache.commons.codec.binary.Base64InputStream -import org.apache.commons.codec.binary.Base64OutputStream import java.io.* import java.util.zip.GZIPOutputStream import javax.crypto.Cipher @@ -74,7 +75,7 @@ class BinaryFile : BinaryData { return when { file != null && file.length() > 0 -> { cipherDecryption.init(Cipher.DECRYPT_MODE, cipherKey.key, IvParameterSpec(cipherKey.iv)) - Base64InputStream(CipherInputStream(FileInputStream(file), cipherDecryption), false) + Base64InputStream(CipherInputStream(FileInputStream(file), cipherDecryption), Base64.NO_WRAP) } else -> ByteArrayInputStream(ByteArray(0)) } @@ -86,7 +87,7 @@ class BinaryFile : BinaryData { return when { file != null -> { cipherEncryption.init(Cipher.ENCRYPT_MODE, cipherKey.key, IvParameterSpec(cipherKey.iv)) - BinaryCountingOutputStream(Base64OutputStream(CipherOutputStream(FileOutputStream(file), cipherEncryption), true)) + BinaryCountingOutputStream(Base64OutputStream(CipherOutputStream(FileOutputStream(file), cipherEncryption), Base64.NO_WRAP)) } else -> throw IOException("Unable to write in an unknown file") }