From 63ad349e0684855bd8afa5b92c5723ddad1ba4b7 Mon Sep 17 00:00:00 2001 From: mpilquist Date: Wed, 20 Sep 2023 13:25:20 -0400 Subject: [PATCH 1/3] Remove Guava dependency from JVM tests --- build.sbt | 5 ----- .../scala/scodec/bits/ByteVectorJvmTest.scala | 16 ++++++---------- 2 files changed, 6 insertions(+), 15 deletions(-) diff --git a/build.sbt b/build.sbt index d0069eac..6ae05128 100644 --- a/build.sbt +++ b/build.sbt @@ -97,11 +97,6 @@ lazy val core = crossProject(JVMPlatform, JSPlatform, NativePlatform) ) lazy val coreJVM = core.jvm - .settings( - libraryDependencies ++= Seq( - "com.google.guava" % "guava" % "32.1.2-jre" % "test" - ) - ) lazy val coreJS = core.js.settings( scalaJSLinkerConfig ~= (_.withModuleKind(ModuleKind.CommonJSModule)), diff --git a/core/jvm/src/test/scala/scodec/bits/ByteVectorJvmTest.scala b/core/jvm/src/test/scala/scodec/bits/ByteVectorJvmTest.scala index 4204bfec..54342c5d 100644 --- a/core/jvm/src/test/scala/scodec/bits/ByteVectorJvmTest.scala +++ b/core/jvm/src/test/scala/scodec/bits/ByteVectorJvmTest.scala @@ -35,6 +35,7 @@ import org.scalacheck.Prop.forAll import Arbitrary.arbitrary import Arbitraries._ +import java.util.{Base64 => JBase64} import java.util.concurrent.Callable class ByteVectorJvmTest extends BitsSuite { @@ -53,36 +54,31 @@ class ByteVectorJvmTest extends BitsSuite { property("toBase64") { forAll { (b: ByteVector) => - val guavaB64 = com.google.common.io.BaseEncoding.base64 - assert(ByteVector.view(guavaB64.decode(b.toBase64)) == b) + assert(ByteVector.view(JBase64.getDecoder.decode(b.toBase64)) == b) } } property("toBase64NoPad") { forAll { (b: ByteVector) => - val guavaB64 = com.google.common.io.BaseEncoding.base64.omitPadding() - assert(ByteVector.view(guavaB64.decode(b.toBase64NoPad)) == b) + assert(ByteVector.view(JBase64.getDecoder.decode(b.toBase64NoPad)) == b) } } property("toBase64Url") { forAll { (b: ByteVector) => - val guavaB64 = com.google.common.io.BaseEncoding.base64Url() - assert(ByteVector.view(guavaB64.decode(b.toBase64Url)) == b) + assert(ByteVector.view(JBase64.getUrlDecoder.decode(b.toBase64Url)) == b) } } property("toBase64UrlNoPad") { forAll { (b: ByteVector) => - val guavaB64 = com.google.common.io.BaseEncoding.base64Url().omitPadding() - assert(ByteVector.view(guavaB64.decode(b.toBase64UrlNoPad)) == b) + assert(ByteVector.view(JBase64.getUrlDecoder.decode(b.toBase64UrlNoPad)) == b) } } property("fromBase64") { forAll { (b: ByteVector) => - val guavaB64 = com.google.common.io.BaseEncoding.base64 - assert(ByteVector.fromValidBase64(guavaB64.encode(b.toArray)) == b) + assert(ByteVector.fromValidBase64(JBase64.getEncoder.encodeToString(b.toArray)) == b) } } From 1b4bc72ae48fc7e3a11c0de80fe84a33d2a0380d Mon Sep 17 00:00:00 2001 From: mpilquist Date: Wed, 20 Sep 2023 13:34:34 -0400 Subject: [PATCH 2/3] Move base 64 tests to be cross platform --- .../scala/scodec/bits/ByteVectorJvmTest.scala | 71 ------------------ .../scala/scodec/bits/ByteVectorTest.scala | 72 ++++++++++++++++++- 2 files changed, 71 insertions(+), 72 deletions(-) diff --git a/core/jvm/src/test/scala/scodec/bits/ByteVectorJvmTest.scala b/core/jvm/src/test/scala/scodec/bits/ByteVectorJvmTest.scala index 54342c5d..d7261138 100644 --- a/core/jvm/src/test/scala/scodec/bits/ByteVectorJvmTest.scala +++ b/core/jvm/src/test/scala/scodec/bits/ByteVectorJvmTest.scala @@ -35,7 +35,6 @@ import org.scalacheck.Prop.forAll import Arbitrary.arbitrary import Arbitraries._ -import java.util.{Base64 => JBase64} import java.util.concurrent.Callable class ByteVectorJvmTest extends BitsSuite { @@ -52,76 +51,6 @@ class ByteVectorJvmTest extends BitsSuite { super.afterAll() } - property("toBase64") { - forAll { (b: ByteVector) => - assert(ByteVector.view(JBase64.getDecoder.decode(b.toBase64)) == b) - } - } - - property("toBase64NoPad") { - forAll { (b: ByteVector) => - assert(ByteVector.view(JBase64.getDecoder.decode(b.toBase64NoPad)) == b) - } - } - - property("toBase64Url") { - forAll { (b: ByteVector) => - assert(ByteVector.view(JBase64.getUrlDecoder.decode(b.toBase64Url)) == b) - } - } - - property("toBase64UrlNoPad") { - forAll { (b: ByteVector) => - assert(ByteVector.view(JBase64.getUrlDecoder.decode(b.toBase64UrlNoPad)) == b) - } - } - - property("fromBase64") { - forAll { (b: ByteVector) => - assert(ByteVector.fromValidBase64(JBase64.getEncoder.encodeToString(b.toArray)) == b) - } - } - - test("fromBase64 - digit count non-divisble by 4") { - assert( - ByteVector.fromBase64Descriptive("A") == Left( - "Final base 64 quantum had only 1 digit - must have at least 2 digits" - ) - ) - assert(ByteVector.fromBase64Descriptive("AB") == Right(hex"00")) - assert(ByteVector.fromBase64Descriptive("ABC") == Right(hex"0010")) - assert(ByteVector.fromBase64Descriptive("ABCD") == Right(hex"001083")) - assert( - ByteVector.fromBase64Descriptive("ABCDA") == Left( - "Final base 64 quantum had only 1 digit - must have at least 2 digits" - ) - ) - assert(ByteVector.fromBase64Descriptive("ABCDAB") == Right(hex"00108300")) - } - - test("fromBase64 - padding") { - assert(ByteVector.fromBase64Descriptive("AB==") == Right(hex"00")) - val paddingError = Left( - "Malformed padding - final quantum may optionally be padded with one or two padding characters such that the quantum is completed" - ) - assert(ByteVector.fromBase64Descriptive("A=") == paddingError) - assert(ByteVector.fromBase64Descriptive("A==") == paddingError) - assert(ByteVector.fromBase64Descriptive("A===") == paddingError) - assert(ByteVector.fromBase64Descriptive("A====") == paddingError) - assert(ByteVector.fromBase64Descriptive("AB=") == paddingError) - assert(ByteVector.fromBase64Descriptive("AB===") == paddingError) - assert(ByteVector.fromBase64Descriptive("ABC==") == paddingError) - assert(ByteVector.fromBase64Descriptive("=") == paddingError) - assert(ByteVector.fromBase64Descriptive("==") == paddingError) - assert(ByteVector.fromBase64Descriptive("===") == paddingError) - assert(ByteVector.fromBase64Descriptive("====") == paddingError) - assert(ByteVector.fromBase64Descriptive("=====") == paddingError) - } - - test("fromBase64 - empty input string") { - assert(ByteVector.fromBase64Descriptive("") == Right(ByteVector.empty)) - } - property("buffer concurrency") { // Concurrently append b1.buffer ++ b2 and b1.buffer ++ b3 // making sure this gives same results as unbuffered appends diff --git a/core/shared/src/test/scala/scodec/bits/ByteVectorTest.scala b/core/shared/src/test/scala/scodec/bits/ByteVectorTest.scala index ff83dca5..00dbd909 100644 --- a/core/shared/src/test/scala/scodec/bits/ByteVectorTest.scala +++ b/core/shared/src/test/scala/scodec/bits/ByteVectorTest.scala @@ -32,7 +32,7 @@ package scodec.bits import java.io.ByteArrayOutputStream import java.nio.ByteBuffer -import java.util.{Arrays, UUID} +import java.util.{Arrays, UUID, Base64 => JBase64} import Arbitraries._ import org.scalacheck._ import Prop.forAll @@ -812,4 +812,74 @@ class ByteVectorTest extends BitsSuite { test("utf8 interpolator") { assertEquals(utf8Bytes"ɟǝǝqpɐǝp", ByteVector.encodeUtf8(s"ɟǝǝqpɐǝp").toOption.get) } + + property("toBase64") { + forAll { (b: ByteVector) => + assert(ByteVector.view(JBase64.getDecoder.decode(b.toBase64)) == b) + } + } + + property("toBase64NoPad") { + forAll { (b: ByteVector) => + assert(ByteVector.view(JBase64.getDecoder.decode(b.toBase64NoPad)) == b) + } + } + + property("toBase64Url") { + forAll { (b: ByteVector) => + assert(ByteVector.view(JBase64.getUrlDecoder.decode(b.toBase64Url)) == b) + } + } + + property("toBase64UrlNoPad") { + forAll { (b: ByteVector) => + assert(ByteVector.view(JBase64.getUrlDecoder.decode(b.toBase64UrlNoPad)) == b) + } + } + + property("fromBase64") { + forAll { (b: ByteVector) => + assert(ByteVector.fromValidBase64(JBase64.getEncoder.encodeToString(b.toArray)) == b) + } + } + + test("fromBase64 - digit count non-divisble by 4") { + assert( + ByteVector.fromBase64Descriptive("A") == Left( + "Final base 64 quantum had only 1 digit - must have at least 2 digits" + ) + ) + assert(ByteVector.fromBase64Descriptive("AB") == Right(hex"00")) + assert(ByteVector.fromBase64Descriptive("ABC") == Right(hex"0010")) + assert(ByteVector.fromBase64Descriptive("ABCD") == Right(hex"001083")) + assert( + ByteVector.fromBase64Descriptive("ABCDA") == Left( + "Final base 64 quantum had only 1 digit - must have at least 2 digits" + ) + ) + assert(ByteVector.fromBase64Descriptive("ABCDAB") == Right(hex"00108300")) + } + + test("fromBase64 - padding") { + assert(ByteVector.fromBase64Descriptive("AB==") == Right(hex"00")) + val paddingError = Left( + "Malformed padding - final quantum may optionally be padded with one or two padding characters such that the quantum is completed" + ) + assert(ByteVector.fromBase64Descriptive("A=") == paddingError) + assert(ByteVector.fromBase64Descriptive("A==") == paddingError) + assert(ByteVector.fromBase64Descriptive("A===") == paddingError) + assert(ByteVector.fromBase64Descriptive("A====") == paddingError) + assert(ByteVector.fromBase64Descriptive("AB=") == paddingError) + assert(ByteVector.fromBase64Descriptive("AB===") == paddingError) + assert(ByteVector.fromBase64Descriptive("ABC==") == paddingError) + assert(ByteVector.fromBase64Descriptive("=") == paddingError) + assert(ByteVector.fromBase64Descriptive("==") == paddingError) + assert(ByteVector.fromBase64Descriptive("===") == paddingError) + assert(ByteVector.fromBase64Descriptive("====") == paddingError) + assert(ByteVector.fromBase64Descriptive("=====") == paddingError) + } + + test("fromBase64 - empty input string") { + assert(ByteVector.fromBase64Descriptive("") == Right(ByteVector.empty)) + } } From 86430cd97f17bc26178d92af2b66ca5eeb1d0b6a Mon Sep 17 00:00:00 2001 From: mpilquist Date: Wed, 20 Sep 2023 13:39:40 -0400 Subject: [PATCH 3/3] Scalafmt --- core/shared/src/test/scala/scodec/bits/ByteVectorTest.scala | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/core/shared/src/test/scala/scodec/bits/ByteVectorTest.scala b/core/shared/src/test/scala/scodec/bits/ByteVectorTest.scala index 00dbd909..da9a9079 100644 --- a/core/shared/src/test/scala/scodec/bits/ByteVectorTest.scala +++ b/core/shared/src/test/scala/scodec/bits/ByteVectorTest.scala @@ -32,7 +32,7 @@ package scodec.bits import java.io.ByteArrayOutputStream import java.nio.ByteBuffer -import java.util.{Arrays, UUID, Base64 => JBase64} +import java.util.{Arrays, Base64 => JBase64, UUID} import Arbitraries._ import org.scalacheck._ import Prop.forAll