@Test void readInvalidFileWithInvalidPrevHashMarker() { File invalidFile = createTmpEventFile(EventFileReaderImpl.EVENT_STREAM_FILE_VERSION_3, (byte) 0x0, prevHash, content); assertThrows(InvalidEventFileException.class, () -> { eventFileReader.read(invalidFile); }); }
@Override public EventFile read(File file) { EventFile eventFile = new EventFile(); String fileName = file.getName(); try (DataInputStream dis = new DataInputStream(new BufferedInputStream(new FileInputStream(file)))) { MessageDigest md = MessageDigest.getInstance(HASH_ALGORITHM); int fileVersion = dis.readInt(); md.update(Ints.toByteArray(fileVersion)); if (fileVersion < EVENT_STREAM_FILE_VERSION_2 || fileVersion > EVENT_STREAM_FILE_VERSION_3) { throw new InvalidEventFileException("Invalid event stream file version " + fileVersion); } byte typePrevHash = dis.readByte(); md.update(typePrevHash); if (typePrevHash != EVENT_TYPE_PREV_HASH) { throw new InvalidEventFileException("Expect EVENT_TYPE_PREV_HASH marker, got " + typePrevHash); } byte[] prevFileHash = new byte[EVENT_PREV_HASH_LENGTH]; dis.readFully(prevFileHash); md.update(prevFileHash); byte[] remaining = dis.readAllBytes(); if (remaining.length != 0) { if (fileVersion == EVENT_STREAM_FILE_VERSION_2) { md.update(remaining); } else { MessageDigest mdForEventData = MessageDigest.getInstance(HASH_ALGORITHM); md.update(mdForEventData.digest(remaining)); } } eventFile.setName(fileName); eventFile.setFileVersion(fileVersion); eventFile.setPreviousHash(Hex.encodeHexString(prevFileHash)); eventFile.setFileHash(Hex.encodeHexString(md.digest())); eventFile.setCount(0L); } catch (InvalidEventFileException e) { throw e; } catch (Exception e) { throw new InvalidEventFileException("Error reading bad event file " + fileName, e); } return eventFile; }
EventFileReaderImpl implements EventFileReader { @Override public EventFile read(File file) { EventFile eventFile = new EventFile(); String fileName = file.getName(); try (DataInputStream dis = new DataInputStream(new BufferedInputStream(new FileInputStream(file)))) { MessageDigest md = MessageDigest.getInstance(HASH_ALGORITHM); int fileVersion = dis.readInt(); md.update(Ints.toByteArray(fileVersion)); if (fileVersion < EVENT_STREAM_FILE_VERSION_2 || fileVersion > EVENT_STREAM_FILE_VERSION_3) { throw new InvalidEventFileException("Invalid event stream file version " + fileVersion); } byte typePrevHash = dis.readByte(); md.update(typePrevHash); if (typePrevHash != EVENT_TYPE_PREV_HASH) { throw new InvalidEventFileException("Expect EVENT_TYPE_PREV_HASH marker, got " + typePrevHash); } byte[] prevFileHash = new byte[EVENT_PREV_HASH_LENGTH]; dis.readFully(prevFileHash); md.update(prevFileHash); byte[] remaining = dis.readAllBytes(); if (remaining.length != 0) { if (fileVersion == EVENT_STREAM_FILE_VERSION_2) { md.update(remaining); } else { MessageDigest mdForEventData = MessageDigest.getInstance(HASH_ALGORITHM); md.update(mdForEventData.digest(remaining)); } } eventFile.setName(fileName); eventFile.setFileVersion(fileVersion); eventFile.setPreviousHash(Hex.encodeHexString(prevFileHash)); eventFile.setFileHash(Hex.encodeHexString(md.digest())); eventFile.setCount(0L); } catch (InvalidEventFileException e) { throw e; } catch (Exception e) { throw new InvalidEventFileException("Error reading bad event file " + fileName, e); } return eventFile; } }
EventFileReaderImpl implements EventFileReader { @Override public EventFile read(File file) { EventFile eventFile = new EventFile(); String fileName = file.getName(); try (DataInputStream dis = new DataInputStream(new BufferedInputStream(new FileInputStream(file)))) { MessageDigest md = MessageDigest.getInstance(HASH_ALGORITHM); int fileVersion = dis.readInt(); md.update(Ints.toByteArray(fileVersion)); if (fileVersion < EVENT_STREAM_FILE_VERSION_2 || fileVersion > EVENT_STREAM_FILE_VERSION_3) { throw new InvalidEventFileException("Invalid event stream file version " + fileVersion); } byte typePrevHash = dis.readByte(); md.update(typePrevHash); if (typePrevHash != EVENT_TYPE_PREV_HASH) { throw new InvalidEventFileException("Expect EVENT_TYPE_PREV_HASH marker, got " + typePrevHash); } byte[] prevFileHash = new byte[EVENT_PREV_HASH_LENGTH]; dis.readFully(prevFileHash); md.update(prevFileHash); byte[] remaining = dis.readAllBytes(); if (remaining.length != 0) { if (fileVersion == EVENT_STREAM_FILE_VERSION_2) { md.update(remaining); } else { MessageDigest mdForEventData = MessageDigest.getInstance(HASH_ALGORITHM); md.update(mdForEventData.digest(remaining)); } } eventFile.setName(fileName); eventFile.setFileVersion(fileVersion); eventFile.setPreviousHash(Hex.encodeHexString(prevFileHash)); eventFile.setFileHash(Hex.encodeHexString(md.digest())); eventFile.setCount(0L); } catch (InvalidEventFileException e) { throw e; } catch (Exception e) { throw new InvalidEventFileException("Error reading bad event file " + fileName, e); } return eventFile; }  }
EventFileReaderImpl implements EventFileReader { @Override public EventFile read(File file) { EventFile eventFile = new EventFile(); String fileName = file.getName(); try (DataInputStream dis = new DataInputStream(new BufferedInputStream(new FileInputStream(file)))) { MessageDigest md = MessageDigest.getInstance(HASH_ALGORITHM); int fileVersion = dis.readInt(); md.update(Ints.toByteArray(fileVersion)); if (fileVersion < EVENT_STREAM_FILE_VERSION_2 || fileVersion > EVENT_STREAM_FILE_VERSION_3) { throw new InvalidEventFileException("Invalid event stream file version " + fileVersion); } byte typePrevHash = dis.readByte(); md.update(typePrevHash); if (typePrevHash != EVENT_TYPE_PREV_HASH) { throw new InvalidEventFileException("Expect EVENT_TYPE_PREV_HASH marker, got " + typePrevHash); } byte[] prevFileHash = new byte[EVENT_PREV_HASH_LENGTH]; dis.readFully(prevFileHash); md.update(prevFileHash); byte[] remaining = dis.readAllBytes(); if (remaining.length != 0) { if (fileVersion == EVENT_STREAM_FILE_VERSION_2) { md.update(remaining); } else { MessageDigest mdForEventData = MessageDigest.getInstance(HASH_ALGORITHM); md.update(mdForEventData.digest(remaining)); } } eventFile.setName(fileName); eventFile.setFileVersion(fileVersion); eventFile.setPreviousHash(Hex.encodeHexString(prevFileHash)); eventFile.setFileHash(Hex.encodeHexString(md.digest())); eventFile.setCount(0L); } catch (InvalidEventFileException e) { throw e; } catch (Exception e) { throw new InvalidEventFileException("Error reading bad event file " + fileName, e); } return eventFile; }  @Override EventFile read(File file); }
EventFileReaderImpl implements EventFileReader { @Override public EventFile read(File file) { EventFile eventFile = new EventFile(); String fileName = file.getName(); try (DataInputStream dis = new DataInputStream(new BufferedInputStream(new FileInputStream(file)))) { MessageDigest md = MessageDigest.getInstance(HASH_ALGORITHM); int fileVersion = dis.readInt(); md.update(Ints.toByteArray(fileVersion)); if (fileVersion < EVENT_STREAM_FILE_VERSION_2 || fileVersion > EVENT_STREAM_FILE_VERSION_3) { throw new InvalidEventFileException("Invalid event stream file version " + fileVersion); } byte typePrevHash = dis.readByte(); md.update(typePrevHash); if (typePrevHash != EVENT_TYPE_PREV_HASH) { throw new InvalidEventFileException("Expect EVENT_TYPE_PREV_HASH marker, got " + typePrevHash); } byte[] prevFileHash = new byte[EVENT_PREV_HASH_LENGTH]; dis.readFully(prevFileHash); md.update(prevFileHash); byte[] remaining = dis.readAllBytes(); if (remaining.length != 0) { if (fileVersion == EVENT_STREAM_FILE_VERSION_2) { md.update(remaining); } else { MessageDigest mdForEventData = MessageDigest.getInstance(HASH_ALGORITHM); md.update(mdForEventData.digest(remaining)); } } eventFile.setName(fileName); eventFile.setFileVersion(fileVersion); eventFile.setPreviousHash(Hex.encodeHexString(prevFileHash)); eventFile.setFileHash(Hex.encodeHexString(md.digest())); eventFile.setCount(0L); } catch (InvalidEventFileException e) { throw e; } catch (Exception e) { throw new InvalidEventFileException("Error reading bad event file " + fileName, e); } return eventFile; }  @Override EventFile read(File file); static final String HASH_ALGORITHM; static final byte EVENT_TYPE_PREV_HASH; static final int EVENT_PREV_HASH_LENGTH; static final byte EVENT_STREAM_FILE_VERSION_2; static final byte EVENT_STREAM_FILE_VERSION_3; }