| 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/memory/scoped_ptr.h" | 5 #include "extensions/browser/content_hash_tree.h" |
| 6 |
| 7 #include <memory> |
| 8 |
| 6 #include "base/stl_util.h" | 9 #include "base/stl_util.h" |
| 7 #include "crypto/secure_hash.h" | 10 #include "crypto/secure_hash.h" |
| 8 #include "crypto/sha2.h" | 11 #include "crypto/sha2.h" |
| 9 #include "extensions/browser/content_hash_tree.h" | |
| 10 #include "testing/gtest/include/gtest/gtest.h" | 12 #include "testing/gtest/include/gtest/gtest.h" |
| 11 | 13 |
| 12 using crypto::kSHA256Length; | 14 using crypto::kSHA256Length; |
| 13 using crypto::SecureHash; | 15 using crypto::SecureHash; |
| 14 | 16 |
| 15 // Helper to return a fake sha256 signature based on a seed. | 17 // Helper to return a fake sha256 signature based on a seed. |
| 16 static std::string FakeSignatureWithSeed(int seed) { | 18 static std::string FakeSignatureWithSeed(int seed) { |
| 17 std::string input; | 19 std::string input; |
| 18 for (int i = 0; i < seed * 3; i++) { | 20 for (int i = 0; i < seed * 3; i++) { |
| 19 input.push_back(static_cast<char>(((i + 19) * seed) % 256)); | 21 input.push_back(static_cast<char>(((i + 19) * seed) % 256)); |
| (...skipping 11 matching lines...) Expand all Loading... |
| 31 // One node. | 33 // One node. |
| 32 std::string node1 = FakeSignatureWithSeed(1); | 34 std::string node1 = FakeSignatureWithSeed(1); |
| 33 nodes.push_back(node1); | 35 nodes.push_back(node1); |
| 34 EXPECT_EQ(node1, ComputeTreeHashRoot(nodes, 16)); | 36 EXPECT_EQ(node1, ComputeTreeHashRoot(nodes, 16)); |
| 35 | 37 |
| 36 // Two nodes. | 38 // Two nodes. |
| 37 std::string node2 = FakeSignatureWithSeed(2); | 39 std::string node2 = FakeSignatureWithSeed(2); |
| 38 nodes.push_back(node2); | 40 nodes.push_back(node2); |
| 39 | 41 |
| 40 std::string expected(kSHA256Length, 0); | 42 std::string expected(kSHA256Length, 0); |
| 41 scoped_ptr<SecureHash> hash(SecureHash::Create(SecureHash::SHA256)); | 43 std::unique_ptr<SecureHash> hash(SecureHash::Create(SecureHash::SHA256)); |
| 42 hash->Update(node1.data(), node1.size()); | 44 hash->Update(node1.data(), node1.size()); |
| 43 hash->Update(node2.data(), node2.size()); | 45 hash->Update(node2.data(), node2.size()); |
| 44 hash->Finish(string_as_array(&expected), expected.size()); | 46 hash->Finish(string_as_array(&expected), expected.size()); |
| 45 EXPECT_EQ(expected, ComputeTreeHashRoot(nodes, 16)); | 47 EXPECT_EQ(expected, ComputeTreeHashRoot(nodes, 16)); |
| 46 } | 48 } |
| 47 | 49 |
| 48 TEST(ContentHashTreeTest, HashTreeMultipleLevels) { | 50 TEST(ContentHashTreeTest, HashTreeMultipleLevels) { |
| 49 std::vector<std::string> nodes; | 51 std::vector<std::string> nodes; |
| 50 for (int i = 0; i < 3; i++) { | 52 for (int i = 0; i < 3; i++) { |
| 51 std::string node; | 53 std::string node; |
| (...skipping 16 matching lines...) Expand all Loading... |
| 68 // 1 2 3 | 70 // 1 2 3 |
| 69 // where 4 is the hash of 1 and 2, 5 is the hash of 3, and 6 is the | 71 // where 4 is the hash of 1 and 2, 5 is the hash of 3, and 6 is the |
| 70 // hash of 4 and 5. | 72 // hash of 4 and 5. |
| 71 std::string hash_of_first_2 = crypto::SHA256HashString(nodes[0] + nodes[1]); | 73 std::string hash_of_first_2 = crypto::SHA256HashString(nodes[0] + nodes[1]); |
| 72 std::string hash_of_third = crypto::SHA256HashString(nodes[2]); | 74 std::string hash_of_third = crypto::SHA256HashString(nodes[2]); |
| 73 expected = crypto::SHA256HashString(hash_of_first_2 + hash_of_third); | 75 expected = crypto::SHA256HashString(hash_of_first_2 + hash_of_third); |
| 74 EXPECT_EQ(expected, ComputeTreeHashRoot(nodes, 2)); | 76 EXPECT_EQ(expected, ComputeTreeHashRoot(nodes, 2)); |
| 75 } | 77 } |
| 76 | 78 |
| 77 } // namespace extensions | 79 } // namespace extensions |
| OLD | NEW |