blob: 9884be2adbc76959f1fb8119829b5e57a1a2e606 [file] [log] [blame]
Damien Martin-Guillerezf88f4d82015-09-25 13:56:55 +00001// Copyright 2015 The Bazel Authors. All rights reserved.
Han-Wen Nienhuysb0e387b2015-02-12 13:27:06 +00002//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14package com.google.devtools.build.lib.shell;
15
lberkiaea56b32017-05-30 12:35:33 +020016import static com.google.common.truth.Truth.assertThat;
Han-Wen Nienhuysb0e387b2015-02-12 13:27:06 +000017import static org.junit.Assert.fail;
18
Han-Wen Nienhuysb0e387b2015-02-12 13:27:06 +000019import java.io.ByteArrayInputStream;
20import java.io.ByteArrayOutputStream;
21import java.util.Random;
22import java.util.logging.Level;
23import java.util.logging.Logger;
lberkiaea56b32017-05-30 12:35:33 +020024import org.junit.Before;
25import org.junit.Test;
26import org.junit.runner.RunWith;
27import org.junit.runners.JUnit4;
Han-Wen Nienhuysb0e387b2015-02-12 13:27:06 +000028
29/**
30 * Tests the command class with large inputs
31 *
32 */
33@RunWith(JUnit4.class)
34public class CommandLargeInputsTest {
35
36 @Before
Florian Weikert2c71a822015-12-02 17:22:38 +000037 public final void configureLogger() throws Exception {
Han-Wen Nienhuysb0e387b2015-02-12 13:27:06 +000038 // enable all log statements to ensure there are no problems with
39 // logging code
40 Logger.getLogger("com.google.devtools.build.lib.shell.Command").setLevel(Level.FINEST);
41 }
42
43 private byte[] getRandomBytes() {
44 byte[] randomBytes;
45 final Random rand = new Random(0xdeadbeef);
46 randomBytes = new byte[10000];
47 rand.nextBytes(randomBytes);
48 return randomBytes;
49 }
50
51 private byte[] getAllByteValues() {
52 byte[] allByteValues = new byte[Byte.MAX_VALUE - Byte.MIN_VALUE];
53 for(int i = 0; i < allByteValues.length; i++) {
54 allByteValues[i] = (byte) (i + Byte.MIN_VALUE);
55 }
56 return allByteValues;
57 }
58
59 @Test
ulfjackf2d45952017-08-09 15:27:49 +020060 public void testCatRandomBinaryToOutputStream() throws Exception {
Han-Wen Nienhuysb0e387b2015-02-12 13:27:06 +000061 final Command command = new Command(new String[] {"cat"});
62 byte[] randomBytes = getRandomBytes();
ulfjackf2d45952017-08-09 15:27:49 +020063 ByteArrayInputStream in = new ByteArrayInputStream(randomBytes);
64
65 CommandResult result =
66 command.executeAsync(in, Command.KILL_SUBPROCESS_ON_INTERRUPT).get();
lberkiaea56b32017-05-30 12:35:33 +020067 assertThat(result.getTerminationStatus().getRawExitCode()).isEqualTo(0);
Han-Wen Nienhuysb0e387b2015-02-12 13:27:06 +000068 TestUtil.assertArrayEquals(randomBytes, result.getStdout());
lberkiaea56b32017-05-30 12:35:33 +020069 assertThat(result.getStderr()).isEmpty();
Han-Wen Nienhuysb0e387b2015-02-12 13:27:06 +000070 }
71
72 @Test
ulfjackf2d45952017-08-09 15:27:49 +020073 public void testCatRandomBinaryToErrorStream() throws Exception {
74 final Command command = new Command(new String[] {"/bin/sh", "-c", "cat >&2"});
75 byte[] randomBytes = getRandomBytes();
76 ByteArrayInputStream in = new ByteArrayInputStream(randomBytes);
77
78 CommandResult result =
79 command.executeAsync(in, Command.KILL_SUBPROCESS_ON_INTERRUPT).get();
80 assertThat(result.getTerminationStatus().getRawExitCode()).isEqualTo(0);
81 TestUtil.assertArrayEquals(randomBytes, result.getStderr());
82 assertThat(result.getStdout()).isEmpty();
83 }
84
85 @Test
86 public void testCatRandomBinaryFromInputStreamToOutputStream() throws Exception {
Han-Wen Nienhuysb0e387b2015-02-12 13:27:06 +000087 final Command command = new Command(new String[] {"cat"});
88 ByteArrayOutputStream out = new ByteArrayOutputStream();
89 ByteArrayOutputStream err = new ByteArrayOutputStream();
90 byte[] randomBytes = getRandomBytes();
ulfjackf2d45952017-08-09 15:27:49 +020091 ByteArrayInputStream in = new ByteArrayInputStream(randomBytes);
92
93 CommandResult result =
94 command.executeAsync(in, out, err, Command.KILL_SUBPROCESS_ON_INTERRUPT).get();
lberkiaea56b32017-05-30 12:35:33 +020095 assertThat(result.getTerminationStatus().getRawExitCode()).isEqualTo(0);
lberkiaea56b32017-05-30 12:35:33 +020096 assertThat(err.toByteArray()).isEmpty();
ulfjackf2d45952017-08-09 15:27:49 +020097 TestUtil.assertArrayEquals(randomBytes, out.toByteArray());
Han-Wen Nienhuysb0e387b2015-02-12 13:27:06 +000098 assertOutAndErrNotAvailable(result);
99 }
100
101 @Test
ulfjackf2d45952017-08-09 15:27:49 +0200102 public void testCatRandomBinaryFromInputStreamToErrorStream() throws Exception {
Han-Wen Nienhuysb0e387b2015-02-12 13:27:06 +0000103 final Command command = new Command(new String[] {"/bin/sh", "-c", "cat >&2"});
104 ByteArrayOutputStream out = new ByteArrayOutputStream();
105 ByteArrayOutputStream err = new ByteArrayOutputStream();
106 byte[] randomBytes = getRandomBytes();
107 ByteArrayInputStream in = new ByteArrayInputStream(randomBytes);
108
ulfjackf2d45952017-08-09 15:27:49 +0200109 CommandResult result =
110 command.executeAsync(in, out, err, Command.KILL_SUBPROCESS_ON_INTERRUPT).get();
lberkiaea56b32017-05-30 12:35:33 +0200111 assertThat(result.getTerminationStatus().getRawExitCode()).isEqualTo(0);
112 assertThat(out.toByteArray()).isEmpty();
Han-Wen Nienhuysb0e387b2015-02-12 13:27:06 +0000113 TestUtil.assertArrayEquals(randomBytes, err.toByteArray());
114 assertOutAndErrNotAvailable(result);
115 }
116
117 @Test
118 public void testStdoutInterleavedWithStdErr() throws Exception {
119 final Command command = new Command(new String[]{"/bin/bash",
120 "-c", "for i in $( seq 0 999); do (echo OUT$i >&1) && (echo ERR$i >&2); done"
121 });
122 ByteArrayOutputStream out = new ByteArrayOutputStream();
123 ByteArrayOutputStream err = new ByteArrayOutputStream();
ulfjackf2d45952017-08-09 15:27:49 +0200124 command.execute(out, err);
Ulf Adams795895a2015-03-06 15:58:35 +0000125 StringBuilder expectedOut = new StringBuilder();
126 StringBuilder expectedErr = new StringBuilder();
Han-Wen Nienhuysb0e387b2015-02-12 13:27:06 +0000127 for (int i = 0; i < 1000; i++) {
Ulf Adams795895a2015-03-06 15:58:35 +0000128 expectedOut.append("OUT").append(i).append("\n");
129 expectedErr.append("ERR").append(i).append("\n");
Han-Wen Nienhuysb0e387b2015-02-12 13:27:06 +0000130 }
lberkiaea56b32017-05-30 12:35:33 +0200131 assertThat(out.toString("UTF-8")).isEqualTo(expectedOut.toString());
132 assertThat(err.toString("UTF-8")).isEqualTo(expectedErr.toString());
Han-Wen Nienhuysb0e387b2015-02-12 13:27:06 +0000133 }
134
135 private void assertOutAndErrNotAvailable(final CommandResult result) {
136 try {
137 result.getStdout();
138 fail();
139 } catch (IllegalStateException e){}
140 try {
141 result.getStderr();
142 fail();
143 } catch (IllegalStateException e){}
144 }
145
146 @Test
147 public void testCatAllByteValues() throws Exception {
148 final Command command = new Command(new String[] {"cat"});
149 byte[] allByteValues = getAllByteValues();
ulfjackf2d45952017-08-09 15:27:49 +0200150 ByteArrayInputStream in = new ByteArrayInputStream(allByteValues);
151
152 CommandResult result =
153 command.executeAsync(in, Command.KILL_SUBPROCESS_ON_INTERRUPT).get();
lberkiaea56b32017-05-30 12:35:33 +0200154 assertThat(result.getTerminationStatus().getRawExitCode()).isEqualTo(0);
155 assertThat(result.getStderr()).isEmpty();
Han-Wen Nienhuysb0e387b2015-02-12 13:27:06 +0000156 TestUtil.assertArrayEquals(allByteValues, result.getStdout());
157 }
Han-Wen Nienhuysb0e387b2015-02-12 13:27:06 +0000158}