Skip to content

Commit

Permalink
Add ComputeSHA256DigestOfString function to chrome_cleaner/os
Browse files Browse the repository at this point in the history
Bug: 830892
Change-Id: I3b900e68527f31cfd52cc27d98cfcdd3a22d001b
Reviewed-on: https://chromium-review.googlesource.com/1147497
Reviewed-by: Chris Sharp <csharp@chromium.org>
Commit-Queue: Joe Mason <joenotcharles@chromium.org>
Cr-Commit-Position: refs/heads/master@{#579511}
  • Loading branch information
Joe Mason authored and Commit Bot committed Jul 31, 2018
1 parent 612af1b commit f0a8a70
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 13 deletions.
2 changes: 1 addition & 1 deletion chrome/chrome_cleaner/os/digest_verifier.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
20 changes: 18 additions & 2 deletions chrome/chrome_cleaner/os/disk_util.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand All @@ -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;
Expand Down
7 changes: 6 additions & 1 deletion chrome/chrome_cleaner/os/disk_util.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
19 changes: 13 additions & 6 deletions chrome/chrome_cleaner/os/disk_util_unittest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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());
Expand All @@ -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());

Expand Down Expand Up @@ -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;
Expand Down
6 changes: 3 additions & 3 deletions chrome/chrome_cleaner/test/test_service_main.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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_);
};

Expand Down

0 comments on commit f0a8a70

Please sign in to comment.