Home | History | Annotate | Download | only in quipper
      1 // Copyright 2015 The Chromium OS Authors. All rights reserved.
      2 // Use of this source code is governed by a BSD-style license that can be
      3 // found in the LICENSE file.
      4 
      5 #include "run_command.h"
      6 
      7 #include <vector>
      8 
      9 #include "compat/string.h"
     10 #include "compat/test.h"
     11 
     12 namespace quipper {
     13 
     14 TEST(RunCommandTest, StoresStdout) {
     15   std::vector<char> output;
     16   EXPECT_EQ(0, RunCommand({"/bin/sh", "-c", "echo 'Hello, world!'"}, &output));
     17   string output_str(output.begin(), output.end());
     18   EXPECT_EQ("Hello, world!\n", output_str);
     19 }
     20 
     21 TEST(RunCommandTest, RunsFromPath) {
     22   std::vector<char> output;
     23   EXPECT_EQ(0, RunCommand({"sh", "-c", "echo 'Hello, world!'"}, &output));
     24   string output_str(output.begin(), output.end());
     25   EXPECT_EQ("Hello, world!\n", output_str);
     26 }
     27 
     28 TEST(RunCommandTest, LargeStdout) {
     29   std::vector<char> output;
     30   EXPECT_EQ(0,
     31             RunCommand({"dd", "if=/dev/zero", "bs=5", "count=4096"}, &output));
     32   EXPECT_EQ(5 * 4096, output.size());
     33   EXPECT_EQ('\0', output[0]);
     34   EXPECT_EQ('\0', output[1]);
     35   EXPECT_EQ('\0', *output.rbegin());
     36 }
     37 
     38 TEST(RunCommandTest, StdoutToDevnull) {
     39   EXPECT_EQ(0, RunCommand({"/bin/sh", "-c", "echo 'Hello, world!'"}, nullptr));
     40 }
     41 
     42 TEST(RunCommandTest, StderrIsNotStored) {
     43   std::vector<char> output;
     44   EXPECT_EQ(0,
     45             RunCommand({"/bin/sh", "-c", "echo 'Hello, void!' >&2"}, &output));
     46   EXPECT_EQ(0, output.size());
     47 }
     48 
     49 TEST(RunCommandTest, NoSuchExecutable) {
     50   std::vector<char> output;
     51   int ret = RunCommand({"/doesnt-exist/not-bin/true"}, &output);
     52   int save_errno = errno;
     53   EXPECT_EQ(-1, ret);
     54   EXPECT_EQ(ENOENT, save_errno);
     55 }
     56 
     57 }  // namespace quipper
     58