Rewrites CryptoUtilTest with matchers

Issue #2745
This commit is contained in:
Ryan Haining 2023-09-20 22:53:18 -07:00
parent 84138eba33
commit 8819aed6e1

View file

@ -1,8 +1,8 @@
package info.nightscout.core.utils
import com.google.common.truth.Truth.assertThat
import com.google.common.truth.TruthJUnit.assume
import info.nightscout.sharedtests.TestBase
import org.junit.jupiter.api.Assertions
import org.junit.jupiter.api.Test
// https://stackoverflow.com/questions/52344522/joseexception-couldnt-create-aes-gcm-nopadding-cipher-illegal-key-size
@ -33,11 +33,11 @@ class CryptoUtilTest : TestBase() {
val encrypted = cryptoUtil.encrypt(password, salt, payload)
assumeAES256isSupported(cryptoUtil)
Assertions.assertNotNull(encrypted)
assertThat(encrypted).isNotNull()
val decrypted = cryptoUtil.decrypt(password, salt, encrypted!!)
assumeAES256isSupported(cryptoUtil)
Assertions.assertEquals(decrypted, payload)
assertThat(decrypted).isEqualTo(payload)
}
@Test
@ -49,18 +49,18 @@ class CryptoUtilTest : TestBase() {
val encrypted = cryptoUtil.encrypt(password, salt, payload)
assumeAES256isSupported(cryptoUtil)
Assertions.assertNotNull(encrypted)
assertThat(encrypted).isNotNull()
val decrypted = cryptoUtil.decrypt(password, salt, encrypted!!)
assumeAES256isSupported(cryptoUtil)
Assertions.assertEquals(decrypted, payload)
assertThat(decrypted).isEqualTo(payload)
}
@Test
fun testHashVector() {
val payload = "{what:payloadYouWantToProtect}"
val hash = cryptoUtil.sha256(payload)
Assertions.assertEquals(hash, "a1aafe3ed6cc127e6d102ddbc40a205147230e9cfd178daf108c83543bbdcd13")
assertThat(hash).isEqualTo("a1aafe3ed6cc127e6d102ddbc40a205147230e9cfd178daf108c83543bbdcd13")
}
@Test
@ -69,44 +69,44 @@ class CryptoUtilTest : TestBase() {
val password = "topSikret"
val expectedHmac = "ea2213953d0f2e55047cae2d23fb4f0de1b805d55e6271efa70d6b85fb692bea" // generated using other HMAC tool
val hash = cryptoUtil.hmac256(payload, password)
Assertions.assertEquals(hash, expectedHmac)
assertThat(hash).isEqualTo(expectedHmac)
}
@Test
fun testPlainPasswordCheck() {
Assertions.assertTrue(cryptoUtil.checkPassword("same", "same"))
Assertions.assertFalse(cryptoUtil.checkPassword("same", "other"))
assertThat(cryptoUtil.checkPassword("same", "same")).isTrue()
assertThat(cryptoUtil.checkPassword("same", "other")).isFalse()
}
@Test
fun testHashedPasswordCheck() {
Assertions.assertTrue(cryptoUtil.checkPassword("givenSecret", cryptoUtil.hashPassword("givenSecret")))
Assertions.assertFalse(cryptoUtil.checkPassword("givenSecret", cryptoUtil.hashPassword("otherSecret")))
assertThat(cryptoUtil.checkPassword("givenSecret", cryptoUtil.hashPassword("givenSecret"))).isTrue()
assertThat(cryptoUtil.checkPassword("givenSecret", cryptoUtil.hashPassword("otherSecret"))).isFalse()
Assertions.assertTrue(
assertThat(
cryptoUtil.checkPassword(
"givenHashToCheck",
"hmac:7fe5f9c7b4b97c5d32d5cfad9d07473543a9938dc07af48a46dbbb49f4f68c12:a0c7cee14312bbe31b51359a67f0d2dfdf46813f319180269796f1f617a64be1"
)
)
Assertions.assertFalse(
).isTrue()
assertThat(
cryptoUtil.checkPassword(
"givenMashToCheck",
"hmac:7fe5f9c7b4b97c5d32d5cfad9d07473543a9938dc07af48a46dbbb49f4f68c12:a0c7cee14312bbe31b51359a67f0d2dfdf46813f319180269796f1f617a64be1"
)
)
Assertions.assertFalse(
).isFalse()
assertThat(
cryptoUtil.checkPassword(
"givenHashToCheck",
"hmac:0fe5f9c7b4b97c5d32d5cfad9d07473543a9938dc07af48a46dbbb49f4f68c12:a0c7cee14312bbe31b51359a67f0d2dfdf46813f319180269796f1f617a64be1"
)
)
Assertions.assertFalse(
).isFalse()
assertThat(
cryptoUtil.checkPassword(
"givenHashToCheck",
"hmac:7fe5f9c7b4b97c5d32d5cfad9d07473543a9938dc07af48a46dbbb49f4f68c12:b0c7cee14312bbe31b51359a67f0d2dfdf46813f319180269796f1f617a64be1"
)
)
).isFalse()
}
}