1 /*
   2  * Copyright (c) 2010, 2011, 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  * @summary test reading of module files with left over bytes at the end.
  27  */
  28 
  29 import java.io.*;
  30 import java.util.*;
  31 import org.openjdk.jigsaw.*;
  32 import org.openjdk.jigsaw.cli.*;
  33 
  34 public class ModuleFormatTestLeftOverBytes {
  35     final String MNAME = "hello";
  36     final String MVER = "0.1";
  37     String moduleinfo = "module " + MNAME + " @ " + MVER + " {}";
  38 
  39     public static void main(String[] args) throws Exception {
  40         new ModuleFormatTestLeftOverBytes().run();
  41     }
  42 
  43     void run() throws Exception {
  44         try {
  45             test();
  46         } catch (Throwable t) {
  47             t.printStackTrace();
  48             errors++;
  49         }
  50 
  51 
  52         if (errors == 0)
  53             System.out.println(count + " tests passed");
  54         else
  55             throw new Exception(errors + "/" + count + " tests failed");
  56     }
  57 
  58     void testEmptyModule() throws Exception {
  59         System.err.println("Test: Empty module");
  60         count++;
  61         reset();
  62         List<File> files = new ArrayList<File>();
  63         addFile(files, createFile("module-info.java", moduleinfo));
  64         compile(files);
  65         compress(MNAME);
  66         append(MNAME, MVER, "These bytes are not made for parsing!");
  67         try {
  68             extract(MNAME, MVER);
  69         } catch (IllegalArgumentException e) {
  70             /* swallow expected IllegalArgumentException */
  71             if (! e.getMessage().startsWith("No SectionType"))
  72                 throw e;
  73         }
  74     }
  75 
  76     void test() throws Exception {
  77         testEmptyModule();
  78     }
  79 
  80     /**
  81      * Extract a module.
  82      */
  83     void extract(String name, String version) throws Exception {
  84         File module = new File(moduleDir, name + "@" + version + ".jmod");
  85         String [] args = {"extract", module.getAbsolutePath()};
  86         Librarian.run(args);
  87     }
  88 
  89     /**
  90      * Append some content at the end of a module file.
  91      */
  92     void append(String name, String version, String content) throws Exception {
  93         String fname = moduleDir + File.separator + name + "@" + version + ".jmod";
  94         RandomAccessFile module = new RandomAccessFile(fname, "rw");
  95         module.seek(module.length());
  96         module.writeUTF(content);
  97         module.close();
  98     }
  99 
 100     /**
 101      * Compress a module.
 102      */
 103     void compress(String name) throws Exception {
 104         compress(name, false);
 105     }
 106 
 107     void compress(String name, boolean haveNatLibs)
 108         throws Exception {
 109         compress(name, haveNatLibs, false);
 110     }
 111 
 112     void compress(String name, boolean haveNatLibs,
 113                   boolean haveNatCmds) throws Exception {
 114         compress(name, haveNatLibs, haveNatCmds, false);
 115     }
 116 
 117     void compress(String name, boolean haveNatLibs,
 118                   boolean haveNatCmds, boolean haveConfig)
 119         throws Exception {
 120         List<String> args = new ArrayList<String>();
 121         args.add("-m");
 122         args.add(classesDir.getAbsolutePath());
 123         args.add("-d");
 124         args.add(moduleDir.getAbsolutePath());
 125         if (haveNatLibs) {
 126             args.add("--natlib");
 127             args.add(natlibDir.toString());
 128         }
 129         if (haveNatCmds) {
 130             args.add("--natcmd");
 131             args.add(natcmdDir.toString());
 132         }
 133         if (haveConfig) {
 134             args.add("--config");
 135             args.add(configDir.toString());
 136         }
 137         args.add("jmod");
 138         args.add("hello");
 139         Packager.main(args.toArray(new String[0]));
 140     }
 141 
 142     /**
 143      * Compile a list of files.
 144      */
 145     void compile(List<File> files) {
 146         List<String> options = new ArrayList<String>();
 147         options.addAll(Arrays.asList("-source", "7", "-d", classesDir.getPath()));
 148         for (File f: files)
 149             options.add(f.getPath());
 150 
 151         String[] opts = options.toArray(new String[options.size()]);
 152         StringWriter sw = new StringWriter();
 153         PrintWriter pw = new PrintWriter(sw);
 154         int rc = com.sun.tools.javac.Main.compile(opts, pw);
 155         pw.close();
 156 
 157         String out = sw.toString();
 158         if (out.trim().length() > 0)
 159             System.err.println(out);
 160         if (rc != 0)
 161             throw new Error("compilation failed: rc=" + rc);
 162     }
 163 
 164     /**
 165      * Add a file to a list if the file is not null.
 166      */
 167     void addFile(List<File> files, File file) {
 168         if (file != null)
 169             files.add(file);
 170     }
 171 
 172 
 173     /**
 174      * Create a test file with given content if the content is not null.
 175      */
 176     File createFile(String path, String body) throws IOException {
 177         if (body == null)
 178             return null;
 179         File file = new File(srcDir, path);
 180         file.getAbsoluteFile().getParentFile().mkdirs();
 181         FileWriter out = new FileWriter(file);
 182         out.write(body);
 183         out.close();
 184         return file;
 185     }
 186 
 187     /**
 188      * Set up empty src and classes directories for a test.
 189      */
 190     void reset() {
 191         resetDir(srcDir);
 192         resetDir(classesDir);
 193         resetDir(moduleDir);
 194         resetDir(new File(MNAME));
 195     }
 196 
 197     /**
 198      * Set up an empty directory.
 199      */
 200     void resetDir(File dir) {
 201         if (dir.exists())
 202             deleteAll(dir);
 203         dir.mkdirs();
 204     }
 205 
 206     /**
 207      * Delete a file or a directory (including all its contents).
 208      */
 209     boolean deleteAll(File file) {
 210         if (file.isDirectory()) {
 211             for (File f: file.listFiles())
 212                 deleteAll(f);
 213         }
 214         return file.delete();
 215     }
 216 
 217     /**
 218      * Report an error.
 219      */
 220     void error(String msg, String... more) {
 221         System.err.println("error: " + msg);
 222         for (String s: more)
 223             System.err.println(s);
 224         errors++;
 225     }
 226 
 227     int count;
 228     int errors;
 229     File srcDir = new File("tmp", "src"); // use "tmp" to help avoid accidents
 230     File classesDir = new File("tmp", "classes");
 231     File moduleDir = new File("tmp", "modules");
 232     File natlibDir = new File(srcDir, "natlib");
 233     File natcmdDir = new File(srcDir, "natcmd");
 234     File configDir = new File(srcDir, "config");
 235 }