|
| 1 | +package io.iohk.ethereum.utils |
| 2 | + |
| 3 | +import akka.util.ByteString |
| 4 | +import org.scalatest.wordspec.AnyWordSpec |
| 5 | +import org.scalatest.matchers.should.Matchers |
| 6 | +import ByteStringUtils._ |
| 7 | +import scala.collection.immutable.ArraySeq |
| 8 | +import scala.util.{Try, Success, Failure} |
| 9 | + |
| 10 | +class ByteStringUtilsTest extends AnyWordSpec with Matchers { |
| 11 | + |
| 12 | + "ByteStringUtilsTest" should { |
| 13 | + |
| 14 | + "succeed parsing a valid hash string" in { |
| 15 | + val validHashString = "00FF00FF" |
| 16 | + val parsed = Try(string2hash(validHashString)) |
| 17 | + val expected = ByteString(Array[Byte](0, -1, 0, -1)) |
| 18 | + parsed shouldEqual Success(expected) |
| 19 | + } |
| 20 | + |
| 21 | + "fail parsing a valid hash string" in { |
| 22 | + val invalidHashString = "XXYYZZXX" |
| 23 | + val parsed = Try(string2hash(invalidHashString)) |
| 24 | + parsed shouldBe a[Failure[_]] |
| 25 | + } |
| 26 | + |
| 27 | + "concatByteStrings for simple bytestrings" in { |
| 28 | + val bs1 = string2hash("0000") |
| 29 | + val bs2 = string2hash("FFFF") |
| 30 | + val summarized: ByteString = bs1 ++ bs2 |
| 31 | + |
| 32 | + val concatenated: ByteString = ByteStringUtils.concatByteStrings(bs1, bs2) |
| 33 | + summarized shouldEqual concatenated |
| 34 | + } |
| 35 | + |
| 36 | + "concatByteStrings for various argument types" in { |
| 37 | + val bs1 = string2hash("0000") |
| 38 | + val bs2 = string2hash("FFFF") |
| 39 | + val bs3: Byte = 2 |
| 40 | + val bs4 = Array[Byte](3, 3) |
| 41 | + val bs5 = Array[Byte](4, 4) |
| 42 | + val summarized: ByteString = bs1 ++ bs2 |
| 43 | + val concatenated: ByteString = ByteStringUtils.concatByteStrings(bs1, bs2, bs3, bs4, bs5) |
| 44 | + concatenated shouldEqual string2hash("0000FFFF0203030404") |
| 45 | + } |
| 46 | + |
| 47 | + "apply padding the same way seqOps does" in { |
| 48 | + val bsu = string2hash("0000FFFF") |
| 49 | + val seq = ArraySeq.unsafeWrapArray(bsu.toArray) |
| 50 | + bsu.padToByteString(3, 0) shouldEqual bsu // result is ByteString |
| 51 | + bsu.padTo(3,0) shouldEqual seq // result is Seq |
| 52 | + |
| 53 | + val longSeq = ArraySeq[Byte](0, 0, -1, -1, 1 ,1) |
| 54 | + val longBsu = string2hash("0000FFFF0101") |
| 55 | + bsu.padToByteString(6, 1) shouldEqual longBsu |
| 56 | + bsu.padTo(6, 1) shouldEqual longSeq |
| 57 | + } |
| 58 | + |
| 59 | + } |
| 60 | +} |
| 61 | + |
0 commit comments