1 /*
 2  * Copyright (c) 2022, 2023, 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 import jdk.test.lib.process.ProcessTools;
25 
26 import java.io.BufferedReader;
27 import java.io.IOException;
28 import java.io.InputStream;
29 import java.io.InputStreamReader;
30 import java.util.ArrayList;
31 import java.util.Arrays;
32 import java.util.List;
33 
34 import static org.testng.Assert.assertNotEquals;
35 import static org.testng.Assert.assertTrue;
36 
37 public class UpcallTestHelper extends NativeTestHelper {
38     public record Output(List<String> stdout, List<String> stderr) {
39         private static void assertContains(List<String> lines, String shouldInclude, String name) {
40             assertTrue(lines.stream().anyMatch(line -> line.contains(shouldInclude)),
41                 "Did not find '" + shouldInclude + "' in " + name);
42         }
43 
44         public Output assertStdErrContains(String shouldInclude) {
45             assertContains(stderr, shouldInclude, "stderr");
46             return this;
47         }
48 
49         public Output assertStdOutContains(String shouldInclude) {
50             assertContains(stdout, shouldInclude, "stdout");
51             return this;
52         }
53     }
54 
55     public Output runInNewProcess(Class<?> target, boolean useSpec, String... programArgs) throws IOException, InterruptedException {
56         assert !target.isArray();
57 
58         List<String> command = new ArrayList<>(List.of(
59             "--enable-preview",
60             "--enable-native-access=ALL-UNNAMED",
61             "-Djava.library.path=" + System.getProperty("java.library.path"),
62             "-Djdk.internal.foreign.UpcallLinker.USE_SPEC=" + useSpec,
63             target.getName()
64         ));
65         command.addAll(Arrays.asList(programArgs));
66 
67         Process process = ProcessTools.createTestJvm(command).start();
68 
69         int result = process.waitFor();
70         assertNotEquals(result, 0);
71 
72         List<String> outLines = linesFromStream(process.getInputStream());
73         outLines.forEach(System.out::println);
74         List<String> errLines = linesFromStream(process.getErrorStream());
75         errLines.forEach(System.err::println);
76 
77         return new Output(outLines, errLines);
78     }
79 
80     private static List<String> linesFromStream(InputStream stream) throws IOException {
81         try (BufferedReader reader = new BufferedReader(new InputStreamReader(stream))) {
82             return reader.lines().toList();
83         }
84     }
85 }