Re-subimission of https://codereview.chromium.org/1041213003/
[chromium-blink-merge.git] / content / renderer / pepper / pepper_broker_unittest.cc
blob5267bebc2c7de0249605595e6467516303774893
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
3 // found in the LICENSE file.
5 #include "content/renderer/pepper/pepper_broker.h"
7 #if defined(OS_POSIX)
8 #include <fcntl.h>
9 #include <sys/socket.h>
10 #endif // defined(OS_POSIX)
12 #include "content/test/mock_render_process.h"
13 #include "testing/gtest/include/gtest/gtest.h"
15 namespace content {
17 class PepperBrokerTest : public ::testing::Test {
18 protected:
19 base::MessageLoopForIO message_loop_;
20 // We need a render process for ppapi::proxy::ProxyChannel to work.
21 MockRenderProcess mock_process_;
24 // Try to initialize PepperBrokerDispatcherWrapper with invalid ChannelHandle.
25 // Initialization should fail.
26 TEST_F(PepperBrokerTest, InitFailure) {
27 PepperBrokerDispatcherWrapper dispatcher_wrapper;
28 IPC::ChannelHandle invalid_channel; // Invalid by default.
30 // An invalid handle should result in a failure (false) without a LOG(FATAL),
31 // such as the one in CreatePipe(). Call it twice because without the invalid
32 // handle check, the posix code would hit a one-time path due to a static
33 // variable and go through the LOG(FATAL) path.
34 EXPECT_FALSE(dispatcher_wrapper.Init(base::kNullProcessId, invalid_channel));
35 EXPECT_FALSE(dispatcher_wrapper.Init(base::kNullProcessId, invalid_channel));
38 // On valid ChannelHandle, initialization should succeed.
39 TEST_F(PepperBrokerTest, InitSuccess) {
40 PepperBrokerDispatcherWrapper dispatcher_wrapper;
41 const char kChannelName[] = "PepperHelperImplTestChannelName";
42 #if defined(OS_POSIX)
43 int fds[2] = {-1, -1};
44 ASSERT_EQ(0, socketpair(AF_UNIX, SOCK_STREAM, 0, fds));
45 // Channel::ChannelImpl::CreatePipe needs the fd to be non-blocking.
46 ASSERT_EQ(0, fcntl(fds[1], F_SETFL, O_NONBLOCK));
47 base::FileDescriptor file_descriptor(fds[1], true); // Auto close.
48 IPC::ChannelHandle valid_channel(kChannelName, file_descriptor);
49 #else
50 IPC::ChannelHandle valid_channel(kChannelName);
51 #endif // defined(OS_POSIX));
53 EXPECT_TRUE(dispatcher_wrapper.Init(base::kNullProcessId, valid_channel));
55 #if defined(OS_POSIX)
56 EXPECT_EQ(0, ::close(fds[0]));
57 #endif // defined(OS_POSIX));
60 } // namespace content