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 17 package org.apache.harmony.tests.java.lang; 18 19 import junit.framework.TestCase; 20 21 import java.io.BufferedReader; 22 import java.io.File; 23 import java.io.FileOutputStream; 24 import java.io.IOException; 25 import java.io.InputStream; 26 import java.io.InputStreamReader; 27 import java.io.OutputStream; 28 29 public class ProcessManagerTest extends TestCase { 30 31 Thread thread = null; 32 Process process = null; 33 boolean isThrown = false; 34 testCat()35 public void testCat() throws IOException, InterruptedException { 36 String[] commands = { "cat" }; 37 Process process = Runtime.getRuntime().exec(commands, null, null); 38 39 OutputStream out = process.getOutputStream(); 40 String greeting = "Hello, World!"; 41 out.write(greeting.getBytes()); 42 out.write('\n'); 43 out.close(); 44 45 assertEquals(greeting, readLine(process)); 46 } 47 48 // BrokenTest: Sporadic failures in CTS, but not in CoreTestRunner testSleep()49 public void testSleep() throws IOException { 50 String[] commands = { "sleep", "1" }; 51 process = Runtime.getRuntime().exec(commands, null, null); 52 try { 53 assertEquals(0, process.waitFor()); 54 55 } catch(InterruptedException ie) { 56 fail("InterruptedException was thrown."); 57 } 58 59 isThrown = false; 60 thread = new Thread() { 61 public void run() { 62 String[] commands = { "sleep", "1000"}; 63 try { 64 process = Runtime.getRuntime().exec(commands, null, null); 65 } catch (IOException e1) { 66 fail("IOException was thrown."); 67 } 68 try { 69 process.waitFor(); 70 fail("InterruptedException was not thrown."); 71 } catch(InterruptedException ie) { 72 isThrown = true; 73 } 74 } 75 }; 76 77 Thread interruptThread = new Thread() { 78 public void run() { 79 try { 80 sleep(10); 81 } catch(InterruptedException ie) { 82 fail("InterruptedException was thrown in " + 83 "the interruptThread."); 84 } 85 thread.interrupt(); 86 } 87 }; 88 thread.start(); 89 interruptThread.start(); 90 try { 91 interruptThread.join(); 92 } catch (InterruptedException e) { 93 fail("InterruptedException was thrown."); 94 } 95 try { 96 Thread.sleep(100); 97 } catch(InterruptedException ie) { 98 99 } 100 101 thread.interrupt(); 102 try { 103 Thread.sleep(100); 104 } catch(InterruptedException ie) { 105 106 } 107 108 assertTrue(isThrown); 109 // Destroy process, otherwise it will run until 1000 seconds 110 // have elapsed but we don't need it again for this test. 111 process.destroy(); 112 } 113 testPwd()114 public void testPwd() throws IOException, InterruptedException { 115 String[] commands = { "sh", "-c", "pwd" }; 116 Process process = Runtime.getRuntime().exec( 117 commands, null, new File("/")); 118 logErrors(process); 119 assertEquals("/", readLine(process)); 120 } 121 testEnvironment()122 public void testEnvironment() throws IOException, InterruptedException { 123 String[] commands = { "sh", "-c", "echo $FOO" }; 124 125 // Remember to set the path so we can find sh. 126 String[] environment = { "FOO=foo", "PATH=" + System.getenv("PATH") }; 127 Process process = Runtime.getRuntime().exec( 128 commands, environment, null); 129 logErrors(process); 130 assertEquals("foo", readLine(process)); 131 } 132 readLine(Process process)133 String readLine(Process process) throws IOException { 134 InputStream in = process.getInputStream(); 135 BufferedReader reader = new BufferedReader(new InputStreamReader(in)); 136 return reader.readLine(); 137 } 138 logErrors(final Process process)139 void logErrors(final Process process) throws IOException { 140 Thread thread = new Thread() { 141 public void run() { 142 InputStream in = process.getErrorStream(); 143 BufferedReader reader 144 = new BufferedReader(new InputStreamReader(in)); 145 String line; 146 try { 147 while ((line = reader.readLine()) != null) { 148 System.err.println(line); 149 } 150 } catch (IOException e) { 151 e.printStackTrace(); 152 } 153 } 154 }; 155 thread.setDaemon(true); 156 thread.start(); 157 } 158 testHeavyLoad()159 public void testHeavyLoad() { 160 int i; 161 for (i = 0; i < 100; i++) 162 stuff(); 163 } 164 stuff()165 private static void stuff() { 166 Runtime rt = Runtime.getRuntime(); 167 try { 168 Process proc = rt.exec("ls"); 169 proc.waitFor(); 170 proc = null; 171 } catch (Exception ex) { 172 System.err.println("Failure: " + ex); 173 throw new RuntimeException(ex); 174 } 175 rt.gc(); 176 rt = null; 177 } 178 179 FileOutputStream out; 180 testCloseNonStandardFds()181 public void testCloseNonStandardFds() 182 throws IOException, InterruptedException { 183 String[] commands = { "ls", "/proc/self/fd" }; 184 185 Process process = Runtime.getRuntime().exec(commands, null, null); 186 int before = countLines(process); 187 188 File tmpFile = File.createTempFile("testCloseNonStandardFds", ".txt"); 189 // Open a new fd. 190 this.out = new FileOutputStream(tmpFile); 191 192 try { 193 process = Runtime.getRuntime().exec(commands, null, null); 194 int after = countLines(process); 195 196 // Assert that the new fd wasn't open in the second run. 197 assertEquals(before, after); 198 } finally { 199 this.out.close(); 200 tmpFile.delete(); 201 } 202 } 203 204 /** 205 * Counts lines of input from the given process. Equivalent to "wc -l". 206 */ countLines(Process process)207 private int countLines(Process process) throws IOException { 208 logErrors(process); 209 InputStream in = process.getInputStream(); 210 BufferedReader reader = new BufferedReader(new InputStreamReader(in)); 211 int count = 0; 212 while (reader.readLine() != null) { 213 count++; 214 } 215 return count; 216 } 217 testInvalidCommand()218 public void testInvalidCommand() 219 throws IOException, InterruptedException { 220 try { 221 String[] commands = { "doesnotexist" }; 222 Runtime.getRuntime().exec(commands, null, null); 223 } catch (IOException e) { /* expected */ } 224 } 225 } 226