OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chrome/browser/extensions/api/storage/storage_schema_manifest_handler.
h" | 5 #include "chrome/browser/extensions/api/storage/storage_schema_manifest_handler.
h" |
6 | 6 |
7 #include <string> | 7 #include <string> |
8 #include <vector> | 8 #include <vector> |
9 | 9 |
10 #include "base/file_util.h" | 10 #include "base/file_util.h" |
(...skipping 26 matching lines...) Expand all Loading... |
37 storage_schema_manifest_handler_->Register(); | 37 storage_schema_manifest_handler_->Register(); |
38 // This is used to invoke the private virtual methods. | 38 // This is used to invoke the private virtual methods. |
39 handler_ = storage_schema_manifest_handler_; | 39 handler_ = storage_schema_manifest_handler_; |
40 } | 40 } |
41 | 41 |
42 scoped_refptr<Extension> CreateExtension(const std::string& schema) { | 42 scoped_refptr<Extension> CreateExtension(const std::string& schema) { |
43 std::string error; | 43 std::string error; |
44 scoped_refptr<Extension> extension = Extension::Create( | 44 scoped_refptr<Extension> extension = Extension::Create( |
45 temp_dir_.path(), Manifest::UNPACKED, manifest_, | 45 temp_dir_.path(), Manifest::UNPACKED, manifest_, |
46 Extension::NO_FLAGS, "", &error); | 46 Extension::NO_FLAGS, "", &error); |
47 if (!extension) | 47 if (!extension.get()) |
48 return NULL; | 48 return NULL; |
49 base::FilePath schema_path = temp_dir_.path().AppendASCII("schema.json"); | 49 base::FilePath schema_path = temp_dir_.path().AppendASCII("schema.json"); |
50 if (schema.empty()) { | 50 if (schema.empty()) { |
51 file_util::Delete(schema_path, false); | 51 file_util::Delete(schema_path, false); |
52 } else { | 52 } else { |
53 if (file_util::WriteFile(schema_path, schema.data(), schema.size()) != | 53 if (file_util::WriteFile(schema_path, schema.data(), schema.size()) != |
54 static_cast<int>(schema.size())) { | 54 static_cast<int>(schema.size())) { |
55 return NULL; | 55 return NULL; |
56 } | 56 } |
57 } | 57 } |
58 return extension; | 58 return extension; |
59 } | 59 } |
60 | 60 |
61 testing::AssertionResult Validates(const std::string& schema) { | 61 testing::AssertionResult Validates(const std::string& schema) { |
62 scoped_refptr<Extension> extension = CreateExtension(schema); | 62 scoped_refptr<Extension> extension = CreateExtension(schema); |
63 if (!extension) | 63 if (!extension.get()) |
64 return testing::AssertionFailure() << "Failed to create test extension"; | 64 return testing::AssertionFailure() << "Failed to create test extension"; |
65 std::string error; | 65 std::string error; |
66 std::vector<InstallWarning> warnings; | 66 std::vector<InstallWarning> warnings; |
67 if (handler_->Validate(extension.get(), &error, &warnings)) | 67 if (handler_->Validate(extension.get(), &error, &warnings)) |
68 return testing::AssertionSuccess(); | 68 return testing::AssertionSuccess(); |
69 return testing::AssertionFailure() << error; | 69 return testing::AssertionFailure() << error; |
70 } | 70 } |
71 | 71 |
72 base::ScopedTempDir temp_dir_; | 72 base::ScopedTempDir temp_dir_; |
73 Feature::ScopedCurrentChannel scoped_channel_; | 73 Feature::ScopedCurrentChannel scoped_channel_; |
74 ManifestHandler* handler_; | 74 ManifestHandler* handler_; |
75 StorageSchemaManifestHandler* storage_schema_manifest_handler_; | 75 StorageSchemaManifestHandler* storage_schema_manifest_handler_; |
76 base::DictionaryValue manifest_; | 76 base::DictionaryValue manifest_; |
77 }; | 77 }; |
78 | 78 |
79 TEST_F(StorageSchemaManifestHandlerTest, Parse) { | 79 TEST_F(StorageSchemaManifestHandlerTest, Parse) { |
80 scoped_refptr<Extension> extension = CreateExtension(""); | 80 scoped_refptr<Extension> extension = CreateExtension(""); |
81 ASSERT_TRUE(extension); | 81 ASSERT_TRUE(extension.get()); |
82 | 82 |
83 // No storage.managed_schema entry. | 83 // No storage.managed_schema entry. |
84 string16 error; | 84 string16 error; |
85 EXPECT_FALSE(handler_->Parse(extension.get(), &error)); | 85 EXPECT_FALSE(handler_->Parse(extension.get(), &error)); |
86 | 86 |
87 // Not a string. | 87 // Not a string. |
88 manifest_.SetInteger("storage.managed_schema", 123); | 88 manifest_.SetInteger("storage.managed_schema", 123); |
89 extension = CreateExtension(""); | 89 extension = CreateExtension(""); |
90 EXPECT_FALSE(extension); | 90 EXPECT_FALSE(extension.get()); |
91 | 91 |
92 // All good now. | 92 // All good now. |
93 manifest_.SetString("storage.managed_schema", "schema.json"); | 93 manifest_.SetString("storage.managed_schema", "schema.json"); |
94 extension = CreateExtension(""); | 94 extension = CreateExtension(""); |
95 ASSERT_TRUE(extension); | 95 ASSERT_TRUE(extension.get()); |
96 EXPECT_TRUE(handler_->Parse(extension.get(), &error)) << error; | 96 EXPECT_TRUE(handler_->Parse(extension.get(), &error)) << error; |
97 } | 97 } |
98 | 98 |
99 TEST_F(StorageSchemaManifestHandlerTest, Validate) { | 99 TEST_F(StorageSchemaManifestHandlerTest, Validate) { |
100 // Doesn't have storage permission. | 100 // Doesn't have storage permission. |
101 EXPECT_FALSE(Validates("")); | 101 EXPECT_FALSE(Validates("")); |
102 | 102 |
103 // File doesn't exist. | 103 // File doesn't exist. |
104 base::ListValue permissions; | 104 base::ListValue permissions; |
105 permissions.AppendString("storage"); | 105 permissions.AppendString("storage"); |
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
154 | 154 |
155 // All good now. | 155 // All good now. |
156 EXPECT_TRUE(Validates( | 156 EXPECT_TRUE(Validates( |
157 "{" | 157 "{" |
158 " \"$schema\": \"http://json-schema.org/draft-03/schema#\"," | 158 " \"$schema\": \"http://json-schema.org/draft-03/schema#\"," |
159 " \"type\": \"object\"" | 159 " \"type\": \"object\"" |
160 "}")); | 160 "}")); |
161 } | 161 } |
162 | 162 |
163 } // namespace extensions | 163 } // namespace extensions |
OLD | NEW |