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 <string> |
| 6 #include <vector> |
| 7 |
| 8 #include "base/file_util.h" |
| 9 #include "base/files/file_path.h" |
| 10 #include "base/path_service.h" |
| 11 #include "base/stl_util.h" |
| 12 #include "base/strings/string_number_conversions.h" |
| 13 #include "extensions/browser/verified_contents.h" |
| 14 #include "extensions/common/extension.h" |
| 15 #include "extensions/common/extension_paths.h" |
| 16 #include "testing/gtest/include/gtest/gtest.h" |
| 17 |
| 18 namespace extensions { |
| 19 |
| 20 namespace { |
| 21 |
| 22 bool HexStringEquals(std::string hex_string, const std::string* bytes) { |
| 23 if (!bytes) |
| 24 return false; |
| 25 std::vector<uint8> decoded; |
| 26 if (!base::HexStringToBytes(hex_string, &decoded)) |
| 27 return false; |
| 28 if (decoded.size() != bytes->size()) |
| 29 return false; |
| 30 |
| 31 if (bytes->empty()) |
| 32 return true; |
| 33 |
| 34 return memcmp(vector_as_array(&decoded), bytes->data(), bytes->size()) == 0; |
| 35 } |
| 36 |
| 37 bool GetPublicKey(const base::FilePath& path, std::string* public_key) { |
| 38 std::string public_key_pem; |
| 39 if (!base::ReadFileToString(path, &public_key_pem)) |
| 40 return false; |
| 41 if (!Extension::ParsePEMKeyBytes(public_key_pem, public_key)) |
| 42 return false; |
| 43 return true; |
| 44 } |
| 45 |
| 46 } // namespace |
| 47 |
| 48 TEST(VerifiedContents, Simple) { |
| 49 // Figure out our test data directory. |
| 50 base::FilePath path; |
| 51 PathService::Get(DIR_TEST_DATA, &path); |
| 52 path = path.AppendASCII("content_verifier/"); |
| 53 |
| 54 // Initialize the VerifiedContents object. |
| 55 std::string public_key; |
| 56 ASSERT_TRUE(GetPublicKey(path.AppendASCII("public_key.pem"), &public_key)); |
| 57 VerifiedContents contents(reinterpret_cast<const uint8*>(public_key.data()), |
| 58 public_key.size()); |
| 59 base::FilePath verified_contents_path = |
| 60 path.AppendASCII("verified_contents.json"); |
| 61 |
| 62 ASSERT_TRUE(contents.InitFrom(verified_contents_path, false)); |
| 63 |
| 64 // Make sure we get expected values. |
| 65 EXPECT_EQ(contents.block_size(), 4096); |
| 66 EXPECT_EQ(contents.extension_id(), "abcdefghijklmnopabcdefghijklmnop"); |
| 67 EXPECT_EQ("1.2.3", contents.version().GetString()); |
| 68 |
| 69 EXPECT_TRUE(HexStringEquals( |
| 70 "fafcb22089fb8920b383b5f7202508e7065aded1bbc3bbf2882724c5974919fb", |
| 71 contents.GetTreeHashRoot( |
| 72 base::FilePath::FromUTF8Unsafe("manifest.json")))); |
| 73 EXPECT_TRUE(HexStringEquals( |
| 74 "b711e21b9290bcda0f3921f915b428f596f9809db78f7a0507446ef433a7ce2c", |
| 75 contents.GetTreeHashRoot( |
| 76 base::FilePath::FromUTF8Unsafe("background.js")))); |
| 77 EXPECT_TRUE(HexStringEquals( |
| 78 "2f7ecb15b4ff866b7144bec07c664df584e95baca8cff662435a292c99f53595", |
| 79 contents.GetTreeHashRoot( |
| 80 base::FilePath::FromUTF8Unsafe("foo/bar.html")))); |
| 81 |
| 82 base::FilePath nonexistent = base::FilePath::FromUTF8Unsafe("nonexistent"); |
| 83 EXPECT_TRUE(contents.GetTreeHashRoot(nonexistent) == NULL); |
| 84 } |
| 85 |
| 86 } // namespace extensions |
OLD | NEW |