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    public void test_close() throws IOException {
113        is = new FileInputStream(fileName);
114        is.close();
115        try {
116            is.read();
117            fail();
118        } catch (IOException expected) {
119        }
120    }
121
122    public void test_close_shared_fd() throws IOException {
123        // Regression test for HARMONY-6642
124        FileInputStream fis1 = new FileInputStream(fileName);
125        FileInputStream fis2 = new FileInputStream(fis1.getFD());
126        try {
127            fis2.close();
128            fis1.read();
129            fail("fd sharing error");
130        } catch (IOException expected) {
131        } finally {
132            try {
133                fis1.close();
134            } catch (IOException e) {}
135        }
136
137        // TODO: how does this differ from the test above?
138        FileInputStream stdin = new FileInputStream(FileDescriptor.in);
139        stdin.close();
140        stdin = new FileInputStream(FileDescriptor.in);
141        try {
142            stdin.read();
143            fail();
144        } catch (IOException expected) {
145        }
146    }
147
148    /**
149     * @tests java.io.FileInputStream#getFD()
150     */
151    public void test_getFD() throws IOException {
152        FileInputStream fis = new FileInputStream(fileName);
153        assertTrue("Returned invalid fd", fis.getFD().valid());
154        fis.close();
155        assertTrue("Returned invalid fd", !fis.getFD().valid());
156    }
157
158    /**
159     * @tests java.io.FileInputStream#read()
160     */
161    public void test_read() throws IOException {
162        InputStreamReader isr = new InputStreamReader(new FileInputStream(fileName));
163        int c = isr.read();
164        isr.close();
165        assertTrue("read returned incorrect char", c == fileString.charAt(0));
166    }
167
168    /**
169     * @tests java.io.FileInputStream#read(byte[])
170     */
171    public void test_read$B() throws IOException {
172        byte[] buf1 = new byte[100];
173        is = new FileInputStream(fileName);
174        is.skip(3000);
175        is.read(buf1);
176        is.close();
177        assertTrue("Failed to read correct data", new String(buf1, 0,
178                buf1.length).equals(fileString.substring(3000, 3100)));
179    }
180
181    /**
182     * @tests java.io.FileInputStream#read(byte[], int, int)
183     */
184    public void test_read$BII() throws IOException {
185        byte[] buf1 = new byte[100];
186        is = new FileInputStream(fileName);
187        is.skip(3000);
188        is.read(buf1, 0, buf1.length);
189        is.close();
190        assertTrue("Failed to read correct data", new String(buf1, 0,
191                buf1.length).equals(fileString.substring(3000, 3100)));
192
193        // Regression test for HARMONY-285
194        File tmpFile = File.createTempFile("FileOutputStream", "tmp");
195        FileInputStream in = new FileInputStream(tmpFile);
196        try {
197            in.read(null, 0, 0);
198            fail("Should throw NullPointerException");
199        } catch (NullPointerException e) {
200            // Expected
201        } finally {
202            in.close();
203            tmpFile.delete();
204        }
205    }
206
207    /**
208     * @tests java.io.FileInputStream#read(byte[], int, int)
209     */
210    public void test_read_$BII_IOException() throws IOException {
211        byte[] buf = new byte[1000];
212        try {
213            is = new FileInputStream(fileName);
214            is.read(buf, -1, 0);
215            fail("should throw IndexOutOfBoundsException");
216        } catch (IndexOutOfBoundsException e) {
217            // Expected
218        } finally {
219            is.close();
220        }
221
222        try {
223            is = new FileInputStream(fileName);
224            is.read(buf, 0, -1);
225            fail("should throw IndexOutOfBoundsException");
226        } catch (IndexOutOfBoundsException e) {
227            // Expected
228        } finally {
229            is.close();
230        }
231
232        try {
233            is = new FileInputStream(fileName);
234            is.read(buf, -1, -1);
235            fail("should throw IndexOutOfBoundsException");
236        } catch (IndexOutOfBoundsException e) {
237            // Expected
238        } finally {
239            is.close();
240        }
241
242        try {
243            is = new FileInputStream(fileName);
244            is.read(buf, 0, 1001);
245            fail("should throw IndexOutOfBoundsException");
246        } catch (IndexOutOfBoundsException e) {
247            // Expected
248        } finally {
249            is.close();
250        }
251
252        try {
253            is = new FileInputStream(fileName);
254            is.read(buf, 1001, 0);
255            fail("should throw IndexOutOfBoundsException");
256        } catch (IndexOutOfBoundsException e) {
257            // Expected
258        } finally {
259            is.close();
260        }
261
262        try {
263            is = new FileInputStream(fileName);
264            is.read(buf, 500, 501);
265            fail("should throw IndexOutOfBoundsException");
266        } catch (IndexOutOfBoundsException e) {
267            // Expected
268        } finally {
269            is.close();
270        }
271
272        try {
273            is = new FileInputStream(fileName);
274            is.close();
275            is.read(buf, 0, 100);
276            fail("should throw IOException");
277        } catch (IOException e) {
278            // Expected
279        } finally {
280            is.close();
281        }
282
283        try {
284            is = new FileInputStream(fileName);
285            is.close();
286            is.read(buf, 0, 0);
287        } finally {
288            is.close();
289        }
290    }
291
292    /**
293     * @tests java.io.FileInputStream#read(byte[], int, int)
294     */
295    public void test_read_$BII_NullPointerException() throws IOException {
296        byte[] buf = null;
297        try {
298            is = new FileInputStream(fileName);
299            is.read(buf, -1, 0);
300            fail("should throw NullPointerException");
301        } catch (NullPointerException e) {
302            // Expected
303        } finally {
304            is.close();
305        }
306    }
307
308    /**
309     * @tests java.io.FileInputStream#read(byte[], int, int)
310     */
311    public void test_read_$BII_IndexOutOfBoundsException() throws IOException {
312        byte[] buf = new byte[1000];
313        try {
314            is = new FileInputStream(fileName);
315            is.close();
316            is.read(buf, -1, -1);
317            fail("should throw IndexOutOfBoundsException");
318        } catch (IndexOutOfBoundsException e) {
319            // Expected
320        } finally {
321            is.close();
322        }
323    }
324
325    /**
326     * @tests java.io.FileInputStream#skip(long)
327     */
328    public void test_skipJ() throws IOException {
329        byte[] buf1 = new byte[10];
330        is = new FileInputStream(fileName);
331        is.skip(1000);
332        is.read(buf1, 0, buf1.length);
333        is.close();
334        assertTrue("Failed to skip to correct position", new String(buf1, 0,
335                buf1.length).equals(fileString.substring(1000, 1010)));
336    }
337
338    /**
339     * @tests java.io.FileInputStream#read(byte[], int, int))
340     */
341    public void test_regressionNNN() throws IOException {
342        // Regression for HARMONY-434
343        FileInputStream fis = new FileInputStream(fileName);
344
345        try {
346            fis.read(new byte[1], -1, 1);
347            fail("IndexOutOfBoundsException must be thrown if off <0");
348        } catch (IndexOutOfBoundsException e) {
349            // Expected
350        }
351
352        try {
353            fis.read(new byte[1], 0, -1);
354            fail("IndexOutOfBoundsException must be thrown if len <0");
355        } catch (IndexOutOfBoundsException e) {
356            // Expected
357        }
358
359        try {
360            fis.read(new byte[1], 0, 5);
361            fail("IndexOutOfBoundsException must be thrown if off+len > b.length");
362        } catch (IndexOutOfBoundsException e) {
363            // Expected
364        }
365
366        try {
367            fis.read(new byte[10], Integer.MAX_VALUE, 5);
368            fail("IndexOutOfBoundsException expected");
369        } catch (IndexOutOfBoundsException e) {
370            // Expected
371        }
372
373        try {
374            fis.read(new byte[10], 5, Integer.MAX_VALUE);
375            fail("IndexOutOfBoundsException expected");
376        } catch (IndexOutOfBoundsException e) {
377            // Expected
378        }
379        fis.close();
380    }
381
382    /**
383     * @tests java.io.FileInputStream#skip(long)
384     */
385    public void test_skipNegativeArgumentJ() throws IOException {
386        FileInputStream fis = new FileInputStream(fileName);
387        try {
388            fis.skip(-5);
389            fail("IOException must be thrown if number of bytes to skip <0");
390        } catch (IOException e) {
391            // Expected IOException
392        } finally {
393            fis.close();
394        }
395    }
396
397    public void test_getChannel() throws Exception {
398        FileInputStream fis = new FileInputStream(fileName);
399        assertEquals(0, fis.getChannel().position());
400        int r;
401        int count = 1;
402        while((r = fis.read()) != -1) {
403            assertEquals(count++, fis.getChannel().position());
404        }
405        fis.close();
406
407        try {
408            fis.getChannel().position();
409            fail("should throw ClosedChannelException");
410        } catch(java.nio.channels.ClosedChannelException e){
411            // Expected
412        }
413
414        fis = new FileInputStream(fileName);
415        assertEquals(0, fis.getChannel().position());
416        byte[] bs = new byte[10];
417        r = fis.read(bs);
418        assertEquals(10, fis.getChannel().position());
419        fis.close();
420
421        fis = new FileInputStream(fileName);
422        assertEquals(0, fis.getChannel().position());
423        bs = new byte[10];
424        fis.skip(100);
425        assertEquals(100, fis.getChannel().position());
426        r = fis.read(bs);
427        assertEquals(110, fis.getChannel().position());
428        fis.close();
429    }
430
431    /**
432     * Sets up the fixture, for example, open a network connection. This method
433     * is called before a test is executed.
434     */
435    protected void setUp() throws IOException {
436        fileName = System.getProperty("user.dir");
437        String separator = System.getProperty("file.separator");
438        if (fileName.charAt(fileName.length() - 1) == separator.charAt(0))
439            fileName = Support_PlatformFile.getNewPlatformFile(fileName,
440                    "input.tst");
441        else
442            fileName = Support_PlatformFile.getNewPlatformFile(fileName
443                    + separator, "input.tst");
444        java.io.OutputStream fos = new java.io.FileOutputStream(fileName);
445        fos.write(fileString.getBytes());
446        fos.close();
447    }
448
449    /**
450     * Tears down the fixture, for example, close a network connection. This
451     * method is called after a test is executed.
452     */
453    protected void tearDown() {
454        new File(fileName).delete();
455    }
456}
457