From 4a966657a633b4c3171af435ae27f2b18437d8d3 Mon Sep 17 00:00:00 2001 From: Dima Date: Tue, 27 Feb 2018 14:46:09 +0300 Subject: [PATCH] Sbt js modules refactoring (#70) * replace codec/core and rename jvm-js modules * add files to git * add info into test --- .../test/scala/fluence/crypto/AesSpec.scala | 38 +++++++++---------- 1 file changed, 18 insertions(+), 20 deletions(-) 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 + } + }