1 // Copyright 2013 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.
5 #include "chrome/common/extensions/api/storage/storage_schema_manifest_handler.h"
10 #include "base/files/file_path.h"
11 #include "base/files/file_util.h"
12 #include "base/files/scoped_temp_dir.h"
13 #include "base/values.h"
14 #include "chrome/common/chrome_version_info.h"
15 #include "chrome/common/extensions/features/feature_channel.h"
16 #include "extensions/common/extension.h"
17 #include "extensions/common/file_util.h"
18 #include "extensions/common/manifest.h"
19 #include "testing/gtest/include/gtest/gtest.h"
21 namespace extensions {
23 class StorageSchemaManifestHandlerTest : public testing::Test {
25 StorageSchemaManifestHandlerTest()
26 : scoped_channel_(chrome::VersionInfo::CHANNEL_DEV) {}
28 ~StorageSchemaManifestHandlerTest() override {}
30 void SetUp() override {
31 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir());
33 manifest_.SetString("name", "test");
34 manifest_.SetString("version", "1.2.3.4");
35 manifest_.SetInteger("manifest_version", 2);
38 scoped_refptr<Extension> CreateExtension(const std::string& schema) {
40 scoped_refptr<Extension> extension = Extension::Create(
41 temp_dir_.path(), Manifest::UNPACKED, manifest_,
42 Extension::NO_FLAGS, "", &error);
45 base::FilePath schema_path = temp_dir_.path().AppendASCII("schema.json");
47 base::DeleteFile(schema_path, false);
49 if (base::WriteFile(schema_path, schema.data(), schema.size()) !=
50 static_cast<int>(schema.size())) {
57 testing::AssertionResult Validates(const std::string& schema) {
58 scoped_refptr<Extension> extension = CreateExtension(schema);
60 return testing::AssertionFailure() << "Failed to create test extension";
62 std::vector<InstallWarning> warnings;
63 if (file_util::ValidateExtension(extension.get(), &error, &warnings))
64 return testing::AssertionSuccess();
65 return testing::AssertionFailure() << error;
68 base::ScopedTempDir temp_dir_;
69 ScopedCurrentChannel scoped_channel_;
70 base::DictionaryValue manifest_;
73 TEST_F(StorageSchemaManifestHandlerTest, Parse) {
74 scoped_refptr<Extension> extension = CreateExtension("");
75 ASSERT_TRUE(extension.get());
78 manifest_.SetInteger("storage.managed_schema", 123);
79 extension = CreateExtension("");
80 EXPECT_FALSE(extension.get());
83 manifest_.SetString("storage.managed_schema", "schema.json");
84 extension = CreateExtension("");
85 ASSERT_TRUE(extension.get());
88 TEST_F(StorageSchemaManifestHandlerTest, Validate) {
89 base::ListValue permissions;
90 permissions.AppendString("storage");
91 manifest_.Set("permissions", permissions.DeepCopy());
93 #if defined(ENABLE_CONFIGURATION_POLICY)
95 manifest_.SetString("storage.managed_schema", "/etc/passwd");
96 EXPECT_FALSE(Validates(""));
99 manifest_.SetString("storage.managed_schema", "../../../../../etc/passwd");
100 EXPECT_FALSE(Validates(""));
103 manifest_.SetString("storage.managed_schema", "not-there");
104 EXPECT_FALSE(Validates(""));
107 manifest_.SetString("storage.managed_schema", "schema.json");
108 EXPECT_FALSE(Validates("-invalid-"));
111 EXPECT_FALSE(Validates("{}"));
114 EXPECT_FALSE(Validates(
116 " \"$schema\": \"http://json-schema.org/draft-42/schema#\""
120 EXPECT_FALSE(Validates(
122 " \"$schema\": \"http://json-schema.org/draft-03/schema#\""
126 EXPECT_FALSE(Validates(
128 " \"$schema\": \"http://json-schema.org/draft-03/schema#\","
129 " \"type\": \"string\""
132 // "additionalProperties" not supported at top level.
133 EXPECT_FALSE(Validates(
135 " \"$schema\": \"http://json-schema.org/draft-03/schema#\","
136 " \"type\": \"object\","
137 " \"additionalProperties\": {}"
142 EXPECT_TRUE(Validates(
144 " \"$schema\": \"http://json-schema.org/draft-03/schema#\","
145 " \"type\": \"object\""
149 } // namespace extensions