1 /*
   2  * Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 package optionsvalidation;
  25 
  26 import java.math.BigInteger;
  27 import java.util.ArrayList;
  28 import java.util.List;
  29 import jdk.test.lib.Platform;
  30 
  31 public class IntJVMOption extends JVMOption {
  32 
  33     private static final BigInteger MIN_LONG;
  34     private static final BigInteger MAX_LONG;
  35     private static final BigInteger MAX_UNSIGNED_LONG;
  36     private static final BigInteger MAX_UNSIGNED_LONG_64;
  37     private static final BigInteger MINUS_ONE = new BigInteger("-1");
  38     private static final BigInteger MAX_4_BYTE_INT_PLUS_ONE = new BigInteger("2147483648");
  39     private static final BigInteger MAX_4_BYTE_UNSIGNED_INT_PLUS_ONE = new BigInteger("4294967296");
  40 
  41     /**
  42      * Mininum option value
  43      */
  44     private BigInteger min;
  45     /**
  46      * Maximum option value
  47      */
  48     private BigInteger max;
  49 
  50     /**
  51      * Option type: intx, uintx, size_t or uint64_t
  52      */
  53     private String type;
  54 
  55     /**
  56      * Is this value signed or unsigned
  57      */
  58     private boolean unsigned;
  59 
  60     /**
  61      * Is this value 64 bit unsigned
  62      */
  63     private boolean uint64 = false;
  64 
  65     static {
  66         if (Platform.is32bit()) {
  67             MIN_LONG = new BigInteger(String.valueOf(Integer.MIN_VALUE));
  68             MAX_LONG = new BigInteger(String.valueOf(Integer.MAX_VALUE));
  69             MAX_UNSIGNED_LONG = MAX_LONG.multiply(new BigInteger("2")).add(BigInteger.ONE);
  70         } else {
  71             MIN_LONG = new BigInteger(String.valueOf(Long.MIN_VALUE));
  72             MAX_LONG = new BigInteger(String.valueOf(Long.MAX_VALUE));
  73             MAX_UNSIGNED_LONG = MAX_LONG.multiply(new BigInteger("2")).add(BigInteger.ONE);
  74         }
  75 
  76         MAX_UNSIGNED_LONG_64 = (new BigInteger(String.valueOf(Long.MAX_VALUE)))
  77                 .multiply(new BigInteger("2")).add(BigInteger.ONE);
  78     }
  79 
  80     private IntJVMOption() {
  81         type = "";
  82     }
  83 
  84     /**
  85      * Initialize new integer option with given type. Type can be: INTX -
  86      * integer signed option UINTX - unsigned integer option UINT64_T - unsigned
  87      * 64 bit integer option
  88      *
  89      * @param name name of the option
  90      * @param type type of the option
  91      */
  92     IntJVMOption(String name, String type) {
  93         this.name = name;
  94         this.type = type;
  95 
  96         switch (type) {
  97             case "uint64_t":
  98                 uint64 = true;
  99             case "uintx":
 100             case "size_t":
 101                 unsigned = true;
 102                 break;
 103         }
 104 
 105         if (unsigned) {
 106             min = BigInteger.ZERO;
 107             max = MAX_UNSIGNED_LONG;
 108         }
 109 
 110         if (uint64) {
 111             max = MAX_UNSIGNED_LONG_64;
 112         }
 113     }
 114 
 115     /**
 116      * Initialize integer option with passed name, min and max values. Min and
 117      * max are string because they can be very big, bigger than long.
 118      *
 119      * @param name name of the option
 120      * @param min minimum value of the option
 121      * @param max maximum value of the option
 122      */
 123     public IntJVMOption(String name, String min, String max) {
 124         this();
 125         this.name = name;
 126         this.min = new BigInteger(min);
 127         this.max = new BigInteger(max);
 128     }
 129 
 130     /**
 131      * Set new minimum option value
 132      *
 133      * @param min new minimum value
 134      */
 135     @Override
 136     void setMin(String min) {
 137         this.min = new BigInteger(min);
 138     }
 139 
 140     /**
 141      * Get string with minimum value of the option
 142      *
 143      * @return string with minimum value of the option
 144      */
 145     @Override
 146     String getMin() {
 147         return min.toString();
 148     }
 149 
 150     /**
 151      * Set new maximum option value
 152      *
 153      * @param max new maximum value
 154      */
 155     @Override
 156     void setMax(String max) {
 157         this.max = new BigInteger(max);
 158     }
 159 
 160     /**
 161      * Get string with maximum value of the option
 162      *
 163      * @return string with maximum value of the option
 164      */
 165     @Override
 166     String getMax() {
 167         return max.toString();
 168     }
 169 
 170     /**
 171      * Return list of strings with valid option values which used for testing
 172      * using jcmd, attach and etc.
 173      *
 174      * @return list of strings which contain valid values for option
 175      */
 176     @Override
 177     protected List<String> getValidValues() {
 178         List<String> validValues = new ArrayList<>();
 179 
 180         validValues.add(min.toString());
 181         validValues.add(max.toString());
 182 
 183         if ((min.compareTo(MINUS_ONE) == -1) && (max.compareTo(MINUS_ONE) == 1)) {
 184             /*
 185              * Add -1 as valid value if min is less than -1 and max is greater than -1
 186              */
 187             validValues.add("-1");
 188         }
 189 
 190         if ((min.compareTo(BigInteger.ZERO) == -1) && (max.compareTo(BigInteger.ZERO) == 1)) {
 191             /*
 192              * Add 0 as valid value if min is less than 0 and max is greater than 0
 193              */
 194             validValues.add("0");
 195         }
 196         if ((min.compareTo(BigInteger.ONE) == -1) && (max.compareTo(BigInteger.ONE) == 1)) {
 197             /*
 198              * Add 1 as valid value if min is less than 1 and max is greater than 1
 199              */
 200             validValues.add("1");
 201         }
 202 
 203         if (max.compareTo(MAX_4_BYTE_INT_PLUS_ONE) == 1) {
 204             /* 
 205              * Check for overflow when flag is assigned to the 
 206              * 4 byte int variable
 207              */
 208             validValues.add(MAX_4_BYTE_INT_PLUS_ONE.toString());
 209         }
 210 
 211         if (max.compareTo(MAX_4_BYTE_UNSIGNED_INT_PLUS_ONE) == 1) {
 212             /* 
 213              * Check for overflow when flag is assigned to the 
 214              * 4 byte unsigned int variable
 215              */
 216             validValues.add(MAX_4_BYTE_UNSIGNED_INT_PLUS_ONE.toString());
 217         }
 218 
 219         return validValues;
 220     }
 221 
 222     /**
 223      * Return list of strings with invalid option values which used for testing
 224      * using jcmd, attach and etc.
 225      *
 226      * @return list of strings which contain invalid values for option
 227      */
 228     @Override
 229     protected List<String> getInvalidValues() {
 230         List<String> invalidValues = new ArrayList<>();
 231 
 232         if (min.compareTo(MIN_LONG) != 0) {
 233             invalidValues.add(min.subtract(BigInteger.ONE).toString());
 234         }
 235 
 236         if (!unsigned && (max.compareTo(MAX_LONG) != 0)) {
 237             invalidValues.add(max.add(BigInteger.ONE).toString());
 238         }
 239 
 240         if (unsigned
 241                 && ((!uint64 && (max.compareTo(MAX_UNSIGNED_LONG) != 0))
 242                 || (uint64 && (max.compareTo(MAX_UNSIGNED_LONG_64) != 0)))) {
 243             invalidValues.add(max.add(BigInteger.ONE).toString());
 244         }
 245 
 246         return invalidValues;
 247     }
 248 
 249     /**
 250      * Return expected error message for option with value "value" when it used
 251      * on command line with passed value
 252      *
 253      * @param value Option value
 254      * @return expected error message
 255      */
 256     @Override
 257     protected String getErrorMessageCommandLine(String value) {
 258         String errorMsg;
 259 
 260         if (withRange) {
 261             /* Option have defined range in VM */
 262             if (unsigned && ((new BigInteger(value)).compareTo(BigInteger.ZERO) < 0)) {
 263                 /*
 264                  * Special case for unsigned options with lower range equal to 0. If 
 265                  * passed value is negative then error will be caught earlier for
 266                  * such options. Thus use different error message.
 267                  */
 268                 errorMsg = String.format("Improperly specified VM option '%s=%s'", name, value);
 269             } else {
 270                 errorMsg = String.format("%s %s=%s is outside the allowed range [ %s ... %s ]",
 271                         type, name, value, min.toString(), max.toString());
 272             }
 273         } else {
 274             errorMsg = "";
 275         }
 276 
 277         return errorMsg;
 278     }
 279 }