| Index: chrome/utility/chrome_content_utility_client_unittest.cc
|
| diff --git a/chrome/utility/chrome_content_utility_client_unittest.cc b/chrome/utility/chrome_content_utility_client_unittest.cc
|
| new file mode 100644
|
| index 0000000000000000000000000000000000000000..db5e555bc1c6e3ac6f9ec4df21c77c843c033d76
|
| --- /dev/null
|
| +++ b/chrome/utility/chrome_content_utility_client_unittest.cc
|
| @@ -0,0 +1,108 @@
|
| +// Copyright (c) 2014 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 "chrome/utility/chrome_content_utility_client.h"
|
| +
|
| +#include "base/command_line.h"
|
| +#include "chrome/utility/utility_message_handler.h"
|
| +#include "testing/gtest/include/gtest/gtest.h"
|
| +#include "ipc/ipc_message_macros.h"
|
| +#include "chrome/common/chrome_utility_messages.h"
|
| +#include "content/public/common/content_switches.h"
|
| +
|
| +// Define messages for testing.
|
| +#define IPC_MESSAGE_START ChromeUtilityMsgStart
|
| +IPC_MESSAGE_CONTROL0(ChromeUtilityMsg_DummyRequest)
|
| +IPC_MESSAGE_CONTROL0(ChromeUtilityHostMsg_DummyResponse)
|
| +
|
| +namespace chrome {
|
| +
|
| +namespace {
|
| +
|
| +class MockHandler : public UtilityMessageHandler {
|
| + public:
|
| + MockHandler() : request_received(false) {}
|
| +
|
| + virtual bool OnMessageReceived(const IPC::Message& message) OVERRIDE {
|
| + bool handled = true;
|
| +
|
| + IPC_BEGIN_MESSAGE_MAP(MockHandler, message)
|
| + IPC_MESSAGE_HANDLER(ChromeUtilityMsg_DummyRequest, OnDummyRequest)
|
| + IPC_MESSAGE_UNHANDLED(handled = false);
|
| + IPC_END_MESSAGE_MAP()
|
| +
|
| + return handled;
|
| + }
|
| +
|
| + void OnDummyRequest() {
|
| + request_received = true;
|
| + }
|
| +
|
| + bool request_received;
|
| +};
|
| +
|
| +class ChromeContentUtilityClientTest : public testing::Test {
|
| +};
|
| +
|
| +} // namespace
|
| +
|
| +// Tests that when running elevated then messages in the whitelist are
|
| +// accepted.
|
| +TEST_F(ChromeContentUtilityClientTest, MessageWhitelistAccept) {
|
| + MockHandler* handler = new MockHandler;
|
| +
|
| + ChromeContentUtilityClient client;
|
| +
|
| + CommandLine* command_line = CommandLine::ForCurrentProcess();
|
| + command_line->AppendSwitch(switches::kUtilityProcessRunningElevated);
|
| +
|
| + // Ownership given to client.
|
| + client.AddHandler(handler);
|
| + client.AddWhitelistMessageType(ChromeUtilityMsg_DummyRequest::ID);
|
| + client.UtilityThreadStarted();
|
| +
|
| + ChromeUtilityMsg_DummyRequest message;
|
| +
|
| + EXPECT_TRUE(client.OnMessageReceived(message));
|
| + EXPECT_TRUE(handler->request_received);
|
| +}
|
| +
|
| +// Tests that when running elevated then messages that aren't in the whitelist
|
| +// are rejected.
|
| +TEST_F(ChromeContentUtilityClientTest, MessageWhitelistReject) {
|
| + MockHandler* handler = new MockHandler;
|
| +
|
| + ChromeContentUtilityClient client;
|
| +
|
| + CommandLine* command_line = CommandLine::ForCurrentProcess();
|
| + command_line->AppendSwitch(switches::kUtilityProcessRunningElevated);
|
| +
|
| + // Ownership given to client.
|
| + client.AddHandler(handler);
|
| + client.UtilityThreadStarted();
|
| +
|
| + ChromeUtilityMsg_DummyRequest message;
|
| +
|
| + EXPECT_FALSE(client.OnMessageReceived(message));
|
| + EXPECT_FALSE(handler->request_received);
|
| +}
|
| +
|
| +// Tests that when not running elevated then messages are recieved without
|
| +// whitelisting.
|
| +TEST_F(ChromeContentUtilityClientTest, NoWhitelistUnelevated) {
|
| + MockHandler* handler = new MockHandler;
|
| +
|
| + ChromeContentUtilityClient client;
|
| +
|
| + // Ownership given to client.
|
| + client.AddHandler(handler);
|
| + client.UtilityThreadStarted();
|
| +
|
| + ChromeUtilityMsg_DummyRequest message;
|
| +
|
| + EXPECT_TRUE(client.OnMessageReceived(message));
|
| + EXPECT_TRUE(handler->request_received);
|
| +}
|
| +
|
| +} // namespace chrome
|
|
|