1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471
|
// Copyright 2014 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "components/services/app_service/public/cpp/file_handler_info.h"
#include "base/memory/scoped_refptr.h"
#include "base/strings/stringprintf.h"
#include "base/test/values_test_util.h"
#include "extensions/common/manifest_constants.h"
#include "extensions/common/manifest_handlers/file_handler_info.h"
#include "extensions/common/manifest_handlers/web_file_handlers_info.h"
#include "extensions/common/manifest_test.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace extensions {
namespace errors = manifest_errors;
using FileHandlersManifestTest = ManifestTest;
TEST_F(FileHandlersManifestTest, InvalidFileHandlers) {
const Testcase testcases[] = {
Testcase("file_handlers_invalid_handlers.json",
errors::kInvalidFileHandlers),
Testcase("file_handlers_invalid_type.json",
errors::kInvalidFileHandlerType),
Testcase("file_handlers_invalid_extension.json",
errors::kInvalidFileHandlerExtension),
Testcase("file_handlers_invalid_no_type_or_extension.json",
errors::kInvalidFileHandlerNoTypeOrExtension),
Testcase("file_handlers_invalid_type_element.json",
errors::kInvalidFileHandlerTypeElement),
Testcase("file_handlers_invalid_extension_element.json",
errors::kInvalidFileHandlerExtensionElement),
Testcase("file_handlers_invalid_too_many.json",
errors::kInvalidFileHandlersTooManyTypesAndExtensions),
Testcase("file_handlers_invalid_include_directories.json",
errors::kInvalidFileHandlerIncludeDirectories),
Testcase("file_handlers_invalid_verb.json",
errors::kInvalidFileHandlerVerb),
};
RunTestcases(testcases, EXPECT_TYPE_ERROR);
}
TEST_F(FileHandlersManifestTest, ValidFileHandlers) {
scoped_refptr<const Extension> extension =
LoadAndExpectSuccess("file_handlers_valid.json");
ASSERT_TRUE(extension.get());
const FileHandlersInfo* handlers =
FileHandlers::GetFileHandlers(extension.get());
ASSERT_TRUE(handlers != nullptr);
ASSERT_EQ(3U, handlers->size());
apps::FileHandlerInfo handler = handlers->at(0);
EXPECT_EQ("directories", handler.id);
EXPECT_EQ(0U, handler.types.size());
EXPECT_EQ(1U, handler.extensions.size());
EXPECT_EQ(1U, handler.extensions.count("*/*"));
EXPECT_EQ(true, handler.include_directories);
handler = handlers->at(1);
EXPECT_EQ("image", handler.id);
EXPECT_EQ(1U, handler.types.size());
EXPECT_EQ(1U, handler.types.count("image/*"));
EXPECT_EQ(2U, handler.extensions.size());
EXPECT_EQ(1U, handler.extensions.count(".png"));
EXPECT_EQ(1U, handler.extensions.count(".gif"));
EXPECT_EQ("add_to", handler.verb);
handler = handlers->at(2);
EXPECT_EQ("text", handler.id);
EXPECT_EQ(1U, handler.types.size());
EXPECT_EQ(1U, handler.types.count("text/*"));
EXPECT_EQ(0U, handler.extensions.size());
}
TEST_F(FileHandlersManifestTest, NotPlatformApp) {
// This should load successfully but have the file handlers ignored.
scoped_refptr<const Extension> extension =
LoadAndExpectSuccess("file_handlers_invalid_not_app.json");
ASSERT_TRUE(extension.get());
const FileHandlersInfo* handlers =
FileHandlers::GetFileHandlers(extension.get());
ASSERT_TRUE(handlers == nullptr);
}
class WebFileHandlersTest : public ManifestTest {
protected:
ManifestData GetManifestData(const char* manifest_part) {
static constexpr char kManifestStub[] =
R"({
"name": "Test",
"version": "0.0.1",
"manifest_version": 3,
"file_handlers": %s
})";
base::Value manifest_value =
base::test::ParseJson(base::StringPrintf(kManifestStub, manifest_part));
EXPECT_EQ(base::Value::Type::DICT, manifest_value.type());
return ManifestData(std::move(manifest_value).TakeDict());
}
};
// `file_handlers` examples.
TEST_F(WebFileHandlersTest, GeneralSuccess) {
struct {
const char* title;
const char* file_handler;
} test_cases[] = {
{
"Minimum required entry.",
R"([{
"name": "Comma separated values",
"action": "/open-csv",
"accept": {"text/csv": ".csv"}
}])",
},
{
"Succeed with a different launch type and multiple icon sizes.",
R"([{
"name": "Comma separated values",
"action": "/csv",
"accept": {"text/csv": ".csv"},
"icons": [{
"src": "/csv.png",
"sizes": "32x32 64x64",
"type": "image/png"
}],
"launch_type": "multiple-clients"
}])",
},
{
"Succeed loading the example provlided in the file handling spec.",
R"([
{
"action": "/open-csv",
"name": "Comma-separated Value",
"accept": {"text/csv": [ ".csv" ]},
"icons": [{
"src": "/csv-file.png",
"sizes": "144x144"
}]
},
{
"action": "/open-svg",
"name": "Image",
"accept": {"image/svg+xml": ".svg"},
"icons": [{
"src": "/svg-file.png",
"sizes": "144x144"
}]
},
{
"action": "/open-graf",
"name": "Grafr",
"accept": {
"application/vnd.grafr.graph": [".grafr", ".graf"],
"application/vnd.alternative-graph-app.graph": ".graph"
},
"launch_type": "multiple-clients"
}
])",
},
{
"Succeed with usage of all possible available keys.",
R"([{
"name": "Comma separated values",
"action": "/csv",
"accept": {"text/csv": ".csv"},
"icons": [{
"src": "/csv.png",
"sizes": "16x16",
"type": "image/png"
}],
"launch-type": "single-client"
}])",
},
{
"Succeed if `action` has a leading forward slash.",
R"([{
"name":"test",
"action":"/path",
"accept": {"text/csv": ".csv"}
}])",
},
};
for (const auto& test_case : test_cases) {
SCOPED_TRACE(test_case.title);
// Load extension and get file handlers.
scoped_refptr<Extension> extension(LoadAndExpectSuccess(
std::move(GetManifestData(test_case.file_handler))));
auto* file_handlers = WebFileHandlers::GetFileHandlers(*extension.get());
ASSERT_TRUE(file_handlers);
// Exercise the web `file_handlers` key with a subkey introduced in MV3.
for (const auto& file_handler : *file_handlers) {
EXPECT_TRUE(file_handler.file_handler.action.size() > 0);
}
}
}
// General usage verification.
TEST_F(WebFileHandlersTest, GeneralErrors) {
struct {
const char* title;
const char* file_handler;
const char* expected_error;
} test_cases[] = {
{
"Error when accept is missing.",
R"([{
"name": "Comma separated values",
"action": "/open-csv"
}])",
"Error at key 'file_handlers'. Parsing array failed at "
"index 0: 'accept' is required",
},
{
"Error when name is missing.",
R"([{
"action": "/open-csv",
"accept": {"text/csv": ".csv"}
}])",
"Error at key 'file_handlers'. Parsing array failed at "
"index 0: 'name' is required",
},
{
"Error when action is missing.",
R"([{
"name": "Comma separated values",
"accept": {"text/csv": ".csv"}
}])",
"Error at key 'file_handlers'. Parsing array failed at "
"index 0: 'action' is required",
},
{
"Error with an empty array.",
R"([])",
"Invalid value for 'file_handlers[0]'. At least one File "
"Handler must be present.",
},
{
"Error with an empty file handler.",
R"([{}])",
"Error at key 'file_handlers'. Parsing array failed at "
"index 0: 'accept' is required",
},
{
"Error with multiple missing file handlers.",
R"([{}, {}])",
"Error at key 'file_handlers'. Parsing array failed at "
"index 0: 'accept' is required",
},
{
"Error with an empty file handler.",
R"([{"accept": {"text/csv": ".csv"}}])",
"Error at key 'file_handlers'. Parsing array failed at "
"index 0: 'action' is required",
},
{
"Error if `name` is empty.",
R"([{
"name": "",
"action": "",
"accept": {"text/csv": ".csv"}
}])",
"Invalid value for 'file_handlers[0]'. `name` must have a value.",
},
{
"Error if `action` is empty.",
R"([{
"name": "test",
"action": "",
"accept": {"text/csv": ".csv"}
}])",
"Invalid value for 'file_handlers[0]'. `action` must have a value.",
},
{
"Error if `action` doesn't have a leading forward slash.",
R"([{
"name":"test",
"action":"path",
"accept": {"text/csv": ".csv"}
}])",
"Invalid value for 'file_handlers[0]'. `action` must "
"start with a forward slash.",
},
{
"Error if `launch_type` multiple-clients is singular.",
R"([{
"name":"test",
"action":"/path",
"accept": {"text/csv": ".csv"},
"launch_type": "multiple-client"
}])",
"Invalid value for 'file_handlers[0]'. `launch_type` must have a "
"valid value.",
}};
for (const auto& test_case : test_cases) {
SCOPED_TRACE(test_case.title);
LoadAndExpectError(GetManifestData(test_case.file_handler),
test_case.expected_error);
}
}
// `accept` property verification.
TEST_F(WebFileHandlersTest, AcceptErrors) {
struct {
const char* title;
const char* file_handler;
const char* expected_error;
} test_cases[] = {
{
"Error if `accept` has a file extension that's missing the leading "
"period.",
R"([{
"name": "test",
"action": "/csv",
"accept": {"text/csv": ["csv"]}
}])",
"Invalid value for 'file_handlers[0]'. `accept` file extension must "
"have a leading period.",
},
{
"Error if `accept` has a mime type in the wrong format.",
R"([{
"name": "test",
"action": "/csv",
"accept": {"csv": ".csv"}
}])",
"Invalid value for 'file_handlers[0]'. `accept` mime type "
"must have exactly one slash.",
},
{
"Error if `accept` has a mime type in the wrong format.",
R"([{
"name": "test",
"action": "/csv",
"accept": {"text//csv": ".csv"}
}])",
"Invalid value for 'file_handlers[0]'. `accept` mime type "
"must have exactly one slash.",
},
{
"`accept` must have the correct type to represent the file "
"extension.",
R"([{
"name": "test",
"action": "/csv",
"accept": {"text/csv": {}}
}])",
"Invalid value for 'file_handlers[0]'. `accept` must have "
"a valid file extension.",
},
{
"Error if `accept` is empty.",
R"([{
"name": "test",
"action": "/csv",
"accept": {}
}])",
"Invalid value for 'file_handlers[0]'. `accept` cannot be empty.",
},
{
"Error if `accept` is empty.",
R"([{
"name": "test",
"action": "/csv",
"accept": {"text/csv": []}
}])",
"Invalid value for 'file_handlers[0]'. `accept` file "
"extension must have a value.",
},
{
"Error if `accept` is empty.",
R"([{
"name": "test",
"action": "/csv",
"accept": {"text/csv": [""]}
}])",
"Invalid value for 'file_handlers[0]'. `accept` file "
"extension must have a value.",
}};
for (const auto& test_case : test_cases) {
SCOPED_TRACE(test_case.title);
LoadAndExpectError(GetManifestData(test_case.file_handler),
test_case.expected_error);
}
}
// `icon` property verification.
TEST_F(WebFileHandlersTest, IconErrors) {
struct {
const char* title;
const char* file_handler;
const char* expected_error;
} test_cases[] = {
{
"`icons.src` must have a value.",
R"([{
"name": "Comma separated values",
"action": "/csv",
"accept": {"text/csv": ".csv"},
"icons": [{"src": ""}]
}])",
"Invalid value for 'file_handlers[0]'. `icon.src` must have a value.",
},
{
"`icon.sizes` must have a value.",
R"([{
"name": "Comma separated values",
"action": "/csv",
"accept": {"text/csv": ".csv"},
"icons": [{
"src": "/csv.png",
"sizes": "",
"type": "image/png"
}]
}])",
"Invalid value for 'file_handlers[0]'. `icon.sizes` must have a "
"value.",
},
{
"`icon.sizes` must have width and height.",
R"([{
"name": "Comma separated values",
"action": "/csv",
"accept": {"text/csv": ".csv"},
"icons": [{
"src": "/csv.png",
"sizes": "16",
"type": "image/png"
}]
}])",
"Invalid value for 'file_handlers[0]'. `icon.sizes` must "
"have width and height.",
},
{
"`icon.sizes` dimensions must be digits.",
R"([{
"name": "Comma separated values",
"action": "/csv",
"accept": {"text/csv": ".csv"},
"icons": [{
"src": "/csv.png",
"sizes": "a1x2",
"type": "image/png"
}]
}])",
"Invalid value for 'file_handlers[0]'. `icon.sizes` "
"dimensions must be digits.",
}};
for (const auto& test_case : test_cases) {
SCOPED_TRACE(test_case.title);
LoadAndExpectError(GetManifestData(test_case.file_handler),
test_case.expected_error);
}
}
// TODO(crbug.com/40169582): Add tests for MV2, MV3, and missing the flag.
// crrev.com/c/4215992/comment/5c5148e7_2b24c9d3
} // namespace extensions
|