FileCertRecordStoreConnection implements CertRecordStoreConnection { @Override public int deleteExpiredX509CertRecords(int expiryTimeMins) { String[] fnames = rootDir.list(); if (fnames == null) { return 0; } long currentTime = System.currentTimeMillis(); int count = 0; for (String fname : fnames) { File file = new File(rootDir, fname); if (notExpired(currentTime, file.lastModified(), expiryTimeMins)) { continue; } file.delete(); count += 1; } return count; } FileCertRecordStoreConnection(File rootDir); @Override void close(); @Override void setOperationTimeout(int opTimeout); @Override X509CertRecord getX509CertRecord(String provider, String instanceId, String service); @Override boolean updateX509CertRecord(X509CertRecord certRecord); @Override boolean insertX509CertRecord(X509CertRecord certRecord); @Override boolean deleteX509CertRecord(String provider, String instanceId, String service); @Override int deleteExpiredX509CertRecords(int expiryTimeMins); @Override List<X509CertRecord> updateUnrefreshedCertificatesNotificationTimestamp(String lastNotifiedServer,
                                                                                   long lastNotifiedTime,
                                                                                   String provider);  }
@Test public void testdeleteExpiredX509CertRecords() throws Exception { ZTSTestUtils.deleteDirectory(new File("/tmp/zts-cert-tests")); FileCertRecordStore store = new FileCertRecordStore(new File("/tmp/zts-cert-tests")); FileCertRecordStoreConnection con = (FileCertRecordStoreConnection) store.getConnection(); assertNotNull(con); X509CertRecord certRecord = new X509CertRecord(); Date now = new Date(); certRecord.setService("cn"); certRecord.setProvider("ostk"); certRecord.setInstanceId("instance-id"); certRecord.setCurrentIP("current-ip"); certRecord.setCurrentSerial("current-serial"); certRecord.setCurrentTime(now); certRecord.setPrevIP("prev-ip"); certRecord.setPrevSerial("prev-serial"); certRecord.setPrevTime(now); boolean result = con.insertX509CertRecord(certRecord); assertTrue(result); X509CertRecord certRecordCheck = con.getX509CertRecord("ostk", "instance-id", "cn"); assertNotNull(certRecordCheck); Thread.sleep(1000); con.deleteExpiredX509CertRecords(0); certRecordCheck = con.getX509CertRecord("ostk", "instance-id", "cn"); assertNull(certRecordCheck); con.close(); }
@Test public void testDeleteExpiredX509CertRecords() { ZTSTestUtils.deleteDirectory(new File("/tmp/zts-cert-tests")); FileCertRecordStore store = new FileCertRecordStore(new File("/tmp/zts-cert-tests")); FileCertRecordStoreConnection con = (FileCertRecordStoreConnection) store.getConnection(); assertNotNull(con); File dir = Mockito.spy(con.rootDir); Mockito.when(dir.list()).thenReturn(null); con.rootDir = dir; assertEquals(con.deleteExpiredX509CertRecords(0), 0); }