test/jdk/jdk/security/logging/LogJvm.java
author coffeys
Tue, 20 Nov 2018 13:12:48 +0000
changeset 52621 f7309a1491d9
permissions -rw-r--r--
8148188: Enhance the security libraries to record events of interest Reviewed-by: egahlin, mullan, weijun, xuelei
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
52621
f7309a1491d9 8148188: Enhance the security libraries to record events of interest
coffeys
parents:
diff changeset
     1
/*
f7309a1491d9 8148188: Enhance the security libraries to record events of interest
coffeys
parents:
diff changeset
     2
 * Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved.
f7309a1491d9 8148188: Enhance the security libraries to record events of interest
coffeys
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
f7309a1491d9 8148188: Enhance the security libraries to record events of interest
coffeys
parents:
diff changeset
     4
 *
f7309a1491d9 8148188: Enhance the security libraries to record events of interest
coffeys
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
f7309a1491d9 8148188: Enhance the security libraries to record events of interest
coffeys
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
f7309a1491d9 8148188: Enhance the security libraries to record events of interest
coffeys
parents:
diff changeset
     7
 * published by the Free Software Foundation.
f7309a1491d9 8148188: Enhance the security libraries to record events of interest
coffeys
parents:
diff changeset
     8
 *
f7309a1491d9 8148188: Enhance the security libraries to record events of interest
coffeys
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
f7309a1491d9 8148188: Enhance the security libraries to record events of interest
coffeys
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
f7309a1491d9 8148188: Enhance the security libraries to record events of interest
coffeys
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
f7309a1491d9 8148188: Enhance the security libraries to record events of interest
coffeys
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
f7309a1491d9 8148188: Enhance the security libraries to record events of interest
coffeys
parents:
diff changeset
    13
 * accompanied this code).
f7309a1491d9 8148188: Enhance the security libraries to record events of interest
coffeys
parents:
diff changeset
    14
 *
f7309a1491d9 8148188: Enhance the security libraries to record events of interest
coffeys
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
f7309a1491d9 8148188: Enhance the security libraries to record events of interest
coffeys
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
f7309a1491d9 8148188: Enhance the security libraries to record events of interest
coffeys
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
f7309a1491d9 8148188: Enhance the security libraries to record events of interest
coffeys
parents:
diff changeset
    18
 *
f7309a1491d9 8148188: Enhance the security libraries to record events of interest
coffeys
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
f7309a1491d9 8148188: Enhance the security libraries to record events of interest
coffeys
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
f7309a1491d9 8148188: Enhance the security libraries to record events of interest
coffeys
parents:
diff changeset
    21
 * questions.
f7309a1491d9 8148188: Enhance the security libraries to record events of interest
coffeys
parents:
diff changeset
    22
 */
f7309a1491d9 8148188: Enhance the security libraries to record events of interest
coffeys
parents:
diff changeset
    23
f7309a1491d9 8148188: Enhance the security libraries to record events of interest
coffeys
parents:
diff changeset
    24
package jdk.security.logging;
f7309a1491d9 8148188: Enhance the security libraries to record events of interest
coffeys
parents:
diff changeset
    25
f7309a1491d9 8148188: Enhance the security libraries to record events of interest
coffeys
parents:
diff changeset
    26
import java.nio.file.Paths;
f7309a1491d9 8148188: Enhance the security libraries to record events of interest
coffeys
parents:
diff changeset
    27
import java.util.ArrayList;
f7309a1491d9 8148188: Enhance the security libraries to record events of interest
coffeys
parents:
diff changeset
    28
import java.util.Arrays;
f7309a1491d9 8148188: Enhance the security libraries to record events of interest
coffeys
parents:
diff changeset
    29
import java.util.List;
f7309a1491d9 8148188: Enhance the security libraries to record events of interest
coffeys
parents:
diff changeset
    30
f7309a1491d9 8148188: Enhance the security libraries to record events of interest
coffeys
parents:
diff changeset
    31
import jdk.test.lib.process.OutputAnalyzer;
f7309a1491d9 8148188: Enhance the security libraries to record events of interest
coffeys
parents:
diff changeset
    32
import jdk.test.lib.process.ProcessTools;
f7309a1491d9 8148188: Enhance the security libraries to record events of interest
coffeys
parents:
diff changeset
    33
f7309a1491d9 8148188: Enhance the security libraries to record events of interest
coffeys
parents:
diff changeset
    34
public final class LogJvm {
f7309a1491d9 8148188: Enhance the security libraries to record events of interest
coffeys
parents:
diff changeset
    35
    private final static String LOGGING_ENABLED= "LOGGING_ENABLED";
f7309a1491d9 8148188: Enhance the security libraries to record events of interest
coffeys
parents:
diff changeset
    36
    private final static String LOGGING_DISABLED= "LOGGING_DISABLED";
f7309a1491d9 8148188: Enhance the security libraries to record events of interest
coffeys
parents:
diff changeset
    37
f7309a1491d9 8148188: Enhance the security libraries to record events of interest
coffeys
parents:
diff changeset
    38
    private final static boolean debug = false;
f7309a1491d9 8148188: Enhance the security libraries to record events of interest
coffeys
parents:
diff changeset
    39
f7309a1491d9 8148188: Enhance the security libraries to record events of interest
coffeys
parents:
diff changeset
    40
    private final List<String> expectedLogMessages = new ArrayList<>();
f7309a1491d9 8148188: Enhance the security libraries to record events of interest
coffeys
parents:
diff changeset
    41
    private final Class<?> clazz;
f7309a1491d9 8148188: Enhance the security libraries to record events of interest
coffeys
parents:
diff changeset
    42
    private final boolean loggingEnabled;
f7309a1491d9 8148188: Enhance the security libraries to record events of interest
coffeys
parents:
diff changeset
    43
f7309a1491d9 8148188: Enhance the security libraries to record events of interest
coffeys
parents:
diff changeset
    44
    public LogJvm(Class<?> clazz, String[] args) {
f7309a1491d9 8148188: Enhance the security libraries to record events of interest
coffeys
parents:
diff changeset
    45
        this.clazz = clazz;
f7309a1491d9 8148188: Enhance the security libraries to record events of interest
coffeys
parents:
diff changeset
    46
        this.loggingEnabled = Arrays.asList(args).contains(LOGGING_ENABLED);
f7309a1491d9 8148188: Enhance the security libraries to record events of interest
coffeys
parents:
diff changeset
    47
        ensureLogging(args);
f7309a1491d9 8148188: Enhance the security libraries to record events of interest
coffeys
parents:
diff changeset
    48
    }
f7309a1491d9 8148188: Enhance the security libraries to record events of interest
coffeys
parents:
diff changeset
    49
f7309a1491d9 8148188: Enhance the security libraries to record events of interest
coffeys
parents:
diff changeset
    50
    private void ensureLogging(String[] args) {
f7309a1491d9 8148188: Enhance the security libraries to record events of interest
coffeys
parents:
diff changeset
    51
        for(String s : args) {
f7309a1491d9 8148188: Enhance the security libraries to record events of interest
coffeys
parents:
diff changeset
    52
            if (s.equals(LOGGING_ENABLED) || s.equals(LOGGING_DISABLED)) {
f7309a1491d9 8148188: Enhance the security libraries to record events of interest
coffeys
parents:
diff changeset
    53
                return;
f7309a1491d9 8148188: Enhance the security libraries to record events of interest
coffeys
parents:
diff changeset
    54
            }
f7309a1491d9 8148188: Enhance the security libraries to record events of interest
coffeys
parents:
diff changeset
    55
        }
f7309a1491d9 8148188: Enhance the security libraries to record events of interest
coffeys
parents:
diff changeset
    56
        throw new RuntimeException(LogJvm.class.getName() +
f7309a1491d9 8148188: Enhance the security libraries to record events of interest
coffeys
parents:
diff changeset
    57
            " requires command line parameter " + LOGGING_ENABLED +
f7309a1491d9 8148188: Enhance the security libraries to record events of interest
coffeys
parents:
diff changeset
    58
            " or " + LOGGING_DISABLED);
f7309a1491d9 8148188: Enhance the security libraries to record events of interest
coffeys
parents:
diff changeset
    59
    }
f7309a1491d9 8148188: Enhance the security libraries to record events of interest
coffeys
parents:
diff changeset
    60
f7309a1491d9 8148188: Enhance the security libraries to record events of interest
coffeys
parents:
diff changeset
    61
    public void addExpected(String logMsg) {
f7309a1491d9 8148188: Enhance the security libraries to record events of interest
coffeys
parents:
diff changeset
    62
        expectedLogMessages.add(logMsg);
f7309a1491d9 8148188: Enhance the security libraries to record events of interest
coffeys
parents:
diff changeset
    63
    }
f7309a1491d9 8148188: Enhance the security libraries to record events of interest
coffeys
parents:
diff changeset
    64
f7309a1491d9 8148188: Enhance the security libraries to record events of interest
coffeys
parents:
diff changeset
    65
    public void testExpected() throws Exception {
f7309a1491d9 8148188: Enhance the security libraries to record events of interest
coffeys
parents:
diff changeset
    66
        OutputAnalyzer out = launchJVM();
f7309a1491d9 8148188: Enhance the security libraries to record events of interest
coffeys
parents:
diff changeset
    67
        if (debug) {
f7309a1491d9 8148188: Enhance the security libraries to record events of interest
coffeys
parents:
diff changeset
    68
            System.out.println("STDOUT DEBUG:\n " + out.getStdout());
f7309a1491d9 8148188: Enhance the security libraries to record events of interest
coffeys
parents:
diff changeset
    69
            System.out.println("\nSTDERR DEBUG:\n " + out.getStderr());
f7309a1491d9 8148188: Enhance the security libraries to record events of interest
coffeys
parents:
diff changeset
    70
        }
f7309a1491d9 8148188: Enhance the security libraries to record events of interest
coffeys
parents:
diff changeset
    71
        if (loggingEnabled) {
f7309a1491d9 8148188: Enhance the security libraries to record events of interest
coffeys
parents:
diff changeset
    72
            testLoggingEnabled(out);
f7309a1491d9 8148188: Enhance the security libraries to record events of interest
coffeys
parents:
diff changeset
    73
        } else {
f7309a1491d9 8148188: Enhance the security libraries to record events of interest
coffeys
parents:
diff changeset
    74
            testLoggingDisabled(out);
f7309a1491d9 8148188: Enhance the security libraries to record events of interest
coffeys
parents:
diff changeset
    75
        }
f7309a1491d9 8148188: Enhance the security libraries to record events of interest
coffeys
parents:
diff changeset
    76
    }
f7309a1491d9 8148188: Enhance the security libraries to record events of interest
coffeys
parents:
diff changeset
    77
f7309a1491d9 8148188: Enhance the security libraries to record events of interest
coffeys
parents:
diff changeset
    78
    public OutputAnalyzer launchJVM() throws Exception {
f7309a1491d9 8148188: Enhance the security libraries to record events of interest
coffeys
parents:
diff changeset
    79
        List<String> args = new ArrayList<>();
f7309a1491d9 8148188: Enhance the security libraries to record events of interest
coffeys
parents:
diff changeset
    80
        if (loggingEnabled) {
f7309a1491d9 8148188: Enhance the security libraries to record events of interest
coffeys
parents:
diff changeset
    81
            args.add("-Djava.util.logging.config.file=" +
f7309a1491d9 8148188: Enhance the security libraries to record events of interest
coffeys
parents:
diff changeset
    82
                Paths.get(System.getProperty("test.src", "."), "logging.properties"));
f7309a1491d9 8148188: Enhance the security libraries to record events of interest
coffeys
parents:
diff changeset
    83
        }
f7309a1491d9 8148188: Enhance the security libraries to record events of interest
coffeys
parents:
diff changeset
    84
        args.add("--add-exports");
f7309a1491d9 8148188: Enhance the security libraries to record events of interest
coffeys
parents:
diff changeset
    85
        args.add("java.base/jdk.internal.event=ALL-UNNAMED");
f7309a1491d9 8148188: Enhance the security libraries to record events of interest
coffeys
parents:
diff changeset
    86
        args.add(clazz.getName());
f7309a1491d9 8148188: Enhance the security libraries to record events of interest
coffeys
parents:
diff changeset
    87
        System.out.println(args);
f7309a1491d9 8148188: Enhance the security libraries to record events of interest
coffeys
parents:
diff changeset
    88
        OutputAnalyzer out = ProcessTools.executeTestJava(args.toArray(new String[0]));
f7309a1491d9 8148188: Enhance the security libraries to record events of interest
coffeys
parents:
diff changeset
    89
        out.shouldHaveExitValue(0);
f7309a1491d9 8148188: Enhance the security libraries to record events of interest
coffeys
parents:
diff changeset
    90
        return out;
f7309a1491d9 8148188: Enhance the security libraries to record events of interest
coffeys
parents:
diff changeset
    91
    }
f7309a1491d9 8148188: Enhance the security libraries to record events of interest
coffeys
parents:
diff changeset
    92
f7309a1491d9 8148188: Enhance the security libraries to record events of interest
coffeys
parents:
diff changeset
    93
    private void testLoggingDisabled(OutputAnalyzer out) {
f7309a1491d9 8148188: Enhance the security libraries to record events of interest
coffeys
parents:
diff changeset
    94
        for (String expected : expectedLogMessages) {
f7309a1491d9 8148188: Enhance the security libraries to record events of interest
coffeys
parents:
diff changeset
    95
            out.shouldNotContain(expected);
f7309a1491d9 8148188: Enhance the security libraries to record events of interest
coffeys
parents:
diff changeset
    96
        }
f7309a1491d9 8148188: Enhance the security libraries to record events of interest
coffeys
parents:
diff changeset
    97
    }
f7309a1491d9 8148188: Enhance the security libraries to record events of interest
coffeys
parents:
diff changeset
    98
f7309a1491d9 8148188: Enhance the security libraries to record events of interest
coffeys
parents:
diff changeset
    99
    private void testLoggingEnabled(OutputAnalyzer out) {
f7309a1491d9 8148188: Enhance the security libraries to record events of interest
coffeys
parents:
diff changeset
   100
        for (String expected : expectedLogMessages) {
f7309a1491d9 8148188: Enhance the security libraries to record events of interest
coffeys
parents:
diff changeset
   101
            out.shouldContain(expected);
f7309a1491d9 8148188: Enhance the security libraries to record events of interest
coffeys
parents:
diff changeset
   102
        }
f7309a1491d9 8148188: Enhance the security libraries to record events of interest
coffeys
parents:
diff changeset
   103
    }
f7309a1491d9 8148188: Enhance the security libraries to record events of interest
coffeys
parents:
diff changeset
   104
}