1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
|
// Copyright (c) 2010 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "base/compiler_specific.h"
#include "base/message_loop.h"
#include "base/process_util.h"
#include "base/test/multiprocess_test.h"
#include "base/test/test_timeouts.h"
#include "chrome/common/chrome_switches.h"
#include "content/common/main_function_params.h"
#include "ipc/ipc_channel.h"
#include "ipc/ipc_switches.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "testing/multiprocess_func_list.h"
// TODO(port): Bring up this test this on other platforms.
#if defined(OS_POSIX)
using base::ProcessHandle;
const char kRendererTestChannelName[] = "test";
extern int RendererMain(const MainFunctionParams& parameters);
// TODO(port): The IPC Channel tests have a class with extremely similar
// functionality, we should combine them.
class RendererMainTest : public base::MultiProcessTest {
protected:
RendererMainTest() {}
// Create a new MessageLoopForIO For each test.
virtual void SetUp();
virtual void TearDown();
// Spawns a child process of the specified type
base::ProcessHandle SpawnChild(const std::string& procname,
IPC::Channel* channel);
virtual CommandLine MakeCmdLine(const std::string& procname,
bool debug_on_start) OVERRIDE;
// Created around each test instantiation.
MessageLoopForIO *message_loop_;
};
void RendererMainTest::SetUp() {
MultiProcessTest::SetUp();
// Construct a fresh IO Message loop for the duration of each test.
message_loop_ = new MessageLoopForIO();
}
void RendererMainTest::TearDown() {
delete message_loop_;
message_loop_ = NULL;
MultiProcessTest::TearDown();
}
ProcessHandle RendererMainTest::SpawnChild(const std::string& procname,
IPC::Channel* channel) {
base::file_handle_mapping_vector fds_to_map;
const int ipcfd = channel->GetClientFileDescriptor();
if (ipcfd > -1) {
fds_to_map.push_back(std::pair<int,int>(ipcfd, 3));
}
return MultiProcessTest::SpawnChild(procname, fds_to_map, false);
}
CommandLine RendererMainTest::MakeCmdLine(const std::string& procname,
bool debug_on_start) {
CommandLine command_line =
MultiProcessTest::MakeCmdLine(procname, debug_on_start);
// Force seccomp off for this test. It's just a problem of refactoring,
// not a bug.
// http://code.google.com/p/chromium/issues/detail?id=59376
command_line.AppendSwitch(switches::kDisableSeccompSandbox);
return command_line;
}
// Listener class that kills the message loop when it connects.
class SuicidalListener : public IPC::Channel::Listener {
public:
void OnChannelConnected(int32 peer_pid) {
MessageLoop::current()->Quit();
}
bool OnMessageReceived(const IPC::Message& message) {
// We shouldn't receive any messages
NOTREACHED();
return false;
}
};
MULTIPROCESS_TEST_MAIN(SimpleRenderer) {
SandboxInitWrapper dummy_sandbox_init;
CommandLine cl(*CommandLine::ForCurrentProcess());
cl.AppendSwitchASCII(switches::kProcessChannelID, kRendererTestChannelName);
MainFunctionParams dummy_params(cl, dummy_sandbox_init, NULL);
return RendererMain(dummy_params);
}
TEST_F(RendererMainTest, CreateDestroy) {
SuicidalListener listener;
IPC::Channel control_channel(kRendererTestChannelName,
IPC::Channel::MODE_SERVER,
&listener);
base::ProcessHandle renderer_pid = SpawnChild("SimpleRenderer",
&control_channel);
ASSERT_TRUE(control_channel.Connect());
MessageLoop::current()->Run();
// The renderer should exit when we close the channel.
control_channel.Close();
EXPECT_TRUE(base::WaitForSingleProcess(renderer_pid,
TestTimeouts::action_timeout_ms()));
}
#endif // defined(OS_POSIX)
|