33import com .redhat .victims .VictimsException ;
44import com .redhat .victims .database .VictimsDB ;
55import com .redhat .victims .database .VictimsDBInterface ;
6+ import java .io .File ;
7+ import java .io .FileInputStream ;
8+ import java .io .FileNotFoundException ;
69import java .io .IOException ;
10+ import java .util .HashSet ;
11+ import java .util .Spliterators ;
712import java .util .logging .Logger ;
13+ import java .util .stream .Collectors ;
14+ import java .util .stream .StreamSupport ;
15+ import org .jboss .windup .qs .victims .ComputeArchivesVictimsHashRules ;
816import org .jboss .windup .util .Logging ;
17+ import org .junit .Assert ;
18+ import org .junit .Ignore ;
919import org .junit .Test ;
1020
1121/**
@@ -18,18 +28,20 @@ public class VictimsLibTest
1828 private static final Logger log = Logging .get (VictimsLibTest .class );
1929
2030
21- // Path to a jar known to contain a vulnerability.
22- private static final String BAD_JAR = "target/testJars/xercesImpl-2.9.1.jar" ;
31+ // Path to a jars known to contain a vulnerability.
32+ private static final String VULNERABLE_JAR1_PATH = "target/testJars/xercesImpl-2.9.1.jar" ;
33+ // Looks like the Xerces vulnerability is not in the Victims database. Adding another one.
34+ private static final String VULNERABLE_JAR2_PATH = "src/test/resources/commons-fileupload-1.0-beta-1.jar" ;
2335
2436 // SHA-512 checksum of xerces:xercesImpl:2.9.1
25- private static final String BAD_JAR_SHA512 = "ec2200e5a5a70f5c64744f6413a546f5e4979b3fb1649b02756ff035d36dde31170eaadc70842230296b60896f04877270c26b40415736299aef44ac16c5811c" ;
37+ private static final String VULNERABLE_JAR1_SHA512 = "ec2200e5a5a70f5c64744f6413a546f5e4979b3fb1649b02756ff035d36dde31170eaadc70842230296b60896f04877270c26b40415736299aef44ac16c5811c" ;
2638
27- // Contained in FILEHASHES table.
28- private static final String BAD_SHA512 = "851eba12748a1aada5829e3a8e2eba05435efaaef9f0e7f68f6246dc1f6407ca56830ef00d587e91c3d889bb70eaf605a305652479ba6986a90b3986f0e74daf" ;
39+ // Contained in FILEHASHES table. Not sure if it is supposed to be found by Victims API.
40+ private static final String SOME_VICTIMS_HASH = "851eba12748a1aada5829e3a8e2eba05435efaaef9f0e7f68f6246dc1f6407ca56830ef00d587e91c3d889bb70eaf605a305652479ba6986a90b3986f0e74daf" ;
2941
3042
3143 @ Test
32- public void testUpdate () throws IOException , VictimsException
44+ public void test01Update () throws IOException , VictimsException
3345 {
3446 try {
3547 VictimsDBInterface db = VictimsDB .db ();
@@ -38,6 +50,7 @@ public void testUpdate() throws IOException, VictimsException
3850 // Update (goes to ~/.victims)
3951 db .synchronize ();
4052 System .out .println (" DB records: " + db .getRecordCount ());
53+ Assert .assertTrue ("DB has some recods after update." , db .getRecordCount () > 0 );
4154 System .out .println ("Database last updated on: " + db .lastUpdated ().toString ());
4255 }
4356 catch (VictimsException ex ){
@@ -49,4 +62,42 @@ public void testUpdate() throws IOException, VictimsException
4962 }
5063 }
5164
65+ @ Test @ Ignore
66+ public void test02IdentifyVulnerableJarHash (){
67+ try
68+ {
69+ VictimsDBInterface db = VictimsDB .db ();
70+ final HashSet <String > vulnerabilities = db .getVulnerabilities (SOME_VICTIMS_HASH );
71+ Assert .assertTrue ("Found some vulnerability for hash " + SOME_VICTIMS_HASH , !vulnerabilities .isEmpty ());
72+ }
73+ catch (VictimsException ex ){
74+ // Prevent failure if offline. Just a warning.
75+ throw new RuntimeException ("Failed when identifying a vulnerable jar" , ex );
76+ }
77+ }
78+
79+ @ Test
80+ public void test03IdentifyVulnerableXercesJarHash (){
81+ try
82+ {
83+ final File vulnerableJar = new File (VULNERABLE_JAR2_PATH );
84+ final String hash = ComputeArchivesVictimsHashRules .computeVictimsHash (new FileInputStream (vulnerableJar ), vulnerableJar .getName ());
85+
86+ VictimsDBInterface db = VictimsDB .db ();
87+ final HashSet <String > vulnerabilities = db .getVulnerabilities (hash );
88+ Assert .assertTrue ("Found some vulnerability for hash " + hash , !vulnerabilities .isEmpty ());
89+ log .info (String .format ("Vulnerabilities found in %s: " , vulnerableJar .getPath ()) + StreamSupport .stream (vulnerabilities .spliterator (), false ).collect (Collectors .joining (", " )));
90+ }
91+ catch (VictimsException ex ){
92+ // Prevent failure if offline. Just a warning.
93+ throw new RuntimeException ("Failed when identifying a vulnerable jar" , ex );
94+ }
95+ catch (FileNotFoundException e ) {
96+ e .printStackTrace ();
97+ }
98+ catch (IOException e ) {
99+ e .printStackTrace ();
100+ }
101+ }
102+
52103}
0 commit comments