summaryrefslogtreecommitdiffstats
path: root/chrome/common
diff options
context:
space:
mode:
authorjeremy@chromium.org <jeremy@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2008-12-09 22:00:47 +0000
committerjeremy@chromium.org <jeremy@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2008-12-09 22:00:47 +0000
commit95cb7fb94d22b073291ee45bf10ea880a4d93b33 (patch)
tree073bb49e20a94846f46106557904de2715c787c7 /chrome/common
parent767cc7f25de0c9be199d1f4f267e569ff6788119 (diff)
downloadchromium_src-95cb7fb94d22b073291ee45bf10ea880a4d93b33.zip
chromium_src-95cb7fb94d22b073291ee45bf10ea880a4d93b33.tar.gz
chromium_src-95cb7fb94d22b073291ee45bf10ea880a4d93b33.tar.bz2
Bring up ipc tests on POSIX.
Review URL: http://codereview.chromium.org/13158 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@6634 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'chrome/common')
-rw-r--r--chrome/common/ipc_fuzzing_tests.cc29
-rw-r--r--chrome/common/ipc_tests.cc129
-rw-r--r--chrome/common/ipc_tests.h23
3 files changed, 91 insertions, 90 deletions
diff --git a/chrome/common/ipc_fuzzing_tests.cc b/chrome/common/ipc_fuzzing_tests.cc
index 9216df9..9a825c9 100644
--- a/chrome/common/ipc_fuzzing_tests.cc
+++ b/chrome/common/ipc_fuzzing_tests.cc
@@ -15,6 +15,7 @@
#include "chrome/common/ipc_channel_proxy.h"
#include "chrome/common/ipc_message_utils.h"
#include "testing/gtest/include/gtest/gtest.h"
+#include "testing/multiprocess_func_list.h"
TEST(IPCMessageIntegrity, ReadBeyondBufferStr) {
//This was BUG 984408.
@@ -264,18 +265,24 @@ class FuzzerClientListener : public SimpleListener {
IPC::Message* last_msg_;
};
-bool RunFuzzServer() {
+// Runs the fuzzing server child mode. Returns when the preset number
+// of messages have been received.
+MULTIPROCESS_TEST_MAIN(RunFuzzServer) {
+ MessageLoopForIO main_message_loop;
FuzzerServerListener listener;
IPC::Channel chan(kFuzzerChannel, IPC::Channel::MODE_SERVER, &listener);
chan.Connect();
listener.Init(&chan);
MessageLoop::current()->Run();
- return true;
+ return 0;
}
+class IPCFuzzingTest : public IPCChannelTest {
+};
+
// This test makes sure that the FuzzerClientListener and FuzzerServerListener
// are working properly by generating two well formed IPC calls.
-TEST(IPCFuzzingTest, SanityTest) {
+TEST_F(IPCFuzzingTest, SanityTest) {
base::ProcessHandle server_process = SpawnChild(FUZZER_SERVER);
ASSERT_TRUE(server_process);
PlatformThread::Sleep(1000);
@@ -304,7 +311,7 @@ TEST(IPCFuzzingTest, SanityTest) {
// after we generate another valid IPC to make sure framing is working
// properly.
#ifdef NDEBUG
-TEST(IPCFuzzingTest, MsgBadPayloadShort) {
+TEST_F(IPCFuzzingTest, MsgBadPayloadShort) {
base::ProcessHandle server_process = SpawnChild(FUZZER_SERVER);
ASSERT_TRUE(server_process);
::Sleep(1000);
@@ -328,12 +335,12 @@ TEST(IPCFuzzingTest, MsgBadPayloadShort) {
}
#endif // NDEBUG
-// This test uses a payload that has the wrong arguments, but so the payload
+// This test uses a payload that has too many arguments, but so the payload
// size is big enough so the unpacking routine does not generate an error as
// in the case of MsgBadPayloadShort test.
// This test does not pinpoint a flaw (per se) as by design we don't carry
// type information on the IPC message.
-TEST(IPCFuzzingTest, MsgBadPayloadArgs) {
+TEST_F(IPCFuzzingTest, MsgBadPayloadArgs) {
base::ProcessHandle server_process = SpawnChild(FUZZER_SERVER);
ASSERT_TRUE(server_process);
PlatformThread::Sleep(1000);
@@ -345,13 +352,15 @@ TEST(IPCFuzzingTest, MsgBadPayloadArgs) {
IPC::Message* msg = new IPC::Message(MSG_ROUTING_CONTROL, MsgClassSI::ID,
IPC::Message::PRIORITY_NORMAL);
- msg->WriteInt(2);
- msg->WriteInt(0x64);
+ msg->WriteWString(L"d");
msg->WriteInt(0);
- msg->WriteInt(0x65);
+ msg->WriteInt(0x65); // Extra argument.
+
chan.Send(msg);
EXPECT_TRUE(listener.ExpectMessage(0, MsgClassSI::ID));
+ // Now send a well formed message to make sure the receiver wasn't
+ // thrown out of sync by the extra argument.
msg = new MsgClassIS(3, L"expect three");
chan.Send(msg);
EXPECT_TRUE(listener.ExpectMessage(3, MsgClassIS::ID));
@@ -387,7 +396,7 @@ class ServerMacroExTest {
int unhandled_msgs_;
};
-TEST(IPCFuzzingTest, MsgMapExMacro) {
+TEST_F(IPCFuzzingTest, MsgMapExMacro) {
IPC::Message* msg = NULL;
ServerMacroExTest server;
diff --git a/chrome/common/ipc_tests.cc b/chrome/common/ipc_tests.cc
index e0c14c8..444824e 100644
--- a/chrome/common/ipc_tests.cc
+++ b/chrome/common/ipc_tests.cc
@@ -18,12 +18,13 @@
#include "base/perftimer.h"
#include "base/process_util.h"
#include "base/scoped_nsautorelease_pool.h"
+#include "base/test_suite.h"
#include "base/thread.h"
#include "chrome/common/chrome_switches.h"
#include "chrome/common/ipc_channel.h"
#include "chrome/common/ipc_channel_proxy.h"
#include "chrome/common/ipc_message_utils.h"
-#include "testing/gtest/include/gtest/gtest.h"
+#include "testing/multiprocess_func_list.h"
// Define to enable IPC performance testing instead of the regular unit tests
// #define PERFORMANCE_TEST
@@ -32,13 +33,43 @@ const wchar_t kTestClientChannel[] = L"T1";
const wchar_t kReflectorChannel[] = L"T2";
const wchar_t kFuzzerChannel[] = L"F3";
-const wchar_t kChild[] = L"child";
-const wchar_t kReflector[] = L"reflector";
-const wchar_t kFuzzer[] = L"fuzzer";
-
#ifndef PERFORMANCE_TEST
-TEST(IPCChannelTest, BasicMessageTest) {
+void IPCChannelTest::SetUp() {
+ MultiProcessTest::SetUp();
+
+ // Construct a fresh IO Message loop for the duration of each test.
+ message_loop_ = new MessageLoopForIO();
+}
+
+void IPCChannelTest::TearDown() {
+ delete message_loop_;
+ message_loop_ = NULL;
+
+ MultiProcessTest::TearDown();
+}
+
+base::ProcessHandle IPCChannelTest::SpawnChild(ChildType child_type) {
+ // kDebugChildren support.
+ bool debug_on_start = CommandLine().HasSwitch(switches::kDebugChildren);
+
+ switch (child_type) {
+ case TEST_CLIENT:
+ return MultiProcessTest::SpawnChild(L"RunTestClient", debug_on_start);
+ break;
+ case TEST_REFLECTOR:
+ return MultiProcessTest::SpawnChild(L"RunReflector", debug_on_start);
+ break;
+ case FUZZER_SERVER:
+ return MultiProcessTest::SpawnChild(L"RunFuzzServer", debug_on_start);
+ break;
+ default:
+ return NULL;
+ break;
+ }
+}
+
+TEST_F(IPCChannelTest, BasicMessageTest) {
int v1 = 10;
std::string v2("foobar");
std::wstring v3(L"hello world");
@@ -94,7 +125,6 @@ class MyChannelListener : public IPC::Channel::Listener {
iter.NextInt();
const std::string data = iter.NextString();
-
if (--messages_left_ == 0) {
MessageLoop::current()->Quit();
} else {
@@ -119,9 +149,7 @@ class MyChannelListener : public IPC::Channel::Listener {
};
static MyChannelListener channel_listener;
-// TODO(playmobil): Implement
-#if defined(OS_WIN)
-TEST(IPCChannelTest, ChannelTest) {
+TEST_F(IPCChannelTest, ChannelTest) {
// setup IPC channel
IPC::Channel chan(kTestClientChannel, IPC::Channel::MODE_SERVER,
&channel_listener);
@@ -129,7 +157,7 @@ TEST(IPCChannelTest, ChannelTest) {
channel_listener.Init(&chan);
- HANDLE process_handle = SpawnChild(TEST_CLIENT);
+ base::ProcessHandle process_handle = SpawnChild(TEST_CLIENT);
ASSERT_TRUE(process_handle);
Send(&chan, "hello from parent");
@@ -138,11 +166,12 @@ TEST(IPCChannelTest, ChannelTest) {
MessageLoop::current()->Run();
// cleanup child process
- WaitForSingleObject(process_handle, 5000);
- CloseHandle(process_handle);
+ EXPECT_TRUE(base::WaitForSingleProcess(process_handle, 5000));
}
-TEST(IPCChannelTest, ChannelProxyTest) {
+// TODO(playmobil): Implement
+#if defined(OS_WIN)
+TEST_F(IPCChannelTest, ChannelProxyTest) {
// The thread needs to out-live the ChannelProxy.
base::Thread thread("ChannelProxyTestServer");
base::Thread::Options options;
@@ -171,7 +200,9 @@ TEST(IPCChannelTest, ChannelProxyTest) {
}
#endif // defined(OS_WIN)
-static bool RunTestClient() {
+MULTIPROCESS_TEST_MAIN(RunTestClient) {
+ MessageLoopForIO main_message_loop;
+
// setup IPC channel
IPC::Channel chan(kTestClientChannel, IPC::Channel::MODE_CLIENT,
&channel_listener);
@@ -180,7 +211,8 @@ static bool RunTestClient() {
Send(&chan, "hello from child");
// run message loop
MessageLoop::current()->Run();
- return true;
+ // return true;
+ return NULL;
}
#endif // !PERFORMANCE_TEST
@@ -204,7 +236,7 @@ static bool RunTestClient() {
// "quit" is sent, it will exit.
class ChannelReflectorListener : public IPC::Channel::Listener {
public:
- ChannelReflectorListener(IPC::Channel *channel) :
+ explicit ChannelReflectorListener(IPC::Channel *channel) :
channel_(channel),
count_messages_(0),
latency_messages_(0) {
@@ -301,7 +333,7 @@ class ChannelPerfListener : public IPC::Channel::Listener {
int latency_messages_;
};
-TEST(IPCChannelTest, Performance) {
+TEST_F(IPCChannelTest, Performance) {
// setup IPC channel
IPC::Channel chan(kReflectorChannel, IPC::Channel::MODE_SERVER, NULL);
ChannelPerfListener perf_listener(&chan, 10000, 100000);
@@ -333,7 +365,8 @@ TEST(IPCChannelTest, Performance) {
}
// This message loop bounces all messages back to the sender
-static bool RunReflector() {
+MULTIPROCESS_TEST_MAIN(RunReflector) {
+ MessageLoopForIO main_message_loop;
IPC::Channel chan(kReflectorChannel, IPC::Channel::MODE_CLIENT, NULL);
ChannelReflectorListener channel_reflector_listener(&chan);
chan.set_listener(&channel_reflector_listener);
@@ -345,7 +378,6 @@ static bool RunReflector() {
#endif // PERFORMANCE_TEST
-// TODO(playmobil): Implement
#if defined(OS_WIN)
// All fatal log messages (e.g. DCHECK failures) imply unit test failures
static void IPCTestAssertHandler(const std::string& str) {
@@ -362,54 +394,13 @@ static void SuppressErrorDialogs() {
UINT existing_flags = SetErrorMode(new_flags);
SetErrorMode(existing_flags | new_flags);
}
-
-HANDLE SpawnChild(ChildType child_type) {
- // spawn child process
- std::wstring cl(GetCommandLineW());
- switch(child_type) {
- case TEST_CLIENT:
- CommandLine::AppendSwitch(&cl, kChild);
- break;
- case TEST_REFLECTOR:
- CommandLine::AppendSwitch(&cl, kReflector);
- break;
- case FUZZER_SERVER:
- CommandLine::AppendSwitch(&cl, kFuzzer);
- break;
- default:
- return NULL;
- }
- // kDebugChildren support.
- if (CommandLine().HasSwitch(switches::kDebugChildren)) {
- CommandLine::AppendSwitch(&cl, switches::kDebugOnStart);
- }
- HANDLE process = NULL;
- if (!base::LaunchApp(cl, false, true, &process))
- return NULL;
-
- return process;
-}
#endif // defined(OS_WIN)
-// TODO(playmobil): Implement
-#if defined(OS_POSIX)
-base::ProcessHandle SpawnChild(ChildType child_type) {
- NOTIMPLEMENTED();
- return NULL;
-}
-#endif
-
-
int main(int argc, char** argv) {
base::ScopedNSAutoreleasePool scoped_pool;
base::EnableTerminationOnHeapCorruption();
-#if defined(OS_WIN)
- // Some tests may use base::Singleton<>, thus we need to instanciate
- // the AtExitManager or else we will leak objects.
- base::AtExitManager at_exit_manager;
-
- MessageLoopForIO main_message_loop;
+#if defined(OS_WIN)
// suppress standard crash dialogs and such unless a debugger is present.
if (!IsDebuggerPresent()) {
SuppressErrorDialogs();
@@ -417,19 +408,11 @@ int main(int argc, char** argv) {
}
#endif // defined(OS_WIN)
-#ifndef PERFORMANCE_TEST
- if (CommandLine().HasSwitch(kChild))
- return RunTestClient();
- if (CommandLine().HasSwitch(kFuzzer))
- return RunFuzzServer();
-#else
- if (CommandLine().HasSwitch(kReflector))
- return RunReflector();
+ int retval = TestSuite(argc, argv).Run();
+#ifdef PERFORMANCE_TEST
if (!InitPerfLog("ipc_perf_child.log"))
return 1;
#endif
-
- testing::InitGoogleTest(&argc, argv);
- return RUN_ALL_TESTS();
+ return retval;
}
diff --git a/chrome/common/ipc_tests.h b/chrome/common/ipc_tests.h
index ceac51a..30c043f 100644
--- a/chrome/common/ipc_tests.h
+++ b/chrome/common/ipc_tests.h
@@ -5,6 +5,7 @@
#ifndef CHROME_COMMON_IPC_TESTS_H__
#define CHROME_COMMON_IPC_TESTS_H__
+#include "base/multiprocess_test.h"
#include "base/process.h"
// This unit test uses 3 types of child processes, a regular pipe client,
@@ -20,13 +21,21 @@ extern const wchar_t kTestClientChannel[];
extern const wchar_t kReflectorChannel[];
extern const wchar_t kFuzzerChannel[];
-// Spawns a child process and then runs the code for one of the 3 possible
-// child modes.
-base::ProcessHandle SpawnChild(ChildType child_type);
+class MessageLoopForIO;
-// Runs the fuzzing server child mode. Returns true when the preset number
-// of messages have been received.
-bool RunFuzzServer();
+//Base class to facilitate Spawning IPC Client processes.
+class IPCChannelTest : public MultiProcessTest {
+ protected:
-#endif // CHROME_COMMON_IPC_TESTS_H__
+ // Create a new MessageLoopForIO For each test.
+ virtual void SetUp();
+ virtual void TearDown();
+
+ // Spawns a child process of the specified type
+ base::ProcessHandle SpawnChild(ChildType child_type);
+ // Created around each test instantiation.
+ MessageLoopForIO *message_loop_;
+};
+
+#endif // CHROME_COMMON_IPC_TESTS_H__