Chromium Code Reviews| 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 class ScopedEnvironmentVariableOverride { | |
| 20 public: | |
| 21 ScopedEnvironmentVariableOverride(const std::string& variable_name, | |
| 22 const std::string& value); | |
| 23 ~ScopedEnvironmentVariableOverride(); | |
| 24 | |
| 25 private: | |
| 26 std::unique_ptr<base::Environment> environment_; | |
| 27 std::string variable_name_; | |
| 28 bool overridden_; | |
| 29 bool was_set_; | |
| 30 std::string old_value_; | |
| 31 }; | |
| 32 | |
| 33 ScopedEnvironmentVariableOverride::ScopedEnvironmentVariableOverride( | |
| 34 const std::string& variable_name, | |
| 35 const std::string& value) | |
| 36 : environment_(base::Environment::Create()), | |
| 37 variable_name_(variable_name), | |
| 38 overridden_(false), | |
| 39 was_set_(false) { | |
| 40 was_set_ = environment_->GetVar(variable_name, &old_value_); | |
| 41 overridden_ = environment_->SetVar(variable_name, value); | |
| 42 } | |
| 43 | |
| 44 ScopedEnvironmentVariableOverride::~ScopedEnvironmentVariableOverride() { | |
| 45 if (overridden_) { | |
| 46 if (was_set_) | |
| 47 environment_->SetVar(variable_name_, old_value_); | |
| 48 else | |
| 49 environment_->UnSetVar(variable_name_); | |
| 50 } | |
| 51 } | |
| 52 | |
| 53 } // namespace | |
| 54 | |
| 17 TEST(ModuleInfoUtilTest, GetCertificateInfoUnsigned) { | 55 TEST(ModuleInfoUtilTest, GetCertificateInfoUnsigned) { |
| 18 base::FilePath path; | 56 base::FilePath path; |
| 19 ASSERT_TRUE(base::PathService::Get(base::FILE_EXE, &path)); | 57 ASSERT_TRUE(base::PathService::Get(base::FILE_EXE, &path)); |
| 20 CertificateInfo cert_info; | 58 CertificateInfo cert_info; |
| 21 GetCertificateInfo(path, &cert_info); | 59 GetCertificateInfo(path, &cert_info); |
| 22 EXPECT_EQ(CertificateType::NO_CERTIFICATE, cert_info.type); | 60 EXPECT_EQ(CertificateType::NO_CERTIFICATE, cert_info.type); |
| 23 EXPECT_TRUE(cert_info.path.empty()); | 61 EXPECT_TRUE(cert_info.path.empty()); |
| 24 EXPECT_TRUE(cert_info.subject.empty()); | 62 EXPECT_TRUE(cert_info.subject.empty()); |
| 25 } | 63 } |
| 26 | 64 |
| 27 TEST(ModuleInfoUtilTest, GetCertificateInfoSigned) { | 65 TEST(ModuleInfoUtilTest, GetCertificateInfoSigned) { |
| 28 std::unique_ptr<base::Environment> env = base::Environment::Create(); | 66 std::unique_ptr<base::Environment> env = base::Environment::Create(); |
| 29 std::string sysroot; | 67 std::string sysroot; |
| 30 ASSERT_TRUE(env->GetVar("SYSTEMROOT", &sysroot)); | 68 ASSERT_TRUE(env->GetVar("SYSTEMROOT", &sysroot)); |
| 31 | 69 |
| 32 base::FilePath path = | 70 base::FilePath path = |
| 33 base::FilePath::FromUTF8Unsafe(sysroot).Append(L"system32\\kernel32.dll"); | 71 base::FilePath::FromUTF8Unsafe(sysroot).Append(L"system32\\kernel32.dll"); |
| 34 | 72 |
| 35 CertificateInfo cert_info; | 73 CertificateInfo cert_info; |
| 36 GetCertificateInfo(path, &cert_info); | 74 GetCertificateInfo(path, &cert_info); |
| 37 EXPECT_NE(CertificateType::NO_CERTIFICATE, cert_info.type); | 75 EXPECT_NE(CertificateType::NO_CERTIFICATE, cert_info.type); |
| 38 EXPECT_FALSE(cert_info.path.empty()); | 76 EXPECT_FALSE(cert_info.path.empty()); |
| 39 EXPECT_FALSE(cert_info.subject.empty()); | 77 EXPECT_FALSE(cert_info.subject.empty()); |
| 40 } | 78 } |
| 79 | |
| 80 TEST(ModuleInfoUtilTest, GetEnvironmentVariablesMapping) { | |
| 81 ScopedEnvironmentVariableOverride scoped_override("foo", "bar"); | |
| 82 | |
| 83 // The mapping for these variables will be retrived. | |
|
chrisha
2017/02/28 18:51:29
retrieved
Patrick Monette
2017/02/28 23:37:38
Done.
| |
| 84 std::vector<base::string16> environment_variables = { | |
| 85 L"foo", L"SYSTEMROOT", | |
| 86 }; | |
| 87 StringMapping string_mapping = | |
| 88 GetEnvironmentVariablesMapping(environment_variables); | |
| 89 | |
| 90 ASSERT_EQ(2, string_mapping.size()); | |
| 91 | |
| 92 EXPECT_STREQ(L"bar", string_mapping[0].first.c_str()); | |
| 93 EXPECT_STREQ(L"%foo%", string_mapping[0].second.c_str()); | |
| 94 EXPECT_FALSE(string_mapping[1].second.empty()); | |
| 95 } | |
| 96 | |
| 97 const struct CollapsePathList { | |
| 98 base::string16 expected_result; | |
| 99 base::string16 test_case; | |
| 100 } kCollapsePathList[] = { | |
| 101 // Negative testing (should not collapse this path). | |
| 102 {L"c:\\a\\a.dll", L"c:\\a\\a.dll"}, | |
| 103 // These two are to test that we select the maximum collapsed path. | |
|
chrisha
2017/02/28 18:51:29
I don't think we actually want this logic.
Patrick Monette
2017/02/28 23:37:38
Chatted offline. Ignored.
| |
| 104 {L"%foo%\\a.dll", L"c:\\foo\\a.dll"}, | |
| 105 {L"%x%\\a.dll", L"c:\\foo\\bar\\a.dll"}, | |
| 106 }; | |
| 107 | |
| 108 TEST(ModuleInfoUtilTest, CollapseMatchingPrefixInString) { | |
| 109 StringMapping string_mapping = { | |
| 110 std::make_pair(L"c:\\foo", L"%foo%"), | |
| 111 std::make_pair(L"c:\\foo\\bar", L"%x%"), | |
| 112 }; | |
| 113 | |
| 114 for (size_t i = 0; i < arraysize(kCollapsePathList); ++i) { | |
| 115 base::string16 test_case = kCollapsePathList[i].test_case; | |
| 116 CollapseMatchingPrefixInString(string_mapping, &test_case); | |
| 117 EXPECT_EQ(kCollapsePathList[i].expected_result, test_case); | |
| 118 } | |
| 119 } | |
| OLD | NEW |