From ab5c30c3dab457a55b8125b069657773c2a5710f Mon Sep 17 00:00:00 2001 From: Konstantinos Chalkias Date: Tue, 14 Feb 2017 14:23:38 +0000 Subject: [PATCH] Comment fixes + split some unit-tests --- .../net/corda/core/crypto/EncodingUtils.kt | 14 ++--- .../corda/core/crypto/EncodingUtilsTest.kt | 55 +++++++++++-------- 2 files changed, 37 insertions(+), 32 deletions(-) diff --git a/core/src/main/kotlin/net/corda/core/crypto/EncodingUtils.kt b/core/src/main/kotlin/net/corda/core/crypto/EncodingUtils.kt index 077911dae1..505da79623 100644 --- a/core/src/main/kotlin/net/corda/core/crypto/EncodingUtils.kt +++ b/core/src/main/kotlin/net/corda/core/crypto/EncodingUtils.kt @@ -9,42 +9,38 @@ import javax.xml.bind.DatatypeConverter // [ByteArray] encoders -/** Convert a byte array to a base 58 encoded string.*/ fun ByteArray.toBase58(): String = Base58.encode(this) -/** Convert a byte array to a base 64 encoded string.*/ fun ByteArray.toBase64(): String = 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 = DatatypeConverter.printHexBinary(this) // [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() = 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() = String(base64ToByteArray()) -/** Hex-String to the actual real [String] */ +/** HEX-String to the actual real [String], i.e. "48656C6C6F20576F726C64" -> "Hello World". */ fun String.hexToRealString() = String(hexToByteArray()) -/** Base58-String to [ByteArray]. */ fun String.base58ToByteArray(): ByteArray = Base58.decode(this) -/** Base64-String to [ByteArray]. */ fun String.base64ToByteArray(): ByteArray = 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 = DatatypeConverter.parseHexBinary(this); diff --git a/core/src/test/kotlin/net/corda/core/crypto/EncodingUtilsTest.kt b/core/src/test/kotlin/net/corda/core/crypto/EncodingUtilsTest.kt index 5aa6fa1b63..6cb99bfcf8 100644 --- a/core/src/test/kotlin/net/corda/core/crypto/EncodingUtilsTest.kt +++ b/core/src/test/kotlin/net/corda/core/crypto/EncodingUtilsTest.kt @@ -8,52 +8,63 @@ import kotlin.test.assertTrue import kotlin.test.fail 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) assertEquals("", emptyByteArray.toBase58()) assertEquals("", emptyByteArray.toBase64()) assertEquals("", emptyByteArray.toHex()) - - // Test 7 zero bytes. + } + @Test + fun `encoding 7 zero bytes`() { val sevenZeroByteArray = ByteArray(7) assertEquals("1111111", sevenZeroByteArray.toBase58()) assertEquals("AAAAAAAAAA==", sevenZeroByteArray.toBase64()) assertEquals("00000000000000", sevenZeroByteArray.toHex()) } + //Decoding tests @Test - fun testDecode() { - val testString = "Hello World" - val testBase58String = "JxF12TrwUP45BMd" - val testBase64String = "SGVsbG8gV29ybGQ=" - val testHexString = "48656C6C6F20576F726C64" - + fun `decoding to real String`() { assertEquals(testString, testBase58String.base58ToRealString()) assertEquals(testString, testBase64String.base64ToRealString()) assertEquals(testString, testHexString.hexToRealString()) + } - // Test empty Strings. + @Test + fun `decoding empty Strings`() { assertEquals("", "".base58ToRealString()) assertEquals("", "".base64ToRealString()) assertEquals("", "".hexToRealString()) + } - // Test for Hex lowercase. + @Test + fun `decoding lowercase and mixed HEX`() { val testHexStringLowercase = testHexString.toLowerCase() 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()) + } - // 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 { testString.base58ToRealString() fail() @@ -74,7 +85,5 @@ class EncodingUtilsTest { } catch (e: IllegalArgumentException) { // expected. } - } - }