OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2014 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 "chrome/utility/chrome_content_utility_client.h" |
| 6 |
| 7 #include "base/command_line.h" |
| 8 #include "chrome/utility/utility_message_handler.h" |
| 9 #include "testing/gtest/include/gtest/gtest.h" |
| 10 #include "ipc/ipc_message_macros.h" |
| 11 #include "chrome/common/chrome_utility_messages.h" |
| 12 #include "content/public/common/content_switches.h" |
| 13 |
| 14 // Define messages for testing. |
| 15 #define IPC_MESSAGE_START ChromeUtilityMsgStart |
| 16 IPC_MESSAGE_CONTROL0(ChromeUtilityMsg_DummyRequest) |
| 17 IPC_MESSAGE_CONTROL0(ChromeUtilityHostMsg_DummyResponse) |
| 18 |
| 19 namespace chrome { |
| 20 |
| 21 namespace { |
| 22 |
| 23 class MockHandler : public UtilityMessageHandler { |
| 24 public: |
| 25 MockHandler() : request_received(false) {} |
| 26 |
| 27 virtual bool OnMessageReceived(const IPC::Message& message) OVERRIDE { |
| 28 bool handled = true; |
| 29 |
| 30 IPC_BEGIN_MESSAGE_MAP(MockHandler, message) |
| 31 IPC_MESSAGE_HANDLER(ChromeUtilityMsg_DummyRequest, OnDummyRequest) |
| 32 IPC_MESSAGE_UNHANDLED(handled = false); |
| 33 IPC_END_MESSAGE_MAP() |
| 34 |
| 35 return handled; |
| 36 } |
| 37 |
| 38 void OnDummyRequest() { |
| 39 request_received = true; |
| 40 } |
| 41 |
| 42 bool request_received; |
| 43 }; |
| 44 |
| 45 class ChromeContentUtilityClientTest : public testing::Test { |
| 46 }; |
| 47 |
| 48 } // namespace |
| 49 |
| 50 // Tests that when running elevated then messages in the whitelist are |
| 51 // accepted. |
| 52 TEST_F(ChromeContentUtilityClientTest, MessageWhitelistAccept) { |
| 53 MockHandler* handler = new MockHandler; |
| 54 |
| 55 ChromeContentUtilityClient client; |
| 56 |
| 57 CommandLine* command_line = CommandLine::ForCurrentProcess(); |
| 58 command_line->AppendSwitch(switches::kUtilityProcessRunningElevated); |
| 59 |
| 60 // Ownership given to client. |
| 61 client.AddHandler(handler); |
| 62 client.AddWhitelistMessageType(ChromeUtilityMsg_DummyRequest::ID); |
| 63 client.UtilityThreadStarted(); |
| 64 |
| 65 ChromeUtilityMsg_DummyRequest message; |
| 66 |
| 67 EXPECT_TRUE(client.OnMessageReceived(message)); |
| 68 EXPECT_TRUE(handler->request_received); |
| 69 } |
| 70 |
| 71 // Tests that when running elevated then messages that aren't in the whitelist |
| 72 // are rejected. |
| 73 TEST_F(ChromeContentUtilityClientTest, MessageWhitelistReject) { |
| 74 MockHandler* handler = new MockHandler; |
| 75 |
| 76 ChromeContentUtilityClient client; |
| 77 |
| 78 CommandLine* command_line = CommandLine::ForCurrentProcess(); |
| 79 command_line->AppendSwitch(switches::kUtilityProcessRunningElevated); |
| 80 |
| 81 // Ownership given to client. |
| 82 client.AddHandler(handler); |
| 83 client.UtilityThreadStarted(); |
| 84 |
| 85 ChromeUtilityMsg_DummyRequest message; |
| 86 |
| 87 EXPECT_FALSE(client.OnMessageReceived(message)); |
| 88 EXPECT_FALSE(handler->request_received); |
| 89 } |
| 90 |
| 91 // Tests that when not running elevated then messages are recieved without |
| 92 // whitelisting. |
| 93 TEST_F(ChromeContentUtilityClientTest, NoWhitelistUnelevated) { |
| 94 MockHandler* handler = new MockHandler; |
| 95 |
| 96 ChromeContentUtilityClient client; |
| 97 |
| 98 // Ownership given to client. |
| 99 client.AddHandler(handler); |
| 100 client.UtilityThreadStarted(); |
| 101 |
| 102 ChromeUtilityMsg_DummyRequest message; |
| 103 |
| 104 EXPECT_TRUE(client.OnMessageReceived(message)); |
| 105 EXPECT_TRUE(handler->request_received); |
| 106 } |
| 107 |
| 108 } // namespace chrome |
OLD | NEW |