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.
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
;
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
);
29 class FirewallHoleTest
: public testing::Test
{
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);
47 void AssertOpenFailure(scoped_ptr
<FirewallHole
> hole
) {
48 EXPECT_TRUE(hole
.get() == nullptr);
53 base::MessageLoopForUI message_loop_
;
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));
68 FirewallHole::PortType::TCP
, 1234, "foo0",
69 base::Bind(&FirewallHoleTest::AssertOpenSuccess
, base::Unretained(this)));
73 TEST_F(FirewallHoleTest
, DenyTcpPortAccess
) {
74 EXPECT_CALL(*mock_permission_broker_client_
,
75 RequestTcpPortAccess(1234, "foo0", _
, _
))
76 .WillOnce(InvokeCallback
<3>(false));
79 FirewallHole::PortType::TCP
, 1234, "foo0",
80 base::Bind(&FirewallHoleTest::AssertOpenFailure
, base::Unretained(this)));
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));
92 FirewallHole::PortType::UDP
, 1234, "foo0",
93 base::Bind(&FirewallHoleTest::AssertOpenSuccess
, base::Unretained(this)));
97 TEST_F(FirewallHoleTest
, DenyUdpPortAccess
) {
98 EXPECT_CALL(*mock_permission_broker_client_
,
99 RequestUdpPortAccess(1234, "foo0", _
, _
))
100 .WillOnce(InvokeCallback
<3>(false));
103 FirewallHole::PortType::UDP
, 1234, "foo0",
104 base::Bind(&FirewallHoleTest::AssertOpenFailure
, base::Unretained(this)));