Index: extensions/browser/computed_hashes_unittest.cc |
diff --git a/extensions/browser/computed_hashes_unittest.cc b/extensions/browser/computed_hashes_unittest.cc |
index da36a903e3d8795eb161b999a1d7562694f536ad..dbcae72f4ae6056f9a8bdd283141dab257269fcf 100644 |
--- a/extensions/browser/computed_hashes_unittest.cc |
+++ b/extensions/browser/computed_hashes_unittest.cc |
@@ -2,12 +2,24 @@ |
// Use of this source code is governed by a BSD-style license that can be |
// found in the LICENSE file. |
+#include "base/base64.h" |
#include "base/files/file_path.h" |
#include "base/files/scoped_temp_dir.h" |
#include "crypto/sha2.h" |
#include "extensions/browser/computed_hashes.h" |
#include "testing/gtest/include/gtest/gtest.h" |
+namespace { |
+ |
+// Helper to return base64 encode result by value. |
+std::string Base64Encode(const std::string& data) { |
+ std::string result; |
+ base::Base64Encode(data, &result); |
+ return result; |
+} |
+ |
+} // namespace |
+ |
namespace extensions { |
TEST(ComputedHashes, ComputedHashes) { |
@@ -59,4 +71,47 @@ TEST(ComputedHashes, ComputedHashes) { |
EXPECT_EQ(hashes2, read_hashes2); |
} |
+// Note: the expected hashes used in this test were generated using linux |
+// command line tools. E.g., from a bash prompt: |
+// $ printf "hello world" | openssl dgst -sha256 -binary | base64 |
+// |
+// The file with multiple-blocks expectations were generated by doing: |
+// $ for i in `seq 500 ; do printf "hello world" ; done > hello.txt |
+// $ dd if=hello.txt bs=4096 count=1 | openssl dgst -sha256 -binary | base64 |
+// $ dd if=hello.txt skip=1 bs=4096 count=1 | \ |
+// openssl dgst -sha256 -binary | base64 |
+TEST(ComputedHashes, ComputeHashesForContent) { |
+ const int block_size = 4096; |
+ |
+ // Simple short input. |
+ std::string content1 = "hello world"; |
+ std::string content1_expected_hash = |
+ "uU0nuZNNPgilLlLX2n2r+sSE7+N6U4DukIj3rOLvzek="; |
+ std::vector<std::string> hashes1; |
+ ComputedHashes::ComputeHashesForContent(content1, block_size, &hashes1); |
+ ASSERT_EQ(1u, hashes1.size()); |
+ EXPECT_EQ(content1_expected_hash, Base64Encode(hashes1[0])); |
+ |
+ // Multiple blocks input. |
+ std::string content2; |
+ for (int i = 0; i < 500; i++) |
+ content2 += "hello world"; |
+ const char* content2_expected_hashes[] = { |
+ "bvtt5hXo8xvHrlzGAhhoqPL/r+4zJXHx+6wAvkv15V8=", |
+ "lTD45F7P6I/HOdi8u7FLRA4qzAYL+7xSNVeusG6MJI0="}; |
+ std::vector<std::string> hashes2; |
+ ComputedHashes::ComputeHashesForContent(content2, block_size, &hashes2); |
+ ASSERT_EQ(2u, hashes2.size()); |
+ EXPECT_EQ(content2_expected_hashes[0], Base64Encode(hashes2[0])); |
+ EXPECT_EQ(content2_expected_hashes[1], Base64Encode(hashes2[1])); |
+ |
+ // Now an empty input. |
+ std::string content3; |
+ std::vector<std::string> hashes3; |
+ ComputedHashes::ComputeHashesForContent(content3, block_size, &hashes3); |
+ ASSERT_EQ(1u, hashes3.size()); |
+ ASSERT_EQ(std::string("47DEQpj8HBSa+/TImW+5JCeuQeRkm5NMpJWZG3hSuFU="), |
+ Base64Encode(hashes3[0])); |
+} |
+ |
} // namespace extensions |