-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
e175308
commit 8a3f952
Showing
5 changed files
with
281 additions
and
41 deletions.
There are no files selected for viewing
79 changes: 79 additions & 0 deletions
79
core/src/main/java/org/owasp/dependencycheck/data/update/cpe/CpeEcosystemCache.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
/* | ||
* This file is part of dependency-check-core. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
* | ||
* Copyright (c) 2020 Jeremy Long. All Rights Reserved. | ||
*/ | ||
package org.owasp.dependencycheck.data.update.cpe; | ||
|
||
import com.google.common.base.Strings; | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
import org.owasp.dependencycheck.data.update.nvd.NvdCveParser; | ||
import org.owasp.dependencycheck.utils.Pair; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
/** | ||
* | ||
* @author Jeremy Long | ||
*/ | ||
public final class CpeEcosystemCache { | ||
|
||
private final static String MULTIPLE_ECOSYSTEMS_IDENTIFIED = "MULTIPLE"; | ||
private static Map<Pair<String, String>, String> cache = new HashMap<>(); | ||
private static Map<Pair<String, String>, String> changed = new HashMap<>(); | ||
|
||
private CpeEcosystemCache() { | ||
//empty constructor for utiilty class | ||
} | ||
/** | ||
* The logger. | ||
*/ | ||
private static final Logger LOGGER = LoggerFactory.getLogger(NvdCveParser.class); | ||
|
||
public static synchronized String getEcosystem(String vendor, String product, String identifiedEcosystem) { | ||
Pair<String, String> key = new Pair<>(vendor, product); | ||
String current = cache.get(key); | ||
String result = null; | ||
if (current == null) { | ||
if (!Strings.isNullOrEmpty(identifiedEcosystem)) { | ||
cache.put(key, identifiedEcosystem); | ||
changed.put(key, identifiedEcosystem); | ||
result = identifiedEcosystem; | ||
} | ||
} else if (MULTIPLE_ECOSYSTEMS_IDENTIFIED.equals(current)) { | ||
//do nothing - result is already null | ||
} else if (current.equals(identifiedEcosystem) || identifiedEcosystem == null) { | ||
result = current; | ||
} else { | ||
cache.put(key, MULTIPLE_ECOSYSTEMS_IDENTIFIED); | ||
changed.put(key, MULTIPLE_ECOSYSTEMS_IDENTIFIED); | ||
} | ||
return result; | ||
} | ||
|
||
public static synchronized void setCache(Map<Pair<String, String>, String> cache) { | ||
CpeEcosystemCache.cache = cache; | ||
CpeEcosystemCache.changed = new HashMap<>(); | ||
} | ||
|
||
public static synchronized Map<Pair<String, String>, String> getChanged() { | ||
return CpeEcosystemCache.changed; | ||
} | ||
|
||
public static synchronized boolean isEmpty() { | ||
return CpeEcosystemCache.cache.isEmpty(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
146 changes: 146 additions & 0 deletions
146
core/src/test/java/org/owasp/dependencycheck/data/update/cpe/CpeEcosystemCacheTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,146 @@ | ||
/* | ||
* This file is part of dependency-check-core. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
* | ||
* Copyright (c) 2020 Jeremy Long. All Rights Reserved. | ||
*/ | ||
package org.owasp.dependencycheck.data.update.cpe; | ||
|
||
import java.util.HashMap; | ||
import java.util.Map; | ||
import org.junit.After; | ||
import org.junit.AfterClass; | ||
import org.junit.Before; | ||
import org.junit.BeforeClass; | ||
import org.junit.Test; | ||
import static org.junit.Assert.*; | ||
import org.owasp.dependencycheck.utils.Pair; | ||
|
||
/** | ||
* | ||
* @author Jeremy Long | ||
*/ | ||
public class CpeEcosystemCacheTest { | ||
|
||
@Before | ||
public void setUp() { | ||
|
||
} | ||
|
||
@After | ||
public void tearDown() { | ||
} | ||
|
||
/** | ||
* Test of getEcosystem method, of class CpeEcosystemCache. | ||
*/ | ||
@Test | ||
public void testGetEcosystem() { | ||
Pair<String, String> key = new Pair<>("apache", "zookeeper"); | ||
Map<Pair<String, String>, String> map = new HashMap<>(); | ||
map.put(key, "java"); | ||
CpeEcosystemCache.setCache(map); | ||
|
||
String expected = "java"; | ||
String result = CpeEcosystemCache.getEcosystem("apache", "zookeeper", null); | ||
assertEquals(expected, result); | ||
|
||
expected = "MULTIPLE"; | ||
result = CpeEcosystemCache.getEcosystem("apache", "zookeeper", "c++"); | ||
assertEquals(expected, result); | ||
|
||
result = CpeEcosystemCache.getEcosystem("pivotal", "spring-framework", null); | ||
assertNull(result); | ||
|
||
expected = "java"; | ||
result = CpeEcosystemCache.getEcosystem("pivotal", "spring-framework", "java"); | ||
assertEquals(expected, result); | ||
|
||
expected = "java"; | ||
result = CpeEcosystemCache.getEcosystem("pivotal", "spring-framework", "java"); | ||
assertEquals(expected, result); | ||
|
||
result = CpeEcosystemCache.getEcosystem("microsoft", "word", null ); | ||
assertNull(result); | ||
|
||
result = CpeEcosystemCache.getEcosystem("microsoft", "word", null ); | ||
assertNull(result); | ||
|
||
result = CpeEcosystemCache.getEcosystem("microsoft", "word", "" ); | ||
assertNull(result); | ||
} | ||
|
||
/** | ||
* Test of setCache method, of class CpeEcosystemCache. | ||
*/ | ||
@Test | ||
public void testSetCache() { | ||
Map<Pair<String, String>, String> map = new HashMap<>(); | ||
CpeEcosystemCache.setCache(map); | ||
assertTrue(CpeEcosystemCache.isEmpty()); | ||
|
||
map = new HashMap<>(); | ||
Pair<String, String> key = new Pair<>("apache", "zookeeper"); | ||
map.put(key, "java"); | ||
CpeEcosystemCache.setCache(map); | ||
|
||
assertFalse(CpeEcosystemCache.isEmpty()); | ||
} | ||
|
||
/** | ||
* Test of getChanged method, of class CpeEcosystemCache. | ||
*/ | ||
@Test | ||
public void testGetChanged() { | ||
Pair<String, String> key = new Pair<>("apache", "zookeeper"); | ||
Map<Pair<String, String>, String> map = new HashMap<>(); | ||
map.put(key, "java"); | ||
CpeEcosystemCache.setCache(map); | ||
|
||
Map<Pair<String, String>, String> result = CpeEcosystemCache.getChanged(); | ||
assertTrue(result.isEmpty()); | ||
|
||
CpeEcosystemCache.getEcosystem("apache", "zookeeper", "java"); | ||
|
||
result = CpeEcosystemCache.getChanged(); | ||
assertTrue(result.isEmpty()); | ||
|
||
CpeEcosystemCache.getEcosystem("apache", "zookeeper", null); | ||
|
||
result = CpeEcosystemCache.getChanged(); | ||
assertTrue(result.isEmpty()); | ||
|
||
CpeEcosystemCache.getEcosystem("apache", "zookeeper", "c++"); | ||
|
||
result = CpeEcosystemCache.getChanged(); | ||
assertFalse(result.isEmpty()); | ||
} | ||
|
||
/** | ||
* Test of isEmpty method, of class CpeEcosystemCache. | ||
*/ | ||
@Test | ||
public void testIsEmpty() { | ||
Map<Pair<String, String>, String> map = new HashMap<>(); | ||
CpeEcosystemCache.setCache(map); | ||
boolean expResult = true; | ||
boolean result = CpeEcosystemCache.isEmpty(); | ||
assertEquals(expResult, result); | ||
map.put(new Pair<String, String>("apache", "zookeeper"), "MULTPILE"); | ||
expResult = false; | ||
result = CpeEcosystemCache.isEmpty(); | ||
assertEquals(expResult, result); | ||
} | ||
|
||
} |