Clark DuVall | e2bdd33 | 2019-08-07 18:32:13 | [diff] [blame] | 1 | // Copyright 2019 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. |
| 4 | |
| 5 | #include "extensions/browser/extension_util.h" |
| 6 | |
| 7 | #include "base/path_service.h" |
| 8 | #include "extensions/common/extension_builder.h" |
| 9 | #include "extensions/common/extension_paths.h" |
| 10 | #include "extensions/common/extension_set.h" |
| 11 | #include "testing/gtest/include/gtest/gtest.h" |
| 12 | |
| 13 | namespace extensions { |
| 14 | namespace { |
| 15 | // Returns a barebones test Extension object with the given name. |
| 16 | static scoped_refptr<const Extension> CreateExtension(const std::string& name) { |
| 17 | base::FilePath path; |
| 18 | base::PathService::Get(DIR_TEST_DATA, &path); |
| 19 | |
| 20 | return ExtensionBuilder(name).SetPath(path.AppendASCII(name)).Build(); |
| 21 | } |
| 22 | } // namespace |
| 23 | |
| 24 | // Tests that extension URLs are properly mapped to local file paths. |
| 25 | TEST(ExtensionUtilTest, MapUrlToLocalFilePath) { |
| 26 | scoped_refptr<const Extension> app(CreateExtension("platform_app")); |
| 27 | ExtensionSet extensions; |
| 28 | extensions.Insert(app); |
| 29 | |
| 30 | // Non-extension URLs don't map to anything. |
| 31 | base::FilePath non_extension_path; |
| 32 | GURL non_extension_url("http://not-an-extension.com/"); |
| 33 | EXPECT_FALSE(util::MapUrlToLocalFilePath(&extensions, non_extension_url, |
| 34 | false, &non_extension_path)); |
| 35 | EXPECT_TRUE(non_extension_path.empty()); |
| 36 | |
| 37 | // Valid resources return a valid path. |
| 38 | base::FilePath valid_path; |
| 39 | GURL valid_url = app->GetResourceURL("manifest.json"); |
| 40 | EXPECT_TRUE(util::MapUrlToLocalFilePath( |
| 41 | &extensions, valid_url, true /* use_blocking_api */, &valid_path)); |
| 42 | EXPECT_FALSE(valid_path.empty()); |
| 43 | |
| 44 | // A file must exist to be mapped to a path using the blocking API. |
| 45 | base::FilePath does_not_exist_path; |
| 46 | GURL does_not_exist_url = app->GetResourceURL("does-not-exist.html"); |
| 47 | EXPECT_FALSE(util::MapUrlToLocalFilePath(&extensions, does_not_exist_url, |
| 48 | true /* use_blocking_api */, |
| 49 | &does_not_exist_path)); |
| 50 | EXPECT_TRUE(does_not_exist_path.empty()); |
| 51 | |
| 52 | // A file does not need to exist to be mapped to a path with the non-blocking |
| 53 | // API. This avoids hitting the disk to see if it exists. |
| 54 | EXPECT_TRUE(util::MapUrlToLocalFilePath(&extensions, does_not_exist_url, |
| 55 | false /* use_blocking_api */, |
| 56 | &does_not_exist_path)); |
| 57 | EXPECT_FALSE(does_not_exist_path.empty()); |
| 58 | } |
| 59 | } // namespace extensions |