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 java.nio.charset.StandardCharsets.UTF_8;
47  import static org.junit.Assert.assertEquals;
48  
49  import java.io.ByteArrayInputStream;
50  import java.io.ByteArrayOutputStream;
51  import java.io.File;
52  import java.io.IOException;
53  import java.io.InputStream;
54  
55  import org.eclipse.jgit.junit.JGitTestUtil;
56  import org.eclipse.jgit.util.FS.ExecutionResult;
57  import org.junit.Before;
58  import org.junit.Test;
59  
60  public class RunExternalScriptTest {
61  	private static final String LF = "\n";
62  
63  	private ByteArrayOutputStream out;
64  
65  	private ByteArrayOutputStream err;
66  
67  	@Before
68  	public void setUp() throws Exception {
69  		out = new ByteArrayOutputStream();
70  		err = new ByteArrayOutputStream();
71  	}
72  
73  	@Test
74  	public void testCopyStdIn() throws IOException, InterruptedException {
75  		String inputStr = "a\nb\rc\r\nd";
76  		File script = writeTempFile("cat -");
77  		int rc = FS.DETECTED.runProcess(
78  				new ProcessBuilder("sh", script.getPath()), out, err,
79  				new ByteArrayInputStream(inputStr.getBytes(UTF_8)));
80  		assertEquals(0, rc);
81  		assertEquals(inputStr, new String(out.toByteArray(), UTF_8));
82  		assertEquals("", new String(err.toByteArray(), UTF_8));
83  	}
84  
85  	@Test
86  	public void testCopyNullStdIn() throws IOException, InterruptedException {
87  		File script = writeTempFile("cat -");
88  		int rc = FS.DETECTED.runProcess(
89  				new ProcessBuilder("sh", script.getPath()), out, err,
90  				(InputStream) null);
91  		assertEquals(0, rc);
92  		assertEquals("", new String(out.toByteArray(), UTF_8));
93  		assertEquals("", new String(err.toByteArray(), UTF_8));
94  	}
95  
96  	@Test
97  	public void testArguments() throws IOException, InterruptedException {
98  		File script = writeTempFile("echo $#,$1,$2,$3,$4,$5,$6");
99  		int rc = FS.DETECTED.runProcess(
100 				new ProcessBuilder("sh",
101 				script.getPath(), "a", "b", "c"), out, err, (InputStream) null);
102 		assertEquals(0, rc);
103 		assertEquals("3,a,b,c,,,\n", new String(out.toByteArray(), UTF_8));
104 		assertEquals("", new String(err.toByteArray(), UTF_8));
105 	}
106 
107 	@Test
108 	public void testRc() throws IOException, InterruptedException {
109 		File script = writeTempFile("exit 3");
110 		int rc = FS.DETECTED.runProcess(
111 				new ProcessBuilder("sh", script.getPath(), "a", "b", "c"),
112 				out, err, (InputStream) null);
113 		assertEquals(3, rc);
114 		assertEquals("", new String(out.toByteArray(), UTF_8));
115 		assertEquals("", new String(err.toByteArray(), UTF_8));
116 	}
117 
118 	@Test
119 	public void testNullStdout() throws IOException, InterruptedException {
120 		File script = writeTempFile("echo hi");
121 		int rc = FS.DETECTED.runProcess(
122 				new ProcessBuilder("sh", script.getPath()), null, err,
123 				(InputStream) null);
124 		assertEquals(0, rc);
125 		assertEquals("", new String(out.toByteArray(), UTF_8));
126 		assertEquals("", new String(err.toByteArray(), UTF_8));
127 	}
128 
129 	@Test
130 	public void testStdErr() throws IOException, InterruptedException {
131 		File script = writeTempFile("echo hi >&2");
132 		int rc = FS.DETECTED.runProcess(
133 				new ProcessBuilder("sh", script.getPath()), null, err,
134 				(InputStream) null);
135 		assertEquals(0, rc);
136 		assertEquals("", new String(out.toByteArray(), UTF_8));
137 		assertEquals("hi" + LF, new String(err.toByteArray(), UTF_8));
138 	}
139 
140 	@Test
141 	public void testAllTogetherBin() throws IOException, InterruptedException {
142 		String inputStr = "a\nb\rc\r\nd";
143 		File script = writeTempFile("echo $#,$1,$2,$3,$4,$5,$6 >&2 ; cat -; exit 5");
144 		int rc = FS.DETECTED.runProcess(
145 				new ProcessBuilder("sh", script.getPath(), "a", "b", "c"),
146 				out, err, new ByteArrayInputStream(inputStr.getBytes(UTF_8)));
147 		assertEquals(5, rc);
148 		assertEquals(inputStr, new String(out.toByteArray(), UTF_8));
149 		assertEquals("3,a,b,c,,," + LF, new String(err.toByteArray(), UTF_8));
150 	}
151 
152 	@Test(expected = IOException.class)
153 	public void testWrongSh() throws IOException, InterruptedException {
154 		File script = writeTempFile("cat -");
155 		FS.DETECTED.runProcess(
156 				new ProcessBuilder("/bin/sh-foo", script.getPath(), "a", "b",
157 						"c"), out, err, (InputStream) null);
158 	}
159 
160 	@Test
161 	public void testWrongScript() throws IOException, InterruptedException {
162 		File script = writeTempFile("cat-foo -");
163 		int rc = FS.DETECTED.runProcess(
164 				new ProcessBuilder("sh", script.getPath(), "a", "b", "c"),
165 				out, err, (InputStream) null);
166 		assertEquals(127, rc);
167 	}
168 
169 	@Test
170 	public void testCopyStdInExecute()
171 			throws IOException, InterruptedException {
172 		String inputStr = "a\nb\rc\r\nd";
173 		File script = writeTempFile("cat -");
174 		ProcessBuilder pb = new ProcessBuilder("sh", script.getPath());
175 		ExecutionResult res = FS.DETECTED.execute(pb,
176 				new ByteArrayInputStream(inputStr.getBytes(UTF_8)));
177 		assertEquals(0, res.getRc());
178 		assertEquals(inputStr,
179 				new String(res.getStdout().toByteArray(), UTF_8));
180 		assertEquals("", new String(res.getStderr().toByteArray(), UTF_8));
181 	}
182 
183 	@Test
184 	public void testStdErrExecute() throws IOException, InterruptedException {
185 		File script = writeTempFile("echo hi >&2");
186 		ProcessBuilder pb = new ProcessBuilder("sh", script.getPath());
187 		ExecutionResult res = FS.DETECTED.execute(pb, null);
188 		assertEquals(0, res.getRc());
189 		assertEquals("", new String(res.getStdout().toByteArray(), UTF_8));
190 		assertEquals("hi" + LF,
191 				new String(res.getStderr().toByteArray(), UTF_8));
192 	}
193 
194 	@Test
195 	public void testAllTogetherBinExecute()
196 			throws IOException, InterruptedException {
197 		String inputStr = "a\nb\rc\r\nd";
198 		File script = writeTempFile(
199 				"echo $#,$1,$2,$3,$4,$5,$6 >&2 ; cat -; exit 5");
200 		ProcessBuilder pb = new ProcessBuilder("sh", script.getPath(), "a",
201 				"b", "c");
202 		ExecutionResult res = FS.DETECTED.execute(pb,
203 				new ByteArrayInputStream(inputStr.getBytes(UTF_8)));
204 		assertEquals(5, res.getRc());
205 		assertEquals(inputStr,
206 				new String(res.getStdout().toByteArray(), UTF_8));
207 		assertEquals("3,a,b,c,,," + LF,
208 				new String(res.getStderr().toByteArray(), UTF_8));
209 	}
210 
211 	private File writeTempFile(String body) throws IOException {
212 		File f = File.createTempFile("RunProcessTestScript_", "");
213 		JGitTestUtil.write(f, body);
214 		return f;
215 	}
216 }