summaryrefslogtreecommitdiffstats
path: root/extensions/browser/verified_contents_unittest.cc
diff options
context:
space:
mode:
authorasargent@chromium.org <asargent@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2014-05-09 15:11:03 +0000
committerasargent@chromium.org <asargent@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2014-05-09 15:11:03 +0000
commit3dc81f218677e2468be0c09215b1eeedf1e35eba (patch)
tree40d261604e0a48078221374a0deeef18a1235789 /extensions/browser/verified_contents_unittest.cc
parentc0af90aec0d1ccedb3ede85d85cf33d5b3fca4a3 (diff)
downloadchromium_src-3dc81f218677e2468be0c09215b1eeedf1e35eba.zip
chromium_src-3dc81f218677e2468be0c09215b1eeedf1e35eba.tar.gz
chromium_src-3dc81f218677e2468be0c09215b1eeedf1e35eba.tar.bz2
Add a class for parsing extension content verification data
The webstore will be vending a "verified_contents.json" file for extensions hosted there that contains a signed list of expected block hashes for the files contained in an extension. This new class handles parsing/validation of that data. BUG=369895 R=rockot@chromium.org Review URL: https://codereview.chromium.org/278593005 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@269305 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'extensions/browser/verified_contents_unittest.cc')
-rw-r--r--extensions/browser/verified_contents_unittest.cc86
1 files changed, 86 insertions, 0 deletions
diff --git a/extensions/browser/verified_contents_unittest.cc b/extensions/browser/verified_contents_unittest.cc
new file mode 100644
index 0000000..50e2655
--- /dev/null
+++ b/extensions/browser/verified_contents_unittest.cc
@@ -0,0 +1,86 @@
+// Copyright 2014 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include <string>
+#include <vector>
+
+#include "base/file_util.h"
+#include "base/files/file_path.h"
+#include "base/path_service.h"
+#include "base/stl_util.h"
+#include "base/strings/string_number_conversions.h"
+#include "extensions/browser/verified_contents.h"
+#include "extensions/common/extension.h"
+#include "extensions/common/extension_paths.h"
+#include "testing/gtest/include/gtest/gtest.h"
+
+namespace extensions {
+
+namespace {
+
+bool HexStringEquals(std::string hex_string, const std::string* bytes) {
+ if (!bytes)
+ return false;
+ std::vector<uint8> decoded;
+ if (!base::HexStringToBytes(hex_string, &decoded))
+ return false;
+ if (decoded.size() != bytes->size())
+ return false;
+
+ if (bytes->empty())
+ return true;
+
+ return memcmp(vector_as_array(&decoded), bytes->data(), bytes->size()) == 0;
+}
+
+bool GetPublicKey(const base::FilePath& path, std::string* public_key) {
+ std::string public_key_pem;
+ if (!base::ReadFileToString(path, &public_key_pem))
+ return false;
+ if (!Extension::ParsePEMKeyBytes(public_key_pem, public_key))
+ return false;
+ return true;
+}
+
+} // namespace
+
+TEST(VerifiedContents, Simple) {
+ // Figure out our test data directory.
+ base::FilePath path;
+ PathService::Get(DIR_TEST_DATA, &path);
+ path = path.AppendASCII("content_verifier/");
+
+ // Initialize the VerifiedContents object.
+ std::string public_key;
+ ASSERT_TRUE(GetPublicKey(path.AppendASCII("public_key.pem"), &public_key));
+ VerifiedContents contents(reinterpret_cast<const uint8*>(public_key.data()),
+ public_key.size());
+ base::FilePath verified_contents_path =
+ path.AppendASCII("verified_contents.json");
+
+ ASSERT_TRUE(contents.InitFrom(verified_contents_path, false));
+
+ // Make sure we get expected values.
+ EXPECT_EQ(contents.block_size(), 4096);
+ EXPECT_EQ(contents.extension_id(), "abcdefghijklmnopabcdefghijklmnop");
+ EXPECT_EQ("1.2.3", contents.version().GetString());
+
+ EXPECT_TRUE(HexStringEquals(
+ "fafcb22089fb8920b383b5f7202508e7065aded1bbc3bbf2882724c5974919fb",
+ contents.GetTreeHashRoot(
+ base::FilePath::FromUTF8Unsafe("manifest.json"))));
+ EXPECT_TRUE(HexStringEquals(
+ "b711e21b9290bcda0f3921f915b428f596f9809db78f7a0507446ef433a7ce2c",
+ contents.GetTreeHashRoot(
+ base::FilePath::FromUTF8Unsafe("background.js"))));
+ EXPECT_TRUE(HexStringEquals(
+ "2f7ecb15b4ff866b7144bec07c664df584e95baca8cff662435a292c99f53595",
+ contents.GetTreeHashRoot(
+ base::FilePath::FromUTF8Unsafe("foo/bar.html"))));
+
+ base::FilePath nonexistent = base::FilePath::FromUTF8Unsafe("nonexistent");
+ EXPECT_TRUE(contents.GetTreeHashRoot(nonexistent) == NULL);
+}
+
+} // namespace extensions