1 /*
   2  * Copyright (c) 2006, 2010, 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 6434207 6442687 6984046
  27  * @modules jdk.jartool
  28  * @summary Ensure that jar ufm actually updates the
  29  * existing jar file's manifest with contents of the
  30  * manifest file.
  31  */
  32 
  33 import java.io.*;
  34 import java.util.logging.*;
  35 import java.util.spi.ToolProvider;
  36 import java.util.zip.*;
  37 
  38 public class UpdateManifest {
  39     static PrintStream out = System.out;
  40     static PrintStream err = System.err;
  41     static boolean debug = true;
  42 
  43     static final ToolProvider JAR_TOOL = ToolProvider.findFirst("jar")
  44         .orElseThrow(() ->
  45             new RuntimeException("jar tool not found")
  46         );
  47 
  48     static final Logger JAR_LOGGER = Logger.getLogger("java.util.jar");
  49 
  50     public static void realMain(String[] args) throws Throwable {
  51         if (args.length == 0) {
  52             debug = false;
  53             File tmp = File.createTempFile("system-out-err", ".txt");
  54             tmp.deleteOnExit();
  55             out = new PrintStream(new FileOutputStream(tmp));
  56             err = out;
  57             // Attributes.read() can log a message we don't care to see.
  58             JAR_LOGGER.setLevel(Level.OFF);
  59         }
  60 
  61         try { testManifestExistence(); } catch (Throwable t) { unexpected(t); }
  62         try { testManifestContents(); } catch (Throwable t) { unexpected(t); }
  63     }
  64 
  65     static void testManifestExistence() throws Throwable {
  66         // Create a file to put in a jar file
  67         File existence = createTextFile("existence");
  68 
  69         // Create a jar file, specifying a Main-Class
  70         final String jarFileName = "um-existence.jar";
  71         new File(jarFileName).delete(); // remove pre-existing first!
  72         int status = JAR_TOOL.run(out, err, "cfe", jarFileName,
  73                                   "Hello", existence.getPath());
  74         check(status == 0);
  75         checkManifest(jarFileName, "Hello");
  76 
  77         // Update that jar file by changing the Main-Class
  78         status = JAR_TOOL.run(out, err, "ufe", jarFileName, "Bye");
  79         check(status == 0);
  80         checkManifest(jarFileName, "Bye");
  81     }
  82 
  83     static void testManifestContents() throws Throwable {
  84         // Create some strings we expect to find in the updated manifest
  85         final String animal =
  86             "Name: animal/marsupial";
  87         final String specTitle =
  88             "Specification-Title: Wombat";
  89 
  90         // Create a text file with manifest entries
  91         File manifestOrig = File.createTempFile("manifestOrig", ".txt");
  92         if (!debug) manifestOrig.deleteOnExit();
  93         PrintWriter pw = new PrintWriter(manifestOrig);
  94         pw.println("Manifest-Version: 1.0");
  95         pw.println("Created-By: 1.7.0-internal (Oracle Corporation)");
  96         pw.println("");
  97         pw.println(animal);
  98         pw.println(specTitle);
  99         pw.close();
 100 
 101         File hello = createTextFile("hello");
 102 
 103         // Create a jar file
 104         final String jarFileName = "um-test.jar";
 105         new File(jarFileName).delete(); // remove pre-existing first!
 106         int status = JAR_TOOL.run(out, err, "cfm", jarFileName,
 107                                   manifestOrig.getPath(), hello.getPath());
 108         check(status == 0);
 109 
 110         // Create a new manifest, to use in updating the jar file.
 111         File manifestUpdate = File.createTempFile("manifestUpdate", ".txt");
 112         if (!debug) manifestUpdate.deleteOnExit();
 113         pw = new PrintWriter(manifestUpdate);
 114         final String createdBy =
 115             "Created-By: 1.7.0-special (Oracle Corporation)";
 116         final String specVersion =
 117             "Specification-Version: 1.0.0.0";
 118         pw.println(createdBy); // replaces line in the original
 119         pw.println("");
 120         pw.println(animal);
 121         pw.println(specVersion); // addition to animal/marsupial section
 122         pw.close();
 123 
 124         // Update jar file with manifest
 125         status = JAR_TOOL.run(out, err, "ufm",
 126                               jarFileName, manifestUpdate.getPath());
 127         check(status == 0);
 128 
 129         // Extract jar, and verify contents of manifest file
 130         File f = new File(jarFileName);
 131         if (!debug) f.deleteOnExit();
 132         ZipFile zf = new ZipFile(f);
 133         ZipEntry ze = zf.getEntry("META-INF/MANIFEST.MF");
 134         BufferedReader r = new BufferedReader(
 135             new InputStreamReader(zf.getInputStream(ze)));
 136         r.readLine(); // skip Manifest-Version
 137         check(r.readLine().equals(createdBy));
 138         r.readLine(); // skip blank line
 139         check(r.readLine().equals(animal));
 140         String s = r.readLine();
 141         if (s.equals(specVersion)) {
 142             check(r.readLine().equals(specTitle));
 143         } else if (s.equals(specTitle)) {
 144             check(r.readLine().equals(specVersion));
 145         } else {
 146             fail("did not match specVersion nor specTitle");
 147         }
 148         zf.close();
 149     }
 150 
 151     // --------------------- Convenience ---------------------------
 152 
 153     static File createTextFile(String name) throws Throwable {
 154         // Create a text file to put in a jar file
 155         File rc = File.createTempFile(name, ".txt");
 156         if (!debug) rc.deleteOnExit();
 157         PrintWriter pw = new PrintWriter(rc);
 158         pw.println("hello, world");
 159         pw.close();
 160         return rc;
 161     }
 162 
 163     static void checkManifest(String jarFileName, String mainClass)
 164                 throws Throwable {
 165         File f = new File(jarFileName);
 166         if (!debug) f.deleteOnExit();
 167         ZipFile zf = new ZipFile(f);
 168         ZipEntry ze = zf.getEntry("META-INF/MANIFEST.MF");
 169         BufferedReader r = new BufferedReader(
 170             new InputStreamReader(zf.getInputStream(ze)));
 171         String line = r.readLine();
 172         while (line != null && !(line.startsWith("Main-Class:"))) {
 173             line = r.readLine();
 174         }
 175         if (line == null) {
 176             fail("Didn't find Main-Class in manifest");
 177         } else {
 178             check(line.equals("Main-Class: " + mainClass));
 179         }
 180         zf.close();
 181     }
 182 
 183     // --------------------- Infrastructure ---------------------------
 184 
 185     static volatile int passed = 0, failed = 0;
 186 
 187     static void pass() {
 188         passed++;
 189     }
 190 
 191     static void fail() {
 192         failed++;
 193         Thread.dumpStack();
 194     }
 195 
 196     static void fail(String msg) {
 197         System.out.println(msg);
 198         fail();
 199     }
 200 
 201     static void unexpected(Throwable t) {
 202         failed++;
 203         t.printStackTrace();
 204     }
 205 
 206     static void check(boolean cond) {
 207         if (cond)
 208             pass();
 209         else
 210             fail();
 211     }
 212 
 213     static void equal(Object x, Object y) {
 214         if ((x == null) ? (y == null) : x.equals(y))
 215             pass();
 216         else
 217             fail(x + " not equal to " + y);
 218     }
 219 
 220     public static void main(String[] args) throws Throwable {
 221         try {
 222             realMain(args);
 223         } catch (Throwable t) {
 224             unexpected(t);
 225         }
 226         System.out.println("\nPassed = " + passed + " failed = " + failed);
 227         if (failed > 0)
 228             throw new AssertionError("Some tests failed");
 229     }
 230 }