-
Notifications
You must be signed in to change notification settings - Fork 143
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Configure Kotlin for testing We are going to write Kotlin in JavaThemis to avoid adding new run-time dependencies. Kotlin has great interop with Java due to this JVM thingie. However, it will be nice to test our Java code with Kotlin to ensure that our API works as intended. Use the current latest stable version of Kotlin, configuruable via the "kotlin_version" variable defined in the root project. There we also add Maven Central repos where Kotlin pluggin and runtime are hosted. Each subproject has Kotlin configured for it, even BoringSSL that does not have any Kotlin code, but must have relevant targets as a dependency. Note that desktop Java and Android use different plugin because Kotlin compiles a little bit different on Android. Kotlin runtime is required to run Kotlin code. Since we use Kotlin only for tests, the runtime is added as test dependency. We build our code for Java 1.7 so an appropriate runtime flavor is used. Adding Kotlin also triggers a weird compilation error in Android's DEX compiler as Kotlin runtime uses a lot of JetBrains annotations. This is a known issue encountered by some project, and there is a workaround. * Kotlin API tests Add test suite written in Kotlin. This code has been automatically converted by IntelliJ from Java test suite and cleaned up manually after that. One painful point is exception testing, the try-catch idiom from Java looks really awful in Kotlin so I've simply polyfilled assertThrows() method which is not available with Java 1.7. Kotlin can translate its lambdas into Runnables so it works pretty well. * Changelog entries Note that Java 8 support is now required since Kotlin compiler needs it.
- Loading branch information
Showing
10 changed files
with
1,223 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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 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 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 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
37 changes: 37 additions & 0 deletions
37
tests/themis/wrappers/android/com/cossacklabs/themis/test/Assert.java
This file contains 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,37 @@ | ||
/* | ||
* Copyright (c) 2020 Cossack Labs Limited | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.cossacklabs.themis.test; | ||
|
||
import org.jetbrains.annotations.NotNull; | ||
|
||
// We use Java 7 -- particularly for Android support -- where JUnit does not have "assertThrows()". | ||
// That makes tests very verbose and repetitive. Provide a polyfill a for it. | ||
|
||
final class Assert { | ||
static <T extends Throwable> T assertThrows(@NotNull Class<T> expected, Runnable runnable) { | ||
try { | ||
runnable.run(); | ||
} | ||
catch (Throwable e) { | ||
if (expected.isInstance(e)) { | ||
return expected.cast(e); | ||
} | ||
throw new AssertionError("expected exception of type "+ expected, e); | ||
} | ||
throw new AssertionError("no expected exception of type " + expected + " was thrown"); | ||
} | ||
} |
208 changes: 208 additions & 0 deletions
208
...themis/wrappers/android/com/cossacklabs/themis/test/SecureCellContextImprintTestKotlin.kt
This file contains 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,208 @@ | ||
/* | ||
* Copyright (c) 2020 Cossack Labs Limited | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package com.cossacklabs.themis.test | ||
|
||
import java.nio.charset.StandardCharsets | ||
import kotlin.experimental.inv | ||
|
||
import com.cossacklabs.themis.* | ||
import com.cossacklabs.themis.test.Assert.assertThrows | ||
|
||
import org.junit.Assert.* | ||
import org.junit.Test | ||
|
||
class SecureCellContextImprintTestKotlin { | ||
@Test | ||
fun initWithGenerated() { | ||
val cell = SecureCell.ContextImprintWithKey(SymmetricKey()) | ||
assertNotNull(cell) | ||
} | ||
|
||
@Test | ||
fun initWithFixed() { | ||
val keyBase64 = "UkVDMgAAAC13PCVZAKOczZXUpvkhsC+xvwWnv3CLmlG0Wzy8ZBMnT+2yx/dg" | ||
val keyBytes = Base64.getDecoder().decode(keyBase64) | ||
val cell = SecureCell.ContextImprintWithKey(keyBytes) | ||
assertNotNull(cell) | ||
} | ||
|
||
@Test | ||
fun initWithEmpty() { | ||
assertThrows(NullArgumentException::class.java) { | ||
SecureCell.ContextImprintWithKey(null as SymmetricKey?) | ||
} | ||
assertThrows(NullArgumentException::class.java) { | ||
SecureCell.ContextImprintWithKey(null as ByteArray?) | ||
} | ||
assertThrows(InvalidArgumentException::class.java) { | ||
SecureCell.ContextImprintWithKey(byteArrayOf()) | ||
} | ||
} | ||
|
||
@Test | ||
fun roundtrip() { | ||
val cell = SecureCell.ContextImprintWithKey(SymmetricKey()) | ||
val message = "All your base are belong to us!".toByteArray(StandardCharsets.UTF_8) | ||
val context = "For great justice".toByteArray(StandardCharsets.UTF_8) | ||
|
||
val encrypted = cell.encrypt(message, context) | ||
assertNotNull(encrypted) | ||
|
||
val decrypted = cell.decrypt(encrypted, context) | ||
assertNotNull(decrypted) | ||
assertArrayEquals(message, decrypted) | ||
} | ||
|
||
@Test | ||
fun dataLengthPreservation() { | ||
val cell = SecureCell.ContextImprintWithKey(SymmetricKey()) | ||
val message = "All your base are belong to us!".toByteArray(StandardCharsets.UTF_8) | ||
val context = "For great justice".toByteArray(StandardCharsets.UTF_8) | ||
|
||
val encrypted = cell.encrypt(message, context) | ||
assertEquals(message.size.toLong(), encrypted.size.toLong()) | ||
} | ||
|
||
@Test | ||
fun contextInclusion() { | ||
val cell = SecureCell.ContextImprintWithKey(SymmetricKey()) | ||
val message = "All your base are belong to us!".toByteArray(StandardCharsets.UTF_8) | ||
val shortContext = ".".toByteArray(StandardCharsets.UTF_8) | ||
val longContext = "You have no chance to survive make your time. Ha ha ha ha ...".toByteArray(StandardCharsets.UTF_8) | ||
|
||
val encryptedShort = cell.encrypt(message, shortContext) | ||
val encryptedLong = cell.encrypt(message, longContext) | ||
|
||
// Context is not (directly) included into encrypted message. | ||
assertEquals(encryptedShort.size.toLong(), encryptedLong.size.toLong()) | ||
} | ||
|
||
@Test | ||
fun contextSignificance() { | ||
val cell = SecureCell.ContextImprintWithKey(SymmetricKey()) | ||
val message = "All your base are belong to us!".toByteArray(StandardCharsets.UTF_8) | ||
val correctContext = "We are CATS".toByteArray(StandardCharsets.UTF_8) | ||
val incorrectContext = "Captain !!".toByteArray(StandardCharsets.UTF_8) | ||
|
||
val encrypted = cell.encrypt(message, correctContext) | ||
|
||
// You can use a different context to decrypt data, but you'll get garbage. | ||
var decrypted = cell.decrypt(encrypted, incorrectContext) | ||
assertNotNull(decrypted) | ||
assertEquals(message.size.toLong(), decrypted.size.toLong()) | ||
assertFalse(message.contentEquals(decrypted)) | ||
|
||
// Only the original context will work. | ||
decrypted = cell.decrypt(encrypted, correctContext) | ||
assertArrayEquals(message, decrypted) | ||
} | ||
|
||
@Test | ||
fun noDetectCorruptedData() { | ||
val cell = SecureCell.ContextImprintWithKey(SymmetricKey()) | ||
val message = "All your base are belong to us!".toByteArray(StandardCharsets.UTF_8) | ||
val context = "We are CATS".toByteArray(StandardCharsets.UTF_8) | ||
|
||
val encrypted = cell.encrypt(message, context) | ||
|
||
// Invert every odd byte, this will surely break the message. | ||
val corrupted = encrypted.copyOf(encrypted.size) | ||
for (i in corrupted.indices) { | ||
if (i % 2 == 1) { | ||
corrupted[i] = corrupted[i].inv() | ||
} | ||
} | ||
|
||
// Decrypts successfully but the content is garbage. | ||
val decrypted = cell.decrypt(corrupted, context) | ||
assertNotNull(decrypted) | ||
assertEquals(message.size.toLong(), decrypted.size.toLong()) | ||
assertFalse(message.contentEquals(decrypted)) | ||
} | ||
|
||
@Test | ||
fun noDetectTruncatedData() { | ||
val cell = SecureCell.ContextImprintWithKey(SymmetricKey()) | ||
val message = "All your base are belong to us!".toByteArray(StandardCharsets.UTF_8) | ||
val context = "We are CATS".toByteArray(StandardCharsets.UTF_8) | ||
|
||
val encrypted = cell.encrypt(message, context) | ||
val truncated = encrypted.copyOf(encrypted.size - 1) | ||
|
||
// Decrypts successfully but the content is garbage. | ||
val decrypted = cell.decrypt(truncated, context) | ||
assertNotNull(decrypted) | ||
assertEquals(truncated.size.toLong(), decrypted.size.toLong()) | ||
assertFalse(message.contentEquals(decrypted)) | ||
} | ||
|
||
@Test | ||
fun noDetectExtendedData() { | ||
val cell = SecureCell.ContextImprintWithKey(SymmetricKey()) | ||
val message = "All your base are belong to us!".toByteArray(StandardCharsets.UTF_8) | ||
val context = "We are CATS".toByteArray(StandardCharsets.UTF_8) | ||
|
||
val encrypted = cell.encrypt(message, context) | ||
val extended = encrypted.copyOf(encrypted.size + 1) | ||
|
||
// Decrypts successfully but the content is garbage. | ||
val decrypted = cell.decrypt(extended, context) | ||
assertNotNull(decrypted) | ||
assertEquals(extended.size.toLong(), decrypted.size.toLong()) | ||
assertFalse(message.contentEquals(decrypted)) | ||
} | ||
|
||
@Test | ||
fun requiredMessageAndContext() { | ||
val cell = SecureCell.ContextImprintWithKey(SymmetricKey()) | ||
val message = "All your base are belong to us!".toByteArray(StandardCharsets.UTF_8) | ||
val context = "We are CATS".toByteArray(StandardCharsets.UTF_8) | ||
|
||
assertThrows(NullArgumentException::class.java) { cell.encrypt(message, null) } | ||
assertThrows(NullArgumentException::class.java) { cell.decrypt(message, null) } | ||
assertThrows(NullArgumentException::class.java) { cell.encrypt(null, context) } | ||
assertThrows(NullArgumentException::class.java) { cell.decrypt(null, context) } | ||
|
||
assertThrows(InvalidArgumentException::class.java) { cell.encrypt(message, byteArrayOf()) } | ||
assertThrows(InvalidArgumentException::class.java) { cell.decrypt(message, byteArrayOf()) } | ||
assertThrows(InvalidArgumentException::class.java) { cell.encrypt(byteArrayOf(), context) } | ||
assertThrows(InvalidArgumentException::class.java) { cell.decrypt(byteArrayOf(), context) } | ||
} | ||
|
||
@Test | ||
@Suppress("DEPRECATION") | ||
@Throws(SecureCellException::class) | ||
fun oldAPI() { | ||
val key = SymmetricKey() | ||
val newCell = SecureCell.ContextImprintWithKey(key) | ||
val oldCell = SecureCell(key.toByteArray(), SecureCell.MODE_CONTEXT_IMPRINT) | ||
val message = "All your base are belong to us!".toByteArray(StandardCharsets.UTF_8) | ||
val context = "We are CATS".toByteArray(StandardCharsets.UTF_8) | ||
|
||
var encrypted: ByteArray | ||
var decrypted: ByteArray? | ||
val result = oldCell.protect(context, message) | ||
encrypted = result.protectedData | ||
assertNotNull(encrypted) | ||
decrypted = newCell.decrypt(encrypted, context) | ||
assertArrayEquals(message, decrypted) | ||
|
||
encrypted = newCell.encrypt(message, context) | ||
assertNotNull(encrypted) | ||
decrypted = oldCell.unprotect(context, SecureCellData(encrypted, null)) | ||
assertArrayEquals(message, decrypted) | ||
} | ||
} |
Oops, something went wrong.