Skip to content

Commit

Permalink
Move base 64 tests to be cross platform
Browse files Browse the repository at this point in the history
  • Loading branch information
mpilquist committed Sep 20, 2023
1 parent 63ad349 commit 1b4bc72
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 72 deletions.
71 changes: 0 additions & 71 deletions core/jvm/src/test/scala/scodec/bits/ByteVectorJvmTest.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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
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, UUID, Base64 => JBase64}
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))
}
}

0 comments on commit 1b4bc72

Please sign in to comment.