Skip to content

Commit

Permalink
Upstream ProcessUtilTest, and make a few other files compile on Android.
Browse files Browse the repository at this point in the history
BUG=
TEST=


Review URL: http://codereview.chromium.org/8059007

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@103236 0039d316-1c4b-4281-b951-d872f2087c98
  • Loading branch information
jingzhao@chromium.org committed Sep 29, 2011
1 parent 86e99b8 commit 4d0f93c
Show file tree
Hide file tree
Showing 9 changed files with 80 additions and 26 deletions.
6 changes: 4 additions & 2 deletions base/debug/debugger_posix.cc
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
#include "build/build_config.h"

#include <errno.h>
#include <execinfo.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
Expand All @@ -18,6 +17,10 @@
#include <string>
#include <vector>

#if !defined(OS_ANDROID)
#include <execinfo.h>
#endif

#if defined(__GLIBCXX__)
#include <cxxabi.h>
#endif
Expand Down Expand Up @@ -45,7 +48,6 @@
#endif

#if defined(OS_ANDROID)
#include <execinfo.h>
#include "base/threading/platform_thread.h"
#endif

Expand Down
4 changes: 4 additions & 0 deletions base/dir_reader_posix_unittest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@
#include "base/logging.h"
#include "testing/gtest/include/gtest/gtest.h"

#if defined(OS_ANDROID)
#include "base/os_compat_android.h"
#endif

namespace base {

TEST(DirReaderPosixUnittest, Read) {
Expand Down
7 changes: 5 additions & 2 deletions base/files/file_path_watcher_stub.cc
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,13 @@ namespace {
class FilePathWatcherImpl : public FilePathWatcher::PlatformDelegate {
public:
virtual bool Watch(const FilePath& path,
FileWatcher::Delegate* delegate,
base::MessageLoopProxy*) OVERRIDE {
FilePathWatcher::Delegate* delegate) OVERRIDE {
return false;
}

virtual void Cancel() OVERRIDE {}

virtual void CancelOnMessageLoopThread() OVERRIDE {}
};

} // namespace
Expand Down
2 changes: 1 addition & 1 deletion base/memory/mru_cache_unittest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -267,5 +267,5 @@ TEST(MRUCacheTest, HashingMRUCache) {
EXPECT_EQ(two.value, cache.Get("Second")->second.value);
cache.ShrinkToSize(1);
EXPECT_EQ(two.value, cache.Get("Second")->second.value);
EXPECT_EQ(cache.end(), cache.Get("First"));
EXPECT_TRUE(cache.Get("First") == cache.end());
}
4 changes: 2 additions & 2 deletions base/message_loop.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
#include "base/message_pump_win.h"
#elif defined(OS_POSIX)
#include "base/message_pump_libevent.h"
#if !defined(OS_MACOSX)
#if !defined(OS_MACOSX) && !defined(OS_ANDROID)

#if defined(USE_WAYLAND)
#include "base/message_pump_wayland.h"
Expand Down Expand Up @@ -85,7 +85,7 @@ class BASE_EXPORT MessageLoop : public base::MessagePump::Delegate {
#if defined(OS_WIN)
typedef base::MessagePumpWin::Dispatcher Dispatcher;
typedef base::MessagePumpForUI::Observer Observer;
#elif !defined(OS_MACOSX)
#elif !defined(OS_MACOSX) && !defined(OS_ANDROID)
typedef base::MessagePumpDispatcher Dispatcher;
typedef base::MessagePumpObserver Observer;
#endif
Expand Down
2 changes: 1 addition & 1 deletion base/os_compat_android.cc
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

#include "base/os_compat_android.h"

#include "base/string_util.h"
#include "base/stringprintf.h"

// There is no futimes() avaiable in Bionic, so we provide our own
// implementation until it is there.
Expand Down
8 changes: 4 additions & 4 deletions base/process_util.h
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ BASE_EXPORT void CloseProcessHandle(ProcessHandle process);
// Win XP SP1 as well.
BASE_EXPORT ProcessId GetProcId(ProcessHandle process);

#if defined(OS_LINUX)
#if defined(OS_LINUX) || defined(OS_ANDROID)
// Returns the path to the executable of the given process.
BASE_EXPORT FilePath GetProcessExecutablePath(ProcessHandle process);

Expand All @@ -184,7 +184,7 @@ const int kMaxOomScore = 1000;
// translate the given value into [0, 15]. Some aliasing of values
// may occur in that case, of course.
BASE_EXPORT bool AdjustOOMScore(ProcessId process, int score);
#endif // defined(OS_LINUX)
#endif // defined(OS_LINUX) || defined(OS_ANDROID)

#if defined(OS_POSIX)
// Returns the ID for the parent of the given process.
Expand Down Expand Up @@ -712,7 +712,7 @@ class BASE_EXPORT ProcessMetrics {
DISALLOW_COPY_AND_ASSIGN(ProcessMetrics);
};

#if defined(OS_LINUX)
#if defined(OS_LINUX) || defined(OS_ANDROID)
// Data from /proc/meminfo about system-wide memory consumption.
// Values are in KB.
struct SystemMemoryInfoKB {
Expand All @@ -730,7 +730,7 @@ struct SystemMemoryInfoKB {
// Fills in the provided |meminfo| structure. Returns true on success.
// Exposed for memory debugging widget.
BASE_EXPORT bool GetSystemMemoryInfo(SystemMemoryInfoKB* meminfo);
#endif // defined(OS_LINUX)
#endif // defined(OS_LINUX) || defined(OS_ANDROID)

// Returns the memory committed by the system in KBytes.
// Returns 0 if it can't compute the commit charge.
Expand Down
71 changes: 57 additions & 14 deletions base/process_util_unittest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@
#include <sys/resource.h>
#include <sys/socket.h>
#endif
#if defined(OS_ANDROID)
#include <sys/wait.h>
#endif
#if defined(OS_WIN)
#include <windows.h>
#endif
Expand All @@ -50,6 +53,14 @@ const wchar_t kProcessName[] = L"base_unittests.exe";
const wchar_t kProcessName[] = L"base_unittests";
#endif // defined(OS_WIN)

#if defined(OS_ANDROID)
const char kShellPath[] = "/system/bin/sh";
const char kPosixShell[] = "sh";
#else
const char kShellPath[] = "/bin/sh";
const char kPosixShell[] = "bash";
#endif

const char kSignalFileSlow[] = "SlowChildProcess.die";
const char kSignalFileCrash[] = "CrashingChildProcess.die";
const char kSignalFileKill[] = "KilledChildProcess.die";
Expand Down Expand Up @@ -533,7 +544,7 @@ std::string TestLaunchProcess(const base::environment_vector& env_changes,
std::vector<std::string> args;
base::file_handle_mapping_vector fds_to_remap;

args.push_back("bash");
args.push_back(kPosixShell);
args.push_back("-c");
args.push_back("echo $BASE_TEST");

Expand Down Expand Up @@ -654,6 +665,24 @@ TEST_F(ProcessUtilTest, AlterEnvironment) {

TEST_F(ProcessUtilTest, GetAppOutput) {
std::string output;

#if defined(OS_ANDROID)
std::vector<std::string> argv;
argv.push_back("sh"); // Instead of /bin/sh, force path search to find it.
argv.push_back("-c");

argv.push_back("exit 0");
EXPECT_TRUE(base::GetAppOutput(CommandLine(argv), &output));
EXPECT_STREQ("", output.c_str());

argv[2] = "exit 1";
EXPECT_FALSE(base::GetAppOutput(CommandLine(argv), &output));
EXPECT_STREQ("", output.c_str());

argv[2] = "echo foobar42";
EXPECT_TRUE(base::GetAppOutput(CommandLine(argv), &output));
EXPECT_STREQ("foobar42\n", output.c_str());
#else
EXPECT_TRUE(base::GetAppOutput(CommandLine(FilePath("true")), &output));
EXPECT_STREQ("", output.c_str());

Expand All @@ -665,15 +694,16 @@ TEST_F(ProcessUtilTest, GetAppOutput) {
argv.push_back("foobar42");
EXPECT_TRUE(base::GetAppOutput(CommandLine(argv), &output));
EXPECT_STREQ("foobar42", output.c_str());
#endif // defined(OS_ANDROID)
}

TEST_F(ProcessUtilTest, GetAppOutputRestricted) {
// Unfortunately, since we can't rely on the path, we need to know where
// everything is. So let's use /bin/sh, which is on every POSIX system, and
// its built-ins.
std::vector<std::string> argv;
argv.push_back("/bin/sh"); // argv[0]
argv.push_back("-c"); // argv[1]
argv.push_back(std::string(kShellPath)); // argv[0]
argv.push_back("-c"); // argv[1]

// On success, should set |output|. We use |/bin/sh -c 'exit 0'| instead of
// |true| since the location of the latter may be |/bin| or |/usr/bin| (and we
Expand Down Expand Up @@ -718,18 +748,25 @@ TEST_F(ProcessUtilTest, GetAppOutputRestrictedSIGPIPE) {
std::vector<std::string> argv;
std::string output;

argv.push_back("/bin/sh");
argv.push_back(std::string(kShellPath)); // argv[0]
argv.push_back("-c");
#if defined(OS_ANDROID)
argv.push_back("while echo 12345678901234567890; do :; done");
EXPECT_TRUE(base::GetAppOutputRestricted(CommandLine(argv), &output, 10));
EXPECT_STREQ("1234567890", output.c_str());
#else
argv.push_back("yes");
EXPECT_TRUE(base::GetAppOutputRestricted(CommandLine(argv), &output, 10));
EXPECT_STREQ("y\ny\ny\ny\ny\n", output.c_str());
#endif
}
#endif

TEST_F(ProcessUtilTest, GetAppOutputRestrictedNoZombies) {
std::vector<std::string> argv;
argv.push_back("/bin/sh"); // argv[0]
argv.push_back("-c"); // argv[1]

argv.push_back(std::string(kShellPath)); // argv[0]
argv.push_back("-c"); // argv[1]
argv.push_back("echo 123456789012345678901234567890"); // argv[2]

// Run |GetAppOutputRestricted()| 300 (> default per-user processes on Mac OS
Expand All @@ -753,9 +790,9 @@ TEST_F(ProcessUtilTest, GetAppOutputWithExitCode) {
std::vector<std::string> argv;
std::string output;
int exit_code;
argv.push_back("/bin/sh"); // argv[0]
argv.push_back("-c"); // argv[1]
argv.push_back("echo foo"); // argv[2];
argv.push_back(std::string(kShellPath)); // argv[0]
argv.push_back("-c"); // argv[1]
argv.push_back("echo foo"); // argv[2];
EXPECT_TRUE(base::GetAppOutputWithExitCode(CommandLine(argv), &output,
&exit_code));
EXPECT_STREQ("foo\n", output.c_str());
Expand All @@ -776,7 +813,7 @@ TEST_F(ProcessUtilTest, GetParentProcessId) {
EXPECT_EQ(ppid, getppid());
}

#if defined(OS_LINUX)
#if defined(OS_LINUX) || defined(OS_ANDROID)
TEST_F(ProcessUtilTest, ParseProcStatCPU) {
// /proc/self/stat for a process running "top".
const char kTopStat[] = "960 (top) S 16230 960 16230 34818 960 "
Expand All @@ -796,7 +833,7 @@ TEST_F(ProcessUtilTest, ParseProcStatCPU) {

EXPECT_EQ(0, base::ParseProcStatCPU(kSelfStat));
}
#endif // defined(OS_LINUX)
#endif // defined(OS_LINUX) || defined(OS_ANDROID)

#endif // defined(OS_POSIX)

Expand All @@ -809,6 +846,9 @@ int tc_set_new_mode(int mode);
}
#endif // defined(USE_TCMALLOC)

// Android doesn't implement set_new_handler, so we can't use the
// OutOfMemoryTest cases.
#if !defined(OS_ANDROID)
class OutOfMemoryDeathTest : public testing::Test {
public:
OutOfMemoryDeathTest()
Expand Down Expand Up @@ -913,7 +953,8 @@ TEST_F(OutOfMemoryDeathTest, ViaSharedLibraries) {
}
#endif // OS_LINUX

#if defined(OS_POSIX)
// Android doesn't implement posix_memalign().
#if defined(OS_POSIX) && !defined(OS_ANDROID)
TEST_F(OutOfMemoryDeathTest, Posix_memalign) {
typedef int (*memalign_t)(void **, size_t, size_t);
#if defined(OS_MACOSX)
Expand All @@ -923,7 +964,7 @@ TEST_F(OutOfMemoryDeathTest, Posix_memalign) {
reinterpret_cast<memalign_t>(dlsym(RTLD_DEFAULT, "posix_memalign"));
#else
memalign_t memalign = posix_memalign;
#endif // OS_*
#endif // defined(OS_MACOSX)
if (memalign) {
// Grab the return value of posix_memalign to silence a compiler warning
// about unused return values. We don't actually care about the return
Expand All @@ -934,7 +975,7 @@ TEST_F(OutOfMemoryDeathTest, Posix_memalign) {
}, "");
}
}
#endif // OS_POSIX
#endif // defined(OS_POSIX) && !defined(OS_ANDROID)

#if defined(OS_MACOSX)

Expand Down Expand Up @@ -1040,4 +1081,6 @@ TEST_F(OutOfMemoryDeathTest, PsychoticallyBigObjCObject) {
#endif // !ARCH_CPU_64_BITS
#endif // OS_MACOSX

#endif // !defined(OS_ANDROID)

#endif // !defined(OS_WIN)
2 changes: 2 additions & 0 deletions base/synchronization/cancellation_flag_unittest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,9 @@ class CancelTask : public Task {
public:
explicit CancelTask(CancellationFlag* flag) : flag_(flag) {}
virtual void Run() {
#if GTEST_HAS_DEATH_TEST
ASSERT_DEBUG_DEATH(flag_->Set(), "");
#endif
}
private:
CancellationFlag* flag_;
Expand Down

0 comments on commit 4d0f93c

Please sign in to comment.