Add some instrumentation for jank in URLRequest::Start.
[chromium-blink-merge.git] / chromeos / network / firewall_hole_unittest.cc
blob3032d5b81c0ba0bf5e39509a35901cc18e85ce19
1 // Copyright 2015 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 "base/bind.h"
6 #include "base/run_loop.h"
7 #include "chromeos/dbus/dbus_thread_manager.h"
8 #include "chromeos/dbus/mock_permission_broker_client.h"
9 #include "chromeos/network/firewall_hole.h"
10 #include "dbus/file_descriptor.h"
11 #include "testing/gmock/include/gmock/gmock.h"
12 #include "testing/gtest/include/gtest/gtest.h"
14 using chromeos::DBusThreadManager;
15 using chromeos::FirewallHole;
16 using chromeos::MockPermissionBrokerClient;
17 using testing::_;
19 namespace {
21 ACTION_TEMPLATE(InvokeCallback,
22 HAS_1_TEMPLATE_PARAMS(int, k),
23 AND_1_VALUE_PARAMS(p1)) {
24 ::std::tr1::get<k>(args).Run(p1);
27 } // namespace
29 class FirewallHoleTest : public testing::Test {
30 public:
31 FirewallHoleTest() {}
32 ~FirewallHoleTest() override {}
34 void SetUp() override {
35 mock_permission_broker_client_ = new MockPermissionBrokerClient();
36 DBusThreadManager::GetSetterForTesting()->SetPermissionBrokerClient(
37 make_scoped_ptr(mock_permission_broker_client_));
40 void TearDown() override { DBusThreadManager::Shutdown(); }
42 void AssertOpenSuccess(scoped_ptr<FirewallHole> hole) {
43 EXPECT_TRUE(hole.get() != nullptr);
44 run_loop_.Quit();
47 void AssertOpenFailure(scoped_ptr<FirewallHole> hole) {
48 EXPECT_TRUE(hole.get() == nullptr);
49 run_loop_.Quit();
52 private:
53 base::MessageLoopForUI message_loop_;
55 protected:
56 base::RunLoop run_loop_;
57 MockPermissionBrokerClient* mock_permission_broker_client_ = nullptr;
60 TEST_F(FirewallHoleTest, GrantTcpPortAccess) {
61 EXPECT_CALL(*mock_permission_broker_client_,
62 RequestTcpPortAccess(1234, "foo0", _, _))
63 .WillOnce(InvokeCallback<3>(true));
64 EXPECT_CALL(*mock_permission_broker_client_, ReleaseTcpPort(1234, "foo0", _))
65 .WillOnce(InvokeCallback<2>(true));
67 FirewallHole::Open(
68 FirewallHole::PortType::TCP, 1234, "foo0",
69 base::Bind(&FirewallHoleTest::AssertOpenSuccess, base::Unretained(this)));
70 run_loop_.Run();
73 TEST_F(FirewallHoleTest, DenyTcpPortAccess) {
74 EXPECT_CALL(*mock_permission_broker_client_,
75 RequestTcpPortAccess(1234, "foo0", _, _))
76 .WillOnce(InvokeCallback<3>(false));
78 FirewallHole::Open(
79 FirewallHole::PortType::TCP, 1234, "foo0",
80 base::Bind(&FirewallHoleTest::AssertOpenFailure, base::Unretained(this)));
81 run_loop_.Run();
84 TEST_F(FirewallHoleTest, GrantUdpPortAccess) {
85 EXPECT_CALL(*mock_permission_broker_client_,
86 RequestUdpPortAccess(1234, "foo0", _, _))
87 .WillOnce(InvokeCallback<3>(true));
88 EXPECT_CALL(*mock_permission_broker_client_, ReleaseUdpPort(1234, "foo0", _))
89 .WillOnce(InvokeCallback<2>(true));
91 FirewallHole::Open(
92 FirewallHole::PortType::UDP, 1234, "foo0",
93 base::Bind(&FirewallHoleTest::AssertOpenSuccess, base::Unretained(this)));
94 run_loop_.Run();
97 TEST_F(FirewallHoleTest, DenyUdpPortAccess) {
98 EXPECT_CALL(*mock_permission_broker_client_,
99 RequestUdpPortAccess(1234, "foo0", _, _))
100 .WillOnce(InvokeCallback<3>(false));
102 FirewallHole::Open(
103 FirewallHole::PortType::UDP, 1234, "foo0",
104 base::Bind(&FirewallHoleTest::AssertOpenFailure, base::Unretained(this)));
105 run_loop_.Run();