View Javadoc
1   /*
2    * Copyright (C) 2015, Christian Halstrick <christian.halstrick@sap.com>
3    * and other copyright owners as documented in the project's IP log.
4    *
5    * This program and the accompanying materials are made available
6    * under the terms of the Eclipse Distribution License v1.0 which
7    * accompanies this distribution, is reproduced below, and is
8    * available at http://www.eclipse.org/org/documents/edl-v10.php
9    *
10   * All rights reserved.
11   *
12   * Redistribution and use in source and binary forms, with or
13   * without modification, are permitted provided that the following
14   * conditions are met:
15   *
16   * - Redistributions of source code must retain the above copyright
17   *   notice, this list of conditions and the following disclaimer.
18   *
19   * - Redistributions in binary form must reproduce the above
20   *   copyright notice, this list of conditions and the following
21   *   disclaimer in the documentation and/or other materials provided
22   *   with the distribution.
23   *
24   * - Neither the name of the Eclipse Foundation, Inc. nor the
25   *   names of its contributors may be used to endorse or promote
26   *   products derived from this software without specific prior
27   *   written permission.
28   *
29   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
30   * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
31   * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
32   * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
33   * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
34   * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
35   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
36   * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
37   * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
38   * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
39   * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
40   * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
41   * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
42   */
43  
44  package org.eclipse.jgit.util;
45  
46  import static org.junit.Assert.assertEquals;
47  
48  import java.io.ByteArrayInputStream;
49  import java.io.ByteArrayOutputStream;
50  import java.io.File;
51  import java.io.IOException;
52  import java.io.InputStream;
53  
54  import org.eclipse.jgit.junit.JGitTestUtil;
55  import org.eclipse.jgit.util.FS.ExecutionResult;
56  import org.junit.Before;
57  import org.junit.Test;
58  
59  public class RunExternalScriptTest {
60  	private static final String LF = "\n";
61  
62  	private ByteArrayOutputStream out;
63  
64  	private ByteArrayOutputStream err;
65  
66  	@Before
67  	public void setUp() throws Exception {
68  		out = new ByteArrayOutputStream();
69  		err = new ByteArrayOutputStream();
70  	}
71  
72  	@Test
73  	public void testCopyStdIn() throws IOException, InterruptedException {
74  		String inputStr = "a\nb\rc\r\nd";
75  		File script = writeTempFile("cat -");
76  		int rc = FS.DETECTED.runProcess(
77  				new ProcessBuilder("sh", script.getPath()), out, err,
78  				new ByteArrayInputStream(inputStr.getBytes()));
79  		assertEquals(0, rc);
80  		assertEquals(inputStr, new String(out.toByteArray()));
81  		assertEquals("", new String(err.toByteArray()));
82  	}
83  
84  	@Test
85  	public void testCopyNullStdIn() throws IOException, InterruptedException {
86  		File script = writeTempFile("cat -");
87  		int rc = FS.DETECTED.runProcess(
88  				new ProcessBuilder("sh", script.getPath()), out, err,
89  				(InputStream) null);
90  		assertEquals(0, rc);
91  		assertEquals("", new String(out.toByteArray()));
92  		assertEquals("", new String(err.toByteArray()));
93  	}
94  
95  	@Test
96  	public void testArguments() throws IOException, InterruptedException {
97  		File script = writeTempFile("echo $#,$1,$2,$3,$4,$5,$6");
98  		int rc = FS.DETECTED.runProcess(
99  				new ProcessBuilder("sh",
100 				script.getPath(), "a", "b", "c"), out, err, (InputStream) null);
101 		assertEquals(0, rc);
102 		assertEquals("3,a,b,c,,,\n", new String(out.toByteArray()));
103 		assertEquals("", new String(err.toByteArray()));
104 	}
105 
106 	@Test
107 	public void testRc() throws IOException, InterruptedException {
108 		File script = writeTempFile("exit 3");
109 		int rc = FS.DETECTED.runProcess(
110 				new ProcessBuilder("sh", script.getPath(), "a", "b", "c"),
111 				out, err, (InputStream) null);
112 		assertEquals(3, rc);
113 		assertEquals("", new String(out.toByteArray()));
114 		assertEquals("", new String(err.toByteArray()));
115 	}
116 
117 	@Test
118 	public void testNullStdout() throws IOException, InterruptedException {
119 		File script = writeTempFile("echo hi");
120 		int rc = FS.DETECTED.runProcess(
121 				new ProcessBuilder("sh", script.getPath()), null, err,
122 				(InputStream) null);
123 		assertEquals(0, rc);
124 		assertEquals("", new String(out.toByteArray()));
125 		assertEquals("", new String(err.toByteArray()));
126 	}
127 
128 	@Test
129 	public void testStdErr() throws IOException, InterruptedException {
130 		File script = writeTempFile("echo hi >&2");
131 		int rc = FS.DETECTED.runProcess(
132 				new ProcessBuilder("sh", script.getPath()), null, err,
133 				(InputStream) null);
134 		assertEquals(0, rc);
135 		assertEquals("", new String(out.toByteArray()));
136 		assertEquals("hi" + LF, new String(err.toByteArray()));
137 	}
138 
139 	@Test
140 	public void testAllTogetherBin() throws IOException, InterruptedException {
141 		String inputStr = "a\nb\rc\r\nd";
142 		File script = writeTempFile("echo $#,$1,$2,$3,$4,$5,$6 >&2 ; cat -; exit 5");
143 		int rc = FS.DETECTED.runProcess(
144 				new ProcessBuilder("sh", script.getPath(), "a", "b", "c"),
145 				out, err, new ByteArrayInputStream(inputStr.getBytes()));
146 		assertEquals(5, rc);
147 		assertEquals(inputStr, new String(out.toByteArray()));
148 		assertEquals("3,a,b,c,,," + LF, new String(err.toByteArray()));
149 	}
150 
151 	@Test(expected = IOException.class)
152 	public void testWrongSh() throws IOException, InterruptedException {
153 		File script = writeTempFile("cat -");
154 		FS.DETECTED.runProcess(
155 				new ProcessBuilder("/bin/sh-foo", script.getPath(), "a", "b",
156 						"c"), out, err, (InputStream) null);
157 	}
158 
159 	@Test
160 	public void testWrongScript() throws IOException, InterruptedException {
161 		File script = writeTempFile("cat-foo -");
162 		int rc = FS.DETECTED.runProcess(
163 				new ProcessBuilder("sh", script.getPath(), "a", "b", "c"),
164 				out, err, (InputStream) null);
165 		assertEquals(127, rc);
166 	}
167 
168 	@Test
169 	public void testCopyStdInExecute()
170 			throws IOException, InterruptedException {
171 		String inputStr = "a\nb\rc\r\nd";
172 		File script = writeTempFile("cat -");
173 		ProcessBuilder pb = new ProcessBuilder("sh", script.getPath());
174 		ExecutionResult res = FS.DETECTED.execute(pb,
175 				new ByteArrayInputStream(inputStr.getBytes()));
176 		assertEquals(0, res.getRc());
177 		assertEquals(inputStr, new String(res.getStdout().toByteArray()));
178 		assertEquals("", new String(res.getStderr().toByteArray()));
179 	}
180 
181 	@Test
182 	public void testStdErrExecute() throws IOException, InterruptedException {
183 		File script = writeTempFile("echo hi >&2");
184 		ProcessBuilder pb = new ProcessBuilder("sh", script.getPath());
185 		ExecutionResult res = FS.DETECTED.execute(pb, null);
186 		assertEquals(0, res.getRc());
187 		assertEquals("", new String(res.getStdout().toByteArray()));
188 		assertEquals("hi" + LF, new String(res.getStderr().toByteArray()));
189 	}
190 
191 	@Test
192 	public void testAllTogetherBinExecute()
193 			throws IOException, InterruptedException {
194 		String inputStr = "a\nb\rc\r\nd";
195 		File script = writeTempFile(
196 				"echo $#,$1,$2,$3,$4,$5,$6 >&2 ; cat -; exit 5");
197 		ProcessBuilder pb = new ProcessBuilder("sh", script.getPath(), "a",
198 				"b", "c");
199 		ExecutionResult res = FS.DETECTED.execute(pb,
200 				new ByteArrayInputStream(inputStr.getBytes()));
201 		assertEquals(5, res.getRc());
202 		assertEquals(inputStr, new String(res.getStdout().toByteArray()));
203 		assertEquals("3,a,b,c,,," + LF,
204 				new String(res.getStderr().toByteArray()));
205 	}
206 
207 	private File writeTempFile(String body) throws IOException {
208 		File f = File.createTempFile("RunProcessTestScript_", "");
209 		JGitTestUtil.write(f, body);
210 		return f;
211 	}
212 }