|
| 1 | +/* |
| 2 | + * Copyright (c) 2024, 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 | +/* @test |
| 25 | + * @bug 8341666 |
| 26 | + * @summary Test of FileInputStream reading from stdin and a named pipe |
| 27 | + * @requires os.family != "windows" |
| 28 | + * @library .. /test/lib |
| 29 | + * @build jdk.test.lib.Platform |
| 30 | + * @run junit/othervm --enable-native-access=ALL-UNNAMED PseudoDevice |
| 31 | + */ |
| 32 | + |
| 33 | +import java.io.File; |
| 34 | +import java.io.FileInputStream; |
| 35 | +import java.io.FileOutputStream; |
| 36 | +import java.io.InputStream; |
| 37 | +import java.io.IOException; |
| 38 | +import java.lang.foreign.Arena; |
| 39 | +import java.lang.foreign.FunctionDescriptor; |
| 40 | +import java.lang.foreign.Linker; |
| 41 | +import java.lang.foreign.MemorySegment; |
| 42 | +import java.lang.foreign.SymbolLookup; |
| 43 | +import java.lang.foreign.ValueLayout; |
| 44 | +import java.lang.invoke.MethodHandle; |
| 45 | +import jdk.test.lib.Platform; |
| 46 | + |
| 47 | +import org.junit.jupiter.api.AfterAll; |
| 48 | +import org.junit.jupiter.api.BeforeAll; |
| 49 | +import org.junit.jupiter.api.Test; |
| 50 | +import org.junit.jupiter.api.condition.DisabledOnOs; |
| 51 | +import org.junit.jupiter.api.condition.OS; |
| 52 | +import static org.junit.jupiter.api.Assertions.*; |
| 53 | + |
| 54 | +public class PseudoDevice { |
| 55 | + |
| 56 | + private static final String PIPE = "pipe"; |
| 57 | + private static final File PIPE_FILE = new File(PIPE); |
| 58 | + private static final String SENTENCE = |
| 59 | + "Rien n'est permis mais tout est possible"; |
| 60 | + |
| 61 | + private static class mkfifo { |
| 62 | + public static final FunctionDescriptor DESC = FunctionDescriptor.of( |
| 63 | + ValueLayout.JAVA_INT, |
| 64 | + ValueLayout.ADDRESS, |
| 65 | + ValueLayout.JAVA_SHORT |
| 66 | + ); |
| 67 | + |
| 68 | + public static final MemorySegment ADDR; |
| 69 | + static { |
| 70 | + Linker linker = Linker.nativeLinker(); |
| 71 | + SymbolLookup stdlib = linker.defaultLookup(); |
| 72 | + ADDR = stdlib.find("mkfifo").orElseThrow(); |
| 73 | + } |
| 74 | + |
| 75 | + public static final MethodHandle HANDLE = |
| 76 | + Linker.nativeLinker().downcallHandle(ADDR, DESC); |
| 77 | + } |
| 78 | + |
| 79 | + public static int mkfifo(MemorySegment x0, short x1) { |
| 80 | + var mh$ = mkfifo.HANDLE; |
| 81 | + try { |
| 82 | + return (int)mh$.invokeExact(x0, x1); |
| 83 | + } catch (Throwable ex$) { |
| 84 | + throw new AssertionError("should not reach here", ex$); |
| 85 | + } |
| 86 | + } |
| 87 | + |
| 88 | + private static Thread createWriteThread() { |
| 89 | + Thread t = new Thread( |
| 90 | + new Runnable() { |
| 91 | + public void run() { |
| 92 | + try (FileOutputStream fos = new FileOutputStream(PIPE);) { |
| 93 | + fos.write(SENTENCE.getBytes()); |
| 94 | + } catch (IOException e) { |
| 95 | + throw new RuntimeException(e); |
| 96 | + } |
| 97 | + } |
| 98 | + } |
| 99 | + ); |
| 100 | + t.start(); |
| 101 | + return t; |
| 102 | + } |
| 103 | + |
| 104 | + @BeforeAll |
| 105 | + static void before() throws InterruptedException, IOException { |
| 106 | + if (Platform.isWindows()) |
| 107 | + return; |
| 108 | + |
| 109 | + PIPE_FILE.delete(); |
| 110 | + try (var newArena = Arena.ofConfined()) { |
| 111 | + var addr = newArena.allocateFrom(PIPE); |
| 112 | + short mode = 0666; |
| 113 | + assertEquals(0, mkfifo(addr, mode)); |
| 114 | + } |
| 115 | + if (!PIPE_FILE.exists()) |
| 116 | + throw new RuntimeException("Failed to create " + PIPE); |
| 117 | + } |
| 118 | + |
| 119 | + @AfterAll |
| 120 | + static void after() throws IOException { |
| 121 | + if (Platform.isWindows()) |
| 122 | + return; |
| 123 | + |
| 124 | + PIPE_FILE.delete(); |
| 125 | + } |
| 126 | + |
| 127 | + /** |
| 128 | + * Tests that new FileInputStream(File).available() does not throw |
| 129 | + */ |
| 130 | + @Test |
| 131 | + @DisabledOnOs(OS.WINDOWS) |
| 132 | + void availableStdin() throws IOException { |
| 133 | + File stdin = new File("/dev", "stdin"); |
| 134 | + if (stdin.exists()) { |
| 135 | + try (InputStream s = new FileInputStream(stdin);) { |
| 136 | + s.available(); |
| 137 | + } |
| 138 | + } |
| 139 | + } |
| 140 | + |
| 141 | + /** |
| 142 | + * Tests that new FileInputStream(File).skip(0) does not throw |
| 143 | + */ |
| 144 | + @Test |
| 145 | + @DisabledOnOs(OS.WINDOWS) |
| 146 | + void skipStdin() throws IOException { |
| 147 | + File stdin = new File("/dev", "stdin"); |
| 148 | + if (stdin.exists()) { |
| 149 | + try (InputStream s = new FileInputStream(stdin);) { |
| 150 | + s.skip(0); |
| 151 | + } |
| 152 | + } |
| 153 | + } |
| 154 | + |
| 155 | + /** |
| 156 | + * Tests new FileInputStream(File).readAllBytes(). |
| 157 | + */ |
| 158 | + @Test |
| 159 | + @DisabledOnOs(OS.WINDOWS) |
| 160 | + void readAllBytes() throws InterruptedException, IOException { |
| 161 | + Thread t = createWriteThread(); |
| 162 | + try (InputStream in = new FileInputStream(PIPE)) { |
| 163 | + String s = new String(in.readAllBytes()); |
| 164 | + System.out.println(s); |
| 165 | + assertEquals(SENTENCE, s); |
| 166 | + } finally { |
| 167 | + t.join(); |
| 168 | + } |
| 169 | + } |
| 170 | + |
| 171 | + /** |
| 172 | + * Tests new FileInputStream(File).readNBytes(byte[],int,int). |
| 173 | + */ |
| 174 | + @Test |
| 175 | + @DisabledOnOs(OS.WINDOWS) |
| 176 | + void readNBytesNoOverride() throws InterruptedException, IOException { |
| 177 | + Thread t = createWriteThread(); |
| 178 | + try (InputStream in = new FileInputStream(PIPE)) { |
| 179 | + final int offset = 11; |
| 180 | + final int length = 17; |
| 181 | + assert length <= SENTENCE.length(); |
| 182 | + byte[] b = new byte[offset + length]; |
| 183 | + int n = in.readNBytes(b, offset, length); |
| 184 | + String s = new String(b, offset, length); |
| 185 | + System.out.println(s); |
| 186 | + assertEquals(SENTENCE.substring(0, length), s); |
| 187 | + } finally { |
| 188 | + t.join(); |
| 189 | + } |
| 190 | + } |
| 191 | + |
| 192 | + /** |
| 193 | + * Tests new FileInputStream(File).readNBytes(int). |
| 194 | + */ |
| 195 | + @Test |
| 196 | + @DisabledOnOs(OS.WINDOWS) |
| 197 | + void readNBytesOverride() throws InterruptedException, IOException { |
| 198 | + Thread t = createWriteThread(); |
| 199 | + try (InputStream in = new FileInputStream(PIPE)) { |
| 200 | + final int length = 17; |
| 201 | + assert length <= SENTENCE.length(); |
| 202 | + byte[] b = in.readNBytes(length); |
| 203 | + String s = new String(b); |
| 204 | + System.out.println(s); |
| 205 | + assertEquals(SENTENCE.substring(0, length), s); |
| 206 | + } finally { |
| 207 | + t.join(); |
| 208 | + } |
| 209 | + } |
| 210 | +} |
0 commit comments