FileInputStreamTest.java revision 5aaf9f30c17c31c0030e66cf06b102a545322bca
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
18package org.apache.harmony.luni.tests.java.io;
19
20import java.io.File;
21import java.io.FileDescriptor;
22import java.io.FileInputStream;
23import java.io.FileNotFoundException;
24import java.io.FileOutputStream;
25import java.io.FilePermission;
26import java.io.IOException;
27import java.io.InputStreamReader;
28import java.security.Permission;
29
30import junit.framework.TestCase;
31import tests.support.Support_PlatformFile;
32
33public class FileInputStreamTest extends TestCase {
34
35    public String fileName;
36
37    private java.io.InputStream is;
38
39    byte[] ibuf = new byte[4096];
40
41    public String fileString = "Test_All_Tests\nTest_java_io_BufferedInputStream\nTest_java_io_BufferedOutputStream\nTest_java_io_ByteArrayInputStream\nTest_java_io_ByteArrayOutputStream\nTest_java_io_DataInputStream\nTest_java_io_File\nTest_java_io_FileDescriptor\nTest_FileInputStream\nTest_java_io_FileNotFoundException\nTest_java_io_FileOutputStream\nTest_java_io_FilterInputStream\nTest_java_io_FilterOutputStream\nTest_java_io_InputStream\nTest_java_io_IOException\nTest_java_io_OutputStream\nTest_java_io_PrintStream\nTest_java_io_RandomAccessFile\nTest_java_io_SyncFailedException\nTest_java_lang_AbstractMethodError\nTest_java_lang_ArithmeticException\nTest_java_lang_ArrayIndexOutOfBoundsException\nTest_java_lang_ArrayStoreException\nTest_java_lang_Boolean\nTest_java_lang_Byte\nTest_java_lang_Character\nTest_java_lang_Class\nTest_java_lang_ClassCastException\nTest_java_lang_ClassCircularityError\nTest_java_lang_ClassFormatError\nTest_java_lang_ClassLoader\nTest_java_lang_ClassNotFoundException\nTest_java_lang_CloneNotSupportedException\nTest_java_lang_Double\nTest_java_lang_Error\nTest_java_lang_Exception\nTest_java_lang_ExceptionInInitializerError\nTest_java_lang_Float\nTest_java_lang_IllegalAccessError\nTest_java_lang_IllegalAccessException\nTest_java_lang_IllegalArgumentException\nTest_java_lang_IllegalMonitorStateException\nTest_java_lang_IllegalThreadStateException\nTest_java_lang_IncompatibleClassChangeError\nTest_java_lang_IndexOutOfBoundsException\nTest_java_lang_InstantiationError\nTest_java_lang_InstantiationException\nTest_java_lang_Integer\nTest_java_lang_InternalError\nTest_java_lang_InterruptedException\nTest_java_lang_LinkageError\nTest_java_lang_Long\nTest_java_lang_Math\nTest_java_lang_NegativeArraySizeException\nTest_java_lang_NoClassDefFoundError\nTest_java_lang_NoSuchFieldError\nTest_java_lang_NoSuchMethodError\nTest_java_lang_NullPointerException\nTest_java_lang_Number\nTest_java_lang_NumberFormatException\nTest_java_lang_Object\nTest_java_lang_OutOfMemoryError\nTest_java_lang_RuntimeException\nTest_java_lang_SecurityManager\nTest_java_lang_Short\nTest_java_lang_StackOverflowError\nTest_java_lang_String\nTest_java_lang_StringBuffer\nTest_java_lang_StringIndexOutOfBoundsException\nTest_java_lang_System\nTest_java_lang_Thread\nTest_java_lang_ThreadDeath\nTest_java_lang_ThreadGroup\nTest_java_lang_Throwable\nTest_java_lang_UnknownError\nTest_java_lang_UnsatisfiedLinkError\nTest_java_lang_VerifyError\nTest_java_lang_VirtualMachineError\nTest_java_lang_vm_Image\nTest_java_lang_vm_MemorySegment\nTest_java_lang_vm_ROMStoreException\nTest_java_lang_vm_VM\nTest_java_lang_Void\nTest_java_net_BindException\nTest_java_net_ConnectException\nTest_java_net_DatagramPacket\nTest_java_net_DatagramSocket\nTest_java_net_DatagramSocketImpl\nTest_java_net_InetAddress\nTest_java_net_NoRouteToHostException\nTest_java_net_PlainDatagramSocketImpl\nTest_java_net_PlainSocketImpl\nTest_java_net_Socket\nTest_java_net_SocketException\nTest_java_net_SocketImpl\nTest_java_net_SocketInputStream\nTest_java_net_SocketOutputStream\nTest_java_net_UnknownHostException\nTest_java_util_ArrayEnumerator\nTest_java_util_Date\nTest_java_util_EventObject\nTest_java_util_HashEnumerator\nTest_java_util_Hashtable\nTest_java_util_Properties\nTest_java_util_ResourceBundle\nTest_java_util_tm\nTest_java_util_Vector\n";
42
43    /**
44     * @tests java.io.FileInputStream#FileInputStream(java.io.File)
45     */
46    public void test_ConstructorLjava_io_File() throws IOException {
47        java.io.File f = new File(fileName);
48        is = new FileInputStream(f);
49        is.close();
50    }
51
52    /**
53     * @tests java.io.FileInputStream#FileInputStream(java.io.FileDescriptor)
54     */
55    public void test_ConstructorLjava_io_FileDescriptor() throws IOException {
56        FileOutputStream fos = new FileOutputStream(fileName);
57        FileInputStream fis = new FileInputStream(fos.getFD());
58        fos.close();
59        fis.close();
60    }
61
62    /**
63     * @tests java.io.FileInputStream#FileInputStream(java.lang.String)
64     */
65    public void test_ConstructorLjava_lang_String() throws IOException {
66        is = new FileInputStream(fileName);
67        is.close();
68    }
69
70    /**
71     * @tests java.io.FileInputStream#FileInputStream(java.lang.String)
72     */
73    public void test_ConstructorLjava_lang_String_I() throws IOException {
74        try {
75            is = new FileInputStream("");
76            fail("should throw FileNotFoundException.");
77        } catch (FileNotFoundException e) {
78            // Expected
79        } finally {
80            if (is != null) {
81                is.close();
82            }
83        }
84        try {
85            is = new FileInputStream(new File(""));
86            fail("should throw FileNotFoundException.");
87        } catch (FileNotFoundException e) {
88            // Expected
89        } finally {
90            if (is != null) {
91                is.close();
92            }
93        }
94    }
95
96    /**
97     * @tests java.io.FileInputStream#available()
98     */
99    public void test_available() throws IOException {
100        try {
101            is = new FileInputStream(fileName);
102            assertTrue("Returned incorrect number of available bytes", is
103                    .available() == fileString.length());
104        } finally {
105            try {
106                is.close();
107            } catch (IOException e) {
108            }
109        }
110    }
111
112    /**
113     * @tests java.io.FileInputStream#close()
114     */
115    public void test_close() throws IOException {
116        is = new FileInputStream(fileName);
117        is.close();
118
119        try {
120            is.read();
121            fail("Able to read from closed stream");
122        } catch (IOException e) {
123            // Expected
124        }
125
126        // Regression test for HARMONY-6642
127        FileInputStream fis = new FileInputStream(fileName);
128        FileInputStream fis2 = new FileInputStream(fis.getFD());
129        try {
130            fis2.close();
131            fis.read();
132            fail("Able to read from closed fd");
133        } catch (IOException e) {
134            // Expected
135        } finally {
136            try {
137                fis.close();
138            } catch (IOException e) {}
139        }
140
141        FileInputStream stdin = new FileInputStream(FileDescriptor.in);
142        stdin.close();
143        stdin = new FileInputStream(FileDescriptor.in);
144        try {
145            stdin.read();
146            fail("Able to read from stdin after close");
147        } catch (IOException e) {
148            // Expected
149        }
150    }
151
152    /**
153     * @tests java.io.FileInputStream#getFD()
154     */
155    public void test_getFD() throws IOException {
156        FileInputStream fis = new FileInputStream(fileName);
157        assertTrue("Returned invalid fd", fis.getFD().valid());
158        fis.close();
159        assertTrue("Returned invalid fd", !fis.getFD().valid());
160    }
161
162    /**
163     * @tests java.io.FileInputStream#read()
164     */
165    public void test_read() throws IOException {
166        InputStreamReader isr = new InputStreamReader(new FileInputStream(fileName));
167        int c = isr.read();
168        isr.close();
169        assertTrue("read returned incorrect char", c == fileString.charAt(0));
170    }
171
172    /**
173     * @tests java.io.FileInputStream#read(byte[])
174     */
175    public void test_read$B() throws IOException {
176        byte[] buf1 = new byte[100];
177        is = new FileInputStream(fileName);
178        is.skip(3000);
179        is.read(buf1);
180        is.close();
181        assertTrue("Failed to read correct data", new String(buf1, 0,
182                buf1.length).equals(fileString.substring(3000, 3100)));
183    }
184
185    /**
186     * @tests java.io.FileInputStream#read(byte[], int, int)
187     */
188    public void test_read$BII() throws IOException {
189        byte[] buf1 = new byte[100];
190        is = new FileInputStream(fileName);
191        is.skip(3000);
192        is.read(buf1, 0, buf1.length);
193        is.close();
194        assertTrue("Failed to read correct data", new String(buf1, 0,
195                buf1.length).equals(fileString.substring(3000, 3100)));
196
197        // Regression test for HARMONY-285
198        File tmpFile = File.createTempFile("FileOutputStream", "tmp");
199        FileInputStream in = new FileInputStream(tmpFile);
200        try {
201            in.read(null, 0, 0);
202            fail("Should throw NullPointerException");
203        } catch (NullPointerException e) {
204            // Expected
205        } finally {
206            in.close();
207            tmpFile.delete();
208        }
209    }
210
211    /**
212     * @tests java.io.FileInputStream#read(byte[], int, int)
213     */
214    public void test_read_$BII_IOException() throws IOException {
215        byte[] buf = new byte[1000];
216        try {
217            is = new FileInputStream(fileName);
218            is.read(buf, -1, 0);
219            fail("should throw IndexOutOfBoundsException");
220        } catch (IndexOutOfBoundsException e) {
221            // Expected
222        } finally {
223            is.close();
224        }
225
226        try {
227            is = new FileInputStream(fileName);
228            is.read(buf, 0, -1);
229            fail("should throw IndexOutOfBoundsException");
230        } catch (IndexOutOfBoundsException e) {
231            // Expected
232        } finally {
233            is.close();
234        }
235
236        try {
237            is = new FileInputStream(fileName);
238            is.read(buf, -1, -1);
239            fail("should throw IndexOutOfBoundsException");
240        } catch (IndexOutOfBoundsException e) {
241            // Expected
242        } finally {
243            is.close();
244        }
245
246        try {
247            is = new FileInputStream(fileName);
248            is.read(buf, 0, 1001);
249            fail("should throw IndexOutOfBoundsException");
250        } catch (IndexOutOfBoundsException e) {
251            // Expected
252        } finally {
253            is.close();
254        }
255
256        try {
257            is = new FileInputStream(fileName);
258            is.read(buf, 1001, 0);
259            fail("should throw IndexOutOfBoundsException");
260        } catch (IndexOutOfBoundsException e) {
261            // Expected
262        } finally {
263            is.close();
264        }
265
266        try {
267            is = new FileInputStream(fileName);
268            is.read(buf, 500, 501);
269            fail("should throw IndexOutOfBoundsException");
270        } catch (IndexOutOfBoundsException e) {
271            // Expected
272        } finally {
273            is.close();
274        }
275
276        try {
277            is = new FileInputStream(fileName);
278            is.close();
279            is.read(buf, 0, 100);
280            fail("should throw IOException");
281        } catch (IOException e) {
282            // Expected
283        } finally {
284            is.close();
285        }
286
287        try {
288            is = new FileInputStream(fileName);
289            is.close();
290            is.read(buf, 0, 0);
291        } finally {
292            is.close();
293        }
294    }
295
296    /**
297     * @tests java.io.FileInputStream#read(byte[], int, int)
298     */
299    public void test_read_$BII_NullPointerException() throws IOException {
300        byte[] buf = null;
301        try {
302            is = new FileInputStream(fileName);
303            is.read(buf, -1, 0);
304            fail("should throw NullPointerException");
305        } catch (NullPointerException e) {
306            // Expected
307        } finally {
308            is.close();
309        }
310    }
311
312    /**
313     * @tests java.io.FileInputStream#read(byte[], int, int)
314     */
315    public void test_read_$BII_IndexOutOfBoundsException() throws IOException {
316        byte[] buf = new byte[1000];
317        try {
318            is = new FileInputStream(fileName);
319            is.close();
320            is.read(buf, -1, -1);
321            fail("should throw IndexOutOfBoundsException");
322        } catch (IndexOutOfBoundsException e) {
323            // Expected
324        } finally {
325            is.close();
326        }
327    }
328
329    /**
330     * @tests java.io.FileInputStream#skip(long)
331     */
332    public void test_skipJ() throws IOException {
333        byte[] buf1 = new byte[10];
334        is = new FileInputStream(fileName);
335        is.skip(1000);
336        is.read(buf1, 0, buf1.length);
337        is.close();
338        assertTrue("Failed to skip to correct position", new String(buf1, 0,
339                buf1.length).equals(fileString.substring(1000, 1010)));
340    }
341
342    /**
343     * @tests java.io.FileInputStream#read(byte[], int, int))
344     */
345    public void test_regressionNNN() throws IOException {
346        // Regression for HARMONY-434
347        FileInputStream fis = new FileInputStream(fileName);
348
349        try {
350            fis.read(new byte[1], -1, 1);
351            fail("IndexOutOfBoundsException must be thrown if off <0");
352        } catch (IndexOutOfBoundsException e) {
353            // Expected
354        }
355
356        try {
357            fis.read(new byte[1], 0, -1);
358            fail("IndexOutOfBoundsException must be thrown if len <0");
359        } catch (IndexOutOfBoundsException e) {
360            // Expected
361        }
362
363        try {
364            fis.read(new byte[1], 0, 5);
365            fail("IndexOutOfBoundsException must be thrown if off+len > b.length");
366        } catch (IndexOutOfBoundsException e) {
367            // Expected
368        }
369
370        try {
371            fis.read(new byte[10], Integer.MAX_VALUE, 5);
372            fail("IndexOutOfBoundsException expected");
373        } catch (IndexOutOfBoundsException e) {
374            // Expected
375        }
376
377        try {
378            fis.read(new byte[10], 5, Integer.MAX_VALUE);
379            fail("IndexOutOfBoundsException expected");
380        } catch (IndexOutOfBoundsException e) {
381            // Expected
382        }
383        fis.close();
384    }
385
386    /**
387     * @tests java.io.FileInputStream#skip(long)
388     */
389    public void test_skipNegativeArgumentJ() throws IOException {
390        FileInputStream fis = new FileInputStream(fileName);
391        try {
392            fis.skip(-5);
393            fail("IOException must be thrown if number of bytes to skip <0");
394        } catch (IOException e) {
395            // Expected IOException
396        } finally {
397            fis.close();
398        }
399    }
400
401    public void test_getChannel() throws Exception {
402        FileInputStream fis = new FileInputStream(fileName);
403        assertEquals(0, fis.getChannel().position());
404        int r;
405        int count = 1;
406        while((r = fis.read()) != -1) {
407            assertEquals(count++, fis.getChannel().position());
408        }
409        fis.close();
410
411        try {
412            fis.getChannel().position();
413            fail("should throw ClosedChannelException");
414        } catch(java.nio.channels.ClosedChannelException e){
415            // Expected
416        }
417
418        fis = new FileInputStream(fileName);
419        assertEquals(0, fis.getChannel().position());
420        byte[] bs = new byte[10];
421        r = fis.read(bs);
422        assertEquals(10, fis.getChannel().position());
423        fis.close();
424
425        fis = new FileInputStream(fileName);
426        assertEquals(0, fis.getChannel().position());
427        bs = new byte[10];
428        fis.skip(100);
429        assertEquals(100, fis.getChannel().position());
430        r = fis.read(bs);
431        assertEquals(110, fis.getChannel().position());
432        fis.close();
433    }
434
435    /**
436     * Sets up the fixture, for example, open a network connection. This method
437     * is called before a test is executed.
438     */
439    protected void setUp() throws IOException {
440        fileName = System.getProperty("user.dir");
441        String separator = System.getProperty("file.separator");
442        if (fileName.charAt(fileName.length() - 1) == separator.charAt(0))
443            fileName = Support_PlatformFile.getNewPlatformFile(fileName,
444                    "input.tst");
445        else
446            fileName = Support_PlatformFile.getNewPlatformFile(fileName
447                    + separator, "input.tst");
448        java.io.OutputStream fos = new java.io.FileOutputStream(fileName);
449        fos.write(fileString.getBytes());
450        fos.close();
451    }
452
453    /**
454     * Tears down the fixture, for example, close a network connection. This
455     * method is called after a test is executed.
456     */
457    protected void tearDown() {
458        new File(fileName).delete();
459    }
460}
461