Coverage Report - org.owasp.dependencycheck.analyzer.FileNameAnalyzer
 
Classes in this File Line Coverage Branch Coverage Complexity
FileNameAnalyzer
95%
23/24
70%
7/10
2
 
 1  
 /*
 2  
  * This file is part of dependency-check-core.
 3  
  *
 4  
  * Licensed under the Apache License, Version 2.0 (the "License");
 5  
  * you may not use this file except in compliance with the License.
 6  
  * You may obtain a copy of the License at
 7  
  *
 8  
  *     http://www.apache.org/licenses/LICENSE-2.0
 9  
  *
 10  
  * Unless required by applicable law or agreed to in writing, software
 11  
  * distributed under the License is distributed on an "AS IS" BASIS,
 12  
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 13  
  * See the License for the specific language governing permissions and
 14  
  * limitations under the License.
 15  
  *
 16  
  * Copyright (c) 2012 Jeremy Long. All Rights Reserved.
 17  
  */
 18  
 package org.owasp.dependencycheck.analyzer;
 19  
 
 20  
 import java.io.File;
 21  
 import java.util.Set;
 22  
 import org.owasp.dependencycheck.Engine;
 23  
 import org.owasp.dependencycheck.dependency.Confidence;
 24  
 import org.owasp.dependencycheck.dependency.Dependency;
 25  
 import org.owasp.dependencycheck.utils.DependencyVersion;
 26  
 import org.owasp.dependencycheck.utils.DependencyVersionUtil;
 27  
 
 28  
 /**
 29  
  *
 30  
  * Takes a dependency and analyzes the filename and determines the hashes.
 31  
  *
 32  
  * @author Jeremy Long <jeremy.long@owasp.org>
 33  
  */
 34  15
 public class FileNameAnalyzer extends AbstractAnalyzer implements Analyzer {
 35  
 
 36  
     //<editor-fold defaultstate="collapsed" desc="All standard implmentation details of Analyzer">
 37  
     /**
 38  
      * The name of the analyzer.
 39  
      */
 40  
     private static final String ANALYZER_NAME = "File Name Analyzer";
 41  
     /**
 42  
      * The phase that this analyzer is intended to run in.
 43  
      */
 44  1
     private static final AnalysisPhase ANALYSIS_PHASE = AnalysisPhase.INFORMATION_COLLECTION;
 45  
     /**
 46  
      * The set of file extensions supported by this analyzer.
 47  
      */
 48  1
     private static final Set<String> EXTENSIONS = null;
 49  
 
 50  
     /**
 51  
      * Returns a list of file EXTENSIONS supported by this analyzer.
 52  
      *
 53  
      * @return a list of file EXTENSIONS supported by this analyzer.
 54  
      */
 55  
     public Set<String> getSupportedExtensions() {
 56  149
         return EXTENSIONS;
 57  
     }
 58  
 
 59  
     /**
 60  
      * Returns the name of the analyzer.
 61  
      *
 62  
      * @return the name of the analyzer.
 63  
      */
 64  
     public String getName() {
 65  10
         return ANALYZER_NAME;
 66  
     }
 67  
 
 68  
     /**
 69  
      * Returns whether or not this analyzer can process the given extension.
 70  
      *
 71  
      * @param extension the file extension to test for support.
 72  
      * @return whether or not the specified file extension is supported by this analyzer.
 73  
      */
 74  
     public boolean supportsExtension(String extension) {
 75  10
         return true;
 76  
     }
 77  
 
 78  
     /**
 79  
      * Returns the phase that the analyzer is intended to run in.
 80  
      *
 81  
      * @return the phase that the analyzer is intended to run in.
 82  
      */
 83  
     public AnalysisPhase getAnalysisPhase() {
 84  7
         return ANALYSIS_PHASE;
 85  
     }
 86  
     //</editor-fold>
 87  
 
 88  
     /**
 89  
      * Collects information about the file name.
 90  
      *
 91  
      * @param dependency the dependency to analyze.
 92  
      * @param engine the engine that is scanning the dependencies
 93  
      * @throws AnalysisException is thrown if there is an error reading the JAR file.
 94  
      */
 95  
     @Override
 96  
     public void analyze(Dependency dependency, Engine engine) throws AnalysisException {
 97  
 
 98  
         //strip any path information that may get added by ArchiveAnalyzer, etc.
 99  18
         final File f = new File(dependency.getFileName());
 100  18
         String fileName = f.getName();
 101  
 
 102  
         //remove file extension
 103  18
         final int pos = fileName.lastIndexOf(".");
 104  18
         if (pos > 0) {
 105  18
             fileName = fileName.substring(0, pos);
 106  
         }
 107  
 
 108  
         //add version evidence
 109  18
         final DependencyVersion version = DependencyVersionUtil.parseVersion(fileName);
 110  18
         if (version != null) {
 111  
             // If the version number is just a number like 2 or 23, reduce the confidence
 112  
             // a shade. This should hopefully correct for cases like log4j.jar or
 113  
             // struts2-core.jar
 114  10
             if (version.getVersionParts() == null || version.getVersionParts().size() < 2) {
 115  0
                 dependency.getVersionEvidence().addEvidence("file", "name",
 116  
                         version.toString(), Confidence.MEDIUM);
 117  
             } else {
 118  10
                 dependency.getVersionEvidence().addEvidence("file", "name",
 119  
                         version.toString(), Confidence.HIGHEST);
 120  
             }
 121  10
             dependency.getVersionEvidence().addEvidence("file", "name",
 122  
                     fileName, Confidence.MEDIUM);
 123  
         }
 124  
 
 125  
         //add as vendor and product evidence
 126  18
         if (fileName.contains("-")) {
 127  10
             dependency.getProductEvidence().addEvidence("file", "name",
 128  
                     fileName, Confidence.HIGHEST);
 129  10
             dependency.getVendorEvidence().addEvidence("file", "name",
 130  
                     fileName, Confidence.HIGHEST);
 131  
         } else {
 132  8
             dependency.getProductEvidence().addEvidence("file", "name",
 133  
                     fileName, Confidence.HIGH);
 134  8
             dependency.getVendorEvidence().addEvidence("file", "name",
 135  
                     fileName, Confidence.HIGH);
 136  
         }
 137  18
     }
 138  
 }