1 /*
   2  * Copyright (c) 2014, 2019, 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 import jdk.test.lib.Utils;
  26 import jdk.test.lib.BuildHelper;
  27 import jdk.test.lib.JDKToolFinder;
  28 import jdk.test.lib.Platform;
  29 import jdk.test.lib.cds.CDSOptions;
  30 import jdk.test.lib.cds.CDSTestUtils;
  31 import jdk.test.lib.cds.CDSTestUtils.Result;
  32 import jdk.test.lib.process.ProcessTools;
  33 import jdk.test.lib.process.OutputAnalyzer;
  34 import java.io.File;
  35 import java.text.SimpleDateFormat;
  36 import java.util.Arrays;
  37 import java.util.ArrayList;
  38 import java.util.Date;
  39 
  40 /**
  41  * This is a test utility class for common AppCDS test functionality.
  42  *
  43  * Various methods use (String ...) for passing VM options. Note that the order
  44  * of the VM options are important in certain cases. Many methods take arguments like
  45  *
  46  *    (String prefix[], String suffix[], String... opts)
  47  *
  48  * Note that the order of the VM options is:
  49  *
  50  *    prefix + opts + suffix
  51  */
  52 public class TestCommon extends CDSTestUtils {
  53     private static final String JSA_FILE_PREFIX = System.getProperty("user.dir") +
  54         File.separator + "appcds-";
  55 
  56     private static final SimpleDateFormat timeStampFormat =
  57         new SimpleDateFormat("HH'h'mm'm'ss's'SSS");
  58 
  59     private static final String timeoutFactor =
  60         System.getProperty("test.timeout.factor", "1.0");
  61 
  62     private static String currentArchiveName;
  63 
  64     // Call this method to start new archive with new unique name
  65     public static void startNewArchiveName() {
  66         deletePriorArchives();
  67         currentArchiveName = JSA_FILE_PREFIX +
  68             timeStampFormat.format(new Date()) + ".jsa";
  69     }
  70 
  71     // Call this method to get current archive name
  72     public static String getCurrentArchiveName() {
  73         return currentArchiveName;
  74     }
  75 
  76     // Attempt to clean old archives to preserve space
  77     // Archives are large artifacts (20Mb or more), and much larger than
  78     // most other artifacts created in jtreg testing.
  79     // Therefore it is a good idea to clean the old archives when they are not needed.
  80     // In most cases the deletion attempt will succeed; on rare occasion the
  81     // delete operation will fail since the system or VM process still holds a handle
  82     // to the file; in such cases the File.delete() operation will silently fail, w/o
  83     // throwing an exception, thus allowing testing to continue.
  84     public static void deletePriorArchives() {
  85         File dir = new File(System.getProperty("user.dir"));
  86         String files[] = dir.list();
  87         for (String name : files) {
  88             if (name.startsWith("appcds-") && name.endsWith(".jsa")) {
  89                 if (!(new File(dir, name)).delete())
  90                     System.out.println("deletePriorArchives(): delete failed for file " + name);
  91             }
  92         }
  93     }
  94 
  95 
  96     // Create AppCDS archive using most common args - convenience method
  97     // Legacy name preserved for compatibility
  98     public static OutputAnalyzer dump(String appJar, String classList[],
  99                                                String... suffix) throws Exception {
 100         return createArchive(appJar, classList, suffix);
 101     }
 102 
 103 
 104     // Create AppCDS archive using most common args - convenience method
 105     public static OutputAnalyzer createArchive(String appJar, String classList[],
 106                                                String... suffix) throws Exception {
 107         AppCDSOptions opts = (new AppCDSOptions()).setAppJar(appJar);
 108         opts.setClassList(classList);
 109         opts.addSuffix(suffix);
 110         return createArchive(opts);
 111     }
 112 
 113     // Create AppCDS archive using appcds options
 114     public static OutputAnalyzer createArchive(AppCDSOptions opts)
 115         throws Exception {
 116 
 117         ArrayList<String> cmd = new ArrayList<String>();
 118         startNewArchiveName();
 119 
 120         for (String p : opts.prefix) cmd.add(p);
 121 
 122         if (opts.appJar != null) {
 123             cmd.add("-cp");
 124             cmd.add(opts.appJar);
 125         } else {
 126             cmd.add("-Djava.class.path=");
 127         }
 128 
 129         cmd.add("-Xshare:dump");
 130 
 131         if (opts.archiveName == null)
 132             opts.archiveName = getCurrentArchiveName();
 133 
 134         cmd.add("-XX:SharedArchiveFile=" + opts.archiveName);
 135 
 136         if (opts.classList != null) {
 137             File classListFile = makeClassList(opts.classList);
 138             cmd.add("-XX:ExtraSharedClassListFile=" + classListFile.getPath());
 139         }
 140 
 141         for (String s : opts.suffix) cmd.add(s);
 142 
 143         String[] cmdLine = cmd.toArray(new String[cmd.size()]);
 144         ProcessBuilder pb = ProcessTools.createJavaProcessBuilder(true, cmdLine);
 145         return executeAndLog(pb, "dump");
 146     }
 147 
 148     // This allows you to run the AppCDS tests with JFR enabled at runtime (though not at
 149     // dump time, as that's uncommon for typical AppCDS users).
 150     //
 151     // To run in this special mode, specify the following in your jtreg command-line
 152     //    -vmoptions:-Dtest.cds.run.with.jfr=true
 153     //
 154     // Some AppCDS tests are not compatible with this mode. See the group
 155     // hotspot_appcds_with_jfr in ../../TEST.ROOT for details.
 156     private static final boolean RUN_WITH_JFR = Boolean.getBoolean("test.cds.run.with.jfr");
 157 
 158     // Execute JVM using AppCDS archive with specified AppCDSOptions
 159     public static OutputAnalyzer runWithArchive(AppCDSOptions opts)
 160         throws Exception {
 161 
 162         ArrayList<String> cmd = new ArrayList<String>();
 163 
 164         for (String p : opts.prefix) cmd.add(p);
 165 
 166         cmd.add("-Xshare:" + opts.xShareMode);
 167         cmd.add("-showversion");
 168         cmd.add("-XX:SharedArchiveFile=" + getCurrentArchiveName());
 169         cmd.add("-Dtest.timeout.factor=" + timeoutFactor);
 170 
 171         if (opts.appJar != null) {
 172             cmd.add("-cp");
 173             cmd.add(opts.appJar);
 174         }
 175 
 176         for (String s : opts.suffix) cmd.add(s);
 177 
 178         if (RUN_WITH_JFR) {
 179             boolean usesJFR = false;
 180             for (String s : cmd) {
 181                 if (s.startsWith("-XX:StartFlightRecording=") || s.startsWith("-XX:FlightRecorderOptions")) {
 182                     System.out.println("JFR option might have been specified. Don't interfere: " + s);
 183                     usesJFR = true;
 184                     break;
 185                 }
 186             }
 187             if (!usesJFR) {
 188                 System.out.println("JFR option not specified. Enabling JFR ...");
 189                 cmd.add(0, "-XX:StartFlightRecording=dumponexit=true");
 190                 System.out.println(cmd);
 191             }
 192         }
 193 
 194         String[] cmdLine = cmd.toArray(new String[cmd.size()]);
 195         ProcessBuilder pb = ProcessTools.createJavaProcessBuilder(true, cmdLine);
 196         return executeAndLog(pb, "exec");
 197     }
 198 
 199 
 200     public static OutputAnalyzer execCommon(String... suffix) throws Exception {
 201         AppCDSOptions opts = (new AppCDSOptions());
 202         opts.addSuffix(suffix);
 203         return runWithArchive(opts);
 204     }
 205 
 206     // This is the new API for running a Java process with CDS enabled.
 207     // See comments in the CDSTestUtils.Result class for how to use this method.
 208     public static Result run(String... suffix) throws Exception {
 209         AppCDSOptions opts = (new AppCDSOptions());
 210         opts.addSuffix(suffix);
 211         return new Result(opts, runWithArchive(opts));
 212     }
 213 
 214     public static OutputAnalyzer exec(String appJar, String... suffix) throws Exception {
 215         AppCDSOptions opts = (new AppCDSOptions()).setAppJar(appJar);
 216         opts.addSuffix(suffix);
 217         return runWithArchive(opts);
 218     }
 219 
 220     public static Result runWithModules(String prefix[], String upgrademodulepath, String modulepath,
 221                                             String mid, String... testClassArgs) throws Exception {
 222         AppCDSOptions opts = makeModuleOptions(prefix, upgrademodulepath, modulepath,
 223                                                mid, testClassArgs);
 224         return new Result(opts, runWithArchive(opts));
 225     }
 226 
 227     public static OutputAnalyzer execAuto(String... suffix) throws Exception {
 228         AppCDSOptions opts = (new AppCDSOptions());
 229         opts.addSuffix(suffix).setXShareMode("auto");
 230         return runWithArchive(opts);
 231     }
 232 
 233     public static OutputAnalyzer execOff(String... suffix) throws Exception {
 234         AppCDSOptions opts = (new AppCDSOptions());
 235         opts.addSuffix(suffix).setXShareMode("off");
 236         return runWithArchive(opts);
 237     }
 238 
 239 
 240     private static AppCDSOptions makeModuleOptions(String prefix[], String upgrademodulepath, String modulepath,
 241                                             String mid, String testClassArgs[]) {
 242         AppCDSOptions opts = (new AppCDSOptions());
 243 
 244         opts.addPrefix(prefix);
 245         if (upgrademodulepath == null) {
 246             opts.addSuffix("-p", modulepath, "-m", mid);
 247         } else {
 248             opts.addSuffix("--upgrade-module-path", upgrademodulepath,
 249                            "-p", modulepath, "-m", mid);
 250         }
 251         opts.addSuffix(testClassArgs);
 252         return opts;
 253     }
 254 
 255     public static OutputAnalyzer execModule(String prefix[], String upgrademodulepath, String modulepath,
 256                                             String mid, String... testClassArgs)
 257         throws Exception {
 258         AppCDSOptions opts = makeModuleOptions(prefix, upgrademodulepath, modulepath,
 259                                                mid, testClassArgs);
 260         return runWithArchive(opts);
 261     }
 262 
 263 
 264     // A common operation: dump, then check results
 265     public static OutputAnalyzer testDump(String appJar, String classList[],
 266                                           String... suffix) throws Exception {
 267         OutputAnalyzer output = dump(appJar, classList, suffix);
 268         output.shouldContain("Loading classes to share");
 269         output.shouldHaveExitValue(0);
 270         return output;
 271     }
 272 
 273 
 274     /**
 275      * Simple test -- dump and execute appJar with the given classList in classlist.
 276      */
 277     public static OutputAnalyzer test(String appJar, String classList[], String... args)
 278         throws Exception {
 279         testDump(appJar, classList);
 280 
 281         OutputAnalyzer output = exec(appJar, args);
 282         return checkExec(output);
 283     }
 284 
 285 
 286     public static OutputAnalyzer checkExecReturn(OutputAnalyzer output, int ret,
 287                            boolean checkContain, String... matches) throws Exception {
 288         try {
 289             for (String s : matches) {
 290                 if (checkContain) {
 291                     output.shouldContain(s);
 292                 } else {
 293                     output.shouldNotContain(s);
 294                 }
 295             }
 296             output.shouldHaveExitValue(ret);
 297         } catch (Exception e) {
 298             checkCommonExecExceptions(output, e);
 299         }
 300 
 301         return output;
 302     }
 303 
 304 
 305     // Convenience concatenation utils
 306     public static String[] list(String ...args) {
 307         return args;
 308     }
 309 
 310 
 311     public static String[] list(String arg, int count) {
 312         ArrayList<String> stringList = new ArrayList<String>();
 313         for (int i = 0; i < count; i++) {
 314             stringList.add(arg);
 315         }
 316 
 317         String outputArray[] = stringList.toArray(new String[stringList.size()]);
 318         return outputArray;
 319     }
 320 
 321 
 322     public static String[] concat(String... args) {
 323         return list(args);
 324     }
 325 
 326 
 327     public static String[] concat(String prefix[], String... extra) {
 328         ArrayList<String> list = new ArrayList<String>();
 329         for (String s : prefix) {
 330             list.add(s);
 331         }
 332         for (String s : extra) {
 333             list.add(s);
 334         }
 335 
 336         return list.toArray(new String[list.size()]);
 337     }
 338 
 339 
 340     // ===================== Concatenate paths
 341     public static String concatPaths(String... paths) {
 342         String prefix = "";
 343         String s = "";
 344         for (String p : paths) {
 345             s += prefix;
 346             s += p;
 347             prefix = File.pathSeparator;
 348         }
 349         return s;
 350     }
 351 
 352 
 353     public static String getTestJar(String jar) {
 354         File jarFile = CDSTestUtils.getTestArtifact(jar, true);
 355         if (!jarFile.isFile()) {
 356             throw new RuntimeException("Not a regular file: " + jarFile.getPath());
 357         }
 358         return jarFile.getPath();
 359     }
 360 
 361 
 362     public static String getTestDir(String d) {
 363         File dirFile = CDSTestUtils.getTestArtifact(d, true);
 364         if (!dirFile.isDirectory()) {
 365             throw new RuntimeException("Not a directory: " + dirFile.getPath());
 366         }
 367         return dirFile.getPath();
 368     }
 369 
 370     public static boolean checkOutputStrings(String outputString1,
 371                                              String outputString2,
 372                                              String split_regex) {
 373         String[] sa1 = outputString1.split(split_regex);
 374         String[] sa2 = outputString2.split(split_regex);
 375         Arrays.sort(sa1);
 376         Arrays.sort(sa2);
 377 
 378         int i = 0;
 379         for (String s : sa1) {
 380             if (!s.equals(sa2[i])) {
 381                 throw new RuntimeException(s + " is different from " + sa2[i]);
 382             }
 383             i ++;
 384         }
 385         return true;
 386     }
 387 }