diff --git a/ergo-core/src/test/scala/org/ergoplatform/utils/generators/ErgoCoreGenerators.scala b/ergo-core/src/test/scala/org/ergoplatform/utils/generators/ErgoCoreGenerators.scala index e56f87641b..0cbb42425e 100644 --- a/ergo-core/src/test/scala/org/ergoplatform/utils/generators/ErgoCoreGenerators.scala +++ b/ergo-core/src/test/scala/org/ergoplatform/utils/generators/ErgoCoreGenerators.scala @@ -88,11 +88,37 @@ object ErgoCoreGenerators { scorex.util.Random.randomBytes } + /** + * Main KV generator: contains special handling for key.head == 0 + */ def extensionKvGen(keySize: Int, valuesSize: Int): Gen[(Array[Byte], Array[Byte])] = for { key <- genSecureBoundedBytes(keySize, keySize) value <- if (key.head == 0) genSecureBoundedBytes(4, 4) else genSecureBoundedBytes(valuesSize, valuesSize) } yield (key, value) + /** + * Random KV generator: all random key/values + */ + def extensionKvGenImvValue(keySize: Int, valuesSize: Int): Gen[(Array[Byte], Array[Byte])] = for { + key <- genSecureBoundedBytes(keySize, keySize) + value <- genSecureBoundedBytes(valuesSize, valuesSize) + } yield (key, value) + + /** + * Special KV generator: does not generate key.head == 1, because improperly packed interlink + * would be generated, leading to failure. + */ + def extensionKvGenValidMKV(keySize: Int, valueSize: Int): Gen[(Array[Byte], Array[Byte])] = { + def keyGenHeadNotOne: Gen[Array[Byte]] = genSecureBoundedBytes(keySize, keySize).flatMap { key => + if (key.headOption.contains(1.toByte)) keyGenHeadNotOne else Gen.const(key) + } + + for { + key <- keyGenHeadNotOne + value <- genSecureBoundedBytes(valueSize, valueSize) + } yield (key, value) + } + lazy val extensionGen: Gen[Extension] = for { headerId <- modifierIdGen mandatoryElements <- Gen.mapOf(extensionKvGen(Extension.FieldKeySize, Extension.FieldValueMaxSize)) diff --git a/src/test/scala/org/ergoplatform/nodeView/state/ErgoStateContextSpec.scala b/src/test/scala/org/ergoplatform/nodeView/state/ErgoStateContextSpec.scala index 386c831b8b..92ef1e4580 100644 --- a/src/test/scala/org/ergoplatform/nodeView/state/ErgoStateContextSpec.scala +++ b/src/test/scala/org/ergoplatform/nodeView/state/ErgoStateContextSpec.scala @@ -67,7 +67,7 @@ class ErgoStateContextSpec extends ErgoCorePropertyTest { sc.appendFullBlock(fbWithFields(imvKey +: oldFields)) shouldBe 'failure // validation of field value sizes - val imvValue = extensionKvGen(Extension.FieldKeySize, Extension.FieldValueMaxSize + 1).sample.get + val imvValue = extensionKvGenImvValue(Extension.FieldKeySize, Extension.FieldValueMaxSize + 1).sample.get sc.appendFullBlock(fbWithFields(imvValue +: oldFields)) shouldBe 'failure // validation of incorrect interlinks @@ -76,8 +76,8 @@ class ErgoStateContextSpec extends ErgoCorePropertyTest { ).fields sc.appendFullBlock(fbWithFields(invalidInterlinks ++ oldFields)) shouldBe 'failure + val validMKV = extensionKvGenValidMKV(Extension.FieldKeySize, Extension.FieldValueMaxSize).sample.get // validation of key duplicates in fields - val validMKV = extensionKvGen(Extension.FieldKeySize, Extension.FieldValueMaxSize).sample.get sc.appendFullBlock(fbWithFields(Seq(validMKV, validMKV) ++ oldFields)) shouldBe 'failure // valid application of correct extension