FileInputStreamTest.java revision 73298fc8f21a2440b4848912e95a072713b93a1a
1/*
2 * Copyright (C) 2010 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package libcore.java.io;
18
19import java.io.File;
20import java.io.FileDescriptor;
21import java.io.FileInputStream;
22import java.io.FileNotFoundException;
23import java.io.FileOutputStream;
24import java.io.IOException;
25import junit.framework.TestCase;
26import libcore.io.IoUtils;
27import libcore.io.Libcore;
28
29public final class FileInputStreamTest extends TestCase {
30    private static final int TOTAL_SIZE = 1024;
31    private static final int SKIP_SIZE = 100;
32
33    private static class DataFeeder extends Thread {
34        private FileDescriptor mOutFd;
35
36        public DataFeeder(FileDescriptor fd) {
37            mOutFd = fd;
38        }
39
40        @Override
41        public void run() {
42            try {
43                FileOutputStream fos = new FileOutputStream(mOutFd);
44                try {
45                    byte[] buffer = new byte[TOTAL_SIZE];
46                    for (int i = 0; i < buffer.length; ++i) {
47                        buffer[i] = (byte) i;
48                    }
49                    fos.write(buffer);
50                } finally {
51                    IoUtils.closeQuietly(fos);
52                    IoUtils.close(mOutFd);
53                }
54            } catch (IOException e) {
55                throw new RuntimeException(e);
56            }
57        }
58    }
59
60    private void verifyData(FileInputStream is, int start, int count) throws IOException {
61        byte buffer[] = new byte[count];
62        assertEquals(count, is.read(buffer));
63        for (int i = 0; i < count; ++i) {
64            assertEquals((byte) (i + start), buffer[i]);
65        }
66    }
67
68    public void testSkipInPipes() throws Exception {
69        FileDescriptor[] pipe = Libcore.os.pipe();
70        DataFeeder feeder = new DataFeeder(pipe[1]);
71        try {
72            feeder.start();
73            FileInputStream fis = new FileInputStream(pipe[0]);
74            fis.skip(SKIP_SIZE);
75            verifyData(fis, SKIP_SIZE, TOTAL_SIZE - SKIP_SIZE);
76            assertEquals(-1, fis.read());
77            feeder.join(1000);
78            assertFalse(feeder.isAlive());
79        } finally {
80            IoUtils.closeQuietly(pipe[0]);
81        }
82    }
83
84    public void testDirectories() throws Exception {
85        try {
86            new FileInputStream(".");
87            fail();
88        } catch (FileNotFoundException expected) {
89        }
90    }
91
92    public void testFileDescriptorOwnership() throws Exception {
93        File tmp = File.createTempFile("FileOutputStreamTest", "tmp");
94        FileOutputStream fos = new FileOutputStream(tmp);
95        fos.write(1);
96        fos.write(1);
97        fos.close();
98
99        FileInputStream fis1 = new FileInputStream(tmp);
100        FileInputStream fis2 = new FileInputStream(fis1.getFD());
101        fis2.close();
102        // The underlying FileDescriptor shouldn't be closed, so this should succeed.
103        assertFalse(fis1.read() == -1);
104        fis1.close();
105        try {
106            // This should fail.
107            fis1.read();
108            fail();
109        } catch (IOException expected) {
110        }
111    }
112}
113