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