hotspot/test/runtime/SharedArchiveFile/LimitSharedSizes.java
changeset 34242 e530decd7c63
parent 30604 b8d532cb6420
child 36209 8db2a78cbc29
--- a/hotspot/test/runtime/SharedArchiveFile/LimitSharedSizes.java	Wed Nov 18 22:00:09 2015 +0000
+++ b/hotspot/test/runtime/SharedArchiveFile/LimitSharedSizes.java	Thu Nov 19 16:32:41 2015 -0600
@@ -23,50 +23,72 @@
 
 /* @test LimitSharedSizes
  * @summary Test handling of limits on shared space size
- * @library /testlibrary
+ * @library /testlibrary /runtime/CommandLine/OptionsValidation/common
  * @modules java.base/sun.misc
  *          java.management
  * @run main LimitSharedSizes
  */
 
 import jdk.test.lib.*;
+import optionsvalidation.JVMOptionsUtils;
 
 public class LimitSharedSizes {
+    static enum Result {
+        OUT_OF_RANGE,
+        TOO_SMALL,
+        VALID,
+        VALID_ARCHIVE
+    }
+
     static enum Region {
         RO, RW, MD, MC
     }
 
+    private static final boolean fitsRange(String name, String value) throws RuntimeException {
+        boolean fits = true;
+        try {
+            fits = JVMOptionsUtils.fitsRange(name, value);
+        } catch (Exception e) {
+            throw new RuntimeException(e.getMessage());
+        }
+        return fits;
+    }
+
     private static class SharedSizeTestData {
         public String optionName;
         public String optionValue;
-        public String expectedErrorMsg;
+        public Result optionResult;
 
-        public SharedSizeTestData(Region region, String value, String msg) {
-            optionName = getName(region);
+        public SharedSizeTestData(Region region, String value) {
+            optionName = "-XX:"+getName(region);
             optionValue = value;
-            expectedErrorMsg = msg;
+            if (fitsRange(getName(region), value) == false) {
+                optionResult = Result.OUT_OF_RANGE;
+            } else {
+                optionResult = Result.TOO_SMALL;
+            }
         }
 
-        public SharedSizeTestData(Region region, String msg) {
-            optionName = getName(region);
-            optionValue = getValue(region);
-            expectedErrorMsg = msg;
+        public SharedSizeTestData(Region region, String value, Result result) {
+            optionName = "-XX:"+getName(region);
+            optionValue = value;
+            optionResult = result;
         }
 
         private String getName(Region region) {
             String name;
             switch (region) {
                 case RO:
-                    name = "-XX:SharedReadOnlySize";
+                    name = "SharedReadOnlySize";
                     break;
                 case RW:
-                    name = "-XX:SharedReadWriteSize";
+                    name = "SharedReadWriteSize";
                     break;
                 case MD:
-                    name = "-XX:SharedMiscDataSize";
+                    name = "SharedMiscDataSize";
                     break;
                 case MC:
-                    name = "-XX:SharedMiscCodeSize";
+                    name = "SharedMiscCodeSize";
                     break;
                 default:
                     name = "Unknown";
@@ -75,53 +97,37 @@
             return name;
         }
 
-        private String getValue(Region region) {
-            String value;
-            switch (region) {
-                case RO:
-                    value = Platform.is64bit() ? "9M" : "8M";
-                    break;
-                case RW:
-                    value = Platform.is64bit() ? "12M" : "7M";
-                    break;
-                case MD:
-                    value = Platform.is64bit() ? "4M" : "2M";
-                    break;
-                case MC:
-                    value = "120k";
-                    break;
-                default:
-                    value = "0M";
-                    break;
-            }
-            return value;
+        public Result getResult() {
+            return optionResult;
         }
     }
 
     private static final SharedSizeTestData[] testTable = {
         // Too small of a region size should not cause a vm crash.
-        // It should result in an error message like the following:
+        // It should result in an error message either like the following #1:
         // The shared miscellaneous code space is not large enough
         // to preload requested classes. Use -XX:SharedMiscCodeSize=
         // to increase the initial size of shared miscellaneous code space.
-        new SharedSizeTestData(Region.RO, "4M",   "read only"),
-        new SharedSizeTestData(Region.RW, "4M",   "read write"),
-        new SharedSizeTestData(Region.MD, "50k",  "miscellaneous data"),
-        new SharedSizeTestData(Region.MC, "20k",  "miscellaneous code"),
+        // or #2:
+        // The shared miscellaneous code space is outside the allowed range
+        new SharedSizeTestData(Region.RO, "4M"),
+        new SharedSizeTestData(Region.RW, "4M"),
+        new SharedSizeTestData(Region.MD, "50k"),
+        new SharedSizeTestData(Region.MC, "20k"),
 
-        // these values are larger than default ones, but should
+        // these values are larger than default ones, and should
         // be acceptable and not cause failure
-        new SharedSizeTestData(Region.RO, "20M", null),
-        new SharedSizeTestData(Region.RW, "20M", null),
-        new SharedSizeTestData(Region.MD, "20M", null),
-        new SharedSizeTestData(Region.MC, "20M", null),
+        new SharedSizeTestData(Region.RO, "20M", Result.VALID),
+        new SharedSizeTestData(Region.RW, "20M", Result.VALID),
+        new SharedSizeTestData(Region.MD, "20M", Result.VALID),
+        new SharedSizeTestData(Region.MC, "20M", Result.VALID),
 
         // test with sizes which just meet the minimum required sizes
         // the following tests also attempt to use the shared archive
-        new SharedSizeTestData(Region.RO, "UseArchive"),
-        new SharedSizeTestData(Region.RW, "UseArchive"),
-        new SharedSizeTestData(Region.MD, "UseArchive"),
-        new SharedSizeTestData(Region.MC, "UseArchive")
+        new SharedSizeTestData(Region.RO, Platform.is64bit() ? "9M":"8M", Result.VALID_ARCHIVE),
+        new SharedSizeTestData(Region.RW, Platform.is64bit() ? "12M":"7M", Result.VALID_ARCHIVE),
+        new SharedSizeTestData(Region.MD, Platform.is64bit() ? "4M":"2M", Result.VALID_ARCHIVE),
+        new SharedSizeTestData(Region.MC, "120k", Result.VALID_ARCHIVE),
     };
 
     public static void main(String[] args) throws Exception {
@@ -131,6 +137,7 @@
             counter++;
 
             String option = td.optionName + "=" + td.optionValue;
+            System.out.println("testing option number <" + counter + ">");
             System.out.println("testing option <" + option + ">");
 
             ProcessBuilder pb = ProcessTools.createJavaProcessBuilder(
@@ -141,43 +148,52 @@
 
             OutputAnalyzer output = new OutputAnalyzer(pb.start());
 
-            if (td.expectedErrorMsg != null) {
-                if (!td.expectedErrorMsg.equals("UseArchive")) {
-                    output.shouldContain("The shared " + td.expectedErrorMsg
-                        + " space is not large enough");
+            switch (td.getResult()) {
+                case VALID:
+                case VALID_ARCHIVE:
+                {
+                  output.shouldNotContain("space is not large enough");
+                  output.shouldHaveExitValue(0);
 
-                    output.shouldHaveExitValue(2);
-                } else {
-                    output.shouldNotContain("space is not large enough");
-                    output.shouldHaveExitValue(0);
-
-                    // try to use the archive
-                    pb = ProcessTools.createJavaProcessBuilder(
-                       "-XX:+UnlockDiagnosticVMOptions",
-                       "-XX:SharedArchiveFile=./" + fileName,
-                       "-XX:+PrintSharedArchiveAndExit",
-                       "-version");
+                  if (td.getResult() == Result.VALID_ARCHIVE) {
+                      // try to use the archive
+                      pb = ProcessTools.createJavaProcessBuilder(
+                         "-XX:+UnlockDiagnosticVMOptions",
+                         "-XX:SharedArchiveFile=./" + fileName,
+                         "-XX:+PrintSharedArchiveAndExit",
+                         "-version");
 
-                    try {
-                        output = new OutputAnalyzer(pb.start());
-                        output.shouldContain("archive is valid");
-                    } catch (RuntimeException e) {
-                        // if sharing failed due to ASLR or similar reasons,
-                        // check whether sharing was attempted at all (UseSharedSpaces)
-                        if ((output.getOutput().contains("Unable to use shared archive") ||
-                             output.getOutput().contains("Unable to map ReadOnly shared space at required address.") ||
-                             output.getOutput().contains("Unable to map ReadWrite shared space at required address.") ||
-                             output.getOutput().contains("Unable to reserve shared space at required address")) &&
-                             output.getExitValue() == 1) {
-                             System.out.println("Unable to use shared archive: test not executed; assumed passed");
-                             return;
-                        }
-                    }
-                    output.shouldHaveExitValue(0);
+                      try {
+                          output = new OutputAnalyzer(pb.start());
+                          output.shouldContain("archive is valid");
+                      } catch (RuntimeException e) {
+                          // if sharing failed due to ASLR or similar reasons,
+                          // check whether sharing was attempted at all (UseSharedSpaces)
+                          if ((output.getOutput().contains("Unable to use shared archive") ||
+                               output.getOutput().contains("Unable to map ReadOnly shared space at required address.") ||
+                               output.getOutput().contains("Unable to map ReadWrite shared space at required address.") ||
+                               output.getOutput().contains("Unable to reserve shared space at required address")) &&
+                               output.getExitValue() == 1) {
+                               System.out.println("Unable to use shared archive: test not executed; assumed passed");
+                               return;
+                          }
+                      }
+                      output.shouldHaveExitValue(0);
+                  }
                 }
-            } else {
-                output.shouldNotContain("space is not large enough");
-                output.shouldHaveExitValue(0);
+                break;
+                case TOO_SMALL:
+                {
+                    output.shouldContain("space is not large enough");
+                    output.shouldHaveExitValue(2);
+                }
+                break;
+                case OUT_OF_RANGE:
+                {
+                    output.shouldContain("outside the allowed range");
+                    output.shouldHaveExitValue(1);
+                }
+                break;
             }
         }
     }