1 /*
   2  * Copyright (c) 2012, 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 /**
  25  * @test
  26  * @bug 4894330 4810347 6277269
  27  * @compile -XDignore.symbol.file ChangeDataModel.java
  28  * @run main ChangeDataModel
  29  * @summary Verify -d32 and -d64 options are accepted(rejected) on all platforms
  30  * @author Joseph D. Darcy, ksrini
  31  */
  32 import java.io.File;
  33 import java.util.HashMap;
  34 import java.util.Map;
  35 
  36 public class ChangeDataModel extends TestHelper {
  37     private static final File TestJar      = new File("test" + JAR_FILE_EXT);
  38     private static final String OPT_PREFIX = "ARCH_OPT:";
  39 
  40     public static void main(String... args) throws Exception {
  41         String[] code = {
  42             "   public static void main(String argv[]) {",
  43             "      System.out.println(\"" + OPT_PREFIX + "-d\" + System.getProperty(\"sun.arch.data.model\", \"none\"));",
  44             "   }",
  45         };
  46         createJar(TestJar, code);
  47 
  48         // verify if data model flag for default data model is accepted
  49         if (is32Bit) {
  50             checkAcceptance(javaCmd, "-d32");
  51         } else if (is64Bit) {
  52             checkAcceptance(javaCmd, "-d64");
  53         } else {
  54             throw new Error("unsupported data model");
  55         }
  56 
  57         // test dual mode systems
  58         if (isDualMode) {
  59             // albeit dual mode we may not have the 64 bit components present
  60             if (dualModePresent()) {
  61                 // 32-bit -> 64-bit
  62                 checkExecCount(javaCmd, "-d64");
  63                 // 64-bit -> 32-bit
  64                 checkExecCount(java64Cmd, "-d32");
  65 
  66                 checkAcceptance(javaCmd, "-d64");
  67                 checkAcceptance(java64Cmd, "-d32");
  68             } else {
  69                 System.out.println("Warning: no 64-bit components found;" +
  70                     " only one data model tested.");
  71             }
  72         } else {
  73             // Negative tests: ensure that non-dual mode systems reject the
  74             // complementary (other) data model
  75             if (is32Bit) {
  76                 checkRejection(javaCmd, "-d64");
  77             } else if (is64Bit) {
  78                 checkRejection(javaCmd, "-d32");
  79             } else {
  80                 throw new Error("unsupported data model");
  81             }
  82         }
  83     }
  84 
  85     static void checkExecCount(String cmd, String dmodel) {
  86         Map<String, String> envMap = new HashMap<>();
  87         envMap.put(JLDEBUG_KEY, "true");
  88         TestResult tr = doExec(envMap, javaCmd, "-d64",
  89                 "-jar", TestJar.getAbsolutePath());
  90         int count = 0;
  91         for (String x : tr.testOutput) {
  92             if (x.contains(EXPECTED_MARKER)) {
  93                 count++;
  94                 if (count > 1) {
  95                     System.out.println(tr);
  96                     throw new RuntimeException("Maximum exec count of 1 execeeded");
  97                 }
  98             }
  99         }
 100     }
 101 
 102     static void checkAcceptance(String cmd, String dmodel) {
 103         TestResult tr = doExec(cmd, dmodel, "-jar", TestJar.getAbsolutePath());
 104         if (!tr.contains(OPT_PREFIX + dmodel)) {
 105             System.out.println(tr);
 106             String message = "Data model flag " + dmodel +
 107                     " not accepted or had improper effect.";
 108             throw new RuntimeException(message);
 109         }
 110     }
 111 
 112     static void checkRejection(String cmd, String dmodel) {
 113         TestResult tr = doExec(cmd, dmodel, "-jar", TestJar.getAbsolutePath());
 114         if (tr.contains(OPT_PREFIX + dmodel)) {
 115             System.out.println(tr);
 116             String message = "Data model flag " + dmodel + " was accepted.";
 117             throw new RuntimeException(message);
 118         }
 119     }
 120 }