Coverage Report - org.owasp.dependencycheck.org.apache.tools.ant.taskdefs.condition.Os
 
Classes in this File Line Coverage Branch Coverage Complexity
Os
60%
40/66
38%
39/102
4.083
 
 1  
 /*
 2  
  *  Licensed to the Apache Software Foundation (ASF) under one or more
 3  
  *  contributor license agreements.  See the NOTICE file distributed with
 4  
  *  this work for additional information regarding copyright ownership.
 5  
  *  The ASF licenses this file to You under the Apache License, Version 2.0
 6  
  *  (the "License"); you may not use this file except in compliance with
 7  
  *  the License.  You may obtain a copy of the License at
 8  
  *
 9  
  *      http://www.apache.org/licenses/LICENSE-2.0
 10  
  *
 11  
  *  Unless required by applicable law or agreed to in writing, software
 12  
  *  distributed under the License is distributed on an "AS IS" BASIS,
 13  
  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 14  
  *  See the License for the specific language governing permissions and
 15  
  *  limitations under the License.
 16  
  *
 17  
  */
 18  
 
 19  
 package org.owasp.dependencycheck.org.apache.tools.ant.taskdefs.condition;
 20  
 
 21  
 import java.util.Locale;
 22  
 
 23  
 import org.owasp.dependencycheck.org.apache.tools.ant.BuildException;
 24  
 
 25  
 /**
 26  
  * Condition that tests the OS type.
 27  
  *
 28  
  * @since Ant 1.4
 29  
  */
 30  
 public class Os implements Condition {
 31  1
     private static final String OS_NAME =
 32  
         System.getProperty("os.name").toLowerCase(Locale.ENGLISH);
 33  1
     private static final String OS_ARCH =
 34  
         System.getProperty("os.arch").toLowerCase(Locale.ENGLISH);
 35  1
     private static final String OS_VERSION =
 36  
         System.getProperty("os.version").toLowerCase(Locale.ENGLISH);
 37  1
     private static final String PATH_SEP =
 38  
         System.getProperty("path.separator");
 39  
 
 40  
     /**
 41  
      * OS family to look for
 42  
      */
 43  
     private String family;
 44  
     /**
 45  
      * Name of OS
 46  
      */
 47  
     private String name;
 48  
     /**
 49  
      * version of OS
 50  
      */
 51  
     private String version;
 52  
     /**
 53  
      * OS architecture
 54  
      */
 55  
     private String arch;
 56  
     /**
 57  
      * OS family that can be tested for. {@value}
 58  
      */
 59  
     public static final String FAMILY_WINDOWS = "windows";
 60  
     /**
 61  
      * OS family that can be tested for. {@value}
 62  
      */
 63  
     public static final String FAMILY_9X = "win9x";
 64  
     /**
 65  
      * OS family that can be tested for. {@value}
 66  
      */
 67  
     public static final String FAMILY_NT = "winnt";
 68  
     /**
 69  
      * OS family that can be tested for. {@value}
 70  
      */
 71  
     public static final String FAMILY_OS2 = "os/2";
 72  
     /**
 73  
      * OS family that can be tested for. {@value}
 74  
      */
 75  
     public static final String FAMILY_NETWARE = "netware";
 76  
     /**
 77  
      * OS family that can be tested for. {@value}
 78  
      */
 79  
     public static final String FAMILY_DOS = "dos";
 80  
     /**
 81  
      * OS family that can be tested for. {@value}
 82  
      */
 83  
     public static final String FAMILY_MAC = "mac";
 84  
     /**
 85  
      * OS family that can be tested for. {@value}
 86  
      */
 87  
     public static final String FAMILY_TANDEM = "tandem";
 88  
     /**
 89  
      * OS family that can be tested for. {@value}
 90  
      */
 91  
     public static final String FAMILY_UNIX = "unix";
 92  
     /**
 93  
      * OS family that can be tested for. {@value}
 94  
      */
 95  
     public static final String FAMILY_VMS = "openvms";
 96  
     /**
 97  
      * OS family that can be tested for. {@value}
 98  
      */
 99  
     public static final String FAMILY_ZOS = "z/os";
 100  
     /** OS family that can be tested for. {@value} */
 101  
     public static final String FAMILY_OS400 = "os/400";
 102  
 
 103  
     /**
 104  
      * OpenJDK is reported to call MacOS X "Darwin"
 105  
      * @see https://issues.apache.org/bugzilla/show_bug.cgi?id=44889
 106  
      * @see https://issues.apache.org/jira/browse/HADOOP-3318
 107  
      */
 108  
     private static final String DARWIN = "darwin";
 109  
 
 110  
     /**
 111  
      * Default constructor
 112  
      *
 113  
      */
 114  
     public Os() {
 115  
         //default
 116  
     }
 117  
 
 118  
     /**
 119  
      * Constructor that sets the family attribute
 120  
      * @param family a String value
 121  
      */
 122  0
     public Os(String family) {
 123  0
         setFamily(family);
 124  0
     }
 125  
 
 126  
     /**
 127  
      * Sets the desired OS family type
 128  
      *
 129  
      * @param f      The OS family type desired<br>
 130  
      *               Possible values:<br>
 131  
      *               <ul>
 132  
      *               <li>dos</li>
 133  
      *               <li>mac</li>
 134  
      *               <li>netware</li>
 135  
      *               <li>os/2</li>
 136  
      *               <li>tandem</li>
 137  
      *               <li>unix</li>
 138  
      *               <li>windows</li>
 139  
      *               <li>win9x</li>
 140  
      *               <li>z/os</li>
 141  
      *               <li>os/400</li>
 142  
      *               </ul>
 143  
      */
 144  
     public void setFamily(String f) {
 145  0
         family = f.toLowerCase(Locale.ENGLISH);
 146  0
     }
 147  
 
 148  
     /**
 149  
      * Sets the desired OS name
 150  
      *
 151  
      * @param name   The OS name
 152  
      */
 153  
     public void setName(String name) {
 154  0
         this.name = name.toLowerCase(Locale.ENGLISH);
 155  0
     }
 156  
 
 157  
     /**
 158  
      * Sets the desired OS architecture
 159  
      *
 160  
      * @param arch   The OS architecture
 161  
      */
 162  
     public void setArch(String arch) {
 163  0
         this.arch = arch.toLowerCase(Locale.ENGLISH);
 164  0
     }
 165  
 
 166  
     /**
 167  
      * Sets the desired OS version
 168  
      *
 169  
      * @param version   The OS version
 170  
      */
 171  
     public void setVersion(String version) {
 172  0
         this.version = version.toLowerCase(Locale.ENGLISH);
 173  0
     }
 174  
 
 175  
     /**
 176  
      * Determines if the OS on which Ant is executing matches the type of
 177  
      * that set in setFamily.
 178  
      * @return true if the os matches.
 179  
      * @throws BuildException if there is an error.
 180  
      * @see Os#setFamily(String)
 181  
      */
 182  
     public boolean eval() throws BuildException {
 183  0
         return isOs(family, name, arch, version);
 184  
     }
 185  
 
 186  
     /**
 187  
      * Determines if the OS on which Ant is executing matches the
 188  
      * given OS family.
 189  
      * @param family the family to check for
 190  
      * @return true if the OS matches
 191  
      * @since 1.5
 192  
      */
 193  
     public static boolean isFamily(String family) {
 194  6
         return isOs(family, null, null, null);
 195  
     }
 196  
 
 197  
     /**
 198  
      * Determines if the OS on which Ant is executing matches the
 199  
      * given OS name.
 200  
      *
 201  
      * @param name the OS name to check for
 202  
      * @return true if the OS matches
 203  
      * @since 1.7
 204  
      */
 205  
     public static boolean isName(String name) {
 206  0
         return isOs(null, name, null, null);
 207  
     }
 208  
 
 209  
     /**
 210  
      * Determines if the OS on which Ant is executing matches the
 211  
      * given OS architecture.
 212  
      *
 213  
      * @param arch the OS architecture to check for
 214  
      * @return true if the OS matches
 215  
      * @since 1.7
 216  
      */
 217  
     public static boolean isArch(String arch) {
 218  0
         return isOs(null, null, arch, null);
 219  
     }
 220  
 
 221  
     /**
 222  
      * Determines if the OS on which Ant is executing matches the
 223  
      * given OS version.
 224  
      *
 225  
      * @param version the OS version to check for
 226  
      * @return true if the OS matches
 227  
      * @since 1.7
 228  
      */
 229  
     public static boolean isVersion(String version) {
 230  0
         return isOs(null, null, null, version);
 231  
     }
 232  
 
 233  
     /**
 234  
      * Determines if the OS on which Ant is executing matches the
 235  
      * given OS family, name, architecture and version
 236  
      *
 237  
      * @param family   The OS family
 238  
      * @param name   The OS name
 239  
      * @param arch   The OS architecture
 240  
      * @param version   The OS version
 241  
      * @return true if the OS matches
 242  
      * @since 1.7
 243  
      */
 244  
     public static boolean isOs(String family, String name, String arch,
 245  
                                String version) {
 246  6
         boolean retValue = false;
 247  
 
 248  6
         if (family != null || name != null || arch != null
 249  
             || version != null) {
 250  
 
 251  6
             boolean isFamily = true;
 252  6
             boolean isName = true;
 253  6
             boolean isArch = true;
 254  6
             boolean isVersion = true;
 255  
 
 256  6
             if (family != null) {
 257  
 
 258  
                 //windows probing logic relies on the word 'windows' in
 259  
                 //the OS
 260  6
                 boolean isWindows = OS_NAME.indexOf(FAMILY_WINDOWS) > -1;
 261  6
                 boolean is9x = false;
 262  6
                 boolean isNT = false;
 263  6
                 if (isWindows) {
 264  
                     //there are only four 9x platforms that we look for
 265  6
                     is9x = (OS_NAME.indexOf("95") >= 0
 266  
                             || OS_NAME.indexOf("98") >= 0
 267  
                             || OS_NAME.indexOf("me") >= 0
 268  
                             //wince isn't really 9x, but crippled enough to
 269  
                             //be a muchness. Ant doesnt run on CE, anyway.
 270  
                             || OS_NAME.indexOf("ce") >= 0);
 271  6
                     isNT = !is9x;
 272  
                 }
 273  6
                 if (family.equals(FAMILY_WINDOWS)) {
 274  1
                     isFamily = isWindows;
 275  5
                 } else if (family.equals(FAMILY_9X)) {
 276  1
                     isFamily = isWindows && is9x;
 277  4
                 } else if (family.equals(FAMILY_NT)) {
 278  0
                     isFamily = isWindows && isNT;
 279  4
                 } else if (family.equals(FAMILY_OS2)) {
 280  0
                     isFamily = OS_NAME.indexOf(FAMILY_OS2) > -1;
 281  4
                 } else if (family.equals(FAMILY_NETWARE)) {
 282  2
                     isFamily = OS_NAME.indexOf(FAMILY_NETWARE) > -1;
 283  2
                 } else if (family.equals(FAMILY_DOS)) {
 284  1
                     isFamily = PATH_SEP.equals(";") && !isFamily(FAMILY_NETWARE);
 285  1
                 } else if (family.equals(FAMILY_MAC)) {
 286  0
                     isFamily = OS_NAME.indexOf(FAMILY_MAC) > -1
 287  
                         || OS_NAME.indexOf(DARWIN) > -1;
 288  1
                 } else if (family.equals(FAMILY_TANDEM)) {
 289  0
                     isFamily = OS_NAME.indexOf("nonstop_kernel") > -1;
 290  1
                 } else if (family.equals(FAMILY_UNIX)) {
 291  0
                     isFamily = PATH_SEP.equals(":")
 292  
                         && !isFamily(FAMILY_VMS)
 293  
                         && (!isFamily(FAMILY_MAC) || OS_NAME.endsWith("x")
 294  
                             || OS_NAME.indexOf(DARWIN) > -1);
 295  1
                 } else if (family.equals(FAMILY_ZOS)) {
 296  0
                     isFamily = OS_NAME.indexOf(FAMILY_ZOS) > -1
 297  
                         || OS_NAME.indexOf("os/390") > -1;
 298  1
                 } else if (family.equals(FAMILY_OS400)) {
 299  0
                     isFamily = OS_NAME.indexOf(FAMILY_OS400) > -1;
 300  1
                 } else if (family.equals(FAMILY_VMS)) {
 301  1
                     isFamily = OS_NAME.indexOf(FAMILY_VMS) > -1;
 302  
                 } else {
 303  0
                     throw new BuildException(
 304  
                         "Don\'t know how to detect os family \""
 305  
                         + family + "\"");
 306  
                 }
 307  
             }
 308  6
             if (name != null) {
 309  0
                 isName = name.equals(OS_NAME);
 310  
             }
 311  6
             if (arch != null) {
 312  0
                 isArch = arch.equals(OS_ARCH);
 313  
             }
 314  6
             if (version != null) {
 315  0
                 isVersion = version.equals(OS_VERSION);
 316  
             }
 317  6
             retValue = isFamily && isName && isArch && isVersion;
 318  
         }
 319  6
         return retValue;
 320  
     }
 321  
 }