1 /*
   2  * Copyright (c) 2016, 2017, 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 8163798
  27  * @summary basic tests for multi-release jar versioned streams
  28  * @library /test/lib
  29  * @modules jdk.jartool/sun.tools.jar java.base/jdk.internal.util.jar
  30  * @build jdk.test.lib.Platform
  31  *        jdk.test.lib.Utils
  32  *        jdk.test.lib.Asserts
  33  *        jdk.test.lib.JDKToolFinder
  34  *        jdk.test.lib.JDKToolLauncher
  35  *        jdk.test.lib.process.*
  36  *        jdk.test.lib.util.FileUtils
  37  * @run testng TestVersionedStream
  38  */
  39 
  40 import org.testng.Assert;
  41 import org.testng.annotations.AfterClass;
  42 import org.testng.annotations.DataProvider;
  43 import org.testng.annotations.Test;
  44 
  45 import java.io.File;
  46 import java.io.IOException;
  47 import java.io.InputStream;
  48 import java.io.UncheckedIOException;
  49 import java.nio.file.Files;
  50 import java.nio.file.Path;
  51 import java.nio.file.Paths;
  52 import java.util.ArrayList;
  53 import java.util.Arrays;
  54 import java.util.HashMap;
  55 import java.util.Iterator;
  56 import java.util.LinkedHashSet;
  57 import java.util.List;
  58 import java.util.Map;
  59 import java.util.Set;
  60 import java.util.jar.JarEntry;
  61 import java.util.jar.JarFile;
  62 import java.util.stream.Collectors;
  63 import java.util.stream.Stream;
  64 import java.util.zip.ZipFile;
  65 
  66 import jdk.test.lib.util.FileUtils;
  67 
  68 public class TestVersionedStream {
  69     private final Path userdir;
  70     private final Set<String> unversionedEntryNames;
  71 
  72     public TestVersionedStream() throws IOException {
  73         userdir = Paths.get(System.getProperty("user.dir", "."));
  74 
  75         // These are not real class files even though they end with .class.
  76         // They are resource files so jar tool validation won't reject them.
  77         // But they are what we want to test, especially q/Bar.class that
  78         // could be in a concealed package if this was a modular multi-release
  79         // jar.
  80         createFiles(
  81                 "base/p/Bar.class",
  82                 "base/p/Foo.class",
  83                 "base/p/Main.class",
  84                 "v9/p/Foo.class",
  85                 "v10/p/Foo.class",
  86                 "v10/q/Bar.class",
  87                 "v11/p/Bar.class",
  88                 "v11/p/Foo.class"
  89         );
  90 
  91         jar("cf mmr.jar -C base . --release 9 -C v9 . " +
  92                 "--release 10 -C v10 . --release 11 -C v11 .");
  93 
  94         System.out.println("Contents of mmr.jar\n=======");
  95 
  96         try(JarFile jf = new JarFile("mmr.jar")) {
  97             unversionedEntryNames = jf.stream()
  98                     .map(je -> je.getName())
  99                     .peek(System.out::println)
 100                     .map(nm -> nm.startsWith("META-INF/versions/")
 101                             ? nm.replaceFirst("META-INF/versions/\\d+/", "")
 102                             : nm)
 103                     .collect(Collectors.toCollection(LinkedHashSet::new));
 104         }
 105 
 106         System.out.println("=======");
 107     }
 108 
 109     @AfterClass
 110     public void close() throws IOException {
 111         Files.walk(userdir, 1)
 112                 .filter(p -> !p.equals(userdir))
 113                 .forEach(p -> {
 114                     try {
 115                         if (Files.isDirectory(p)) {
 116                             FileUtils.deleteFileTreeWithRetry(p);
 117                         } else {
 118                             FileUtils.deleteFileIfExistsWithRetry(p);
 119                         }
 120                     } catch (IOException x) {
 121                         throw new UncheckedIOException(x);
 122                     }
 123                 });
 124     }
 125 
 126     @DataProvider
 127     public Object[][] data() {
 128         return new Object[][] {
 129             {Runtime.Version.parse("8")},
 130             {Runtime.Version.parse("9")},
 131             {Runtime.Version.parse("10")},
 132             {Runtime.Version.parse("11")},
 133             {JarFile.baseVersion()},
 134             {JarFile.runtimeVersion()}
 135         };
 136     }
 137 
 138     @Test(dataProvider="data")
 139     public void test(Runtime.Version version) throws Exception {
 140         try (JarFile jf = new JarFile(new File("mmr.jar"), false, ZipFile.OPEN_READ, version);
 141              Stream<JarEntry> jes = jdk.internal.util.jar.VersionedStream.stream(jf))
 142         {
 143             Assert.assertNotNull(jes);
 144 
 145             // put versioned entries in list so we can reuse them
 146             List<JarEntry> versionedEntries = jes.collect(Collectors.toList());
 147 
 148             Assert.assertTrue(versionedEntries.size() > 0);
 149 
 150             // also keep the names
 151             List<String> versionedNames = new ArrayList<>(versionedEntries.size());
 152 
 153             // verify the correct order while building enames
 154             Iterator<String> allIt = unversionedEntryNames.iterator();
 155             Iterator<JarEntry> verIt = versionedEntries.iterator();
 156             boolean match = false;
 157 
 158             while (verIt.hasNext()) {
 159                 match = false;
 160                 if (!allIt.hasNext()) break;
 161                 String name = verIt.next().getName();
 162                 versionedNames.add(name);
 163                 while (allIt.hasNext()) {
 164                     if (name.equals(allIt.next())) {
 165                         match = true;
 166                         break;
 167                     }
 168                 }
 169             }
 170             if (!match) {
 171                 Assert.fail("versioned entries not in same order as unversioned entries");
 172             }
 173 
 174             // verify the contents
 175             Map<String,String> contents = new HashMap<>();
 176             contents.put("p/Bar.class", "base/p/Bar.class");
 177             contents.put("p/Main.class", "base/p/Main.class");
 178             switch (version.major()) {
 179                 case 8:
 180                     contents.put("p/Foo.class", "base/p/Foo.class");
 181                     break;
 182                 case 9:
 183                     contents.put("p/Foo.class", "v9/p/Foo.class");
 184                     break;
 185                 case 10:
 186                     contents.put("p/Foo.class", "v10/p/Foo.class");
 187                     contents.put("q/Bar.class", "v10/q/Bar.class");
 188                     break;
 189                 case 11:
 190                     contents.put("p/Bar.class", "v11/p/Bar.class");
 191                     contents.put("p/Foo.class", "v11/p/Foo.class");
 192                     contents.put("q/Bar.class", "v10/q/Bar.class");
 193                     break;
 194                 default:
 195                     Assert.fail("Test out of date, please add more cases");
 196             }
 197 
 198             contents.entrySet().stream().forEach(e -> {
 199                 String name = e.getKey();
 200                 int i = versionedNames.indexOf(name);
 201                 Assert.assertTrue(i != -1, name + " not in enames");
 202                 JarEntry je = versionedEntries.get(i);
 203                 try (InputStream is = jf.getInputStream(je)) {
 204                     String s = new String(is.readAllBytes()).replaceAll(System.lineSeparator(), "");
 205                     Assert.assertTrue(s.endsWith(e.getValue()), s);
 206                 } catch (IOException x) {
 207                     throw new UncheckedIOException(x);
 208                 }
 209             });
 210         }
 211     }
 212 
 213     private void createFiles(String... files) {
 214         ArrayList<String> list = new ArrayList();
 215         Arrays.stream(files)
 216                 .map(f -> Paths.get(userdir.toAbsolutePath().toString(), f))
 217                 .forEach(p -> {
 218                     try {
 219                         Files.createDirectories(p.getParent());
 220                         Files.createFile(p);
 221                         list.clear();
 222                         list.add(p.toString().replace(File.separatorChar, '/'));
 223                         Files.write(p, list);
 224                     } catch (IOException x) {
 225                         throw new UncheckedIOException(x);
 226                     }});
 227     }
 228 
 229     private void jar(String args) {
 230         new sun.tools.jar.Main(System.out, System.err, "jar")
 231                 .run(args.split(" +"));
 232     }
 233 }