Commit f0a8a707 authored by Joe Mason's avatar Joe Mason Committed by Commit Bot

Add ComputeSHA256DigestOfString function to chrome_cleaner/os

Bug: 830892
Change-Id: I3b900e68527f31cfd52cc27d98cfcdd3a22d001b
Reviewed-on: https://chromium-review.googlesource.com/1147497Reviewed-by: default avatarChris Sharp <csharp@chromium.org>
Commit-Queue: Joe Mason <joenotcharles@chromium.org>
Cr-Commit-Position: refs/heads/master@{#579511}
parent 612af1b6
......@@ -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;
}
......
......@@ -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<SecureHash> 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;
......
......@@ -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
......
......@@ -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;
......
......@@ -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_);
};
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment