1 /*
   2  * Copyright (c) 2013, 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 8023524
  27  * @summary tests logging generated classes for lambda
  28  * @library /java/nio/file
  29  * @run testng LogGeneratedClassesTest
  30  */
  31 import java.io.File;
  32 import java.io.IOException;
  33 import java.util.ArrayList;
  34 import java.util.List;
  35 import java.nio.file.Files;
  36 import java.nio.file.Path;
  37 import java.nio.file.Paths;
  38 import java.nio.file.attribute.PosixFileAttributeView;
  39 
  40 import org.testng.annotations.AfterClass;
  41 import org.testng.annotations.BeforeClass;
  42 import org.testng.annotations.Test;
  43 import org.testng.SkipException;
  44 
  45 import static java.nio.file.attribute.PosixFilePermissions.*;
  46 import static org.testng.Assert.assertEquals;
  47 import static org.testng.Assert.assertFalse;
  48 import static org.testng.Assert.assertTrue;
  49 
  50 public class LogGeneratedClassesTest extends LUtils {
  51     String longFQCN;
  52 
  53     @BeforeClass
  54     public void setup() throws IOException {
  55         final List<String> scratch = new ArrayList<>();
  56         scratch.clear();
  57         scratch.add("package com.example;");
  58         scratch.add("public class TestLambda {");
  59         scratch.add("    interface I {");
  60         scratch.add("        int foo();");
  61         scratch.add("    }");
  62         scratch.add("    public static void main(String[] args) {");
  63         scratch.add("        I lam = () -> 10;");
  64         scratch.add("        Runnable r = () -> {");
  65         scratch.add("            System.out.println(\"Runnable\");");
  66         scratch.add("        };");
  67         scratch.add("        r.run();");
  68         scratch.add("        System.out.println(\"Finish\");");
  69         scratch.add("    }");
  70         scratch.add("}");
  71 
  72         File test = new File("TestLambda.java");
  73         createFile(test, scratch);
  74         compile("-d", ".", test.getName());
  75 
  76         scratch.remove(0);
  77         scratch.remove(0);
  78         scratch.add(0, "public class LongPackageName {");
  79         StringBuilder sb = new StringBuilder("com.example.");
  80         // longer than 255 which exceed max length of most filesystems
  81         for (int i = 0; i < 30; i++) {
  82             sb.append("nonsense.");
  83         }
  84         sb.append("enough");
  85         longFQCN = sb.toString() + ".LongPackageName";
  86         sb.append(";");
  87         sb.insert(0, "package ");
  88         scratch.add(0, sb.toString());
  89         test = new File("LongPackageName.java");
  90         createFile(test, scratch);
  91         compile("-d", ".", test.getName());
  92 
  93         // create target
  94         Files.createDirectory(Paths.get("dump"));
  95         Files.createDirectories(Paths.get("dumpLong/com/example/nonsense"));
  96         Files.createFile(Paths.get("dumpLong/com/example/nonsense/nonsense"));
  97         Files.createFile(Paths.get("file"));
  98     }
  99 
 100     @AfterClass
 101     public void cleanup() throws IOException {
 102         Files.delete(Paths.get("TestLambda.java"));
 103         Files.delete(Paths.get("LongPackageName.java"));
 104         Files.delete(Paths.get("file"));
 105         TestUtil.removeAll(Paths.get("com"));
 106         TestUtil.removeAll(Paths.get("dump"));
 107         TestUtil.removeAll(Paths.get("dumpLong"));
 108     }
 109 
 110     @Test
 111     public void testNotLogging() {
 112         TestResult tr = doExec(JAVA_CMD.getAbsolutePath(),
 113                                "-cp", ".",
 114                                "-Djava.security.manager",
 115                                "com.example.TestLambda");
 116         tr.assertZero("Should still return 0");
 117     }
 118 
 119     @Test
 120     public void testLogging() throws IOException {
 121         assertTrue(Files.exists(Paths.get("dump")));
 122         TestResult tr = doExec(JAVA_CMD.getAbsolutePath(),
 123                                "-cp", ".",
 124                                "-Djdk.internal.lambda.dumpProxyClasses=dump",
 125                                "-Djava.security.manager",
 126                                "com.example.TestLambda");
 127         // dump/com/example + 2 class files
 128         assertEquals(Files.walk(Paths.get("dump")).count(), 5, "Two lambda captured");
 129         tr.assertZero("Should still return 0");
 130     }
 131 
 132     @Test
 133     public void testDumpDirNotExist() throws IOException {
 134         assertFalse(Files.exists(Paths.get("notExist")));
 135         TestResult tr = doExec(JAVA_CMD.getAbsolutePath(),
 136                                "-cp", ".",
 137                                "-Djdk.internal.lambda.dumpProxyClasses=notExist",
 138                                "-Djava.security.manager",
 139                                "com.example.TestLambda");
 140         assertEquals(tr.testOutput.stream()
 141                                   .filter(s -> s.startsWith("WARNING"))
 142                                   .peek(s -> assertTrue(s.contains("does not exist")))
 143                                   .count(),
 144                      1, "only show error once");
 145         tr.assertZero("Should still return 0");
 146     }
 147 
 148     @Test
 149     public void testDumpDirIsFile() throws IOException {
 150         assertTrue(Files.isRegularFile(Paths.get("file")));
 151         TestResult tr = doExec(JAVA_CMD.getAbsolutePath(),
 152                                "-cp", ".",
 153                                "-Djdk.internal.lambda.dumpProxyClasses=file",
 154                                "-Djava.security.manager",
 155                                "com.example.TestLambda");
 156         assertEquals(tr.testOutput.stream()
 157                                   .filter(s -> s.startsWith("WARNING"))
 158                                   .peek(s -> assertTrue(s.contains("not a directory")))
 159                                   .count(),
 160                      1, "only show error once");
 161         tr.assertZero("Should still return 0");
 162     }
 163 
 164     private static boolean isWriteableDirectory(Path p) {
 165         if (!Files.isDirectory(p)) {
 166             return false;
 167         }
 168         Path test = p.resolve(Paths.get("test"));
 169         try {
 170             try {
 171                 Files.createFile(test);
 172                 assertTrue(Files.exists(test));
 173                 return true;
 174             } catch (IOException e) {
 175                 assertFalse(Files.exists(test));
 176                 return false;
 177             }
 178         } finally {
 179             if (Files.exists(test)) {
 180                 try {
 181                     Files.delete(test);
 182                 } catch (IOException e) {
 183                     throw new Error(e);
 184                 }
 185             }
 186         }
 187     }
 188 
 189     @Test
 190     public void testDumpDirNotWritable() throws IOException {
 191         if (!Files.getFileStore(Paths.get("."))
 192                   .supportsFileAttributeView(PosixFileAttributeView.class)) {
 193             // No easy way to setup readonly directory without POSIX
 194             // We would like to skip the test with a cause with
 195             //     throw new SkipException("Posix not supported");
 196             // but jtreg will report failure so we just pass the test
 197             // which we can look at if jtreg changed its behavior
 198             return;
 199         }
 200 
 201         Files.createDirectory(Paths.get("readOnly"),
 202                               asFileAttribute(fromString("r-xr-xr-x")));
 203         try {
 204             if (isWriteableDirectory(Paths.get("readOnly"))) {
 205                 // Skipping the test: it's allowed to write into read-only directory
 206                 // (e.g. current user is super user).
 207                 System.out.println("WARNING: readOnly directory is writeable. Skipping testDumpDirNotWritable test.");
 208                 return;
 209             }
 210 
 211             TestResult tr = doExec(JAVA_CMD.getAbsolutePath(),
 212                                    "-cp", ".",
 213                                    "-Djdk.internal.lambda.dumpProxyClasses=readOnly",
 214                                    "-Djava.security.manager",
 215                                    "com.example.TestLambda");
 216             assertEquals(tr.testOutput.stream()
 217                                       .filter(s -> s.startsWith("WARNING"))
 218                                       .peek(s -> assertTrue(s.contains("not writable")))
 219                                       .count(),
 220                          1, "only show error once");
 221             tr.assertZero("Should still return 0");
 222         } finally {
 223             TestUtil.removeAll(Paths.get("readOnly"));
 224         }
 225     }
 226 
 227     @Test
 228     public void testLoggingException() throws IOException {
 229         assertTrue(Files.exists(Paths.get("dumpLong")));
 230         TestResult tr = doExec(JAVA_CMD.getAbsolutePath(),
 231                                "-cp", ".",
 232                                "-Djdk.internal.lambda.dumpProxyClasses=dumpLong",
 233                                "-Djava.security.manager",
 234                                longFQCN);
 235         assertEquals(tr.testOutput.stream()
 236                                   .filter(s -> s.startsWith("WARNING: Exception"))
 237                                   .count(),
 238                      2, "show error each capture");
 239         // dumpLong/com/example/nosense/nosense
 240         assertEquals(Files.walk(Paths.get("dumpLong")).count(), 5, "Two lambda captured failed to log");
 241         tr.assertZero("Should still return 0");
 242     }
 243 }