SignatureManagerImpl implements SignatureManager { @Override public Map<SignatureType, SignatureFileInfo> getLatestSignatureFiles() { final Configuration properties = config.getProperties(); properties.setProperty(DroidGlobalProperty.LAST_UPDATE_CHECK.getName(), System.currentTimeMillis()); Map<SignatureType, SignatureFileInfo> latestSigFiles = new HashMap<SignatureType, SignatureFileInfo>(); final Map<SignatureType, SortedMap<String, SignatureFileInfo>> availableSignatureFiles = getAvailableSignatureFiles(); for (Map.Entry<SignatureType, SignatureUpdateService> entry : signatureUpdateServices.entrySet()) { SignatureType type = entry.getKey(); SignatureUpdateService updateService = entry.getValue(); final Map<String, SignatureFileInfo> signaturesForType = availableSignatureFiles.get(type); try { int latestVersionForType = getLatestVersionForType(type, signaturesForType); SignatureFileInfo latestUpdate = updateService.getLatestVersion(latestVersionForType); if (latestUpdate != null && latestUpdate.getVersion() > 0 && !signaturesForType.containsValue(latestUpdate)) { latestSigFiles.put(type, latestUpdate); } } catch (SignatureServiceException e) { latestSigFiles.put(type, new SignatureFileInfo(e)); } } return latestSigFiles; } SignatureManagerImpl();  SignatureManagerImpl(DroidGlobalConfig config, Map<SignatureType, SignatureUpdateService> updateServices); void init(); @Override Map<SignatureType, SortedMap<String, SignatureFileInfo>> getAvailableSignatureFiles(); void setConfig(DroidGlobalConfig config); @Override Map<SignatureType, SignatureFileInfo> getLatestSignatureFiles(); void setSignatureUpdateServices(Map<SignatureType, SignatureUpdateService> signatureUpdateServices); @Override SignatureFileInfo downloadLatest(final SignatureType type); @Override Map<SignatureType, SignatureFileInfo> getDefaultSignatures(); @Override SignatureFileInfo install(final SignatureType type, final Path signatureFile, final boolean setDefault);  }
@Test public void testGetLatestConatinerSignatureFileWhenCurrentFileIsOutOfDate() throws Exception { SignatureFileInfo latestContainerSigInfo = new SignatureFileInfo(20100930, false, SignatureType.CONTAINER); when(containerSignatureService.getLatestVersion(20100828)).thenReturn(latestContainerSigInfo); deleteSigDirs(); createSigDirs(); final Path containerSigFile1 = containerSigFileDir.resolve("container_sigs-20100827.xml"); final Path containerSigFile2 = containerSigFileDir.resolve("container_sigs-20100828.xml"); Files.createFile(containerSigFile1); Files.createFile(containerSigFile2); assertTrue(Files.exists(containerSigFile1)); assertTrue(Files.exists(containerSigFile2)); SignatureFileInfo sigFileInfo = signatureManager.getLatestSignatureFiles().get(SignatureType.CONTAINER); assertEquals(latestContainerSigInfo, sigFileInfo); }
@Ignore @Test public void testCheckForNewSignatureFileWhenNewFileIsNotAvailable() throws SignatureServiceException { SignatureFileInfo signatureFileInfo = new SignatureFileInfo(26, false, SignatureType.BINARY); when(signatureService.getLatestVersion(20100101)).thenReturn(signatureFileInfo); when(containerSignatureService.getLatestVersion(20100101)).thenReturn(null); Map<SignatureType, SignatureFileInfo> newSignatures = signatureManager.getLatestSignatureFiles(); final SignatureFileInfo newSignature = newSignatures.get(SignatureType.BINARY); assertNull(newSignature); ArgumentCaptor<Long> captor = ArgumentCaptor.forClass(Long.class); verify(configuration).setProperty(eq(DroidGlobalProperty.LAST_UPDATE_CHECK.getName()), captor.capture()); assertThat((double) captor.getValue(), Matchers.closeTo(System.currentTimeMillis(), 200L)); }
@Test @Ignore public void testCheckForNewSignatureFileWhenNewFileIsAvailable() throws SignatureServiceException { SignatureFileInfo signatureFileInfo = new SignatureFileInfo(100, false, SignatureType.BINARY); when(signatureService.getLatestVersion(26)).thenReturn(signatureFileInfo); Map<SignatureType, SignatureFileInfo> newSignatures = signatureManager.getLatestSignatureFiles(); assertNotNull(newSignatures.get(SignatureType.BINARY)); assertEquals(signatureFileInfo, newSignatures.get(SignatureType.BINARY)); ArgumentCaptor<Long> captor = ArgumentCaptor.forClass(Long.class); verify(configuration).setProperty(eq(DroidGlobalProperty.LAST_UPDATE_CHECK.getName()), captor.capture()); assertThat((double) captor.getValue(), Matchers.closeTo(System.currentTimeMillis(), 1000L)); }
@Test public void testGetLatestContainerSignatureFileWhenCurrentFileIsLatest() throws Exception { SignatureFileInfo latestContainerSigInfo = new SignatureFileInfo(20100929, false, SignatureType.CONTAINER); when(containerSignatureService.getLatestVersion(20100101)).thenReturn(latestContainerSigInfo); deleteSigDirs(); createSigDirs(); final Path containerSigFile1 = containerSigFileDir.resolve("container_sigs-20100927.xml"); final Path containerSigFile2 = containerSigFileDir.resolve("container_sigs-20100929.xml"); Files.createFile(containerSigFile1); Files.createFile(containerSigFile2); assertTrue(Files.exists(containerSigFile1)); assertTrue(Files.exists(containerSigFile2)); SignatureFileInfo sigFileInfo = signatureManager.getLatestSignatureFiles().get(SignatureType.CONTAINER); assertNull(sigFileInfo); }