|
| 1 | +package org.stellar.sdk |
| 2 | + |
| 3 | +import io.kotest.assertions.throwables.shouldThrow |
| 4 | +import io.kotest.core.spec.style.FunSpec |
| 5 | +import io.kotest.matchers.shouldBe |
| 6 | +import java.math.BigInteger |
| 7 | +import java.nio.charset.StandardCharsets |
| 8 | +import org.stellar.sdk.xdr.MemoType |
| 9 | + |
| 10 | +class MemoTest : |
| 11 | + FunSpec({ |
| 12 | + context("MEMO_NONE") { |
| 13 | + test("should create empty memo") { |
| 14 | + val memo = Memo.none() |
| 15 | + memo.toXdr().discriminant shouldBe MemoType.MEMO_NONE |
| 16 | + memo.toString() shouldBe "" |
| 17 | + } |
| 18 | + } |
| 19 | + |
| 20 | + context("MEMO_TEXT") { |
| 21 | + test("should create text memo from string") { |
| 22 | + val memo = Memo.text("test") |
| 23 | + memo.toXdr().discriminant shouldBe MemoType.MEMO_TEXT |
| 24 | + memo.text shouldBe "test" |
| 25 | + memo.bytes shouldBe "test".toByteArray(StandardCharsets.UTF_8) |
| 26 | + memo.toString() shouldBe "test" |
| 27 | + } |
| 28 | + |
| 29 | + test("should create text memo from empty string") { |
| 30 | + val memo = Memo.text("") |
| 31 | + memo.toXdr().discriminant shouldBe MemoType.MEMO_TEXT |
| 32 | + memo.text shouldBe "" |
| 33 | + memo.bytes shouldBe "".toByteArray(StandardCharsets.UTF_8) |
| 34 | + memo.toString() shouldBe "" |
| 35 | + } |
| 36 | + |
| 37 | + test("should create text memo with UTF-8 content") { |
| 38 | + val memo = Memo.text("三") |
| 39 | + memo.toXdr().discriminant shouldBe MemoType.MEMO_TEXT |
| 40 | + memo.text shouldBe "三" |
| 41 | + memo.bytes shouldBe "三".toByteArray(StandardCharsets.UTF_8) |
| 42 | + memo.toString() shouldBe "三" |
| 43 | + } |
| 44 | + |
| 45 | + test("should create text memo with exactly 28 bytes") { |
| 46 | + val text = "1234567890123456789012345678" |
| 47 | + text.toByteArray(StandardCharsets.UTF_8).size shouldBe 28 |
| 48 | + val memo = Memo.text(text) |
| 49 | + memo.toXdr().discriminant shouldBe MemoType.MEMO_TEXT |
| 50 | + memo.text shouldBe text |
| 51 | + memo.bytes shouldBe text.toByteArray(StandardCharsets.UTF_8) |
| 52 | + memo.toString() shouldBe text |
| 53 | + } |
| 54 | + |
| 55 | + test("should throw exception when text is too long") { |
| 56 | + val longText = "12345678901234567890123456789" |
| 57 | + longText.toByteArray(StandardCharsets.UTF_8).size shouldBe 29 |
| 58 | + val exception = shouldThrow<IllegalArgumentException> { Memo.text(longText) } |
| 59 | + exception.message shouldBe "text cannot be more than 28-bytes long." |
| 60 | + } |
| 61 | + |
| 62 | + test("should throw exception when UTF-8 text is too long") { |
| 63 | + val longText = "价值交易的开源协议!!" // 30 bytes |
| 64 | + longText.toByteArray(StandardCharsets.UTF_8).size shouldBe 29 |
| 65 | + val exception = shouldThrow<IllegalArgumentException> { Memo.text(longText) } |
| 66 | + exception.message shouldBe "text cannot be more than 28-bytes long." |
| 67 | + } |
| 68 | + } |
| 69 | + |
| 70 | + context("MEMO_ID") { |
| 71 | + test("should create id memo") { |
| 72 | + val memo = Memo.id(9223372036854775807L) |
| 73 | + memo.id shouldBe BigInteger.valueOf(9223372036854775807L) |
| 74 | + memo.toXdr().discriminant shouldBe MemoType.MEMO_ID |
| 75 | + memo.toXdr().id.uint64.number shouldBe BigInteger.valueOf(9223372036854775807L) |
| 76 | + memo.toString() shouldBe "9223372036854775807" |
| 77 | + } |
| 78 | + |
| 79 | + test("should create id memo with 0") { |
| 80 | + val memo = Memo.id(0) |
| 81 | + memo.id shouldBe BigInteger.ZERO |
| 82 | + memo.toXdr().discriminant shouldBe MemoType.MEMO_ID |
| 83 | + memo.toXdr().id.uint64.number shouldBe BigInteger.ZERO |
| 84 | + memo.toString() shouldBe "0" |
| 85 | + } |
| 86 | + |
| 87 | + test("should throw exception when id is negative") { |
| 88 | + val exception = shouldThrow<IllegalArgumentException> { Memo.id(-1) } |
| 89 | + exception.message shouldBe "MEMO_ID must be between 0 and 2^64-1" |
| 90 | + } |
| 91 | + } |
| 92 | + |
| 93 | + context("MEMO_HASH") { |
| 94 | + test("should create hash memo from byte array") { |
| 95 | + val bytes = ByteArray(32) { 'A'.code.toByte() } |
| 96 | + val memo = Memo.hash(bytes) |
| 97 | + memo.toXdr().discriminant shouldBe MemoType.MEMO_HASH |
| 98 | + memo.bytes shouldBe bytes |
| 99 | + memo.hexValue shouldBe "4141414141414141414141414141414141414141414141414141414141414141" |
| 100 | + memo.toString() shouldBe "4141414141414141414141414141414141414141414141414141414141414141" |
| 101 | + } |
| 102 | + |
| 103 | + test("should create hash memo from hex string") { |
| 104 | + val hashHex = "4142434445464748494a4b4c0000000000000000000000000000000000000000" |
| 105 | + val memo = Memo.hash(hashHex) |
| 106 | + memo.toXdr().discriminant shouldBe MemoType.MEMO_HASH |
| 107 | + memo.hexValue shouldBe hashHex.lowercase() |
| 108 | + memo.toString() shouldBe hashHex.lowercase() |
| 109 | + } |
| 110 | + |
| 111 | + test("should create hash memo from uppercase hex string") { |
| 112 | + val hashHex = "4142434445464748494a4b4c0000000000000000000000000000000000000000" |
| 113 | + val memo = Memo.hash(hashHex.uppercase()) |
| 114 | + memo.toXdr().discriminant shouldBe MemoType.MEMO_HASH |
| 115 | + memo.hexValue shouldBe hashHex.lowercase() |
| 116 | + memo.toString() shouldBe hashHex.lowercase() |
| 117 | + } |
| 118 | + |
| 119 | + test("should throw exception when bytes are not 32-bytes long") { |
| 120 | + val bytes31 = ByteArray(31) |
| 121 | + val exception31 = shouldThrow<IllegalArgumentException> { Memo.hash(bytes31) } |
| 122 | + exception31.message shouldBe "bytes must be 32-bytes long." |
| 123 | + |
| 124 | + val bytes33 = ByteArray(33) |
| 125 | + val exception33 = shouldThrow<IllegalArgumentException> { Memo.hash(bytes33) } |
| 126 | + exception33.message shouldBe "bytes must be 32-bytes long." |
| 127 | + } |
| 128 | + |
| 129 | + test("should throw exception when hex string is invalid") { |
| 130 | + shouldThrow<IllegalArgumentException> { Memo.hash("test") } |
| 131 | + shouldThrow<IllegalArgumentException> { Memo.hash("") } |
| 132 | + shouldThrow<IllegalArgumentException> { |
| 133 | + Memo.hash("00000000000000000000000000000000000000000000000000000000000000") |
| 134 | + } // 63 chars |
| 135 | + } |
| 136 | + } |
| 137 | + |
| 138 | + context("MEMO_RETURN_HASH") { |
| 139 | + test("should create return hash memo from byte array") { |
| 140 | + val bytes = ByteArray(32) { 'A'.code.toByte() } |
| 141 | + val memo = Memo.returnHash(bytes) |
| 142 | + memo.toXdr().discriminant shouldBe MemoType.MEMO_RETURN |
| 143 | + memo.bytes shouldBe bytes |
| 144 | + memo.hexValue shouldBe "4141414141414141414141414141414141414141414141414141414141414141" |
| 145 | + memo.toString() shouldBe "4141414141414141414141414141414141414141414141414141414141414141" |
| 146 | + } |
| 147 | + |
| 148 | + test("should create return hash memo from hex string") { |
| 149 | + val hashHex = "4142434445464748494a4b4c0000000000000000000000000000000000000000" |
| 150 | + val memo = Memo.returnHash(hashHex) |
| 151 | + memo.toXdr().discriminant shouldBe MemoType.MEMO_RETURN |
| 152 | + memo.hexValue shouldBe hashHex.lowercase() |
| 153 | + memo.toString() shouldBe hashHex.lowercase() |
| 154 | + } |
| 155 | + |
| 156 | + test("should create return hash memo from uppercase hex string") { |
| 157 | + val hashHex = "4142434445464748494a4b4c0000000000000000000000000000000000000000" |
| 158 | + val memo = Memo.returnHash(hashHex.uppercase()) |
| 159 | + memo.toXdr().discriminant shouldBe MemoType.MEMO_RETURN |
| 160 | + memo.hexValue shouldBe hashHex.lowercase() |
| 161 | + memo.toString() shouldBe hashHex.lowercase() |
| 162 | + } |
| 163 | + |
| 164 | + test("should throw exception when bytes are not 32-bytes long") { |
| 165 | + val bytes31 = ByteArray(31) |
| 166 | + val exception31 = shouldThrow<IllegalArgumentException> { Memo.returnHash(bytes31) } |
| 167 | + exception31.message shouldBe "bytes must be 32-bytes long." |
| 168 | + |
| 169 | + val bytes33 = ByteArray(33) |
| 170 | + val exception33 = shouldThrow<IllegalArgumentException> { Memo.returnHash(bytes33) } |
| 171 | + exception33.message shouldBe "bytes must be 32-bytes long." |
| 172 | + } |
| 173 | + |
| 174 | + test("should throw exception when hex string is invalid") { |
| 175 | + shouldThrow<IllegalArgumentException> { Memo.returnHash("test") } |
| 176 | + shouldThrow<IllegalArgumentException> { Memo.returnHash("") } |
| 177 | + shouldThrow<IllegalArgumentException> { |
| 178 | + Memo.returnHash("00000000000000000000000000000000000000000000000000000000000000") |
| 179 | + } // 63 chars |
| 180 | + } |
| 181 | + } |
| 182 | + }) |
0 commit comments