Search in sources :

Example 21 with StreamFileData

use of com.hedera.mirror.importer.domain.StreamFileData in project hedera-mirror-node by hashgraph.

the class RecordFileReaderTest method readInvalidFileWithGarbageAppended.

@TestFactory
Stream<DynamicTest> readInvalidFileWithGarbageAppended() {
    String template = "read corrupted version %d file %s";
    return DynamicTest.stream(getFilteredFiles(false), (recordFile) -> String.format(template, recordFile.getVersion(), recordFile.getName()), (recordFile) -> {
        // given
        Path testFile = getTestFile(recordFile);
        byte[] bytes = FileUtils.readFileToByteArray(testFile.toFile());
        byte[] bytesCorrupted = ArrayUtils.addAll(bytes, new byte[] { 0, 1, 2, 3 });
        StreamFileData streamFileData = StreamFileData.from(recordFile.getName(), bytesCorrupted);
        // when
        assertThrows(InvalidStreamFileException.class, () -> recordFileReader.read(streamFileData));
    });
}
Also used : Path(java.nio.file.Path) StreamFileData(com.hedera.mirror.importer.domain.StreamFileData) TestFactory(org.junit.jupiter.api.TestFactory)

Example 22 with StreamFileData

use of com.hedera.mirror.importer.domain.StreamFileData in project hedera-mirror-node by hashgraph.

the class CompositeSignatureFileReaderTest method testValidV2.

@Test
void testValidV2() throws Exception {
    byte[] versionNumber = { SignatureFileReaderV2.SIGNATURE_TYPE_FILE_HASH };
    byte[] randomExtraBytes = new byte[3];
    SecureRandom.getInstanceStrong().nextBytes(randomExtraBytes);
    byte[] bytes = Bytes.concat(versionNumber, randomExtraBytes);
    StreamFileData streamFileData = StreamFileData.from(SIGNATURE_FILENAME, bytes);
    compositeBalanceFileReader.read(streamFileData);
    verify(signatureFileReaderV2, times(1)).read(any(StreamFileData.class));
    verify(signatureFileReaderV5, times(0)).read(any(StreamFileData.class));
}
Also used : StreamFileData(com.hedera.mirror.importer.domain.StreamFileData) Test(org.junit.jupiter.api.Test)

Example 23 with StreamFileData

use of com.hedera.mirror.importer.domain.StreamFileData in project hedera-mirror-node by hashgraph.

the class CompositeSignatureFileReaderTest method testInvalidFileVersion.

@Test
void testInvalidFileVersion() {
    byte[] invalidVersionNumber = { 12 };
    StreamFileData invalidFileData = StreamFileData.from(SIGNATURE_FILENAME, invalidVersionNumber);
    SignatureFileParsingException exception = assertThrows(SignatureFileParsingException.class, () -> {
        compositeBalanceFileReader.read(invalidFileData);
    });
    assertTrue(exception.getMessage().contains("Unsupported signature file version: " + invalidVersionNumber[0]));
}
Also used : SignatureFileParsingException(com.hedera.mirror.importer.exception.SignatureFileParsingException) StreamFileData(com.hedera.mirror.importer.domain.StreamFileData) Test(org.junit.jupiter.api.Test)

Example 24 with StreamFileData

use of com.hedera.mirror.importer.domain.StreamFileData in project hedera-mirror-node by hashgraph.

the class SignatureFileReaderV5Test method testReadValidFile.

@Test
void testReadValidFile() {
    StreamFileData streamFileData = StreamFileData.from(signatureFile);
    FileStreamSignature fileStreamSignature = fileReaderV5.read(streamFileData);
    assertNotNull(fileStreamSignature);
    assertThat(fileStreamSignature.getBytes()).isNotEmpty().isEqualTo(streamFileData.getBytes());
    assertArrayEquals(Base64.decodeBase64(entireFileHashBase64.getBytes()), fileStreamSignature.getFileHash());
    assertArrayEquals(Base64.decodeBase64(entireFileSignatureBase64.getBytes()), fileStreamSignature.getFileHashSignature());
    assertArrayEquals(Base64.decodeBase64(metadataHashBase64.getBytes()), fileStreamSignature.getMetadataHash());
    assertArrayEquals(Base64.decodeBase64(metadataSignatureBase64.getBytes()), fileStreamSignature.getMetadataHashSignature());
}
Also used : StreamFileData(com.hedera.mirror.importer.domain.StreamFileData) FileStreamSignature(com.hedera.mirror.importer.domain.FileStreamSignature) Test(org.junit.jupiter.api.Test) DynamicTest(org.junit.jupiter.api.DynamicTest)

Example 25 with StreamFileData

use of com.hedera.mirror.importer.domain.StreamFileData in project hedera-mirror-node by hashgraph.

the class ProtoBalanceFileReader method toFlux.

private Flux<AccountBalance> toFlux(StreamFileData streamFileData) {
    return Flux.defer(() -> {
        InputStream inputStream = streamFileData.getInputStream();
        ExtensionRegistryLite extensionRegistry = ExtensionRegistryLite.getEmptyRegistry();
        CodedInputStream input = CodedInputStream.newInstance(inputStream);
        AtomicLong consensusTimestamp = new AtomicLong(0L);
        UnknownFieldSet.Builder unknownFieldSet = UnknownFieldSet.newBuilder();
        return Flux.<AccountBalance>generate(sink -> {
            try {
                boolean done = false;
                while (!done) {
                    int tag = input.readTag();
                    switch(tag) {
                        case TAG_EOF:
                            done = true;
                            break;
                        case TAG_TIMESTAMP:
                            Timestamp timestamp = input.readMessage(Timestamp.parser(), extensionRegistry);
                            consensusTimestamp.set(DomainUtils.timestampInNanosMax(timestamp));
                            break;
                        case TAG_BALANCE:
                            Assert.state(consensusTimestamp.get() > 0, "Missing consensus timestamp)");
                            var ab = input.readMessage(SingleAccountBalances.parser(), extensionRegistry);
                            sink.next(toAccountBalance(consensusTimestamp.get(), ab));
                            return;
                        default:
                            log.warn("Unsupported tag: {}", tag);
                            done = !unknownFieldSet.mergeFieldFrom(tag, input);
                    }
                }
                Assert.state(consensusTimestamp.get() > 0, "Missing consensus timestamp)");
                sink.complete();
            } catch (IOException e) {
                sink.error(new StreamFileReaderException(e));
            } catch (IllegalStateException e) {
                sink.error(new InvalidStreamFileException(e));
            }
        }).doFinally(s -> IOUtils.closeQuietly(inputStream));
    });
}
Also used : EntityId(com.hedera.mirror.common.domain.entity.EntityId) AccountBalanceFile(com.hedera.mirror.common.domain.balance.AccountBalanceFile) AccountBalance(com.hedera.mirror.common.domain.balance.AccountBalance) ExtensionRegistryLite(com.google.protobuf.ExtensionRegistryLite) Named(javax.inject.Named) SingleAccountBalances(com.hedera.services.stream.proto.SingleAccountBalances) Timestamp(com.hederahashgraph.api.proto.java.Timestamp) UnknownFieldSet(com.google.protobuf.UnknownFieldSet) StreamFileReaderException(com.hedera.mirror.importer.exception.StreamFileReaderException) DomainUtils(com.hedera.mirror.common.util.DomainUtils) IOException(java.io.IOException) Instant(java.time.Instant) TokenBalance(com.hedera.mirror.common.domain.balance.TokenBalance) Collectors(java.util.stream.Collectors) AtomicLong(java.util.concurrent.atomic.AtomicLong) IOUtils(org.apache.commons.io.IOUtils) Flux(reactor.core.publisher.Flux) InvalidStreamFileException(com.hedera.mirror.importer.exception.InvalidStreamFileException) List(java.util.List) CodedInputStream(com.google.protobuf.CodedInputStream) Log4j2(lombok.extern.log4j.Log4j2) DigestUtils(org.apache.commons.codec.digest.DigestUtils) InputStream(java.io.InputStream) Assert(org.springframework.util.Assert) StreamFileData(com.hedera.mirror.importer.domain.StreamFileData) InvalidStreamFileException(com.hedera.mirror.importer.exception.InvalidStreamFileException) CodedInputStream(com.google.protobuf.CodedInputStream) InputStream(java.io.InputStream) CodedInputStream(com.google.protobuf.CodedInputStream) ExtensionRegistryLite(com.google.protobuf.ExtensionRegistryLite) StreamFileReaderException(com.hedera.mirror.importer.exception.StreamFileReaderException) IOException(java.io.IOException) Timestamp(com.hederahashgraph.api.proto.java.Timestamp) AtomicLong(java.util.concurrent.atomic.AtomicLong) UnknownFieldSet(com.google.protobuf.UnknownFieldSet)

Aggregations

StreamFileData (com.hedera.mirror.importer.domain.StreamFileData)51 Test (org.junit.jupiter.api.Test)40 AccountBalanceFile (com.hedera.mirror.common.domain.balance.AccountBalanceFile)8 Path (java.nio.file.Path)6 TestFactory (org.junit.jupiter.api.TestFactory)6 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)6 FileStreamSignature (com.hedera.mirror.importer.domain.FileStreamSignature)4 AllAccountBalances (com.hedera.services.stream.proto.AllAccountBalances)4 UnknownFieldSet (com.google.protobuf.UnknownFieldSet)3 RecordFile (com.hedera.mirror.common.domain.transaction.RecordFile)3 RecordItem (com.hedera.mirror.common.domain.transaction.RecordItem)3 InvalidStreamFileException (com.hedera.mirror.importer.exception.InvalidStreamFileException)3 Instant (java.time.Instant)3 ValueSource (org.junit.jupiter.params.provider.ValueSource)3 AccountBalance (com.hedera.mirror.common.domain.balance.AccountBalance)2 TokenBalance (com.hedera.mirror.common.domain.balance.TokenBalance)2 EntityId (com.hedera.mirror.common.domain.entity.EntityId)2 EventFile (com.hedera.mirror.common.domain.event.EventFile)2 DomainUtils (com.hedera.mirror.common.util.DomainUtils)2 StreamFilename (com.hedera.mirror.importer.domain.StreamFilename)2