Пример #1
0
 @Test
 public void shouldReturnEchoResult() throws Exception {
   if (SystemUtil.isWindows()) {
     ConsoleResult result = CommandLine.createCommandLine("cmd").runOrBomb(null);
     assertThat(result.outputAsString(), containsString("Windows"));
   } else {
     String expectedValue = "my input";
     ConsoleResult result =
         CommandLine.createCommandLine("echo").withArgs(expectedValue).runOrBomb(null);
     assertThat(result.outputAsString(), is(expectedValue));
   }
 }
Пример #2
0
  @Test
  public void testToStrings() throws Exception {
    final CommandLine cl = CommandLine.createCommandLine(EXEC_WITH_SPACES);

    cl.withArg(ARG_SPACES_NOQUOTES);
    cl.withArg(ARG_NOSPACES);
    cl.withArg(ARG_SPACES);

    final String expectedWithQuotes =
        DBL_QUOTE
            + EXEC_WITH_SPACES
            + DBL_QUOTE
            + " "
            + DBL_QUOTE
            + ARG_SPACES_NOQUOTES
            + DBL_QUOTE
            + " "
            + ARG_NOSPACES
            + " "
            + DBL_QUOTE
            + ARG_SPACES
            + DBL_QUOTE;
    assertEquals(expectedWithQuotes, cl.toString());

    assertEquals(expectedWithQuotes.replaceAll(DBL_QUOTE, ""), cl.toStringForDisplay());

    assertEquals("Did the impl of CommandLine.toString() change?", expectedWithQuotes, cl + "");
  }
Пример #3
0
 @Test
 public void shouldGetTheCommandFromCommandlineAsIs() throws IOException {
   String file = "originalCommand";
   CommandLine command = CommandLine.createCommandLine(file);
   command.setWorkingDir(new File("."));
   String[] commandLineArgs = command.getCommandLine();
   assertThat(commandLineArgs[0], is(file));
 }
Пример #4
0
 @Test
 public void shouldShowPasswordsInToStringForDisplayAsStars() throws IOException {
   CommandLine line =
       CommandLine.createCommandLine("echo")
           .withArg("My Password is:")
           .withArg(new PasswordArgument("secret"));
   assertThat(line.toStringForDisplay(), not(containsString("secret")));
 }
Пример #5
0
 @Test
 public void testToStringMisMatchedQuote() {
   final CommandLine cl2 = CommandLine.createCommandLine(EXEC_WITH_SPACES);
   final String argWithMismatchedDblQuote = "argMisMatch='singlequoted\"WithMismatchedDblQuote'";
   cl2.withArg(argWithMismatchedDblQuote);
   assertEquals(
       "Did behavior of mismatched quotes change? Previously it would truncate args.",
       DBL_QUOTE + EXEC_WITH_SPACES + DBL_QUOTE + " ",
       cl2.toString());
 }
Пример #6
0
  @Test
  public void shouldPrefixStderrOutput() {
    CommandLine line =
        CommandLine.createCommandLine("rmdir").withArg("/a/directory/that/does/not/exist");
    InMemoryStreamConsumer output = new InMemoryStreamConsumer();
    ProcessWrapper processWrapper = line.execute(output, new EnvironmentVariableContext(), null);
    processWrapper.waitForExit();

    assertThat(output.getAllOutput(), containsString("STDERR: "));
  }
Пример #7
0
  @Test
  @RunIf(
      value = EnhancedOSChecker.class,
      arguments = {DO_NOT_RUN_ON, OSChecker.WINDOWS})
  public void shouldBeAbleToSpecifyEncoding() throws IOException {
    String chrisWasHere = "?????";
    CommandLine line =
        CommandLine.createCommandLine("echo").withArg(chrisWasHere).withEncoding("UTF-8");
    InMemoryStreamConsumer output = new InMemoryStreamConsumer();
    ProcessWrapper processWrapper = line.execute(output, new EnvironmentVariableContext(), null);
    processWrapper.waitForExit();

    assertThat(output.getAllOutput(), containsString(chrisWasHere));
  }
Пример #8
0
 @Test
 public void shouldLogPasswordsOnTheLogAsStars() {
   LogFixture logFixture = LogFixture.startListening();
   LogFixture.enableDebug();
   CommandLine line =
       CommandLine.createCommandLine("notexist").withArg(new PasswordArgument("secret"));
   try {
     line.runOrBomb(null);
   } catch (Exception e) {
     // ignored
   }
   assertThat(ArrayUtil.join(logFixture.getMessages()), containsString("notexist ******"));
   logFixture.stopListening();
 }
Пример #9
0
  @Test
  @RunIf(
      value = EnhancedOSChecker.class,
      arguments = {DO_NOT_RUN_ON, OSChecker.WINDOWS})
  public void shouldBeAbleToRunCommandsFromRelativeDirectories() throws IOException {
    File shellScript = new File(tempFolder, "hello-world.sh");
    FileUtil.writeContentToFile("echo ${PWD}", shellScript);
    assertThat(shellScript.setExecutable(true), is(true));

    CommandLine line = CommandLine.createCommandLine("../hello-world.sh").withWorkingDir(subFolder);

    InMemoryStreamConsumer out = new InMemoryStreamConsumer();
    line.execute(out, new EnvironmentVariableContext(), null).waitForExit();

    assertThat(out.getAllOutput().trim(), endsWith("subFolder"));
  }
Пример #10
0
 @Test
 @RunIf(
     value = EnhancedOSChecker.class,
     arguments = {DO_NOT_RUN_ON, OSChecker.WINDOWS})
 public void shouldNotLogPasswordsFromStream() {
   LogFixture logFixture = LogFixture.startListening();
   CommandLine line =
       CommandLine.createCommandLine("/bin/echo")
           .withArg("=>")
           .withArg(new PasswordArgument("secret"));
   line.runOrBomb(null);
   System.out.println(ArrayUtil.join(logFixture.getMessages()));
   assertThat(ArrayUtil.join(logFixture.getMessages()), not(containsString("secret")));
   assertThat(ArrayUtil.join(logFixture.getMessages()), containsString("=> ******"));
   logFixture.stopListening();
 }
Пример #11
0
  @Test
  public void shouldShowPasswordsInDescribeAsStars() throws IOException {
    HashMap<String, String> map = new HashMap<String, String>();
    map.put("password1", "secret");
    map.put("password2", "secret");
    CommandLine line =
        CommandLine.createCommandLine("echo")
            .withArg("My Password is:")
            .withEnv(map)
            .withArg(new PasswordArgument("secret"))
            .withArg(new PasswordArgument("new-pwd"));

    line.addInput(new String[] {"my pwd is: new-pwd "});
    assertThat(line.describe(), not(containsString("secret")));
    assertThat(line.describe(), not(containsString("new-pwd")));
  }
Пример #12
0
  @Test
  @RunIf(
      value = EnhancedOSChecker.class,
      arguments = {DO_NOT_RUN_ON, OSChecker.WINDOWS})
  public void shouldNotRunLocalCommandsThatAreNotExecutable() throws IOException {
    createScript("echo", "echo 'this should not be here'");

    CommandLine line =
        CommandLine.createCommandLine("echo")
            .withArg("Using the REAL echo")
            .withWorkingDir(subFolder);

    InMemoryStreamConsumer out = new InMemoryStreamConsumer();
    line.execute(out, new EnvironmentVariableContext(), null).waitForExit();

    assertThat(out.getAllOutput(), containsString("Using the REAL echo"));
  }
Пример #13
0
  @Test
  @RunIf(
      value = EnhancedOSChecker.class,
      arguments = {DO_NOT_RUN_ON, OSChecker.WINDOWS})
  public void shouldBeAbleToRunCommandsInSubdirectoriesWithNoWorkingDir() throws IOException {

    File shellScript = createScript("hello-world.sh", "echo 'Hello World!'");
    assertThat(shellScript.setExecutable(true), is(true));

    CommandLine line =
        CommandLine.createCommandLine("subFolder/hello-world.sh").withWorkingDir(tempFolder);

    InMemoryStreamConsumer out = new InMemoryStreamConsumer();
    line.execute(out, new EnvironmentVariableContext(), null).waitForExit();

    assertThat(out.getAllOutput(), containsString("Hello World!"));
  }
Пример #14
0
  @Test
  @RunIf(value = OSChecker.class, arguments = OSChecker.WINDOWS)
  public void shouldLogPasswordsOnOutputAsStarsUnderWindows() throws IOException {
    CommandLine line =
        CommandLine.createCommandLine("cmd")
            .withArg("/c")
            .withArg("echo")
            .withArg("My Password is:")
            .withArg(new PasswordArgument("secret"));
    InMemoryStreamConsumer output = new InMemoryStreamConsumer();
    InMemoryStreamConsumer displayOutputStreamConsumer = InMemoryStreamConsumer.inMemoryConsumer();
    ProcessWrapper processWrapper = line.execute(output, new EnvironmentVariableContext(), null);
    processWrapper.waitForExit();

    assertThat(output.getAllOutput(), containsString("secret"));
    assertThat(displayOutputStreamConsumer.getAllOutput(), not(containsString("secret")));
  }
Пример #15
0
  @Test
  @RunIf(
      value = EnhancedOSChecker.class,
      arguments = {DO_NOT_RUN_ON, OSChecker.WINDOWS})
  public void shouldLogPasswordsOnEnvironemntAsStarsUnderLinux() throws IOException {
    CommandLine line =
        CommandLine.createCommandLine("echo")
            .withArg("My Password is:")
            .withArg("secret")
            .withArg(new PasswordArgument("secret"));
    EnvironmentVariableContext environmentVariableContext = new EnvironmentVariableContext();
    environmentVariableContext.setProperty("ENV_PASSWORD", "secret", false);
    InMemoryStreamConsumer output = new InMemoryStreamConsumer();

    InMemoryStreamConsumer forDisplay = InMemoryStreamConsumer.inMemoryConsumer();
    ProcessWrapper processWrapper = line.execute(output, environmentVariableContext, null);
    processWrapper.waitForExit();

    assertThat(forDisplay.getAllOutput(), not(containsString("secret")));
    assertThat(output.getAllOutput(), containsString("secret"));
  }
Пример #16
0
  @Test
  @RunIf(
      value = EnhancedOSChecker.class,
      arguments = {DO_NOT_RUN_ON, OSChecker.WINDOWS})
  public void shouldNotLogPasswordsOnExceptionThrown() throws IOException {
    File dir = FileUtil.createTempFolder();
    File file = new File(dir, "test.sh");
    FileOutputStream out = new FileOutputStream(file);
    out.write("echo $1 && exit 10".getBytes());
    out.close();

    CommandLine line =
        CommandLine.createCommandLine("/bin/sh")
            .withArg(file.getAbsolutePath())
            .withArg(new PasswordArgument("secret"));
    try {
      line.runOrBomb(null);
    } catch (CommandLineException e) {
      assertThat(e.getMessage(), not(containsString("secret")));
    }
  }
Пример #17
0
 @Test(expected = Exception.class)
 public void shouldReturnThrowExceptionWhenCommandNotExist() throws Exception {
   CommandLine.createCommandLine("something").runOrBomb(null);
 }
Пример #18
0
 @Test
 public void shouldReportPasswordsOnTheLogAsStars() {
   CommandLine line =
       CommandLine.createCommandLine("notexist").withArg(new PasswordArgument("secret"));
   assertThat(line.toString(), not(containsString("secret")));
 }