Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove Guava dependency from JVM tests #466

Merged
merged 3 commits into from
Sep 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 0 additions & 5 deletions build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -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)),
Expand Down
75 changes: 0 additions & 75 deletions core/jvm/src/test/scala/scodec/bits/ByteVectorJvmTest.scala
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can this test move to shared sources now?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep good idea

Original file line number Diff line number Diff line change
Expand Up @@ -51,81 +51,6 @@ class ByteVectorJvmTest extends BitsSuite {
super.afterAll()
}

property("toBase64") {
forAll { (b: ByteVector) =>
val guavaB64 = com.google.common.io.BaseEncoding.base64
assert(ByteVector.view(guavaB64.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)
}
}

property("toBase64Url") {
forAll { (b: ByteVector) =>
val guavaB64 = com.google.common.io.BaseEncoding.base64Url()
assert(ByteVector.view(guavaB64.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)
}
}

property("fromBase64") {
forAll { (b: ByteVector) =>
val guavaB64 = com.google.common.io.BaseEncoding.base64
assert(ByteVector.fromValidBase64(guavaB64.encode(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
Expand Down
72 changes: 71 additions & 1 deletion core/shared/src/test/scala/scodec/bits/ByteVectorTest.scala
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ package scodec.bits

import java.io.ByteArrayOutputStream
import java.nio.ByteBuffer
import java.util.{Arrays, UUID}
import java.util.{Arrays, Base64 => JBase64, UUID}
import Arbitraries._
import org.scalacheck._
import Prop.forAll
Expand Down Expand Up @@ -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))
}
}