-
Notifications
You must be signed in to change notification settings - Fork 51
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Coordinator: DRY refactor test helpers and data for future reuse (#100)
* coordinator: adds slice ByteArrayExtensions.kt * coordinator: adds testing file system helper * coordinator: adds testing l1-blob-proof-submission submitter * coordinator: rename findFile function * coordinator: move prover responses to common test data for code reuse * coordinator: fix ByteArray.sliceOf off-by-one bug * coordinator: remove unnecessary!! * coordinator: fix gradle deps * coordinator: fix test * coordinator: reduce transitive dependencies
- Loading branch information
Showing
46 changed files
with
332 additions
and
151 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
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
50 changes: 50 additions & 0 deletions
50
jvm-libs/kotlin-extensions/src/main/kotlin/net/consensys/ByteArrayExtensions.kt
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,50 @@ | ||
package net.consensys | ||
|
||
fun ByteArray.assertSize(expectedSize: UInt, fieldName: String = ""): ByteArray = apply { | ||
require(size == expectedSize.toInt()) { "$fieldName expected to have $expectedSize bytes, but got $size" } | ||
} | ||
|
||
fun ByteArray.assertIs32Bytes(fieldName: String = ""): ByteArray = assertSize(32u, fieldName) | ||
|
||
fun ByteArray.assertIs20Bytes(fieldName: String = ""): ByteArray = assertSize(20u, fieldName) | ||
|
||
fun ByteArray.setFirstByteToZero(): ByteArray { | ||
this[0] = 0 | ||
return this | ||
} | ||
|
||
/** | ||
* Slices the ByteArray into sliceSize bytes chunks and returns the sliceNumber-th chunk. | ||
*/ | ||
fun ByteArray.sliceOf( | ||
sliceSize: Int, | ||
sliceNumber: Int, | ||
allowIncompleteLastSlice: Boolean = false | ||
): ByteArray { | ||
assert(sliceSize > 0) { | ||
"sliceSize=$sliceSize should be greater than 0" | ||
} | ||
|
||
val startIndex = sliceNumber * sliceSize | ||
val endIndex = (sliceNumber * sliceSize + sliceSize - 1) | ||
.let { | ||
if (it >= this.size && allowIncompleteLastSlice) { | ||
this.size - 1 | ||
} else { | ||
it | ||
} | ||
} | ||
|
||
assert(startIndex <= this.size && endIndex < this.size) { | ||
"slice $startIndex..$endIndex is out of array size=${this.size}" | ||
} | ||
|
||
return this.sliceArray(startIndex..endIndex) | ||
} | ||
|
||
/** | ||
* Slices the ByteArray into 32 bytes chunks and returns the sliceNumber-th chunk. | ||
*/ | ||
fun ByteArray.sliceOf32(sliceNumber: Int): ByteArray { | ||
return this.sliceOf(sliceSize = 32, sliceNumber) | ||
} |
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
112 changes: 112 additions & 0 deletions
112
jvm-libs/kotlin-extensions/src/test/kotlin/net/consensys/ByteArrayExtensionsTest.kt
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,112 @@ | ||
package net.consensys | ||
|
||
import org.assertj.core.api.Assertions.assertThat | ||
import org.assertj.core.api.Assertions.assertThatThrownBy | ||
import org.junit.jupiter.api.Test | ||
import kotlin.random.Random | ||
|
||
class ByteArrayExtensionsTest { | ||
@Test | ||
fun `ByteArray#encodeHex`() { | ||
assertThat(byteArrayOf().encodeHex()).isEqualTo("0x") | ||
assertThat(byteArrayOf(0).encodeHex()).isEqualTo("0x00") | ||
assertThat(byteArrayOf(1).encodeHex()).isEqualTo("0x01") | ||
assertThat(byteArrayOf(0x12, 0x34, 0x56).encodeHex()).isEqualTo("0x123456") | ||
} | ||
|
||
@Test | ||
fun `ByteArray#assertSize`() { | ||
assertThatThrownBy { | ||
byteArrayOf(1, 2, 3).assertSize(2u, "shortNumber") | ||
}.isInstanceOf(IllegalArgumentException::class.java) | ||
.hasMessage("shortNumber expected to have 2 bytes, but got 3") | ||
|
||
assertThat(byteArrayOf(1, 2, 3).assertSize(3u)).isEqualTo(byteArrayOf(1, 2, 3)) | ||
} | ||
|
||
@Test | ||
fun `ByteArray#assertIs32Bytes`() { | ||
assertThatThrownBy { | ||
byteArrayOf(1, 2, 3).assertIs32Bytes("hash") | ||
}.isInstanceOf(IllegalArgumentException::class.java) | ||
.hasMessage("hash expected to have 32 bytes, but got 3") | ||
ByteArray(32).assertIs32Bytes() | ||
} | ||
|
||
@Test | ||
fun `ByteArray#assertIs20Bytes`() { | ||
assertThatThrownBy { | ||
byteArrayOf(1, 2, 3).assertIs20Bytes("address") | ||
}.isInstanceOf(IllegalArgumentException::class.java) | ||
.hasMessage("address expected to have 20 bytes, but got 3") | ||
ByteArray(20).assertIs20Bytes() | ||
} | ||
|
||
@Test | ||
fun `ByteArray#setFirstByteToZero`() { | ||
assertThat(Random.Default.nextBytes(32).setFirstByteToZero()[0]).isEqualTo(0) | ||
} | ||
|
||
@Test | ||
fun `BigInteger#toKWei`() { | ||
assertThat(1_234_000.toBigInteger().toKWei().toUInt()).isEqualTo(1234u) | ||
assertThat(1_234_400.toBigInteger().toKWei().toUInt()).isEqualTo(1234u) | ||
assertThat(1_234_500.toBigInteger().toKWei().toUInt()).isEqualTo(1235u) | ||
assertThat(1_234_600.toBigInteger().toKWei().toUInt()).isEqualTo(1235u) | ||
} | ||
|
||
@Test | ||
fun `ByteArray#sliceOf`() { | ||
val bytes = Random.Default.nextBytes(64) | ||
bytes.sliceOf(sliceSize = 5, sliceNumber = 0).also { | ||
assertThat(it).hasSize(5) | ||
assertThat(it).isEqualTo(bytes.sliceArray(0..4)) | ||
} | ||
bytes.sliceOf(sliceSize = 10, sliceNumber = 0).also { | ||
assertThat(it).hasSize(10) | ||
assertThat(it).isEqualTo(bytes.sliceArray(0..9)) | ||
} | ||
bytes.sliceOf(sliceSize = 10, sliceNumber = 2).also { | ||
assertThat(it).hasSize(10) | ||
assertThat(it).isEqualTo(bytes.sliceArray(20..29)) | ||
} | ||
bytes.sliceOf(sliceSize = 1, sliceNumber = 63, allowIncompleteLastSlice = false).also { | ||
assertThat(it).hasSize(1) | ||
assertThat(it).isEqualTo(bytes.sliceArray(63..63)) | ||
} | ||
assertThatThrownBy { | ||
bytes.sliceOf(sliceSize = 1, sliceNumber = 64, allowIncompleteLastSlice = false) | ||
} | ||
.isInstanceOf(AssertionError::class.java) | ||
.hasMessage("slice 64..64 is out of array size=64") | ||
|
||
bytes.sliceOf(sliceSize = 10, sliceNumber = 6, allowIncompleteLastSlice = true).also { | ||
assertThat(it).hasSize(4) | ||
assertThat(it).isEqualTo(bytes.sliceArray(60..63)) | ||
} | ||
|
||
assertThatThrownBy { | ||
bytes.sliceOf(sliceSize = 10, sliceNumber = 6, allowIncompleteLastSlice = false) | ||
} | ||
.isInstanceOf(AssertionError::class.java) | ||
.hasMessage("slice 60..69 is out of array size=64") | ||
|
||
assertThatThrownBy { | ||
bytes.sliceOf(sliceSize = 10, sliceNumber = 7) | ||
} | ||
.isInstanceOf(AssertionError::class.java) | ||
.hasMessage("slice 70..79 is out of array size=64") | ||
} | ||
|
||
@Test | ||
fun `ByteArray#sliceOf32Bytes`() { | ||
val bytes = Random.Default.nextBytes(64) | ||
assertThat(bytes.sliceOf32(0)).isEqualTo(bytes.sliceArray(0..31)) | ||
assertThat(bytes.sliceOf32(1)).isEqualTo(bytes.sliceArray(32..63)) | ||
assertThatThrownBy { | ||
Random.Default.nextBytes(64 + 16).sliceOf32(sliceNumber = 2) | ||
} | ||
.isInstanceOf(AssertionError::class.java) | ||
.hasMessage("slice 64..95 is out of array size=80") | ||
} | ||
} |
Oops, something went wrong.