|
| 1 | +/* |
| 2 | + * Copyright (c) 2021, 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 static java.lang.String.format; |
| 25 | + |
| 26 | +import java.io.ByteArrayInputStream; |
| 27 | +import java.io.ByteArrayOutputStream; |
| 28 | +import java.io.IOException; |
| 29 | +import java.io.InputStream; |
| 30 | +import java.io.OutputStream; |
| 31 | +import java.nio.channels.Channels; |
| 32 | +import java.nio.channels.FileChannel; |
| 33 | +import java.nio.file.Files; |
| 34 | +import java.nio.file.Path; |
| 35 | +import java.nio.file.StandardOpenOption; |
| 36 | +import java.util.Arrays; |
| 37 | +import java.util.Random; |
| 38 | +import java.util.concurrent.atomic.AtomicReference; |
| 39 | +import java.util.function.Consumer; |
| 40 | +import java.util.function.Supplier; |
| 41 | + |
| 42 | +import jdk.test.lib.RandomFactory; |
| 43 | + |
| 44 | +/* |
| 45 | + * @test |
| 46 | + * @library /test/lib |
| 47 | + * @build jdk.test.lib.RandomFactory |
| 48 | + * @run main TransferTo |
| 49 | + * @bug 8265891 |
| 50 | + * @summary tests whether sun.nio.ChannelInputStream.transferTo conforms to the |
| 51 | + * InputStream.transferTo contract defined in the javadoc |
| 52 | + * @key randomness |
| 53 | + */ |
| 54 | +public class TransferTo { |
| 55 | + private static final int MIN_SIZE = 10_000; |
| 56 | + private static final int MAX_SIZE_INCR = 100_000_000 - MIN_SIZE; |
| 57 | + |
| 58 | + private static final int ITERATIONS = 10; |
| 59 | + |
| 60 | + private static final Random RND = RandomFactory.getRandom(); |
| 61 | + |
| 62 | + public static void main(String[] args) throws Exception { |
| 63 | + test(fileChannelInput(), fileChannelOutput()); |
| 64 | + test(readableByteChannelInput(), defaultOutput()); |
| 65 | + } |
| 66 | + |
| 67 | + private static void test(InputStreamProvider inputStreamProvider, OutputStreamProvider outputStreamProvider) |
| 68 | + throws Exception { |
| 69 | + testNullPointerException(inputStreamProvider); |
| 70 | + testStreamContents(inputStreamProvider, outputStreamProvider); |
| 71 | + } |
| 72 | + |
| 73 | + private static void testNullPointerException(InputStreamProvider inputStreamProvider) throws Exception { |
| 74 | + try (InputStream in = inputStreamProvider.input()) { |
| 75 | + assertThrowsNPE(() -> in.transferTo(null), "out"); |
| 76 | + } |
| 77 | + |
| 78 | + try (InputStream in = inputStreamProvider.input((byte) 1)) { |
| 79 | + assertThrowsNPE(() -> in.transferTo(null), "out"); |
| 80 | + } |
| 81 | + |
| 82 | + try (InputStream in = inputStreamProvider.input((byte) 1, (byte) 2)) { |
| 83 | + assertThrowsNPE(() -> in.transferTo(null), "out"); |
| 84 | + } |
| 85 | + } |
| 86 | + |
| 87 | + private static void testStreamContents(InputStreamProvider inputStreamProvider, |
| 88 | + OutputStreamProvider outputStreamProvider) throws Exception { |
| 89 | + checkTransferredContents(inputStreamProvider, outputStreamProvider, new byte[0]); |
| 90 | + checkTransferredContents(inputStreamProvider, outputStreamProvider, createRandomBytes(1024, 4096)); |
| 91 | + |
| 92 | + // to span through several batches |
| 93 | + checkTransferredContents(inputStreamProvider, outputStreamProvider, createRandomBytes(16384, 16384)); |
| 94 | + |
| 95 | + // randomly chosen starting positions within source and target |
| 96 | + for (int i = 0; i < ITERATIONS; i++) { |
| 97 | + byte[] inBytes = createRandomBytes(MIN_SIZE, MAX_SIZE_INCR); |
| 98 | + int posIn = RND.nextInt(inBytes.length); |
| 99 | + int posOut = RND.nextInt(MIN_SIZE); |
| 100 | + checkTransferredContents(inputStreamProvider, outputStreamProvider, inBytes, posIn, posOut); |
| 101 | + } |
| 102 | + |
| 103 | + // beyond source EOF |
| 104 | + checkTransferredContents(inputStreamProvider, outputStreamProvider, createRandomBytes(4096, 0), 4096, 0); |
| 105 | + |
| 106 | + // beyond target EOF |
| 107 | + checkTransferredContents(inputStreamProvider, outputStreamProvider, createRandomBytes(4096, 0), 0, 4096); |
| 108 | + } |
| 109 | + |
| 110 | + private static void checkTransferredContents(InputStreamProvider inputStreamProvider, |
| 111 | + OutputStreamProvider outputStreamProvider, byte[] inBytes) throws Exception { |
| 112 | + checkTransferredContents(inputStreamProvider, outputStreamProvider, inBytes, 0, 0); |
| 113 | + } |
| 114 | + |
| 115 | + private static void checkTransferredContents(InputStreamProvider inputStreamProvider, |
| 116 | + OutputStreamProvider outputStreamProvider, byte[] inBytes, int posIn, int posOut) throws Exception { |
| 117 | + AtomicReference<Supplier<byte[]>> recorder = new AtomicReference<>(); |
| 118 | + try (InputStream in = inputStreamProvider.input(inBytes); |
| 119 | + OutputStream out = outputStreamProvider.output(recorder::set)) { |
| 120 | + // skip bytes till starting position |
| 121 | + in.skipNBytes(posIn); |
| 122 | + out.write(new byte[posOut]); |
| 123 | + |
| 124 | + long reported = in.transferTo(out); |
| 125 | + int count = inBytes.length - posIn; |
| 126 | + |
| 127 | + if (reported != count) |
| 128 | + throw new AssertionError( |
| 129 | + format("reported %d bytes but should report %d", reported, count)); |
| 130 | + |
| 131 | + byte[] outBytes = recorder.get().get(); |
| 132 | + if (!Arrays.equals(inBytes, posIn, posIn + count, outBytes, posOut, posOut + count)) |
| 133 | + throw new AssertionError( |
| 134 | + format("inBytes.length=%d, outBytes.length=%d", count, outBytes.length)); |
| 135 | + } |
| 136 | + } |
| 137 | + |
| 138 | + private static byte[] createRandomBytes(int min, int maxRandomAdditive) { |
| 139 | + byte[] bytes = new byte[min + (maxRandomAdditive == 0 ? 0 : RND.nextInt(maxRandomAdditive))]; |
| 140 | + RND.nextBytes(bytes); |
| 141 | + return bytes; |
| 142 | + } |
| 143 | + |
| 144 | + private interface InputStreamProvider { |
| 145 | + InputStream input(byte... bytes) throws Exception; |
| 146 | + } |
| 147 | + |
| 148 | + private interface OutputStreamProvider { |
| 149 | + OutputStream output(Consumer<Supplier<byte[]>> spy) throws Exception; |
| 150 | + } |
| 151 | + |
| 152 | + private static OutputStreamProvider defaultOutput() { |
| 153 | + return new OutputStreamProvider() { |
| 154 | + @Override |
| 155 | + public OutputStream output(Consumer<Supplier<byte[]>> spy) { |
| 156 | + ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); |
| 157 | + spy.accept(outputStream::toByteArray); |
| 158 | + return outputStream; |
| 159 | + } |
| 160 | + }; |
| 161 | + } |
| 162 | + |
| 163 | + private static InputStreamProvider fileChannelInput() { |
| 164 | + return new InputStreamProvider() { |
| 165 | + @Override |
| 166 | + public InputStream input(byte... bytes) throws Exception { |
| 167 | + Path path = Files.createTempFile(null, null); |
| 168 | + Files.write(path, bytes); |
| 169 | + FileChannel fileChannel = FileChannel.open(path); |
| 170 | + return Channels.newInputStream(fileChannel); |
| 171 | + } |
| 172 | + }; |
| 173 | + } |
| 174 | + |
| 175 | + private static InputStreamProvider readableByteChannelInput() { |
| 176 | + return new InputStreamProvider() { |
| 177 | + @Override |
| 178 | + public InputStream input(byte... bytes) throws Exception { |
| 179 | + return Channels.newInputStream(Channels.newChannel(new ByteArrayInputStream(bytes))); |
| 180 | + } |
| 181 | + }; |
| 182 | + } |
| 183 | + |
| 184 | + private static OutputStreamProvider fileChannelOutput() { |
| 185 | + return new OutputStreamProvider() { |
| 186 | + public OutputStream output(Consumer<Supplier<byte[]>> spy) throws Exception { |
| 187 | + Path path = Files.createTempFile(null, null); |
| 188 | + FileChannel fileChannel = FileChannel.open(path, StandardOpenOption.WRITE); |
| 189 | + spy.accept(() -> { |
| 190 | + try { |
| 191 | + return Files.readAllBytes(path); |
| 192 | + } catch (IOException e) { |
| 193 | + throw new AssertionError("Failed to verify output file", e); |
| 194 | + } |
| 195 | + }); |
| 196 | + return Channels.newOutputStream(fileChannel); |
| 197 | + } |
| 198 | + }; |
| 199 | + } |
| 200 | + |
| 201 | + public interface Thrower { |
| 202 | + public void run() throws Throwable; |
| 203 | + } |
| 204 | + |
| 205 | + public static void assertThrowsNPE(Thrower thrower, String message) { |
| 206 | + assertThrows(thrower, NullPointerException.class, message); |
| 207 | + } |
| 208 | + |
| 209 | + public static <T extends Throwable> void assertThrows(Thrower thrower, Class<T> throwable, String message) { |
| 210 | + Throwable thrown; |
| 211 | + try { |
| 212 | + thrower.run(); |
| 213 | + thrown = null; |
| 214 | + } catch (Throwable caught) { |
| 215 | + thrown = caught; |
| 216 | + } |
| 217 | + |
| 218 | + if (!throwable.isInstance(thrown)) { |
| 219 | + String caught = thrown == null ? "nothing" : thrown.getClass().getCanonicalName(); |
| 220 | + throw new AssertionError(format("Expected to catch %s, but caught %s", throwable, caught), thrown); |
| 221 | + } |
| 222 | + |
| 223 | + if (thrown != null && !message.equals(thrown.getMessage())) { |
| 224 | + throw new AssertionError( |
| 225 | + format("Expected exception message to be '%s', but it's '%s'", message, thrown.getMessage())); |
| 226 | + } |
| 227 | + } |
| 228 | +} |
1 commit comments
openjdk-notifier[bot] commentedon Sep 8, 2021
Review
Issues