Skip to content

Commit

Permalink
Merge pull request #2 from RomanTsisyk/add-qa-code-handling-functiona…
Browse files Browse the repository at this point in the history
…lity

Add Unit and Instrumentation Tests for QR Code Features
  • Loading branch information
RomanTsisyk authored Nov 28, 2024
2 parents 1d5bbde + 9afbb12 commit dc70bdd
Show file tree
Hide file tree
Showing 12 changed files with 221 additions and 71 deletions.
88 changes: 88 additions & 0 deletions .idea/androidTestResultsUserPreferences.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 16 additions & 0 deletions .idea/deploymentTargetSelector.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 3 additions & 2 deletions cryptolib/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,9 @@ dependencies {

// Unit testing libraries
testImplementation ("junit:junit:4.13.2")
testImplementation ("org.mockito:mockito-core:5.4.0")
testImplementation ("org.robolectric:robolectric:4.9")
testImplementation ("io.mockk:mockk:1.13.12") // Or
testImplementation ("org.mockito:mockito-core:5.14.2")
testImplementation ("org.robolectric:robolectric:4.14.1")

// Android instrumentation testing libraries
androidTestImplementation ("androidx.test.ext:junit:1.2.1")
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package io.github.romantsisyk.cryptolib.qr

import android.graphics.Bitmap
import androidx.test.ext.junit.runners.AndroidJUnit4
import io.github.romantsisyk.cryptolib.crypto.qr.QRCodeGenerator
import org.junit.Assert.*
import org.junit.Test
import org.junit.runner.RunWith

@RunWith(AndroidJUnit4::class)
class QRCodeGeneratorTest {

@Test
fun testGenerateQRCode() {
val bitmap: Bitmap = QRCodeGenerator.generateQRCode("Test")
assertNotNull(bitmap)
assertEquals(300, bitmap.width) // Ensure the QR code is 300x300 pixels
assertEquals(300, bitmap.height)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package io.github.romantsisyk.cryptolib.qr

import android.graphics.Bitmap
import androidx.test.ext.junit.runners.AndroidJUnit4
import io.github.romantsisyk.cryptolib.crypto.qr.QRCodeGenerator
import io.github.romantsisyk.cryptolib.crypto.qr.QRCodeScanner
import junit.framework.TestCase.assertEquals
import junit.framework.TestCase.assertNull
import org.junit.Test
import org.junit.runner.RunWith

@RunWith(AndroidJUnit4::class)
class QRCodeScannerTest {

@Test
fun testDecodeQRCodeValid() {
val bitmap = QRCodeGenerator.generateQRCode("Valid QR Code")
val result = QRCodeScanner.decodeQRCode(bitmap)
assertEquals("Valid QR Code", result)
}

@Test
fun testDecodeQRCodeInvalid() {
val bitmap = Bitmap.createBitmap(100, 100, Bitmap.Config.ARGB_8888)
val result = QRCodeScanner.decodeQRCode(bitmap)
assertNull(result)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package io.github.romantsisyk.cryptolib.qr

import androidx.test.ext.junit.runners.AndroidJUnit4
import io.github.romantsisyk.cryptolib.crypto.qr.QRKeyManager
import io.github.romantsisyk.cryptolib.crypto.qr.QRUtils
import junit.framework.TestCase.assertEquals
import junit.framework.TestCase.assertNotNull
import org.junit.Test
import org.junit.runner.RunWith

@RunWith(AndroidJUnit4::class)
class QRUtilsTest {

@Test
fun testEncryptDecryptWithRealKey() {
val key = QRKeyManager.generateKey()
assertNotNull(key)

val data = "Test Data"
val (encryptedData, iv) = QRUtils.encryptData(data, key)
assertNotNull(encryptedData)
assertNotNull(iv)

val decryptedData = QRUtils.decryptData(encryptedData, key, iv)
assertEquals(data, decryptedData)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,10 @@ object QRKeyManager {
keyStore.load(null)
return keyStore.getKey(ALIAS, null) as SecretKey
}

// fun generateKey(): SecretKey {
// val keyGenerator = KeyGenerator.getInstance("AES", "AndroidKeyStore")
// keyGenerator.init(256) // Use appropriate key size
// return keyGenerator.generateKey()
// }
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,27 @@ package io.github.romantsisyk.cryptolib.crypto.qr
import javax.crypto.Cipher
import javax.crypto.SecretKey
import android.util.Base64
import javax.crypto.spec.GCMParameterSpec

object QRUtils {

private const val TRANSFORMATION = "AES"
object QRUtils {
private const val TRANSFORMATION = "AES/GCM/NoPadding"
private const val TAG_LENGTH_BIT = 128

fun encryptData(data: String, key: SecretKey): String {
fun encryptData(data: String, key: SecretKey): Pair<String, ByteArray> {
val cipher = Cipher.getInstance(TRANSFORMATION)
cipher.init(Cipher.ENCRYPT_MODE, key)
val iv = cipher.iv // Initialization vector
val encryptedBytes = cipher.doFinal(data.toByteArray())
return Base64.encodeToString(encryptedBytes, Base64.DEFAULT)
return Base64.encodeToString(encryptedBytes, Base64.DEFAULT) to iv
}

fun decryptData(encryptedData: String, key: SecretKey): String {
fun decryptData(encryptedData: String, key: SecretKey, iv: ByteArray): String {
val cipher = Cipher.getInstance(TRANSFORMATION)
cipher.init(Cipher.DECRYPT_MODE, key)
val gcmSpec = GCMParameterSpec(TAG_LENGTH_BIT, iv)
cipher.init(Cipher.DECRYPT_MODE, key, gcmSpec)
val decodedBytes = Base64.decode(encryptedData, Base64.DEFAULT)
val decryptedBytes = cipher.doFinal(decodedBytes)
return String(decryptedBytes)
}
}
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package io.github.romantsisyk.cryptolib.crypto.qr

import android.graphics.Bitmap
import org.junit.Assert.*
import org.junit.Test
import org.robolectric.RobolectricTestRunner
import org.junit.runner.RunWith

@RunWith(RobolectricTestRunner::class)
class BitmapLuminanceSourceTest {

@Test
fun testGetMatrix() {
val bitmap = Bitmap.createBitmap(2, 2, Bitmap.Config.ARGB_8888)
bitmap.setPixel(0, 0, 0xFF000000.toInt()) // Black
bitmap.setPixel(0, 1, 0xFFFFFFFF.toInt()) // White

val source = BitmapLuminanceSource(bitmap)
val matrix = source.matrix
assertEquals(4, matrix.size)
}
}

This file was deleted.

0 comments on commit dc70bdd

Please sign in to comment.