Add encryption option
This commit is contained in:
54
src/test/kotlin/nl/astraeus/persistence/EncryptionTest.kt
Normal file
54
src/test/kotlin/nl/astraeus/persistence/EncryptionTest.kt
Normal file
@@ -0,0 +1,54 @@
|
||||
package nl.astraeus.persistence
|
||||
|
||||
import org.junit.jupiter.api.Test
|
||||
|
||||
import org.junit.jupiter.api.Assertions.*
|
||||
import java.io.ByteArrayInputStream
|
||||
import java.io.ByteArrayOutputStream
|
||||
import java.security.SecureRandom
|
||||
|
||||
class EncryptionTest {
|
||||
|
||||
@Test
|
||||
fun testKeyGen() {
|
||||
println(generateBase64Key())
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testEncryptDecrypt() {
|
||||
val random = SecureRandom()
|
||||
val randomBytes = ByteArray(random.nextInt(10000))
|
||||
random.nextBytes(randomBytes)
|
||||
val base64Key = generateBase64Key()
|
||||
val encryptor = Encryptor(
|
||||
base64Key,
|
||||
base64Key,
|
||||
)
|
||||
|
||||
val encrypted = encryptor.encrypt(randomBytes)
|
||||
val decrypted = encryptor.decrypt(encrypted)
|
||||
|
||||
assertArrayEquals(randomBytes, decrypted)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `test encryption-decryption streams`() {
|
||||
val random = SecureRandom()
|
||||
val key = generateBase64Key()
|
||||
val baos = ByteArrayOutputStream()
|
||||
val encryptionStream = EncryptingOutputStream(baos, key)
|
||||
val bytes = ByteArray(random.nextInt(10000))
|
||||
random.nextBytes(bytes)
|
||||
|
||||
encryptionStream.use {
|
||||
it.write(bytes)
|
||||
}
|
||||
|
||||
val bais = ByteArrayInputStream(baos.toByteArray())
|
||||
val decryptingStream = DecryptingInputStream(bais, key)
|
||||
|
||||
val decryptedBytes = decryptingStream.readAllBytes()
|
||||
|
||||
assertArrayEquals(bytes, decryptedBytes)
|
||||
}
|
||||
}
|
||||
@@ -21,10 +21,12 @@ class TestOptimisticLocking {
|
||||
|
||||
val pst = Persistent(
|
||||
directory = File("data", "test-locking"),
|
||||
true,
|
||||
null,
|
||||
null,
|
||||
arrayOf(
|
||||
index<Person>("name") { p -> (p as? Person)?.name ?: "" },
|
||||
),
|
||||
true
|
||||
)
|
||||
|
||||
pst.transaction {
|
||||
|
||||
@@ -22,7 +22,7 @@ class TestPersistence {
|
||||
|
||||
val pst = Persistent(
|
||||
directory = File("data", "test-persistence"),
|
||||
arrayOf(
|
||||
indexes = arrayOf(
|
||||
index<Person>("name") { p -> (p as? Person)?.name ?: "" },
|
||||
index<Person>("age") { p -> (p as? Person)?.age ?: -1 },
|
||||
index<Person>("ageGt20") { p -> ((p as? Person)?.age ?: 0) > 20 },
|
||||
@@ -131,7 +131,7 @@ class TestPersistence {
|
||||
}
|
||||
}
|
||||
|
||||
//pst.snapshot()
|
||||
pst.snapshot()
|
||||
|
||||
pst.transaction {
|
||||
store(
|
||||
@@ -179,6 +179,6 @@ class TestPersistence {
|
||||
}
|
||||
|
||||
pst.datastore.printStatus()
|
||||
//pst.removeOldFiles()
|
||||
pst.removeOldFiles()
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3,8 +3,8 @@ package nl.astraeus.persistence
|
||||
import org.junit.jupiter.api.Test
|
||||
import java.io.File
|
||||
|
||||
|
||||
class TestPersistenceJavaInKotlin {
|
||||
|
||||
internal class Person(
|
||||
var name: String,
|
||||
var age: Int
|
||||
@@ -23,19 +23,21 @@ class TestPersistenceJavaInKotlin {
|
||||
|
||||
val persistent = Persistent(
|
||||
File("data", "java-kotlin-test"),
|
||||
arrayOf(
|
||||
enableOptimisticLocking = false,
|
||||
indexes = arrayOf(
|
||||
Index(
|
||||
Person::class,
|
||||
"name"
|
||||
) { p -> (p as Person).name }
|
||||
),
|
||||
false
|
||||
)
|
||||
|
||||
persistent.transaction {
|
||||
val person = find(Person::class.java, 1L)
|
||||
|
||||
if (person != null) {
|
||||
println("Person: ${person.name} is ${person.age} years old."
|
||||
println(
|
||||
"Person: ${person.name} is ${person.age} years old."
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -22,7 +22,7 @@ class TestThreaded {
|
||||
|
||||
val pst = Persistent(
|
||||
directory = File("data", "test-threaded"),
|
||||
arrayOf(
|
||||
indexes = arrayOf(
|
||||
index<Person>("name") { p -> (p as? Person)?.name ?: "" },
|
||||
index<Person>("age") { p -> (p as? Person)?.age ?: -1 },
|
||||
index<Person>("ageGt20") { p -> ((p as? Person)?.age ?: 0) > 20 },
|
||||
|
||||
Reference in New Issue
Block a user