Chromium Code Reviews| Index: chrome/browser/extensions/api/storage/storage_schema_manifest_handler_unittest.cc |
| diff --git a/chrome/browser/extensions/api/storage/storage_schema_manifest_handler_unittest.cc b/chrome/browser/extensions/api/storage/storage_schema_manifest_handler_unittest.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..13ba6f0473ca5d67f8ffaa27823766087641d44c |
| --- /dev/null |
| +++ b/chrome/browser/extensions/api/storage/storage_schema_manifest_handler_unittest.cc |
| @@ -0,0 +1,173 @@ |
| +// Copyright (c) 2013 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 "chrome/browser/extensions/api/storage/storage_schema_manifest_handler.h" |
| + |
| +#include <string> |
| +#include <vector> |
| + |
| +#include "base/file_util.h" |
| +#include "base/files/file_path.h" |
| +#include "base/files/scoped_temp_dir.h" |
| +#include "base/json/json_writer.h" |
| +#include "base/values.h" |
| +#include "chrome/common/chrome_version_info.h" |
| +#include "chrome/common/extensions/extension.h" |
| +#include "chrome/common/extensions/features/feature.h" |
| +#include "chrome/common/extensions/manifest.h" |
| +#include "testing/gtest/include/gtest/gtest.h" |
| + |
| +namespace extensions { |
| + |
| +class StorageSchemaManifestHandlerTest : public testing::Test { |
| + public: |
| + StorageSchemaManifestHandlerTest() {} |
| + virtual ~StorageSchemaManifestHandlerTest() {} |
| + |
| + virtual void SetUp() OVERRIDE { |
| + ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); |
| + |
| + channel_ = Feature::GetCurrentChannel(); |
| + Feature::SetCurrentChannel(chrome::VersionInfo::CHANNEL_DEV); |
|
not at google - send to devlin
2013/05/25 00:50:57
There is Feature::ScopedCurrentChannel for this.
Joao da Silva
2013/05/27 12:13:11
Nice, done.
|
| + |
| + manifest_.SetString("name", "test"); |
| + manifest_.SetString("version", "1.2.3.4"); |
| + manifest_.SetInteger("manifest_version", 2); |
| + |
| + storage_schema_manifest_handler_ = new StorageSchemaManifestHandler(); |
| + //storage_schema_manifest_handler_->Register(); |
|
not at google - send to devlin
2013/05/25 00:50:57
hm? it registers itself? when does |handler_| get
Joao da Silva
2013/05/27 12:13:11
Ah, this is a leftover from a version that used a
|
| + // This is used to invoke the private virtual methods. |
| + handler_ = storage_schema_manifest_handler_; |
| + } |
| + |
| + virtual void TearDown() OVERRIDE { |
| + Feature::SetCurrentChannel(channel_); |
| + } |
| + |
| + scoped_refptr<Extension> CreateExtension(const std::string& schema) { |
|
not at google - send to devlin
2013/05/25 00:50:57
Does chrome/common/extensions/extension_builder.h
Joao da Silva
2013/05/27 12:13:11
Yes, using that now.
|
| + std::string content; |
| + base::JSONWriter::Write(&manifest_, &content); |
| + const base::FilePath& path = temp_dir_.path(); |
| + int size = file_util::WriteFile( |
| + path.AppendASCII("manifest.json"), content.data(), content.size()); |
| + if (size != static_cast<int>(content.size())) |
| + return NULL; |
| + if (schema.empty()) { |
| + file_util::Delete(path.AppendASCII("schema.json"), false); |
| + } else { |
| + size = file_util::WriteFile( |
| + path.AppendASCII("schema.json"), schema.data(), schema.size()); |
| + if (size != static_cast<int>(schema.size())) |
| + return NULL; |
| + } |
| + std::string error; |
| + scoped_refptr<Extension> extension = |
| + Extension::Create(path, Manifest::EXTERNAL_POLICY_DOWNLOAD, manifest_, |
| + Extension::NO_FLAGS, &error); |
| + EXPECT_TRUE(error.empty()) << error; |
| + return extension; |
| + } |
| + |
| + bool Validates(const std::string& schema) { |
| + scoped_refptr<Extension> extension = CreateExtension(schema); |
| + if (!extension) |
| + return false; |
| + std::vector<InstallWarning> warnings; |
| + return handler_->Validate(extension.get(), &error_, &warnings); |
| + } |
| + |
| + base::ScopedTempDir temp_dir_; |
| + chrome::VersionInfo::Channel channel_; |
| + ManifestHandler* handler_; |
| + StorageSchemaManifestHandler* storage_schema_manifest_handler_; |
| + base::DictionaryValue manifest_; |
| + std::string error_; |
| +}; |
| + |
| +TEST_F(StorageSchemaManifestHandlerTest, Parse) { |
| + scoped_refptr<Extension> extension = CreateExtension(""); |
| + ASSERT_TRUE(extension); |
| + |
| + // No storage.managed_schema entry. |
| + string16 error; |
| + EXPECT_FALSE(handler_->Parse(extension.get(), &error)); |
| + |
| + // Not a string. |
| + manifest_.SetInteger("storage.managed_schema", 123); |
| + extension = CreateExtension(""); |
| + ASSERT_TRUE(extension); |
| + EXPECT_FALSE(handler_->Parse(extension.get(), &error)); |
| + |
| + // All good now. |
| + manifest_.SetString("storage.managed_schema", "schema.json"); |
| + extension = CreateExtension(""); |
| + ASSERT_TRUE(extension); |
| + EXPECT_TRUE(handler_->Parse(extension.get(), &error)) << error; |
| +} |
| + |
| +TEST_F(StorageSchemaManifestHandlerTest, Validate) { |
| + // Doesn't have storage permission. |
| + EXPECT_FALSE(Validates("")); |
| + |
| + // File doesn't exist. |
| + base::ListValue permissions; |
| + permissions.AppendString("storage"); |
| + manifest_.Set("permissions", permissions.DeepCopy()); |
| + EXPECT_FALSE(Validates("")); |
| + |
| + // Absolute path. |
| + manifest_.SetString("storage.managed_storage", "/etc/passwd"); |
| + EXPECT_FALSE(Validates("")); |
| + |
| + // Path with .. |
| + manifest_.SetString("storage.managed_storage", "../../../../../etc/passwd"); |
| + EXPECT_FALSE(Validates("")); |
| + |
| + // Does not exist. |
| + manifest_.SetString("storage.managed_storage", "not-there"); |
| + EXPECT_FALSE(Validates("")); |
| + |
| + // Invalid JSON. |
| + manifest_.SetString("storage.managed_schema", "schema.json"); |
| + EXPECT_FALSE(Validates("-invalid-")); |
| + |
| + // No version. |
| + EXPECT_FALSE(Validates("{}")); |
| + |
| + // Invalid version. |
| + EXPECT_FALSE(Validates( |
| + "{" |
| + " \"$schema\": \"http://json-schema.org/draft-42/schema#\"" |
| + "}")); |
| + |
| + // Missing type. |
| + EXPECT_FALSE(Validates( |
| + "{" |
| + " \"$schema\": \"http://json-schema.org/draft-03/schema#\"" |
| + "}")); |
| + |
| + // Invalid type. |
| + EXPECT_FALSE(Validates( |
| + "{" |
| + " \"$schema\": \"http://json-schema.org/draft-03/schema#\"," |
| + " \"type\": \"string\"" |
| + "}")); |
| + |
| + // "additionalProperties" not supported at top level. |
| + EXPECT_FALSE(Validates( |
| + "{" |
| + " \"$schema\": \"http://json-schema.org/draft-03/schema#\"," |
| + " \"type\": \"object\"," |
| + " \"additionalProperties\": {}" |
| + "}")); |
| + |
| + // All good now. |
| + EXPECT_TRUE(Validates( |
| + "{" |
| + " \"$schema\": \"http://json-schema.org/draft-03/schema#\"," |
| + " \"type\": \"object\"" |
| + "}")) << error_; |
|
not at google - send to devlin
2013/05/25 00:50:57
if you make Validates return a testing::AssertionR
Joao da Silva
2013/05/27 12:13:11
Nice, done. Going to use this pattern more often,
|
| +} |
| + |
| +} // namespace extensions |