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));
});
}
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));
}
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]));
}
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());
}
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));
});
}
Aggregations