KeyRefresher { protected boolean haveFilesBeenChanged(final String filePath, byte[] checksum) { final Path path = Paths.get(filePath); if (!path.isAbsolute()) { if (LOGGER.isDebugEnabled()) { LOGGER.debug("Relative path: {} specified - ignoring change check", filePath); } return false; } try (InputStream is = Files.newInputStream(path); DigestInputStream digestInputStream = new DigestInputStream(is, md)) { while (digestInputStream.read() != -1) { } } catch (IOException ex) { LOGGER.warn("Error reading file " + filePath, ex); return false; } byte[] digest = md.digest(); if (!Arrays.equals(checksum, digest)) { System.arraycopy(digest, 0, checksum, 0, digest.length); return true; } return false; } KeyRefresher(final String athenzPublicCert, final String athenzPrivateKey, final TrustStore trustStore,
            final KeyManagerProxy keyManagerProxy, final TrustManagerProxy trustManagerProxy);  KeyRefresher(final String athenzPublicCert, final String athenzPrivateKey, final TrustStore trustStore,
                        final KeyManagerProxy keyManagerProxy, final TrustManagerProxy trustManagerProxy,
                        final KeyRefresherListener keyRefresherListener); KeyManagerProxy getKeyManagerProxy(); TrustManagerProxy getTrustManagerProxy(); void shutdown(); void startup(); void startup(int retryFrequency); static final int DEFAULT_RETRY_CHECK_FREQUENCY; }
@Test public void haveFilesBeenChangedTestFilesAltered() throws Exception { KeyRefresher keyRefresher = new KeyRefresher("", "", mockedTrustStore, mockedKeyManagerProxy, mockedTrustManagerProxy); assertTrue(keyRefresher.haveFilesBeenChanged(classLoader.getResource("testFile").getPath(), new byte[md.getDigestLength()])); }
@Test public void haveFilesBeenChangedTestFilesMultiple() throws Exception { byte[] checksum = new byte[md.getDigestLength()]; KeyRefresher keyRefresher = new KeyRefresher("", "", mockedTrustStore, mockedKeyManagerProxy, mockedTrustManagerProxy); assertTrue(keyRefresher.haveFilesBeenChanged(classLoader.getResource("testFile").getPath(), checksum)); assertFalse(keyRefresher.haveFilesBeenChanged(classLoader.getResource("testFile").getPath(), checksum)); checksum[0] = 0; checksum[1] = 1; checksum[2] = 2; assertTrue(keyRefresher.haveFilesBeenChanged(classLoader.getResource("testFile").getPath(), checksum)); }
@Test public void haveFilesBeenChangedTestFilesMultipleRelativePath() throws Exception { byte[] checksum = new byte[md.getDigestLength()]; KeyRefresher keyRefresher = new KeyRefresher("", "", mockedTrustStore, mockedKeyManagerProxy, mockedTrustManagerProxy); assertFalse(keyRefresher.haveFilesBeenChanged("testfile", checksum)); checksum[0] = 0; checksum[1] = 1; checksum[2] = 2; assertFalse(keyRefresher.haveFilesBeenChanged("testfile", checksum)); }
@Test public void filesBeenChangedTestIOException() throws Exception { KeyRefresher keyRefresher = new KeyRefresher("", "", mockedTrustStore, mockedKeyManagerProxy, mockedTrustManagerProxy); assertFalse(keyRefresher.haveFilesBeenChanged(classLoader.getResource("").getPath(), new byte[md.getDigestLength()])); }
@Test public void haveFilesBeenChangedTestFilesSame(@Mocked MessageDigest mockedMessageDigest) throws Exception { KeyRefresher keyRefresher = new KeyRefresher("", "", mockedTrustStore, mockedKeyManagerProxy, mockedTrustManagerProxy); byte[] stuff = new byte[md.getDigestLength()]; new Expectations() {{ mockedMessageDigest.digest(); result = stuff; }}; assertFalse(keyRefresher.haveFilesBeenChanged(classLoader.getResource("testFile").getPath(), stuff)); }