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