Home | History | Annotate | Download | only in lang
      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 
     18 package org.apache.harmony.tests.java.lang;
     19 
     20 import java.io.File;
     21 import java.io.IOException;
     22 import java.io.InputStream;
     23 import java.io.OutputStream;
     24 import java.util.ArrayList;
     25 import libcore.io.Libcore;
     26 
     27 public class ProcessTest extends junit.framework.TestCase {
     28   // Test that failures to exec don't leave zombies lying around.
     29   public void test_55017() throws Exception {
     30     ArrayList<Process> children = new ArrayList<Process>();
     31     for (int i = 0; i < 256; ++i) {
     32       try {
     33         children.add(Runtime.getRuntime().exec(new String[] { "/system/bin/does-not-exist" }, null, null));
     34         System.gc();
     35       } catch (IOException expected) {
     36       }
     37     }
     38     assertEquals(0, children.size());
     39 
     40     String pid = Integer.toString(Libcore.os.getpid());
     41     boolean onDevice = new File("/system/bin").exists();
     42     String[] psCommand = onDevice ? new String[] { "ps", "--ppid", pid }
     43                                   : new String[] { "ps", "S", "--ppid", pid };
     44     Process ps = Runtime.getRuntime().exec(psCommand, null, null);
     45     int zombieCount = 0;
     46     for (String line : readAndCloseStream(ps.getInputStream()).split("\n")) {
     47       if (line.contains(" Z ") || line.contains(" Z+ ")) {
     48         ++zombieCount;
     49       }
     50     }
     51     assertEquals(0, zombieCount);
     52   }
     53 
     54   public void test_getOutputStream() throws Exception {
     55     String[] commands = { "cat", "-"};
     56     Process p = Runtime.getRuntime().exec(commands, null, null);
     57     OutputStream os = p.getOutputStream();
     58     // send data, and check if it is echoed back correctly
     59     String str1 = "Some data for testing communication between processes\n";
     60     String str2 = "More data that serves the same purpose.\n";
     61     String str3 = "Here is some more data.\n";
     62     os.write(str1.getBytes());
     63     try {
     64       Thread.sleep(1000);
     65     } catch (InterruptedException e) {
     66       e.printStackTrace();
     67     }
     68     os.write(str2.getBytes());
     69     os.write(str3.getBytes());
     70     os.close();
     71 
     72     String received = readAndCloseStream(p.getInputStream());
     73     assertEquals(str1 + str2 + str3, received);
     74 
     75     String stderr = readAndCloseStream(p.getErrorStream());
     76     assertEquals("", stderr);
     77 
     78     p.waitFor();
     79     p.destroy();
     80   }
     81 
     82   public void test_getErrorStream() throws Exception {
     83     String[] commands = { "cat", "--no-such-option"};
     84     Process p = Runtime.getRuntime().exec(commands, null, null);
     85 
     86     p.getOutputStream().close();
     87 
     88     String received = readAndCloseStream(p.getInputStream());
     89     assertEquals("", received);
     90 
     91     String stderr = readAndCloseStream(p.getErrorStream());
     92     assertTrue(stderr, stderr.contains("no-such-option"));
     93 
     94     p.waitFor();
     95     p.destroy();
     96   }
     97 
     98   private String readAndCloseStream(InputStream is) throws IOException {
     99     StringBuffer result = new StringBuffer();
    100     while (true) {
    101       int c = is.read();
    102       if (c == -1) {
    103         break;
    104       }
    105       result.append((char) c);
    106     }
    107     is.close();
    108     return result.toString();
    109   }
    110 
    111   public void test_exitValue() throws Exception {
    112     String[] commands = { "sh", "-c", "exit 0" };
    113     Process process = Runtime.getRuntime().exec(commands, null, null);
    114     process.waitFor();
    115     assertEquals(0, process.exitValue());
    116 
    117     String[] commandsNonZeroExit = { "sh", "-c", "exit 34" };
    118     process = Runtime.getRuntime().exec(commandsNonZeroExit, null, null);
    119     process.waitFor();
    120     assertEquals(34, process.exitValue());
    121 
    122     String[] commandsSleep = { "sleep", "3000" };
    123     process = Runtime.getRuntime().exec(commandsSleep, null, null);
    124     process.destroy();
    125     process.waitFor(); // destroy is asynchronous.
    126     assertTrue(process.exitValue() != 0);
    127 
    128     process = Runtime.getRuntime().exec(new String[] { "sleep", "3000" }, null, null);
    129     try {
    130       process.exitValue();
    131       fail();
    132     } catch(IllegalThreadStateException expected) {
    133     }
    134   }
    135 
    136   public void test_destroy() throws Exception {
    137     String[] commands = { "sh", "-c", "exit 0"};
    138     Process process = Runtime.getRuntime().exec(commands, null, null);
    139     process.destroy();
    140     process.destroy();
    141     process.destroy();
    142   }
    143 }
    144