diff --git a/chrome/chrome_cleaner/os/digest_verifier.cc b/chrome/chrome_cleaner/os/digest_verifier.cc index f28e23e002a068..435a4a89b1cf73 100644 --- a/chrome/chrome_cleaner/os/digest_verifier.cc +++ b/chrome/chrome_cleaner/os/digest_verifier.cc @@ -36,7 +36,7 @@ bool DigestVerifier::IsKnownFile(const base::FilePath& file) const { return false; std::string actual_digest; - if (!chrome_cleaner::ComputeDigestSHA256(file, &actual_digest)) { + if (!chrome_cleaner::ComputeSHA256DigestOfPath(file, &actual_digest)) { LOG(ERROR) << "Failed to compute digest for " << SanitizePath(file); return false; } diff --git a/chrome/chrome_cleaner/os/disk_util.cc b/chrome/chrome_cleaner/os/disk_util.cc index 6b8e4b23615088..3f2ceff79a4a55 100644 --- a/chrome/chrome_cleaner/os/disk_util.cc +++ b/chrome/chrome_cleaner/os/disk_util.cc @@ -542,7 +542,7 @@ bool RetrieveDetailedFileInformation( RetrievePathInformation(expanded_path, file_information); // Retrieve the detailed file information. - if (!ComputeDigestSHA256(expanded_path, &file_information->sha256)) { + if (!ComputeSHA256DigestOfPath(expanded_path, &file_information->sha256)) { LOG(ERROR) << "Unable to compute digest SHA256 for: '" << file_information->path << "'"; return false; @@ -588,7 +588,8 @@ bool RetrieveFileInformation(const base::FilePath& file_path, } } -bool ComputeDigestSHA256(const base::FilePath& path, std::string* digest) { +bool ComputeSHA256DigestOfPath(const base::FilePath& path, + std::string* digest) { DCHECK(digest); base::File file(path, base::File::FLAG_OPEN | base::File::FLAG_READ); @@ -611,6 +612,21 @@ bool ComputeDigestSHA256(const base::FilePath& path, std::string* digest) { return true; } +bool ComputeSHA256DigestOfString(const std::string& content, + std::string* digest) { + DCHECK(digest); + + std::unique_ptr ctx(SecureHash::Create(SecureHash::SHA256)); + + ctx->Update(content.c_str(), content.length()); + + char digest_bytes[crypto::kSHA256Length]; + ctx->Finish(digest_bytes, crypto::kSHA256Length); + + *digest = base::HexEncode(digest_bytes, crypto::kSHA256Length); + return true; +} + bool GUIDLess::operator()(const GUID& smaller, const GUID& larger) const { if (smaller.Data1 < larger.Data1) return true; diff --git a/chrome/chrome_cleaner/os/disk_util.h b/chrome/chrome_cleaner/os/disk_util.h index 4285c1e87f7d71..885d4a627b2c28 100644 --- a/chrome/chrome_cleaner/os/disk_util.h +++ b/chrome/chrome_cleaner/os/disk_util.h @@ -124,7 +124,12 @@ bool RetrieveFileInformation(const base::FilePath& file_path, // Compute the SHA256 checksum of |path| and store it as base16 into |digest|. // Return true on success. -bool ComputeDigestSHA256(const base::FilePath& path, std::string* digest); +bool ComputeSHA256DigestOfPath(const base::FilePath& path, std::string* digest); + +// Compute the SHA256 of |content| and store it as base16 into |digest|. +// Return true on success. +bool ComputeSHA256DigestOfString(const std::string& content, + std::string* digest); // Return the list of registered Layered Service Providers. In case the same DLL // is registered with multiple ProviderId, |providers| is a map from the DLL diff --git a/chrome/chrome_cleaner/os/disk_util_unittest.cc b/chrome/chrome_cleaner/os/disk_util_unittest.cc index 84eaada6c9f542..295e78e86769d8 100644 --- a/chrome/chrome_cleaner/os/disk_util_unittest.cc +++ b/chrome/chrome_cleaner/os/disk_util_unittest.cc @@ -630,14 +630,14 @@ TEST(DiskUtilTests, ExpandWow64Path) { ASSERT_TRUE(PathEqual(expanded_file3, file_path3_native)); } -TEST(DiskUtilTests, ComputeDigestSHA256) { +TEST(DiskUtilTests, ComputeSHA256DigestOfPath) { base::ScopedTempDir temp_dir; ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); // Check the digest of an non-existing file. base::FilePath file_path1(temp_dir.GetPath().Append(kFileName1)); std::string digest1; - EXPECT_FALSE(ComputeDigestSHA256(file_path1, &digest1)); + EXPECT_FALSE(ComputeSHA256DigestOfPath(file_path1, &digest1)); EXPECT_TRUE(digest1.empty()); // Create an empty file and validate the digest. @@ -646,7 +646,7 @@ TEST(DiskUtilTests, ComputeDigestSHA256) { empty_file.Close(); std::string digest2; - EXPECT_TRUE(ComputeDigestSHA256(file_path2, &digest2)); + EXPECT_TRUE(ComputeSHA256DigestOfPath(file_path2, &digest2)); EXPECT_STREQ( "E3B0C44298FC1C149AFBF4C8996FB92427AE41E4649B934CA495991B7852B855", digest2.c_str()); @@ -661,13 +661,13 @@ TEST(DiskUtilTests, ComputeDigestSHA256) { valid_file.Close(); std::string digest3; - EXPECT_TRUE(ComputeDigestSHA256(file_path3, &digest3)); + EXPECT_TRUE(ComputeSHA256DigestOfPath(file_path3, &digest3)); EXPECT_STREQ( "BD283E41A3672B6BDAA574F8BD7176F8BCA95BD81383CDE32AA6D78B1DB0E371", digest3.c_str()); } -TEST(DiskUtilTests, ComputeDigestSHA256OnBigFile) { +TEST(DiskUtilTests, ComputeSHA256DigestOfPathOnBigFile) { base::ScopedTempDir temp_dir; ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); @@ -712,11 +712,18 @@ TEST(DiskUtilTests, ComputeDigestSHA256OnBigFile) { valid_file.Close(); std::string digest; - EXPECT_TRUE(ComputeDigestSHA256(file_path, &digest)); + EXPECT_TRUE(ComputeSHA256DigestOfPath(file_path, &digest)); EXPECT_STREQ(info->digest, digest.c_str()); } } +TEST(DiskUtilTests, ComputeSHA256DigestOfString) { + std::string digest_result; + std::string content(kFileContent2, sizeof(kFileContent2)); + EXPECT_TRUE(ComputeSHA256DigestOfString(content, &digest_result)); + EXPECT_STREQ(kFileContentDigests[2], digest_result.c_str()); +} + TEST(DiskUtilTests, GetLayeredServiceProviders) { // Make sure that running the OS implementation doesn't crash/dcheck. LSPPathToGUIDs providers; diff --git a/chrome/chrome_cleaner/test/test_service_main.cc b/chrome/chrome_cleaner/test/test_service_main.cc index 20afd2754bed36..9cd460901df879 100644 --- a/chrome/chrome_cleaner/test/test_service_main.cc +++ b/chrome/chrome_cleaner/test/test_service_main.cc @@ -57,9 +57,9 @@ class TestService { } private: - SERVICE_STATUS_HANDLE status_handle_ = 0; - SERVICE_STATUS service_status_ = {}; - HANDLE service_stop_event_ = INVALID_HANDLE_VALUE; + SERVICE_STATUS_HANDLE status_handle_{}; + SERVICE_STATUS service_status_{}; + HANDLE service_stop_event_{INVALID_HANDLE_VALUE}; THREAD_CHECKER(service_status_thread_checker_); };