Search in sources :

Example 1 with SszData

use of tech.pegasys.teku.infrastructure.ssz.SszData in project teku by ConsenSys.

the class SszSchemaTestBase method assertTreeRoundtrip.

protected void assertTreeRoundtrip(final SszSchema<?> schema, final int maxBranchLevelsSkipped, final SszData data) {
    final InMemoryStoringTreeNodeStore nodeStore = new InMemoryStoringTreeNodeStore();
    final TreeNode node = data.getBackingNode();
    final long rootGIndex = 34;
    schema.storeBackingNodes(nodeStore, maxBranchLevelsSkipped, rootGIndex, node);
    final TreeNode result = schema.loadBackingNodes(nodeStore, data.hashTreeRoot(), rootGIndex);
    assertThatTreeNode(result).isTreeEqual(node);
    final SszData rebuiltData = schema.createFromBackingNode(result);
    assertThat(rebuiltData).isEqualTo(data);
}
Also used : TreeNodeAssert.assertThatTreeNode(tech.pegasys.teku.infrastructure.ssz.schema.TreeNodeAssert.assertThatTreeNode) TreeNode(tech.pegasys.teku.infrastructure.ssz.tree.TreeNode) SszData(tech.pegasys.teku.infrastructure.ssz.SszData)

Example 2 with SszData

use of tech.pegasys.teku.infrastructure.ssz.SszData in project teku by ConsenSys.

the class SszSchemaTestBase method loadBackingNodes_shouldRestoreDefaultTree.

@MethodSource("testSchemaArguments")
@ParameterizedTest
void loadBackingNodes_shouldRestoreDefaultTree(SszSchema<?> schema) {
    final InMemoryStoringTreeNodeStore nodeStore = new InMemoryStoringTreeNodeStore();
    final TreeNode node = schema.getDefault().getBackingNode();
    final long rootGIndex = 67;
    schema.storeBackingNodes(nodeStore, 100, rootGIndex, node);
    // LeafNode should be equal
    final TreeNode result = schema.loadBackingNodes(nodeStore, node.hashTreeRoot(), rootGIndex);
    assertThatTreeNode(result).isTreeEqual(node);
    // And should be equal when accessing the actual value
    final SszData rebuiltData = schema.createFromBackingNode(result);
    assertThat(rebuiltData).isEqualTo(schema.createFromBackingNode(node));
}
Also used : TreeNodeAssert.assertThatTreeNode(tech.pegasys.teku.infrastructure.ssz.schema.TreeNodeAssert.assertThatTreeNode) TreeNode(tech.pegasys.teku.infrastructure.ssz.tree.TreeNode) SszData(tech.pegasys.teku.infrastructure.ssz.SszData) MethodSource(org.junit.jupiter.params.provider.MethodSource) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 3 with SszData

use of tech.pegasys.teku.infrastructure.ssz.SszData in project teku by ConsenSys.

the class AbstractSszGenericTestExecutor method runTest.

@Override
public void runTest(final TestDefinition testDefinition) throws Throwable {
    final Bytes inputData = TestDataUtils.readSszData(testDefinition, "serialized.ssz_snappy");
    final Path path = testDefinition.getTestDirectory().resolve("value.yaml");
    if (path.toFile().exists()) {
        final SszSchema<?> schema = getSchema(testDefinition);
        final Meta meta = TestDataUtils.loadYaml(testDefinition, "meta.yaml", Meta.class);
        final SszData result = schema.sszDeserialize(inputData);
        assertThat(result.hashTreeRoot()).isEqualTo(Bytes32.fromHexString(meta.root));
        assertValueCorrect(testDefinition, result);
    } else {
        try {
            final SszSchema<?> schema = getSchema(testDefinition);
            assertThatThrownBy(() -> schema.sszDeserialize(inputData)).isInstanceOf(IllegalArgumentException.class);
        } catch (final IllegalArgumentException e) {
        // The schema itself was invalid so this counts as success
        // Can't just put getSchema in the assertThatThrownBy because we need to let
        // TestAbortedException actually throw so that the test is ignored if we don't support the
        // type
        }
    }
}
Also used : Path(java.nio.file.Path) Bytes(org.apache.tuweni.bytes.Bytes) SszData(tech.pegasys.teku.infrastructure.ssz.SszData)

Example 4 with SszData

use of tech.pegasys.teku.infrastructure.ssz.SszData in project teku by ConsenSys.

the class SszSchemaTestBase method sszDeserialize_tooLongSszShouldFailFastWithoutReadingWholeInput.

@MethodSource("testSchemaArguments")
@ParameterizedTest
void sszDeserialize_tooLongSszShouldFailFastWithoutReadingWholeInput(SszSchema<SszData> schema) {
    long maxSszLength = schema.getSszLengthBounds().getMaxBytes();
    // ignore too large and degenerative structs
    assumeThat(maxSszLength).isLessThan(32 * 1024 * 1024).isGreaterThan(0);
    // ignore lists using SszSuperNode as many validations are skipped
    if (schema instanceof AbstractSszCollectionSchema) {
        assumeThat(((AbstractSszCollectionSchema<?, ?>) schema).getHints().getHint(SszSuperNodeHint.class)).describedAs("uses SszSuperNode").isEmpty();
    }
    SszData data = randomSsz.randomData(schema);
    Bytes ssz = data.sszSerialize();
    Bytes sszWithExtraData = Bytes.wrap(ssz, Bytes.random((int) (maxSszLength - ssz.size() + 1)));
    AtomicInteger bytesCounter = new AtomicInteger();
    SimpleSszReader countingReader = new SimpleSszReader(sszWithExtraData) {

        @Override
        public Bytes read(int length) {
            bytesCounter.addAndGet(length);
            return super.read(length);
        }
    };
    assertThatThrownBy(() -> schema.sszDeserialize(countingReader)).isInstanceOf(SszDeserializeException.class);
    assertThat(bytesCounter.get()).isLessThanOrEqualTo(ssz.size());
}
Also used : SimpleSszReader(tech.pegasys.teku.infrastructure.ssz.sos.SimpleSszReader) Bytes(org.apache.tuweni.bytes.Bytes) AbstractSszCollectionSchema(tech.pegasys.teku.infrastructure.ssz.schema.impl.AbstractSszCollectionSchema) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) SszSuperNodeHint(tech.pegasys.teku.infrastructure.ssz.schema.SszSchemaHints.SszSuperNodeHint) SszData(tech.pegasys.teku.infrastructure.ssz.SszData) MethodSource(org.junit.jupiter.params.provider.MethodSource) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 5 with SszData

use of tech.pegasys.teku.infrastructure.ssz.SszData in project teku by ConsenSys.

the class SszSchemaTestBase method getDefaultTree_shouldBeEqualToDefaultStructure.

@MethodSource("testSchemaArguments")
@ParameterizedTest
void getDefaultTree_shouldBeEqualToDefaultStructure(SszSchema<SszData> schema) {
    SszData defaultTreeData = schema.createFromBackingNode(schema.getDefaultTree());
    SszDataAssert.assertThatSszData(defaultTreeData).isEqualByAllMeansTo(schema.getDefault());
}
Also used : SszData(tech.pegasys.teku.infrastructure.ssz.SszData) MethodSource(org.junit.jupiter.params.provider.MethodSource) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Aggregations

SszData (tech.pegasys.teku.infrastructure.ssz.SszData)6 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)3 MethodSource (org.junit.jupiter.params.provider.MethodSource)3 Bytes (org.apache.tuweni.bytes.Bytes)2 TreeNodeAssert.assertThatTreeNode (tech.pegasys.teku.infrastructure.ssz.schema.TreeNodeAssert.assertThatTreeNode)2 TreeNode (tech.pegasys.teku.infrastructure.ssz.tree.TreeNode)2 Path (java.nio.file.Path)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1 SszSuperNodeHint (tech.pegasys.teku.infrastructure.ssz.schema.SszSchemaHints.SszSuperNodeHint)1 AbstractSszCollectionSchema (tech.pegasys.teku.infrastructure.ssz.schema.impl.AbstractSszCollectionSchema)1 SimpleSszReader (tech.pegasys.teku.infrastructure.ssz.sos.SimpleSszReader)1