ProcessManagerTest.java revision a99b695964e28a5906003d40db48cbd550fbcdf4
1/*
2 * Copyright (C) 2007 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 tests.api.java.lang;
18
19import dalvik.annotation.TestLevel;
20import dalvik.annotation.TestTargetNew;
21import dalvik.annotation.TestTargetClass;
22
23import junit.framework.TestCase;
24
25import java.io.BufferedReader;
26import java.io.File;
27import java.io.FileInputStream;
28import java.io.IOException;
29import java.io.InputStream;
30import java.io.InputStreamReader;
31import java.io.OutputStream;
32
33@TestTargetClass(Process.class)
34public class ProcessManagerTest extends TestCase {
35
36    Thread thread = null;
37    Process process = null;
38    boolean isThrown = false;
39
40    @TestTargetNew(
41        level = TestLevel.COMPLETE,
42        notes = "",
43        method = "getOutputStream",
44        args = {}
45    )
46    public void testCat() throws IOException, InterruptedException {
47        String[] commands = { "cat" };
48        Process process = Runtime.getRuntime().exec(commands, null, null);
49
50        OutputStream out = process.getOutputStream();
51        String greeting = "Hello, World!";
52        out.write(greeting.getBytes());
53        out.write('\n');
54        out.close();
55
56        assertEquals(greeting, readLine(process));
57    }
58
59    @TestTargetNew(
60        level = TestLevel.COMPLETE,
61        notes = "",
62        method = "waitFor",
63        args = {}
64    )
65    public void testSleep() throws IOException {
66        String[] commands = { "sleep", "1" };
67        process = Runtime.getRuntime().exec(commands, null, null);
68        try {
69            assertEquals(0, process.waitFor());
70
71        } catch(InterruptedException ie) {
72            fail("InterruptedException was thrown.");
73        }
74
75        isThrown = false;
76        thread = new Thread() {
77            public void run() {
78                String[] commands = { "sleep", "1000"};
79                try {
80                    process = Runtime.getRuntime().exec(commands, null, null);
81                } catch (IOException e1) {
82                    fail("IOException was thrown.");
83                }
84                try {
85                    process.waitFor();
86                    fail("InterruptedException was not thrown.");
87                } catch(InterruptedException ie) {
88                    isThrown = true;
89                }
90            }
91        };
92
93        Thread interruptThread = new Thread() {
94            public void run() {
95                try {
96                    sleep(10);
97                } catch(InterruptedException ie) {
98                    fail("InterruptedException was thrown in " +
99                            "the interruptThread.");
100                }
101                thread.interrupt();
102            }
103        };
104        thread.start();
105        interruptThread.start();
106        try {
107            interruptThread.join();
108        } catch (InterruptedException e) {
109            fail("InterruptedException was thrown.");
110        }
111        try {
112            Thread.sleep(100);
113        } catch(InterruptedException ie) {
114
115        }
116
117        thread.interrupt();
118        //process.destroy();
119        try {
120            Thread.sleep(100);
121        } catch(InterruptedException ie) {
122
123        }
124
125        assertTrue(isThrown);
126    }
127
128    @TestTargetNew(
129        level = TestLevel.COMPLETE,
130        notes = "",
131        method = "getInputStream",
132        args = {}
133    )
134    public void testPwd() throws IOException, InterruptedException {
135        String[] commands = { "sh", "-c", "pwd" };
136        Process process = Runtime.getRuntime().exec(
137                commands, null, new File("/"));
138        logErrors(process);
139        assertEquals("/", readLine(process));
140    }
141
142    @TestTargetNew(
143        level = TestLevel.COMPLETE,
144        notes = "",
145        method = "getInputStream",
146        args = {}
147    )
148    public void testEnvironment() throws IOException, InterruptedException {
149        String[] commands = { "sh", "-c", "echo $FOO" };
150
151        // Remember to set the path so we can find sh.
152        String[] environment = { "FOO=foo", "PATH=" + System.getenv("PATH") };
153        Process process = Runtime.getRuntime().exec(
154                commands, environment, null);
155        logErrors(process);
156        assertEquals("foo", readLine(process));
157    }
158
159    String readLine(Process process) throws IOException {
160        InputStream in = process.getInputStream();
161        BufferedReader reader = new BufferedReader(new InputStreamReader(in));
162        return reader.readLine();
163    }
164
165    void logErrors(final Process process) throws IOException {
166        Thread thread = new Thread() {
167            public void run() {
168                InputStream in = process.getErrorStream();
169                BufferedReader reader
170                        = new BufferedReader(new InputStreamReader(in));
171                String line;
172                try {
173                    while ((line = reader.readLine()) != null) {
174                        System.err.println(line);
175                    }
176                } catch (IOException e) {
177                    e.printStackTrace();
178                }
179            }
180        };
181        thread.setDaemon(true);
182        thread.start();
183    }
184
185    @TestTargetNew(
186        level = TestLevel.PARTIAL_COMPLETE,
187        notes = "Stress test.",
188        method = "waitFor",
189        args = {}
190    )
191    public void testHeavyLoad() {
192        int i;
193        for (i = 0; i < 2000; i++)
194            stuff();
195    }
196
197    private static void stuff() {
198        Runtime rt = Runtime.getRuntime();
199        try {
200            Process proc = rt.exec("ls");
201            proc.waitFor();
202            proc = null;
203        } catch (Exception ex) {
204            System.err.println("Failure: " + ex);
205            throw new RuntimeException(ex);
206        }
207        rt.gc();
208        rt = null;
209    }
210
211    InputStream in;
212
213    @TestTargetNew(
214        level = TestLevel.ADDITIONAL,
215        notes = "Check non standard fd behavior",
216        clazz = Runtime.class,
217        method = "exec",
218        args = {String[].class, String[].class, java.io.File.class}
219    )
220    public void testCloseNonStandardFds()
221            throws IOException, InterruptedException {
222        String[] commands = { "ls", "/proc/self/fd" };
223
224        Process process = Runtime.getRuntime().exec(commands, null, null);
225        int before = countLines(process);
226
227        // Open a new fd.
228        this.in = new FileInputStream("/proc/version");
229
230        try {
231            process = Runtime.getRuntime().exec(commands, null, null);
232            int after = countLines(process);
233
234            // Assert that the new fd wasn't open in the second run.
235            assertEquals(before, after);
236        } finally {
237            this.in = null;
238        }
239    }
240
241    /**
242     * Counts lines of input from the given process. Equivalent to "wc -l".
243     */
244    private int countLines(Process process) throws IOException {
245        logErrors(process);
246        InputStream in = process.getInputStream();
247        BufferedReader reader = new BufferedReader(new InputStreamReader(in));
248        int count = 0;
249        while (reader.readLine() != null) {
250            count++;
251        }
252        return count;
253    }
254
255    @TestTargetNew(
256        level = TestLevel.ADDITIONAL,
257        notes = "Check non standard fd behavior",
258        clazz = Runtime.class,
259        method = "exec",
260        args = {String[].class, String[].class, java.io.File.class}
261    )
262    public void testInvalidCommand()
263            throws IOException, InterruptedException {
264        try {
265            String[] commands = { "doesnotexist" };
266            Runtime.getRuntime().exec(commands, null, null);
267        } catch (IOException e) { /* expected */ }
268    }
269}
270