-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
9345f63
commit ae41b85
Showing
4 changed files
with
108 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package ivy.learn.util | ||
|
||
import arrow.core.Either | ||
import arrow.core.raise.catch | ||
import arrow.core.right | ||
import kotlinx.io.bytestring.decodeToByteString | ||
import kotlinx.io.bytestring.decodeToString | ||
import kotlin.io.encoding.Base64 | ||
import kotlin.io.encoding.ExperimentalEncodingApi | ||
|
||
class Base64Util { | ||
@OptIn(ExperimentalEncodingApi::class) | ||
fun decode( | ||
textBase64: String, | ||
): Either<String, String> = catch({ | ||
Base64.withPadding(Base64.PaddingOption.ABSENT_OPTIONAL) | ||
.decodeToByteString(textBase64).decodeToString() | ||
.right() | ||
}) { e -> | ||
Either.Left("Base64 decode of '$textBase64' failed because $e") | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
package ivy.learn.util | ||
|
||
import com.google.testing.junit.testparameterinjector.TestParameter | ||
import com.google.testing.junit.testparameterinjector.TestParameterInjector | ||
import io.kotest.assertions.arrow.core.shouldBeLeft | ||
import io.kotest.assertions.arrow.core.shouldBeRight | ||
import io.kotest.matchers.shouldBe | ||
import org.junit.Before | ||
import org.junit.Test | ||
import org.junit.runner.RunWith | ||
|
||
@RunWith(TestParameterInjector::class) | ||
class Base64UtilTest { | ||
private lateinit var base64: Base64Util | ||
|
||
@Before | ||
fun setup() { | ||
base64 = Base64Util() | ||
} | ||
|
||
enum class ValidBase64TestCase( | ||
val encoded: String, | ||
val expectedDecoded: String, | ||
) { | ||
PADDED( | ||
encoded = "SGVsbG8sIEJhc2U2NCE=", | ||
expectedDecoded = "Hello, Base64!", | ||
), | ||
NOT_PADDED( | ||
encoded = "SGVsbG8sIEJhc2U2NCE", | ||
expectedDecoded = "Hello, Base64!" | ||
) | ||
} | ||
|
||
@Test | ||
fun `decodes valid base64`( | ||
@TestParameter testCase: ValidBase64TestCase, | ||
) { | ||
// Given | ||
val encoded = testCase.encoded | ||
|
||
// When | ||
val decoded = base64.decode(encoded) | ||
|
||
// Then | ||
decoded.shouldBeRight() shouldBe testCase.expectedDecoded | ||
} | ||
|
||
@Test | ||
fun `fails to decode invalid base64`() { | ||
// Given | ||
val encoded = "Hello, Base64!" | ||
|
||
// When | ||
val decoded = base64.decode(encoded) | ||
|
||
// Then | ||
decoded.shouldBeLeft() | ||
} | ||
} |