1 /*
   2  * Copyright (c) 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 /*
  26  * @test
  27  * @summary Test the handling of long path to the classlist file.
  28  * @requires vm.cds
  29  * @library /test/lib
  30  * @modules java.base/jdk.internal.misc
  31  *          java.management
  32  *          jdk.jartool/sun.tools.jar
  33  * @compile test-classes/Hello.java
  34  * @run driver LongClassListPath
  35  */
  36 
  37 import java.io.File;
  38 import java.nio.file.Files;
  39 import java.nio.file.Path;
  40 import java.nio.file.Paths;
  41 import java.nio.file.StandardCopyOption;
  42 import java.util.Arrays;
  43 import jdk.test.lib.cds.CDSOptions;
  44 import jdk.test.lib.cds.CDSTestUtils;
  45 import jdk.test.lib.process.OutputAnalyzer;
  46 
  47 public class LongClassListPath {
  48     private static final int MAX_PATH = 260;
  49     public static void main(String[] args) throws Exception {
  50         String[] classes = {"hello"};
  51         String classList =
  52             CDSTestUtils.makeClassList(classes).getPath();
  53         String archiveName = "LongClassListPath.jsa";
  54 
  55         // Create a directory with long path and copy the classlist file to
  56         // the directory.
  57         Path classDir = Paths.get(System.getProperty("test.classes"));
  58         Path destDir = classDir;
  59         int subDirLen = MAX_PATH - classDir.toString().length() - 2;
  60         if (subDirLen > 0) {
  61             char[] chars = new char[subDirLen];
  62             Arrays.fill(chars, 'x');
  63             String subPath = new String(chars);
  64             destDir = Paths.get(System.getProperty("test.classes"), subPath);
  65         }
  66         File longDir = destDir.toFile();
  67         longDir.mkdir();
  68         String destClassList = longDir.getPath() + File.separator + "LongClassListPath.classlist";
  69         Files.copy(Paths.get(classList), Paths.get(destClassList), StandardCopyOption.REPLACE_EXISTING);
  70 
  71         CDSOptions opts = (new CDSOptions())
  72             .addPrefix("-XX:ExtraSharedClassListFile=" + destClassList, "-cp", JarBuilder.getOrCreateHelloJar())
  73             .setArchiveName(archiveName);
  74         CDSTestUtils.createArchiveAndCheck(opts);
  75     }
  76 }