Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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 "base/base64.h" | |
| 5 #include "base/files/file_path.h" | 6 #include "base/files/file_path.h" |
| 6 #include "base/files/scoped_temp_dir.h" | 7 #include "base/files/scoped_temp_dir.h" |
| 7 #include "crypto/sha2.h" | 8 #include "crypto/sha2.h" |
| 8 #include "extensions/browser/computed_hashes.h" | 9 #include "extensions/browser/computed_hashes.h" |
| 9 #include "testing/gtest/include/gtest/gtest.h" | 10 #include "testing/gtest/include/gtest/gtest.h" |
| 10 | 11 |
| 12 namespace { | |
| 13 | |
| 14 // Helper to return base64 encode result by value. | |
| 15 std::string Base64Encode(std::string data) { | |
|
Ken Rockot(use gerrit already)
2014/08/05 23:10:32
nit: Might as well pass |data| as a const ref?
asargent_no_longer_on_chrome
2014/08/06 04:47:33
Good catch, done.
| |
| 16 std::string result; | |
| 17 base::Base64Encode(data, &result); | |
| 18 return result; | |
| 19 } | |
| 20 | |
| 21 } // namespace | |
| 22 | |
| 11 namespace extensions { | 23 namespace extensions { |
| 12 | 24 |
| 13 TEST(ComputedHashes, ComputedHashes) { | 25 TEST(ComputedHashes, ComputedHashes) { |
| 14 base::ScopedTempDir scoped_dir; | 26 base::ScopedTempDir scoped_dir; |
| 15 ASSERT_TRUE(scoped_dir.CreateUniqueTempDir()); | 27 ASSERT_TRUE(scoped_dir.CreateUniqueTempDir()); |
| 16 base::FilePath computed_hashes = | 28 base::FilePath computed_hashes = |
| 17 scoped_dir.path().AppendASCII("computed_hashes.json"); | 29 scoped_dir.path().AppendASCII("computed_hashes.json"); |
| 18 | 30 |
| 19 // We'll add hashes for 2 files, one of which uses a subdirectory | 31 // We'll add hashes for 2 files, one of which uses a subdirectory |
| 20 // path. The first file will have a list of 1 block hash, and the | 32 // path. The first file will have a list of 1 block hash, and the |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 52 | 64 |
| 53 // Finally make sure that we can retrieve the hashes for the subdir | 65 // Finally make sure that we can retrieve the hashes for the subdir |
| 54 // path even when that path contains forward slashes (on windows). | 66 // path even when that path contains forward slashes (on windows). |
| 55 base::FilePath path2_fwd_slashes = | 67 base::FilePath path2_fwd_slashes = |
| 56 base::FilePath::FromUTF8Unsafe("foo/bar.txt"); | 68 base::FilePath::FromUTF8Unsafe("foo/bar.txt"); |
| 57 block_size = 0; | 69 block_size = 0; |
| 58 EXPECT_TRUE(reader.GetHashes(path2_fwd_slashes, &block_size, &read_hashes2)); | 70 EXPECT_TRUE(reader.GetHashes(path2_fwd_slashes, &block_size, &read_hashes2)); |
| 59 EXPECT_EQ(hashes2, read_hashes2); | 71 EXPECT_EQ(hashes2, read_hashes2); |
| 60 } | 72 } |
| 61 | 73 |
| 74 // Note: the expected hashes used in this test were generated using linux | |
| 75 // command line tools. E.g., from a bash prompt: | |
| 76 // $ printf "hello world" | openssl dgst -sha256 -binary | base64 | |
| 77 // | |
| 78 // The file with multiple-blocks expectations were generated by doing: | |
| 79 // $ for i in `seq 500 ; do printf "hello world" ; done > hello.txt | |
| 80 // $ dd if=hello.txt bs=4096 count=1 | openssl dgst -sha256 -binary | base64 | |
| 81 // $ dd if=hello.txt skip=1 bs=4096 count=1 | \ | |
| 82 // openssl dgst -sha256 -binary | base64 | |
| 83 TEST(ComputedHashes, ComputeHashesForContent) { | |
| 84 const int block_size = 4096; | |
| 85 | |
| 86 // Simple short input. | |
| 87 std::string content1 = "hello world"; | |
| 88 std::string content1_expected_hash = | |
| 89 "uU0nuZNNPgilLlLX2n2r+sSE7+N6U4DukIj3rOLvzek="; | |
| 90 std::vector<std::string> hashes1; | |
| 91 ComputedHashes::ComputeHashesForContent(content1, block_size, &hashes1); | |
| 92 ASSERT_EQ(1u, hashes1.size()); | |
| 93 EXPECT_EQ(content1_expected_hash, Base64Encode(hashes1[0])); | |
| 94 | |
| 95 // Multiple blocks input. | |
| 96 std::string content2; | |
| 97 for (int i = 0; i < 500; i++) | |
| 98 content2 += "hello world"; | |
| 99 const char* content2_expected_hashes[] = { | |
| 100 "bvtt5hXo8xvHrlzGAhhoqPL/r+4zJXHx+6wAvkv15V8=", | |
| 101 "lTD45F7P6I/HOdi8u7FLRA4qzAYL+7xSNVeusG6MJI0="}; | |
| 102 std::vector<std::string> hashes2; | |
| 103 ComputedHashes::ComputeHashesForContent(content2, block_size, &hashes2); | |
| 104 ASSERT_EQ(2u, hashes2.size()); | |
| 105 EXPECT_EQ(content2_expected_hashes[0], Base64Encode(hashes2[0])); | |
| 106 EXPECT_EQ(content2_expected_hashes[1], Base64Encode(hashes2[1])); | |
| 107 | |
| 108 // Now an empty input. | |
| 109 std::string content3; | |
| 110 std::vector<std::string> hashes3; | |
| 111 ComputedHashes::ComputeHashesForContent(content3, block_size, &hashes3); | |
| 112 ASSERT_EQ(1u, hashes3.size()); | |
| 113 ASSERT_EQ(std::string("47DEQpj8HBSa+/TImW+5JCeuQeRkm5NMpJWZG3hSuFU="), | |
| 114 Base64Encode(hashes3[0])); | |
| 115 } | |
| 116 | |
| 62 } // namespace extensions | 117 } // namespace extensions |
| OLD | NEW |