OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "content/renderer/pepper/pepper_broker_impl.h" | 5 #include "content/renderer/pepper/pepper_broker_impl.h" |
6 | 6 |
7 #if defined(OS_POSIX) | 7 #if defined(OS_POSIX) |
8 #include <fcntl.h> | 8 #include <fcntl.h> |
9 #include <sys/socket.h> | 9 #include <sys/socket.h> |
10 #endif // defined(OS_POSIX) | 10 #endif // defined(OS_POSIX) |
11 | 11 |
12 #include "content/test/mock_render_process.h" | 12 #include "content/test/mock_render_process.h" |
13 #include "testing/gtest/include/gtest/gtest.h" | 13 #include "testing/gtest/include/gtest/gtest.h" |
14 | 14 |
15 namespace content { | 15 namespace content { |
16 | 16 |
17 class PepperBrokerImplTest : public ::testing::Test { | 17 class PepperBrokerImplTest : public ::testing::Test { |
18 protected: | 18 protected: |
19 MessageLoopForIO message_loop_; | 19 MessageLoopForIO message_loop_; |
20 // We need a render process for ppapi::proxy::ProxyChannel to work. | 20 // We need a render process for ppapi::proxy::ProxyChannel to work. |
21 MockRenderProcess mock_process_; | 21 MockRenderProcess mock_process_; |
22 }; | 22 }; |
23 | 23 |
24 // Try to initialize PepperBrokerDispatcherWrapper with invalid ChannelHandle. | 24 // Try to initialize PepperBrokerDispatcherWrapper with invalid ChannelHandle. |
25 // Initialization should fail. | 25 // Initialization should fail. |
26 TEST_F(PepperBrokerImplTest, InitFailure) { | 26 TEST_F(PepperBrokerImplTest, InitFailure) { |
27 PepperBrokerDispatcherWrapper dispatcher_wrapper; | 27 PepperBrokerDispatcherWrapper dispatcher_wrapper; |
28 base::ProcessHandle broker_process_handle = base::kNullProcessHandle; | |
29 IPC::ChannelHandle invalid_channel; // Invalid by default. | 28 IPC::ChannelHandle invalid_channel; // Invalid by default. |
30 | 29 |
31 // An invalid handle should result in a failure (false) without a LOG(FATAL), | 30 // An invalid handle should result in a failure (false) without a LOG(FATAL), |
32 // such as the one in CreatePipe(). Call it twice because without the invalid | 31 // such as the one in CreatePipe(). Call it twice because without the invalid |
33 // handle check, the posix code would hit a one-time path due to a static | 32 // handle check, the posix code would hit a one-time path due to a static |
34 // variable and go through the LOG(FATAL) path. | 33 // variable and go through the LOG(FATAL) path. |
35 EXPECT_FALSE(dispatcher_wrapper.Init(broker_process_handle, invalid_channel)); | 34 EXPECT_FALSE(dispatcher_wrapper.Init(invalid_channel)); |
36 EXPECT_FALSE(dispatcher_wrapper.Init(broker_process_handle, invalid_channel)); | 35 EXPECT_FALSE(dispatcher_wrapper.Init(invalid_channel)); |
37 } | 36 } |
38 | 37 |
39 // On valid ChannelHandle, initialization should succeed. | 38 // On valid ChannelHandle, initialization should succeed. |
40 TEST_F(PepperBrokerImplTest, InitSuccess) { | 39 TEST_F(PepperBrokerImplTest, InitSuccess) { |
41 PepperBrokerDispatcherWrapper dispatcher_wrapper; | 40 PepperBrokerDispatcherWrapper dispatcher_wrapper; |
42 base::ProcessHandle broker_process_handle = base::kNullProcessHandle; | |
43 const char kChannelName[] = "PepperPluginDelegateImplTestChannelName"; | 41 const char kChannelName[] = "PepperPluginDelegateImplTestChannelName"; |
44 #if defined(OS_POSIX) | 42 #if defined(OS_POSIX) |
45 int fds[2] = {-1, -1}; | 43 int fds[2] = {-1, -1}; |
46 ASSERT_EQ(0, socketpair(AF_UNIX, SOCK_STREAM, 0, fds)); | 44 ASSERT_EQ(0, socketpair(AF_UNIX, SOCK_STREAM, 0, fds)); |
47 // Channel::ChannelImpl::CreatePipe needs the fd to be non-blocking. | 45 // Channel::ChannelImpl::CreatePipe needs the fd to be non-blocking. |
48 ASSERT_EQ(0, fcntl(fds[1], F_SETFL, O_NONBLOCK)); | 46 ASSERT_EQ(0, fcntl(fds[1], F_SETFL, O_NONBLOCK)); |
49 base::FileDescriptor file_descriptor(fds[1], true); // Auto close. | 47 base::FileDescriptor file_descriptor(fds[1], true); // Auto close. |
50 IPC::ChannelHandle valid_channel(kChannelName, file_descriptor); | 48 IPC::ChannelHandle valid_channel(kChannelName, file_descriptor); |
51 #else | 49 #else |
52 IPC::ChannelHandle valid_channel(kChannelName); | 50 IPC::ChannelHandle valid_channel(kChannelName); |
53 #endif // defined(OS_POSIX)); | 51 #endif // defined(OS_POSIX)); |
54 | 52 |
55 EXPECT_TRUE(dispatcher_wrapper.Init(broker_process_handle, valid_channel)); | 53 EXPECT_TRUE(dispatcher_wrapper.Init(valid_channel)); |
56 | 54 |
57 #if defined(OS_POSIX) | 55 #if defined(OS_POSIX) |
58 EXPECT_EQ(0, ::close(fds[0])); | 56 EXPECT_EQ(0, ::close(fds[0])); |
59 #endif // defined(OS_POSIX)); | 57 #endif // defined(OS_POSIX)); |
60 } | 58 } |
61 | 59 |
62 } // namespace content | 60 } // namespace content |
OLD | NEW |