FileSSHRecordStoreConnection implements SSHRecordStoreConnection { @Override public int deleteExpiredSSHCertRecords(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; } FileSSHRecordStoreConnection(File rootDir); @Override void close(); @Override void setOperationTimeout(int opTimeout); @Override SSHCertRecord getSSHCertRecord(String instanceId, String service); @Override boolean updateSSHCertRecord(SSHCertRecord certRecord); @Override boolean insertSSHCertRecord(SSHCertRecord certRecord); @Override boolean deleteSSHCertRecord(String instanceId, String service); @Override int deleteExpiredSSHCertRecords(int expiryTimeMins);  }
@Test public void testDeleteExpiredSSHCertRecords() throws Exception { ZTSTestUtils.deleteDirectory(new File("/tmp/zts-ssh-tests")); FileSSHRecordStore store = new FileSSHRecordStore(new File("/tmp/zts-ssh-tests")); FileSSHRecordStoreConnection con = (FileSSHRecordStoreConnection) store.getConnection(); assertNotNull(con); SSHCertRecord certRecord = new SSHCertRecord(); certRecord.setInstanceId("instance-id"); certRecord.setService("cn"); certRecord.setPrincipals("host1,host2"); certRecord.setClientIP("10.10.10.11"); certRecord.setPrivateIP("10.10.10.12"); assertTrue(con.insertSSHCertRecord(certRecord)); SSHCertRecord certRecordCheck = con.getSSHCertRecord("instance-id", "cn"); assertNotNull(certRecordCheck); Thread.sleep(1000); con.deleteExpiredSSHCertRecords(0); certRecordCheck = con.getSSHCertRecord("instance-id", "cn"); assertNull(certRecordCheck); con.close(); }
@Test public void testDeleteExpiredSSHCertRecordsNoDir() { ZTSTestUtils.deleteDirectory(new File("/tmp/zts-ssh-tests")); FileSSHRecordStore store = new FileSSHRecordStore(new File("/tmp/zts-ssh-tests")); FileSSHRecordStoreConnection con = (FileSSHRecordStoreConnection) store.getConnection(); assertNotNull(con); File dir = Mockito.spy(con.rootDir); Mockito.when(dir.list()).thenReturn(null); con.rootDir = dir; assertEquals(con.deleteExpiredSSHCertRecords(0), 0); }