OLD | NEW |
(Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "base/files/file_path.h" |
| 6 #include "base/files/scoped_temp_dir.h" |
| 7 #include "crypto/sha2.h" |
| 8 #include "extensions/browser/computed_hashes.h" |
| 9 #include "testing/gtest/include/gtest/gtest.h" |
| 10 |
| 11 namespace extensions { |
| 12 |
| 13 TEST(ComputedHashes, ComputedHashes) { |
| 14 base::ScopedTempDir scoped_dir; |
| 15 ASSERT_TRUE(scoped_dir.CreateUniqueTempDir()); |
| 16 base::FilePath computed_hashes = |
| 17 scoped_dir.path().AppendASCII("computed_hashes.json"); |
| 18 |
| 19 // 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 |
| 21 // second file will have 2 block hashes. |
| 22 base::FilePath path1(FILE_PATH_LITERAL("foo.txt")); |
| 23 base::FilePath path2 = |
| 24 base::FilePath(FILE_PATH_LITERAL("foo")).AppendASCII("bar.txt"); |
| 25 std::vector<std::string> hashes1; |
| 26 std::vector<std::string> hashes2; |
| 27 hashes1.push_back(crypto::SHA256HashString("first")); |
| 28 hashes2.push_back(crypto::SHA256HashString("second")); |
| 29 hashes2.push_back(crypto::SHA256HashString("third")); |
| 30 |
| 31 // Write them into the file. |
| 32 ComputedHashes::Writer writer; |
| 33 writer.AddHashes(path1, 4096, hashes1); |
| 34 writer.AddHashes(path2, 2048, hashes2); |
| 35 EXPECT_TRUE(writer.WriteToFile(computed_hashes)); |
| 36 |
| 37 // Now read them back again and assert that we got what we wrote. |
| 38 ComputedHashes::Reader reader; |
| 39 std::vector<std::string> read_hashes1; |
| 40 std::vector<std::string> read_hashes2; |
| 41 EXPECT_TRUE(reader.InitFromFile(computed_hashes)); |
| 42 |
| 43 int block_size = 0; |
| 44 EXPECT_TRUE(reader.GetHashes(path1, &block_size, &read_hashes1)); |
| 45 EXPECT_EQ(block_size, 4096); |
| 46 block_size = 0; |
| 47 EXPECT_TRUE(reader.GetHashes(path2, &block_size, &read_hashes2)); |
| 48 EXPECT_EQ(block_size, 2048); |
| 49 |
| 50 EXPECT_EQ(hashes1, read_hashes1); |
| 51 EXPECT_EQ(hashes2, read_hashes2); |
| 52 |
| 53 // Finally make sure that we can retrieve the hashes for the subdir |
| 54 // path even when that path contains forward slashes (on windows). |
| 55 base::FilePath path2_fwd_slashes = |
| 56 base::FilePath::FromUTF8Unsafe("foo/bar.txt"); |
| 57 block_size = 0; |
| 58 EXPECT_TRUE(reader.GetHashes(path2_fwd_slashes, &block_size, &read_hashes2)); |
| 59 EXPECT_EQ(hashes2, read_hashes2); |
| 60 } |
| 61 |
| 62 } // namespace extensions |
OLD | NEW |