Roll src/third_party/WebKit 116cf7f:79abaa8 (svn 189234:189235)
[chromium-blink-merge.git] / chrome / installer / util / advanced_firewall_manager_win_unittest.cc
blobdc67e51fde9f49729f3100f4efa83de8baa3df2b
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"
12 namespace installer {
14 class AdvancedFirewallManagerTest : public ::testing::Test {
15 public:
16 AdvancedFirewallManagerTest() : skip_test_(true) {}
18 protected:
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.";
25 return;
27 skip_test_ = false;
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 {
36 if (!skip_test_)
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())));
48 EXPECT_TRUE(name);
49 rule_names->push_back(base::string16(name));
53 bool skip_test_;
54 AdvancedFirewallManager manager_;
56 private:
57 DISALLOW_COPY_AND_ASSIGN(AdvancedFirewallManagerTest);
60 TEST_F(AdvancedFirewallManagerTest, NoRule) {
61 if (skip_test_)
62 return;
63 std::vector<base::string16> rule_names;
64 GetAllRules(&rule_names);
65 EXPECT_TRUE(rule_names.empty());
68 TEST_F(AdvancedFirewallManagerTest, AddRule) {
69 if (skip_test_)
70 return;
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);
81 rule_names.clear();
82 GetAllRules(&rule_names);
83 EXPECT_TRUE(rule_names.empty());
84 EXPECT_FALSE(manager_.HasAnyRule());
87 } // namespace installer