diff --git a/jvm/src/test/scala/fluence/crypto/AesSpec.scala b/jvm/src/test/scala/fluence/crypto/AesSpec.scala index e824075..052a154 100644 --- a/jvm/src/test/scala/fluence/crypto/AesSpec.scala +++ b/jvm/src/test/scala/fluence/crypto/AesSpec.scala @@ -1,13 +1,14 @@ package fluence.crypto -import fluence.crypto.algorithm.{ AesConfig, AesCrypt, CryptoErr } +import fluence.crypto.algorithm.{AesConfig, AesCrypt, CryptoErr} import cats.instances.try_._ -import org.scalatest.{ Matchers, WordSpec } +import org.scalactic.source.Position +import org.scalatest.{Assertion, Matchers, WordSpec} import scodec.bits.ByteVector -import scala.util.{ Random, Try } +import scala.util.{Random, Try} -class AesSpec extends WordSpec with Matchers { +class AesSpec extends WordSpec with Matchers with slogging.LazyLogging { def rndString(size: Int): String = Random.nextString(10) val conf = AesConfig() @@ -23,20 +24,14 @@ class AesSpec extends WordSpec with Matchers { crypt.decrypt(crypted).get shouldBe str val fakeAes = AesCrypt.forString[Try](ByteVector("wrong".getBytes()), withIV = true, config = conf) - fakeAes.decrypt(crypted).map(_ ⇒ false).recover { - case e: CryptoErr ⇒ true - case _ ⇒ false - }.get shouldBe true + checkCryptoError(fakeAes.decrypt(crypted)) //we cannot check if first bytes is iv or already data, but encryption goes wrong val aesWithoutIV = AesCrypt.forString[Try](pass, withIV = false, config = conf) aesWithoutIV.decrypt(crypted).get shouldNot be (str) val aesWrongSalt = AesCrypt.forString[Try](pass, withIV = true, config = conf.copy(salt = rndString(10))) - aesWrongSalt.decrypt(crypted).map(_ ⇒ false).recover { - case e: CryptoErr ⇒ true - case _ ⇒ false - }.get shouldBe true + checkCryptoError(aesWrongSalt.decrypt(crypted)) } "work without IV" in { @@ -48,21 +43,24 @@ class AesSpec extends WordSpec with Matchers { crypt.decrypt(crypted).get shouldBe str val fakeAes = AesCrypt.forString[Try](ByteVector("wrong".getBytes()), withIV = false, config = conf) - fakeAes.decrypt(crypted).map(_ ⇒ false).recover { - case e: CryptoErr ⇒ true - case _ ⇒ false - }.get shouldBe true + checkCryptoError(fakeAes.decrypt(crypted)) //we cannot check if first bytes is iv or already data, but encryption goes wrong val aesWithIV = AesCrypt.forString[Try](pass, withIV = true, config = conf) aesWithIV.decrypt(crypted).get shouldNot be (str) val aesWrongSalt = AesCrypt.forString[Try](pass, withIV = true, config = conf.copy(salt = rndString(10))) - aesWrongSalt.decrypt(crypted).map(_ ⇒ false).recover { - case e: CryptoErr ⇒ true - case _ ⇒ false - }.get shouldBe true + checkCryptoError(aesWrongSalt.decrypt(crypted)) } } + def checkCryptoError(tr: Try[String])(implicit pos: Position): Assertion = { + tr.map(_ ⇒ false).recover { + case e: CryptoErr ⇒ true + case e ⇒ + logger.error("Unexpected error", e) + false + }.get shouldBe true + } + }