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_info.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 if (base::GetCurrentProcessIntegrityLevel() != base::HIGH_INTEGRITY
) {
22 LOG(WARNING
) << "XP or not elevated. Skipping the test.";
26 base::FilePath exe_path
;
27 PathService::Get(base::FILE_EXE
, &exe_path
);
28 EXPECT_TRUE(manager_
.Init(L
"AdvancedFirewallManagerTest", exe_path
));
29 manager_
.DeleteAllRules();
32 // Tears down the test fixture.
33 virtual void TearDown() override
{
35 manager_
.DeleteAllRules();
38 // Forwards calls to |manager_| to avoid making each test a friend of
39 // |AdvancedFirewallManager|.
40 void GetAllRules(std::vector
<base::string16
>* rule_names
) {
41 std::vector
<base::win::ScopedComPtr
<INetFwRule
> > rules
;
42 manager_
.GetAllRules(&rules
);
43 for (size_t i
= 0; i
< rules
.size(); ++i
) {
44 base::win::ScopedBstr name
;
45 EXPECT_TRUE(SUCCEEDED(rules
[i
]->get_Name(name
.Receive())));
47 rule_names
->push_back(base::string16(name
));
52 AdvancedFirewallManager manager_
;
55 DISALLOW_COPY_AND_ASSIGN(AdvancedFirewallManagerTest
);
58 TEST_F(AdvancedFirewallManagerTest
, NoRule
) {
61 std::vector
<base::string16
> rule_names
;
62 GetAllRules(&rule_names
);
63 EXPECT_TRUE(rule_names
.empty());
66 TEST_F(AdvancedFirewallManagerTest
, AddRule
) {
69 const wchar_t kRuleName
[] = L
"Port56789";
70 EXPECT_TRUE(manager_
.AddUDPRule(kRuleName
, L
"Test Description", 56789));
72 std::vector
<base::string16
> rule_names
;
73 GetAllRules(&rule_names
);
74 ASSERT_EQ(1u, rule_names
.size());
75 EXPECT_EQ(rule_names
[0], kRuleName
);
76 EXPECT_TRUE(manager_
.HasAnyRule());
78 manager_
.DeleteRuleByName(kRuleName
);
80 GetAllRules(&rule_names
);
81 EXPECT_TRUE(rule_names
.empty());
82 EXPECT_FALSE(manager_
.HasAnyRule());
85 } // namespace installer