| OLD | NEW |
| 1 // Copyright 2017 The Chromium Authors. All rights reserved. | 1 // Copyright 2017 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "chrome/browser/conflicts/module_info_util_win.h" | 5 #include "chrome/browser/conflicts/module_info_util_win.h" |
| 6 | 6 |
| 7 #include <memory> | 7 #include <memory> |
| 8 #include <string> | 8 #include <string> |
| 9 | 9 |
| 10 #include "base/base_paths.h" | 10 #include "base/base_paths.h" |
| 11 #include "base/environment.h" | 11 #include "base/environment.h" |
| 12 #include "base/files/file_path.h" | 12 #include "base/files/file_path.h" |
| 13 #include "base/path_service.h" | 13 #include "base/path_service.h" |
| 14 #include "base/strings/utf_string_conversions.h" | 14 #include "base/strings/utf_string_conversions.h" |
| 15 #include "testing/gtest/include/gtest/gtest.h" | 15 #include "testing/gtest/include/gtest/gtest.h" |
| 16 | 16 |
| 17 namespace { |
| 18 |
| 19 // Overrides |variables_name| to |value| for the lifetime of this class. Upon |
| 20 // destruction, the previous value is restored. |
| 21 class ScopedEnvironmentVariableOverride { |
| 22 public: |
| 23 ScopedEnvironmentVariableOverride(const std::string& variable_name, |
| 24 const std::string& value); |
| 25 ~ScopedEnvironmentVariableOverride(); |
| 26 |
| 27 private: |
| 28 std::unique_ptr<base::Environment> environment_; |
| 29 std::string variable_name_; |
| 30 bool overridden_; |
| 31 bool was_set_; |
| 32 std::string old_value_; |
| 33 }; |
| 34 |
| 35 ScopedEnvironmentVariableOverride::ScopedEnvironmentVariableOverride( |
| 36 const std::string& variable_name, |
| 37 const std::string& value) |
| 38 : environment_(base::Environment::Create()), |
| 39 variable_name_(variable_name), |
| 40 overridden_(false), |
| 41 was_set_(false) { |
| 42 was_set_ = environment_->GetVar(variable_name, &old_value_); |
| 43 overridden_ = environment_->SetVar(variable_name, value); |
| 44 } |
| 45 |
| 46 ScopedEnvironmentVariableOverride::~ScopedEnvironmentVariableOverride() { |
| 47 if (overridden_) { |
| 48 if (was_set_) |
| 49 environment_->SetVar(variable_name_, old_value_); |
| 50 else |
| 51 environment_->UnSetVar(variable_name_); |
| 52 } |
| 53 } |
| 54 |
| 55 } // namespace |
| 56 |
| 17 TEST(ModuleInfoUtilTest, GetCertificateInfoUnsigned) { | 57 TEST(ModuleInfoUtilTest, GetCertificateInfoUnsigned) { |
| 18 base::FilePath path; | 58 base::FilePath path; |
| 19 ASSERT_TRUE(base::PathService::Get(base::FILE_EXE, &path)); | 59 ASSERT_TRUE(base::PathService::Get(base::FILE_EXE, &path)); |
| 20 CertificateInfo cert_info; | 60 CertificateInfo cert_info; |
| 21 GetCertificateInfo(path, &cert_info); | 61 GetCertificateInfo(path, &cert_info); |
| 22 EXPECT_EQ(CertificateType::NO_CERTIFICATE, cert_info.type); | 62 EXPECT_EQ(CertificateType::NO_CERTIFICATE, cert_info.type); |
| 23 EXPECT_TRUE(cert_info.path.empty()); | 63 EXPECT_TRUE(cert_info.path.empty()); |
| 24 EXPECT_TRUE(cert_info.subject.empty()); | 64 EXPECT_TRUE(cert_info.subject.empty()); |
| 25 } | 65 } |
| 26 | 66 |
| 27 TEST(ModuleInfoUtilTest, GetCertificateInfoSigned) { | 67 TEST(ModuleInfoUtilTest, GetCertificateInfoSigned) { |
| 28 std::unique_ptr<base::Environment> env = base::Environment::Create(); | 68 std::unique_ptr<base::Environment> env = base::Environment::Create(); |
| 29 std::string sysroot; | 69 std::string sysroot; |
| 30 ASSERT_TRUE(env->GetVar("SYSTEMROOT", &sysroot)); | 70 ASSERT_TRUE(env->GetVar("SYSTEMROOT", &sysroot)); |
| 31 | 71 |
| 32 base::FilePath path = | 72 base::FilePath path = |
| 33 base::FilePath::FromUTF8Unsafe(sysroot).Append(L"system32\\kernel32.dll"); | 73 base::FilePath::FromUTF8Unsafe(sysroot).Append(L"system32\\kernel32.dll"); |
| 34 | 74 |
| 35 CertificateInfo cert_info; | 75 CertificateInfo cert_info; |
| 36 GetCertificateInfo(path, &cert_info); | 76 GetCertificateInfo(path, &cert_info); |
| 37 EXPECT_NE(CertificateType::NO_CERTIFICATE, cert_info.type); | 77 EXPECT_NE(CertificateType::NO_CERTIFICATE, cert_info.type); |
| 38 EXPECT_FALSE(cert_info.path.empty()); | 78 EXPECT_FALSE(cert_info.path.empty()); |
| 39 EXPECT_FALSE(cert_info.subject.empty()); | 79 EXPECT_FALSE(cert_info.subject.empty()); |
| 40 } | 80 } |
| 81 |
| 82 TEST(ModuleInfoUtilTest, GetEnvironmentVariablesMapping) { |
| 83 ScopedEnvironmentVariableOverride scoped_override("foo", "C:\\bar\\"); |
| 84 |
| 85 // The mapping for these variables will be retrieved. |
| 86 std::vector<base::string16> environment_variables = { |
| 87 L"foo", L"SYSTEMROOT", |
| 88 }; |
| 89 StringMapping string_mapping = |
| 90 GetEnvironmentVariablesMapping(environment_variables); |
| 91 |
| 92 ASSERT_EQ(2u, string_mapping.size()); |
| 93 |
| 94 EXPECT_STREQ(L"c:\\bar", string_mapping[0].first.c_str()); |
| 95 EXPECT_STREQ(L"%foo%", string_mapping[0].second.c_str()); |
| 96 EXPECT_FALSE(string_mapping[1].second.empty()); |
| 97 } |
| 98 |
| 99 const struct CollapsePathList { |
| 100 base::string16 expected_result; |
| 101 base::string16 test_case; |
| 102 } kCollapsePathList[] = { |
| 103 // Negative testing (should not collapse this path). |
| 104 {L"c:\\a\\a.dll", L"c:\\a\\a.dll"}, |
| 105 // These two are to test that we select the maximum collapsed path. |
| 106 {L"%foo%\\a.dll", L"c:\\foo\\a.dll"}, |
| 107 {L"%x%\\a.dll", L"c:\\foo\\bar\\a.dll"}, |
| 108 // Tests that only full path components are collapsed. |
| 109 {L"c:\\foo_bar\\a.dll", L"c:\\foo_bar\\a.dll"}, |
| 110 }; |
| 111 |
| 112 TEST(ModuleInfoUtilTest, CollapseMatchingPrefixInPath) { |
| 113 StringMapping string_mapping = { |
| 114 std::make_pair(L"c:\\foo", L"%foo%"), |
| 115 std::make_pair(L"c:\\foo\\bar", L"%x%"), |
| 116 }; |
| 117 |
| 118 for (size_t i = 0; i < arraysize(kCollapsePathList); ++i) { |
| 119 base::string16 test_case = kCollapsePathList[i].test_case; |
| 120 CollapseMatchingPrefixInPath(string_mapping, &test_case); |
| 121 EXPECT_EQ(kCollapsePathList[i].expected_result, test_case); |
| 122 } |
| 123 } |
| OLD | NEW |