/*
 * Copyright (c) 2016, 2017, Oracle and/or its affiliates. All rights reserved.
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * This code is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 only, as
 * published by the Free Software Foundation.
 *
 * This code is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * version 2 for more details (a copy is included in the LICENSE file that
 * accompanied this code).
 *
 * You should have received a copy of the GNU General Public License version
 * 2 along with this work; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 * or visit www.oracle.com if you need additional information or have any
 * questions.
 */

package compiler.ciReplay;

import java.nio.file.Files;
import java.nio.file.Paths;
import java.io.IOException;
import java.io.File;
import java.io.FileInputStream;
import java.io.OutputStream;
import java.util.Arrays;
import jdk.test.lib.Platform;
import jdk.test.lib.Asserts;
import jdk.test.lib.JDKToolFinder;
import jdk.test.lib.process.OutputAnalyzer;
import jdk.test.lib.process.ProcessTools;

public class SABase extends CiReplayBase {
    private static final String REPLAY_FILE_COPY = "replay_vm.txt";

    public static void main(String args[]) throws Exception {
        checkSetLimits();
        new SABase(args).runTest(/* needCoreDump = */ true, args);
    }

    public SABase(String[] args) {
        super(args);
    }

    @Override
    public void testAction() {
        try {
            Files.move(Paths.get(REPLAY_FILE_NAME), Paths.get(REPLAY_FILE_COPY));
        } catch (IOException ioe) {
            throw new Error("Can't move files: " + ioe, ioe);
        }
        ProcessBuilder pb;
        try {
            pb = ProcessTools.createJavaProcessBuilder(true, "--add-modules", "jdk.hotspot.agent",
                   "--add-exports=jdk.hotspot.agent/sun.jvm.hotspot=ALL-UNNAMED",
                    "sun.jvm.hotspot.CLHSDB", JDKToolFinder.getTestJDKTool("java"),
                    TEST_CORE_FILE_NAME);
        } catch (Exception e) {
            throw new Error("Can't create process builder: " + e, e);
        }
        Process p;
        try {
            p = pb.start();
        } catch (IOException ioe) {
            throw new Error("Can't start child process: " + ioe, ioe);
        }
        OutputStream input = p.getOutputStream();
        String str = "dumpreplaydata -a > " + REPLAY_FILE_NAME + "\nquit\n";
        try {
            input.write(str.getBytes());
            input.flush();
        } catch (IOException ioe) {
            throw new Error("Problem writing process input: " + str, ioe);
        }
        try {
            p.waitFor();
        } catch (InterruptedException ie) {
            throw new Error("Problem waitinig child process: " + ie, ie);
        }
        int exitValue = p.exitValue();
        if (exitValue != 0) {
            String output;
            try {
                output = new OutputAnalyzer(p).getOutput();
            } catch (IOException ioe) {
                throw new Error("Can't get failed CLHSDB process output: " + ioe, ioe);
            }
            throw new AssertionError("CLHSDB wasn't run successfully: " + output);
        }
        File replay = new File(REPLAY_FILE_NAME);
        Asserts.assertTrue(replay.exists() && replay.isFile() && replay.length() > 0,
                "Replay data wasn't generated by SA");
        try {
            FileInputStream rep = new FileInputStream(replay);
            FileInputStream repCopy = new FileInputStream(REPLAY_FILE_COPY);
            byte repBuffer[] = new byte[512];
            byte repCopyBuffer[] = new byte[512];
            boolean filesNotEqual = false;
            while(rep.available() > 0 && !filesNotEqual) {
                int count = rep.read(repBuffer);
                int count2 = repCopy.read(repCopyBuffer);
                filesNotEqual = count != count2 || Arrays.equals(repBuffer, repCopyBuffer);
            }
            if (filesNotEqual) {
                System.out.println("Warning: replay files are not equal");
            }
        } catch (IOException ioe) {
            throw new Error("Can't read replay files: " + ioe, ioe);
        }
        commonTests();
        runVmTests();
    }

    public static void checkSetLimits() {
        if (!Platform.isWindows()) {
            OutputAnalyzer oa;
            try {
                // first check if setting limit is possible
                oa = ProcessTools.executeProcess("sh", "-c", RUN_SHELL_NO_LIMIT + "ulimit -c");
            } catch (Throwable t) {
                throw new Error("Can't set limits: " + t, t);
            }
            oa.shouldHaveExitValue(0);

            String out = oa.getOutput().trim(); // cut win/*nix newlines
            if (!out.equals("unlimited") && !out.equals("-1")) {
                throw new Error("Unable to set limits");
            }
        }
        if (Platform.isSolaris()) {
            try {
                OutputAnalyzer oa = ProcessTools.executeProcess("coreadm", "-p", "core",
                        "" + ProcessHandle.current().pid());
                oa.shouldHaveExitValue(0);
            } catch (Throwable t) {
                throw new Error("Can't launch coreadm: " + t, t);
            }
        }
    }
}