mirror of
https://github.com/corda/corda.git
synced 2025-02-21 09:51:57 +00:00
Comment fixes + split some unit-tests
This commit is contained in:
parent
9e447bd22e
commit
ab5c30c3da
@ -9,42 +9,38 @@ import javax.xml.bind.DatatypeConverter
|
|||||||
|
|
||||||
// [ByteArray] encoders
|
// [ByteArray] encoders
|
||||||
|
|
||||||
/** Convert a byte array to a base 58 encoded string.*/
|
|
||||||
fun ByteArray.toBase58(): String =
|
fun ByteArray.toBase58(): String =
|
||||||
Base58.encode(this)
|
Base58.encode(this)
|
||||||
|
|
||||||
/** Convert a byte array to a base 64 encoded string.*/
|
|
||||||
fun ByteArray.toBase64(): String =
|
fun ByteArray.toBase64(): String =
|
||||||
Base64.getEncoder().encodeToString(this)
|
Base64.getEncoder().encodeToString(this)
|
||||||
|
|
||||||
/** Convert a byte array to a hex (base 16) capitalised encoded string.*/
|
/** Convert a byte array to a hex (base 16) capitalized encoded string.*/
|
||||||
fun ByteArray.toHex(): String =
|
fun ByteArray.toHex(): String =
|
||||||
DatatypeConverter.printHexBinary(this)
|
DatatypeConverter.printHexBinary(this)
|
||||||
|
|
||||||
|
|
||||||
// [String] encoders and decoders
|
// [String] encoders and decoders
|
||||||
|
|
||||||
/** Base58-String to the actual real [String] */
|
/** Base58-String to the actual real [String], i.e. "JxF12TrwUP45BMd" -> "Hello World". */
|
||||||
fun String.base58ToRealString() =
|
fun String.base58ToRealString() =
|
||||||
String(base58ToByteArray(), Charset.defaultCharset())
|
String(base58ToByteArray(), Charset.defaultCharset())
|
||||||
|
|
||||||
/** Base64-String to the actual real [String] */
|
/** Base64-String to the actual real [String], i.e. "SGVsbG8gV29ybGQ=" -> "Hello World". */
|
||||||
fun String.base64ToRealString() =
|
fun String.base64ToRealString() =
|
||||||
String(base64ToByteArray())
|
String(base64ToByteArray())
|
||||||
|
|
||||||
/** Hex-String to the actual real [String] */
|
/** HEX-String to the actual real [String], i.e. "48656C6C6F20576F726C64" -> "Hello World". */
|
||||||
fun String.hexToRealString() =
|
fun String.hexToRealString() =
|
||||||
String(hexToByteArray())
|
String(hexToByteArray())
|
||||||
|
|
||||||
/** Base58-String to [ByteArray]. */
|
|
||||||
fun String.base58ToByteArray(): ByteArray =
|
fun String.base58ToByteArray(): ByteArray =
|
||||||
Base58.decode(this)
|
Base58.decode(this)
|
||||||
|
|
||||||
/** Base64-String to [ByteArray]. */
|
|
||||||
fun String.base64ToByteArray(): ByteArray =
|
fun String.base64ToByteArray(): ByteArray =
|
||||||
Base64.getDecoder().decode(this)
|
Base64.getDecoder().decode(this)
|
||||||
|
|
||||||
/** Hex-String to [ByteArray]. Accept lowercase or capital or mixed letters. */
|
/** Hex-String to [ByteArray]. Accept any hex form (capitalized, lowercase, mixed). */
|
||||||
fun String.hexToByteArray(): ByteArray =
|
fun String.hexToByteArray(): ByteArray =
|
||||||
DatatypeConverter.parseHexBinary(this);
|
DatatypeConverter.parseHexBinary(this);
|
||||||
|
|
||||||
|
@ -8,52 +8,63 @@ import kotlin.test.assertTrue
|
|||||||
import kotlin.test.fail
|
import kotlin.test.fail
|
||||||
|
|
||||||
class EncodingUtilsTest {
|
class EncodingUtilsTest {
|
||||||
@Test
|
|
||||||
fun testEncode() {
|
|
||||||
// Test Hello World.
|
|
||||||
val testbytes = "Hello World".toByteArray()
|
|
||||||
assertEquals("JxF12TrwUP45BMd", testbytes.toBase58())
|
|
||||||
assertEquals("SGVsbG8gV29ybGQ=", testbytes.toBase64())
|
|
||||||
assertEquals("48656C6C6F20576F726C64", testbytes.toHex())
|
|
||||||
|
|
||||||
// Test empty encode.
|
val testString = "Hello World"
|
||||||
|
val testBytes = testString.toByteArray()
|
||||||
|
val testBase58String = "JxF12TrwUP45BMd" // Base58 format for Hello World.
|
||||||
|
val testBase64String = "SGVsbG8gV29ybGQ=" // Base64 format for Hello World.
|
||||||
|
val testHexString = "48656C6C6F20576F726C64" // HEX format for Hello World.
|
||||||
|
|
||||||
|
// Encoding tests
|
||||||
|
@Test
|
||||||
|
fun `encoding Hello World`() {
|
||||||
|
assertEquals(testBase58String, testBytes.toBase58())
|
||||||
|
assertEquals(testBase64String, testBytes.toBase64())
|
||||||
|
assertEquals(testHexString, testBytes.toHex())
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun `empty encoding`() {
|
||||||
val emptyByteArray = ByteArray(0)
|
val emptyByteArray = ByteArray(0)
|
||||||
assertEquals("", emptyByteArray.toBase58())
|
assertEquals("", emptyByteArray.toBase58())
|
||||||
assertEquals("", emptyByteArray.toBase64())
|
assertEquals("", emptyByteArray.toBase64())
|
||||||
assertEquals("", emptyByteArray.toHex())
|
assertEquals("", emptyByteArray.toHex())
|
||||||
|
}
|
||||||
// Test 7 zero bytes.
|
@Test
|
||||||
|
fun `encoding 7 zero bytes`() {
|
||||||
val sevenZeroByteArray = ByteArray(7)
|
val sevenZeroByteArray = ByteArray(7)
|
||||||
assertEquals("1111111", sevenZeroByteArray.toBase58())
|
assertEquals("1111111", sevenZeroByteArray.toBase58())
|
||||||
assertEquals("AAAAAAAAAA==", sevenZeroByteArray.toBase64())
|
assertEquals("AAAAAAAAAA==", sevenZeroByteArray.toBase64())
|
||||||
assertEquals("00000000000000", sevenZeroByteArray.toHex())
|
assertEquals("00000000000000", sevenZeroByteArray.toHex())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//Decoding tests
|
||||||
@Test
|
@Test
|
||||||
fun testDecode() {
|
fun `decoding to real String`() {
|
||||||
val testString = "Hello World"
|
|
||||||
val testBase58String = "JxF12TrwUP45BMd"
|
|
||||||
val testBase64String = "SGVsbG8gV29ybGQ="
|
|
||||||
val testHexString = "48656C6C6F20576F726C64"
|
|
||||||
|
|
||||||
assertEquals(testString, testBase58String.base58ToRealString())
|
assertEquals(testString, testBase58String.base58ToRealString())
|
||||||
assertEquals(testString, testBase64String.base64ToRealString())
|
assertEquals(testString, testBase64String.base64ToRealString())
|
||||||
assertEquals(testString, testHexString.hexToRealString())
|
assertEquals(testString, testHexString.hexToRealString())
|
||||||
|
}
|
||||||
|
|
||||||
// Test empty Strings.
|
@Test
|
||||||
|
fun `decoding empty Strings`() {
|
||||||
assertEquals("", "".base58ToRealString())
|
assertEquals("", "".base58ToRealString())
|
||||||
assertEquals("", "".base64ToRealString())
|
assertEquals("", "".base64ToRealString())
|
||||||
assertEquals("", "".hexToRealString())
|
assertEquals("", "".hexToRealString())
|
||||||
|
}
|
||||||
|
|
||||||
// Test for Hex lowercase.
|
@Test
|
||||||
|
fun `decoding lowercase and mixed HEX`() {
|
||||||
val testHexStringLowercase = testHexString.toLowerCase()
|
val testHexStringLowercase = testHexString.toLowerCase()
|
||||||
assertEquals(testHexString.hexToRealString(), testHexStringLowercase.hexToRealString())
|
assertEquals(testHexString.hexToRealString(), testHexStringLowercase.hexToRealString())
|
||||||
|
|
||||||
// Test for Hex mixed.
|
val testHexStringMixed = testHexString.replace('C', 'c')
|
||||||
val testHexStringMixed = testHexString.replace('C','c')
|
|
||||||
assertEquals(testHexString.hexToRealString(), testHexStringMixed.hexToRealString())
|
assertEquals(testHexString.hexToRealString(), testHexStringMixed.hexToRealString())
|
||||||
|
}
|
||||||
|
|
||||||
// Test for wrong format.
|
@Test
|
||||||
|
fun `decoding on wrong format`() {
|
||||||
|
// the String "Hello World" is not a valid Base58 or Base64 or HEX format
|
||||||
try {
|
try {
|
||||||
testString.base58ToRealString()
|
testString.base58ToRealString()
|
||||||
fail()
|
fail()
|
||||||
@ -74,7 +85,5 @@ class EncodingUtilsTest {
|
|||||||
} catch (e: IllegalArgumentException) {
|
} catch (e: IllegalArgumentException) {
|
||||||
// expected.
|
// expected.
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user