1/*
2 * Copyright (c) 2005, 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 6336960
26 * @summary Validate that GZIPInputStream.readUByte throws the correct
27 * exception when read() on the InputStream with which the GZIPInputStream was
28 * created returns an out-of-bounds value.
29 */
30
31import java.io.*;
32import java.util.zip.GZIPInputStream;
33
34public class ReadUByte {
35    /** Guaranteed to return a value that is out-of-spec for read(). */
36    static class BrokenInputStream extends ByteArrayInputStream {
37        BrokenInputStream() {
38            super(new byte[16]);
39        }
40
41        public int read() {
42            return -33;
43        }
44    }
45
46    public static void realMain(String[] args) throws Throwable {
47        try {
48            new GZIPInputStream(new BrokenInputStream());
49            fail("Failed to throw expected IOException");
50        } catch (IOException ex) {
51            String msg = ex.getMessage();
52            if (msg.indexOf("ReadUByte$BrokenInputStream.read() returned value out of range") < 0) {
53                fail("IOException contains incorrect message: '" + msg + "'");
54            }
55        }
56    }
57
58    //--------------------- Infrastructure ---------------------------
59    static volatile int passed = 0, failed = 0;
60    static void pass() {passed++;}
61    static void fail() {failed++; Thread.dumpStack();}
62    static void fail(String msg) {System.out.println(msg); fail();}
63    static void unexpected(Throwable t) {failed++; t.printStackTrace();}
64    static void check(boolean cond) {if (cond) pass(); else fail();}
65    static void equal(Object x, Object y) {
66        if (x == null ? y == null : x.equals(y)) pass();
67        else fail(x + " not equal to " + y);}
68    public static void main(String[] args) throws Throwable {
69        try {realMain(args);} catch (Throwable t) {unexpected(t);}
70        System.out.println("\nPassed = " + passed + " failed = " + failed);
71        if (failed > 0) throw new AssertionError("Some tests failed");}
72}
73