|
| 1 | +/* |
| 2 | + * Copyright (c) 2020, 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 org.testng.Assert; |
| 25 | +import org.testng.annotations.Test; |
| 26 | + |
| 27 | +import java.io.ByteArrayInputStream; |
| 28 | +import java.io.ByteArrayOutputStream; |
| 29 | +import java.nio.charset.StandardCharsets; |
| 30 | +import java.util.zip.GZIPInputStream; |
| 31 | +import java.util.zip.GZIPOutputStream; |
| 32 | + |
| 33 | +/** |
| 34 | + * @test |
| 35 | + * @bug 8244706 |
| 36 | + * @summary Verify that the OS header flag in the stream written out by java.util.zip.GZIPOutputStream |
| 37 | + * has the correct expected value |
| 38 | + * @run testng GZIPOutputStreamHeaderTest |
| 39 | + */ |
| 40 | +public class GZIPOutputStreamHeaderTest { |
| 41 | + |
| 42 | + private static final int OS_HEADER_INDEX = 9; |
| 43 | + private static final int HEADER_VALUE_OS_UNKNOWN = 255; |
| 44 | + |
| 45 | + /** |
| 46 | + * Test that the {@code OS} header field in the GZIP output stream |
| 47 | + * has a value of {@code 255} which represents "unknown" |
| 48 | + */ |
| 49 | + @Test |
| 50 | + public void testOSHeader() throws Exception { |
| 51 | + final String data = "Hello world!!!"; |
| 52 | + final ByteArrayOutputStream baos = new ByteArrayOutputStream(); |
| 53 | + try (final GZIPOutputStream gzipOutputStream = new GZIPOutputStream(baos);) { |
| 54 | + gzipOutputStream.write(data.getBytes(StandardCharsets.UTF_8)); |
| 55 | + } |
| 56 | + final byte[] compressed = baos.toByteArray(); |
| 57 | + Assert.assertNotNull(compressed, "Compressed data is null"); |
| 58 | + Assert.assertEquals(toUnsignedByte(compressed[OS_HEADER_INDEX]), HEADER_VALUE_OS_UNKNOWN, |
| 59 | + "Unexpected value for OS header"); |
| 60 | + // finally verify that the compressed data is readable back to the original |
| 61 | + final String uncompressed; |
| 62 | + try (final ByteArrayOutputStream os = new ByteArrayOutputStream(); |
| 63 | + final ByteArrayInputStream bis = new ByteArrayInputStream(compressed); |
| 64 | + final GZIPInputStream gzipInputStream = new GZIPInputStream(bis)) { |
| 65 | + gzipInputStream.transferTo(os); |
| 66 | + uncompressed = new String(os.toByteArray(), StandardCharsets.UTF_8); |
| 67 | + } |
| 68 | + Assert.assertEquals(uncompressed, data, "Unexpected data read from GZIPInputStream"); |
| 69 | + } |
| 70 | + |
| 71 | + private static int toUnsignedByte(final byte b) { |
| 72 | + return b & 0xff; |
| 73 | + } |
| 74 | +} |
0 commit comments