1 /* 2 * Licensed to the Apache Software Foundation (ASF) under one or more 3 * contributor license agreements. See the NOTICE file distributed with 4 * this work for additional information regarding copyright ownership. 5 * The ASF licenses this file to You under the Apache License, Version 2.0 6 * (the "License"); you may not use this file except in compliance with 7 * the License. You may obtain a copy of the License at 8 * 9 * http://www.apache.org/licenses/LICENSE-2.0 10 * 11 * Unless required by applicable law or agreed to in writing, software 12 * distributed under the License is distributed on an "AS IS" BASIS, 13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 * See the License for the specific language governing permissions and 15 * limitations under the License. 16 */ 17 package org.apache.harmony.tests.java.util.zip; 18 19 import java.io.ByteArrayInputStream; 20 import java.io.ByteArrayOutputStream; 21 import java.io.File; 22 import java.io.FileInputStream; 23 import java.io.FileOutputStream; 24 import java.io.IOException; 25 import java.io.InputStream; 26 import java.net.URL; 27 import java.util.zip.Checksum; 28 import java.util.zip.GZIPInputStream; 29 import java.util.zip.GZIPOutputStream; 30 31 import tests.support.resource.Support_Resources; 32 33 public class GZIPInputStreamTest extends junit.framework.TestCase { 34 File resources; 35 36 class TestGZIPInputStream extends GZIPInputStream { 37 TestGZIPInputStream(InputStream in) throws IOException { 38 super(in); 39 } 40 41 TestGZIPInputStream(InputStream in, int size) throws IOException { 42 super(in, size); 43 } 44 45 Checksum getChecksum() { 46 return crc; 47 } 48 49 boolean endofInput() { 50 return eos; 51 } 52 } 53 54 /** 55 * @tests java.util.zip.GZIPInputStream#GZIPInputStream(java.io.InputStream) 56 */ 57 public void test_ConstructorLjava_io_InputStream() { 58 // test method java.util.zip.GZIPInputStream.constructor 59 try { 60 Support_Resources.copyFile(resources, null, "hyts_gInput.txt.gz"); 61 final URL gInput = new File(resources.toString() + "/hyts_gInput.txt.gz").toURL(); 62 TestGZIPInputStream inGZIP = new TestGZIPInputStream(gInput 63 .openConnection().getInputStream()); 64 assertNotNull("the constructor for GZIPInputStream is null", 65 inGZIP); 66 assertEquals("the CRC value of the inputStream is not zero", 0, inGZIP 67 .getChecksum().getValue()); 68 inGZIP.close(); 69 } catch (IOException e) { 70 fail( 71 "an IO error occured while trying to open the input file"); 72 } 73 } 74 75 /** 76 * @tests java.util.zip.GZIPInputStream#GZIPInputStream(java.io.InputStream, 77 *int) 78 */ 79 public void test_ConstructorLjava_io_InputStreamI() { 80 // test method java.util.zip.GZIPInputStream.constructorI 81 try { 82 Support_Resources.copyFile(resources, null, "hyts_gInput.txt.gz"); 83 final URL gInput = new File(resources.toString() + "/hyts_gInput.txt.gz").toURL(); 84 TestGZIPInputStream inGZIP = new TestGZIPInputStream(gInput 85 .openConnection().getInputStream(), 200); 86 assertNotNull("the constructor for GZIPInputStream is null", 87 inGZIP); 88 assertEquals("the CRC value of the inputStream is not zero", 0, inGZIP 89 .getChecksum().getValue()); 90 inGZIP.close(); 91 92 try { 93 TestGZIPInputStream inGZIP1 = new TestGZIPInputStream(gInput 94 .openConnection().getInputStream(), 0); 95 fail("IllegalArgumentException expected"); 96 } catch (IllegalArgumentException ioe) { 97 //expected 98 } 99 100 Support_Resources.copyFile(resources, null, "hyts_checkInput.txt"); 101 final URL jarInput = new File(resources.toString() + "/hyts_checkInput.txt").toURL(); 102 try { 103 TestGZIPInputStream inGZIP1 = new TestGZIPInputStream(jarInput 104 .openConnection().getInputStream(), 200); 105 fail("IOException expected"); 106 } catch (IOException ex) { 107 //expected 108 } 109 110 } catch (IOException e) { 111 fail( 112 "an IO error occured while trying to open the input file"); 113 } 114 } 115 116 /** 117 * @tests java.util.zip.GZIPInputStream#read(byte[], int, int) 118 */ 119 public void test_read$BII() throws IOException { 120 // test method java.util.zip.GZIPInputStream.readBII 121 byte orgBuf[] = { '3', '5', '2', 'r', 'g', 'e', 'f', 'd', 'e', 'w' }; 122 byte outBuf[] = new byte[100]; 123 int result = 0; 124 Support_Resources.copyFile(resources, null, "hyts_gInput.txt.gz"); 125 final URL gInput = new File(resources.toString() + "/hyts_gInput.txt.gz").toURL(); 126 TestGZIPInputStream inGZIP = new TestGZIPInputStream(gInput 127 .openConnection().getInputStream()); 128 while (!(inGZIP.endofInput())) { 129 result += inGZIP.read(outBuf, result, outBuf.length - result); 130 } 131 assertEquals( 132 "the checkSum value of the compressed and decompressed data does not equal", 133 2074883667L, inGZIP.getChecksum().getValue()); 134 for (int i = 0; i < orgBuf.length; i++) { 135 assertEquals( 136 "the decompressed data does not equal the original data decompressed", 137 orgBuf[i], outBuf[i]); 138 // System.out.println(orgBuf[i] + " " + outBuf[i]); 139 } 140 int r = 0; 141 try { 142 inGZIP.read(outBuf, 100, 1); 143 } catch (IndexOutOfBoundsException e) { 144 r = 1; 145 } 146 inGZIP.close(); 147 // line below fails on RI also, comment out. 148 // assertEquals("Boundary Check was not present", 1, r); 149 150 // Create compressed data which is exactly 512 bytes (after the 151 // header), 152 // the size of the InflaterStream internal buffer 153 byte[] test = new byte[507]; 154 for (int i = 0; i < 256; i++) { 155 test[i] = (byte) i; 156 } 157 for (int i = 256; i < test.length; i++) { 158 test[i] = (byte) (256 - i); 159 } 160 ByteArrayOutputStream bout = new ByteArrayOutputStream(); 161 GZIPOutputStream out = new GZIPOutputStream(bout); 162 out.write(test); 163 out.close(); 164 byte[] comp = bout.toByteArray(); 165 GZIPInputStream gin2 = new GZIPInputStream(new ByteArrayInputStream( 166 comp), 512); 167 int total = 0; 168 while ((result = gin2.read(test)) != -1) { 169 total += result; 170 } 171 assertEquals("Should return -1", -1, gin2.read()); 172 gin2.close(); 173 assertEquals("Incorrectly decompressed", test.length, total); 174 175 gin2 = new GZIPInputStream(new ByteArrayInputStream(comp), 512); 176 total = 0; 177 while ((result = gin2.read(new byte[200])) != -1) { 178 total += result; 179 } 180 assertEquals("Should return -1", -1, gin2.read()); 181 gin2.close(); 182 assertEquals("Incorrectly decompressed", test.length, total); 183 184 gin2 = new GZIPInputStream(new ByteArrayInputStream(comp), 516); 185 total = 0; 186 while ((result = gin2.read(new byte[200])) != -1) { 187 total += result; 188 } 189 assertEquals("Should return -1", -1, gin2.read()); 190 gin2.close(); 191 assertEquals("Incorrectly decompressed", test.length, total); 192 193 comp[40] = 0; 194 gin2 = new GZIPInputStream(new ByteArrayInputStream(comp), 512); 195 boolean exception = false; 196 try { 197 while (gin2.read(test) != -1) { 198 ; 199 } 200 } catch (IOException e) { 201 exception = true; 202 } 203 assertTrue("Exception expected", exception); 204 205 ByteArrayOutputStream baos = new ByteArrayOutputStream(); 206 GZIPOutputStream zipout = new GZIPOutputStream(baos); 207 zipout.write(test); 208 zipout.close(); 209 outBuf = new byte[530]; 210 GZIPInputStream in = new GZIPInputStream(new ByteArrayInputStream(baos.toByteArray())); 211 try { 212 in.read(outBuf, 530, 1); 213 fail("Test failed IOOBE was not thrown"); 214 } catch (IndexOutOfBoundsException e) { 215 } 216 while (true) { 217 result = in.read(outBuf, 0, 5); 218 if (result == -1) { 219 //"EOF was reached"; 220 break; 221 } 222 } 223 result = -10; 224 result = in.read(null, 100, 1); 225 result = in.read(outBuf, -100, 1); 226 result = in.read(outBuf, -1, 1);// 100, 1); 227 } 228 229 /** 230 * @tests java.util.zip.GZIPInputStream#close() 231 */ 232 public void test_close() { 233 // test method java.util.zip.GZIPInputStream.close 234 byte outBuf[] = new byte[100]; 235 try { 236 int result = 0; 237 Support_Resources.copyFile(resources, null, "hyts_gInput.txt.gz"); 238 final URL gInput = new File(resources.toString() + "/hyts_gInput.txt.gz").toURL(); 239 TestGZIPInputStream inGZIP = new TestGZIPInputStream(gInput 240 .openConnection().getInputStream()); 241 while (!(inGZIP.endofInput())) { 242 result += inGZIP.read(outBuf, result, outBuf.length - result); 243 } 244 assertEquals("the checkSum value of the compressed and decompressed data does not equal", 245 2074883667L, inGZIP.getChecksum().getValue()); 246 inGZIP.close(); 247 int r = 0; 248 try { 249 inGZIP.read(outBuf, 0, 1); 250 } catch (IOException e) { 251 r = 1; 252 } 253 assertEquals("GZIPInputStream can still be used after close is called", 254 1, r); 255 } catch (IOException e) { 256 e.printStackTrace(); 257 fail("unexpected: " + e); 258 } 259 } 260 261 /** 262 * Regression test for HARMONY-3703. 263 * 264 * @tests java.util.zip.GZIPInputStream#read() 265 */ 266 public void test_read() throws IOException { 267 GZIPInputStream gis = null; 268 int result = 0; 269 byte[] buffer = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }; 270 File f = new File(resources.getAbsolutePath() + "test.gz"); 271 FileOutputStream out = new FileOutputStream(f); 272 GZIPOutputStream gout = new GZIPOutputStream(out); 273 274 // write 100 bytes to the stream 275 for (int i = 0; i < 10; i++) { 276 gout.write(buffer); 277 } 278 gout.finish(); 279 out.write(1); 280 out.close(); 281 282 gis = new GZIPInputStream(new FileInputStream(f)); 283 buffer = new byte[100]; 284 gis.read(buffer); 285 result = gis.read(); 286 gis.close(); 287 f.delete(); 288 289 assertEquals("Incorrect value returned at the end of the file", -1, result); 290 } 291 292 @Override 293 protected void setUp() { 294 resources = Support_Resources.createTempFolder(); 295 } 296 297 @Override 298 protected void tearDown() { 299 } 300 301 } 302