1 // Copyright 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.
5 #include "chrome/installer/util/advanced_firewall_manager_win.h"
7 #include "base/path_service.h"
8 #include "base/process/process_handle.h"
9 #include "base/win/scoped_bstr.h"
10 #include "testing/gtest/include/gtest/gtest.h"
14 class AdvancedFirewallManagerTest
: public ::testing::Test
{
16 AdvancedFirewallManagerTest() : skip_test_(true) {}
19 // Sets up the test fixture.
20 virtual void SetUp() override
{
21 base::IntegrityLevel level
= base::INTEGRITY_UNKNOWN
;
22 if (!GetProcessIntegrityLevel(base::GetCurrentProcessHandle(), &level
) ||
23 level
!= base::HIGH_INTEGRITY
) {
24 LOG(WARNING
) << "XP or not elevated. Skipping the test.";
28 base::FilePath exe_path
;
29 PathService::Get(base::FILE_EXE
, &exe_path
);
30 EXPECT_TRUE(manager_
.Init(L
"AdvancedFirewallManagerTest", exe_path
));
31 manager_
.DeleteAllRules();
34 // Tears down the test fixture.
35 virtual void TearDown() override
{
37 manager_
.DeleteAllRules();
40 // Forwards calls to |manager_| to avoid making each test a friend of
41 // |AdvancedFirewallManager|.
42 void GetAllRules(std::vector
<base::string16
>* rule_names
) {
43 std::vector
<base::win::ScopedComPtr
<INetFwRule
> > rules
;
44 manager_
.GetAllRules(&rules
);
45 for (size_t i
= 0; i
< rules
.size(); ++i
) {
46 base::win::ScopedBstr name
;
47 EXPECT_TRUE(SUCCEEDED(rules
[i
]->get_Name(name
.Receive())));
49 rule_names
->push_back(base::string16(name
));
54 AdvancedFirewallManager manager_
;
57 DISALLOW_COPY_AND_ASSIGN(AdvancedFirewallManagerTest
);
60 TEST_F(AdvancedFirewallManagerTest
, NoRule
) {
63 std::vector
<base::string16
> rule_names
;
64 GetAllRules(&rule_names
);
65 EXPECT_TRUE(rule_names
.empty());
68 TEST_F(AdvancedFirewallManagerTest
, AddRule
) {
71 const wchar_t kRuleName
[] = L
"Port56789";
72 EXPECT_TRUE(manager_
.AddUDPRule(kRuleName
, L
"Test Description", 56789));
74 std::vector
<base::string16
> rule_names
;
75 GetAllRules(&rule_names
);
76 ASSERT_EQ(1u, rule_names
.size());
77 EXPECT_EQ(rule_names
[0], kRuleName
);
78 EXPECT_TRUE(manager_
.HasAnyRule());
80 manager_
.DeleteRuleByName(kRuleName
);
82 GetAllRules(&rule_names
);
83 EXPECT_TRUE(rule_names
.empty());
84 EXPECT_FALSE(manager_
.HasAnyRule());
87 } // namespace installer