2 * Copyright (C) 2007 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package tests.api.java.lang;
19 import dalvik.annotation.BrokenTest;
20 import dalvik.annotation.TestLevel;
21 import dalvik.annotation.TestTargetNew;
22 import dalvik.annotation.TestTargetClass;
24 import junit.framework.TestCase;
26 import java.io.BufferedReader;
28 import java.io.FileInputStream;
29 import java.io.IOException;
30 import java.io.InputStream;
31 import java.io.InputStreamReader;
32 import java.io.OutputStream;
34 @TestTargetClass(Process.class)
35 public class ProcessManagerTest extends TestCase {
38 Process process = null;
39 boolean isThrown = false;
42 level = TestLevel.COMPLETE,
44 method = "getOutputStream",
47 public void testCat() throws IOException, InterruptedException {
48 String[] commands = { "cat" };
49 Process process = Runtime.getRuntime().exec(commands, null, null);
51 OutputStream out = process.getOutputStream();
52 String greeting = "Hello, World!";
53 out.write(greeting.getBytes());
57 assertEquals(greeting, readLine(process));
61 level = TestLevel.COMPLETE,
66 @BrokenTest("Sporadic failures in CTS, but not in CoreTestRunner")
67 public void testSleep() throws IOException {
68 String[] commands = { "sleep", "1" };
69 process = Runtime.getRuntime().exec(commands, null, null);
71 assertEquals(0, process.waitFor());
73 } catch(InterruptedException ie) {
74 fail("InterruptedException was thrown.");
78 thread = new Thread() {
80 String[] commands = { "sleep", "1000"};
82 process = Runtime.getRuntime().exec(commands, null, null);
83 } catch (IOException e1) {
84 fail("IOException was thrown.");
88 fail("InterruptedException was not thrown.");
89 } catch(InterruptedException ie) {
95 Thread interruptThread = new Thread() {
99 } catch(InterruptedException ie) {
100 fail("InterruptedException was thrown in " +
101 "the interruptThread.");
107 interruptThread.start();
109 interruptThread.join();
110 } catch (InterruptedException e) {
111 fail("InterruptedException was thrown.");
115 } catch(InterruptedException ie) {
123 } catch(InterruptedException ie) {
127 assertTrue(isThrown);
131 level = TestLevel.COMPLETE,
133 method = "getInputStream",
136 public void testPwd() throws IOException, InterruptedException {
137 String[] commands = { "sh", "-c", "pwd" };
138 Process process = Runtime.getRuntime().exec(
139 commands, null, new File("/"));
141 assertEquals("/", readLine(process));
145 level = TestLevel.COMPLETE,
147 method = "getInputStream",
150 public void testEnvironment() throws IOException, InterruptedException {
151 String[] commands = { "sh", "-c", "echo $FOO" };
153 // Remember to set the path so we can find sh.
154 String[] environment = { "FOO=foo", "PATH=" + System.getenv("PATH") };
155 Process process = Runtime.getRuntime().exec(
156 commands, environment, null);
158 assertEquals("foo", readLine(process));
161 String readLine(Process process) throws IOException {
162 InputStream in = process.getInputStream();
163 BufferedReader reader = new BufferedReader(new InputStreamReader(in));
164 return reader.readLine();
167 void logErrors(final Process process) throws IOException {
168 Thread thread = new Thread() {
170 InputStream in = process.getErrorStream();
171 BufferedReader reader
172 = new BufferedReader(new InputStreamReader(in));
175 while ((line = reader.readLine()) != null) {
176 System.err.println(line);
178 } catch (IOException e) {
183 thread.setDaemon(true);
188 level = TestLevel.PARTIAL_COMPLETE,
189 notes = "Stress test.",
193 public void testHeavyLoad() {
195 for (i = 0; i < 100; i++)
199 private static void stuff() {
200 Runtime rt = Runtime.getRuntime();
202 Process proc = rt.exec("ls");
205 } catch (Exception ex) {
206 System.err.println("Failure: " + ex);
207 throw new RuntimeException(ex);
216 level = TestLevel.ADDITIONAL,
217 notes = "Check non standard fd behavior",
218 clazz = Runtime.class,
220 args = {String[].class, String[].class, java.io.File.class}
222 public void testCloseNonStandardFds()
223 throws IOException, InterruptedException {
224 String[] commands = { "ls", "/proc/self/fd" };
226 Process process = Runtime.getRuntime().exec(commands, null, null);
227 int before = countLines(process);
230 this.in = new FileInputStream("/proc/version");
233 process = Runtime.getRuntime().exec(commands, null, null);
234 int after = countLines(process);
236 // Assert that the new fd wasn't open in the second run.
237 assertEquals(before, after);
244 * Counts lines of input from the given process. Equivalent to "wc -l".
246 private int countLines(Process process) throws IOException {
248 InputStream in = process.getInputStream();
249 BufferedReader reader = new BufferedReader(new InputStreamReader(in));
251 while (reader.readLine() != null) {
258 level = TestLevel.ADDITIONAL,
259 notes = "Check non standard fd behavior",
260 clazz = Runtime.class,
262 args = {String[].class, String[].class, java.io.File.class}
264 public void testInvalidCommand()
265 throws IOException, InterruptedException {
267 String[] commands = { "doesnotexist" };
268 Runtime.getRuntime().exec(commands, null, null);
269 } catch (IOException e) { /* expected */ }