15821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Copyright (c) 2012 The Chromium Authors. All rights reserved.
25821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be
35821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// found in the LICENSE file.
45821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
5558790d6acca3451cf3a6b497803a5f07d0bec58Ben Murdoch#include "content/renderer/pepper/pepper_broker.h"
65821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
75821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#if defined(OS_POSIX)
85821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include <fcntl.h>
95821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include <sys/socket.h>
105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#endif  // defined(OS_POSIX)
115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "content/test/mock_render_process.h"
135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "testing/gtest/include/gtest/gtest.h"
145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)namespace content {
165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
17558790d6acca3451cf3a6b497803a5f07d0bec58Ben Murdochclass PepperBrokerTest : public ::testing::Test {
185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) protected:
19c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  base::MessageLoopForIO message_loop_;
205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // We need a render process for ppapi::proxy::ProxyChannel to work.
215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  MockRenderProcess mock_process_;
225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)};
235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Try to initialize PepperBrokerDispatcherWrapper with invalid ChannelHandle.
255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Initialization should fail.
26558790d6acca3451cf3a6b497803a5f07d0bec58Ben MurdochTEST_F(PepperBrokerTest, InitFailure) {
275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  PepperBrokerDispatcherWrapper dispatcher_wrapper;
285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  IPC::ChannelHandle invalid_channel;  // Invalid by default.
295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // An invalid handle should result in a failure (false) without a LOG(FATAL),
315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // such as the one in CreatePipe().  Call it twice because without the invalid
325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // handle check, the posix code would hit a one-time path due to a static
335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // variable and go through the LOG(FATAL) path.
342a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  EXPECT_FALSE(dispatcher_wrapper.Init(base::kNullProcessId, invalid_channel));
352a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  EXPECT_FALSE(dispatcher_wrapper.Init(base::kNullProcessId, invalid_channel));
365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// On valid ChannelHandle, initialization should succeed.
39558790d6acca3451cf3a6b497803a5f07d0bec58Ben MurdochTEST_F(PepperBrokerTest, InitSuccess) {
405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  PepperBrokerDispatcherWrapper dispatcher_wrapper;
41fb250657ef40d7500f20882d5c9909c1013367d3Ben Murdoch  const char kChannelName[] = "PepperHelperImplTestChannelName";
425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#if defined(OS_POSIX)
435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  int fds[2] = {-1, -1};
445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  ASSERT_EQ(0, socketpair(AF_UNIX, SOCK_STREAM, 0, fds));
455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Channel::ChannelImpl::CreatePipe needs the fd to be non-blocking.
465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  ASSERT_EQ(0, fcntl(fds[1], F_SETFL, O_NONBLOCK));
475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  base::FileDescriptor file_descriptor(fds[1], true);  // Auto close.
485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  IPC::ChannelHandle valid_channel(kChannelName, file_descriptor);
495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#else
505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  IPC::ChannelHandle valid_channel(kChannelName);
515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#endif  // defined(OS_POSIX));
525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
532a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  EXPECT_TRUE(dispatcher_wrapper.Init(base::kNullProcessId, valid_channel));
545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#if defined(OS_POSIX)
565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  EXPECT_EQ(0, ::close(fds[0]));
575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#endif  // defined(OS_POSIX));
585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
605821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}  // namespace content
61