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. 4 5#include "chrome/installer/util/advanced_firewall_manager_win.h" 6 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" 11 12namespace installer { 13 14class AdvancedFirewallManagerTest : public ::testing::Test { 15 public: 16 AdvancedFirewallManagerTest() : skip_test_(true) {} 17 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; 26 }; 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(); 32 } 33 34 // Tears down the test fixture. 35 virtual void TearDown() OVERRIDE { 36 if (!skip_test_) 37 manager_.DeleteAllRules(); 38 } 39 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)); 50 } 51 } 52 53 bool skip_test_; 54 AdvancedFirewallManager manager_; 55 56 private: 57 DISALLOW_COPY_AND_ASSIGN(AdvancedFirewallManagerTest); 58}; 59 60TEST_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()); 66} 67 68TEST_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)); 73 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()); 79 80 manager_.DeleteRuleByName(kRuleName); 81 rule_names.clear(); 82 GetAllRules(&rule_names); 83 EXPECT_TRUE(rule_names.empty()); 84 EXPECT_FALSE(manager_.HasAnyRule()); 85} 86 87} // namespace installer 88