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